mainwindow_moc.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * mainwindow_moc.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "mainwindow_moc.h"
  12. #include "ui_mainwindow_moc.h"
  13. #include <QProcess>
  14. #include <QDir>
  15. #include "../lib/CConfigHandler.h"
  16. #include "../lib/VCMIDirs.h"
  17. #include "../lib/filesystem/Filesystem.h"
  18. #include "../lib/logging/CBasicLogConfigurator.h"
  19. #include "updatedialog_moc.h"
  20. void MainWindow::load()
  21. {
  22. // Set current working dir to executable folder.
  23. // This is important on Mac for relative paths to work inside DMG.
  24. QDir::setCurrent(QApplication::applicationDirPath());
  25. console = new CConsoleHandler();
  26. CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", console);
  27. logConfig.configureDefault();
  28. CResourceHandler::initialize();
  29. CResourceHandler::load("config/filesystem.json");
  30. #ifdef Q_OS_IOS
  31. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().binaryPath() / "icons"));
  32. #else
  33. for(auto & string : VCMIDirs::get().dataPaths())
  34. QDir::addSearchPath("icons", pathToQString(string / "launcher" / "icons"));
  35. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().userDataPath() / "launcher" / "icons"));
  36. #endif
  37. settings.init();
  38. }
  39. MainWindow::MainWindow(QWidget * parent)
  40. : QMainWindow(parent), ui(new Ui::MainWindow)
  41. {
  42. load(); // load FS before UI
  43. ui->setupUi(this);
  44. //load window settings
  45. QSettings s(Ui::teamName, Ui::appName);
  46. auto size = s.value("MainWindow/Size").toSize();
  47. if(size.isValid())
  48. {
  49. resize(size);
  50. }
  51. auto position = s.value("MainWindow/Position").toPoint();
  52. if(!position.isNull())
  53. {
  54. move(position);
  55. }
  56. //set default margins
  57. auto width = ui->startGameTitle->fontMetrics().boundingRect(ui->startGameTitle->text()).width();
  58. if(ui->startGameButton->iconSize().width() < width)
  59. {
  60. ui->startGameButton->setIconSize(QSize(width, width));
  61. }
  62. auto tab_icon_size = ui->tabSelectList->iconSize();
  63. if(tab_icon_size.width() < width)
  64. {
  65. ui->tabSelectList->setIconSize(QSize(width, width + tab_icon_size.height() - tab_icon_size.width()));
  66. ui->tabSelectList->setGridSize(QSize(width, width));
  67. // 4 is a dirty hack to make it look right
  68. ui->tabSelectList->setMaximumWidth(width + 4);
  69. }
  70. ui->tabListWidget->setCurrentIndex(0);
  71. ui->settingsView->setDisplayList();
  72. connect(ui->tabSelectList, SIGNAL(currentRowChanged(int)),
  73. ui->tabListWidget, SLOT(setCurrentIndex(int)));
  74. if(settings["launcher"]["updateOnStartup"].Bool())
  75. UpdateDialog::showUpdateDialog(false);
  76. }
  77. MainWindow::~MainWindow()
  78. {
  79. //save window settings
  80. QSettings s(Ui::teamName, Ui::appName);
  81. s.setValue("MainWindow/Size", size());
  82. s.setValue("MainWindow/Position", pos());
  83. delete ui;
  84. }
  85. void MainWindow::on_startGameButton_clicked()
  86. {
  87. #ifdef Q_OS_IOS
  88. qApp->quit();
  89. #else
  90. startExecutable(pathToQString(VCMIDirs::get().clientPath()));
  91. #endif
  92. }
  93. #ifndef Q_OS_IOS
  94. void MainWindow::startExecutable(QString name)
  95. {
  96. QProcess process;
  97. // Start the executable
  98. if(process.startDetached(name, {}))
  99. {
  100. close(); // exit launcher
  101. }
  102. else
  103. {
  104. QMessageBox::critical(this,
  105. "Error starting executable",
  106. "Failed to start " + name + "\n"
  107. "Reason: " + process.errorString(),
  108. QMessageBox::Ok,
  109. QMessageBox::Ok);
  110. }
  111. }
  112. #endif