csettingsview_moc.cpp 16 KB

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