csettingsview_moc.cpp 11 KB

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