csettingsview_moc.cpp 17 KB

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