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