refine custom style logics

Scan and build the menu automatically.
This commit is contained in:
Le Tan 2017-04-22 19:54:43 +08:00
parent c25c4298f7
commit 2425d90b37
7 changed files with 126 additions and 39 deletions

View File

@ -1,4 +1,10 @@
# Changes History # Changes History
## v1.3
- Support code block syntax highlight in edit mode;
- A more pleasant AutoIndent and AutoList;
- `Ctrl+<Num>` instead of `Ctrl+Alt+<Num>` to insert title;
- Support custom Markdown CSS styles and editor styles;
## v1.2 ## v1.2
- Support **MathJax**. - Support **MathJax**.
- Fix a crash on macOS. - Fix a crash on macOS.

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="512px" height="512px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
<polygon points="448,224 288,224 288,64 224,64 224,224 64,224 64,288 224,288 224,448 288,448 288,288 448,288 "/>
</svg>

After

Width:  |  Height:  |  Size: 604 B

Binary file not shown.

View File

@ -970,12 +970,28 @@
<translation>使Markdown进行渲染</translation> <translation>使Markdown进行渲染</translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="843"/> <location filename="../vmainwindow.cpp" line="818"/>
<location filename="../vmainwindow.cpp" line="902"/>
<source>&amp;Add Style</source>
<translation> (&amp;A)</translation>
</message>
<message>
<location filename="../vmainwindow.cpp" line="819"/>
<source>Open the folder to add your custom CSS style files</source>
<translation>CSS样式文件</translation>
</message>
<message>
<location filename="../vmainwindow.cpp" line="892"/>
<source>Editor &amp;Style</source> <source>Editor &amp;Style</source>
<translation> (&amp;S)</translation> <translation> (&amp;S)</translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="854"/> <location filename="../vmainwindow.cpp" line="903"/>
<source>Open the folder to add your custom MDHL style files</source>
<translation>MDHL样式文件</translation>
</message>
<message>
<location filename="../vmainwindow.cpp" line="877"/>
<source>Set as the editor style</source> <source>Set as the editor style</source>
<translation>使</translation> <translation>使</translation>
</message> </message>
@ -1131,32 +1147,32 @@
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="752"/> <location filename="../vmainwindow.cpp" line="752"/>
<location filename="../vmainwindow.cpp" line="818"/> <location filename="../vmainwindow.cpp" line="837"/>
<source>System</source> <source>System</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="778"/> <location filename="../vmainwindow.cpp" line="808"/>
<source>Rendering &amp;Style</source> <source>Rendering &amp;Style</source>
<translation> (&amp;S)</translation> <translation> (&amp;S)</translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="789"/> <location filename="../vmainwindow.cpp" line="793"/>
<source>Set as the CSS style for Markdown rendering</source> <source>Set as the CSS style for Markdown rendering</source>
<translation>使CSS样式对Markdown进行渲染</translation> <translation>使CSS样式对Markdown进行渲染</translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="809"/> <location filename="../vmainwindow.cpp" line="828"/>
<source>&amp;Background Color</source> <source>&amp;Background Color</source>
<translation> (&amp;B)</translation> <translation> (&amp;B)</translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="819"/> <location filename="../vmainwindow.cpp" line="838"/>
<source>Use system&apos;s background color configuration for editor</source> <source>Use system&apos;s background color configuration for editor</source>
<translation>使</translation> <translation>使</translation>
</message> </message>
<message> <message>
<location filename="../vmainwindow.cpp" line="829"/> <location filename="../vmainwindow.cpp" line="848"/>
<source>Set as the background color for editor</source> <source>Set as the background color for editor</source>
<translation>使</translation> <translation>使</translation>
</message> </message>

View File

@ -773,35 +773,54 @@ void VMainWindow::initRenderBackgroundMenu(QMenu *menu)
} }
} }
void VMainWindow::initRenderStyleMenu(QMenu *p_menu) void VMainWindow::updateRenderStyleMenu()
{ {
QMenu *styleMenu = p_menu->addMenu(tr("Rendering &Style")); QMenu *menu = dynamic_cast<QMenu *>(sender());
styleMenu->setToolTipsVisible(true); V_ASSERT(menu);
QActionGroup *styleAct = new QActionGroup(this); QList<QAction *> actions = menu->actions();
connect(styleAct, &QActionGroup::triggered, // Remove all other actions except the first one.
this, &VMainWindow::setRenderStyle); for (int i = 1; i < actions.size(); ++i) {
menu->removeAction(actions[i]);
m_renderStyleActs->removeAction(actions[i]);
delete actions[i];
}
bool foundCurrentCss = false; // Update the menu actions with styles.
QVector<QString> styles = vconfig.getCssStyles(); QVector<QString> styles = vconfig.getCssStyles();
for (auto const &style : styles) { for (auto const &style : styles) {
QAction *act = new QAction(style, styleAct); QAction *act = new QAction(style, m_renderStyleActs);
act->setToolTip(tr("Set as the CSS style for Markdown rendering")); act->setToolTip(tr("Set as the CSS style for Markdown rendering"));
act->setCheckable(true); act->setCheckable(true);
act->setData(style); act->setData(style);
// Add it to the menu.
menu->addAction(act);
if (vconfig.getTemplateCss() == style) { if (vconfig.getTemplateCss() == style) {
act->setChecked(true); act->setChecked(true);
foundCurrentCss = true;
} }
} }
}
if (!foundCurrentCss && styles.isEmpty()) { void VMainWindow::initRenderStyleMenu(QMenu *p_menu)
delete styleAct; {
return; QMenu *styleMenu = p_menu->addMenu(tr("Rendering &Style"));
} styleMenu->setToolTipsVisible(true);
connect(styleMenu, &QMenu::aboutToShow,
this, &VMainWindow::updateRenderStyleMenu);
styleMenu->addActions(styleAct->actions()); m_renderStyleActs = new QActionGroup(this);
connect(m_renderStyleActs, &QActionGroup::triggered,
this, &VMainWindow::setRenderStyle);
QAction *addAct = new QAction(QIcon(":/resources/icons/add_style.svg"),
tr("&Add Style"), m_renderStyleActs);
addAct->setToolTip(tr("Open the folder to add your custom CSS style files"));
addAct->setCheckable(true);
addAct->setData("AddStyle");
styleMenu->addAction(addAct);
} }
void VMainWindow::initEditorBackgroundMenu(QMenu *menu) void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
@ -838,35 +857,54 @@ void VMainWindow::initEditorBackgroundMenu(QMenu *menu)
} }
} }
void VMainWindow::initEditorStyleMenu(QMenu *p_menu) void VMainWindow::updateEditorStyleMenu()
{ {
QMenu *styleMenu = p_menu->addMenu(tr("Editor &Style")); QMenu *menu = dynamic_cast<QMenu *>(sender());
styleMenu->setToolTipsVisible(true); V_ASSERT(menu);
QActionGroup *styleAct = new QActionGroup(this); QList<QAction *> actions = menu->actions();
connect(styleAct, &QActionGroup::triggered, // Remove all other actions except the first one.
this, &VMainWindow::setEditorStyle); for (int i = 1; i < actions.size(); ++i) {
menu->removeAction(actions[i]);
m_editorStyleActs->removeAction(actions[i]);
delete actions[i];
}
bool found = false; // Update the menu actions with styles.
QVector<QString> styles = vconfig.getEditorStyles(); QVector<QString> styles = vconfig.getEditorStyles();
for (auto const &style : styles) { for (auto const &style : styles) {
QAction *act = new QAction(style, styleAct); QAction *act = new QAction(style, m_editorStyleActs);
act->setToolTip(tr("Set as the editor style")); act->setToolTip(tr("Set as the editor style"));
act->setCheckable(true); act->setCheckable(true);
act->setData(style); act->setData(style);
// Add it to the menu.
menu->addAction(act);
if (vconfig.getEditorStyle() == style) { if (vconfig.getEditorStyle() == style) {
act->setChecked(true); act->setChecked(true);
found = true;
} }
} }
}
if (!found && styles.isEmpty()) { void VMainWindow::initEditorStyleMenu(QMenu *p_menu)
delete styleAct; {
return; QMenu *styleMenu = p_menu->addMenu(tr("Editor &Style"));
} styleMenu->setToolTipsVisible(true);
connect(styleMenu, &QMenu::aboutToShow,
this, &VMainWindow::updateEditorStyleMenu);
styleMenu->addActions(styleAct->actions()); m_editorStyleActs = new QActionGroup(this);
connect(m_editorStyleActs, &QActionGroup::triggered,
this, &VMainWindow::setEditorStyle);
QAction *addAct = new QAction(QIcon(":/resources/icons/add_style.svg"),
tr("&Add Style"), m_editorStyleActs);
addAct->setToolTip(tr("Open the folder to add your custom MDHL style files"));
addAct->setCheckable(true);
addAct->setData("AddStyle");
styleMenu->addAction(addAct);
} }
void VMainWindow::setRenderBackgroundColor(QAction *action) void VMainWindow::setRenderBackgroundColor(QAction *action)
@ -884,8 +922,15 @@ void VMainWindow::setRenderStyle(QAction *p_action)
return; return;
} }
vconfig.setTemplateCss(p_action->data().toString()); QString data = p_action->data().toString();
vnote->updateTemplate(); if (data == "AddStyle") {
// Add custom style.
QUrl url = QUrl::fromLocalFile(vconfig.getStyleConfigFolder());
QDesktopServices::openUrl(url);
} else {
vconfig.setTemplateCss(data);
vnote->updateTemplate();
}
} }
void VMainWindow::setEditorStyle(QAction *p_action) void VMainWindow::setEditorStyle(QAction *p_action)
@ -894,7 +939,14 @@ void VMainWindow::setEditorStyle(QAction *p_action)
return; return;
} }
vconfig.setEditorStyle(p_action->data().toString()); QString data = p_action->data().toString();
if (data == "AddStyle") {
// Add custom style.
QUrl url = QUrl::fromLocalFile(vconfig.getStyleConfigFolder());
QDesktopServices::openUrl(url);
} else {
vconfig.setEditorStyle(data);
}
} }
void VMainWindow::updateActionStateFromTabStatusChange(const VFile *p_file, void VMainWindow::updateActionStateFromTabStatusChange(const VFile *p_file,

View File

@ -54,6 +54,8 @@ private slots:
void setRenderBackgroundColor(QAction *action); void setRenderBackgroundColor(QAction *action);
void setRenderStyle(QAction *p_action); void setRenderStyle(QAction *p_action);
void setEditorStyle(QAction *p_action); void setEditorStyle(QAction *p_action);
void updateRenderStyleMenu();
void updateEditorStyleMenu();
void changeHighlightCursorLine(bool p_checked); void changeHighlightCursorLine(bool p_checked);
void changeHighlightSelectedWord(bool p_checked); void changeHighlightSelectedWord(bool p_checked);
void changeHighlightSearchedWord(bool p_checked); void changeHighlightSearchedWord(bool p_checked);
@ -158,6 +160,9 @@ private:
QAction *m_autoIndentAct; QAction *m_autoIndentAct;
QActionGroup *m_renderStyleActs;
QActionGroup *m_editorStyleActs;
// Menus // Menus
QMenu *viewMenu; QMenu *viewMenu;

View File

@ -99,5 +99,6 @@
<file>resources/docs/shortcuts_en.md</file> <file>resources/docs/shortcuts_en.md</file>
<file>resources/docs/shortcuts_zh.md</file> <file>resources/docs/shortcuts_zh.md</file>
<file>resources/styles/default.css</file> <file>resources/styles/default.css</file>
<file>resources/icons/add_style.svg</file>
</qresource> </qresource>
</RCC> </RCC>