diff --git a/src/resources/pre_template.html b/src/resources/pre_template.html index cfce4cd2..989446e1 100644 --- a/src/resources/pre_template.html +++ b/src/resources/pre_template.html @@ -2,6 +2,9 @@ + diff --git a/src/resources/template.html b/src/resources/template.html index 6b597de3..195f0a08 100644 --- a/src/resources/template.html +++ b/src/resources/template.html @@ -2,6 +2,9 @@ + diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 49dda2d3..e32656e6 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -8,6 +8,7 @@ current_notebook=0 tab_stop_width=4 is_expand_tab=1 current_background_color=System +current_render_background_color=System [predefined_colors] 1\name=White diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index a1f2a8a7..64f85678 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -40,8 +40,6 @@ QRgb VUtils::QRgbFromString(const QString &str) QString gStr = str.mid(2, 2); QString bStr = str.right(2); - qDebug() << rStr << gStr << bStr; - bool ok, ret = true; int red = rStr.toInt(&ok, 16); ret = ret && ok; diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 65c88830..b91b6325 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -55,10 +55,12 @@ void VConfigManager::initialize() readPredefinedColorsFromSettings(); curBackgroundColor = getConfigFromSettings("global", "current_background_color").toString(); - customBackgroundColor = getConfigFromSettings("global", "custom_background_color").toString(); updatePaletteColor(); + curRenderBackgroundColor = getConfigFromSettings("global", + "current_render_background_color").toString(); + // Update notebooks readNotebookFromSettings(); @@ -209,9 +211,7 @@ void VConfigManager::updateMarkdownEditStyle() void VConfigManager::updatePaletteColor() { QString rgb; - if (curBackgroundColor == "Custom") { - rgb = customBackgroundColor; - } else if (curBackgroundColor == "System") { + if (curBackgroundColor == "System") { return; } else { for (int i = 0; i < predefinedColors.size(); ++i) { diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index e32f46b6..485be6f1 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -80,8 +80,8 @@ public: inline const QString &getCurBackgroundColor() const; inline void setCurBackgroundColor(const QString &colorName); - inline const QString &getCustomBackgroundColor() const; - inline void setCustomBackgroundColor(const QString &colorRgb); + inline const QString &getCurRenderBackgroundColor() const; + inline void setCurRenderBackgroundColor(const QString &colorName); private: void updateMarkdownEditStyle(); @@ -118,7 +118,7 @@ private: // App defined color QVector predefinedColors; QString curBackgroundColor; - QString customBackgroundColor; + QString curRenderBackgroundColor; // The name of the config file in each directory static const QString dirConfigFileName; @@ -274,20 +274,19 @@ inline void VConfigManager::setCurBackgroundColor(const QString &colorName) updatePaletteColor(); } -inline const QString& VConfigManager::getCustomBackgroundColor() const +inline const QString& VConfigManager::getCurRenderBackgroundColor() const { - return customBackgroundColor; + return curRenderBackgroundColor; } -inline void VConfigManager::setCustomBackgroundColor(const QString &colorRgb) +inline void VConfigManager::setCurRenderBackgroundColor(const QString &colorName) { - if (customBackgroundColor == colorRgb) { + if (curRenderBackgroundColor == colorName) { return; } - customBackgroundColor = colorRgb; - setConfigToSettings("global", "custom_background_color", - customBackgroundColor); - updatePaletteColor(); + curRenderBackgroundColor = colorName; + setConfigToSettings("global", "current_render_background_color", + curRenderBackgroundColor); } #endif // VCONFIGMANAGER_H diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 72251df7..3e6d58c7 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -211,6 +211,10 @@ void VMainWindow::initActions() backgroundColorAct = new QActionGroup(this); connect(backgroundColorAct, &QActionGroup::triggered, this, &VMainWindow::setEditorBackgroundColor); + + renderBackgroundAct = new QActionGroup(this); + connect(renderBackgroundAct, &QActionGroup::triggered, + this, &VMainWindow::setRenderBackgroundColor); } void VMainWindow::initToolBar() @@ -258,43 +262,7 @@ void VMainWindow::initMenuBar() default: qWarning() << "error: unsupported tab stop width" << tabStopWidth << "in config"; } - - QMenu *backgroundColorMenu = editMenu->addMenu(tr("&Background Color")); - // System background color - const QString &curBgColor = vconfig.getCurBackgroundColor(); - QAction *tmpAct = new QAction(tr("System"), backgroundColorAct); - tmpAct->setStatusTip(tr("Use system's background color configuration for editor")); - tmpAct->setCheckable(true); - tmpAct->setData("System"); - if (curBgColor == "System") { - tmpAct->setChecked(true); - } - backgroundColorMenu->addAction(tmpAct); - const QVector &bgColors = vconfig.getPredefinedColors(); - for (int i = 0; i < bgColors.size(); ++i) { - tmpAct = new QAction(bgColors[i].name, backgroundColorAct); - tmpAct->setStatusTip(tr("Set background color for editor")); - tmpAct->setCheckable(true); - tmpAct->setData(bgColors[i].name); - tmpAct->setIcon(QIcon(predefinedColorPixmaps[i])); - if (curBgColor == bgColors[i].name) { - tmpAct->setChecked(true); - } - - backgroundColorMenu->addAction(tmpAct); - } - // Custom background color - tmpAct = new QAction(tr("Custom"), backgroundColorAct); - tmpAct->setStatusTip(tr("Set customed background color for editor")); - tmpAct->setCheckable(true); - tmpAct->setData("Custom"); - if (!vconfig.getCustomBackgroundColor().isEmpty()) { - tmpAct->setIcon(QIcon(predefinedColorPixmaps.back())); - } - if (curBgColor == "Custom") { - tmpAct->setChecked(true); - } - backgroundColorMenu->addAction(tmpAct); + initEditorBackgroundMenu(editMenu); // Markdown Menu QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter")); @@ -307,6 +275,8 @@ void VMainWindow::initMenuBar() hoedownAct->setChecked(true); } + initRenderBackgroundMenu(markdownMenu); + // Help menu helpMenu->addAction(aboutQtAct); helpMenu->addAction(aboutAct); @@ -534,10 +504,6 @@ void VMainWindow::setEditorBackgroundColor(QAction *action) if (!action) { return; } - // TODO: implement custom color logics - if (action->data().toString() == "Custom") { - return; - } vconfig.setCurBackgroundColor(action->data().toString()); } @@ -554,12 +520,69 @@ void VMainWindow::initPredefinedColorPixmaps() pixmap.fill(color); predefinedColorPixmaps.append(pixmap); } +} - const QString &customBgColor = vconfig.getCustomBackgroundColor(); - if (!customBgColor.isEmpty()) { - QColor color(VUtils::QRgbFromString(customBgColor)); - QPixmap pixmap(size, size); - pixmap.fill(color); - predefinedColorPixmaps.append(pixmap); +void VMainWindow::initRenderBackgroundMenu(QMenu *menu) +{ + QMenu *renderBgMenu = menu->addMenu(tr("&Rendering background")); + const QString &curBgColor = vconfig.getCurRenderBackgroundColor(); + QAction *tmpAct = new QAction(tr("System"), renderBackgroundAct); + tmpAct->setStatusTip(tr("Use system's background color configuration for rendering")); + tmpAct->setCheckable(true); + tmpAct->setData("System"); + if (curBgColor == "System") { + tmpAct->setChecked(true); + } + renderBgMenu->addAction(tmpAct); + + const QVector &bgColors = vconfig.getPredefinedColors(); + for (int i = 0; i < bgColors.size(); ++i) { + tmpAct = new QAction(bgColors[i].name, renderBackgroundAct); + tmpAct->setStatusTip(tr("Set background color for rendering")); + tmpAct->setCheckable(true); + tmpAct->setData(bgColors[i].name); + tmpAct->setIcon(QIcon(predefinedColorPixmaps[i])); + if (curBgColor == bgColors[i].name) { + tmpAct->setChecked(true); + } + + renderBgMenu->addAction(tmpAct); } } + +void VMainWindow::initEditorBackgroundMenu(QMenu *menu) +{ + QMenu *backgroundColorMenu = menu->addMenu(tr("&Background Color")); + // System background color + const QString &curBgColor = vconfig.getCurBackgroundColor(); + QAction *tmpAct = new QAction(tr("System"), backgroundColorAct); + tmpAct->setStatusTip(tr("Use system's background color configuration for editor")); + tmpAct->setCheckable(true); + tmpAct->setData("System"); + if (curBgColor == "System") { + tmpAct->setChecked(true); + } + backgroundColorMenu->addAction(tmpAct); + const QVector &bgColors = vconfig.getPredefinedColors(); + for (int i = 0; i < bgColors.size(); ++i) { + tmpAct = new QAction(bgColors[i].name, backgroundColorAct); + tmpAct->setStatusTip(tr("Set background color for editor")); + tmpAct->setCheckable(true); + tmpAct->setData(bgColors[i].name); + tmpAct->setIcon(QIcon(predefinedColorPixmaps[i])); + if (curBgColor == bgColors[i].name) { + tmpAct->setChecked(true); + } + + backgroundColorMenu->addAction(tmpAct); + } +} + +void VMainWindow::setRenderBackgroundColor(QAction *action) +{ + if (!action) { + return; + } + vconfig.setCurRenderBackgroundColor(action->data().toString()); + vnote->updateTemplate(); +} diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 0d0b2852..2e518fec 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -43,6 +43,7 @@ private slots: void changeExpandTab(bool checked); void setTabStopWidth(QAction *action); void setEditorBackgroundColor(QAction *action); + void setRenderBackgroundColor(QAction *action); signals: void curNotebookChanged(const QString ¬ebookName); @@ -54,6 +55,8 @@ private: void initMenuBar(); bool isConflictWithExistingNotebooks(const QString &name); void initPredefinedColorPixmaps(); + void initRenderBackgroundMenu(QMenu *menu); + void initEditorBackgroundMenu(QMenu *menu); // If true, comboBox changes will not trigger any signal out bool notebookComboMuted; @@ -89,6 +92,7 @@ private: QAction *fourSpaceTabAct; QAction *eightSpaceTabAct; QActionGroup *backgroundColorAct; + QActionGroup *renderBackgroundAct; QVector predefinedColorPixmaps; }; diff --git a/src/vnote.cpp b/src/vnote.cpp index 484f275b..c748dd6e 100644 --- a/src/vnote.cpp +++ b/src/vnote.cpp @@ -16,24 +16,52 @@ QString VNote::postTemplateHtml; VNote::VNote() : QObject() { vconfig.initialize(); - decorateTemplate(); + initTemplate(); notebooks = vconfig.getNotebooks(); emit notebooksChanged(notebooks); } -void VNote::decorateTemplate() +void VNote::initTemplate() { - if (templateHtml.isEmpty()) { - templateHtml = VUtils::readFileFromDisk(vconfig.getTemplatePath()); - templateHtml.replace("CSS_PLACE_HOLDER", vconfig.getTemplateCssUrl()); + if (templateHtml.isEmpty() || preTemplateHtml.isEmpty() + || postTemplateHtml.isEmpty()) { + updateTemplate(); } - if (preTemplateHtml.isEmpty()) { - preTemplateHtml = VUtils::readFileFromDisk(vconfig.getPreTemplatePath()); - preTemplateHtml.replace("CSS_PLACE_HOLDER", vconfig.getTemplateCssUrl()); +} + +void VNote::updateTemplate() +{ + // Get background color + QString rgb; + const QString &curRenderBg = vconfig.getCurRenderBackgroundColor(); + const QVector &predefinedColors = vconfig.getPredefinedColors(); + if (curRenderBg != "System") { + for (int i = 0; i < predefinedColors.size(); ++i) { + if (predefinedColors[i].name == curRenderBg) { + rgb = predefinedColors[i].rgb; + break; + } + } } - if (postTemplateHtml.isEmpty()) { - postTemplateHtml = VUtils::readFileFromDisk(vconfig.getPostTemplatePath()); + QString cssStyle; + if (!rgb.isEmpty()) { + cssStyle = "body { background-color: #" + rgb + "; }"; } + QString styleHolder(""); + QString cssHolder("CSS_PLACE_HOLDER"); + templateHtml = VUtils::readFileFromDisk(vconfig.getTemplatePath()); + templateHtml.replace(cssHolder, vconfig.getTemplateCssUrl()); + if (!cssStyle.isEmpty()) { + templateHtml.replace(styleHolder, cssStyle); + } + + preTemplateHtml = VUtils::readFileFromDisk(vconfig.getPreTemplatePath()); + preTemplateHtml.replace(cssHolder, vconfig.getTemplateCssUrl()); + if (!cssStyle.isEmpty()) { + preTemplateHtml.replace(styleHolder, cssStyle); + } + + postTemplateHtml = VUtils::readFileFromDisk(vconfig.getPostTemplatePath()); } const QVector& VNote::getNotebooks() diff --git a/src/vnote.h b/src/vnote.h index 319c7401..63ee517e 100644 --- a/src/vnote.h +++ b/src/vnote.h @@ -18,7 +18,7 @@ public: const QVector& getNotebooks(); - static void decorateTemplate(); + void initTemplate(); // Used by Marked static QString templateHtml; @@ -31,6 +31,9 @@ public: void removeNotebook(const QString &name); void renameNotebook(const QString &name, const QString &newName); +public slots: + void updateTemplate(); + signals: // Force to do a fully update void notebooksChanged(const QVector ¬ebooks);