csettingsview_moc.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #endif
  79. ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
  80. ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String()));
  81. ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String()));
  82. ui->comboBoxPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
  83. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  84. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  85. // all calls to plainText will trigger textChanged() signal overwriting config. Create backup before editing widget
  86. JsonNode urls = settings["launcher"]["repositoryURL"];
  87. ui->plainTextEditRepos->clear();
  88. for(auto entry : urls.Vector())
  89. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  90. ui->lineEditUserDataDir->setText(pathToQString(VCMIDirs::get().userDataPath()));
  91. ui->lineEditGameDir->setText(pathToQString(VCMIDirs::get().binaryPath()));
  92. ui->lineEditTempDir->setText(pathToQString(VCMIDirs::get().userLogsPath()));
  93. std::string encoding = settings["general"]["encoding"].String();
  94. size_t encodingIndex = boost::range::find(knownEncodingsList, encoding) - knownEncodingsList;
  95. if(encodingIndex < ui->comboBoxEncoding->count())
  96. ui->comboBoxEncoding->setCurrentIndex((int)encodingIndex);
  97. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  98. std::string language = settings["general"]["language"].String();
  99. size_t languageIndex = boost::range::find(languageTagList, language) - languageTagList;
  100. if(languageIndex < ui->comboBoxLanguage->count())
  101. ui->comboBoxLanguage->setCurrentIndex((int)languageIndex);
  102. }
  103. void CSettingsView::fillValidResolutions(bool isExtraResolutionsModEnabled)
  104. {
  105. this->isExtraResolutionsModEnabled = isExtraResolutionsModEnabled;
  106. fillValidResolutionsForScreen(ui->comboBoxDisplayIndex->isVisible() ? ui->comboBoxDisplayIndex->currentIndex() : 0);
  107. }
  108. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  109. {
  110. ui->comboBoxResolution->blockSignals(true); // avoid saving wrong resolution after adding first item from the list
  111. ui->comboBoxResolution->clear();
  112. // TODO: read available resolutions from all mods
  113. QVariantList resolutions;
  114. if(isExtraResolutionsModEnabled)
  115. {
  116. const auto extrasResolutionsPath = settings["launcher"]["extraResolutionsModPath"].String().c_str();
  117. const auto extrasResolutionsJson = JsonUtils::JsonFromFile(CLauncherDirs::get().modsPath() + extrasResolutionsPath);
  118. resolutions = extrasResolutionsJson.toMap().value(QLatin1String{"GUISettings"}).toList();
  119. }
  120. if(resolutions.isEmpty())
  121. {
  122. ui->comboBoxResolution->blockSignals(false);
  123. ui->comboBoxResolution->addItem(resolutionToString({800, 600}));
  124. return;
  125. }
  126. const auto screens = qGuiApp->screens();
  127. const auto currentScreen = screenIndex < screens.size() ? screens[screenIndex] : qGuiApp->primaryScreen();
  128. const auto screenSize = currentScreen->size();
  129. for(const auto & entry : resolutions)
  130. {
  131. const auto resolutionMap = entry.toMap().value(QLatin1String{"resolution"}).toMap();
  132. if(resolutionMap.isEmpty())
  133. continue;
  134. const auto widthValue = resolutionMap[QLatin1String{"x"}];
  135. const auto heightValue = resolutionMap[QLatin1String{"y"}];
  136. if(!widthValue.isValid() || !heightValue.isValid())
  137. continue;
  138. const QSize resolution{widthValue.toInt(), heightValue.toInt()};
  139. #ifndef VCMI_IOS
  140. if(screenSize.width() < resolution.width() || screenSize.height() < resolution.height())
  141. continue;
  142. #endif
  143. ui->comboBoxResolution->addItem(resolutionToString(resolution));
  144. }
  145. int resX = settings["video"]["screenRes"]["width"].Integer();
  146. int resY = settings["video"]["screenRes"]["height"].Integer();
  147. int resIndex = ui->comboBoxResolution->findText(resolutionToString({resX, resY}));
  148. ui->comboBoxResolution->setCurrentIndex(resIndex);
  149. ui->comboBoxResolution->blockSignals(false);
  150. // if selected resolution no longer exists, force update value to the first resolution
  151. if(resIndex == -1)
  152. ui->comboBoxResolution->setCurrentIndex(0);
  153. }
  154. CSettingsView::CSettingsView(QWidget * parent)
  155. : QWidget(parent), ui(new Ui::CSettingsView)
  156. {
  157. ui->setupUi(this);
  158. ui->labelBuildVersion->setText(QString::fromStdString(GameConstants::VCMI_VERSION));
  159. loadSettings();
  160. }
  161. CSettingsView::~CSettingsView()
  162. {
  163. delete ui;
  164. }
  165. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  166. {
  167. QStringList list = arg1.split("x");
  168. Settings node = settings.write["video"]["screenRes"];
  169. node["width"].Float() = list[0].toInt();
  170. node["height"].Float() = list[1].toInt();
  171. }
  172. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  173. {
  174. Settings nodeFullscreen = settings.write["video"]["fullscreen"];
  175. Settings nodeRealFullscreen = settings.write["video"]["realFullscreen"];
  176. nodeFullscreen->Bool() = (index != 0);
  177. nodeRealFullscreen->Bool() = (index == 2);
  178. }
  179. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  180. {
  181. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  182. node->Bool() = index;
  183. }
  184. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  185. {
  186. Settings node = settings.write["video"];
  187. node["displayIndex"].Float() = index;
  188. fillValidResolutionsForScreen(index);
  189. }
  190. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  191. {
  192. Settings node = settings.write["server"]["playerAI"];
  193. node->String() = arg1.toUtf8().data();
  194. }
  195. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  196. {
  197. Settings node = settings.write["server"]["friendlyAI"];
  198. node->String() = arg1.toUtf8().data();
  199. }
  200. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  201. {
  202. Settings node = settings.write["server"]["neutralAI"];
  203. node->String() = arg1.toUtf8().data();
  204. }
  205. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  206. {
  207. Settings node = settings.write["server"]["enemyAI"];
  208. node->String() = arg1.toUtf8().data();
  209. }
  210. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  211. {
  212. Settings node = settings.write["server"]["port"];
  213. node->Float() = arg1;
  214. }
  215. void CSettingsView::on_plainTextEditRepos_textChanged()
  216. {
  217. Settings node = settings.write["launcher"]["repositoryURL"];
  218. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  219. node->Vector().clear();
  220. for(QString line : list)
  221. {
  222. if(line.trimmed().size() > 0)
  223. {
  224. JsonNode entry;
  225. entry.String() = line.trimmed().toUtf8().data();
  226. node->Vector().push_back(entry);
  227. }
  228. }
  229. }
  230. void CSettingsView::on_comboBoxEncoding_currentIndexChanged(int index)
  231. {
  232. Settings node = settings.write["general"]["encoding"];
  233. node->String() = knownEncodingsList[index];
  234. }
  235. void CSettingsView::on_openTempDir_clicked()
  236. {
  237. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  238. }
  239. void CSettingsView::on_openUserDataDir_clicked()
  240. {
  241. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  242. }
  243. void CSettingsView::on_openGameDataDir_clicked()
  244. {
  245. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  246. }
  247. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  248. {
  249. Settings node = settings.write["video"]["showIntro"];
  250. node->Bool() = index;
  251. }
  252. void CSettingsView::on_changeGameDataDir_clicked()
  253. {
  254. }
  255. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  256. {
  257. Settings node = settings.write["general"]["saveFrequency"];
  258. node->Integer() = index;
  259. }
  260. void CSettingsView::on_updatesButton_clicked()
  261. {
  262. UpdateDialog::showUpdateDialog(true);
  263. }
  264. void CSettingsView::on_comboBoxLanguage_currentIndexChanged(int index)
  265. {
  266. Settings node = settings.write["general"]["language"];
  267. node->String() = languageTagList[index];
  268. if ( auto mainWindow = dynamic_cast<MainWindow*>(qApp->activeWindow()) )
  269. mainWindow->updateTranslation();
  270. }
  271. void CSettingsView::changeEvent(QEvent *event)
  272. {
  273. if(event->type() == QEvent::LanguageChange)
  274. {
  275. ui->retranslateUi(this);
  276. }
  277. QWidget::changeEvent(event);
  278. }