csettingsview_moc.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "mainwindow_moc.h"
  14. #include "../jsonutils.h"
  15. #include "../languages.h"
  16. #include "../launcherdirs.h"
  17. #include "../updatedialog_moc.h"
  18. #include <QFileInfo>
  19. #include <QGuiApplication>
  20. #include "../../lib/CConfigHandler.h"
  21. #include "../../lib/VCMIDirs.h"
  22. namespace
  23. {
  24. QString resolutionToString(const QSize & resolution)
  25. {
  26. return QString{"%1x%2"}.arg(resolution.width()).arg(resolution.height());
  27. }
  28. static const std::string cursorTypesList[] =
  29. {
  30. "auto",
  31. "hardware",
  32. "software"
  33. };
  34. }
  35. void CSettingsView::setDisplayList()
  36. {
  37. QStringList list;
  38. for (const auto screen : QGuiApplication::screens())
  39. list << QString{"%1 - %2"}.arg(screen->name(), resolutionToString(screen->size()));
  40. if(list.count() < 2)
  41. {
  42. ui->comboBoxDisplayIndex->hide();
  43. ui->labelDisplayIndex->hide();
  44. fillValidResolutionsForScreen(0);
  45. }
  46. else
  47. {
  48. int displayIndex = settings["video"]["displayIndex"].Integer();
  49. ui->comboBoxDisplayIndex->addItems(list);
  50. // calls fillValidResolutions() in slot
  51. ui->comboBoxDisplayIndex->setCurrentIndex(displayIndex);
  52. }
  53. }
  54. void CSettingsView::loadSettings()
  55. {
  56. ui->comboBoxShowIntro->setCurrentIndex(settings["video"]["showIntro"].Bool());
  57. #ifdef Q_OS_IOS
  58. ui->comboBoxFullScreen->setCurrentIndex(true);
  59. ui->comboBoxFullScreen->setDisabled(true);
  60. #else
  61. ui->comboBoxFullScreen->setCurrentIndex(settings["video"]["fullscreen"].Bool());
  62. if (settings["video"]["realFullscreen"].Bool())
  63. ui->comboBoxFullScreen->setCurrentIndex(2);
  64. #endif
  65. ui->comboBoxFriendlyAI->setCurrentText(QString::fromStdString(settings["server"]["friendlyAI"].String()));
  66. ui->comboBoxNeutralAI->setCurrentText(QString::fromStdString(settings["server"]["neutralAI"].String()));
  67. ui->comboBoxEnemyAI->setCurrentText(QString::fromStdString(settings["server"]["enemyAI"].String()));
  68. ui->comboBoxPlayerAI->setCurrentText(QString::fromStdString(settings["server"]["playerAI"].String()));
  69. ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer());
  70. ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool());
  71. JsonNode urls = settings["launcher"]["repositoryURL"];
  72. ui->plainTextEditRepos->blockSignals(true); // Do not report loading as change of data
  73. ui->plainTextEditRepos->clear();
  74. for(auto entry : urls.Vector())
  75. ui->plainTextEditRepos->appendPlainText(QString::fromUtf8(entry.String().c_str()));
  76. ui->plainTextEditRepos->blockSignals(false);
  77. ui->lineEditUserDataDir->setText(pathToQString(VCMIDirs::get().userDataPath()));
  78. ui->lineEditGameDir->setText(pathToQString(VCMIDirs::get().binaryPath()));
  79. ui->lineEditTempDir->setText(pathToQString(VCMIDirs::get().userLogsPath()));
  80. ui->comboBoxAutoSave->setCurrentIndex(settings["general"]["saveFrequency"].Integer() > 0 ? 1 : 0);
  81. Languages::fillLanguages(ui->comboBoxLanguage);
  82. std::string cursorType = settings["video"]["cursor"].String();
  83. size_t cursorTypeIndex = boost::range::find(cursorTypesList, cursorType) - cursorTypesList;
  84. ui->comboBoxCursorType->setCurrentIndex((int)cursorTypeIndex);
  85. }
  86. void CSettingsView::fillValidResolutions(bool isExtraResolutionsModEnabled)
  87. {
  88. this->isExtraResolutionsModEnabled = isExtraResolutionsModEnabled;
  89. fillValidResolutionsForScreen(ui->comboBoxDisplayIndex->isVisible() ? ui->comboBoxDisplayIndex->currentIndex() : 0);
  90. }
  91. void CSettingsView::fillValidResolutionsForScreen(int screenIndex)
  92. {
  93. ui->comboBoxResolution->blockSignals(true); // avoid saving wrong resolution after adding first item from the list
  94. ui->comboBoxResolution->clear();
  95. // TODO: read available resolutions from all mods
  96. QVariantList resolutions;
  97. if(isExtraResolutionsModEnabled)
  98. {
  99. const auto extrasResolutionsPath = settings["launcher"]["extraResolutionsModPath"].String().c_str();
  100. const auto extrasResolutionsJson = JsonUtils::JsonFromFile(CLauncherDirs::get().modsPath() + extrasResolutionsPath);
  101. resolutions = extrasResolutionsJson.toMap().value(QLatin1String{"GUISettings"}).toList();
  102. }
  103. if(resolutions.isEmpty())
  104. {
  105. ui->comboBoxResolution->blockSignals(false);
  106. ui->comboBoxResolution->addItem(resolutionToString({800, 600}));
  107. return;
  108. }
  109. const auto screens = qGuiApp->screens();
  110. const auto currentScreen = screenIndex < screens.size() ? screens[screenIndex] : qGuiApp->primaryScreen();
  111. const auto screenSize = currentScreen->size();
  112. MAYBE_UNUSED(screenSize);
  113. for(const auto & entry : resolutions)
  114. {
  115. const auto resolutionMap = entry.toMap().value(QLatin1String{"resolution"}).toMap();
  116. if(resolutionMap.isEmpty())
  117. continue;
  118. const auto widthValue = resolutionMap[QLatin1String{"x"}];
  119. const auto heightValue = resolutionMap[QLatin1String{"y"}];
  120. if(!widthValue.isValid() || !heightValue.isValid())
  121. continue;
  122. const QSize resolution{widthValue.toInt(), heightValue.toInt()};
  123. #ifndef VCMI_IOS
  124. if(screenSize.width() < resolution.width() || screenSize.height() < resolution.height())
  125. continue;
  126. #endif
  127. ui->comboBoxResolution->addItem(resolutionToString(resolution));
  128. }
  129. int resX = settings["video"]["screenRes"]["width"].Integer();
  130. int resY = settings["video"]["screenRes"]["height"].Integer();
  131. int resIndex = ui->comboBoxResolution->findText(resolutionToString({resX, resY}));
  132. ui->comboBoxResolution->setCurrentIndex(resIndex);
  133. ui->comboBoxResolution->blockSignals(false);
  134. // if selected resolution no longer exists, force update value to the first resolution
  135. if(resIndex == -1)
  136. ui->comboBoxResolution->setCurrentIndex(0);
  137. }
  138. CSettingsView::CSettingsView(QWidget * parent)
  139. : QWidget(parent), ui(new Ui::CSettingsView)
  140. {
  141. ui->setupUi(this);
  142. ui->lineEditBuildVersion->setText(QString::fromStdString(GameConstants::VCMI_VERSION));
  143. loadSettings();
  144. }
  145. CSettingsView::~CSettingsView()
  146. {
  147. delete ui;
  148. }
  149. void CSettingsView::on_comboBoxResolution_currentTextChanged(const QString & arg1)
  150. {
  151. QStringList list = arg1.split("x");
  152. Settings node = settings.write["video"]["screenRes"];
  153. node["width"].Float() = list[0].toInt();
  154. node["height"].Float() = list[1].toInt();
  155. }
  156. void CSettingsView::on_comboBoxFullScreen_currentIndexChanged(int index)
  157. {
  158. Settings nodeFullscreen = settings.write["video"]["fullscreen"];
  159. Settings nodeRealFullscreen = settings.write["video"]["realFullscreen"];
  160. nodeFullscreen->Bool() = (index != 0);
  161. nodeRealFullscreen->Bool() = (index == 2);
  162. }
  163. void CSettingsView::on_comboBoxAutoCheck_currentIndexChanged(int index)
  164. {
  165. Settings node = settings.write["launcher"]["autoCheckRepositories"];
  166. node->Bool() = index;
  167. }
  168. void CSettingsView::on_comboBoxDisplayIndex_currentIndexChanged(int index)
  169. {
  170. Settings node = settings.write["video"];
  171. node["displayIndex"].Float() = index;
  172. fillValidResolutionsForScreen(index);
  173. }
  174. void CSettingsView::on_comboBoxPlayerAI_currentIndexChanged(const QString & arg1)
  175. {
  176. Settings node = settings.write["server"]["playerAI"];
  177. node->String() = arg1.toUtf8().data();
  178. }
  179. void CSettingsView::on_comboBoxFriendlyAI_currentIndexChanged(const QString & arg1)
  180. {
  181. Settings node = settings.write["server"]["friendlyAI"];
  182. node->String() = arg1.toUtf8().data();
  183. }
  184. void CSettingsView::on_comboBoxNeutralAI_currentIndexChanged(const QString & arg1)
  185. {
  186. Settings node = settings.write["server"]["neutralAI"];
  187. node->String() = arg1.toUtf8().data();
  188. }
  189. void CSettingsView::on_comboBoxEnemyAI_currentIndexChanged(const QString & arg1)
  190. {
  191. Settings node = settings.write["server"]["enemyAI"];
  192. node->String() = arg1.toUtf8().data();
  193. }
  194. void CSettingsView::on_spinBoxNetworkPort_valueChanged(int arg1)
  195. {
  196. Settings node = settings.write["server"]["port"];
  197. node->Float() = arg1;
  198. }
  199. void CSettingsView::on_plainTextEditRepos_textChanged()
  200. {
  201. Settings node = settings.write["launcher"]["repositoryURL"];
  202. QStringList list = ui->plainTextEditRepos->toPlainText().split('\n');
  203. node->Vector().clear();
  204. for(QString line : list)
  205. {
  206. if(line.trimmed().size() > 0)
  207. {
  208. JsonNode entry;
  209. entry.String() = line.trimmed().toUtf8().data();
  210. node->Vector().push_back(entry);
  211. }
  212. }
  213. }
  214. void CSettingsView::on_openTempDir_clicked()
  215. {
  216. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditTempDir->text()).absoluteFilePath()));
  217. }
  218. void CSettingsView::on_openUserDataDir_clicked()
  219. {
  220. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditUserDataDir->text()).absoluteFilePath()));
  221. }
  222. void CSettingsView::on_openGameDataDir_clicked()
  223. {
  224. QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(ui->lineEditGameDir->text()).absoluteFilePath()));
  225. }
  226. void CSettingsView::on_comboBoxShowIntro_currentIndexChanged(int index)
  227. {
  228. Settings node = settings.write["video"]["showIntro"];
  229. node->Bool() = index;
  230. }
  231. void CSettingsView::on_changeGameDataDir_clicked()
  232. {
  233. }
  234. void CSettingsView::on_comboBoxAutoSave_currentIndexChanged(int index)
  235. {
  236. Settings node = settings.write["general"]["saveFrequency"];
  237. node->Integer() = index;
  238. }
  239. void CSettingsView::on_updatesButton_clicked()
  240. {
  241. UpdateDialog::showUpdateDialog(true);
  242. }
  243. void CSettingsView::on_comboBoxLanguage_currentIndexChanged(int index)
  244. {
  245. Settings node = settings.write["general"]["language"];
  246. QString selectedLanguage = ui->comboBoxLanguage->itemData(index).toString();
  247. node->String() = selectedLanguage.toStdString();
  248. if(auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow()))
  249. mainWindow->updateTranslation();
  250. }
  251. void CSettingsView::changeEvent(QEvent *event)
  252. {
  253. if(event->type() == QEvent::LanguageChange)
  254. {
  255. ui->retranslateUi(this);
  256. Languages::fillLanguages(ui->comboBoxLanguage);
  257. }
  258. QWidget::changeEvent(event);
  259. }
  260. void CSettingsView::on_comboBoxCursorType_currentIndexChanged(int index)
  261. {
  262. Settings node = settings.write["video"]["cursor"];
  263. node->String() = cursorTypesList[index];
  264. }
  265. void CSettingsView::on_listWidgetSettings_currentRowChanged(int currentRow)
  266. {
  267. QVector<QWidget*> targetWidgets = {
  268. ui->labelGeneral,
  269. ui->labelVideo,
  270. ui->labelArtificialIntelligence,
  271. ui->labelDataDirs,
  272. ui->labelRepositories
  273. };
  274. QWidget * currentTarget = targetWidgets[currentRow];
  275. // We want to scroll in a way that will put target widget in topmost visible position
  276. // To show not just header, but all settings in this group as well
  277. // In order to do that, let's scroll to the very bottom and the scroll back up until target widget is visible
  278. int maxPosition = ui->settingsScrollArea->verticalScrollBar()->maximum();
  279. ui->settingsScrollArea->verticalScrollBar()->setValue(maxPosition);
  280. ui->settingsScrollArea->ensureWidgetVisible(currentTarget, 5, 5);
  281. }