diff --git a/resources/vnote.ini b/resources/vnote.ini new file mode 100644 index 00000000..3d8fcab4 --- /dev/null +++ b/resources/vnote.ini @@ -0,0 +1,7 @@ +[global] +welcome_page_path=:/resources/welcome.html +template_path=:/resources/template.html +pre_template_path=:/resources/pre_template.html +post_template_path=:/resources/post_template.html +template_css_url=qrc:/resources/markdown.css +current_notebook=0 diff --git a/vconfigmanager.cpp b/vconfigmanager.cpp index 847ce4d2..fc6dcb2b 100644 --- a/vconfigmanager.cpp +++ b/vconfigmanager.cpp @@ -10,31 +10,102 @@ #include "utils/vutils.h" #include "vstyleparser.h" +const QString VConfigManager::orgName = QString("tamlok"); +const QString VConfigManager::appName = QString("vnote"); const QString VConfigManager::dirConfigFileName = QString(".vnote.json"); -VConfigManager* VConfigManager::instance = NULL; +const QString VConfigManager::defaultConfigFilePath = QString(":/resources/vnote.ini"); VConfigManager::VConfigManager() - : baseEditFont(QFont()) + : userSettings(NULL), defaultSettings(NULL) { } -VConfigManager* VConfigManager::getInst() +VConfigManager::~VConfigManager() { - if (!instance) { - instance = new VConfigManager(); - instance->initialize(); + if (userSettings) { + delete userSettings; + } + if (defaultSettings) { + delete defaultSettings; } - return instance; } void VConfigManager::initialize() { + userSettings = new QSettings(QSettings::IniFormat, QSettings::UserScope, orgName, appName); + defaultSettings = new QSettings(defaultConfigFilePath, QSettings::IniFormat); + baseEditFont.setPointSize(11); baseEditPalette = QTextEdit().palette(); + welcomePagePath = getConfigFromSettings("global", "welcome_page_path").toString(); + templatePath = getConfigFromSettings("global", "template_path").toString(); + preTemplatePath = getConfigFromSettings("global", "pre_template_path").toString(); + postTemplatePath = getConfigFromSettings("global", "post_template_path").toString(); + templateCssUrl = getConfigFromSettings("global", "template_css_url").toString(); + curNotebookIndex = getConfigFromSettings("global", "current_notebook").toInt(); + + // Update notebooks + readNotebookFromSettings(); + updateMarkdownEditStyle(); } +void VConfigManager::readNotebookFromSettings() +{ + notebooks.clear(); + int size = userSettings->beginReadArray("notebooks"); + for (int i = 0; i < size; ++i) { + userSettings->setArrayIndex(i); + VNotebook notebook; + QString name = userSettings->value("name").toString(); + QString path = userSettings->value("path").toString(); + notebook.setName(name); + notebook.setPath(path); + notebooks.append(notebook); + } + userSettings->endArray(); + qDebug() << "read" << notebooks.size() + << "notebook items from [notebooks] section"; +} + +void VConfigManager::writeNotebookToSettings() +{ + userSettings->beginWriteArray("notebooks"); + for (int i = 0; i < notebooks.size(); ++i) { + userSettings->setArrayIndex(i); + userSettings->setValue("name", notebooks[i].getName()); + userSettings->setValue("path", notebooks[i].getPath()); + } + userSettings->endArray(); + qDebug() << "write" << notebooks.size() + << "notebook items in [notebooks] section"; +} + +QVariant VConfigManager::getConfigFromSettings(const QString §ion, const QString &key) +{ + QString fullKey = section + "/" + key; + // First, look up the user-scoped config file + QVariant value = userSettings->value(fullKey); + if (!value.isNull()) { + qDebug() << "user config:" << fullKey << value.toString(); + return value; + } + + // Second, look up the default config file + value = defaultSettings->value(fullKey); + qDebug() << "default config:" << fullKey << value.toString(); + return value; +} + +void VConfigManager::setConfigToSettings(const QString §ion, const QString &key, const QVariant &value) +{ + // Set the user-scoped config file + QString fullKey = section + "/" + key; + userSettings->setValue(fullKey, value); + qDebug() << "set user config:" << fullKey << value.toString(); +} + QJsonObject VConfigManager::readDirectoryConfig(const QString &path) { QString configFile = QDir(path).filePath(dirConfigFileName); diff --git a/vconfigmanager.h b/vconfigmanager.h index c1ff160a..1a0b4adb 100644 --- a/vconfigmanager.h +++ b/vconfigmanager.h @@ -4,37 +4,140 @@ #include #include #include +#include +#include "vnotebook.h" #include "hgmarkdownhighlighter.h" class QJsonObject; class QString; -#define VConfigInst VConfigManager::getInst() - class VConfigManager { public: - static VConfigManager *getInst(); + VConfigManager(); + ~VConfigManager(); + void initialize(); + + // Static helper functions // Read config from the directory config json file into a QJsonObject static QJsonObject readDirectoryConfig(const QString &path); static bool writeDirectoryConfig(const QString &path, const QJsonObject &configJson); static bool deleteDirectoryConfig(const QString &path); + // Constants + static const QString orgName; + static const QString appName; + + inline QFont getMdEditFont() const; + + inline QPalette getMdEditPalette() const; + + inline QVector getMdHighlightingStyles() const; + + inline QString getWelcomePagePath() const; + + inline QString getTemplatePath() const; + + inline QString getTemplateCssUrl() const; + + inline QFont getBaseEditFont() const; + + inline int getCurNotebookIndex() const; + inline void setCurNotebookIndex(int index); + + inline const QVector& getNotebooks() const; + inline void setNotebooks(const QVector ¬ebooks); + +private: void updateMarkdownEditStyle(); + QVariant getConfigFromSettings(const QString §ion, const QString &key); + void setConfigToSettings(const QString §ion, const QString &key, const QVariant &value); + void readNotebookFromSettings(); + void writeNotebookToSettings(); QFont baseEditFont; QPalette baseEditPalette; QFont mdEditFont; QPalette mdEditPalette; QVector mdHighlightingStyles; + QString welcomePagePath; + QString templatePath; + QString preTemplatePath; + QString postTemplatePath; + QString templateCssUrl; + int curNotebookIndex; + QVector notebooks; -private: - VConfigManager(); - void initialize(); // The name of the config file in each directory static const QString dirConfigFileName; - static VConfigManager *instance; + // The name of the default configuration file + static const QString defaultConfigFilePath; + // QSettings for the user configuration + QSettings *userSettings; + // Qsettings for @defaultConfigFileName + QSettings *defaultSettings; }; + +inline QFont VConfigManager::getMdEditFont() const +{ + return mdEditFont; +} + +inline QPalette VConfigManager::getMdEditPalette() const +{ + return mdEditPalette; +} + +inline QVector VConfigManager::getMdHighlightingStyles() const +{ + return mdHighlightingStyles; +} + +inline QString VConfigManager::getWelcomePagePath() const +{ + return welcomePagePath; +} + +inline QString VConfigManager::getTemplatePath() const +{ + return templatePath; +} + +inline QString VConfigManager::getTemplateCssUrl() const +{ + return templateCssUrl; +} + +inline QFont VConfigManager::getBaseEditFont() const +{ + return baseEditFont; +} + +inline int VConfigManager::getCurNotebookIndex() const +{ + return curNotebookIndex; +} + +inline void VConfigManager::setCurNotebookIndex(int index) +{ + if (index == curNotebookIndex) { + return; + } + curNotebookIndex = index; + setConfigToSettings("global", "current_notebook", index); +} + +inline const QVector& VConfigManager::getNotebooks() const +{ + return notebooks; +} + +inline void VConfigManager::setNotebooks(const QVector ¬ebooks) +{ + this->notebooks = notebooks; + writeNotebookToSettings(); +} + #endif // VCONFIGMANAGER_H diff --git a/vdirectorytree.cpp b/vdirectorytree.cpp index a362693c..d87d46e6 100644 --- a/vdirectorytree.cpp +++ b/vdirectorytree.cpp @@ -48,6 +48,10 @@ void VDirectoryTree::setTreePath(const QString& path) return; } + if (path.isEmpty()) { + clear(); + return; + } treePath = path; qDebug() << "set directory tree path:" << path; diff --git a/vedit.cpp b/vedit.cpp index 84bfd5da..1528e33e 100644 --- a/vedit.cpp +++ b/vedit.cpp @@ -3,14 +3,16 @@ #include "vnote.h" #include "vconfigmanager.h" +extern VConfigManager vconfig; + VEdit::VEdit(VNoteFile *noteFile, QWidget *parent) : QTextEdit(parent), noteFile(noteFile) { if (noteFile->docType == DocType::Markdown) { - setPalette(VConfigInst->mdEditPalette); - setFont(VConfigInst->mdEditFont); + setPalette(vconfig.getMdEditPalette()); + setFont(vconfig.getMdEditFont()); } else { - setFont(VConfigInst->baseEditFont); + setFont(vconfig.getBaseEditFont()); } } diff --git a/veditor.cpp b/veditor.cpp index bc90fb3a..3265991b 100644 --- a/veditor.cpp +++ b/veditor.cpp @@ -11,6 +11,8 @@ #include "hgmarkdownhighlighter.h" #include "vconfigmanager.h" +extern VConfigManager vconfig; + VEditor::VEditor(const QString &path, const QString &name, bool modifiable, QWidget *parent) : QStackedWidget(parent) @@ -44,14 +46,14 @@ void VEditor::setupUI() setupMarkdownPreview(); textBrowser = NULL; - mdHighlighter = new HGMarkdownHighlighter(VConfigInst->mdHighlightingStyles, + mdHighlighter = new HGMarkdownHighlighter(vconfig.getMdHighlightingStyles(), textEditor->document(), 500); break; case DocType::Html: textBrowser = new QTextBrowser(); addWidget(textBrowser); - textBrowser->setFont(VConfigInst->baseEditFont); + textBrowser->setFont(vconfig.getBaseEditFont()); webPreviewer = NULL; break; default: diff --git a/vmainwindow.cpp b/vmainwindow.cpp index 3580ec0b..d2d650d5 100644 --- a/vmainwindow.cpp +++ b/vmainwindow.cpp @@ -6,15 +6,15 @@ #include "vtabwidget.h" #include "vconfigmanager.h" +extern VConfigManager vconfig; + VMainWindow::VMainWindow(QWidget *parent) : QMainWindow(parent) { - VConfigInst; + vnote = new VNote(); setupUI(); initActions(); initToolBar(); - vnote = new VNote(); - vnote->readGlobalConfig(); updateNotebookComboBox(); } @@ -106,22 +106,28 @@ void VMainWindow::updateNotebookComboBox() const QVector ¬ebooks = vnote->getNotebooks(); notebookComboBox->clear(); + if (notebooks.isEmpty()) { + return; + } for (int i = 0; i addItem(notebooks[i].getName()); } qDebug() << "update notebook combobox with" << notebookComboBox->count() << "items"; - notebookComboBox->setCurrentIndex(vnote->getCurNotebookIndex()); + notebookComboBox->setCurrentIndex(vconfig.getCurNotebookIndex()); } void VMainWindow::setCurNotebookIndex(int index) { Q_ASSERT(index < vnote->getNotebooks().size()); qDebug() << "set current notebook index:" << index; - vnote->setCurNotebookIndex(index); - notebookComboBox->setCurrentIndex(index); + vconfig.setCurNotebookIndex(index); // Update directoryTree - emit curNotebookIndexChanged(vnote->getNotebooks()[index].getPath()); + QString treePath; + if (index > -1) { + treePath = vnote->getNotebooks()[index].getPath(); + } + emit curNotebookIndexChanged(treePath); } diff --git a/vnote.cpp b/vnote.cpp index 1254dde3..96b533fb 100644 --- a/vnote.cpp +++ b/vnote.cpp @@ -2,106 +2,26 @@ #include #include "vnote.h" #include "utils/vutils.h" +#include "vconfigmanager.h" -const QString VNote::orgName = QString("tamlok"); -const QString VNote::appName = QString("VNote"); -const QString VNote::welcomePagePath = QString(":/resources/welcome.html"); -const QString VNote::preTemplatePath = QString(":/resources/pre_template.html"); -const QString VNote::postTemplatePath = QString(":/resources/post_template.html"); -const QString VNote::templatePath = QString(":/resources/template.html"); -const QString VNote::defaultCssUrl = QString("qrc:/resources/markdown.css"); +VConfigManager vconfig; QString VNote::templateHtml; -QString VNote::cssUrl = VNote::defaultCssUrl; VNote::VNote() - : curNotebookIndex(0) { + vconfig.initialize(); decorateTemplate(); + notebooks = vconfig.getNotebooks(); } void VNote::decorateTemplate() { - templateHtml = VUtils::readFileFromDisk(templatePath); - templateHtml.replace("CSS_PLACE_HOLDER", cssUrl); -} - -void VNote::readGlobalConfig() -{ - QSettings settings(QSettings::IniFormat, QSettings::UserScope, - orgName, appName); - - // [global] section - settings.beginGroup("global"); - curNotebookIndex = settings.value("current_notebook", 0).toInt(); - qDebug() << "read current_notebook=" << curNotebookIndex; - settings.endGroup(); - - readGlobalConfigNotebooks(settings); -} - -void VNote::writeGlobalConfig() -{ - QSettings settings(QSettings::IniFormat, QSettings::UserScope, - orgName, appName); - - // [global] section - settings.beginGroup("global"); - settings.setValue("current_notebook", curNotebookIndex); - qDebug() << "write current_notebook=" << curNotebookIndex; - settings.endGroup(); - - writeGlobalConfigNotebooks(settings); -} - -void VNote::readGlobalConfigNotebooks(QSettings &settings) -{ - notebooks.clear(); - int size = settings.beginReadArray("notebooks"); - for (int i = 0; i < size; ++i) { - settings.setArrayIndex(i); - VNotebook notebook; - QString name = settings.value("name").toString(); - QString path = settings.value("path").toString(); - notebook.setName(name); - notebook.setPath(path); - notebooks.append(notebook); - } - settings.endArray(); - qDebug() << "read" << notebooks.size() - << "notebook items from [notebooks] section"; -} - -void VNote::writeGlobalConfigNotebooks(QSettings &settings) -{ - settings.beginWriteArray("notebooks"); - for (int i = 0; i < notebooks.size(); ++i) { - settings.setArrayIndex(i); - settings.setValue("name", notebooks[i].getName()); - settings.setValue("path", notebooks[i].getPath()); - } - settings.endArray(); - qDebug() << "write" << notebooks.size() - << "notebook items in [notebooks] section"; + templateHtml = VUtils::readFileFromDisk(vconfig.getTemplatePath()); + templateHtml.replace("CSS_PLACE_HOLDER", vconfig.getTemplateCssUrl()); } const QVector& VNote::getNotebooks() { return notebooks; } - -int VNote::getCurNotebookIndex() const -{ - return curNotebookIndex; -} - -void VNote::setCurNotebookIndex(int index) -{ - curNotebookIndex = index; - - // Update settings - QSettings settings(QSettings::IniFormat, QSettings::UserScope, - orgName, appName); - settings.setValue("global/current_notebook", curNotebookIndex); - qDebug() << "write current_notebook=" << curNotebookIndex; -} diff --git a/vnote.h b/vnote.h index 42dfb97f..ebb371b5 100644 --- a/vnote.h +++ b/vnote.h @@ -11,36 +11,15 @@ class VNote { public: VNote(); - void readGlobalConfig(); - void writeGlobalConfig(); const QVector& getNotebooks(); - int getCurNotebookIndex() const; - void setCurNotebookIndex(int index); static void decorateTemplate(); - static const QString orgName; - static const QString appName; - static const QString welcomePagePath; - static const QString templatePath; - static QString templateHtml; private: - // Write notebooks section of global config - void writeGlobalConfigNotebooks(QSettings &settings); - // Read notebooks section of global config - void readGlobalConfigNotebooks(QSettings &settings); - QVector notebooks; - int curNotebookIndex; - static const QString preTemplatePath; - static const QString postTemplatePath; - static const QString defaultCssUrl; - - // For self CSS definition - static QString cssUrl; }; #endif // VNOTE_H diff --git a/vnote.qrc b/vnote.qrc index 3c609274..5148a7df 100644 --- a/vnote.qrc +++ b/vnote.qrc @@ -35,5 +35,6 @@ resources/styles/default.mdhl resources/styles/solarized-light.mdhl resources/styles/solarized-dark.mdhl + resources/vnote.ini diff --git a/vtabwidget.cpp b/vtabwidget.cpp index 5180ecb3..21b043c8 100644 --- a/vtabwidget.cpp +++ b/vtabwidget.cpp @@ -3,6 +3,9 @@ #include "vtabwidget.h" #include "veditor.h" #include "vnote.h" +#include "vconfigmanager.h" + +extern VConfigManager vconfig; VTabWidget::VTabWidget(QWidget *parent) : QTabWidget(parent) @@ -16,7 +19,7 @@ VTabWidget::VTabWidget(QWidget *parent) void VTabWidget::openWelcomePage() { - int idx = openFileInTab(VNote::welcomePagePath, "", false); + int idx = openFileInTab(vconfig.getWelcomePagePath(), "", false); setTabText(idx, "Welcome to VNote"); setTabToolTip(idx, "VNote"); }