support changing background color of editor

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-27 22:41:41 +08:00
parent 8960358feb
commit 0c3fc3b006
10 changed files with 261 additions and 5 deletions

View File

@ -7,3 +7,15 @@ template_css_url=qrc:/resources/markdown.css
current_notebook=0 current_notebook=0
tab_stop_width=4 tab_stop_width=4
is_expand_tab=1 is_expand_tab=1
current_background_color=System
[predefined_colors]
1\name=White
1\rgb=EEEEEE
2\name=Green
2\rgb=CCE8CF
3\name=Wheat2
3\rgb=EED8AE
4\name=LightGrey
4\rgb=D3D3D3
size=4

View File

@ -4,7 +4,6 @@
VUtils::VUtils() VUtils::VUtils()
{ {
} }
QString VUtils::readFileFromDisk(const QString &filePath) QString VUtils::readFileFromDisk(const QString &filePath)
@ -33,3 +32,27 @@ bool VUtils::writeFileToDisk(const QString &filePath, const QString &text)
qDebug() << "write file content:" << filePath; qDebug() << "write file content:" << filePath;
return true; return true;
} }
QRgb VUtils::QRgbFromString(const QString &str)
{
Q_ASSERT(str.length() == 6);
QString rStr = str.left(2);
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;
int green = gStr.toInt(&ok, 16);
ret = ret && ok;
int blue = bStr.toInt(&ok, 16);
ret = ret && ok;
if (ret) {
return qRgb(red, green, blue);
}
qWarning() << "error: fail to construct QRgb from string" << str;
return QRgb();
}

View File

@ -2,6 +2,8 @@
#define VUTILS_H #define VUTILS_H
#include <QString> #include <QString>
#include <QColor>
#include "vconfigmanager.h"
class VUtils class VUtils
{ {
@ -10,6 +12,8 @@ public:
static QString readFileFromDisk(const QString &filePath); static QString readFileFromDisk(const QString &filePath);
static bool writeFileToDisk(const QString &filePath, const QString &text); static bool writeFileToDisk(const QString &filePath, const QString &text);
// Transform FFFFFF string to QRgb
static QRgb QRgbFromString(const QString &str);
}; };
#endif // VUTILS_H #endif // VUTILS_H

View File

@ -53,12 +53,34 @@ void VConfigManager::initialize()
tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt(); tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt();
isExpandTab = getConfigFromSettings("global", "is_expand_tab").toBool(); isExpandTab = getConfigFromSettings("global", "is_expand_tab").toBool();
readPredefinedColorsFromSettings();
curBackgroundColor = getConfigFromSettings("global", "current_background_color").toString();
customBackgroundColor = getConfigFromSettings("global", "custom_background_color").toString();
updatePaletteColor();
// Update notebooks // Update notebooks
readNotebookFromSettings(); readNotebookFromSettings();
updateMarkdownEditStyle(); updateMarkdownEditStyle();
} }
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() void VConfigManager::readNotebookFromSettings()
{ {
notebooks.clear(); notebooks.clear();
@ -183,3 +205,29 @@ void VConfigManager::updateMarkdownEditStyle()
mdEditFont = baseEditFont; mdEditFont = baseEditFont;
parser.fetchMarkdownEditorStyles(mdEditPalette, mdEditFont); parser.fetchMarkdownEditorStyles(mdEditPalette, mdEditFont);
} }
void VConfigManager::updatePaletteColor()
{
QString rgb;
if (curBackgroundColor == "Custom") {
rgb = customBackgroundColor;
} else if (curBackgroundColor == "System") {
return;
} else {
for (int i = 0; i < predefinedColors.size(); ++i) {
if (predefinedColors[i].name == curBackgroundColor) {
rgb = predefinedColors[i].rgb;
break;
}
}
}
if (rgb.isEmpty()) {
return;
}
baseEditPalette.setColor(QPalette::Base,
QColor(VUtils::QRgbFromString(rgb)));
// Update markdown editor palette
updateMarkdownEditStyle();
}

View File

@ -13,11 +13,18 @@
class QJsonObject; class QJsonObject;
class QString; class QString;
enum MarkdownConverterType { enum MarkdownConverterType
{
Hoedown = 0, Hoedown = 0,
Marked Marked
}; };
struct VColor
{
QString name;
QString rgb; // 'FFFFFF', ithout '#'
};
class VConfigManager class VConfigManager
{ {
public: public:
@ -48,6 +55,7 @@ public:
inline QString getTemplateCssUrl() const; inline QString getTemplateCssUrl() const;
inline QFont getBaseEditFont() const; inline QFont getBaseEditFont() const;
inline QPalette getBaseEditPalette() const;
inline int getCurNotebookIndex() const; inline int getCurNotebookIndex() const;
inline void setCurNotebookIndex(int index); inline void setCurNotebookIndex(int index);
@ -67,12 +75,23 @@ public:
inline bool getIsExpandTab() const; inline bool getIsExpandTab() const;
inline void setIsExpandTab(bool isExpandTab); inline void setIsExpandTab(bool isExpandTab);
inline const QVector<VColor> &getPredefinedColors() const;
inline const QString &getCurBackgroundColor() const;
inline void setCurBackgroundColor(const QString &colorName);
inline const QString &getCustomBackgroundColor() const;
inline void setCustomBackgroundColor(const QString &colorRgb);
private: private:
void updateMarkdownEditStyle(); void updateMarkdownEditStyle();
QVariant getConfigFromSettings(const QString &section, const QString &key); QVariant getConfigFromSettings(const QString &section, const QString &key);
void setConfigToSettings(const QString &section, const QString &key, const QVariant &value); void setConfigToSettings(const QString &section, const QString &key, const QVariant &value);
void readNotebookFromSettings(); void readNotebookFromSettings();
void writeNotebookToSettings(); void writeNotebookToSettings();
void readPredefinedColorsFromSettings();
// Update baseEditPalette according to curBackgroundColor
void updatePaletteColor();
QFont baseEditFont; QFont baseEditFont;
QPalette baseEditPalette; QPalette baseEditPalette;
@ -96,6 +115,11 @@ private:
// Expand tab to @tabStopWidth spaces // Expand tab to @tabStopWidth spaces
bool isExpandTab; bool isExpandTab;
// App defined color
QVector<VColor> predefinedColors;
QString curBackgroundColor;
QString customBackgroundColor;
// The name of the config file in each directory // The name of the config file in each directory
static const QString dirConfigFileName; static const QString dirConfigFileName;
// The name of the default configuration file // The name of the default configuration file
@ -142,6 +166,11 @@ inline QFont VConfigManager::getBaseEditFont() const
return baseEditFont; return baseEditFont;
} }
inline QPalette VConfigManager::getBaseEditPalette() const
{
return baseEditPalette;
}
inline int VConfigManager::getCurNotebookIndex() const inline int VConfigManager::getCurNotebookIndex() const
{ {
return curNotebookIndex; return curNotebookIndex;
@ -224,4 +253,41 @@ inline void VConfigManager::setIsExpandTab(bool isExpandTab)
setConfigToSettings("global", "is_expand_tab", this->isExpandTab); setConfigToSettings("global", "is_expand_tab", this->isExpandTab);
} }
inline const QVector<VColor>& VConfigManager::getPredefinedColors() const
{
return predefinedColors;
}
inline const QString& VConfigManager::getCurBackgroundColor() const
{
return curBackgroundColor;
}
inline void VConfigManager::setCurBackgroundColor(const QString &colorName)
{
if (curBackgroundColor == colorName) {
return;
}
curBackgroundColor = colorName;
setConfigToSettings("global", "current_background_color",
curBackgroundColor);
updatePaletteColor();
}
inline const QString& VConfigManager::getCustomBackgroundColor() const
{
return customBackgroundColor;
}
inline void VConfigManager::setCustomBackgroundColor(const QString &colorRgb)
{
if (customBackgroundColor == colorRgb) {
return;
}
customBackgroundColor = colorRgb;
setConfigToSettings("global", "custom_background_color",
customBackgroundColor);
updatePaletteColor();
}
#endif // VCONFIGMANAGER_H #endif // VCONFIGMANAGER_H

View File

@ -10,17 +10,32 @@ VEdit::VEdit(VNoteFile *noteFile, QWidget *parent)
: QTextEdit(parent), noteFile(noteFile), mdHighlighter(NULL) : QTextEdit(parent), noteFile(noteFile), mdHighlighter(NULL)
{ {
if (noteFile->docType == DocType::Markdown) { if (noteFile->docType == DocType::Markdown) {
setPalette(vconfig.getMdEditPalette());
setFont(vconfig.getMdEditFont());
setAcceptRichText(false); setAcceptRichText(false);
mdHighlighter = new HGMarkdownHighlighter(vconfig.getMdHighlightingStyles(), mdHighlighter = new HGMarkdownHighlighter(vconfig.getMdHighlightingStyles(),
500, document()); 500, document());
} else { } else {
setFont(vconfig.getBaseEditFont());
setAutoFormatting(QTextEdit::AutoBulletList); setAutoFormatting(QTextEdit::AutoBulletList);
} }
updateTabSettings(); updateTabSettings();
updateFontAndPalette();
}
void VEdit::updateFontAndPalette()
{
switch (noteFile->docType) {
case DocType::Markdown:
setFont(vconfig.getMdEditFont());
setPalette(vconfig.getMdEditPalette());
break;
case DocType::Html:
setFont(vconfig.getBaseEditFont());
setPalette(vconfig.getBaseEditPalette());
break;
default:
qWarning() << "error: unknown doc type" << int(noteFile->docType);
return;
}
} }
void VEdit::updateTabSettings() void VEdit::updateTabSettings()
@ -53,6 +68,7 @@ void VEdit::beginEdit()
{ {
setReadOnly(false); setReadOnly(false);
updateTabSettings(); updateTabSettings();
updateFontAndPalette();
switch (noteFile->docType) { switch (noteFile->docType) {
case DocType::Html: case DocType::Html:
setHtml(noteFile->content); setHtml(noteFile->content);

View File

@ -28,6 +28,7 @@ protected:
private: private:
void updateTabSettings(); void updateTabSettings();
void updateFontAndPalette();
bool isExpandTab; bool isExpandTab;
QString tabSpaces; QString tabSpaces;

View File

@ -56,6 +56,7 @@ void VEditor::setupUI()
textBrowser = new QTextBrowser(); textBrowser = new QTextBrowser();
addWidget(textBrowser); addWidget(textBrowser);
textBrowser->setFont(vconfig.getBaseEditFont()); textBrowser->setFont(vconfig.getBaseEditFont());
textBrowser->setPalette(vconfig.getBaseEditPalette());
webPreviewer = NULL; webPreviewer = NULL;
break; break;
default: default:
@ -86,6 +87,8 @@ void VEditor::showFileReadMode()
switch (noteFile->docType) { switch (noteFile->docType) {
case DocType::Html: case DocType::Html:
textBrowser->setHtml(noteFile->content); textBrowser->setHtml(noteFile->content);
textBrowser->setFont(vconfig.getBaseEditFont());
textBrowser->setPalette(vconfig.getBaseEditPalette());
setCurrentWidget(textBrowser); setCurrentWidget(textBrowser);
break; break;
case DocType::Markdown: case DocType::Markdown:

View File

@ -7,6 +7,7 @@
#include "vconfigmanager.h" #include "vconfigmanager.h"
#include "dialog/vnewnotebookdialog.h" #include "dialog/vnewnotebookdialog.h"
#include "dialog/vnotebookinfodialog.h" #include "dialog/vnotebookinfodialog.h"
#include "utils/vutils.h"
extern VConfigManager vconfig; extern VConfigManager vconfig;
@ -15,6 +16,7 @@ VMainWindow::VMainWindow(QWidget *parent)
{ {
// Must be called before those who uses VConfigManager // Must be called before those who uses VConfigManager
vnote = new VNote(); vnote = new VNote();
initPredefinedColorPixmaps();
setupUI(); setupUI();
initActions(); initActions();
initToolBar(); initToolBar();
@ -205,6 +207,10 @@ void VMainWindow::initActions()
eightSpaceTabAct->setData(8); eightSpaceTabAct->setData(8);
connect(tabStopWidthAct, &QActionGroup::triggered, connect(tabStopWidthAct, &QActionGroup::triggered,
this, &VMainWindow::setTabStopWidth); this, &VMainWindow::setTabStopWidth);
backgroundColorAct = new QActionGroup(this);
connect(backgroundColorAct, &QActionGroup::triggered,
this, &VMainWindow::setEditorBackgroundColor);
} }
void VMainWindow::initToolBar() void VMainWindow::initToolBar()
@ -253,6 +259,43 @@ void VMainWindow::initMenuBar()
qWarning() << "error: unsupported tab stop width" << tabStopWidth << "in config"; 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);
// Markdown Menu // Markdown Menu
QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter")); QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter"));
converterMenu->addAction(hoedownAct); converterMenu->addAction(hoedownAct);
@ -485,3 +528,38 @@ void VMainWindow::setTabStopWidth(QAction *action)
} }
vconfig.setTabStopWidth(action->data().toInt()); vconfig.setTabStopWidth(action->data().toInt());
} }
void VMainWindow::setEditorBackgroundColor(QAction *action)
{
if (!action) {
return;
}
// TODO: implement custom color logics
if (action->data().toString() == "Custom") {
return;
}
vconfig.setCurBackgroundColor(action->data().toString());
}
void VMainWindow::initPredefinedColorPixmaps()
{
const QVector<VColor> &bgColors = vconfig.getPredefinedColors();
predefinedColorPixmaps.clear();
int size = 256;
for (int i = 0; i < bgColors.size(); ++i) {
// Generate QPixmap filled in this color
QColor color(VUtils::QRgbFromString(bgColors[i].rgb));
QPixmap pixmap(size, size);
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);
}
}

View File

@ -42,6 +42,7 @@ private slots:
void aboutMessage(); void aboutMessage();
void changeExpandTab(bool checked); void changeExpandTab(bool checked);
void setTabStopWidth(QAction *action); void setTabStopWidth(QAction *action);
void setEditorBackgroundColor(QAction *action);
signals: signals:
void curNotebookChanged(const QString &notebookName); void curNotebookChanged(const QString &notebookName);
@ -52,6 +53,7 @@ private:
void initToolBar(); void initToolBar();
void initMenuBar(); void initMenuBar();
bool isConflictWithExistingNotebooks(const QString &name); bool isConflictWithExistingNotebooks(const QString &name);
void initPredefinedColorPixmaps();
// If true, comboBox changes will not trigger any signal out // If true, comboBox changes will not trigger any signal out
bool notebookComboMuted; bool notebookComboMuted;
@ -86,6 +88,9 @@ private:
QAction *twoSpaceTabAct; QAction *twoSpaceTabAct;
QAction *fourSpaceTabAct; QAction *fourSpaceTabAct;
QAction *eightSpaceTabAct; QAction *eightSpaceTabAct;
QActionGroup *backgroundColorAct;
QVector<QPixmap> predefinedColorPixmaps;
}; };
#endif // VMAINWINDOW_H #endif // VMAINWINDOW_H