Skip to content

Commit e81665b

Browse files
author
Pierre Raybaut
committed
Various optimizations around QwtTextEngine and QwtText
1 parent 025bd52 commit e81665b

File tree

2 files changed

+68
-61
lines changed

2 files changed

+68
-61
lines changed

qwt/qwt_text.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,6 @@
1010
import math
1111

1212

13-
class QwtTextEngineDict(object):
14-
def __init__(self):
15-
self.__map = {QwtText.PlainText: QwtPlainTextEngine(),
16-
QwtText.RichText: QwtRichTextEngine()}
17-
18-
def textEngine(self, *args):
19-
if len(args) == 1:
20-
format_ = args[0]
21-
return self.__map.get(format_)
22-
elif len(args) == 2:
23-
text, format_ = args
24-
25-
if format_ == QwtText.AutoText:
26-
for key, engine in list(self.__map.iteritems()):
27-
if key != QwtText.PlainText:
28-
if engine and engine.mightRender(text):
29-
return engine
30-
31-
engine = self.__map.get(format_)
32-
if engine is not None:
33-
return engine
34-
35-
engine = self.__map[QwtText.PlainText]
36-
return engine
37-
else:
38-
raise TypeError("%s().textEngine() takes 1 or 2 argument(s) (%s "\
39-
"given)" % (self.__class__.__name__, len(args)))
40-
41-
def setTextEngine(self, format_, engine):
42-
if format_ == QwtText.AutoText:
43-
return
44-
if format_ == QwtText.PlainText and engine is None:
45-
return
46-
self.__map.setdefault(format_, engine)
4713

4814

4915
class QwtText_PrivateData(object):
@@ -286,6 +252,43 @@ def setTextEngine(self, format_, engine):
286252
self._dict.setTextEngine(format_, engine)
287253

288254

255+
class QwtTextEngineDict(object):
256+
# Optimization: a single text engine for all QwtText objects
257+
# (this is not how it's implemented in Qwt6 C++ library)
258+
__map = {QwtText.PlainText: QwtPlainTextEngine(),
259+
QwtText.RichText: QwtRichTextEngine()}
260+
261+
def textEngine(self, *args):
262+
if len(args) == 1:
263+
format_ = args[0]
264+
return self.__map.get(format_)
265+
elif len(args) == 2:
266+
text, format_ = args
267+
268+
if format_ == QwtText.AutoText:
269+
for key, engine in list(self.__map.iteritems()):
270+
if key != QwtText.PlainText:
271+
if engine and engine.mightRender(text):
272+
return engine
273+
274+
engine = self.__map.get(format_)
275+
if engine is not None:
276+
return engine
277+
278+
engine = self.__map[QwtText.PlainText]
279+
return engine
280+
else:
281+
raise TypeError("%s().textEngine() takes 1 or 2 argument(s) (%s "\
282+
"given)" % (self.__class__.__name__, len(args)))
283+
284+
def setTextEngine(self, format_, engine):
285+
if format_ == QwtText.AutoText:
286+
return
287+
if format_ == QwtText.PlainText and engine is None:
288+
return
289+
self.__map.setdefault(format_, engine)
290+
291+
289292
class QwtTextLabel_PrivateData(object):
290293
def __init__(self):
291294
self.indent = 4

qwt/qwt_text_engine.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,36 @@ def __init__(self, text, flags, font):
5050
self.adjustSize();
5151

5252

53-
ASCENTCACHE = {}
53+
class QwtTextEngine(object):
54+
def __init__(self):
55+
pass
56+
5457

55-
class QwtPlainTextEngine_PrivateData(object):
58+
ASCENTCACHE = {}
5659

60+
class QwtPlainTextEngine(QwtTextEngine):
61+
def __init__(self):
62+
self.qrectf_max = QRectF(0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
63+
self._fm_cache = {}
64+
65+
def fontmetrics(self, font):
66+
fm = self._fm_cache.get(font)
67+
if fm is None:
68+
return self._fm_cache.setdefault(font, QFontMetricsF(font))
69+
else:
70+
return fm
71+
72+
def heightForWidth(self, font, flags, text, width):
73+
fm = self.fontmetrics(font)
74+
rect = fm.boundingRect(QRectF(0, 0, width, QWIDGETSIZE_MAX),
75+
flags, text)
76+
return rect.height()
77+
78+
def textSize(self, font, flags, text):
79+
fm = self.fontmetrics(font)
80+
rect = fm.boundingRect(self.qrectf_max, flags, text)
81+
return rect.size()
82+
5783
def effectiveAscent(self, font):
5884
global ASCENTCACHE
5985
fontKey = font.key()
@@ -66,7 +92,7 @@ def findAscent(self, font):
6692
dummy = "E"
6793
white = QColor(Qt.white)
6894

69-
fm = QFontMetrics(font)
95+
fm = self.fontmetrics(font)
7096
pm = QPixmap(fm.width(dummy), fm.height())
7197
pm.fill(white)
7298

@@ -87,32 +113,10 @@ def findAscent(self, font):
87113
return fm.ascent()-row+1
88114
return fm.ascent()
89115

90-
91-
class QwtTextEngine(object):
92-
def __init__(self):
93-
pass
94-
95-
96-
class QwtPlainTextEngine(QwtTextEngine):
97-
def __init__(self):
98-
self.__data = QwtPlainTextEngine_PrivateData()
99-
100-
def heightForWidth(self, font, flags, text, width):
101-
fm = QFontMetricsF(font)
102-
rect = fm.boundingRect(QRectF(0, 0, width, QWIDGETSIZE_MAX),
103-
flags, text)
104-
return rect.height()
105-
106-
def textSize(self, font, flags, text):
107-
fm = QFontMetricsF(font)
108-
rect = fm.boundingRect(QRectF(0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX),
109-
flags, text)
110-
return rect.size()
111-
112116
def textMargins(self, font):
113117
left = right = top = 0
114-
fm = QFontMetricsF(font)
115-
top = fm.ascent() - self.__data.effectiveAscent(font)
118+
fm = self.fontmetrics(font)
119+
top = fm.ascent() - self.effectiveAscent(font)
116120
bottom = fm.descent()
117121
return left, right, top, bottom
118122

0 commit comments

Comments
 (0)