mainwindow_moc.cpp 4.2 KB

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