support custom editor style by mdhl files

This commit is contained in:
Le Tan 2017-04-22 17:45:43 +08:00
parent d0d4e1443d
commit d9185d3352
7 changed files with 255 additions and 14 deletions

View File

@ -8,6 +8,10 @@ foreground: 93a1a1 # base1
background: 002b36 # base03
caret: ffffff
editor-current-line
background: 37474f
vim-background: 004d40
H1
foreground: 6c71c4 # violet
font-style: bold
@ -71,6 +75,42 @@ foreground: 93a1a1 # base1
VERBATIM
foreground: 859900 # green
font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New
# Codeblock sylte from HighlightJS (bold, italic, underlined, color)
# The last occurence of the same attribute takes effect
hljs-comment: 888888
hljs-keyword: bold
hljs-attribute: bold
hljs-selector-tag: bold
hljs-meta-keyword: bold
hljs-doctag: bold
hljs-name: bold
hljs-type: bf360c
hljs-string: bf360c
hljs-number: bf360c
hljs-selector-id: bf360c
hljs-selector-class: bf360c
hljs-quote: bf360c
hljs-template-tag: bf360c
hljs-deletion: bf360c
hljs-title: bold, bf360c
hljs-section: bold, bf360c
hljs-regexp: bc6060
hljs-symbol: bc6060
hljs-variable: bc6060
hljs-template-variable: bc6060
hljs-link: bc6060
hljs-selector-attr: bc6060
hljs-selector-pseudo: bc6060
hljs-literal: 78a960
hljs-built_in: 397300
hljs-bullet: 397300
hljs-code: 397300
hljs-addition: 397300
hljs-meta: 1f7199
hljs-meta-string: 4d99bf
hljs-emphasis: italic
hljs-strong: bold
BLOCKQUOTE
foreground: d33682 # magenta

View File

@ -8,6 +8,10 @@ foreground: 586e75 # base01
background: fdf6e3 # base3
caret: 000000
editor-current-line
background: c5cae9
vim-background: a5d6a7
H1
foreground: 6c71c4 # violet
font-style: bold
@ -71,6 +75,42 @@ foreground: 93a1a1 # base1
VERBATIM
foreground: 859900 # green
font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New
# Codeblock sylte from HighlightJS (bold, italic, underlined, color)
# The last occurence of the same attribute takes effect
hljs-comment: 888888
hljs-keyword: bold
hljs-attribute: bold
hljs-selector-tag: bold
hljs-meta-keyword: bold
hljs-doctag: bold
hljs-name: bold
hljs-type: 880000
hljs-string: 880000
hljs-number: 880000
hljs-selector-id: 880000
hljs-selector-class: 880000
hljs-quote: 880000
hljs-template-tag: 880000
hljs-deletion: 880000
hljs-title: bold, 880000
hljs-section: bold, 880000
hljs-regexp: bc6060
hljs-symbol: bc6060
hljs-variable: bc6060
hljs-template-variable: bc6060
hljs-link: bc6060
hljs-selector-attr: bc6060
hljs-selector-pseudo: bc6060
hljs-literal: 78a960
hljs-built_in: 397300
hljs-bullet: 397300
hljs-code: 397300
hljs-addition: 397300
hljs-meta: 1f7199
hljs-meta-string: 4d99bf
hljs-emphasis: italic
hljs-strong: bold
BLOCKQUOTE
foreground: d33682 # magenta

View File

@ -1,6 +1,7 @@
[global]
welcome_page_path=:/resources/welcome.html
template_css=default
editor_style=default
current_notebook=0
tab_stop_width=4
is_expand_tab=true
@ -20,7 +21,7 @@ enable_mathjax=false
; -1 - calculate the factor
web_zoom_factor=-1
; Syntax highlight within code blocks in edit mode
enable_code_block_highlight=false
enable_code_block_highlight=true
[session]
tools_dock_checked=true

View File

@ -17,6 +17,9 @@ const QString VConfigManager::dirConfigFileName = QString(".vnote.json");
const QString VConfigManager::defaultConfigFilePath = QString(":/resources/vnote.ini");
const QString VConfigManager::c_styleConfigFolder = QString("styles");
const QString VConfigManager::c_defaultCssFile = QString(":/resources/styles/default.css");
const QString VConfigManager::c_defaultMdhlFile = QString(":/resources/styles/default.mdhl");
const QString VConfigManager::c_solarizedDarkMdhlFile = QString(":/resources/styles/solarized-dark.mdhl");
const QString VConfigManager::c_solarizedLightMdhlFile = QString(":/resources/styles/solarized-light.mdhl");
VConfigManager::VConfigManager()
: userSettings(NULL), defaultSettings(NULL)
@ -58,6 +61,8 @@ void VConfigManager::initialize()
baseEditFont.setPointSize(m_editorFontSize);
baseEditPalette = QTextEdit().palette();
m_editorStyle = getConfigFromSettings("global", "editor_style").toString();
welcomePagePath = getConfigFromSettings("global", "welcome_page_path").toString();
m_templateCss = getConfigFromSettings("global", "template_css").toString();
curNotebookIndex = getConfigFromSettings("global", "current_notebook").toInt();
@ -78,7 +83,7 @@ void VConfigManager::initialize()
readPredefinedColorsFromSettings();
curBackgroundColor = getConfigFromSettings("global", "current_background_color").toString();
updatePaletteColor();
updateEditStyle();
curRenderBackgroundColor = getConfigFromSettings("global",
"current_render_background_color").toString();
@ -88,8 +93,6 @@ void VConfigManager::initialize()
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",
@ -251,7 +254,7 @@ void VConfigManager::updateMarkdownEditStyle()
static const QString defaultCurrentLineVimBackground = "#A5D6A7";
// Read style file .mdhl
QString file(":/resources/styles/default.mdhl");
QString file(getEditorStyleUrl());
QString styleStr = VUtils::readFileFromDisk(file);
if (styleStr.isEmpty()) {
@ -281,20 +284,20 @@ void VConfigManager::updateMarkdownEditStyle()
m_editorCurrentLineVimBackground = "#" + *vimBackgroundIt;
}
}
qDebug() << "editor-current-line:" << m_editorCurrentLineBackground << m_editorCurrentLineVimBackground;
}
void VConfigManager::updatePaletteColor()
void VConfigManager::updateEditStyle()
{
static const QColor defaultColor = baseEditPalette.color(QPalette::Base);
QColor newColor = defaultColor;
bool force = false;
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));
force = true;
}
break;
}
@ -305,6 +308,10 @@ void VConfigManager::updatePaletteColor()
// Update markdown editor palette
updateMarkdownEditStyle();
if (force) {
mdEditPalette.setColor(QPalette::Base, newColor);
}
}
void VConfigManager::setWebZoomFactor(qreal p_factor)
@ -356,6 +363,28 @@ QVector<QString> VConfigManager::getCssStyles() const
for (auto const &item : files) {
res.push_back(item.left(item.size() - 4));
}
return res;
}
QVector<QString> VConfigManager::getEditorStyles() const
{
QVector<QString> res;
QDir dir(getStyleConfigFolder());
if (!dir.exists()) {
// Output pre-defined mdhl styles to this folder.
outputDefaultEditorStyle();
}
// Get all the .mdhl files in the folder.
dir.setFilter(QDir::Files | QDir::NoSymLinks);
dir.setNameFilters(QStringList("*.mdhl"));
QStringList files = dir.entryList();
res.reserve(files.size());
for (auto const &item : files) {
res.push_back(item.left(item.size() - 5));
}
return res;
}
@ -368,11 +397,44 @@ bool VConfigManager::outputDefaultCssStyle() const
return false;
}
}
QFileInfo finfo(c_defaultMdhlFile);
return VUtils::copyFile(c_defaultCssFile,
getStyleConfigFolder() + QDir::separator() + "default.css",
getStyleConfigFolder() + QDir::separator() + finfo.fileName(),
false);
}
bool VConfigManager::outputDefaultEditorStyle() const
{
// Make sure the styles folder exists.
QDir dir(getConfigFolder());
if (!dir.exists(c_styleConfigFolder)) {
if (!dir.mkdir(c_styleConfigFolder)) {
return false;
}
}
QString srcPath = c_defaultMdhlFile;
QString destPath = getStyleConfigFolder() + QDir::separator() + QFileInfo(srcPath).fileName();
if (!VUtils::copyFile(srcPath, destPath, false)) {
return false;
}
srcPath = c_solarizedDarkMdhlFile;
destPath = getStyleConfigFolder() + QDir::separator() + QFileInfo(srcPath).fileName();
if (!VUtils::copyFile(srcPath, destPath, false)) {
return false;
}
srcPath = c_solarizedLightMdhlFile;
destPath = getStyleConfigFolder() + QDir::separator() + QFileInfo(srcPath).fileName();
if (!VUtils::copyFile(srcPath, destPath, false)) {
return false;
}
return true;
}
QString VConfigManager::getTemplateCssUrl()
{
QString cssPath = getStyleConfigFolder() + QDir::separator() + m_templateCss + ".css";
@ -394,6 +456,28 @@ QString VConfigManager::getTemplateCssUrl()
return cssPath;
}
QString VConfigManager::getEditorStyleUrl()
{
QString mdhlPath = getStyleConfigFolder() + QDir::separator() + m_editorStyle + ".mdhl";
if (!QFile::exists(mdhlPath)) {
// Specified mdhl file not exists.
if (m_editorStyle == "default") {
bool ret = outputDefaultEditorStyle();
if (!ret) {
// Use embedded file.
mdhlPath = c_defaultMdhlFile;
}
} else {
setEditorStyle("default");
return getEditorStyleUrl();
}
}
qDebug() << "use editor style:" << mdhlPath;
return mdhlPath;
}
const QString &VConfigManager::getTemplateCss() const
{
return m_templateCss;
@ -407,3 +491,18 @@ void VConfigManager::setTemplateCss(const QString &p_css)
m_templateCss = p_css;
setConfigToSettings("global", "template_css", m_templateCss);
}
const QString &VConfigManager::getEditorStyle() const
{
return m_editorStyle;
}
void VConfigManager::setEditorStyle(const QString &p_style)
{
if (m_editorStyle == p_style) {
return;
}
m_editorStyle = p_style;
setConfigToSettings("global", "editor_style", m_editorStyle);
updateEditStyle();
}

View File

@ -56,9 +56,14 @@ public:
QString getTemplateCssUrl();
QString getEditorStyleUrl();
const QString &getTemplateCss() const;
void setTemplateCss(const QString &p_css);
const QString &getEditorStyle() const;
void setEditorStyle(const QString &p_style);
inline QFont getBaseEditFont() const;
inline QPalette getBaseEditPalette() const;
@ -152,21 +157,26 @@ public:
// Read all available css files in c_styleConfigFolder.
QVector<QString> getCssStyles() const;
// Read all available mdhl files in c_styleConfigFolder.
QVector<QString> getEditorStyles() const;
private:
void updateMarkdownEditStyle();
QVariant getConfigFromSettings(const QString &section, const QString &key);
void setConfigToSettings(const QString &section, const QString &key, const QVariant &value);
void readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent);
void writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks);
void readPredefinedColorsFromSettings();
// Update baseEditPalette according to curBackgroundColor
void updatePaletteColor();
// 1. Update styles common in HTML and Markdown;
// 2. Update styles for Markdown.
void updateEditStyle();
void updateMarkdownEditStyle();
// Migrate ini file from tamlok/vnote.ini to vnote/vnote.ini.
// This is for the change of org name.
void migrateIniFile();
bool outputDefaultCssStyle() const;
bool outputDefaultEditorStyle() const;
int m_editorFontSize;
QFont baseEditFont;
@ -177,6 +187,7 @@ private:
QMap<QString, QTextCharFormat> m_codeBlockStyles;
QString welcomePagePath;
QString m_templateCss;
QString m_editorStyle;
int curNotebookIndex;
// Markdown Converter
@ -251,6 +262,11 @@ private:
// The folder name of style files.
static const QString c_styleConfigFolder;
static const QString c_defaultCssFile;
// MDHL files for editor styles.
static const QString c_defaultMdhlFile;
static const QString c_solarizedDarkMdhlFile;
static const QString c_solarizedLightMdhlFile;
};
@ -452,7 +468,7 @@ inline void VConfigManager::setCurBackgroundColor(const QString &colorName)
curBackgroundColor = colorName;
setConfigToSettings("global", "current_background_color",
curBackgroundColor);
updatePaletteColor();
updateEditStyle();
}
inline const QString& VConfigManager::getCurRenderBackgroundColor() const

View File

@ -591,6 +591,8 @@ void VMainWindow::initEditMenu()
editMenu->addSeparator();
initEditorStyleMenu(editMenu);
initEditorBackgroundMenu(editMenu);
editMenu->addAction(cursorLineAct);
@ -805,8 +807,8 @@ void VMainWindow::initRenderStyleMenu(QMenu *p_menu)
void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
{
QMenu *backgroundColorMenu = menu->addMenu(tr("&Background Color"));
backgroundColorMenu->setToolTipsVisible(true);
QActionGroup *backgroundColorAct = new QActionGroup(this);
connect(backgroundColorAct, &QActionGroup::triggered,
this, &VMainWindow::setEditorBackgroundColor);
@ -836,6 +838,37 @@ void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
}
}
void VMainWindow::initEditorStyleMenu(QMenu *p_menu)
{
QMenu *styleMenu = p_menu->addMenu(tr("Editor &Style"));
styleMenu->setToolTipsVisible(true);
QActionGroup *styleAct = new QActionGroup(this);
connect(styleAct, &QActionGroup::triggered,
this, &VMainWindow::setEditorStyle);
bool found = false;
QVector<QString> styles = vconfig.getEditorStyles();
for (auto const &style : styles) {
QAction *act = new QAction(style, styleAct);
act->setToolTip(tr("Set as the editor style"));
act->setCheckable(true);
act->setData(style);
if (vconfig.getEditorStyle() == style) {
act->setChecked(true);
found = true;
}
}
if (!found && styles.isEmpty()) {
delete styleAct;
return;
}
styleMenu->addActions(styleAct->actions());
}
void VMainWindow::setRenderBackgroundColor(QAction *action)
{
if (!action) {
@ -850,10 +883,20 @@ void VMainWindow::setRenderStyle(QAction *p_action)
if (!p_action) {
return;
}
vconfig.setTemplateCss(p_action->data().toString());
vnote->updateTemplate();
}
void VMainWindow::setEditorStyle(QAction *p_action)
{
if (!p_action) {
return;
}
vconfig.setEditorStyle(p_action->data().toString());
}
void VMainWindow::updateActionStateFromTabStatusChange(const VFile *p_file,
bool p_editMode)
{

View File

@ -53,6 +53,7 @@ private slots:
void setEditorBackgroundColor(QAction *action);
void setRenderBackgroundColor(QAction *action);
void setRenderStyle(QAction *p_action);
void setEditorStyle(QAction *p_action);
void changeHighlightCursorLine(bool p_checked);
void changeHighlightSelectedWord(bool p_checked);
void changeHighlightSearchedWord(bool p_checked);
@ -100,6 +101,7 @@ private:
void initRenderBackgroundMenu(QMenu *menu);
void initRenderStyleMenu(QMenu *p_menu);
void initEditorBackgroundMenu(QMenu *menu);
void initEditorStyleMenu(QMenu *p_emnu);
void changeSplitterView(int nrPanel);
void updateWindowTitle(const QString &str);
void updateActionStateFromTabStatusChange(const VFile *p_file,