Skip to content

Commit b19fced

Browse files
committed
QwtAbstractScaleDraw: added option to set the tick color lighter factor for each tick type (minor, medium, major)
This feature is used with the new flatStyle option
1 parent d693225 commit b19fced

4 files changed

Lines changed: 59 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- QwtPlot: added "flatStyle" option, a PythonQwt-exclusive feature improving
77
default plot style (without margin, more compact and flat look) -- option is
88
enabled by default
9+
- QwtAbstractScaleDraw: added option to set the tick color lighter factor for
10+
each tick type (minor, medium, major) -- this feature is used with the new
11+
flatStyle option
912
- Fixed obvious errors (+ poor implementations) in untested code parts
1013

1114

qwt/plot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,11 @@ def setFlatStyle(self, state):
10171017
for axis_id in self.validAxes:
10181018
self.axisWidget(axis_id).setMargin(0)
10191019
self.axisWidget(axis_id).setSpacing(0)
1020+
for tick_type, factor in ((QwtScaleDiv.MajorTick, 100),
1021+
(QwtScaleDiv.MediumTick, 125),
1022+
(QwtScaleDiv.MinorTick, 150)):
1023+
self.axisScaleDraw(axis_id).setTickLighterFactor(tick_type,
1024+
factor)
10201025
# self.axisWidget(axis_id).setBorderDist(0, 0)
10211026
else:
10221027
self.canvas().setFrameStyle(QFrame.Panel|QFrame.Sunken)
@@ -1029,6 +1034,10 @@ def setFlatStyle(self, state):
10291034
for axis_id in self.validAxes:
10301035
self.axisWidget(axis_id).setMargin(2)
10311036
self.axisWidget(axis_id).setSpacing(2)
1037+
for tick_type in (QwtScaleDiv.MajorTick, QwtScaleDiv.MediumTick,
1038+
QwtScaleDiv.MinorTick):
1039+
self.axisScaleDraw(axis_id).setTickLighterFactor(tick_type,
1040+
100)
10321041
self.__data.flatStyle = state
10331042

10341043
def flatStyle(self):

qwt/scale_draw.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ def __init__(self):
4040
self.components = QwtAbstractScaleDraw.Backbone|\
4141
QwtAbstractScaleDraw.Ticks|\
4242
QwtAbstractScaleDraw.Labels
43-
self.tickLength = [None] * 3
44-
self.tickLength[QwtScaleDiv.MinorTick] = 4.0
45-
self.tickLength[QwtScaleDiv.MediumTick] = 6.0
46-
self.tickLength[QwtScaleDiv.MajorTick] = 8.0
43+
self.tick_length = {QwtScaleDiv.MinorTick: 4.,
44+
QwtScaleDiv.MediumTick: 6.,
45+
QwtScaleDiv.MajorTick: 8.}
46+
self.tick_lighter_factor = {QwtScaleDiv.MinorTick: 100,
47+
QwtScaleDiv.MediumTick: 100,
48+
QwtScaleDiv.MajorTick: 100}
4749

4850
self.map = QwtScaleMap()
4951
self.scaleDiv = QwtScaleDiv()
@@ -247,13 +249,15 @@ def draw(self, painter, palette):
247249
if self.hasComponent(QwtAbstractScaleDraw.Ticks):
248250
painter.save()
249251
pen = painter.pen()
250-
pen.setColor(palette.color(QPalette.WindowText))
251252
pen.setCapStyle(Qt.FlatCap)
252-
painter.setPen(pen)
253+
default_color = palette.color(QPalette.WindowText)
253254
for tickType in range(QwtScaleDiv.NTickTypes):
254-
tickLen = self.__data.tickLength[tickType]
255+
tickLen = self.__data.tick_length[tickType]
255256
if tickLen <= 0.:
256257
continue
258+
factor = self.__data.tick_lighter_factor[tickType]
259+
pen.setColor(default_color.lighter(factor))
260+
painter.setPen(pen)
257261
ticks = self.__data.scaleDiv.ticks(tickType)
258262
for v in ticks:
259263
if self.__data.scaleDiv.contains(v):
@@ -335,39 +339,33 @@ def minimumExtent(self):
335339
"""
336340
return self.__data.minExtent
337341

338-
def setTickLength(self, tickType, length):
342+
def setTickLength(self, tick_type, length):
339343
"""
340344
Set the length of the ticks
341345
342-
:param int tickType: Tick type
346+
:param int tick_type: Tick type
343347
:param float length: New length
344348
345349
.. warning::
346350
347351
the length is limited to [0..1000]
348352
"""
349-
if tickType < QwtScaleDiv.MinorTick or\
350-
tickType > QwtScaleDiv.MajorTick:
351-
return
352-
if length < 0.:
353-
length = 0.
354-
maxTickLen = 1000.
355-
if length > maxTickLen:
356-
length = maxTickLen
357-
self.__data.tickLength[tickType] = length
353+
if tick_type not in self.__data.tick_length:
354+
raise ValueError("Invalid tick type: %r" % tick_type)
355+
self.__data.tick_length[tick_type] = min([1000., max([0., length])])
358356

359-
def tickLength(self, tickType):
357+
def tickLength(self, tick_type):
360358
"""
359+
:param int tick_type: Tick type
361360
:return: Length of the ticks
362361
363362
.. seealso::
364363
365364
:py:meth:`setTickLength()`, :py:meth:`maxTickLength()`
366365
"""
367-
if tickType < QwtScaleDiv.MinorTick or\
368-
tickType > QwtScaleDiv.MajorTick:
369-
return 0
370-
return self.__data.tickLength[tickType]
366+
if tick_type not in self.__data.tick_length:
367+
raise ValueError("Invalid tick type: %r" % tick_type)
368+
return self.__data.tick_length[tick_type]
371369

372370
def maxTickLength(self):
373371
"""
@@ -379,10 +377,31 @@ def maxTickLength(self):
379377
380378
:py:meth:`tickLength()`, :py:meth:`setTickLength()`
381379
"""
382-
length = 0.
383-
for tickType in range(QwtScaleDiv.NTickTypes):
384-
length = max([length, self.__data.tickLength[tickType]])
385-
return length
380+
return max([0.] + list(self.__data.tick_length.values()))
381+
382+
def setTickLighterFactor(self, tick_type, factor):
383+
"""
384+
Set the color lighter factor of the ticks
385+
386+
:param int tick_type: Tick type
387+
:param int factor: New factor
388+
"""
389+
if tick_type not in self.__data.tick_length:
390+
raise ValueError("Invalid tick type: %r" % tick_type)
391+
self.__data.tick_lighter_factor[tick_type] = min([0, factor])
392+
393+
def tickLighterFactor(self, tick_type):
394+
"""
395+
:param int tick_type: Tick type
396+
:return: Color lighter factor of the ticks
397+
398+
.. seealso::
399+
400+
:py:meth:`setTickLighterFactor()`
401+
"""
402+
if tick_type not in self.__data.tick_length:
403+
raise ValueError("Invalid tick type: %r" % tick_type)
404+
return self.__data.tick_lighter_factor[tick_type]
386405

387406
def label(self, value):
388407
"""

qwt/tests/ReallySimpleDemo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, *args):
2323
self.setAxisTitle(QwtPlot.xBottom, 'x -->')
2424
self.setAxisTitle(QwtPlot.yLeft, 'y -->')
2525
self.enableAxis(self.xBottom)
26+
self.setFlatStyle(False)
2627

2728
# insert a few curves
2829
cSin = QwtPlotCurve('y = sin(x)')

0 commit comments

Comments
 (0)