mainwindow_moc.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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), ui(new Ui::MainWindow)
  36. {
  37. load(); // load FS before UI
  38. ui->setupUi(this);
  39. auto width = ui->startGameTitle->fontMetrics().boundingRect(ui->startGameTitle->text()).width();
  40. if(ui->startGameButton->iconSize().width() < width)
  41. {
  42. ui->startGameButton->setIconSize(QSize(width, width));
  43. }
  44. auto tab_icon_size = ui->tabSelectList->iconSize();
  45. if(tab_icon_size.width() < width)
  46. {
  47. ui->tabSelectList->setIconSize(QSize(width, width + tab_icon_size.height() - tab_icon_size.width()));
  48. ui->tabSelectList->setGridSize(QSize(width, width));
  49. // 4 is a dirty hack to make it look right
  50. ui->tabSelectList->setMaximumWidth(width + 4);
  51. }
  52. ui->tabListWidget->setCurrentIndex(0);
  53. ui->settingsView->setDisplayList();
  54. connect(ui->tabSelectList, SIGNAL(currentRowChanged(int)),
  55. ui->tabListWidget, SLOT(setCurrentIndex(int)));
  56. }
  57. MainWindow::~MainWindow()
  58. {
  59. delete ui;
  60. }
  61. void MainWindow::on_startGameButton_clicked()
  62. {
  63. startExecutable(pathToQString(VCMIDirs::get().clientPath()));
  64. }
  65. void MainWindow::startExecutable(QString name)
  66. {
  67. QProcess process;
  68. // Start the executable
  69. if(process.startDetached(name))
  70. {
  71. close(); // exit launcher
  72. }
  73. else
  74. {
  75. QMessageBox::critical(this,
  76. "Error starting executable",
  77. "Failed to start " + name + "\n"
  78. "Reason: " + process.errorString(),
  79. QMessageBox::Ok,
  80. QMessageBox::Ok);
  81. return;
  82. }
  83. }