csettingsview_moc.cpp 18 KB

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