From c8bb8b0aae6b7fa361119a9e7315fb3fdb3489be Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Tue, 14 Sep 2021 22:10:40 +0200 Subject: [PATCH] add reset paths button to settings (#4384) --- cockatrice/src/dlg_settings.cpp | 64 ++++++++++++++++++++++---------- cockatrice/src/dlg_settings.h | 3 ++ cockatrice/src/settingscache.cpp | 55 ++++++++++++++++++--------- cockatrice/src/settingscache.h | 3 ++ 4 files changed, 88 insertions(+), 37 deletions(-) diff --git a/cockatrice/src/dlg_settings.cpp b/cockatrice/src/dlg_settings.cpp index 85b0f8a5..bfea023a 100644 --- a/cockatrice/src/dlg_settings.cpp +++ b/cockatrice/src/dlg_settings.cpp @@ -43,7 +43,8 @@ GeneralSettingsPage::GeneralSettingsPage() { - QString setLanguage = SettingsCache::instance().getLang(); + SettingsCache &settings = SettingsCache::instance(); + QString setLanguage = settings.getLang(); QStringList qmFiles = findQmFiles(); for (int i = 0; i < qmFiles.size(); i++) { QString langName = languageName(qmFiles[i]); @@ -54,34 +55,34 @@ GeneralSettingsPage::GeneralSettingsPage() } // updates - QList channels = SettingsCache::instance().getUpdateReleaseChannels(); + QList channels = settings.getUpdateReleaseChannels(); foreach (ReleaseChannel *chan, channels) { updateReleaseChannelBox.insertItem(chan->getIndex(), tr(chan->getName().toUtf8())); } - updateReleaseChannelBox.setCurrentIndex(SettingsCache::instance().getUpdateReleaseChannel()->getIndex()); + updateReleaseChannelBox.setCurrentIndex(settings.getUpdateReleaseChannel()->getIndex()); - updateNotificationCheckBox.setChecked(SettingsCache::instance().getNotifyAboutUpdates()); - newVersionOracleCheckBox.setChecked(SettingsCache::instance().getNotifyAboutNewVersion()); + updateNotificationCheckBox.setChecked(settings.getNotifyAboutUpdates()); + newVersionOracleCheckBox.setChecked(settings.getNotifyAboutNewVersion()); // pixmap cache pixmapCacheEdit.setMinimum(PIXMAPCACHE_SIZE_MIN); // 2047 is the max value to avoid overflowing of QPixmapCache::setCacheLimit(int size) pixmapCacheEdit.setMaximum(PIXMAPCACHE_SIZE_MAX); pixmapCacheEdit.setSingleStep(64); - pixmapCacheEdit.setValue(SettingsCache::instance().getPixmapCacheSize()); + pixmapCacheEdit.setValue(settings.getPixmapCacheSize()); pixmapCacheEdit.setSuffix(" MB"); - showTipsOnStartup.setChecked(SettingsCache::instance().getShowTipsOnStartup()); + showTipsOnStartup.setChecked(settings.getShowTipsOnStartup()); connect(&languageBox, SIGNAL(currentIndexChanged(int)), this, SLOT(languageBoxChanged(int))); - connect(&pixmapCacheEdit, SIGNAL(valueChanged(int)), &SettingsCache::instance(), SLOT(setPixmapCacheSize(int))); - connect(&updateReleaseChannelBox, SIGNAL(currentIndexChanged(int)), &SettingsCache::instance(), + connect(&pixmapCacheEdit, SIGNAL(valueChanged(int)), &settings, SLOT(setPixmapCacheSize(int))); + connect(&updateReleaseChannelBox, SIGNAL(currentIndexChanged(int)), &settings, SLOT(setUpdateReleaseChannel(int))); - connect(&updateNotificationCheckBox, SIGNAL(stateChanged(int)), &SettingsCache::instance(), + connect(&updateNotificationCheckBox, SIGNAL(stateChanged(int)), &settings, SLOT(setNotifyAboutUpdate(int))); - connect(&newVersionOracleCheckBox, SIGNAL(stateChanged(int)), &SettingsCache::instance(), + connect(&newVersionOracleCheckBox, SIGNAL(stateChanged(int)), &settings, SLOT(setNotifyAboutNewVersion(int))); - connect(&showTipsOnStartup, SIGNAL(clicked(bool)), &SettingsCache::instance(), SLOT(setShowTipsOnStartup(bool))); + connect(&showTipsOnStartup, SIGNAL(clicked(bool)), &settings, SLOT(setShowTipsOnStartup(bool))); auto *personalGrid = new QGridLayout; personalGrid->addWidget(&languageLabel, 0, 0); @@ -97,37 +98,38 @@ GeneralSettingsPage::GeneralSettingsPage() personalGroupBox = new QGroupBox; personalGroupBox->setLayout(personalGrid); - deckPathEdit = new QLineEdit(SettingsCache::instance().getDeckPath()); + deckPathEdit = new QLineEdit(settings.getDeckPath()); deckPathEdit->setReadOnly(true); QPushButton *deckPathButton = new QPushButton("..."); connect(deckPathButton, SIGNAL(clicked()), this, SLOT(deckPathButtonClicked())); - replaysPathEdit = new QLineEdit(SettingsCache::instance().getReplaysPath()); + replaysPathEdit = new QLineEdit(settings.getReplaysPath()); replaysPathEdit->setReadOnly(true); QPushButton *replaysPathButton = new QPushButton("..."); connect(replaysPathButton, SIGNAL(clicked()), this, SLOT(replaysPathButtonClicked())); - picsPathEdit = new QLineEdit(SettingsCache::instance().getPicsPath()); + picsPathEdit = new QLineEdit(settings.getPicsPath()); picsPathEdit->setReadOnly(true); QPushButton *picsPathButton = new QPushButton("..."); connect(picsPathButton, SIGNAL(clicked()), this, SLOT(picsPathButtonClicked())); - cardDatabasePathEdit = new QLineEdit(SettingsCache::instance().getCardDatabasePath()); + cardDatabasePathEdit = new QLineEdit(settings.getCardDatabasePath()); cardDatabasePathEdit->setReadOnly(true); QPushButton *cardDatabasePathButton = new QPushButton("..."); connect(cardDatabasePathButton, SIGNAL(clicked()), this, SLOT(cardDatabasePathButtonClicked())); - customCardDatabasePathEdit = new QLineEdit(SettingsCache::instance().getCustomCardDatabasePath()); + customCardDatabasePathEdit = new QLineEdit(settings.getCustomCardDatabasePath()); customCardDatabasePathEdit->setReadOnly(true); QPushButton *customCardDatabasePathButton = new QPushButton("..."); connect(customCardDatabasePathButton, SIGNAL(clicked()), this, SLOT(customCardDatabaseButtonClicked())); - tokenDatabasePathEdit = new QLineEdit(SettingsCache::instance().getTokenDatabasePath()); + tokenDatabasePathEdit = new QLineEdit(settings.getTokenDatabasePath()); tokenDatabasePathEdit->setReadOnly(true); QPushButton *tokenDatabasePathButton = new QPushButton("..."); connect(tokenDatabasePathButton, SIGNAL(clicked()), this, SLOT(tokenDatabasePathButtonClicked())); - if (SettingsCache::instance().getIsPortableBuild()) { + bool isPortable = settings.getIsPortableBuild(); + if (isPortable) { deckPathEdit->setEnabled(false); replaysPathEdit->setEnabled(false); picsPathEdit->setEnabled(false); @@ -139,8 +141,13 @@ GeneralSettingsPage::GeneralSettingsPage() replaysPathButton->setVisible(false); picsPathButton->setVisible(false); cardDatabasePathButton->setVisible(false); - customCardDatabasePathEdit->setVisible(false); + customCardDatabasePathButton->setVisible(false); tokenDatabasePathButton->setVisible(false); + } else { + resetAllPathsButton = new QPushButton(tr("Reset all paths")); + connect(resetAllPathsButton, SIGNAL(clicked()), this, SLOT(resetAllPathsClicked())); + allPathsResetLabel = new QLabel(tr("All paths have been reset")); + allPathsResetLabel->setVisible(false); } auto *pathsGrid = new QGridLayout; @@ -162,6 +169,10 @@ GeneralSettingsPage::GeneralSettingsPage() pathsGrid->addWidget(&tokenDatabasePathLabel, 5, 0); pathsGrid->addWidget(tokenDatabasePathEdit, 5, 1); pathsGrid->addWidget(tokenDatabasePathButton, 5, 2); + if (!isPortable) { + pathsGrid->addWidget(resetAllPathsButton, 6, 0); + pathsGrid->addWidget(allPathsResetLabel, 6, 1); + } pathsGroupBox = new QGroupBox; pathsGroupBox->setLayout(pathsGrid); @@ -251,6 +262,19 @@ void GeneralSettingsPage::tokenDatabasePathButtonClicked() SettingsCache::instance().setTokenDatabasePath(path); } +void GeneralSettingsPage::resetAllPathsClicked() +{ + SettingsCache &settings = SettingsCache::instance(); + settings.resetPaths(); + deckPathEdit->setText(settings.getDeckPath()); + replaysPathEdit->setText(settings.getReplaysPath()); + picsPathEdit->setText(settings.getPicsPath()); + cardDatabasePathEdit->setText(settings.getCardDatabasePath()); + customCardDatabasePathEdit->setText(settings.getCustomCardDatabasePath()); + tokenDatabasePathEdit->setText(settings.getTokenDatabasePath()); + allPathsResetLabel->setVisible(true); +} + void GeneralSettingsPage::languageBoxChanged(int index) { SettingsCache::instance().setLang(languageBox.itemData(index).toString()); diff --git a/cockatrice/src/dlg_settings.h b/cockatrice/src/dlg_settings.h index c23a2969..36ea93ab 100644 --- a/cockatrice/src/dlg_settings.h +++ b/cockatrice/src/dlg_settings.h @@ -44,6 +44,7 @@ private slots: void cardDatabasePathButtonClicked(); void customCardDatabaseButtonClicked(); void tokenDatabasePathButtonClicked(); + void resetAllPathsClicked(); void languageBoxChanged(int index); private: @@ -55,6 +56,8 @@ private: QLineEdit *cardDatabasePathEdit; QLineEdit *customCardDatabasePathEdit; QLineEdit *tokenDatabasePathEdit; + QPushButton *resetAllPathsButton; + QLabel *allPathsResetLabel; QSpinBox pixmapCacheEdit; QGroupBox *personalGroupBox; QGroupBox *pathsGroupBox; diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index 045dce37..6caf61a0 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -128,7 +128,7 @@ QString SettingsCache::getSafeConfigPath(QString configEntry, QString defaultPat QString tmp = settings->value(configEntry).toString(); // if the config settings is empty or refers to a not-existing folder, // ensure that the defaut path exists and return it - if (!QDir(tmp).exists() || tmp.isEmpty()) { + if (tmp.isEmpty() || !QDir(tmp).exists()) { if (!QDir().mkpath(defaultPath)) qDebug() << "[SettingsCache] Could not create folder:" << defaultPath; tmp = defaultPath; @@ -156,7 +156,6 @@ SettingsCache::SettingsCache() // define a dummy context that will be used where needed QString dummy = QT_TRANSLATE_NOOP("i18n", "English"); - QString dataPath = getDataPath(); QString settingsPath = getSettingsPath(); settings = new QSettings(settingsPath + "global.ini", QSettings::IniFormat, this); shortcutsSettings = new ShortcutsSettings(settingsPath, this); @@ -190,21 +189,7 @@ SettingsCache::SettingsCache() seenTips.append(tipNumber.toInt()); } - deckPath = getSafeConfigPath("paths/decks", dataPath + "/decks/"); - replaysPath = getSafeConfigPath("paths/replays", dataPath + "/replays/"); - themesPath = getSafeConfigPath("paths/themes", dataPath + "/themes/"); - picsPath = getSafeConfigPath("paths/pics", dataPath + "/pics/"); - // this has never been exposed as an user-configurable setting - if (picsPath.endsWith("/")) { - customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "CUSTOM/"); - } else { - customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "/CUSTOM/"); - } - customCardDatabasePath = getSafeConfigPath("paths/customsets", dataPath + "/customsets/"); - - cardDatabasePath = getSafeConfigFilePath("paths/carddatabase", dataPath + "/cards.xml"); - tokenDatabasePath = getSafeConfigFilePath("paths/tokendatabase", dataPath + "/tokens.xml"); - spoilerDatabasePath = getSafeConfigFilePath("paths/spoilerdatabase", dataPath + "/spoiler.xml"); + loadPaths(); themeName = settings->value("theme/name").toString(); @@ -395,6 +380,7 @@ void SettingsCache::setCustomCardDatabasePath(const QString &_customCardDatabase { customCardDatabasePath = _customCardDatabasePath; settings->setValue("paths/customsets", customCardDatabasePath); + emit cardDatabasePathChanged(); } void SettingsCache::setPicsPath(const QString &_picsPath) @@ -987,6 +973,41 @@ void SettingsCache::setMaxFontSize(int _max) settings->setValue("game/maxfontsize", maxFontSize); } +void SettingsCache::loadPaths() +{ + QString dataPath = getDataPath(); + deckPath = getSafeConfigPath("paths/decks", dataPath + "/decks/"); + replaysPath = getSafeConfigPath("paths/replays", dataPath + "/replays/"); + themesPath = getSafeConfigPath("paths/themes", dataPath + "/themes/"); + picsPath = getSafeConfigPath("paths/pics", dataPath + "/pics/"); + // this has never been exposed as an user-configurable setting + if (picsPath.endsWith("/")) { + customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "CUSTOM/"); + } else { + customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "/CUSTOM/"); + } + customCardDatabasePath = getSafeConfigPath("paths/customsets", dataPath + "/customsets/"); + + cardDatabasePath = getSafeConfigFilePath("paths/carddatabase", dataPath + "/cards.xml"); + tokenDatabasePath = getSafeConfigFilePath("paths/tokendatabase", dataPath + "/tokens.xml"); + spoilerDatabasePath = getSafeConfigFilePath("paths/spoilerdatabase", dataPath + "/spoiler.xml"); +} + +void SettingsCache::resetPaths() +{ + QStringList databasePaths{customCardDatabasePath, cardDatabasePath, spoilerDatabasePath, tokenDatabasePath}; + QString picsPath_ = picsPath; + settings->remove("paths"); // removes all keys in paths/* + loadPaths(); + if (databasePaths != + QStringList{customCardDatabasePath, cardDatabasePath, spoilerDatabasePath, tokenDatabasePath}) { + emit cardDatabasePathChanged(); + } + if (picsPath_ != picsPath) { + emit picsPathChanged(); + } +} + SettingsCache &SettingsCache::instance() { return *settingsCache; diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index e9478d76..94279795 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -129,6 +129,7 @@ private: void translateLegacySettings(); QString getSafeConfigPath(QString configEntry, QString defaultPath) const; QString getSafeConfigFilePath(QString configEntry, QString defaultPath) const; + void loadPaths(); bool rememberGameSettings; QList releaseChannels; bool isPortableBuild; @@ -473,6 +474,8 @@ public: } static SettingsCache &instance(); + void resetPaths(); + public slots: void setDownloadSpoilerStatus(bool _spoilerStatus);