mainwindow_moc.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "main.h"
  21. #ifdef VCMI_IOS
  22. int __argc;
  23. char ** __argv;
  24. #endif
  25. void MainWindow::load()
  26. {
  27. // Set current working dir to executable folder.
  28. // This is important on Mac for relative paths to work inside DMG.
  29. QDir::setCurrent(QApplication::applicationDirPath());
  30. #ifndef VCMI_IOS
  31. console = new CConsoleHandler();
  32. #endif
  33. CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", console);
  34. logConfig.configureDefault();
  35. CResourceHandler::initialize();
  36. CResourceHandler::load("config/filesystem.json");
  37. #ifdef Q_OS_IOS
  38. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().binaryPath() / "icons"));
  39. #else
  40. for(auto & string : VCMIDirs::get().dataPaths())
  41. QDir::addSearchPath("icons", pathToQString(string / "launcher" / "icons"));
  42. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().userDataPath() / "launcher" / "icons"));
  43. #endif
  44. settings.init();
  45. }
  46. MainWindow::MainWindow(QWidget * parent)
  47. : QMainWindow(parent), ui(new Ui::MainWindow)
  48. {
  49. load(); // load FS before UI
  50. ui->setupUi(this);
  51. //load window settings
  52. QSettings s(Ui::teamName, Ui::appName);
  53. auto size = s.value("MainWindow/Size").toSize();
  54. if(size.isValid())
  55. {
  56. resize(size);
  57. }
  58. auto position = s.value("MainWindow/Position").toPoint();
  59. if(!position.isNull())
  60. {
  61. move(position);
  62. }
  63. //set default margins
  64. auto width = ui->startGameTitle->fontMetrics().boundingRect(ui->startGameTitle->text()).width();
  65. if(ui->startGameButton->iconSize().width() < width)
  66. {
  67. ui->startGameButton->setIconSize(QSize(width, width));
  68. }
  69. auto tab_icon_size = ui->tabSelectList->iconSize();
  70. if(tab_icon_size.width() < width)
  71. {
  72. ui->tabSelectList->setIconSize(QSize(width, width + tab_icon_size.height() - tab_icon_size.width()));
  73. ui->tabSelectList->setGridSize(QSize(width, width));
  74. // 4 is a dirty hack to make it look right
  75. ui->tabSelectList->setMaximumWidth(width + 4);
  76. }
  77. ui->tabListWidget->setCurrentIndex(0);
  78. ui->settingsView->isExtraResolutionsModEnabled = ui->stackedWidgetPage2->isExtraResolutionsModEnabled();
  79. ui->settingsView->setDisplayList();
  80. connect(ui->stackedWidgetPage2, &CModListView::extraResolutionsEnabledChanged,
  81. ui->settingsView, &CSettingsView::fillValidResolutions);
  82. connect(ui->tabSelectList, &QListWidget::currentRowChanged, [this](int i) {
  83. #ifdef Q_OS_IOS
  84. if(auto widget = qApp->focusWidget())
  85. widget->clearFocus();
  86. #endif
  87. ui->tabListWidget->setCurrentIndex(i);
  88. });
  89. if(settings["launcher"]["updateOnStartup"].Bool())
  90. UpdateDialog::showUpdateDialog(false);
  91. }
  92. MainWindow::~MainWindow()
  93. {
  94. //save window settings
  95. QSettings s(Ui::teamName, Ui::appName);
  96. s.setValue("MainWindow/Size", size());
  97. s.setValue("MainWindow/Position", pos());
  98. delete ui;
  99. }
  100. void MainWindow::startGame(const QStringList & args)
  101. {
  102. #ifdef Q_OS_IOS
  103. __argc = args.size() + 1; //first argument is omitted
  104. __argv = new char*[__argc];
  105. __argv[0] = "vcmiclient";
  106. for(int i = 1; i < __argc; ++i)
  107. {
  108. const char * s = args[i].toLocal8Bit().constData();
  109. __argv[i] = new char[strlen(s)];
  110. strcpy(__argv[i], s);
  111. }
  112. logGlobal->warn("Starting game with the arguments: %s", args.join(" ").toStdString());
  113. qApp->quit();
  114. #else
  115. startExecutable(pathToQString(VCMIDirs::get().clientPath()), args);
  116. #endif
  117. }
  118. void MainWindow::on_startGameButton_clicked()
  119. {
  120. startGame({});
  121. }
  122. #ifndef Q_OS_IOS
  123. void MainWindow::startExecutable(QString name, const QStringList & args)
  124. {
  125. QProcess process;
  126. // Start the executable
  127. if(process.startDetached(name, args))
  128. {
  129. close(); // exit launcher
  130. }
  131. else
  132. {
  133. QMessageBox::critical(this,
  134. "Error starting executable",
  135. "Failed to start " + name + "\n"
  136. "Reason: " + process.errorString(),
  137. QMessageBox::Ok,
  138. QMessageBox::Ok);
  139. }
  140. }
  141. #endif