csettingsview_moc.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "../updatedialog_moc.h"
  14. #include <QFileInfo>
  15. #include <QGuiApplication>
  16. #include "../../lib/CConfigHandler.h"
  17. #include "../../lib/VCMIDirs.h"
  18. namespace
  19. {
  20. QString resolutionToString(const QSize & resolution)
  21. {
  22. return QString{"%1x%2"}.arg(resolution.width()).arg(resolution.height());
  23. }
  24. }
  25. /// List of encoding which can be selected from Launcher.
  26. /// Note that it is possible to specify enconding manually in settings.json
  27. static const std::string knownEncodingsList[] = //TODO: remove hardcode
  28. {
  29. // European Windows-125X encodings
  30. "CP1250", // West European, covers mostly Slavic languages that use latin script
  31. "CP1251", // Covers languages that use cyrillic scrypt
  32. "CP1252", // Latin/East European, covers most of latin languages
  33. // Chinese encodings
  34. "GBK", // extension of GB2312, also known as CP936
  35. "GB2312", // basic set for Simplified Chinese. Separate from GBK to allow proper detection of H3 fonts
  36. // Korean encodings
  37. "CP949" // extension of EUC-KR.
  38. };
  39. void CSettingsView::setDisplayList()
  40. {
  41. QStringList list;
  42. for (const auto screen : QGuiApplication::screens())
  43. list << QString{"%1 - %2"}.arg(screen->name(), resolutionToString(screen->size()));
  44. if(list.count() < 2)
  45. {
  46. ui->comboBoxDisplayIndex->hide();
  47. ui->labelDisplayIndex->hide();
  48. }
  49. else
  50. {
  51. int displayIndex = settings["video"]["displayIndex"].Integer();
  52. ui->comboBoxDisplayIndex->clear();
  53. ui->comboBoxDisplayIndex->addItems(list);
  54. ui->comboBoxDisplayIndex->setCurrentIndex(displayIndex);
  55. }
  56. }
  57. void CSettingsView::loadSettings()
  58. {
  59. int resX = settings["video"]["screenRes"]["width"].Float();
  60. int resY = settings["video"]["screenRes"]["height"].Float();
  61. int resIndex = ui->comboBoxResolution->findText(QString("%1x%2").arg(resX).arg(resY));
  62. ui->comboBoxResolution->setCurrentIndex(resIndex);
  63. ui->comboBoxShowIntro->setCurrentIndex(settings["video"]["showIntro"].Bool());
  64. #ifdef Q_OS_IOS
  65. ui->comboBoxFullScreen->setCurrentIndex(true);
  66. ui->checkBoxFullScreen->setChecked(false);
  67. for (auto widget : std::initializer_list<QWidget *>{ui->comboBoxFullScreen, ui->checkBoxFullScreen})
  68. widget->setDisabled(true);
  69. #else
  70. ui->comboBoxFullScreen->setCurrentIndex(settings["video"]["fullscreen"].Bool());
  71. ui->checkBoxFullScreen->setChecked(settings["video"]["realFullscreen"].Bool());
  72. #endif
  73. ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
  74. ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String()));
  75. ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String()));
  76. ui->comboBoxPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
  77. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  78. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  79. // all calls to plainText will trigger textChanged() signal overwriting config. Create backup before editing widget
  80. JsonNode urls = settings["launcher"]["repositoryURL"];
  81. ui->plainTextEditRepos->clear();
  82. for(auto entry : urls.Vector())
  83. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  84. ui->lineEditUserDataDir->setText(pathToQString(VCMIDirs::get().userDataPath()));
  85. ui->lineEditGameDir->setText(pathToQString(VCMIDirs::get().binaryPath()));
  86. ui->lineEditTempDir->setText(pathToQString(VCMIDirs::get().userCachePath()));
  87. std::string encoding = settings["general"]["encoding"].String();
  88. size_t encodingIndex = boost::range::find(knownEncodingsList, encoding) - knownEncodingsList;
  89. if(encodingIndex < ui->comboBoxEncoding->count())
  90. ui->comboBoxEncoding->setCurrentIndex((int)encodingIndex);
  91. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  92. }
  93. CSettingsView::CSettingsView(QWidget * parent)
  94. : QWidget(parent), ui(new Ui::CSettingsView)
  95. {
  96. ui->setupUi(this);
  97. loadSettings();
  98. }
  99. CSettingsView::~CSettingsView()
  100. {
  101. delete ui;
  102. }
  103. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  104. {
  105. QStringList list = arg1.split("x");
  106. Settings node = settings.write["video"]["screenRes"];
  107. node["width"].Float() = list[0].toInt();
  108. node["height"].Float() = list[1].toInt();
  109. }
  110. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  111. {
  112. Settings node = settings.write["video"]["fullscreen"];
  113. node->Bool() = index;
  114. }
  115. void CSettingsView::on_checkBoxFullScreen_stateChanged(int state)
  116. {
  117. Settings node = settings.write["video"]["realFullscreen"];
  118. node->Bool() = state;
  119. }
  120. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  121. {
  122. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  123. node->Bool() = index;
  124. }
  125. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  126. {
  127. Settings node = settings.write["video"];
  128. node["displayIndex"].Float() = index;
  129. }
  130. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  131. {
  132. Settings node = settings.write["server"]["playerAI"];
  133. node->String() = arg1.toUtf8().data();
  134. }
  135. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  136. {
  137. Settings node = settings.write["server"]["friendlyAI"];
  138. node->String() = arg1.toUtf8().data();
  139. }
  140. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  141. {
  142. Settings node = settings.write["server"]["neutralAI"];
  143. node->String() = arg1.toUtf8().data();
  144. }
  145. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  146. {
  147. Settings node = settings.write["server"]["enemyAI"];
  148. node->String() = arg1.toUtf8().data();
  149. }
  150. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  151. {
  152. Settings node = settings.write["server"]["port"];
  153. node->Float() = arg1;
  154. }
  155. void CSettingsView::on_plainTextEditRepos_textChanged()
  156. {
  157. Settings node = settings.write["launcher"]["repositoryURL"];
  158. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  159. node->Vector().clear();
  160. for(QString line : list)
  161. {
  162. if(line.trimmed().size() > 0)
  163. {
  164. JsonNode entry;
  165. entry.String() = line.trimmed().toUtf8().data();
  166. node->Vector().push_back(entry);
  167. }
  168. }
  169. }
  170. void CSettingsView::on_comboBoxEncoding_currentIndexChanged(int index)
  171. {
  172. Settings node = settings.write["general"]["encoding"];
  173. node->String() = knownEncodingsList[index];
  174. }
  175. void CSettingsView::on_openTempDir_clicked()
  176. {
  177. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  178. }
  179. void CSettingsView::on_openUserDataDir_clicked()
  180. {
  181. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  182. }
  183. void CSettingsView::on_openGameDataDir_clicked()
  184. {
  185. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  186. }
  187. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  188. {
  189. Settings node = settings.write["video"]["showIntro"];
  190. node->Bool() = index;
  191. }
  192. void CSettingsView::on_changeGameDataDir_clicked()
  193. {
  194. }
  195. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  196. {
  197. Settings node = settings.write["general"]["saveFrequency"];
  198. node->Integer() = index;
  199. }
  200. void CSettingsView::on_updatesButton_clicked()
  201. {
  202. UpdateDialog::showUpdateDialog(true);
  203. }