csettingsview_moc.cpp 7.7 KB

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