csettingsview_moc.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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->comboBoxFullScreen->setCurrentIndex(settings["video"]["fullscreen"].Bool());
  62. ui->comboBoxShowIntro->setCurrentIndex(settings["video"]["showIntro"].Bool());
  63. ui->checkBoxFullScreen->setChecked(settings["video"]["realFullscreen"].Bool());
  64. int friendlyAIIndex = ui->comboBoxFriendlyAI->findText(QString::fromUtf8(settings["server"]["friendlyAI"].String().c_str()));
  65. int neutralAIIndex = ui->comboBoxNeutralAI->findText(QString::fromUtf8(settings["server"]["neutralAI"].String().c_str()));
  66. int enemyAIIndex = ui->comboBoxEnemyAI->findText(QString::fromUtf8(settings["server"]["enemyAI"].String().c_str()));
  67. int playerAIIndex = ui->comboBoxPlayerAI->findText(QString::fromUtf8(settings["server"]["playerAI"].String().c_str()));
  68. ui->comboBoxFriendlyAI->setCurrentIndex(friendlyAIIndex);
  69. ui->comboBoxNeutralAI->setCurrentIndex(neutralAIIndex);
  70. ui->comboBoxEnemyAI->setCurrentIndex(enemyAIIndex);
  71. ui->comboBoxPlayerAI->setCurrentIndex(playerAIIndex);
  72. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  73. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  74. // all calls to plainText will trigger textChanged() signal overwriting config. Create backup before editing widget
  75. JsonNode urls = settings["launcher"]["repositoryURL"];
  76. ui->plainTextEditRepos->clear();
  77. for(auto entry : urls.Vector())
  78. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  79. ui->lineEditUserDataDir->setText(pathToQString(VCMIDirs::get().userDataPath()));
  80. ui->lineEditGameDir->setText(pathToQString(VCMIDirs::get().binaryPath()));
  81. ui->lineEditTempDir->setText(pathToQString(VCMIDirs::get().userCachePath()));
  82. std::string encoding = settings["general"]["encoding"].String();
  83. size_t encodingIndex = boost::range::find(knownEncodingsList, encoding) - knownEncodingsList;
  84. if(encodingIndex < ui->comboBoxEncoding->count())
  85. ui->comboBoxEncoding->setCurrentIndex((int)encodingIndex);
  86. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  87. }
  88. CSettingsView::CSettingsView(QWidget * parent)
  89. : QWidget(parent), ui(new Ui::CSettingsView)
  90. {
  91. ui->setupUi(this);
  92. loadSettings();
  93. }
  94. CSettingsView::~CSettingsView()
  95. {
  96. delete ui;
  97. }
  98. void CSettingsView::on_comboBoxResolution_currentIndexChanged(const QString & arg1)
  99. {
  100. QStringList list = arg1.split("x");
  101. Settings node = settings.write["video"]["screenRes"];
  102. node["width"].Float() = list[0].toInt();
  103. node["height"].Float() = list[1].toInt();
  104. }
  105. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  106. {
  107. Settings node = settings.write["video"]["fullscreen"];
  108. node->Bool() = index;
  109. }
  110. void CSettingsView::on_checkBoxFullScreen_stateChanged(int state)
  111. {
  112. Settings node = settings.write["video"]["realFullscreen"];
  113. node->Bool() = state;
  114. }
  115. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  116. {
  117. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  118. node->Bool() = index;
  119. }
  120. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  121. {
  122. Settings node = settings.write["video"];
  123. node["displayIndex"].Float() = index;
  124. }
  125. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  126. {
  127. Settings node = settings.write["server"]["playerAI"];
  128. node->String() = arg1.toUtf8().data();
  129. }
  130. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  131. {
  132. Settings node = settings.write["server"]["friendlyAI"];
  133. node->String() = arg1.toUtf8().data();
  134. }
  135. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  136. {
  137. Settings node = settings.write["server"]["neutralAI"];
  138. node->String() = arg1.toUtf8().data();
  139. }
  140. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  141. {
  142. Settings node = settings.write["server"]["enemyAI"];
  143. node->String() = arg1.toUtf8().data();
  144. }
  145. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  146. {
  147. Settings node = settings.write["server"]["port"];
  148. node->Float() = arg1;
  149. }
  150. void CSettingsView::on_plainTextEditRepos_textChanged()
  151. {
  152. Settings node = settings.write["launcher"]["repositoryURL"];
  153. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  154. node->Vector().clear();
  155. for(QString line : list)
  156. {
  157. if(line.trimmed().size() > 0)
  158. {
  159. JsonNode entry;
  160. entry.String() = line.trimmed().toUtf8().data();
  161. node->Vector().push_back(entry);
  162. }
  163. }
  164. }
  165. void CSettingsView::on_comboBoxEncoding_currentIndexChanged(int index)
  166. {
  167. Settings node = settings.write["general"]["encoding"];
  168. node->String() = knownEncodingsList[index];
  169. }
  170. void CSettingsView::on_openTempDir_clicked()
  171. {
  172. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  173. }
  174. void CSettingsView::on_openUserDataDir_clicked()
  175. {
  176. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  177. }
  178. void CSettingsView::on_openGameDataDir_clicked()
  179. {
  180. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  181. }
  182. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  183. {
  184. Settings node = settings.write["video"]["showIntro"];
  185. node->Bool() = index;
  186. }
  187. void CSettingsView::on_changeGameDataDir_clicked()
  188. {
  189. }
  190. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  191. {
  192. Settings node = settings.write["general"]["saveFrequency"];
  193. node->Integer() = index;
  194. }
  195. void CSettingsView::on_updatesButton_clicked()
  196. {
  197. UpdateDialog::showUpdateDialog(true);
  198. }