csettingsview_moc.cpp 6.4 KB

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