Ensure that syntax char formats are unambiguously identifiable

This commit is contained in:
Kovid Goyal 2013-11-11 17:01:18 +05:30
parent 4f4dd87b2a
commit 76d22f8f68
5 changed files with 24 additions and 11 deletions

View File

@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
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)

View File

@ -8,10 +8,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
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

View File

@ -9,8 +9,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
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

View File

@ -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):

View File

@ -8,7 +8,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
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: