From 3b442f55a9463fbfc28010d7f642a9ab92650373 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 25 Dec 2016 20:10:29 +0800 Subject: [PATCH] highlight curren cursor line Add configuration "highlight_cursor_line" in vnote.ini. Signed-off-by: Le Tan --- src/resources/vnote.ini | 1 + src/vconfigmanager.cpp | 1 + src/vconfigmanager.h | 20 ++++++++++++++++++++ src/vmainwindow.cpp | 20 ++++++++++++++++++++ src/vmainwindow.h | 2 ++ src/vmdedit.cpp | 22 ++++++++++++++++++++++ src/vmdedit.h | 1 + 7 files changed, 67 insertions(+) diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index e32656e6..88074b8d 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -7,6 +7,7 @@ template_css_url=qrc:/resources/markdown.css current_notebook=0 tab_stop_width=4 is_expand_tab=1 +highlight_cursor_line=1 current_background_color=System current_render_background_color=System diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index b5abc43e..1627c100 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -52,6 +52,7 @@ void VConfigManager::initialize() tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt(); isExpandTab = getConfigFromSettings("global", "is_expand_tab").toBool(); + m_highlightCursorLine = getConfigFromSettings("global", "highlight_cursor_line").toBool(); readPredefinedColorsFromSettings(); curBackgroundColor = getConfigFromSettings("global", "current_background_color").toString(); diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 70224df0..05cb24b5 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -76,6 +76,9 @@ public: inline bool getIsExpandTab() const; inline void setIsExpandTab(bool isExpandTab); + inline bool getHighlightCursorLine() const; + inline void setHighlightCursorLine(bool p_cursorLine); + inline const QVector &getPredefinedColors() const; inline const QString &getCurBackgroundColor() const; @@ -124,6 +127,9 @@ private: // Expand tab to @tabStopWidth spaces bool isExpandTab; + // Highlight current cursor line. + bool m_highlightCursorLine; + // App defined color QVector predefinedColors; QString curBackgroundColor; @@ -266,6 +272,20 @@ inline void VConfigManager::setIsExpandTab(bool isExpandTab) setConfigToSettings("global", "is_expand_tab", this->isExpandTab); } +inline bool VConfigManager::getHighlightCursorLine() const +{ + return m_highlightCursorLine; +} + +inline void VConfigManager::setHighlightCursorLine(bool p_cursorLine) +{ + if (p_cursorLine == m_highlightCursorLine) { + return; + } + m_highlightCursorLine = p_cursorLine; + setConfigToSettings("global", "highlight_cursor_line", m_highlightCursorLine); +} + inline const QVector& VConfigManager::getPredefinedColors() const { return predefinedColors; diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 256de2ab..9e3dbdcf 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -13,12 +13,15 @@ extern VConfigManager vconfig; +VNote *g_vnote; + VMainWindow::VMainWindow(QWidget *parent) : QMainWindow(parent) { setWindowIcon(QIcon(":/resources/icons/vnote.ico")); // Must be called before those who uses VConfigManager vnote = new VNote(this); + g_vnote = vnote; vnote->initPalette(palette()); initPredefinedColorPixmaps(); setupUI(); @@ -210,6 +213,12 @@ void VMainWindow::initActions() connect(backgroundColorAct, &QActionGroup::triggered, this, &VMainWindow::setEditorBackgroundColor); + m_cursorLineAct = new QAction(tr("Highlight Cursor Line"), this); + m_cursorLineAct->setStatusTip(tr("Highlight current cursor line")); + m_cursorLineAct->setCheckable(true); + connect(m_cursorLineAct, &QAction::triggered, + this, &VMainWindow::changeHighlightCursorLine); + renderBackgroundAct = new QActionGroup(this); connect(renderBackgroundAct, &QActionGroup::triggered, this, &VMainWindow::setRenderBackgroundColor); @@ -348,6 +357,12 @@ void VMainWindow::initMenuBar() qWarning() << "unsupported tab stop width" << tabStopWidth << "in config"; } initEditorBackgroundMenu(editMenu); + editMenu->addAction(m_cursorLineAct); + if (vconfig.getHighlightCursorLine()) { + m_cursorLineAct->setChecked(true); + } else { + m_cursorLineAct->setChecked(false); + } // Markdown Menu QMenu *converterMenu = markdownMenu->addMenu(tr("&Converter")); @@ -446,6 +461,11 @@ void VMainWindow::changeExpandTab(bool checked) vconfig.setIsExpandTab(checked); } +void VMainWindow::changeHighlightCursorLine(bool p_checked) +{ + vconfig.setHighlightCursorLine(p_checked); +} + void VMainWindow::setTabStopWidth(QAction *action) { if (!action) { diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 628c6fbc..06e9df22 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -45,6 +45,7 @@ private slots: void setTabStopWidth(QAction *action); void setEditorBackgroundColor(QAction *action); void setRenderBackgroundColor(QAction *action); + void changeHighlightCursorLine(bool p_checked); void handleCurTabStatusChanged(const VFile *p_file, const VEditTab *p_editTab, bool p_editMode); void onePanelView(); void twoPanelView(); @@ -122,6 +123,7 @@ private: QAction *fourSpaceTabAct; QAction *eightSpaceTabAct; QActionGroup *backgroundColorAct; + QAction *m_cursorLineAct; QActionGroup *renderBackgroundAct; // Menus diff --git a/src/vmdedit.cpp b/src/vmdedit.cpp index e84e005d..09246e22 100644 --- a/src/vmdedit.cpp +++ b/src/vmdedit.cpp @@ -8,6 +8,7 @@ #include "utils/vutils.h" extern VConfigManager vconfig; +extern VNote *g_vnote; enum ImageProperty { ImagePath = 1 }; @@ -27,6 +28,10 @@ VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent) connect(this, &VMdEdit::cursorPositionChanged, this, &VMdEdit::updateCurHeader); + if (vconfig.getHighlightCursorLine()) { + connect(this, &VMdEdit::cursorPositionChanged, + this, &VMdEdit::highlightCurrentLine); + } m_editOps->updateTabSettings(); updateFontAndPalette(); @@ -452,3 +457,20 @@ int VMdEdit::removeObjectReplacementLine(QString &p_text, int p_index) const p_text.remove(prevLineIdx + 1, p_index - prevLineIdx + 1); return prevLineIdx; } + +void VMdEdit::highlightCurrentLine() +{ + static QColor lineColor = QColor(g_vnote->getColorFromPalette("Indigo1")); + QList extraSelects; + + if (!isReadOnly()) { + QTextEdit::ExtraSelection select; + + select.format.setBackground(lineColor); + select.format.setProperty(QTextFormat::FullWidthSelection, true); + select.cursor = textCursor(); + select.cursor.clearSelection(); + extraSelects.append(select); + } + setExtraSelections(extraSelects); +} diff --git a/src/vmdedit.h b/src/vmdedit.h index 9109546a..af44221d 100644 --- a/src/vmdedit.h +++ b/src/vmdedit.h @@ -36,6 +36,7 @@ private slots: void updateCurHeader(); // Update block list containing image links. void updateImageBlocks(QSet p_imageBlocks); + void highlightCurrentLine(); protected: void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;