csettingsview_moc.cpp 7.4 KB

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