csettingsview_moc.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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::loadSettings()
  20. {
  21. int resX = settings["video"]["screenRes"]["width"].Float();
  22. int resY = settings["video"]["screenRes"]["height"].Float();
  23. int resIndex = ui->comboBoxResolution->findText(QString("%1x%2").arg(resX).arg(resY));
  24. ui->comboBoxResolution->setCurrentIndex(resIndex);
  25. ui->comboBoxFullScreen->setCurrentIndex(settings["video"]["fullscreen"].Bool());
  26. ui->comboBoxShowIntro->setCurrentIndex(settings["video"]["showIntro"].Bool());
  27. int neutralAIIndex = ui->comboBoxNeutralAI->findText(QString::fromUtf8(settings["server"]["neutralAI"].String().c_str()));
  28. int playerAIIndex = ui->comboBoxPlayerAI->findText(QString::fromUtf8(settings["server"]["playerAI"].String().c_str()));
  29. ui->comboBoxNeutralAI->setCurrentIndex(neutralAIIndex);
  30. ui->comboBoxPlayerAI->setCurrentIndex(playerAIIndex);
  31. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Float());
  32. // all calls to plainText will trigger textChanged() signal overwriting config. Create backup before editing widget
  33. JsonNode urls = settings["launcher"]["repositoryURL"];
  34. ui->plainTextEditRepos->clear();
  35. for (auto entry : urls.Vector())
  36. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  37. ui->lineEditUserDataDir->setText(QString::fromUtf8(VCMIDirs::get().userDataPath().c_str()));
  38. ui->lineEditGameDir->setText(QString::fromUtf8(M_DATA_DIR));
  39. ui->lineEditTempDir->setText(QString::fromUtf8(VCMIDirs::get().userCachePath().c_str()));
  40. std::string encoding = settings["general"]["encoding"].String();
  41. size_t encodingIndex = boost::range::find(knownEncodingsList, encoding) - knownEncodingsList;
  42. if (encodingIndex < ui->comboBoxEncoding->count())
  43. ui->comboBoxEncoding->setCurrentIndex(encodingIndex);
  44. }
  45. CSettingsView::CSettingsView(QWidget *parent) :
  46. QWidget(parent),
  47. ui(new Ui::CSettingsView)
  48. {
  49. ui->setupUi(this);
  50. loadSettings();
  51. }
  52. CSettingsView::~CSettingsView()
  53. {
  54. delete ui;
  55. }
  56. void CSettingsView::on_comboBoxResolution_currentIndexChanged(const QString &arg1)
  57. {
  58. QStringList list = arg1.split("x");
  59. Settings node = settings.write["video"]["screenRes"];
  60. node["width"].Float() = list[0].toInt();
  61. node["height"].Float() = list[1].toInt();
  62. }
  63. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  64. {
  65. Settings node = settings.write["video"]["fullscreen"];
  66. node->Bool() = index;
  67. }
  68. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString &arg1)
  69. {
  70. Settings node = settings.write["server"]["playerAI"];
  71. node->String() = arg1.toUtf8().data();
  72. }
  73. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString &arg1)
  74. {
  75. Settings node = settings.write["server"]["neutralAI"];
  76. node->String() = arg1.toUtf8().data();
  77. }
  78. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  79. {
  80. Settings node = settings.write["server"]["port"];
  81. node->Float() = arg1;
  82. }
  83. void CSettingsView::on_plainTextEditRepos_textChanged()
  84. {
  85. Settings node = settings.write["launcher"]["repositoryURL"];
  86. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  87. node->Vector().clear();
  88. for (QString line : list)
  89. {
  90. if (line.trimmed().size() > 0)
  91. {
  92. JsonNode entry;
  93. entry.String() = line.trimmed().toUtf8().data();
  94. node->Vector().push_back(entry);
  95. }
  96. }
  97. }
  98. void CSettingsView::on_comboBoxEncoding_currentIndexChanged(int index)
  99. {
  100. Settings node = settings.write["general"]["encoding"];
  101. node->String() = knownEncodingsList[index];
  102. }
  103. void CSettingsView::on_openTempDir_clicked()
  104. {
  105. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  106. }
  107. void CSettingsView::on_openUserDataDir_clicked()
  108. {
  109. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  110. }
  111. void CSettingsView::on_openGameDataDir_clicked()
  112. {
  113. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  114. }
  115. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  116. {
  117. Settings node = settings.write["video"]["showIntro"];
  118. node->Bool() = index;
  119. }
  120. void CSettingsView::on_changeGameDataDir_clicked()
  121. {
  122. }