diff --git a/src/veditoperations.h b/src/veditoperations.h index e7034a7a..f78f6dd8 100644 --- a/src/veditoperations.h +++ b/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); diff --git a/src/vmdedit.cpp b/src/vmdedit.cpp index 09246e22..74c87973 100644 --- a/src/vmdedit.cpp +++ b/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 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(); +} diff --git a/src/vmdedit.h b/src/vmdedit.h index af44221d..16e2e288 100644 --- a/src/vmdedit.h +++ b/src/vmdedit.h @@ -4,7 +4,9 @@ #include "vedit.h" #include #include +#include #include "vtoc.h" +#include "veditoperations.h" class HGMarkdownHighlighter; @@ -37,6 +39,7 @@ private slots: // Update block list containing image links. void updateImageBlocks(QSet p_imageBlocks); void highlightCurrentLine(); + void handleEditStateChanged(KeyState p_state); protected: void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; @@ -71,6 +74,10 @@ private: QVector m_insertedImages; QVector m_initImages; QVector m_headers; + QColor m_cursorLineColor; + + static const QString c_cursorLineColor; + static const QString c_cursorLineColorVim; }; #endif // VMDEDIT_H diff --git a/src/vmdeditoperations.cpp b/src/vmdeditoperations.cpp index 6c110fe7..5da3ad09 100644 --- a/src/vmdeditoperations.cpp +++ b/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 &p_seq) return true; } +void VMdEditOperations::setKeyState(KeyState p_state) +{ + if (m_keyState == p_state) { + return; + } + m_keyState = p_state; + emit keyStateChanged(m_keyState); +} + diff --git a/src/vmdeditoperations.h b/src/vmdeditoperations.h index 97d407f8..1acc96ee 100644 --- a/src/vmdeditoperations.h +++ b/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);