csettingsview_moc.cpp 15 KB

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