mainwindow_moc.cpp 4.1 KB

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