Browse Source

change cursorline color in Vim mode

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
059e8dacd2
5 changed files with 44 additions and 7 deletions
  1. 3 0
      src/veditoperations.h
  2. 19 2
      src/vmdedit.cpp
  3. 7 0
      src/vmdedit.h
  4. 14 5
      src/vmdeditoperations.cpp
  5. 1 0
      src/vmdeditoperations.h

+ 3 - 0
src/veditoperations.h

@@ -27,6 +27,9 @@ public:
     virtual bool handleKeyPressEvent(QKeyEvent *p_event) = 0;
     void updateTabSettings();
 
+signals:
+    void keyStateChanged(KeyState p_state);
+
 protected:
     void insertTextAtCurPos(const QString &p_text);
 

+ 19 - 2
src/vmdedit.cpp

@@ -12,11 +12,16 @@ extern VNote *g_vnote;
 
 enum ImageProperty { ImagePath = 1 };
 
+const QString VMdEdit::c_cursorLineColor = "Indigo1";
+const QString VMdEdit::c_cursorLineColorVim = "Green2";
+
 VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
     : VEdit(p_file, p_parent), m_mdHighlighter(NULL)
 {
     Q_ASSERT(p_file->getDocType() == DocType::Markdown);
 
+    m_cursorLineColor = QColor(g_vnote->getColorFromPalette(c_cursorLineColor));
+
     setAcceptRichText(false);
     m_mdHighlighter = new HGMarkdownHighlighter(vconfig.getMdHighlightingStyles(),
                                                 500, document());
@@ -25,6 +30,8 @@ VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
     connect(m_mdHighlighter, &HGMarkdownHighlighter::imageBlocksUpdated,
             this, &VMdEdit::updateImageBlocks);
     m_editOps = new VMdEditOperations(this, m_file);
+    connect(m_editOps, &VEditOperations::keyStateChanged,
+            this, &VMdEdit::handleEditStateChanged);
 
     connect(this, &VMdEdit::cursorPositionChanged,
             this, &VMdEdit::updateCurHeader);
@@ -460,13 +467,12 @@ int VMdEdit::removeObjectReplacementLine(QString &p_text, int p_index) const
 
 void VMdEdit::highlightCurrentLine()
 {
-    static QColor lineColor = QColor(g_vnote->getColorFromPalette("Indigo1"));
     QList<QTextEdit::ExtraSelection> extraSelects;
 
     if (!isReadOnly()) {
         QTextEdit::ExtraSelection select;
 
-        select.format.setBackground(lineColor);
+        select.format.setBackground(m_cursorLineColor);
         select.format.setProperty(QTextFormat::FullWidthSelection, true);
         select.cursor = textCursor();
         select.cursor.clearSelection();
@@ -474,3 +480,14 @@ void VMdEdit::highlightCurrentLine()
     }
     setExtraSelections(extraSelects);
 }
+
+void VMdEdit::handleEditStateChanged(KeyState p_state)
+{
+    qDebug() << "edit state" << (int)p_state;
+    if (p_state == KeyState::Normal) {
+        m_cursorLineColor = QColor(g_vnote->getColorFromPalette(c_cursorLineColor));
+    } else {
+        m_cursorLineColor = QColor(g_vnote->getColorFromPalette(c_cursorLineColorVim));
+    }
+    highlightCurrentLine();
+}

+ 7 - 0
src/vmdedit.h

@@ -4,7 +4,9 @@
 #include "vedit.h"
 #include <QVector>
 #include <QString>
+#include <QColor>
 #include "vtoc.h"
+#include "veditoperations.h"
 
 class HGMarkdownHighlighter;
 
@@ -37,6 +39,7 @@ private slots:
     // Update block list containing image links.
     void updateImageBlocks(QSet<int> p_imageBlocks);
     void highlightCurrentLine();
+    void handleEditStateChanged(KeyState p_state);
 
 protected:
     void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
@@ -71,6 +74,10 @@ private:
     QVector<QString> m_insertedImages;
     QVector<QString> m_initImages;
     QVector<VHeader> m_headers;
+    QColor m_cursorLineColor;
+
+    static const QString c_cursorLineColor;
+    static const QString c_cursorLineColorVim;
 };
 
 #endif // VMDEDIT_H

+ 14 - 5
src/vmdeditoperations.cpp

@@ -414,7 +414,7 @@ bool VMdEditOperations::handleKeyD(QKeyEvent *p_event)
     if (p_event->modifiers() == Qt::ControlModifier) {
         // Ctrl+D, enter Vim-pending mode.
         // Will accept the key stroke in m_pendingTime as Vim normal command.
-        m_keyState = KeyState::Vim;
+        setKeyState(KeyState::Vim);
         m_pendingTimer->stop();
         m_pendingTimer->start();
         p_event->accept();
@@ -526,7 +526,7 @@ bool VMdEditOperations::handleKeyEsc(QKeyEvent *p_event)
     m_editor->setTextCursor(cursor);
 
     m_pendingTimer->stop();
-    m_keyState = KeyState::Normal;
+    setKeyState(KeyState::Normal);
     m_pendingKey.clear();
 
     p_event->accept();
@@ -804,7 +804,7 @@ bool VMdEditOperations::handleKeyPressVim(QKeyEvent *p_event)
         if (modifiers == Qt::NoModifier) {
             // V to enter visual mode.
             if (m_pendingKey.isEmpty() && m_keyState != KeyState::VimVisual) {
-                m_keyState = KeyState::VimVisual;
+                setKeyState(KeyState::VimVisual);
                 goto pending;
             }
         }
@@ -863,7 +863,7 @@ bool VMdEditOperations::handleKeyPressVim(QKeyEvent *p_event)
         cursor.clearSelection();
         m_editor->setTextCursor(cursor);
     }
-    m_keyState = KeyState::Normal;
+    setKeyState(KeyState::Normal);
     m_pendingKey.clear();
     p_event->accept();
     return true;
@@ -900,7 +900,7 @@ void VMdEditOperations::pendingTimerTimeout()
         m_pendingTimer->start();
         return;
     }
-    m_keyState = KeyState::Normal;
+    setKeyState(KeyState::Normal);
     m_pendingKey.clear();
 }
 
@@ -917,3 +917,12 @@ bool VMdEditOperations::suffixNumAllowed(const QList<QString> &p_seq)
     return true;
 }
 
+void VMdEditOperations::setKeyState(KeyState p_state)
+{
+    if (m_keyState == p_state) {
+        return;
+    }
+    m_keyState = p_state;
+    emit keyStateChanged(m_keyState);
+}
+

+ 1 - 0
src/vmdeditoperations.h

@@ -27,6 +27,7 @@ private:
     bool insertImageFromURL(const QUrl &imageUrl);
     void insertImageFromPath(const QString &title, const QString &path, const QString &oriImagePath);
     void insertImageFromQImage(const QString &title, const QString &path, const QImage &image);
+    void setKeyState(KeyState p_state);
 
     // Key press handlers.
     bool handleKeyTab(QKeyEvent *p_event);