csettingsview_moc.cpp 15 KB

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