csettingsview_moc.cpp 9.7 KB

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