From 2425d90b37b244cad279c2b374fae4e681fc1d8a Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sat, 22 Apr 2017 19:54:43 +0800 Subject: [PATCH] refine custom style logics Scan and build the menu automatically. --- changes.md | 6 ++ src/resources/icons/add_style.svg | 7 ++ src/translations/vnote_zh_CN.qm | Bin 22142 -> 22473 bytes src/translations/vnote_zh_CN.ts | 32 ++++++--- src/vmainwindow.cpp | 114 ++++++++++++++++++++++-------- src/vmainwindow.h | 5 ++ src/vnote.qrc | 1 + 7 files changed, 126 insertions(+), 39 deletions(-) create mode 100644 src/resources/icons/add_style.svg diff --git a/changes.md b/changes.md index 59709fca..1afe2599 100644 --- a/changes.md +++ b/changes.md @@ -1,4 +1,10 @@ # Changes History +## v1.3 +- Support code block syntax highlight in edit mode; +- A more pleasant AutoIndent and AutoList; +- `Ctrl+` instead of `Ctrl+Alt+` to insert title; +- Support custom Markdown CSS styles and editor styles; + ## v1.2 - Support **MathJax**. - Fix a crash on macOS. diff --git a/src/resources/icons/add_style.svg b/src/resources/icons/add_style.svg new file mode 100644 index 00000000..86bd5b44 --- /dev/null +++ b/src/resources/icons/add_style.svg @@ -0,0 +1,7 @@ + + + + + + diff --git a/src/translations/vnote_zh_CN.qm b/src/translations/vnote_zh_CN.qm index f3075c825620f2a0c001f586a58c191d2abbbe63..d7cf79fbfa28184cc66d5d4b4663fe517a00c19d 100644 GIT binary patch delta 1980 zcma)6dr*{h6g|89y>|E8T@!E>V_8KPc2@yGUYok|ekhAcDn1Ei4HaAyB`snH6iJ;- zTAk2@G$r$aC77uwPK_Xlf|*WcqcSR~SXw^NEU@eB)J)S%(;qu$zwh4PIrpA>e_!uj zNy}%F^`0*W0LzMjxZ_}Eo&ea_-Pq3prkw$Xb6_V|1L0G^Rx|>ZxnSE*0x=Qb3zR_O z1q_&Eq;q%S-CPKmHk8}_qyK=Zo(`w^z;t#fFk~T2KLrzRgPCaum|1T08t2C3YM7ss z8tP!ax)wAbK$F zSy&*zCJ|$=1F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HoLu>8`AmI@n*+rmdCyknN z3rKcq!X}LeeA+Zo!}TPYrWuzw81QsxCi_fg{RB-x%lp9KahgvHvVkr$G!K&R0OndZ zhCkChsH^}ixo-5cm1}2QyTSrVTTt5om}Y7VPf}gnc~_;Tzddb_wk?MKdH$FGq z_v)%%>;eWh>3*zi2SP%04L)>P^%TS@+sQIakdmT-0SAQWO$&kag@SF~HlXuVVc@iP zS$|kaT^hsfjTR=>*Ykb6kf(VDM5PNeO?h-DN|?LF3=Ewrl%*dBoI?i*$JHbNXN211 zlR)1>;rv9}6;>rQ<`a8G2sdUA0x}YXdyNL*^-7_mnM6HKiN2ZnK>U6&uJiy96D4N$ z=J~D_ZcO?_%xVkbX6<6BH30Bk?#A?5amRx9$ZsHi3@1W&^9LsgZ4&qGKEVce-56gY zp56A8$_vH%$P!K*FE;eq3Iq)in+81u5+{oHwv+icltu_3MpIv;0>oI1anv_s{b=4F5sF3-8ec3C6k6&Dv#o=)C!_G`8m zcw6TlYu7rDW!2`P~qw_<6=n=};gV`MG zvNC;rC-Oa23cjH4Lvq~cOl?(WI^O1@yp(xyY!EwKDcpXaLZ&Ip@6p(RJIbm#b{5JbDz+u5_caK)?0M5>EWH{=j}Vj0@M_mg$7_tNy=0I+^jW-7bHCjBc-d$tA8^TnpqXXE*Qh^a2&07X1Bwde4CpP$Uq z7-HX6*GF=oQ!?*6=M6Zt<{!RoVdO@c>k6*&lY7|Qe31)^IBstB;7#gz-6GdqXWc1_ zF^lZ_otC}{4sN={GOW(V`d~NqEVks>^&BDEGRBhz_O@HzDY*vthgxzEmNOw+EZa2O h8TAvEeU5C75Ty8Pvqx4ZD@suHQfqgw>heUZ&;J9a<{SV3 diff --git a/src/translations/vnote_zh_CN.ts b/src/translations/vnote_zh_CN.ts index 1987b356..65192c65 100644 --- a/src/translations/vnote_zh_CN.ts +++ b/src/translations/vnote_zh_CN.ts @@ -970,12 +970,28 @@ 使用该背景色对Markdown进行渲染 - + + + &Add Style + 添加样式 (&A) + + + + Open the folder to add your custom CSS style files + 打开样式文件夹以添加自定义CSS样式文件 + + + Editor &Style 编辑器样式 (&S) - + + Open the folder to add your custom MDHL style files + 打开样式文件夹以添加自定义MDHL样式文件 + + + Set as the editor style 使用该样式设置编辑器 @@ -1131,32 +1147,32 @@ - + System 默认 - + Rendering &Style 渲染样式 (&S) - + Set as the CSS style for Markdown rendering 使用该CSS样式对Markdown进行渲染 - + &Background Color 背景颜色 (&B) - + Use system's background color configuration for editor 为编辑器使用系统的背景色设置 - + Set as the background color for editor 使用该背景色设置编辑器 diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 4101e03f..79445c89 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -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")); - styleMenu->setToolTipsVisible(true); + QMenu *menu = dynamic_cast(sender()); + V_ASSERT(menu); - QActionGroup *styleAct = new QActionGroup(this); - connect(styleAct, &QActionGroup::triggered, - this, &VMainWindow::setRenderStyle); + QList actions = menu->actions(); + // Remove all other actions except the first one. + 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 styles = vconfig.getCssStyles(); 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->setCheckable(true); act->setData(style); + // Add it to the menu. + menu->addAction(act); + if (vconfig.getTemplateCss() == style) { act->setChecked(true); - foundCurrentCss = true; } } +} - if (!foundCurrentCss && styles.isEmpty()) { - delete styleAct; - return; - } +void VMainWindow::initRenderStyleMenu(QMenu *p_menu) +{ + 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) @@ -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")); - styleMenu->setToolTipsVisible(true); + QMenu *menu = dynamic_cast(sender()); + V_ASSERT(menu); - QActionGroup *styleAct = new QActionGroup(this); - connect(styleAct, &QActionGroup::triggered, - this, &VMainWindow::setEditorStyle); + QList actions = menu->actions(); + // Remove all other actions except the first one. + 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 styles = vconfig.getEditorStyles(); 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->setCheckable(true); act->setData(style); + // Add it to the menu. + menu->addAction(act); + if (vconfig.getEditorStyle() == style) { act->setChecked(true); - found = true; } } +} - if (!found && styles.isEmpty()) { - delete styleAct; - return; - } +void VMainWindow::initEditorStyleMenu(QMenu *p_menu) +{ + 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) @@ -884,8 +922,15 @@ void VMainWindow::setRenderStyle(QAction *p_action) return; } - vconfig.setTemplateCss(p_action->data().toString()); - vnote->updateTemplate(); + QString data = p_action->data().toString(); + 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) @@ -894,7 +939,14 @@ void VMainWindow::setEditorStyle(QAction *p_action) 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, diff --git a/src/vmainwindow.h b/src/vmainwindow.h index e2385610..e4298749 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -54,6 +54,8 @@ private slots: void setRenderBackgroundColor(QAction *action); void setRenderStyle(QAction *p_action); void setEditorStyle(QAction *p_action); + void updateRenderStyleMenu(); + void updateEditorStyleMenu(); void changeHighlightCursorLine(bool p_checked); void changeHighlightSelectedWord(bool p_checked); void changeHighlightSearchedWord(bool p_checked); @@ -158,6 +160,9 @@ private: QAction *m_autoIndentAct; + QActionGroup *m_renderStyleActs; + QActionGroup *m_editorStyleActs; + // Menus QMenu *viewMenu; diff --git a/src/vnote.qrc b/src/vnote.qrc index 9ab465ec..1556fe27 100644 --- a/src/vnote.qrc +++ b/src/vnote.qrc @@ -99,5 +99,6 @@ resources/docs/shortcuts_en.md resources/docs/shortcuts_zh.md resources/styles/default.css + resources/icons/add_style.svg