support changing rendering background color

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-28 21:06:26 +08:00
parent 0c3fc3b006
commit 53bce98db6
10 changed files with 137 additions and 75 deletions

View File

@ -2,6 +2,9 @@
<html lang="en">
<meta charset="utf-8">
<head>
<style type="text/css">
<!-- BACKGROUND_PLACE_HOLDER -->
</style>
<link rel="stylesheet" type="text/css" href="CSS_PLACE_HOLDER">
<link rel="stylesheet" type="text/css" href="qrc:/utils/highlightjs/styles/default.css">
<script src="qrc:/utils/highlightjs/highlight.pack.js"></script>

View File

@ -2,6 +2,9 @@
<html lang="en">
<meta charset="utf-8">
<head>
<style type="text/css">
<!-- BACKGROUND_PLACE_HOLDER -->
</style>
<link rel="stylesheet" type="text/css" href="CSS_PLACE_HOLDER">
<link rel="stylesheet" type="text/css" href="qrc:/utils/highlightjs/styles/default.css">
<script src="qrc:/utils/marked/marked.min.js"></script>

View File

@ -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

View File

@ -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;

View File

@ -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) {

View File

@ -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<VColor> 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

View File

@ -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<VColor> &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<VColor> &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<VColor> &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();
}

View File

@ -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 &notebookName);
@ -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<QPixmap> predefinedColorPixmaps;
};

View File

@ -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<VColor> &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("<!-- BACKGROUND_PLACE_HOLDER -->");
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<VNotebook>& VNote::getNotebooks()

View File

@ -18,7 +18,7 @@ public:
const QVector<VNotebook>& 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<VNotebook> &notebooks);