csettingsview_moc.cpp 16 KB

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