csettingsview_moc.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "../jsonutils.h"
  15. #include "../launcherdirs.h"
  16. #include "../updatedialog_moc.h"
  17. #include <QFileInfo>
  18. #include <QGuiApplication>
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/VCMIDirs.h"
  21. namespace
  22. {
  23. QString resolutionToString(const QSize & resolution)
  24. {
  25. return QString{"%1x%2"}.arg(resolution.width()).arg(resolution.height());
  26. }
  27. }
  28. /// List of encoding which can be selected from Launcher.
  29. /// Note that it is possible to specify enconding manually in settings.json
  30. static const std::string knownEncodingsList[] = //TODO: remove hardcode
  31. {
  32. // Asks vcmi to automatically detect encoding
  33. "auto",
  34. // European Windows-125X encodings
  35. "CP1250", // West European, covers mostly Slavic languages that use latin script
  36. "CP1251", // Covers languages that use cyrillic scrypt
  37. "CP1252", // Latin/East European, covers most of latin languages
  38. // Chinese encodings
  39. "GBK", // extension of GB2312, also known as CP936
  40. "GB2312", // basic set for Simplified Chinese. Separate from GBK to allow proper detection of H3 fonts
  41. // Korean encodings
  42. "CP949" // extension of EUC-KR.
  43. };
  44. /// List of tags of languages that can be selected from Launcher (and have translation for Launcher)
  45. static const std::string languageTagList[] =
  46. {
  47. "english",
  48. "german",
  49. "polish",
  50. "russian",
  51. "ukrainian",
  52. };
  53. static const std::string cursorTypesList[] =
  54. {
  55. "auto",
  56. "hardware",
  57. "software"
  58. };
  59. void CSettingsView::setDisplayList()
  60. {
  61. QStringList list;
  62. for (const auto screen : QGuiApplication::screens())
  63. list << QString{"%1 - %2"}.arg(screen->name(), resolutionToString(screen->size()));
  64. if(list.count() < 2)
  65. {
  66. ui->comboBoxDisplayIndex->hide();
  67. ui->labelDisplayIndex->hide();
  68. fillValidResolutionsForScreen(0);
  69. }
  70. else
  71. {
  72. int displayIndex = settings["video"]["displayIndex"].Integer();
  73. ui->comboBoxDisplayIndex->addItems(list);
  74. // calls fillValidResolutions() in slot
  75. ui->comboBoxDisplayIndex->setCurrentIndex(displayIndex);
  76. }
  77. }
  78. void CSettingsView::loadSettings()
  79. {
  80. ui->comboBoxShowIntro->setCurrentIndex(settings["video"]["showIntro"].Bool());
  81. #ifdef Q_OS_IOS
  82. ui->comboBoxFullScreen->setCurrentIndex(true);
  83. ui->comboBoxFullScreen->setDisabled(true);
  84. #else
  85. ui->comboBoxFullScreen->setCurrentIndex(settings["video"]["fullscreen"].Bool());
  86. if (settings["video"]["realFullscreen"].Bool())
  87. ui->comboBoxFullScreen->setCurrentIndex(2);
  88. #endif
  89. ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
  90. ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String()));
  91. ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String()));
  92. ui->comboBoxPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
  93. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  94. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  95. // all calls to plainText will trigger textChanged() signal overwriting config. Create backup before editing widget
  96. JsonNode urls = settings["launcher"]["repositoryURL"];
  97. ui->plainTextEditRepos->clear();
  98. for(auto entry : urls.Vector())
  99. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  100. ui->lineEditUserDataDir->setText(pathToQString(VCMIDirs::get().userDataPath()));
  101. ui->lineEditGameDir->setText(pathToQString(VCMIDirs::get().binaryPath()));
  102. ui->lineEditTempDir->setText(pathToQString(VCMIDirs::get().userLogsPath()));
  103. std::string encoding = settings["general"]["encoding"].String();
  104. size_t encodingIndex = boost::range::find(knownEncodingsList, encoding) - knownEncodingsList;
  105. if(encodingIndex < ui->comboBoxEncoding->count())
  106. ui->comboBoxEncoding->setCurrentIndex((int)encodingIndex);
  107. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  108. std::string language = settings["general"]["language"].String();
  109. size_t languageIndex = boost::range::find(languageTagList, language) - languageTagList;
  110. if(languageIndex < ui->comboBoxLanguage->count())
  111. ui->comboBoxLanguage->setCurrentIndex((int)languageIndex);
  112. std::string cursorType = settings["video"]["cursor"].String();
  113. size_t cursorTypeIndex = boost::range::find(cursorTypesList, cursorType) - cursorTypesList;
  114. ui->comboBoxCursorType->setCurrentIndex((int)cursorTypeIndex);
  115. }
  116. void CSettingsView::fillValidResolutions(bool isExtraResolutionsModEnabled)
  117. {
  118. this->isExtraResolutionsModEnabled = isExtraResolutionsModEnabled;
  119. fillValidResolutionsForScreen(ui->comboBoxDisplayIndex->isVisible() ? ui->comboBoxDisplayIndex->currentIndex() : 0);
  120. }
  121. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  122. {
  123. ui->comboBoxResolution->blockSignals(true); // avoid saving wrong resolution after adding first item from the list
  124. ui->comboBoxResolution->clear();
  125. // TODO: read available resolutions from all mods
  126. QVariantList resolutions;
  127. if(isExtraResolutionsModEnabled)
  128. {
  129. const auto extrasResolutionsPath = settings["launcher"]["extraResolutionsModPath"].String().c_str();
  130. const auto extrasResolutionsJson = JsonUtils::JsonFromFile(CLauncherDirs::get().modsPath() + extrasResolutionsPath);
  131. resolutions = extrasResolutionsJson.toMap().value(QLatin1String{"GUISettings"}).toList();
  132. }
  133. if(resolutions.isEmpty())
  134. {
  135. ui->comboBoxResolution->blockSignals(false);
  136. ui->comboBoxResolution->addItem(resolutionToString({800, 600}));
  137. return;
  138. }
  139. const auto screens = qGuiApp->screens();
  140. const auto currentScreen = screenIndex < screens.size() ? screens[screenIndex] : qGuiApp->primaryScreen();
  141. const auto screenSize = currentScreen->size();
  142. MAYBE_UNUSED(screenSize);
  143. for(const auto & entry : resolutions)
  144. {
  145. const auto resolutionMap = entry.toMap().value(QLatin1String{"resolution"}).toMap();
  146. if(resolutionMap.isEmpty())
  147. continue;
  148. const auto widthValue = resolutionMap[QLatin1String{"x"}];
  149. const auto heightValue = resolutionMap[QLatin1String{"y"}];
  150. if(!widthValue.isValid() || !heightValue.isValid())
  151. continue;
  152. const QSize resolution{widthValue.toInt(), heightValue.toInt()};
  153. #ifndef VCMI_IOS
  154. if(screenSize.width() < resolution.width() || screenSize.height() < resolution.height())
  155. continue;
  156. #endif
  157. ui->comboBoxResolution->addItem(resolutionToString(resolution));
  158. }
  159. int resX = settings["video"]["screenRes"]["width"].Integer();
  160. int resY = settings["video"]["screenRes"]["height"].Integer();
  161. int resIndex = ui->comboBoxResolution->findText(resolutionToString({resX, resY}));
  162. ui->comboBoxResolution->setCurrentIndex(resIndex);
  163. ui->comboBoxResolution->blockSignals(false);
  164. // if selected resolution no longer exists, force update value to the first resolution
  165. if(resIndex == -1)
  166. ui->comboBoxResolution->setCurrentIndex(0);
  167. }
  168. CSettingsView::CSettingsView(QWidget * parent)
  169. : QWidget(parent), ui(new Ui::CSettingsView)
  170. {
  171. ui->setupUi(this);
  172. ui->lineEditBuildVersion->setText(QString::fromStdString(GameConstants::VCMI_VERSION));
  173. loadSettings();
  174. }
  175. CSettingsView::~CSettingsView()
  176. {
  177. delete ui;
  178. }
  179. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  180. {
  181. QStringList list = arg1.split("x");
  182. Settings node = settings.write["video"]["screenRes"];
  183. node["width"].Float() = list[0].toInt();
  184. node["height"].Float() = list[1].toInt();
  185. }
  186. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  187. {
  188. Settings nodeFullscreen = settings.write["video"]["fullscreen"];
  189. Settings nodeRealFullscreen = settings.write["video"]["realFullscreen"];
  190. nodeFullscreen->Bool() = (index != 0);
  191. nodeRealFullscreen->Bool() = (index == 2);
  192. }
  193. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  194. {
  195. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  196. node->Bool() = index;
  197. }
  198. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  199. {
  200. Settings node = settings.write["video"];
  201. node["displayIndex"].Float() = index;
  202. fillValidResolutionsForScreen(index);
  203. }
  204. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  205. {
  206. Settings node = settings.write["server"]["playerAI"];
  207. node->String() = arg1.toUtf8().data();
  208. }
  209. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  210. {
  211. Settings node = settings.write["server"]["friendlyAI"];
  212. node->String() = arg1.toUtf8().data();
  213. }
  214. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  215. {
  216. Settings node = settings.write["server"]["neutralAI"];
  217. node->String() = arg1.toUtf8().data();
  218. }
  219. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  220. {
  221. Settings node = settings.write["server"]["enemyAI"];
  222. node->String() = arg1.toUtf8().data();
  223. }
  224. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  225. {
  226. Settings node = settings.write["server"]["port"];
  227. node->Float() = arg1;
  228. }
  229. void CSettingsView::on_plainTextEditRepos_textChanged()
  230. {
  231. Settings node = settings.write["launcher"]["repositoryURL"];
  232. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  233. node->Vector().clear();
  234. for(QString line : list)
  235. {
  236. if(line.trimmed().size() > 0)
  237. {
  238. JsonNode entry;
  239. entry.String() = line.trimmed().toUtf8().data();
  240. node->Vector().push_back(entry);
  241. }
  242. }
  243. }
  244. void CSettingsView::on_comboBoxEncoding_currentIndexChanged(int index)
  245. {
  246. Settings node = settings.write["general"]["encoding"];
  247. node->String() = knownEncodingsList[index];
  248. }
  249. void CSettingsView::on_openTempDir_clicked()
  250. {
  251. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  252. }
  253. void CSettingsView::on_openUserDataDir_clicked()
  254. {
  255. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  256. }
  257. void CSettingsView::on_openGameDataDir_clicked()
  258. {
  259. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  260. }
  261. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  262. {
  263. Settings node = settings.write["video"]["showIntro"];
  264. node->Bool() = index;
  265. }
  266. void CSettingsView::on_changeGameDataDir_clicked()
  267. {
  268. }
  269. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  270. {
  271. Settings node = settings.write["general"]["saveFrequency"];
  272. node->Integer() = index;
  273. }
  274. void CSettingsView::on_updatesButton_clicked()
  275. {
  276. UpdateDialog::showUpdateDialog(true);
  277. }
  278. void CSettingsView::on_comboBoxLanguage_currentIndexChanged(int index)
  279. {
  280. Settings node = settings.write["general"]["language"];
  281. node->String() = languageTagList[index];
  282. if ( auto mainWindow = dynamic_cast<MainWindow*>(qApp->activeWindow()) )
  283. mainWindow->updateTranslation();
  284. }
  285. void CSettingsView::changeEvent(QEvent *event)
  286. {
  287. if(event->type() == QEvent::LanguageChange)
  288. {
  289. ui->retranslateUi(this);
  290. }
  291. QWidget::changeEvent(event);
  292. }
  293. void CSettingsView::on_comboBoxCursorType_currentIndexChanged(int index)
  294. {
  295. Settings node = settings.write["video"]["cursor"];
  296. node->String() = cursorTypesList[index];
  297. }
  298. void CSettingsView::on_listWidgetSettings_currentRowChanged(int currentRow)
  299. {
  300. QVector<QWidget*> targetWidgets = {
  301. ui->labelGeneral,
  302. ui->labelVideo,
  303. ui->labelArtificialIntelligence,
  304. ui->labelDataDirs,
  305. ui->labelRepositories
  306. };
  307. QWidget * currentTarget = targetWidgets[currentRow];
  308. // We want to scroll in a way that will put target widget in topmost visible position
  309. // To show not just header, but all settings in this group as well
  310. // In order to do that, let's scroll to the very bottom and the scroll back up until target widget is visible
  311. int maxPosition = ui->settingsScrollArea->verticalScrollBar()->maximum();
  312. ui->settingsScrollArea->verticalScrollBar()->setValue(maxPosition);
  313. ui->settingsScrollArea->ensureWidgetVisible(currentTarget, 5, 5);
  314. }