csettingsview_moc.cpp 13 KB

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