mainwindow_moc.cpp 3.2 KB

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