diff --git a/src/calibre/gui2/tweak_book/editor/__init__.py b/src/calibre/gui2/tweak_book/editor/__init__.py index caf01372b9..76a59f6a67 100644 --- a/src/calibre/gui2/tweak_book/editor/__init__.py +++ b/src/calibre/gui2/tweak_book/editor/__init__.py @@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' +from PyQt4.Qt import QTextCharFormat + from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES from calibre.ebooks.oeb.polish.container import guess_type @@ -25,3 +27,11 @@ def editor_from_syntax(syntax, parent=None): from calibre.gui2.tweak_book.editor.widget import Editor return Editor(syntax, parent=parent) +SYNTAX_PROPERTY = QTextCharFormat.UserProperty + +class SyntaxTextCharFormat(QTextCharFormat): + + def __init__(self, *args, **kwargs): + QTextCharFormat.__init__(self, *args, **kwargs) + self.setProperty(SYNTAX_PROPERTY, True) + diff --git a/src/calibre/gui2/tweak_book/editor/syntax/css.py b/src/calibre/gui2/tweak_book/editor/syntax/css.py index 5de019d172..4addf3ca3c 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/css.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/css.py @@ -8,10 +8,9 @@ __copyright__ = '2013, Kovid Goyal ' import re +from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter -from PyQt4.Qt import QTextCharFormat - space_pat = re.compile(r'[ \n\t\r\f]+') cdo_pat = re.compile(r'/\*') sheet_tokens = [(re.compile(k), v, n) for k, v, n in [ @@ -242,7 +241,7 @@ def create_formats(highlighter): 'unknown-normal': _('Invalid text'), 'unterminated-string': _('Unterminated string'), }.iteritems(): - f = formats[name] = QTextCharFormat(formats['error']) + f = formats[name] = SyntaxTextCharFormat(formats['error']) f.setToolTip(msg) return formats diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.py b/src/calibre/gui2/tweak_book/editor/syntax/html.py index 94479e4a5b..fd7e238de7 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/html.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/html.py @@ -9,8 +9,9 @@ __copyright__ = '2013, Kovid Goyal ' import re from functools import partial -from PyQt4.Qt import (QTextCharFormat, QFont) +from PyQt4.Qt import QFont +from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter, run_loop from calibre.gui2.tweak_book.editor.syntax.css import create_formats as create_css_formats, state_map as css_state_map, State as CSSState @@ -109,7 +110,7 @@ def mark_nbsp(state, text, nbsp_format): ans = [] fmt = None if state.bold or state.italic: - fmt = QTextCharFormat() + fmt = SyntaxTextCharFormat() if state.bold: fmt.setFontWeight(QFont.Bold) if state.italic: @@ -313,9 +314,9 @@ def create_formats(highlighter): 'bad-closing': _('A closing tag must contain only the tag name and nothing else'), 'no-attr-value': _('Expecting an attribute value'), }.iteritems(): - f = formats[name] = QTextCharFormat(formats['error']) + f = formats[name] = SyntaxTextCharFormat(formats['error']) f.setToolTip(msg) - f = formats['title'] = QTextCharFormat() + f = formats['title'] = SyntaxTextCharFormat() f.setFontWeight(QFont.Bold) return formats diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index afd3e8c6dc..cbd5f2115d 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -14,6 +14,7 @@ from PyQt4.Qt import ( QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect) from calibre.gui2.tweak_book import tprefs +from calibre.gui2.tweak_book.editor import SYNTAX_PROPERTY from calibre.gui2.tweak_book.editor.themes import THEMES, default_theme, theme_color from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter, XMLHighlighter @@ -211,7 +212,7 @@ class TextEdit(QPlainTextEdit): return pos = cursor.positionInBlock() for r in cursor.block().layout().additionalFormats(): - if r.start <= pos < r.start + r.length: + if r.start <= pos < r.start + r.length and r.format.property(SYNTAX_PROPERTY).toBool(): return r.format def show_tooltip(self, ev): diff --git a/src/calibre/gui2/tweak_book/editor/themes.py b/src/calibre/gui2/tweak_book/editor/themes.py index ed7cb3b596..ac94e9b134 100644 --- a/src/calibre/gui2/tweak_book/editor/themes.py +++ b/src/calibre/gui2/tweak_book/editor/themes.py @@ -8,7 +8,9 @@ __copyright__ = '2013, Kovid Goyal ' from collections import namedtuple -from PyQt4.Qt import (QColor, QTextCharFormat, QBrush, QFont, QApplication, QPalette) +from PyQt4.Qt import (QColor, QBrush, QFont, QApplication, QPalette) + +from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat underline_styles = {'single', 'dash', 'dot', 'dash_dot', 'dash_dot_dot', 'wave', 'spell'} @@ -198,10 +200,10 @@ def u(x): if 'Dot' in x: return x + 'Line' return x + 'Underline' -underline_styles = {x:getattr(QTextCharFormat, u(x)) for x in underline_styles} +underline_styles = {x:getattr(SyntaxTextCharFormat, u(x)) for x in underline_styles} def highlight_to_char_format(h): - ans = QTextCharFormat() + ans = SyntaxTextCharFormat() if h.bold: ans.setFontWeight(QFont.Bold) if h.italic: