csettingsview_moc.cpp 17 KB

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