mainwindow_moc.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. void MainWindow::load()
  20. {
  21. // Set current working dir to executable folder.
  22. // This is important on Mac for relative paths to work inside DMG.
  23. QDir::setCurrent(QApplication::applicationDirPath());
  24. console = new CConsoleHandler();
  25. CBasicLogConfigurator logConfig(VCMIDirs::get().userCachePath() / "VCMI_Launcher_log.txt", console);
  26. logConfig.configureDefault();
  27. CResourceHandler::initialize();
  28. CResourceHandler::load("config/filesystem.json");
  29. for (auto & string : VCMIDirs::get().dataPaths())
  30. QDir::addSearchPath("icons", pathToQString(string / "launcher" / "icons"));
  31. QDir::addSearchPath("icons", pathToQString(VCMIDirs::get().userDataPath() / "launcher" / "icons"));
  32. settings.init();
  33. }
  34. MainWindow::MainWindow(QWidget * parent) :
  35. QMainWindow(parent),
  36. ui(new Ui::MainWindow)
  37. {
  38. load(); // load FS before UI
  39. ui->setupUi(this);
  40. auto width = ui->startGameTitle->fontMetrics().boundingRect(ui->startGameTitle->text()).width();
  41. if (ui->startGameButton->iconSize().width() < width)
  42. {
  43. ui->startGameButton->setIconSize(QSize(width, width));
  44. }
  45. auto tab_icon_size = ui->tabSelectList->iconSize();
  46. if (tab_icon_size.width() < width)
  47. {
  48. ui->tabSelectList->setIconSize(QSize(
  49. width,
  50. width + tab_icon_size.height() - tab_icon_size.width()));
  51. ui->tabSelectList->setGridSize(QSize(width, width));
  52. // 4 is a dirty hack to make it look right
  53. ui->tabSelectList->setMaximumWidth(width + 4);
  54. }
  55. ui->tabListWidget->setCurrentIndex(0);
  56. ui->settingsView->setDisplayList();
  57. connect(ui->tabSelectList, SIGNAL(currentRowChanged(int)),
  58. ui->tabListWidget, SLOT(setCurrentIndex(int)));
  59. }
  60. MainWindow::~MainWindow()
  61. {
  62. delete ui;
  63. }
  64. void MainWindow::on_startGameButton_clicked()
  65. {
  66. startExecutable(pathToQString(VCMIDirs::get().clientPath()));
  67. }
  68. void MainWindow::startExecutable(QString name)
  69. {
  70. QProcess process;
  71. // Start the executable
  72. if (process.startDetached(name))
  73. {
  74. close(); // exit launcher
  75. }
  76. else
  77. {
  78. QMessageBox::critical(this,
  79. "Error starting executable",
  80. "Failed to start " + name + "\n"
  81. "Reason: " + process.errorString(),
  82. QMessageBox::Ok,
  83. QMessageBox::Ok);
  84. return;
  85. }
  86. }