csettingsview_moc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <QFileInfo>
  19. #include <QGuiApplication>
  20. #include "../../lib/CConfigHandler.h"
  21. #ifndef VCMI_MOBILE
  22. #include <SDL2/SDL.h>
  23. #endif
  24. namespace
  25. {
  26. QString resolutionToString(const QSize & resolution)
  27. {
  28. return QString{"%1x%2"}.arg(resolution.width()).arg(resolution.height());
  29. }
  30. static const std::string cursorTypesList[] =
  31. {
  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 VCMI_MOBILE
  59. ui->comboBoxFullScreen->hide();
  60. ui->labelFullScreen->hide();
  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. fillValidScalingRange();
  68. ui->spinBoxInterfaceScaling->setValue(settings["video"]["resolution"]["scaling"].Float());
  69. ui->spinBoxFramerateLimit->setValue(settings["video"]["targetfps"].Float());
  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->comboBoxEnemyPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
  74. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  75. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  76. ui->lineEditRepositoryDefault->setText(QString::fromStdString(settings["launcher"]["defaultRepositoryURL"].String()));
  77. ui->lineEditRepositoryExtra->setText(QString::fromStdString(settings["launcher"]["extraRepositoryURL"].String()));
  78. ui->lineEditRepositoryDefault->setEnabled(settings["launcher"]["defaultRepositoryEnabled"].Bool());
  79. ui->lineEditRepositoryExtra->setEnabled(settings["launcher"]["extraRepositoryEnabled"].Bool());
  80. ui->checkBoxRepositoryDefault->setChecked(settings["launcher"]["defaultRepositoryEnabled"].Bool());
  81. ui->checkBoxRepositoryExtra->setChecked(settings["launcher"]["extraRepositoryEnabled"].Bool());
  82. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  83. ui->spinBoxAutoSaveLimit->setValue(settings["general"]["autosaveCountLimit"].Integer());
  84. ui->checkBoxAutoSavePrefix->setChecked(settings["general"]["useSavePrefix"].Bool());
  85. ui->lineEditAutoSavePrefix->setText(QString::fromStdString(settings["general"]["savePrefix"].String()));
  86. ui->lineEditAutoSavePrefix->setEnabled(settings["general"]["useSavePrefix"].Bool());
  87. Languages::fillLanguages(ui->comboBoxLanguage, false);
  88. std::string cursorType = settings["video"]["cursor"].String();
  89. size_t cursorTypeIndex = boost::range::find(cursorTypesList, cursorType) - cursorTypesList;
  90. ui->comboBoxCursorType->setCurrentIndex((int)cursorTypeIndex);
  91. }
  92. void CSettingsView::fillValidResolutions()
  93. {
  94. fillValidResolutionsForScreen(ui->comboBoxDisplayIndex->isVisible() ? ui->comboBoxDisplayIndex->currentIndex() : 0);
  95. }
  96. QSize CSettingsView::getPreferredRenderingResolution()
  97. {
  98. #ifndef VCMI_MOBILE
  99. bool fullscreen = settings["video"]["fullscreen"].Bool();
  100. bool realFullscreen = settings["video"]["realFullscreen"].Bool();
  101. if (!fullscreen || realFullscreen)
  102. {
  103. int resX = settings["video"]["resolution"]["width"].Integer();
  104. int resY = settings["video"]["resolution"]["height"].Integer();
  105. return QSize(resX, resY);
  106. }
  107. #endif
  108. return QApplication::primaryScreen()->geometry().size() * QApplication::primaryScreen()->devicePixelRatio();
  109. }
  110. void CSettingsView::fillValidScalingRange()
  111. {
  112. //FIXME: this code is copy of ScreenHandler::getSupportedScalingRange
  113. // H3 resolution, any resolution smaller than that is not correctly supported
  114. static const QSize minResolution = {800, 600};
  115. // arbitrary limit on *downscaling*. Allow some downscaling, if requested by user. Should be generally limited to 100+ for all but few devices
  116. static const double minimalScaling = 50;
  117. QSize renderResolution = getPreferredRenderingResolution();
  118. double maximalScalingWidth = 100.0 * renderResolution.width() / minResolution.width();
  119. double maximalScalingHeight = 100.0 * renderResolution.height() / minResolution.height();
  120. double maximalScaling = std::min(maximalScalingWidth, maximalScalingHeight);
  121. ui->spinBoxInterfaceScaling->setRange(minimalScaling, maximalScaling);
  122. }
  123. #ifndef VCMI_MOBILE
  124. static QVector<QSize> findAvailableResolutions(int displayIndex)
  125. {
  126. // Ugly workaround since we don't actually need SDL in Launcher
  127. // However Qt at the moment provides no way to query list of available resolutions
  128. QVector<QSize> result;
  129. SDL_Init(SDL_INIT_VIDEO);
  130. int modesCount = SDL_GetNumDisplayModes(displayIndex);
  131. for (int i =0; i < modesCount; ++i)
  132. {
  133. SDL_DisplayMode mode;
  134. if (SDL_GetDisplayMode(displayIndex, i, &mode) != 0)
  135. continue;
  136. QSize resolution(mode.w, mode.h);
  137. result.push_back(resolution);
  138. }
  139. boost::range::sort(result, [](const auto & left, const auto & right)
  140. {
  141. return left.height() * left.width() < right.height() * right.width();
  142. });
  143. result.erase(boost::unique(result).end(), result.end());
  144. SDL_Quit();
  145. return result;
  146. }
  147. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  148. {
  149. ui->comboBoxResolution->blockSignals(true); // avoid saving wrong resolution after adding first item from the list
  150. ui->comboBoxResolution->clear();
  151. bool fullscreen = settings["video"]["fullscreen"].Bool();
  152. bool realFullscreen = settings["video"]["realFullscreen"].Bool();
  153. if (!fullscreen || realFullscreen)
  154. {
  155. QVector<QSize> resolutions = findAvailableResolutions(screenIndex);
  156. for(const auto & entry : resolutions)
  157. ui->comboBoxResolution->addItem(resolutionToString(entry));
  158. }
  159. else
  160. {
  161. ui->comboBoxResolution->addItem(resolutionToString(getPreferredRenderingResolution()));
  162. }
  163. ui->comboBoxResolution->setEnabled(ui->comboBoxResolution->count() > 1);
  164. int resX = settings["video"]["resolution"]["width"].Integer();
  165. int resY = settings["video"]["resolution"]["height"].Integer();
  166. int resIndex = ui->comboBoxResolution->findText(resolutionToString({resX, resY}));
  167. ui->comboBoxResolution->setCurrentIndex(resIndex);
  168. // if selected resolution no longer exists, force update value to the largest (last) resolution
  169. if(resIndex == -1)
  170. ui->comboBoxResolution->setCurrentIndex(ui->comboBoxResolution->count() - 1);
  171. ui->comboBoxResolution->blockSignals(false);
  172. }
  173. #else
  174. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  175. {
  176. // resolutions are not selectable on mobile platforms
  177. ui->comboBoxResolution->hide();
  178. ui->labelResolution->hide();
  179. }
  180. #endif
  181. CSettingsView::CSettingsView(QWidget * parent)
  182. : QWidget(parent), ui(new Ui::CSettingsView)
  183. {
  184. ui->setupUi(this);
  185. loadSettings();
  186. }
  187. CSettingsView::~CSettingsView()
  188. {
  189. delete ui;
  190. }
  191. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  192. {
  193. QStringList list = arg1.split("x");
  194. Settings node = settings.write["video"]["resolution"];
  195. node["width"].Float() = list[0].toInt();
  196. node["height"].Float() = list[1].toInt();
  197. fillValidResolutions();
  198. fillValidScalingRange();
  199. }
  200. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  201. {
  202. Settings nodeFullscreen = settings.write["video"]["fullscreen"];
  203. Settings nodeRealFullscreen = settings.write["video"]["realFullscreen"];
  204. nodeFullscreen->Bool() = (index != 0);
  205. nodeRealFullscreen->Bool() = (index == 2);
  206. fillValidResolutions();
  207. fillValidScalingRange();
  208. }
  209. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  210. {
  211. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  212. node->Bool() = index;
  213. }
  214. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  215. {
  216. Settings node = settings.write["video"];
  217. node["displayIndex"].Float() = index;
  218. fillValidResolutionsForScreen(index);
  219. }
  220. void CSettingsView::on_comboBoxFriendlyAI_currentTextChanged(const QString & arg1)
  221. {
  222. Settings node = settings.write["server"]["friendlyAI"];
  223. node->String() = arg1.toUtf8().data();
  224. }
  225. void CSettingsView::on_comboBoxNeutralAI_currentTextChanged(const QString & arg1)
  226. {
  227. Settings node = settings.write["server"]["neutralAI"];
  228. node->String() = arg1.toUtf8().data();
  229. }
  230. void CSettingsView::on_comboBoxEnemyAI_currentTextChanged(const QString & arg1)
  231. {
  232. Settings node = settings.write["server"]["enemyAI"];
  233. node->String() = arg1.toUtf8().data();
  234. }
  235. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  236. {
  237. Settings node = settings.write["server"]["port"];
  238. node->Float() = arg1;
  239. }
  240. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  241. {
  242. Settings node = settings.write["video"]["showIntro"];
  243. node->Bool() = index;
  244. }
  245. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  246. {
  247. Settings node = settings.write["general"]["saveFrequency"];
  248. node->Integer() = index;
  249. }
  250. void CSettingsView::on_comboBoxLanguage_currentIndexChanged(int index)
  251. {
  252. Settings node = settings.write["general"]["language"];
  253. QString selectedLanguage = ui->comboBoxLanguage->itemData(index).toString();
  254. node->String() = selectedLanguage.toStdString();
  255. if(auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow()))
  256. mainWindow->updateTranslation();
  257. }
  258. void CSettingsView::changeEvent(QEvent *event)
  259. {
  260. if(event->type() == QEvent::LanguageChange)
  261. {
  262. ui->retranslateUi(this);
  263. Languages::fillLanguages(ui->comboBoxLanguage, false);
  264. loadTranslation();
  265. }
  266. QWidget::changeEvent(event);
  267. }
  268. void CSettingsView::showEvent(QShowEvent * event)
  269. {
  270. loadTranslation();
  271. QWidget::showEvent(event);
  272. }
  273. void CSettingsView::on_comboBoxCursorType_currentIndexChanged(int index)
  274. {
  275. Settings node = settings.write["video"]["cursor"];
  276. node->String() = cursorTypesList[index];
  277. }
  278. void CSettingsView::on_listWidgetSettings_currentRowChanged(int currentRow)
  279. {
  280. QVector<QWidget*> targetWidgets = {
  281. ui->labelGeneral,
  282. ui->labelVideo,
  283. ui->labelArtificialIntelligence,
  284. ui->labelRepositories
  285. };
  286. QWidget * currentTarget = targetWidgets[currentRow];
  287. // We want to scroll in a way that will put target widget in topmost visible position
  288. // To show not just header, but all settings in this group as well
  289. // In order to do that, let's scroll to the very bottom and the scroll back up until target widget is visible
  290. int maxPosition = ui->settingsScrollArea->verticalScrollBar()->maximum();
  291. ui->settingsScrollArea->verticalScrollBar()->setValue(maxPosition);
  292. ui->settingsScrollArea->ensureWidgetVisible(currentTarget, 5, 5);
  293. }
  294. void CSettingsView::loadTranslation()
  295. {
  296. Languages::fillLanguages(ui->comboBoxLanguageBase, true);
  297. QString baseLanguage = Languages::getHeroesDataLanguage();
  298. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  299. if (!mainWindow)
  300. return;
  301. QString languageName = QString::fromStdString(settings["general"]["language"].String());
  302. QString modName = mainWindow->getModView()->getTranslationModName(languageName);
  303. bool translationExists = !modName.isEmpty();
  304. bool translationNeeded = languageName != baseLanguage;
  305. bool showTranslation = translationNeeded && translationExists;
  306. ui->labelTranslation->setVisible(showTranslation);
  307. ui->labelTranslationStatus->setVisible(showTranslation);
  308. ui->pushButtonTranslation->setVisible(showTranslation);
  309. if (!translationExists || !translationNeeded)
  310. return;
  311. bool translationAvailable = mainWindow->getModView()->isModAvailable(modName);
  312. bool translationEnabled = mainWindow->getModView()->isModEnabled(modName);
  313. ui->pushButtonTranslation->setVisible(!translationEnabled);
  314. if (translationEnabled)
  315. {
  316. ui->labelTranslationStatus->setText(tr("Active"));
  317. }
  318. if (!translationEnabled && !translationAvailable)
  319. {
  320. ui->labelTranslationStatus->setText(tr("Disabled"));
  321. ui->pushButtonTranslation->setText(tr("Enable"));
  322. }
  323. if (translationAvailable)
  324. {
  325. ui->labelTranslationStatus->setText(tr("Not Installed"));
  326. ui->pushButtonTranslation->setText(tr("Install"));
  327. }
  328. }
  329. void CSettingsView::on_pushButtonTranslation_clicked()
  330. {
  331. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  332. assert(mainWindow);
  333. if (!mainWindow)
  334. return;
  335. QString languageName = QString::fromStdString(settings["general"]["language"].String());
  336. QString modName = mainWindow->getModView()->getTranslationModName(languageName);
  337. assert(!modName.isEmpty());
  338. if (modName.isEmpty())
  339. return;
  340. if (mainWindow->getModView()->isModAvailable(modName))
  341. {
  342. mainWindow->switchToModsTab();
  343. mainWindow->getModView()->doInstallMod(modName);
  344. }
  345. else
  346. {
  347. mainWindow->getModView()->enableModByName(modName);
  348. }
  349. }
  350. void CSettingsView::on_comboBoxLanguageBase_currentIndexChanged(int index)
  351. {
  352. Settings node = settings.write["general"]["gameDataLanguage"];
  353. QString selectedLanguage = ui->comboBoxLanguageBase->itemData(index).toString();
  354. node->String() = selectedLanguage.toStdString();
  355. }
  356. void CSettingsView::on_checkBoxRepositoryDefault_stateChanged(int arg1)
  357. {
  358. Settings node = settings.write["launcher"]["defaultRepositoryEnabled"];
  359. node->Bool() = arg1;
  360. ui->lineEditRepositoryDefault->setEnabled(arg1);
  361. }
  362. void CSettingsView::on_checkBoxRepositoryExtra_stateChanged(int arg1)
  363. {
  364. Settings node = settings.write["launcher"]["extraRepositoryEnabled"];
  365. node->Bool() = arg1;
  366. ui->lineEditRepositoryExtra->setEnabled(arg1);
  367. }
  368. void CSettingsView::on_lineEditRepositoryExtra_textEdited(const QString &arg1)
  369. {
  370. Settings node = settings.write["launcher"]["extraRepositoryURL"];
  371. node->String() = arg1.toStdString();
  372. }
  373. void CSettingsView::on_spinBoxInterfaceScaling_valueChanged(int arg1)
  374. {
  375. Settings node = settings.write["video"]["resolution"]["scaling"];
  376. node->Float() = arg1;
  377. }
  378. void CSettingsView::on_refreshRepositoriesButton_clicked()
  379. {
  380. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  381. assert(mainWindow);
  382. if (!mainWindow)
  383. return;
  384. mainWindow->getModView()->loadRepositories();
  385. }
  386. void CSettingsView::on_spinBoxFramerateLimit_valueChanged(int arg1)
  387. {
  388. Settings node = settings.write["video"]["targetfps"];
  389. node->Float() = arg1;
  390. }
  391. void CSettingsView::on_comboBoxEnemyPlayerAI_currentTextChanged(const QString &arg1)
  392. {
  393. Settings node = settings.write["server"]["playerAI"];
  394. node->String() = arg1.toUtf8().data();
  395. }
  396. void CSettingsView::on_comboBoxAlliedPlayerAI_currentTextChanged(const QString &arg1)
  397. {
  398. Settings node = settings.write["server"]["alliedAI"];
  399. node->String() = arg1.toUtf8().data();
  400. }
  401. void CSettingsView::on_checkBoxAutoSavePrefix_stateChanged(int arg1)
  402. {
  403. Settings node = settings.write["general"]["useSavePrefix"];
  404. node->Bool() = arg1;
  405. ui->lineEditAutoSavePrefix->setEnabled(arg1);
  406. }
  407. void CSettingsView::on_spinBoxAutoSaveLimit_valueChanged(int arg1)
  408. {
  409. Settings node = settings.write["general"]["autosaveCountLimit"];
  410. node->Float() = arg1;
  411. }
  412. void CSettingsView::on_lineEditAutoSavePrefix_textEdited(const QString &arg1)
  413. {
  414. Settings node = settings.write["general"]["savePrefix"];
  415. node->String() = arg1.toStdString();
  416. }