mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
use VConfigManager to hanlde configurations
Move config related stuff to VConfigManager. For a config value, VConfigManager will first try to look it up in the user-scoped vnote.ini config file, if it is empty, then try to look it up in the default vnote.ini. Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
c8d9745253
commit
8d9278f491
7
resources/vnote.ini
Normal file
7
resources/vnote.ini
Normal file
@ -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
|
@ -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);
|
||||
|
117
vconfigmanager.h
117
vconfigmanager.h
@ -4,37 +4,140 @@
|
||||
#include <QFont>
|
||||
#include <QPalette>
|
||||
#include <QVector>
|
||||
#include <QSettings>
|
||||
#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<HighlightingStyle> 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<VNotebook>& getNotebooks() const;
|
||||
inline void setNotebooks(const QVector<VNotebook> ¬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<HighlightingStyle> mdHighlightingStyles;
|
||||
QString welcomePagePath;
|
||||
QString templatePath;
|
||||
QString preTemplatePath;
|
||||
QString postTemplatePath;
|
||||
QString templateCssUrl;
|
||||
int curNotebookIndex;
|
||||
QVector<VNotebook> 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<HighlightingStyle> 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<VNotebook>& VConfigManager::getNotebooks() const
|
||||
{
|
||||
return notebooks;
|
||||
}
|
||||
|
||||
inline void VConfigManager::setNotebooks(const QVector<VNotebook> ¬ebooks)
|
||||
{
|
||||
this->notebooks = notebooks;
|
||||
writeNotebookToSettings();
|
||||
}
|
||||
|
||||
#endif // VCONFIGMANAGER_H
|
||||
|
@ -48,6 +48,10 @@ void VDirectoryTree::setTreePath(const QString& path)
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.isEmpty()) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
treePath = path;
|
||||
qDebug() << "set directory tree path:" << path;
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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<VNotebook> ¬ebooks = vnote->getNotebooks();
|
||||
|
||||
notebookComboBox->clear();
|
||||
if (notebooks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i <notebooks.size(); ++i) {
|
||||
notebookComboBox->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);
|
||||
}
|
||||
|
92
vnote.cpp
92
vnote.cpp
@ -2,106 +2,26 @@
|
||||
#include <QDebug>
|
||||
#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<VNotebook>& 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;
|
||||
}
|
||||
|
21
vnote.h
21
vnote.h
@ -11,36 +11,15 @@ class VNote
|
||||
{
|
||||
public:
|
||||
VNote();
|
||||
void readGlobalConfig();
|
||||
void writeGlobalConfig();
|
||||
|
||||
const QVector<VNotebook>& 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<VNotebook> 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
|
||||
|
@ -35,5 +35,6 @@
|
||||
<file>resources/styles/default.mdhl</file>
|
||||
<file>resources/styles/solarized-light.mdhl</file>
|
||||
<file>resources/styles/solarized-dark.mdhl</file>
|
||||
<file>resources/vnote.ini</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user