csettingsview_moc.cpp 9.8 KB

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