A small speedup by using QTextCharFormat instead of SyntaxCharFormat when cloning formats

This commit is contained in:
Kovid Goyal 2014-06-23 15:33:14 +05:30
parent 2960c06867
commit cf3c80598e
4 changed files with 18 additions and 24 deletions

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from PyQt4.Qt import QTextCharFormat, QFont
from PyQt4.Qt import QTextCharFormat
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES
from calibre.ebooks.oeb.polish.container import guess_type
@ -37,16 +37,10 @@ SYNTAX_PROPERTY = QTextCharFormat.UserProperty
SPELL_PROPERTY = SYNTAX_PROPERTY + 1
SPELL_LOCALE_PROPERTY = SPELL_PROPERTY + 1
class SyntaxTextCharFormat(QTextCharFormat):
def __init__(self, *args):
QTextCharFormat.__init__(self, *args)
self.setProperty(SYNTAX_PROPERTY, True)
def __repr__(self):
return 'SyntaxFormat(id=%s, color=%s, italic=%s, bold=%s)' % (
id(self), self.foreground().color().name(), self.fontItalic(), self.fontWeight() >= QFont.DemiBold)
__str__ = __repr__
def syntax_text_char_format(*args):
ans = QTextCharFormat(*args)
ans.setProperty(SYNTAX_PROPERTY, True)
return ans
class StoreLocale(object):

View File

@ -10,7 +10,7 @@ import re
from PyQt4.Qt import QTextBlockUserData
from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat
from calibre.gui2.tweak_book.editor import syntax_text_char_format
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
space_pat = re.compile(r'[ \n\t\r\f]+')
@ -267,7 +267,7 @@ def create_formats(highlighter):
'unknown-normal': _('Invalid text'),
'unterminated-string': _('Unterminated string'),
}.iteritems():
f = formats[name] = SyntaxTextCharFormat(formats['error'])
f = formats[name] = syntax_text_char_format(formats['error'])
f.setToolTip(msg)
return formats

View File

@ -10,13 +10,13 @@ import re
from functools import partial
from collections import namedtuple
from PyQt4.Qt import QFont, QTextBlockUserData
from PyQt4.Qt import QFont, QTextBlockUserData, QTextCharFormat
from calibre.ebooks.oeb.polish.spell import html_spell_tags, xml_spell_tags
from calibre.spell.dictionary import parse_lang_code
from calibre.spell.break_iterator import split_into_words_and_positions
from calibre.gui2.tweak_book import dictionaries, tprefs
from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat, SPELL_PROPERTY, SPELL_LOCALE_PROPERTY, store_locale
from calibre.gui2.tweak_book.editor import syntax_text_char_format, SPELL_PROPERTY, SPELL_LOCALE_PROPERTY, store_locale
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, CSSState, CSSUserData)
@ -236,7 +236,7 @@ def check_spelling(text, tpos, tlen, fmt, locale, sfmt):
split_ans.append((length, fmt))
else:
if store_locale.enabled:
s = SyntaxTextCharFormat(sfmt)
s = QTextCharFormat(sfmt)
s.setProperty(SPELL_LOCALE_PROPERTY, locale)
split_ans.append((length, s))
else:
@ -249,7 +249,7 @@ def process_text(state, text, nbsp_format, spell_format, user_data):
ans = []
fmt = None
if state.is_bold or state.is_italic:
fmt = SyntaxTextCharFormat()
fmt = syntax_text_char_format()
if state.is_bold:
fmt.setFontWeight(QFont.Bold)
if state.is_italic:
@ -266,7 +266,7 @@ def process_text(state, text, nbsp_format, spell_format, user_data):
if tprefs['inline_spell_check'] and state.tags and user_data.tag_ok_for_spell(state.tags[-1].name) and hasattr(dictionaries, 'active_user_dictionaries'):
split_ans = []
locale = state.current_lang or dictionaries.default_locale
sfmt = SyntaxTextCharFormat(spell_format)
sfmt = QTextCharFormat(spell_format)
if fmt is not None:
sfmt.merge(fmt)
@ -483,9 +483,9 @@ def create_formats(highlighter, add_css=True):
'no-attr-value': _('Expecting an attribute value'),
'only-prefix': _('A tag name cannot end with a colon'),
}.iteritems():
f = formats[name] = SyntaxTextCharFormat(formats['error'])
f = formats[name] = syntax_text_char_format(formats['error'])
f.setToolTip(msg)
f = formats['title'] = SyntaxTextCharFormat()
f = formats['title'] = syntax_text_char_format()
f.setFontWeight(QFont.Bold)
if add_css:
formats['css_sub_formats'] = create_css_formats(highlighter)

View File

@ -12,11 +12,11 @@ from PyQt4.Qt import (
QColor, QBrush, QFont, QApplication, QPalette, QComboBox,
QPushButton, QIcon, QFormLayout, QLineEdit, QWidget, QScrollArea,
QVBoxLayout, Qt, QHBoxLayout, pyqtSignal, QPixmap, QColorDialog,
QToolButton, QCheckBox, QSize, QLabel, QSplitter)
QToolButton, QCheckBox, QSize, QLabel, QSplitter, QTextCharFormat)
from calibre.gui2 import error_dialog
from calibre.gui2.tweak_book import tprefs
from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat
from calibre.gui2.tweak_book.editor import syntax_text_char_format
from calibre.gui2.tweak_book.widgets import Dialog
underline_styles = {'single', 'dash', 'dot', 'dash_dot', 'dash_dot_dot', 'wave', 'spell'}
@ -233,7 +233,7 @@ def u(x):
if 'Dot' in x:
return x + 'Line'
return x + 'Underline'
underline_styles = {x:getattr(SyntaxTextCharFormat, u(x)) for x in underline_styles}
underline_styles = {x:getattr(QTextCharFormat, u(x)) for x in underline_styles}
def to_highlight(data):
data = data.copy()
@ -258,7 +258,7 @@ def get_theme(name):
return read_custom_theme(ans)
def highlight_to_char_format(h):
ans = SyntaxTextCharFormat()
ans = syntax_text_char_format()
if h.bold:
ans.setFontWeight(QFont.Bold)
if h.italic: