csettingsview_moc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * csettingsview_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 "csettingsview_moc.h"
  12. #include "ui_csettingsview_moc.h"
  13. #include "mainwindow_moc.h"
  14. #include "../modManager/cmodlistview_moc.h"
  15. #include "../jsonutils.h"
  16. #include "../languages.h"
  17. #include "../launcherdirs.h"
  18. #include "../updatedialog_moc.h"
  19. #include <QFileInfo>
  20. #include <QGuiApplication>
  21. #include "../../lib/CConfigHandler.h"
  22. #include "../../lib/VCMIDirs.h"
  23. namespace
  24. {
  25. QString resolutionToString(const QSize & resolution)
  26. {
  27. return QString{"%1x%2"}.arg(resolution.width()).arg(resolution.height());
  28. }
  29. static const std::string cursorTypesList[] =
  30. {
  31. "auto",
  32. "hardware",
  33. "software"
  34. };
  35. }
  36. void CSettingsView::setDisplayList()
  37. {
  38. QStringList list;
  39. for (const auto screen : QGuiApplication::screens())
  40. list << QString{"%1 - %2"}.arg(screen->name(), resolutionToString(screen->size()));
  41. if(list.count() < 2)
  42. {
  43. ui->comboBoxDisplayIndex->hide();
  44. ui->labelDisplayIndex->hide();
  45. fillValidResolutionsForScreen(0);
  46. }
  47. else
  48. {
  49. int displayIndex = settings["video"]["displayIndex"].Integer();
  50. ui->comboBoxDisplayIndex->addItems(list);
  51. // calls fillValidResolutions() in slot
  52. ui->comboBoxDisplayIndex->setCurrentIndex(displayIndex);
  53. }
  54. }
  55. void CSettingsView::loadSettings()
  56. {
  57. ui->comboBoxShowIntro->setCurrentIndex(settings["video"]["showIntro"].Bool());
  58. #ifdef Q_OS_IOS
  59. ui->comboBoxFullScreen->setCurrentIndex(1);
  60. ui->comboBoxFullScreen->setDisabled(true);
  61. #else
  62. if (settings["video"]["realFullscreen"].Bool())
  63. ui->comboBoxFullScreen->setCurrentIndex(2);
  64. else
  65. ui->comboBoxFullScreen->setCurrentIndex(settings["video"]["fullscreen"].Bool());
  66. #endif
  67. ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
  68. ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String()));
  69. ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String()));
  70. ui->comboBoxPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
  71. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  72. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  73. JsonNode urls = settings["launcher"]["repositoryURL"];
  74. ui->plainTextEditRepos->blockSignals(true); // Do not report loading as change of data
  75. ui->plainTextEditRepos->clear();
  76. for(auto entry : urls.Vector())
  77. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  78. ui->plainTextEditRepos->blockSignals(false);
  79. ui->lineEditUserDataDir->setText(pathToQString(VCMIDirs::get().userDataPath()));
  80. ui->lineEditGameDir->setText(pathToQString(VCMIDirs::get().binaryPath()));
  81. ui->lineEditTempDir->setText(pathToQString(VCMIDirs::get().userLogsPath()));
  82. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  83. Languages::fillLanguages(ui->comboBoxLanguage, false);
  84. std::string cursorType = settings["video"]["cursor"].String();
  85. size_t cursorTypeIndex = boost::range::find(cursorTypesList, cursorType) - cursorTypesList;
  86. ui->comboBoxCursorType->setCurrentIndex((int)cursorTypeIndex);
  87. }
  88. void CSettingsView::fillValidResolutions(bool isExtraResolutionsModEnabled)
  89. {
  90. this->isExtraResolutionsModEnabled = isExtraResolutionsModEnabled;
  91. fillValidResolutionsForScreen(ui->comboBoxDisplayIndex->isVisible() ? ui->comboBoxDisplayIndex->currentIndex() : 0);
  92. }
  93. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  94. {
  95. ui->comboBoxResolution->blockSignals(true); // avoid saving wrong resolution after adding first item from the list
  96. ui->comboBoxResolution->clear();
  97. // TODO: read available resolutions from all mods
  98. QVariantList resolutions;
  99. if(isExtraResolutionsModEnabled)
  100. {
  101. const auto extrasResolutionsPath = settings["launcher"]["extraResolutionsModPath"].String().c_str();
  102. const auto extrasResolutionsJson = JsonUtils::JsonFromFile(CLauncherDirs::get().modsPath() + extrasResolutionsPath);
  103. resolutions = extrasResolutionsJson.toMap().value(QLatin1String{"GUISettings"}).toList();
  104. }
  105. if(resolutions.isEmpty())
  106. {
  107. ui->comboBoxResolution->blockSignals(false);
  108. ui->comboBoxResolution->addItem(resolutionToString({800, 600}));
  109. return;
  110. }
  111. const auto screens = qGuiApp->screens();
  112. const auto currentScreen = screenIndex < screens.size() ? screens[screenIndex] : qGuiApp->primaryScreen();
  113. [[maybe_unused]] const auto screenSize = currentScreen->size();
  114. for(const auto & entry : resolutions)
  115. {
  116. const auto resolutionMap = entry.toMap().value(QLatin1String{"resolution"}).toMap();
  117. if(resolutionMap.isEmpty())
  118. continue;
  119. const auto widthValue = resolutionMap[QLatin1String{"x"}];
  120. const auto heightValue = resolutionMap[QLatin1String{"y"}];
  121. if(!widthValue.isValid() || !heightValue.isValid())
  122. continue;
  123. const QSize resolution{widthValue.toInt(), heightValue.toInt()};
  124. #ifndef VCMI_IOS
  125. if(screenSize.width() < resolution.width() || screenSize.height() < resolution.height())
  126. continue;
  127. #endif
  128. ui->comboBoxResolution->addItem(resolutionToString(resolution));
  129. }
  130. int resX = settings["video"]["resolution"]["width"].Integer();
  131. int resY = settings["video"]["resolution"]["height"].Integer();
  132. int resIndex = ui->comboBoxResolution->findText(resolutionToString({resX, resY}));
  133. ui->comboBoxResolution->setCurrentIndex(resIndex);
  134. ui->comboBoxResolution->blockSignals(false);
  135. // if selected resolution no longer exists, force update value to the first resolution
  136. if(resIndex == -1)
  137. ui->comboBoxResolution->setCurrentIndex(0);
  138. }
  139. CSettingsView::CSettingsView(QWidget * parent)
  140. : QWidget(parent), ui(new Ui::CSettingsView)
  141. {
  142. ui->setupUi(this);
  143. ui->lineEditBuildVersion->setText(QString::fromStdString(GameConstants::VCMI_VERSION));
  144. loadSettings();
  145. }
  146. CSettingsView::~CSettingsView()
  147. {
  148. delete ui;
  149. }
  150. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  151. {
  152. QStringList list = arg1.split("x");
  153. Settings node = settings.write["video"]["resolution"];
  154. node["width"].Float() = list[0].toInt();
  155. node["height"].Float() = list[1].toInt();
  156. }
  157. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  158. {
  159. Settings nodeFullscreen = settings.write["video"]["fullscreen"];
  160. Settings nodeRealFullscreen = settings.write["video"]["realFullscreen"];
  161. nodeFullscreen->Bool() = (index != 0);
  162. nodeRealFullscreen->Bool() = (index == 2);
  163. }
  164. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  165. {
  166. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  167. node->Bool() = index;
  168. }
  169. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  170. {
  171. Settings node = settings.write["video"];
  172. node["displayIndex"].Float() = index;
  173. fillValidResolutionsForScreen(index);
  174. }
  175. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  176. {
  177. Settings node = settings.write["server"]["playerAI"];
  178. node->String() = arg1.toUtf8().data();
  179. }
  180. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  181. {
  182. Settings node = settings.write["server"]["friendlyAI"];
  183. node->String() = arg1.toUtf8().data();
  184. }
  185. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  186. {
  187. Settings node = settings.write["server"]["neutralAI"];
  188. node->String() = arg1.toUtf8().data();
  189. }
  190. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  191. {
  192. Settings node = settings.write["server"]["enemyAI"];
  193. node->String() = arg1.toUtf8().data();
  194. }
  195. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  196. {
  197. Settings node = settings.write["server"]["port"];
  198. node->Float() = arg1;
  199. }
  200. void CSettingsView::on_plainTextEditRepos_textChanged()
  201. {
  202. Settings node = settings.write["launcher"]["repositoryURL"];
  203. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  204. node->Vector().clear();
  205. for(QString line : list)
  206. {
  207. if(line.trimmed().size() > 0)
  208. {
  209. JsonNode entry;
  210. entry.String() = line.trimmed().toUtf8().data();
  211. node->Vector().push_back(entry);
  212. }
  213. }
  214. }
  215. void CSettingsView::on_openTempDir_clicked()
  216. {
  217. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  218. }
  219. void CSettingsView::on_openUserDataDir_clicked()
  220. {
  221. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  222. }
  223. void CSettingsView::on_openGameDataDir_clicked()
  224. {
  225. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  226. }
  227. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  228. {
  229. Settings node = settings.write["video"]["showIntro"];
  230. node->Bool() = index;
  231. }
  232. void CSettingsView::on_changeGameDataDir_clicked()
  233. {
  234. }
  235. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  236. {
  237. Settings node = settings.write["general"]["saveFrequency"];
  238. node->Integer() = index;
  239. }
  240. void CSettingsView::on_updatesButton_clicked()
  241. {
  242. UpdateDialog::showUpdateDialog(true);
  243. }
  244. void CSettingsView::on_comboBoxLanguage_currentIndexChanged(int index)
  245. {
  246. Settings node = settings.write["general"]["language"];
  247. QString selectedLanguage = ui->comboBoxLanguage->itemData(index).toString();
  248. node->String() = selectedLanguage.toStdString();
  249. if(auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow()))
  250. mainWindow->updateTranslation();
  251. }
  252. void CSettingsView::changeEvent(QEvent *event)
  253. {
  254. if(event->type() == QEvent::LanguageChange)
  255. {
  256. ui->retranslateUi(this);
  257. Languages::fillLanguages(ui->comboBoxLanguage, false);
  258. loadTranslation();
  259. }
  260. QWidget::changeEvent(event);
  261. }
  262. void CSettingsView::showEvent(QShowEvent * event)
  263. {
  264. loadTranslation();
  265. QWidget::showEvent(event);
  266. }
  267. void CSettingsView::on_comboBoxCursorType_currentIndexChanged(int index)
  268. {
  269. Settings node = settings.write["video"]["cursor"];
  270. node->String() = cursorTypesList[index];
  271. }
  272. void CSettingsView::on_listWidgetSettings_currentRowChanged(int currentRow)
  273. {
  274. QVector<QWidget*> targetWidgets = {
  275. ui->labelGeneral,
  276. ui->labelVideo,
  277. ui->labelArtificialIntelligence,
  278. ui->labelDataDirs,
  279. ui->labelRepositories
  280. };
  281. QWidget * currentTarget = targetWidgets[currentRow];
  282. // We want to scroll in a way that will put target widget in topmost visible position
  283. // To show not just header, but all settings in this group as well
  284. // In order to do that, let's scroll to the very bottom and the scroll back up until target widget is visible
  285. int maxPosition = ui->settingsScrollArea->verticalScrollBar()->maximum();
  286. ui->settingsScrollArea->verticalScrollBar()->setValue(maxPosition);
  287. ui->settingsScrollArea->ensureWidgetVisible(currentTarget, 5, 5);
  288. }
  289. void CSettingsView::loadTranslation()
  290. {
  291. Languages::fillLanguages(ui->comboBoxLanguageBase, true);
  292. QString baseLanguage = Languages::getHeroesDataLanguage();
  293. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  294. if (!mainWindow)
  295. return;
  296. QString languageName = QString::fromStdString(settings["general"]["language"].String());
  297. QString modName = mainWindow->getModView()->getTranslationModName(languageName);
  298. bool translationExists = !modName.isEmpty();
  299. bool translationNeeded = languageName != baseLanguage;
  300. bool showTranslation = translationNeeded && translationExists;
  301. ui->labelTranslation->setVisible(showTranslation);
  302. ui->labelTranslationStatus->setVisible(showTranslation);
  303. ui->pushButtonTranslation->setVisible(showTranslation);
  304. if (!translationExists || !translationNeeded)
  305. return;
  306. bool translationAvailable = mainWindow->getModView()->isModAvailable(modName);
  307. bool translationEnabled = mainWindow->getModView()->isModEnabled(modName);
  308. ui->pushButtonTranslation->setVisible(!translationEnabled);
  309. if (translationEnabled)
  310. {
  311. ui->labelTranslationStatus->setText(tr("Active"));
  312. }
  313. if (!translationEnabled && !translationAvailable)
  314. {
  315. ui->labelTranslationStatus->setText(tr("Disabled"));
  316. ui->pushButtonTranslation->setText(tr("Enable"));
  317. }
  318. if (translationAvailable)
  319. {
  320. ui->labelTranslationStatus->setText(tr("Not Installed"));
  321. ui->pushButtonTranslation->setText(tr("Install"));
  322. }
  323. }
  324. void CSettingsView::on_pushButtonTranslation_clicked()
  325. {
  326. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  327. assert(mainWindow);
  328. if (!mainWindow)
  329. return;
  330. QString languageName = QString::fromStdString(settings["general"]["language"].String());
  331. QString modName = mainWindow->getModView()->getTranslationModName(languageName);
  332. assert(!modName.isEmpty());
  333. if (modName.isEmpty())
  334. return;
  335. if (mainWindow->getModView()->isModAvailable(modName))
  336. {
  337. mainWindow->switchToModsTab();
  338. mainWindow->getModView()->doInstallMod(modName);
  339. }
  340. else
  341. {
  342. mainWindow->getModView()->enableModByName(modName);
  343. }
  344. }
  345. void CSettingsView::on_comboBoxLanguageBase_currentIndexChanged(int index)
  346. {
  347. Settings node = settings.write["general"]["gameDataLanguage"];
  348. QString selectedLanguage = ui->comboBoxLanguageBase->itemData(index).toString();
  349. node->String() = selectedLanguage.toStdString();
  350. }