Skip to content

Commit 54fbf0e

Browse files
committed
Added QwtScaleDraw.setFixedSize/fixedSize methods to set the new fixed size option
When drawing text labels, if fixed size option is enabled, the axes width and height won't change when axis range is changing. On the contrary, if this option is disabled (default behavior), axes are drawn in order to optimize layout space and depends on text label individual sizes. This option is not implemented in Qwt C++ library: this may be used either as an optimization (updating plot layout is faster when this option is enabled) or as an appearance preference (with Qwt default behavior, the size of axes may change when zooming and/or panning plot canvas which in some cases may not be desired).
1 parent 8d02eca commit 54fbf0e

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# History of changes
22

3+
## Version 0.5.0
4+
5+
Various optimizations
6+
Added QwtScaleDraw.setFixedSize/fixedSize methods to set the new fixed size option (see documentation)
7+
8+
## Version 0.4.0
9+
10+
Color bar: fixed axis ticks shaking when color bar is enabled
11+
Fixed QwtPainter.drawColorBar for horizontal color bars (typo)
12+
Restored compatibility with original Qwt signals (QwtPlot, ...)
13+
314
## Version 0.3.0
415

516
Renamed the project (python-qwt --> PythonQwt), for various reasons.

qwt/plot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,20 @@ def setAxisLabelRotation(self, axisId, rotation):
774774
if self.axisValid(axisId):
775775
self.axisWidget(axisId).setLabelRotation(rotation)
776776

777+
def setAxisFixedSize(self, axisId, state):
778+
"""
779+
Set tick labels fixed size option
780+
781+
:param int axisId: Axis index
782+
:param bool state: On/off
783+
784+
.. seealso::
785+
786+
:py:meth:`qwt.scale_draw.QwtScaleDraw.setFixedSize()`
787+
"""
788+
if self.axisValid(axisId):
789+
self.axisWidget(axisId).setFixedSize(state)
790+
777791
def setAxisMaxMinor(self, axisId, maxMinor):
778792
"""
779793
Set the maximum number of minor scale intervals for a specified axis

qwt/scale_draw.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def __init__(self):
439439
self.alignment = QwtScaleDraw.BottomScale
440440
self.labelAlignment = 0
441441
self.labelRotation = 0.
442-
442+
self.fixedSize = False
443443
self.pos = QPointF()
444444

445445

@@ -483,6 +483,7 @@ def __init__(self):
483483
QwtAbstractScaleDraw.__init__(self)
484484
self.__data = QwtScaleDraw_PrivateData()
485485
self.setLength(100)
486+
self._max_label_sizes = {}
486487

487488
def alignment(self):
488489
"""
@@ -1124,6 +1125,49 @@ def labelAlignment(self):
11241125
:py:meth:`setLabelAlignment()`, :py:meth:`labelRotation()`
11251126
"""
11261127
return self.__data.labelAlignment
1128+
1129+
def setFixedSize(self, state):
1130+
"""
1131+
Set fixed size option state
1132+
1133+
When drawing text labels, if fixed size option is enabled, the axes
1134+
width and height won't change when axis range is changing. On the
1135+
contrary, if this option is disabled (default behavior), axes are
1136+
drawn in order to optimize layout space and depends on text label
1137+
individual sizes.
1138+
1139+
This option is not implemented in Qwt C++ library: this may be used
1140+
either as an optimization (updating plot layout is faster when this
1141+
option is enabled) or as an appearance preference (with Qwt default
1142+
behavior, the size of axes may change when zooming and/or panning
1143+
plot canvas which in some cases may not be desired).
1144+
1145+
:param bool state: On/off
1146+
1147+
.. seealso::
1148+
1149+
:py:meth:`fixedSize()`
1150+
"""
1151+
self.__data.fixedSize = state
1152+
1153+
def fixedSize(self):
1154+
"""
1155+
:return: True if fixed size option is enabled for labels
1156+
1157+
.. seealso::
1158+
1159+
:py:meth:`setFixedSize()`
1160+
"""
1161+
return self.__data.fixedSize
1162+
1163+
def _get_max_label_size(self, font):
1164+
fid = font.toString()
1165+
size = self._max_label_sizes.get(fid)
1166+
if size is None:
1167+
size = self.labelSize(font, -999999)
1168+
return self._max_label_sizes.setdefault(fid, size)
1169+
else:
1170+
return size
11271171

11281172
def maxLabelWidth(self, font):
11291173
"""
@@ -1133,7 +1177,10 @@ def maxLabelWidth(self, font):
11331177
ticks = self.scaleDiv().ticks(QwtScaleDiv.MajorTick)
11341178
if not ticks:
11351179
return 0
1136-
return np.ceil(max([self.labelSize(font, v).width()
1180+
if self.fixedSize():
1181+
return self._get_max_label_size(font).width()
1182+
else:
1183+
return np.ceil(max([self.labelSize(font, v).width()
11371184
for v in ticks if self.scaleDiv().contains(v)]))
11381185

11391186
def maxLabelHeight(self, font):
@@ -1144,7 +1191,10 @@ def maxLabelHeight(self, font):
11441191
ticks = self.scaleDiv().ticks(QwtScaleDiv.MajorTick)
11451192
if not ticks:
11461193
return 0
1147-
return np.ceil(max([self.labelSize(font, v).height()
1194+
if self.fixedSize():
1195+
return self._get_max_label_size(font).height()
1196+
else:
1197+
return np.ceil(max([self.labelSize(font, v).height()
11481198
for v in ticks if self.scaleDiv().contains(v)]))
11491199

11501200
def updateMap(self):

0 commit comments

Comments
 (0)