csettingsview_moc.cpp 13 KB

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