Browse Source

highlight curren cursor line

Add configuration "highlight_cursor_line" in vnote.ini.

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
3b442f55a9
7 changed files with 67 additions and 0 deletions
  1. 1 0
      src/resources/vnote.ini
  2. 1 0
      src/vconfigmanager.cpp
  3. 20 0
      src/vconfigmanager.h
  4. 20 0
      src/vmainwindow.cpp
  5. 2 0
      src/vmainwindow.h
  6. 22 0
      src/vmdedit.cpp
  7. 1 0
      src/vmdedit.h

+ 1 - 0
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
 

+ 1 - 0
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();

+ 20 - 0
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<VColor> &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<VColor> 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<VColor>& VConfigManager::getPredefinedColors() const
 {
     return predefinedColors;

+ 20 - 0
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) {

+ 2 - 0
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

+ 22 - 0
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<QTextEdit::ExtraSelection> 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);
+}

+ 1 - 0
src/vmdedit.h

@@ -36,6 +36,7 @@ private slots:
     void updateCurHeader();
     // Update block list containing image links.
     void updateImageBlocks(QSet<int> p_imageBlocks);
+    void highlightCurrentLine();
 
 protected:
     void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;