Browse Source

add heading tool button

Le Tan 8 years ago
parent
commit
cb6338ecf6

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

@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<svg width="512" height="512" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
+ <g>
+  <title>Layer 1</title>
+  <text stroke="#000000" transform="matrix(16.72881317138672,0,0,16.72881317138672,2707.567729830742,-3759.186347961426) " font-weight="bold" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" id="svg_4" y="248.490375" x="-146.756839" stroke-width="0" fill="#000000">H</text>
+ </g>
+</svg>

+ 2 - 1
src/vconstants.h

@@ -72,7 +72,8 @@ enum class TextDecoration
     Underline,
     Strikethrough,
     InlineCode,
-    CodeBlock
+    CodeBlock,
+    Heading
 };
 
 enum FindOption

+ 0 - 5
src/veditoperations.cpp

@@ -100,8 +100,3 @@ void VEditOperations::setVimMode(VimMode p_mode)
         m_vim->setMode(p_mode);
     }
 }
-
-void VEditOperations::decorateText(TextDecoration p_decoration)
-{
-    Q_UNUSED(p_decoration);
-}

+ 5 - 1
src/veditoperations.h

@@ -38,7 +38,11 @@ public:
     void requestUpdateVimStatus();
 
     // Insert decoration markers or decorate selected text.
-    virtual void decorateText(TextDecoration p_decoration);
+    virtual void decorateText(TextDecoration p_decoration, int p_level = -1)
+    {
+        Q_UNUSED(p_decoration);
+        Q_UNUSED(p_level);
+    }
 
     // Set Vim mode if not NULL.
     void setVimMode(VimMode p_mode);

+ 2 - 2
src/veditor.cpp

@@ -893,10 +893,10 @@ void VEditor::setInputMethodEnabled(bool p_enabled)
     }
 }
 
-void VEditor::decorateText(TextDecoration p_decoration)
+void VEditor::decorateText(TextDecoration p_decoration, int p_level)
 {
     if (m_editOps) {
-        m_editOps->decorateText(p_decoration);
+        m_editOps->decorateText(p_decoration, p_level);
     }
 }
 

+ 1 - 1
src/veditor.h

@@ -120,7 +120,7 @@ public:
     void setInputMethodEnabled(bool p_enabled);
 
     // Insert decoration markers or decorate selected text.
-    void decorateText(TextDecoration p_decoration);
+    void decorateText(TextDecoration p_decoration, int p_level);
 
     virtual bool isBlockVisible(const QTextBlock &p_block) = 0;
 

+ 5 - 1
src/vedittab.h

@@ -73,7 +73,11 @@ public:
     virtual void requestUpdateVimStatus() = 0;
 
     // Insert decoration markers or decorate selected text.
-    virtual void decorateText(TextDecoration p_decoration) {Q_UNUSED(p_decoration);}
+    virtual void decorateText(TextDecoration p_decoration, int p_level = -1)
+    {
+        Q_UNUSED(p_decoration);
+        Q_UNUSED(p_level);
+    }
 
     // Create a filled VEditTabInfo.
     virtual VEditTabInfo fetchTabInfo() const;

+ 63 - 0
src/vmainwindow.cpp

@@ -424,6 +424,8 @@ void VMainWindow::initEditToolBar(QSize p_iconSize)
 
     m_editToolBar->addAction(m_headingSequenceAct);
 
+    initHeadingButton(m_editToolBar);
+
     QAction *boldAct = new QAction(QIcon(":/resources/icons/bold.svg"),
                                    tr("Bold\t%1").arg(VUtils::getShortcutText("Ctrl+B")),
                                    this);
@@ -1829,6 +1831,8 @@ void VMainWindow::updateActionsStateFromTab(const VEditTab *p_tab)
 
     m_attachmentBtn->setEnabled(file && file->getType() == FileType::Note);
 
+    m_headingBtn->setEnabled(file && editMode);
+
     setActionsEnabled(m_editToolBar, file && editMode);
 
     // Handle heading sequence act independently.
@@ -2712,3 +2716,62 @@ void VMainWindow::openFlashPage()
               OpenFileMode::Edit,
               true);
 }
+
+void VMainWindow::initHeadingButton(QToolBar *p_tb)
+{
+    m_headingBtn = new QPushButton(QIcon(":/resources/icons/heading.svg"),
+                                              "",
+                                              this);
+    m_headingBtn->setToolTip(tr("Headings"));
+    m_headingBtn->setProperty("CornerBtn", true);
+    m_headingBtn->setFocusPolicy(Qt::NoFocus);
+    m_headingBtn->setEnabled(false);
+
+    QMenu *menu = new QMenu(this);
+    QAction *act = menu->addAction(tr("Heading 1\t%1").arg(VUtils::getShortcutText("Ctrl+1")));
+    QFont font = act->font();
+    int ps = font.pointSize();
+    font.setBold(true);
+    font.setPointSize(ps + 8);
+    act->setFont(font);
+    act->setData(1);
+
+    act = menu->addAction(tr("Heading 2\t%1").arg(VUtils::getShortcutText("Ctrl+2")));
+    font.setPointSize(ps + 6);
+    act->setFont(font);
+    act->setData(2);
+
+    act = menu->addAction(tr("Heading 3\t%1").arg(VUtils::getShortcutText("Ctrl+3")));
+    font.setPointSize(ps + 4);
+    act->setFont(font);
+    act->setData(3);
+
+    act = menu->addAction(tr("Heading 4\t%1").arg(VUtils::getShortcutText("Ctrl+4")));
+    font.setPointSize(ps + 2);
+    act->setFont(font);
+    act->setData(4);
+
+    act = menu->addAction(tr("Heading 5\t%1").arg(VUtils::getShortcutText("Ctrl+5")));
+    font.setPointSize(ps + 2);
+    act->setFont(font);
+    act->setData(5);
+
+    act = menu->addAction(tr("Heading 6\t%1").arg(VUtils::getShortcutText("Ctrl+6")));
+    font.setPointSize(ps + 2);
+    act->setFont(font);
+    act->setData(6);
+
+    act = menu->addAction(tr("Clear\t%1").arg(VUtils::getShortcutText("Ctrl+7")));
+    act->setData(0);
+
+    connect(menu, &QMenu::triggered,
+            this, [this](QAction *p_action) {
+                if (m_curTab) {
+                    int level = p_action->data().toInt();
+                    m_curTab->decorateText(TextDecoration::Heading, level);
+                }
+            });
+
+    m_headingBtn->setMenu(menu);
+    p_tb->addWidget(m_headingBtn);
+}

+ 4 - 0
src/vmainwindow.h

@@ -261,6 +261,8 @@ private:
 
     void initShortcuts();
 
+    void initHeadingButton(QToolBar *p_tb);
+
     // Captain mode functions.
 
     // Popup the attachment list if it is enabled.
@@ -372,6 +374,8 @@ private:
     // Attachment list.
     VAttachmentList *m_attachmentList;
 
+    QPushButton *m_headingBtn;
+
     QVector<QPixmap> predefinedColorPixmaps;
 
     // Single instance guard.

+ 7 - 3
src/vmdeditoperations.cpp

@@ -223,7 +223,7 @@ bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event)
     {
         if (modifiers == Qt::ControlModifier) {
             // Ctrl + <N>: insert title at level <N>.
-            if (insertTitle(key == Qt::Key_7 ? 0 : key - Qt::Key_0)) {
+            if (decorateHeading(key == Qt::Key_7 ? 0 : key - Qt::Key_0)) {
                 p_event->accept();
                 ret = true;
                 goto exit;
@@ -649,7 +649,7 @@ void VMdEditOperations::changeListBlockSeqNumber(QTextBlock &p_block, int p_seq)
     cursor.insertText(QString::number(p_seq));
 }
 
-bool VMdEditOperations::insertTitle(int p_level)
+bool VMdEditOperations::decorateHeading(int p_level)
 {
     QTextDocument *doc = m_editor->documentW();
     QTextCursor cursor = m_editor->textCursorW();
@@ -674,7 +674,7 @@ bool VMdEditOperations::insertTitle(int p_level)
     return true;
 }
 
-void VMdEditOperations::decorateText(TextDecoration p_decoration)
+void VMdEditOperations::decorateText(TextDecoration p_decoration, int p_level)
 {
     if (p_decoration == TextDecoration::None) {
         return;
@@ -702,6 +702,10 @@ void VMdEditOperations::decorateText(TextDecoration p_decoration)
         decorateCodeBlock();
         break;
 
+    case TextDecoration::Heading:
+        decorateHeading(p_level);
+        break;
+
     default:
         validDecoration = false;
         qDebug() << "decoration" << (int)p_decoration << "is not implemented yet";

+ 7 - 7
src/vmdeditoperations.h

@@ -30,7 +30,7 @@ public:
 
     // Insert decoration markers or decorate selected text.
     // If it is Vim Normal mode, change to Insert mode first.
-    void decorateText(TextDecoration p_decoration) Q_DECL_OVERRIDE;
+    void decorateText(TextDecoration p_decoration, int p_level = -1) Q_DECL_OVERRIDE;
 
 private:
     // Insert image from @oriImagePath as @path.
@@ -55,12 +55,6 @@ private:
     bool handleKeyReturn(QKeyEvent *p_event);
     bool handleKeyBracketLeft(QKeyEvent *p_event);
 
-    // Insert title of level @p_level.
-    // Will detect if current block already has some leading #s. If yes,
-    // will delete it and insert the correct #s.
-    // @p_level: 0 to cancel title.
-    bool insertTitle(int p_level);
-
     // Change the sequence number of a list block.
     void changeListBlockSeqNumber(QTextBlock &p_block, int p_seq);
 
@@ -79,6 +73,12 @@ private:
     // Insert strikethrough marker or set selected text strikethrough.
     void decorateStrikethrough();
 
+    // Insert title of level @p_level.
+    // Will detect if current block already has some leading #s. If yes,
+    // will delete it and insert the correct #s.
+    // @p_level: 0 to cancel title.
+    bool decorateHeading(int p_level);
+
     // The cursor position after auto indent or auto list.
     // It will be -1 if last key press do not trigger the auto indent or auto list.
     int m_autoIndentPos;

+ 2 - 2
src/vmdtab.cpp

@@ -717,10 +717,10 @@ VEditTabInfo VMdTab::fetchTabInfo() const
     return info;
 }
 
-void VMdTab::decorateText(TextDecoration p_decoration)
+void VMdTab::decorateText(TextDecoration p_decoration, int p_level)
 {
     if (m_editor) {
-        m_editor->decorateText(p_decoration);
+        m_editor->decorateText(p_decoration, p_level);
     }
 }
 

+ 1 - 1
src/vmdtab.h

@@ -66,7 +66,7 @@ public:
     void requestUpdateVimStatus() Q_DECL_OVERRIDE;
 
     // Insert decoration markers or decorate selected text.
-    void decorateText(TextDecoration p_decoration) Q_DECL_OVERRIDE;
+    void decorateText(TextDecoration p_decoration, int p_level = -1) Q_DECL_OVERRIDE;
 
     // Create a filled VEditTabInfo.
     VEditTabInfo fetchTabInfo() const Q_DECL_OVERRIDE;

+ 1 - 0
src/vnote.qrc

@@ -143,5 +143,6 @@
         <file>resources/icons/apply_snippet.svg</file>
         <file>resources/icons/reading_modified.svg</file>
         <file>resources/icons/flash_page.svg</file>
+        <file>resources/icons/heading.svg</file>
     </qresource>
 </RCC>