csettingsview_moc.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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().userCachePath()));
  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. loadSettings();
  148. }
  149. CSettingsView::~CSettingsView()
  150. {
  151. delete ui;
  152. }
  153. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  154. {
  155. QStringList list = arg1.split("x");
  156. Settings node = settings.write["video"]["screenRes"];
  157. node["width"].Float() = list[0].toInt();
  158. node["height"].Float() = list[1].toInt();
  159. }
  160. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  161. {
  162. Settings node = settings.write["video"]["fullscreen"];
  163. node->Bool() = index;
  164. }
  165. void CSettingsView::on_checkBoxFullScreen_stateChanged(int state)
  166. {
  167. Settings node = settings.write["video"]["realFullscreen"];
  168. node->Bool() = state;
  169. }
  170. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  171. {
  172. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  173. node->Bool() = index;
  174. }
  175. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  176. {
  177. Settings node = settings.write["video"];
  178. node["displayIndex"].Float() = index;
  179. fillValidResolutionsForScreen(index);
  180. }
  181. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  182. {
  183. Settings node = settings.write["server"]["playerAI"];
  184. node->String() = arg1.toUtf8().data();
  185. }
  186. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  187. {
  188. Settings node = settings.write["server"]["friendlyAI"];
  189. node->String() = arg1.toUtf8().data();
  190. }
  191. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  192. {
  193. Settings node = settings.write["server"]["neutralAI"];
  194. node->String() = arg1.toUtf8().data();
  195. }
  196. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  197. {
  198. Settings node = settings.write["server"]["enemyAI"];
  199. node->String() = arg1.toUtf8().data();
  200. }
  201. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  202. {
  203. Settings node = settings.write["server"]["port"];
  204. node->Float() = arg1;
  205. }
  206. void CSettingsView::on_plainTextEditRepos_textChanged()
  207. {
  208. Settings node = settings.write["launcher"]["repositoryURL"];
  209. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  210. node->Vector().clear();
  211. for(QString line : list)
  212. {
  213. if(line.trimmed().size() > 0)
  214. {
  215. JsonNode entry;
  216. entry.String() = line.trimmed().toUtf8().data();
  217. node->Vector().push_back(entry);
  218. }
  219. }
  220. }
  221. void CSettingsView::on_comboBoxEncoding_currentIndexChanged(int index)
  222. {
  223. Settings node = settings.write["general"]["encoding"];
  224. node->String() = knownEncodingsList[index];
  225. }
  226. void CSettingsView::on_openTempDir_clicked()
  227. {
  228. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  229. }
  230. void CSettingsView::on_openUserDataDir_clicked()
  231. {
  232. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  233. }
  234. void CSettingsView::on_openGameDataDir_clicked()
  235. {
  236. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  237. }
  238. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  239. {
  240. Settings node = settings.write["video"]["showIntro"];
  241. node->Bool() = index;
  242. }
  243. void CSettingsView::on_changeGameDataDir_clicked()
  244. {
  245. }
  246. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  247. {
  248. Settings node = settings.write["general"]["saveFrequency"];
  249. node->Integer() = index;
  250. }
  251. void CSettingsView::on_updatesButton_clicked()
  252. {
  253. UpdateDialog::showUpdateDialog(true);
  254. }