mainwindow_moc.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <QDir>
  14. #include "../lib/CConfigHandler.h"
  15. #include "../lib/VCMIDirs.h"
  16. #include "../lib/Languages.h"
  17. #include "../lib/filesystem/Filesystem.h"
  18. #include "../lib/logging/CBasicLogConfigurator.h"
  19. #include "updatedialog_moc.h"
  20. #include "main.h"
  21. #include "helper.h"
  22. void MainWindow::load()
  23. {
  24. // Set current working dir to executable folder.
  25. // This is important on Mac for relative paths to work inside DMG.
  26. QDir::setCurrent(QApplication::applicationDirPath());
  27. #ifndef VCMI_MOBILE
  28. console = new CConsoleHandler();
  29. #endif
  30. CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Launcher_log.txt", console);
  31. logConfig.configureDefault();
  32. CResourceHandler::initialize();
  33. CResourceHandler::load("config/filesystem.json");
  34. Helper::loadSettings();
  35. }
  36. void MainWindow::computeSidePanelSizes()
  37. {
  38. QVector<QToolButton*> widgets = {
  39. ui->modslistButton,
  40. ui->settingsButton,
  41. ui->aboutButton,
  42. ui->startEditorButton,
  43. ui->startGameButton
  44. };
  45. for(auto & widget : widgets)
  46. {
  47. QFontMetrics metrics(widget->font());
  48. QSize iconSize = widget->iconSize();
  49. // this is minimal space that is needed for our button to avoid text clipping
  50. int buttonHeight = iconSize.height() + metrics.height() + 4;
  51. widget->setMinimumHeight(buttonHeight);
  52. widget->setMaximumHeight(buttonHeight * 1.2);
  53. }
  54. }
  55. MainWindow::MainWindow(QWidget * parent)
  56. : QMainWindow(parent), ui(new Ui::MainWindow)
  57. {
  58. load(); // load FS before UI
  59. bool setupCompleted = settings["launcher"]["setupCompleted"].Bool();
  60. if (!setupCompleted)
  61. detectPreferredLanguage();
  62. updateTranslation(); // load translation
  63. ui->setupUi(this);
  64. setWindowIcon(QIcon{":/icons/menu-game.png"});
  65. ui->modslistButton->setIcon(QIcon{":/icons/menu-mods.png"});
  66. ui->settingsButton->setIcon(QIcon{":/icons/menu-settings.png"});
  67. ui->aboutButton->setIcon(QIcon{":/icons/about-project.png"});
  68. ui->startEditorButton->setIcon(QIcon{":/icons/menu-editor.png"});
  69. ui->startGameButton->setIcon(QIcon{":/icons/menu-game.png"});
  70. #ifndef VCMI_MOBILE
  71. //load window settings
  72. QSettings s(Ui::teamName, Ui::appName);
  73. auto size = s.value("MainWindow/Size").toSize();
  74. if(size.isValid())
  75. {
  76. resize(size);
  77. }
  78. auto position = s.value("MainWindow/Position").toPoint();
  79. if(!position.isNull())
  80. {
  81. move(position);
  82. }
  83. #endif
  84. #ifndef ENABLE_EDITOR
  85. ui->startEditorButton->hide();
  86. #endif
  87. computeSidePanelSizes();
  88. bool h3DataFound = CResourceHandler::get()->existsResource(ResourcePath("DATA/GENRLTXT.TXT"));
  89. if (h3DataFound && setupCompleted)
  90. ui->tabListWidget->setCurrentIndex(TabRows::MODS);
  91. else
  92. enterSetup();
  93. ui->settingsView->setDisplayList();
  94. if(settings["launcher"]["updateOnStartup"].Bool())
  95. UpdateDialog::showUpdateDialog(false);
  96. }
  97. void MainWindow::detectPreferredLanguage()
  98. {
  99. auto preferredLanguages = QLocale::system().uiLanguages();
  100. std::string selectedLanguage;
  101. for (auto const & userLang : preferredLanguages)
  102. {
  103. logGlobal->info("Preferred language: %s", userLang.toStdString());
  104. for (auto const & vcmiLang : Languages::getLanguageList())
  105. if (vcmiLang.tagIETF == userLang.toStdString())
  106. selectedLanguage = vcmiLang.identifier;
  107. if (!selectedLanguage.empty())
  108. {
  109. logGlobal->info("Selected language: %s", selectedLanguage);
  110. Settings node = settings.write["general"]["language"];
  111. node->String() = selectedLanguage;
  112. return;
  113. }
  114. }
  115. }
  116. void MainWindow::enterSetup()
  117. {
  118. ui->startGameButton->setEnabled(false);
  119. ui->startEditorButton->setEnabled(false);
  120. ui->settingsButton->setEnabled(false);
  121. ui->aboutButton->setEnabled(false);
  122. ui->modslistButton->setEnabled(false);
  123. ui->tabListWidget->setCurrentIndex(TabRows::SETUP);
  124. }
  125. void MainWindow::exitSetup()
  126. {
  127. Settings writer = settings.write["launcher"]["setupCompleted"];
  128. writer->Bool() = true;
  129. ui->startGameButton->setEnabled(true);
  130. ui->startEditorButton->setEnabled(true);
  131. ui->settingsButton->setEnabled(true);
  132. ui->aboutButton->setEnabled(true);
  133. ui->modslistButton->setEnabled(true);
  134. ui->tabListWidget->setCurrentIndex(TabRows::MODS);
  135. }
  136. void MainWindow::switchToModsTab()
  137. {
  138. ui->startGameButton->setEnabled(true);
  139. ui->tabListWidget->setCurrentIndex(TabRows::MODS);
  140. }
  141. void MainWindow::changeEvent(QEvent *event)
  142. {
  143. if(event->type() == QEvent::LanguageChange)
  144. {
  145. ui->retranslateUi(this);
  146. }
  147. QMainWindow::changeEvent(event);
  148. }
  149. MainWindow::~MainWindow()
  150. {
  151. #ifndef VCMI_MOBILE
  152. //save window settings
  153. QSettings s(Ui::teamName, Ui::appName);
  154. s.setValue("MainWindow/Size", size());
  155. s.setValue("MainWindow/Position", pos());
  156. #endif
  157. delete ui;
  158. }
  159. void MainWindow::on_startGameButton_clicked()
  160. {
  161. startGame({});
  162. }
  163. void MainWindow::on_startEditorButton_clicked()
  164. {
  165. startEditor({});
  166. }
  167. const CModList & MainWindow::getModList() const
  168. {
  169. return ui->modlistView->getModList();
  170. }
  171. CModListView * MainWindow::getModView()
  172. {
  173. return ui->modlistView;
  174. }
  175. void MainWindow::on_modslistButton_clicked()
  176. {
  177. switchToModsTab();
  178. }
  179. void MainWindow::on_settingsButton_clicked()
  180. {
  181. ui->startGameButton->setEnabled(true);
  182. ui->tabListWidget->setCurrentIndex(TabRows::SETTINGS);
  183. }
  184. void MainWindow::on_aboutButton_clicked()
  185. {
  186. ui->startGameButton->setEnabled(true);
  187. ui->tabListWidget->setCurrentIndex(TabRows::ABOUT);
  188. }
  189. void MainWindow::updateTranslation()
  190. {
  191. #ifdef ENABLE_QT_TRANSLATIONS
  192. const std::string translationFile = settings["general"]["language"].String() + ".qm";
  193. logGlobal->info("Loading translation '%s'", translationFile);
  194. if (!translator.load(QString{":/translation/%1"}.arg(translationFile.c_str())))
  195. {
  196. logGlobal->error("Failed to load translation");
  197. return;
  198. }
  199. if (!qApp->installTranslator(&translator))
  200. logGlobal->error("Failed to install translator");
  201. #endif
  202. }