From 2798ab8ea0a4d9ab57e746a0edd7e53bd0d87262 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Tue, 26 Dec 2017 19:26:52 +0800 Subject: [PATCH] support reseting VNote in settings Notebooks information will not be reset. --- src/dialog/vsettingsdialog.cpp | 39 ++++++++++++++++++++++++++++++++++ src/dialog/vsettingsdialog.h | 5 +++++ src/vconfigmanager.cpp | 34 ++++++++++++++++++++++++++++- src/vconfigmanager.h | 10 +++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/dialog/vsettingsdialog.cpp b/src/dialog/vsettingsdialog.cpp index d6ab3d79..d1eb5abb 100644 --- a/src/dialog/vsettingsdialog.cpp +++ b/src/dialog/vsettingsdialog.cpp @@ -15,11 +15,20 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent) m_tabs = new QStackedLayout(); + // Reset VNote. + m_resetVNoteBtn = new QPushButton(tr("Reset VNote"), this); + m_resetVNoteBtn->setProperty("DangerBtn", true); + m_resetVNoteBtn->setToolTip(tr("Reset all the configurations of VNote")); + connect(m_resetVNoteBtn, &QPushButton::clicked, + this, &VSettingsDialog::resetVNote); + m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(m_btnBox, &QDialogButtonBox::accepted, this, &VSettingsDialog::saveConfiguration); connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); m_btnBox->button(QDialogButtonBox::Ok)->setProperty("SpecialBtn", true); + m_btnBox->addButton(m_resetVNoteBtn, QDialogButtonBox::ResetRole); + QHBoxLayout *tabLayout = new QHBoxLayout(); tabLayout->addWidget(m_tabList); tabLayout->addLayout(m_tabs); @@ -57,6 +66,36 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent) loadConfiguration(); } +void VSettingsDialog::resetVNote() +{ + int ret = VUtils::showMessage(QMessageBox::Warning, + tr("Warning"), + tr("Are you sure to reset VNote?"), + tr("All configurations (except notebooks information) " + "will be reset to default values. " + "It is UNRECOVERABLE!"), + QMessageBox::Ok | QMessageBox::Cancel, + QMessageBox::Cancel, + this, + MessageBoxType::Danger); + + if (ret == QMessageBox::Cancel) { + return; + } + + g_config->resetConfigurations(); + + VUtils::showMessage(QMessageBox::Information, + tr("Information"), + tr("Please restart VNote to make it work."), + tr("Any change to VNote before restart will be lost!"), + QMessageBox::Ok, + QMessageBox::Ok, + this); + + reject(); +} + void VSettingsDialog::addTab(QWidget *p_widget, const QString &p_label) { int idx = m_tabs->addWidget(p_widget); diff --git a/src/dialog/vsettingsdialog.h b/src/dialog/vsettingsdialog.h index 2406134f..c4afa557 100644 --- a/src/dialog/vsettingsdialog.h +++ b/src/dialog/vsettingsdialog.h @@ -150,6 +150,8 @@ public: private slots: void saveConfiguration(); + void resetVNote(); + private: void loadConfiguration(); @@ -158,6 +160,9 @@ private: QStackedLayout *m_tabs; QListWidget *m_tabList; QDialogButtonBox *m_btnBox; + + // Reset all the configuration of VNote. + QPushButton *m_resetVNoteBtn; }; #endif // VSETTINGSDIALOG_H diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 310bde90..6fbed427 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -51,7 +51,8 @@ VConfigManager::VConfigManager(QObject *p_parent) : QObject(p_parent), userSettings(NULL), defaultSettings(NULL), - m_sessionSettings(NULL) + m_sessionSettings(NULL), + m_hasReset(false) { } @@ -433,6 +434,10 @@ QVariant VConfigManager::getConfigFromSettings(const QString §ion, const QSt void VConfigManager::setConfigToSettings(const QString §ion, const QString &key, const QVariant &value) { + if (m_hasReset) { + return; + } + // Set the user-scoped config file setConfigToSettingsBySectionKey(userSettings, section, key, value); qDebug() << "set user config:" << (section + "/" + key) << value; @@ -465,6 +470,10 @@ void VConfigManager::setConfigToSessionSettings(const QString &p_section, const QString &p_key, const QVariant &p_value) { + if (m_hasReset) { + return; + } + setConfigToSettingsBySectionKey(m_sessionSettings, p_section, p_key, @@ -1107,6 +1116,10 @@ QVector VConfigManager::getLastOpenedFiles() void VConfigManager::setLastOpenedFiles(const QVector &p_files) { + if (m_hasReset) { + return; + } + const QString section("last_opened_files"); // Clear it first @@ -1312,3 +1325,22 @@ void VConfigManager::initCodeBlockCssStyles() m_codeBlockCssStyles.insert(fi.completeBaseName(), dir.filePath(item)); } } + +void VConfigManager::resetConfigurations() +{ + // Clear userSettings. + userSettings->clear(); + + // Clear m_sessionSettings except the notebooks information. + clearGroupOfSettings(m_sessionSettings, "last_opened_files"); + clearGroupOfSettings(m_sessionSettings, "geometry"); + + m_hasReset = true; +} + +void VConfigManager::clearGroupOfSettings(QSettings *p_settings, const QString &p_group) +{ + p_settings->beginGroup(p_group); + p_settings->remove(""); + p_settings->endGroup(); +} diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 679b648d..4a4dd87f 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -81,7 +81,9 @@ public: static QJsonObject readDirectoryConfig(const QString &path); static bool writeDirectoryConfig(const QString &path, const QJsonObject &configJson); + static bool directoryConfigExist(const QString &path); + static bool deleteDirectoryConfig(const QString &path); // Get the path of the folder used to store default notebook. @@ -98,6 +100,9 @@ public: // CSS style for data in label. static const QString c_dataTextStyle; + // Reset the configuratio files. + void resetConfigurations(); + QFont getMdEditFont() const; QPalette getMdEditPalette() const; @@ -442,6 +447,8 @@ private: const QString &p_key, const QVariant &p_value); + void clearGroupOfSettings(QSettings *p_settings, const QString &p_group); + // Init defaultSettings, userSettings, and m_sessionSettings. void initSettings(); @@ -806,6 +813,9 @@ private: // Whether close note before open it via external editor. bool m_closeBeforeExternalEditor; + // Whether user has reset the configurations. + bool m_hasReset; + // The name of the config file in each directory, obsolete. // Use c_dirConfigFile instead. static const QString c_obsoleteDirConfigFile;