Highlight cursor line

This commit is contained in:
Kovid Goyal 2013-10-30 08:29:53 +05:30
parent a1a89053f3
commit aa9e04aeb0

View File

@ -9,7 +9,8 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import textwrap import textwrap
from PyQt4.Qt import ( from PyQt4.Qt import (
QPlainTextEdit, QApplication, QFontDatabase, QToolTip, QPalette, QFont) QPlainTextEdit, QApplication, QFontDatabase, QToolTip, QPalette, QFont,
QTextEdit, QTextFormat)
from calibre.gui2.tweak_book import tprefs from calibre.gui2.tweak_book import tprefs
from calibre.gui2.tweak_book.editor.themes import THEMES, DEFAULT_THEME, theme_color from calibre.gui2.tweak_book.editor.themes import THEMES, DEFAULT_THEME, theme_color
@ -36,6 +37,7 @@ class TextEdit(QPlainTextEdit):
self.highlighter = SyntaxHighlighter(self) self.highlighter = SyntaxHighlighter(self)
self.apply_theme() self.apply_theme()
self.setMouseTracking(True) self.setMouseTracking(True)
self.cursorPositionChanged.connect(self.highlight_cursor_line)
def apply_theme(self): def apply_theme(self):
theme = THEMES.get(tprefs['editor_theme'], None) theme = THEMES.get(tprefs['editor_theme'], None)
@ -44,6 +46,7 @@ class TextEdit(QPlainTextEdit):
self.theme = theme self.theme = theme
pal = self.palette() pal = self.palette()
pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg')) pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg'))
pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg'))
pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg')) pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg'))
pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg')) pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg'))
pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg')) pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg'))
@ -68,6 +71,14 @@ class TextEdit(QPlainTextEdit):
self.highlighter.setDocument(self.document()) self.highlighter.setDocument(self.document())
self.setPlainText(text) self.setPlainText(text)
def highlight_cursor_line(self):
sel = QTextEdit.ExtraSelection()
sel.format.setBackground(self.palette().alternateBase())
sel.format.setProperty(QTextFormat.FullWidthSelection, True)
sel.cursor = self.textCursor()
sel.cursor.clearSelection()
self.setExtraSelections([sel])
def event(self, ev): def event(self, ev):
if ev.type() == ev.ToolTip: if ev.type() == ev.ToolTip:
self.show_tooltip(ev) self.show_tooltip(ev)