support reseting VNote in settings

Notebooks information will not be reset.
This commit is contained in:
Le Tan 2017-12-26 19:26:52 +08:00
parent 6ca70895ac
commit 2798ab8ea0
4 changed files with 87 additions and 1 deletions

View File

@ -15,11 +15,20 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent)
m_tabs = new QStackedLayout(); 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); m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &VSettingsDialog::saveConfiguration); connect(m_btnBox, &QDialogButtonBox::accepted, this, &VSettingsDialog::saveConfiguration);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
m_btnBox->button(QDialogButtonBox::Ok)->setProperty("SpecialBtn", true); m_btnBox->button(QDialogButtonBox::Ok)->setProperty("SpecialBtn", true);
m_btnBox->addButton(m_resetVNoteBtn, QDialogButtonBox::ResetRole);
QHBoxLayout *tabLayout = new QHBoxLayout(); QHBoxLayout *tabLayout = new QHBoxLayout();
tabLayout->addWidget(m_tabList); tabLayout->addWidget(m_tabList);
tabLayout->addLayout(m_tabs); tabLayout->addLayout(m_tabs);
@ -57,6 +66,36 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent)
loadConfiguration(); 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) void VSettingsDialog::addTab(QWidget *p_widget, const QString &p_label)
{ {
int idx = m_tabs->addWidget(p_widget); int idx = m_tabs->addWidget(p_widget);

View File

@ -150,6 +150,8 @@ public:
private slots: private slots:
void saveConfiguration(); void saveConfiguration();
void resetVNote();
private: private:
void loadConfiguration(); void loadConfiguration();
@ -158,6 +160,9 @@ private:
QStackedLayout *m_tabs; QStackedLayout *m_tabs;
QListWidget *m_tabList; QListWidget *m_tabList;
QDialogButtonBox *m_btnBox; QDialogButtonBox *m_btnBox;
// Reset all the configuration of VNote.
QPushButton *m_resetVNoteBtn;
}; };
#endif // VSETTINGSDIALOG_H #endif // VSETTINGSDIALOG_H

View File

@ -51,7 +51,8 @@ VConfigManager::VConfigManager(QObject *p_parent)
: QObject(p_parent), : QObject(p_parent),
userSettings(NULL), userSettings(NULL),
defaultSettings(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) void VConfigManager::setConfigToSettings(const QString &section, const QString &key, const QVariant &value)
{ {
if (m_hasReset) {
return;
}
// Set the user-scoped config file // Set the user-scoped config file
setConfigToSettingsBySectionKey(userSettings, section, key, value); setConfigToSettingsBySectionKey(userSettings, section, key, value);
qDebug() << "set user config:" << (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 QString &p_key,
const QVariant &p_value) const QVariant &p_value)
{ {
if (m_hasReset) {
return;
}
setConfigToSettingsBySectionKey(m_sessionSettings, setConfigToSettingsBySectionKey(m_sessionSettings,
p_section, p_section,
p_key, p_key,
@ -1107,6 +1116,10 @@ QVector<VFileSessionInfo> VConfigManager::getLastOpenedFiles()
void VConfigManager::setLastOpenedFiles(const QVector<VFileSessionInfo> &p_files) void VConfigManager::setLastOpenedFiles(const QVector<VFileSessionInfo> &p_files)
{ {
if (m_hasReset) {
return;
}
const QString section("last_opened_files"); const QString section("last_opened_files");
// Clear it first // Clear it first
@ -1312,3 +1325,22 @@ void VConfigManager::initCodeBlockCssStyles()
m_codeBlockCssStyles.insert(fi.completeBaseName(), dir.filePath(item)); 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();
}

View File

@ -81,7 +81,9 @@ public:
static QJsonObject readDirectoryConfig(const QString &path); static QJsonObject readDirectoryConfig(const QString &path);
static bool writeDirectoryConfig(const QString &path, const QJsonObject &configJson); static bool writeDirectoryConfig(const QString &path, const QJsonObject &configJson);
static bool directoryConfigExist(const QString &path); static bool directoryConfigExist(const QString &path);
static bool deleteDirectoryConfig(const QString &path); static bool deleteDirectoryConfig(const QString &path);
// Get the path of the folder used to store default notebook. // Get the path of the folder used to store default notebook.
@ -98,6 +100,9 @@ public:
// CSS style for data in label. // CSS style for data in label.
static const QString c_dataTextStyle; static const QString c_dataTextStyle;
// Reset the configuratio files.
void resetConfigurations();
QFont getMdEditFont() const; QFont getMdEditFont() const;
QPalette getMdEditPalette() const; QPalette getMdEditPalette() const;
@ -442,6 +447,8 @@ private:
const QString &p_key, const QString &p_key,
const QVariant &p_value); const QVariant &p_value);
void clearGroupOfSettings(QSettings *p_settings, const QString &p_group);
// Init defaultSettings, userSettings, and m_sessionSettings. // Init defaultSettings, userSettings, and m_sessionSettings.
void initSettings(); void initSettings();
@ -806,6 +813,9 @@ private:
// Whether close note before open it via external editor. // Whether close note before open it via external editor.
bool m_closeBeforeExternalEditor; bool m_closeBeforeExternalEditor;
// Whether user has reset the configurations.
bool m_hasReset;
// The name of the config file in each directory, obsolete. // The name of the config file in each directory, obsolete.
// Use c_dirConfigFile instead. // Use c_dirConfigFile instead.
static const QString c_obsoleteDirConfigFile; static const QString c_obsoleteDirConfigFile;