| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #include "vconfigmanager.h"
- #include <QDir>
- #include <QFile>
- #include <QString>
- #include <QJsonArray>
- #include <QJsonObject>
- #include <QJsonDocument>
- #include <QtDebug>
- #include <QTextEdit>
- #include "utils/vutils.h"
- #include "vstyleparser.h"
- const QString VConfigManager::orgName = QString("tamlok");
- const QString VConfigManager::appName = QString("vnote");
- const QString VConfigManager::c_version = QString("1.1");
- const QString VConfigManager::dirConfigFileName = QString(".vnote.json");
- const QString VConfigManager::defaultConfigFilePath = QString(":/resources/vnote.ini");
- VConfigManager::VConfigManager()
- : userSettings(NULL), defaultSettings(NULL)
- {
- }
- VConfigManager::~VConfigManager()
- {
- if (userSettings) {
- delete userSettings;
- }
- if (defaultSettings) {
- delete defaultSettings;
- }
- }
- void VConfigManager::initialize()
- {
- userSettings = new QSettings(QSettings::IniFormat, QSettings::UserScope, orgName, appName);
- defaultSettings = new QSettings(defaultConfigFilePath, QSettings::IniFormat);
- m_editorFontSize = getConfigFromSettings("global", "editor_font_size").toInt();
- if (m_editorFontSize <= 0) {
- m_editorFontSize = 12;
- }
- baseEditFont.setPointSize(m_editorFontSize);
- baseEditPalette = QTextEdit().palette();
- welcomePagePath = getConfigFromSettings("global", "welcome_page_path").toString();
- templateCssUrl = getConfigFromSettings("global", "template_css_url").toString();
- curNotebookIndex = getConfigFromSettings("global", "current_notebook").toInt();
- markdownExtensions = hoedown_extensions(HOEDOWN_EXT_TABLES | HOEDOWN_EXT_FENCED_CODE |
- HOEDOWN_EXT_HIGHLIGHT | HOEDOWN_EXT_AUTOLINK |
- HOEDOWN_EXT_QUOTE | HOEDOWN_EXT_MATH | HOEDOWN_EXT_MATH_EXPLICIT);
- mdConverterType = (MarkdownConverterType)getConfigFromSettings("global", "markdown_converter").toInt();
- tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt();
- isExpandTab = getConfigFromSettings("global", "is_expand_tab").toBool();
- m_highlightCursorLine = getConfigFromSettings("global", "highlight_cursor_line").toBool();
- m_highlightSelectedWord = getConfigFromSettings("global", "highlight_selected_word").toBool();
- m_highlightSearchedWord = getConfigFromSettings("global", "highlight_searched_word").toBool();
- m_autoIndent = getConfigFromSettings("global", "auto_indent").toBool();
- m_autoList = getConfigFromSettings("global", "auto_list").toBool();
- readPredefinedColorsFromSettings();
- curBackgroundColor = getConfigFromSettings("global", "current_background_color").toString();
- updatePaletteColor();
- curRenderBackgroundColor = getConfigFromSettings("global",
- "current_render_background_color").toString();
- m_toolsDockChecked = getConfigFromSettings("session", "tools_dock_checked").toBool();
- m_mainWindowGeometry = getConfigFromSettings("session", "main_window_geometry").toByteArray();
- m_mainWindowState = getConfigFromSettings("session", "main_window_state").toByteArray();
- m_mainSplitterState = getConfigFromSettings("session", "main_splitter_state").toByteArray();
- updateMarkdownEditStyle();
- m_findCaseSensitive = getConfigFromSettings("global",
- "find_case_sensitive").toBool();
- m_findWholeWordOnly = getConfigFromSettings("global",
- "find_whole_word_only").toBool();
- m_findRegularExpression = getConfigFromSettings("global",
- "find_regular_expression").toBool();
- m_findIncrementalSearch = getConfigFromSettings("global",
- "find_incremental_search").toBool();
- m_language = getConfigFromSettings("global", "language").toString();
- m_enableMermaid = getConfigFromSettings("global", "enable_mermaid").toBool();
- m_enableMathjax = getConfigFromSettings("global", "enable_mathjax").toBool();
- m_webZoomFactor = getConfigFromSettings("global", "web_zoom_factor").toReal();
- if (!isCustomWebZoomFactor()) {
- // Calculate the zoom factor based on DPI.
- m_webZoomFactor = VUtils::calculateScaleFactor();
- qDebug() << "set WebZoomFactor to" << m_webZoomFactor;
- }
- }
- void VConfigManager::readPredefinedColorsFromSettings()
- {
- predefinedColors.clear();
- int size = defaultSettings->beginReadArray("predefined_colors");
- for (int i = 0; i < size; ++i) {
- defaultSettings->setArrayIndex(i);
- VColor color;
- color.name = defaultSettings->value("name").toString();
- color.rgb = defaultSettings->value("rgb").toString();
- predefinedColors.append(color);
- }
- defaultSettings->endArray();
- qDebug() << "read" << predefinedColors.size()
- << "pre-defined colors from [predefined_colors] section";
- }
- void VConfigManager::readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent)
- {
- Q_ASSERT(p_notebooks.isEmpty());
- int size = userSettings->beginReadArray("notebooks");
- for (int i = 0; i < size; ++i) {
- userSettings->setArrayIndex(i);
- QString name = userSettings->value("name").toString();
- QString path = userSettings->value("path").toString();
- VNotebook *notebook = new VNotebook(name, path, parent);
- p_notebooks.append(notebook);
- }
- userSettings->endArray();
- qDebug() << "read" << p_notebooks.size()
- << "notebook items from [notebooks] section";
- }
- void VConfigManager::writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks)
- {
- // Clear it first
- userSettings->beginGroup("notebooks");
- userSettings->remove("");
- userSettings->endGroup();
- userSettings->beginWriteArray("notebooks");
- for (int i = 0; i < p_notebooks.size(); ++i) {
- userSettings->setArrayIndex(i);
- const VNotebook ¬ebook = *p_notebooks[i];
- userSettings->setValue("name", notebook.getName());
- userSettings->setValue("path", notebook.getPath());
- }
- userSettings->endArray();
- qDebug() << "write" << p_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);
- qDebug() << "read config file:" << configFile;
- QFile config(configFile);
- if (!config.open(QIODevice::ReadOnly)) {
- qWarning() << "fail to read directory configuration file:"
- << configFile;
- return QJsonObject();
- }
- QByteArray configData = config.readAll();
- return QJsonDocument::fromJson(configData).object();
- }
- bool VConfigManager::directoryConfigExist(const QString &path)
- {
- QString configFile = QDir(path).filePath(dirConfigFileName);
- QFile config(configFile);
- return config.exists();
- }
- bool VConfigManager::writeDirectoryConfig(const QString &path, const QJsonObject &configJson)
- {
- QString configFile = QDir(path).filePath(dirConfigFileName);
- qDebug() << "write config file:" << configFile;
- QFile config(configFile);
- if (!config.open(QIODevice::WriteOnly)) {
- qWarning() << "fail to open directory configuration file for write:"
- << configFile;
- return false;
- }
- QJsonDocument configDoc(configJson);
- config.write(configDoc.toJson());
- return true;
- }
- bool VConfigManager::deleteDirectoryConfig(const QString &path)
- {
- QString configFile = QDir(path).filePath(dirConfigFileName);
- QFile config(configFile);
- if (!config.remove()) {
- qWarning() << "fail to delete directory configuration file:"
- << configFile;
- return false;
- }
- qDebug() << "delete config file:" << configFile;
- return true;
- }
- void VConfigManager::updateMarkdownEditStyle()
- {
- static const QString defaultCurrentLineBackground = "#C5CAE9";
- static const QString defaultCurrentLineVimBackground = "#A5D6A7";
- // Read style file .mdhl
- QString file(":/resources/styles/default.mdhl");
- QString styleStr = VUtils::readFileFromDisk(file);
- if (styleStr.isEmpty()) {
- return;
- }
- VStyleParser parser;
- parser.parseMarkdownStyle(styleStr);
- mdHighlightingStyles = parser.fetchMarkdownStyles(baseEditFont);
- mdEditPalette = baseEditPalette;
- mdEditFont = baseEditFont;
- QMap<QString, QMap<QString, QString>> styles;
- parser.fetchMarkdownEditorStyles(mdEditPalette, mdEditFont, styles);
- m_editorCurrentLineBackground = defaultCurrentLineBackground;
- m_editorCurrentLineVimBackground = defaultCurrentLineVimBackground;
- auto editorCurrentLineIt = styles.find("editor-current-line");
- if (editorCurrentLineIt != styles.end()) {
- auto backgroundIt = editorCurrentLineIt->find("background");
- if (backgroundIt != editorCurrentLineIt->end()) {
- m_editorCurrentLineBackground = *backgroundIt;
- }
- auto vimBackgroundIt = editorCurrentLineIt->find("vim-background");
- if (vimBackgroundIt != editorCurrentLineIt->end()) {
- m_editorCurrentLineVimBackground = "#" + *vimBackgroundIt;
- }
- }
- qDebug() << "editor-current-line:" << m_editorCurrentLineBackground << m_editorCurrentLineVimBackground;
- }
- void VConfigManager::updatePaletteColor()
- {
- static const QColor defaultColor = baseEditPalette.color(QPalette::Base);
- QColor newColor = defaultColor;
- if (curBackgroundColor != "System") {
- for (int i = 0; i < predefinedColors.size(); ++i) {
- if (predefinedColors[i].name == curBackgroundColor) {
- QString rgb = predefinedColors[i].rgb;
- if (!rgb.isEmpty()) {
- newColor = QColor(VUtils::QRgbFromString(rgb));
- }
- break;
- }
- }
- }
- baseEditPalette.setColor(QPalette::Base, newColor);
- // Update markdown editor palette
- updateMarkdownEditStyle();
- }
- void VConfigManager::setWebZoomFactor(qreal p_factor)
- {
- if (isCustomWebZoomFactor()) {
- if (VUtils::realEqual(m_webZoomFactor, p_factor)) {
- return;
- } else if (VUtils::realEqual(p_factor, -1)) {
- m_webZoomFactor = VUtils::calculateScaleFactor();
- setConfigToSettings("global", "web_zoom_factor", -1);
- return;
- }
- } else {
- if (VUtils::realEqual(p_factor, -1)) {
- return;
- }
- }
- m_webZoomFactor = p_factor;
- setConfigToSettings("global", "web_zoom_factor", m_webZoomFactor);
- }
|