csettingsview_moc.cpp 10 KB

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