mainwindow_moc.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef VCMI_IOS
  26. console = new CConsoleHandler();
  27. #endif
  28. CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", console);
  29. logConfig.configureDefault();
  30. CResourceHandler::initialize();
  31. CResourceHandler::load("config/filesystem.json");
  32. #ifdef Q_OS_IOS
  33. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().binaryPath() / "icons"));
  34. #else
  35. for(auto & string : VCMIDirs::get().dataPaths())
  36. QDir::addSearchPath("icons", pathToQString(string / "launcher" / "icons"));
  37. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().userDataPath() / "launcher" / "icons"));
  38. #endif
  39. settings.init();
  40. }
  41. MainWindow::MainWindow(QWidget * parent)
  42. : QMainWindow(parent), ui(new Ui::MainWindow)
  43. {
  44. load(); // load FS before UI
  45. ui->setupUi(this);
  46. //load window settings
  47. QSettings s(Ui::teamName, Ui::appName);
  48. auto size = s.value("MainWindow/Size").toSize();
  49. if(size.isValid())
  50. {
  51. resize(size);
  52. }
  53. auto position = s.value("MainWindow/Position").toPoint();
  54. if(!position.isNull())
  55. {
  56. move(position);
  57. }
  58. //set default margins
  59. auto width = ui->startGameTitle->fontMetrics().boundingRect(ui->startGameTitle->text()).width();
  60. if(ui->startGameButton->iconSize().width() < width)
  61. {
  62. ui->startGameButton->setIconSize(QSize(width, width));
  63. }
  64. auto tab_icon_size = ui->tabSelectList->iconSize();
  65. if(tab_icon_size.width() < width)
  66. {
  67. ui->tabSelectList->setIconSize(QSize(width, width + tab_icon_size.height() - tab_icon_size.width()));
  68. ui->tabSelectList->setGridSize(QSize(width, width));
  69. // 4 is a dirty hack to make it look right
  70. ui->tabSelectList->setMaximumWidth(width + 4);
  71. }
  72. ui->tabListWidget->setCurrentIndex(0);
  73. ui->settingsView->setDisplayList();
  74. connect(ui->tabSelectList, SIGNAL(currentRowChanged(int)),
  75. ui->tabListWidget, SLOT(setCurrentIndex(int)));
  76. if(settings["launcher"]["updateOnStartup"].Bool())
  77. UpdateDialog::showUpdateDialog(false);
  78. }
  79. MainWindow::~MainWindow()
  80. {
  81. //save window settings
  82. QSettings s(Ui::teamName, Ui::appName);
  83. s.setValue("MainWindow/Size", size());
  84. s.setValue("MainWindow/Position", pos());
  85. delete ui;
  86. }
  87. void MainWindow::on_startGameButton_clicked()
  88. {
  89. #ifdef Q_OS_IOS
  90. qApp->quit();
  91. #else
  92. startExecutable(pathToQString(VCMIDirs::get().clientPath()));
  93. #endif
  94. }
  95. #ifndef Q_OS_IOS
  96. void MainWindow::startExecutable(QString name)
  97. {
  98. QProcess process;
  99. // Start the executable
  100. if(process.startDetached(name, {}))
  101. {
  102. close(); // exit launcher
  103. }
  104. else
  105. {
  106. QMessageBox::critical(this,
  107. "Error starting executable",
  108. "Failed to start " + name + "\n"
  109. "Reason: " + process.errorString(),
  110. QMessageBox::Ok,
  111. QMessageBox::Ok);
  112. }
  113. }
  114. #endif