csettingsview_moc.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. ui->comboBoxResolution->blockSignals(true); // 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. ui->comboBoxResolution->blockSignals(false);
  193. }
  194. void CSettingsView::fillValidRenderers()
  195. {
  196. ui->comboBoxRendererType->blockSignals(true); // avoid saving wrong resolution after adding first item from the list
  197. ui->comboBoxRendererType->clear();
  198. auto driversList = getAvailableRenderingDrivers();
  199. ui->comboBoxRendererType->addItems(driversList);
  200. std::string rendererName = settings["video"]["driver"].String();
  201. int index = ui->comboBoxRendererType->findText(QString::fromStdString(rendererName));
  202. ui->comboBoxRendererType->setCurrentIndex(index);
  203. ui->comboBoxRendererType->blockSignals(false);
  204. }
  205. #else
  206. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  207. {
  208. // resolutions are not selectable on mobile platforms
  209. ui->comboBoxResolution->hide();
  210. ui->labelResolution->hide();
  211. }
  212. void CSettingsView::fillValidRenderers()
  213. {
  214. // untested on mobile platforms
  215. ui->comboBoxRendererType-hide();
  216. ui->labelRendererType-hide();
  217. }
  218. #endif
  219. CSettingsView::CSettingsView(QWidget * parent)
  220. : QWidget(parent), ui(new Ui::CSettingsView)
  221. {
  222. ui->setupUi(this);
  223. loadSettings();
  224. }
  225. CSettingsView::~CSettingsView()
  226. {
  227. delete ui;
  228. }
  229. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  230. {
  231. QStringList list = arg1.split("x");
  232. Settings node = settings.write["video"]["resolution"];
  233. node["width"].Float() = list[0].toInt();
  234. node["height"].Float() = list[1].toInt();
  235. fillValidResolutions();
  236. fillValidScalingRange();
  237. }
  238. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  239. {
  240. Settings nodeFullscreen = settings.write["video"]["fullscreen"];
  241. Settings nodeRealFullscreen = settings.write["video"]["realFullscreen"];
  242. nodeFullscreen->Bool() = (index != 0);
  243. nodeRealFullscreen->Bool() = (index == 2);
  244. fillValidResolutions();
  245. fillValidScalingRange();
  246. }
  247. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  248. {
  249. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  250. node->Bool() = index;
  251. }
  252. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  253. {
  254. Settings node = settings.write["video"];
  255. node["displayIndex"].Float() = index;
  256. fillValidResolutionsForScreen(index);
  257. }
  258. void CSettingsView::on_comboBoxFriendlyAI_currentTextChanged(const QString & arg1)
  259. {
  260. Settings node = settings.write["server"]["friendlyAI"];
  261. node->String() = arg1.toUtf8().data();
  262. }
  263. void CSettingsView::on_comboBoxNeutralAI_currentTextChanged(const QString & arg1)
  264. {
  265. Settings node = settings.write["server"]["neutralAI"];
  266. node->String() = arg1.toUtf8().data();
  267. }
  268. void CSettingsView::on_comboBoxEnemyAI_currentTextChanged(const QString & arg1)
  269. {
  270. Settings node = settings.write["server"]["enemyAI"];
  271. node->String() = arg1.toUtf8().data();
  272. }
  273. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  274. {
  275. Settings node = settings.write["server"]["port"];
  276. node->Float() = arg1;
  277. }
  278. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  279. {
  280. Settings node = settings.write["video"]["showIntro"];
  281. node->Bool() = index;
  282. }
  283. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  284. {
  285. Settings node = settings.write["general"]["saveFrequency"];
  286. node->Integer() = index;
  287. }
  288. void CSettingsView::on_comboBoxLanguage_currentIndexChanged(int index)
  289. {
  290. Settings node = settings.write["general"]["language"];
  291. QString selectedLanguage = ui->comboBoxLanguage->itemData(index).toString();
  292. node->String() = selectedLanguage.toStdString();
  293. if(auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow()))
  294. mainWindow->updateTranslation();
  295. }
  296. void CSettingsView::changeEvent(QEvent *event)
  297. {
  298. if(event->type() == QEvent::LanguageChange)
  299. {
  300. ui->retranslateUi(this);
  301. Languages::fillLanguages(ui->comboBoxLanguage, false);
  302. loadTranslation();
  303. }
  304. QWidget::changeEvent(event);
  305. }
  306. void CSettingsView::showEvent(QShowEvent * event)
  307. {
  308. loadTranslation();
  309. QWidget::showEvent(event);
  310. }
  311. void CSettingsView::on_comboBoxCursorType_currentIndexChanged(int index)
  312. {
  313. Settings node = settings.write["video"]["cursor"];
  314. node->String() = cursorTypesList[index];
  315. }
  316. void CSettingsView::on_listWidgetSettings_currentRowChanged(int currentRow)
  317. {
  318. QVector<QWidget*> targetWidgets = {
  319. ui->labelGeneral,
  320. ui->labelVideo,
  321. ui->labelArtificialIntelligence,
  322. ui->labelRepositories
  323. };
  324. QWidget * currentTarget = targetWidgets[currentRow];
  325. // We want to scroll in a way that will put target widget in topmost visible position
  326. // To show not just header, but all settings in this group as well
  327. // In order to do that, let's scroll to the very bottom and the scroll back up until target widget is visible
  328. int maxPosition = ui->settingsScrollArea->verticalScrollBar()->maximum();
  329. ui->settingsScrollArea->verticalScrollBar()->setValue(maxPosition);
  330. ui->settingsScrollArea->ensureWidgetVisible(currentTarget, 5, 5);
  331. }
  332. void CSettingsView::loadTranslation()
  333. {
  334. Languages::fillLanguages(ui->comboBoxLanguageBase, true);
  335. QString baseLanguage = Languages::getHeroesDataLanguage();
  336. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  337. if (!mainWindow)
  338. return;
  339. QString languageName = QString::fromStdString(settings["general"]["language"].String());
  340. QString modName = mainWindow->getModView()->getTranslationModName(languageName);
  341. bool translationExists = !modName.isEmpty();
  342. bool translationNeeded = languageName != baseLanguage;
  343. bool showTranslation = translationNeeded && translationExists;
  344. ui->labelTranslation->setVisible(showTranslation);
  345. ui->labelTranslationStatus->setVisible(showTranslation);
  346. ui->pushButtonTranslation->setVisible(showTranslation);
  347. if (!translationExists || !translationNeeded)
  348. return;
  349. bool translationAvailable = mainWindow->getModView()->isModAvailable(modName);
  350. bool translationEnabled = mainWindow->getModView()->isModEnabled(modName);
  351. ui->pushButtonTranslation->setVisible(!translationEnabled);
  352. if (translationEnabled)
  353. {
  354. ui->labelTranslationStatus->setText(tr("Active"));
  355. }
  356. if (!translationEnabled && !translationAvailable)
  357. {
  358. ui->labelTranslationStatus->setText(tr("Disabled"));
  359. ui->pushButtonTranslation->setText(tr("Enable"));
  360. }
  361. if (translationAvailable)
  362. {
  363. ui->labelTranslationStatus->setText(tr("Not Installed"));
  364. ui->pushButtonTranslation->setText(tr("Install"));
  365. }
  366. }
  367. void CSettingsView::on_pushButtonTranslation_clicked()
  368. {
  369. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  370. assert(mainWindow);
  371. if (!mainWindow)
  372. return;
  373. QString languageName = QString::fromStdString(settings["general"]["language"].String());
  374. QString modName = mainWindow->getModView()->getTranslationModName(languageName);
  375. assert(!modName.isEmpty());
  376. if (modName.isEmpty())
  377. return;
  378. if (mainWindow->getModView()->isModAvailable(modName))
  379. {
  380. mainWindow->switchToModsTab();
  381. mainWindow->getModView()->doInstallMod(modName);
  382. }
  383. else
  384. {
  385. mainWindow->getModView()->enableModByName(modName);
  386. }
  387. }
  388. void CSettingsView::on_comboBoxLanguageBase_currentIndexChanged(int index)
  389. {
  390. Settings node = settings.write["general"]["gameDataLanguage"];
  391. QString selectedLanguage = ui->comboBoxLanguageBase->itemData(index).toString();
  392. node->String() = selectedLanguage.toStdString();
  393. }
  394. void CSettingsView::on_checkBoxRepositoryDefault_stateChanged(int arg1)
  395. {
  396. Settings node = settings.write["launcher"]["defaultRepositoryEnabled"];
  397. node->Bool() = arg1;
  398. ui->lineEditRepositoryDefault->setEnabled(arg1);
  399. }
  400. void CSettingsView::on_checkBoxRepositoryExtra_stateChanged(int arg1)
  401. {
  402. Settings node = settings.write["launcher"]["extraRepositoryEnabled"];
  403. node->Bool() = arg1;
  404. ui->lineEditRepositoryExtra->setEnabled(arg1);
  405. }
  406. void CSettingsView::on_lineEditRepositoryExtra_textEdited(const QString &arg1)
  407. {
  408. Settings node = settings.write["launcher"]["extraRepositoryURL"];
  409. node->String() = arg1.toStdString();
  410. }
  411. void CSettingsView::on_spinBoxInterfaceScaling_valueChanged(int arg1)
  412. {
  413. Settings node = settings.write["video"]["resolution"]["scaling"];
  414. node->Float() = arg1;
  415. }
  416. void CSettingsView::on_refreshRepositoriesButton_clicked()
  417. {
  418. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  419. assert(mainWindow);
  420. if (!mainWindow)
  421. return;
  422. mainWindow->getModView()->loadRepositories();
  423. }
  424. void CSettingsView::on_spinBoxFramerateLimit_valueChanged(int arg1)
  425. {
  426. Settings node = settings.write["video"]["targetfps"];
  427. node->Float() = arg1;
  428. }
  429. void CSettingsView::on_checkBoxVSync_stateChanged(int arg1)
  430. {
  431. Settings node = settings.write["video"]["vsync"];
  432. node->Bool() = arg1;
  433. ui->spinBoxFramerateLimit->setDisabled(settings["video"]["vsync"].Bool());
  434. }
  435. void CSettingsView::on_comboBoxEnemyPlayerAI_currentTextChanged(const QString &arg1)
  436. {
  437. Settings node = settings.write["server"]["playerAI"];
  438. node->String() = arg1.toUtf8().data();
  439. }
  440. void CSettingsView::on_comboBoxAlliedPlayerAI_currentTextChanged(const QString &arg1)
  441. {
  442. Settings node = settings.write["server"]["alliedAI"];
  443. node->String() = arg1.toUtf8().data();
  444. }
  445. void CSettingsView::on_checkBoxAutoSavePrefix_stateChanged(int arg1)
  446. {
  447. Settings node = settings.write["general"]["useSavePrefix"];
  448. node->Bool() = arg1;
  449. ui->lineEditAutoSavePrefix->setEnabled(arg1);
  450. }
  451. void CSettingsView::on_spinBoxAutoSaveLimit_valueChanged(int arg1)
  452. {
  453. Settings node = settings.write["general"]["autosaveCountLimit"];
  454. node->Float() = arg1;
  455. }
  456. void CSettingsView::on_lineEditAutoSavePrefix_textEdited(const QString &arg1)
  457. {
  458. Settings node = settings.write["general"]["savePrefix"];
  459. node->String() = arg1.toStdString();
  460. }
  461. void CSettingsView::on_spinBoxReservedArea_valueChanged(int arg1)
  462. {
  463. Settings node = settings.write["video"]["reservedWidth"];
  464. node->Float() = float(arg1) / 100; // percentage -> ratio
  465. }
  466. void CSettingsView::on_comboBoxRendererType_currentTextChanged(const QString &arg1)
  467. {
  468. Settings node = settings.write["video"]["driver"];
  469. node->String() = arg1.toStdString();
  470. }