Browse Source

support reseting VNote in settings

Notebooks information will not be reset.
Le Tan 8 years ago
parent
commit
2798ab8ea0
4 changed files with 87 additions and 1 deletions
  1. 39 0
      src/dialog/vsettingsdialog.cpp
  2. 5 0
      src/dialog/vsettingsdialog.h
  3. 33 1
      src/vconfigmanager.cpp
  4. 10 0
      src/vconfigmanager.h

+ 39 - 0
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);

+ 5 - 0
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

+ 33 - 1
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 &section, const QSt
 
 void VConfigManager::setConfigToSettings(const QString &section, 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<VFileSessionInfo> VConfigManager::getLastOpenedFiles()
 
 void VConfigManager::setLastOpenedFiles(const QVector<VFileSessionInfo> &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();
+}

+ 10 - 0
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;