mainwindow_moc.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/filesystem/Filesystem.h"
  17. #include "../lib/logging/CBasicLogConfigurator.h"
  18. #include "../lib/texts/Languages.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->startGameButton
  43. };
  44. for(auto & widget : widgets)
  45. {
  46. QFontMetrics metrics(widget->font());
  47. QSize iconSize = widget->iconSize();
  48. // this is minimal space that is needed for our button to avoid text clipping
  49. int buttonHeight = iconSize.height() + metrics.height() + 4;
  50. widget->setMinimumHeight(buttonHeight);
  51. widget->setMaximumHeight(buttonHeight * 1.2);
  52. }
  53. }
  54. MainWindow::MainWindow(QWidget * parent)
  55. : QMainWindow(parent), ui(new Ui::MainWindow)
  56. {
  57. load(); // load FS before UI
  58. bool setupCompleted = settings["launcher"]["setupCompleted"].Bool();
  59. if (!setupCompleted)
  60. detectPreferredLanguage();
  61. updateTranslation(); // load translation
  62. ui->setupUi(this);
  63. setWindowIcon(QIcon{":/icons/menu-game.png"});
  64. ui->modslistButton->setIcon(QIcon{":/icons/menu-mods.png"});
  65. ui->settingsButton->setIcon(QIcon{":/icons/menu-settings.png"});
  66. ui->aboutButton->setIcon(QIcon{":/icons/about-project.png"});
  67. ui->startGameButton->setIcon(QIcon{":/icons/menu-game.png"});
  68. #ifndef VCMI_MOBILE
  69. //load window settings
  70. QSettings s(Ui::teamName, Ui::appName);
  71. auto size = s.value("MainWindow/Size").toSize();
  72. if(size.isValid())
  73. {
  74. resize(size);
  75. }
  76. auto position = s.value("MainWindow/Position").toPoint();
  77. if(!position.isNull())
  78. {
  79. move(position);
  80. }
  81. #endif
  82. #ifndef ENABLE_EDITOR
  83. ui->startEditorButton->hide();
  84. #endif
  85. computeSidePanelSizes();
  86. bool h3DataFound = CResourceHandler::get()->existsResource(ResourcePath("DATA/GENRLTXT.TXT"));
  87. if (h3DataFound && setupCompleted)
  88. ui->tabListWidget->setCurrentIndex(TabRows::START);
  89. else
  90. enterSetup();
  91. ui->settingsView->setDisplayList();
  92. if(settings["launcher"]["updateOnStartup"].Bool())
  93. UpdateDialog::showUpdateDialog(false);
  94. }
  95. void MainWindow::detectPreferredLanguage()
  96. {
  97. auto preferredLanguages = QLocale::system().uiLanguages();
  98. std::string selectedLanguage;
  99. for (auto const & userLang : preferredLanguages)
  100. {
  101. logGlobal->info("Preferred language: %s", userLang.toStdString());
  102. for (auto const & vcmiLang : Languages::getLanguageList())
  103. if (vcmiLang.tagIETF == userLang.toStdString())
  104. selectedLanguage = vcmiLang.identifier;
  105. if (!selectedLanguage.empty())
  106. {
  107. logGlobal->info("Selected language: %s", selectedLanguage);
  108. Settings node = settings.write["general"]["language"];
  109. node->String() = selectedLanguage;
  110. return;
  111. }
  112. }
  113. }
  114. void MainWindow::enterSetup()
  115. {
  116. ui->startGameButton->setEnabled(false);
  117. ui->settingsButton->setEnabled(false);
  118. ui->aboutButton->setEnabled(false);
  119. ui->modslistButton->setEnabled(false);
  120. ui->tabListWidget->setCurrentIndex(TabRows::SETUP);
  121. }
  122. void MainWindow::exitSetup()
  123. {
  124. Settings writer = settings.write["launcher"]["setupCompleted"];
  125. writer->Bool() = true;
  126. ui->startGameButton->setEnabled(true);
  127. ui->settingsButton->setEnabled(true);
  128. ui->aboutButton->setEnabled(true);
  129. ui->modslistButton->setEnabled(true);
  130. ui->tabListWidget->setCurrentIndex(TabRows::MODS);
  131. }
  132. void MainWindow::switchToStartTab()
  133. {
  134. ui->startGameButton->setEnabled(true);
  135. ui->startGameButton->setChecked(true);
  136. ui->tabListWidget->setCurrentIndex(TabRows::START);
  137. }
  138. void MainWindow::switchToModsTab()
  139. {
  140. ui->startGameButton->setEnabled(true);
  141. ui->modslistButton->setChecked(true);
  142. ui->tabListWidget->setCurrentIndex(TabRows::MODS);
  143. }
  144. void MainWindow::changeEvent(QEvent * event)
  145. {
  146. if(event->type() == QEvent::LanguageChange)
  147. {
  148. ui->retranslateUi(this);
  149. }
  150. QMainWindow::changeEvent(event);
  151. }
  152. MainWindow::~MainWindow()
  153. {
  154. #ifndef VCMI_MOBILE
  155. //save window settings
  156. QSettings s(Ui::teamName, Ui::appName);
  157. s.setValue("MainWindow/Size", size());
  158. s.setValue("MainWindow/Position", pos());
  159. #endif
  160. delete ui;
  161. }
  162. void MainWindow::on_startGameButton_clicked()
  163. {
  164. switchToStartTab();
  165. }
  166. void MainWindow::on_startEditorButton_clicked()
  167. {
  168. hide();
  169. startEditor({});
  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. QString translationFileResourcePath = QString{":/translation/%1"}.arg(translationFile.c_str());
  194. logGlobal->info("Loading translation %s", translationFile);
  195. if(!QFile::exists(translationFileResourcePath))
  196. {
  197. logGlobal->debug("Translation file %s does not exist", translationFileResourcePath.toStdString());
  198. return;
  199. }
  200. if (!translator.load(translationFileResourcePath))
  201. {
  202. logGlobal->error("Failed to load translation file %s", translationFileResourcePath.toStdString());
  203. return;
  204. }
  205. if(translationFile == "english.qm")
  206. {
  207. // translator doesn't need to be installed for English
  208. return;
  209. }
  210. if (!qApp->installTranslator(&translator))
  211. {
  212. logGlobal->error("Failed to install translator for translation file %s", translationFileResourcePath.toStdString());
  213. }
  214. #endif
  215. }