Browse Source

refine custom style logics

Scan and build the menu automatically.
Le Tan 8 years ago
parent
commit
2425d90b37

+ 6 - 0
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+<Num>` instead of `Ctrl+Alt+<Num>` to insert title;
+- Support custom Markdown CSS styles and editor styles;
+
 ## v1.2
 - Support **MathJax**.
 - Fix a crash on macOS.

+ 7 - 0
src/resources/icons/add_style.svg

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

BIN
src/translations/vnote_zh_CN.qm


+ 24 - 8
src/translations/vnote_zh_CN.ts

@@ -970,12 +970,28 @@
         <translation>使用该背景色对Markdown进行渲染</translation>
     </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>
         <translation>编辑器样式 (&amp;S)</translation>
     </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>
         <translation>使用该样式设置编辑器</translation>
     </message>
@@ -1131,32 +1147,32 @@
     </message>
     <message>
         <location filename="../vmainwindow.cpp" line="752"/>
-        <location filename="../vmainwindow.cpp" line="818"/>
+        <location filename="../vmainwindow.cpp" line="837"/>
         <source>System</source>
         <translation>默认</translation>
     </message>
     <message>
-        <location filename="../vmainwindow.cpp" line="778"/>
+        <location filename="../vmainwindow.cpp" line="808"/>
         <source>Rendering &amp;Style</source>
         <translation>渲染样式 (&amp;S)</translation>
     </message>
     <message>
-        <location filename="../vmainwindow.cpp" line="789"/>
+        <location filename="../vmainwindow.cpp" line="793"/>
         <source>Set as the CSS style for Markdown rendering</source>
         <translation>使用该CSS样式对Markdown进行渲染</translation>
     </message>
     <message>
-        <location filename="../vmainwindow.cpp" line="809"/>
+        <location filename="../vmainwindow.cpp" line="828"/>
         <source>&amp;Background Color</source>
         <translation>背景颜色 (&amp;B)</translation>
     </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>
         <translation>为编辑器使用系统的背景色设置</translation>
     </message>
     <message>
-        <location filename="../vmainwindow.cpp" line="829"/>
+        <location filename="../vmainwindow.cpp" line="848"/>
         <source>Set as the background color for editor</source>
         <translation>使用该背景色设置编辑器</translation>
     </message>

+ 83 - 31
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<QMenu *>(sender());
+    V_ASSERT(menu);
 
-    QActionGroup *styleAct = new QActionGroup(this);
-    connect(styleAct, &QActionGroup::triggered,
-            this, &VMainWindow::setRenderStyle);
+    QList<QAction *> 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<QString> 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);
+
+    m_renderStyleActs = new QActionGroup(this);
+    connect(m_renderStyleActs, &QActionGroup::triggered,
+            this, &VMainWindow::setRenderStyle);
 
-    styleMenu->addActions(styleAct->actions());
+    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<QMenu *>(sender());
+    V_ASSERT(menu);
 
-    QActionGroup *styleAct = new QActionGroup(this);
-    connect(styleAct, &QActionGroup::triggered,
-            this, &VMainWindow::setEditorStyle);
+    QList<QAction *> 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<QString> 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);
+
+    m_editorStyleActs = new QActionGroup(this);
+    connect(m_editorStyleActs, &QActionGroup::triggered,
+            this, &VMainWindow::setEditorStyle);
 
-    styleMenu->addActions(styleAct->actions());
+    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,

+ 5 - 0
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;
 

+ 1 - 0
src/vnote.qrc

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