Skip to content

Commit 3cc5755

Browse files
committed
Optimize QwtAbstractScaleDraw.tickLabel method for 30% performance improvement
1 parent 96d3480 commit 3cc5755

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Version 0.12.0
44

5+
- 30% performance improvement (measured by `qwt.tests.test_loadtest`) by optimizing
6+
the `QwtAbstractScaleDraw.tickLabel` method:
7+
- Suppressed an unnecessary call to `QFont.textSize` (which can be quite slow)
8+
- Cached the text size with the label `QwtText` object
59
- Added support for margins in `QwtPlot` (see Issue #82):
610
- Default margins are set to 0.05 (5% of the plot area) at each side of the plot
711
- Margins are adjustable for each plot axis using `QwtPlot.setAxisMargin` (and

qwt/scale_draw.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
import math
2323

24+
from qtpy.QtCore import QLineF, QPoint, QPointF, QRect, QRectF, Qt, qFuzzyCompare
25+
from qtpy.QtGui import QFontMetrics, QPalette, QTransform
26+
27+
from qwt._math import qwtRadians
2428
from qwt.scale_div import QwtScaleDiv
2529
from qwt.scale_map import QwtScaleMap
2630
from qwt.text import QwtText
27-
from qwt._math import qwtRadians
28-
29-
from qtpy.QtGui import QPalette, QFontMetrics, QTransform
30-
from qtpy.QtCore import Qt, qFuzzyCompare, QLineF, QRectF, QPointF, QRect, QPoint
3131

3232

3333
class QwtAbstractScaleDraw_PrivateData(object):
@@ -181,7 +181,7 @@ def setScaleDiv(self, scaleDiv):
181181
"""
182182
self.__data.scaleDiv = scaleDiv
183183
self.__data.map.setScaleInterval(scaleDiv.lowerBound(), scaleDiv.upperBound())
184-
self.__data.labelCache.clear()
184+
self.invalidateCache()
185185

186186
def setTransformation(self, transformation):
187187
"""
@@ -436,16 +436,16 @@ def tickLabel(self, font, value):
436436
437437
:param QFont font: Font
438438
:param float value: Value
439-
:return: Tick label
439+
:return: Tuple (tick label, text size)
440440
"""
441-
lbl = self.__data.labelCache.get(value)
441+
lbl, tsize = self.__data.labelCache.get(value, (None, None))
442442
if lbl is None:
443443
lbl = QwtText(self.label(value))
444444
lbl.setRenderFlags(0)
445445
lbl.setLayoutAttribute(QwtText.MinimumLayout)
446-
lbl.textSize(font)
447-
self.__data.labelCache[value] = lbl
448-
return lbl
446+
tsize = lbl.textSize(font)
447+
self.__data.labelCache[value] = lbl, tsize
448+
return lbl, tsize
449449

450450
def invalidateCache(self):
451451
"""
@@ -941,11 +941,10 @@ def drawLabel(self, painter, value):
941941
:py:meth:`drawTick()`, :py:meth:`drawBackbone()`,
942942
:py:meth:`boundingLabelRect()`
943943
"""
944-
lbl = self.tickLabel(painter.font(), value)
944+
lbl, labelSize = self.tickLabel(painter.font(), value)
945945
if lbl is None or lbl.isEmpty():
946946
return
947947
pos = self.labelPosition(value)
948-
labelSize = lbl.textSize(painter.font())
949948
transform = self.labelTransformation(pos, labelSize)
950949
painter.save()
951950
painter.setWorldTransform(transform, True)
@@ -967,11 +966,10 @@ def boundingLabelRect(self, font, value):
967966
968967
:py:meth:`labelRect()`
969968
"""
970-
lbl = self.tickLabel(font, value)
969+
lbl, labelSize = self.tickLabel(font, value)
971970
if lbl.isEmpty():
972971
return QRect()
973972
pos = self.labelPosition(value)
974-
labelSize = lbl.textSize(font)
975973
transform = self.labelTransformation(pos, labelSize)
976974
return transform.mapRect(QRect(QPoint(0, 0), labelSize.toSize()))
977975

@@ -1024,11 +1022,10 @@ def labelRect(self, font, value):
10241022
:param float value: Value
10251023
:return: Bounding rectangle that is needed to draw a label
10261024
"""
1027-
lbl = self.tickLabel(font, value)
1025+
lbl, labelSize = self.tickLabel(font, value)
10281026
if not lbl or lbl.isEmpty():
10291027
return QRectF(0.0, 0.0, 0.0, 0.0)
10301028
pos = self.labelPosition(value)
1031-
labelSize = lbl.textSize(font)
10321029
transform = self.labelTransformation(pos, labelSize)
10331030
br = transform.mapRect(QRectF(QPointF(0, 0), labelSize))
10341031
br.translate(-pos.x(), -pos.y())

0 commit comments

Comments
 (0)