Skip to content

Commit dfe71c5

Browse files
committed
Code cleaning/refactoring following recent major API change
1 parent 60e9fdf commit dfe71c5

File tree

7 files changed

+104
-186
lines changed

7 files changed

+104
-186
lines changed

qwt/painter.py

Lines changed: 19 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -16,143 +16,38 @@
1616
from qwt.color_map import QwtColorMap
1717
from qwt.scale_map import QwtScaleMap
1818

19-
from qwt.qt.QtGui import (QPaintEngine, QApplication, QFont, QFontInfo, QFrame,
20-
QPixmap, QPainter, QPolygonF, QPalette, QStyle, QPen,
21-
QAbstractTextDocumentLayout, QStyleOptionFocusRect,
22-
QBrush, QLinearGradient, QPainterPath, QColor,
23-
QStyleOption, QTransform)
24-
from qwt.qt.QtCore import (QSize, QRectF, Qt, QPointF, QSizeF, QRect, QPoint,
25-
QT_VERSION)
26-
27-
import numpy as np
19+
from qwt.qt.QtGui import (QPaintEngine, QFrame, QPixmap, QPainter, QPalette,
20+
QStyle, QPen, QStyleOptionFocusRect, QBrush,
21+
QLinearGradient, QPainterPath, QColor, QStyleOption)
22+
from qwt.qt.QtCore import Qt, QRect, QPoint, QT_VERSION
2823

2924
QWIDGETSIZE_MAX = (1<<24)-1
3025

3126

32-
def qwtScreenResolution():
33-
screenResolution = QSize()
34-
if not screenResolution.isValid():
35-
desktop = QApplication.desktop()
36-
if desktop is not None:
37-
screenResolution.setWidth(desktop.logicalDpiX())
38-
screenResolution.setHeight(desktop.logicalDpiY())
39-
return screenResolution
40-
41-
42-
def qwtUnscaleFont(painter):
43-
if painter.font().pixelSize() >= 0:
44-
return
45-
screenResolution = qwtScreenResolution()
46-
pd = painter.device()
47-
if pd.logicalDpiX() != screenResolution.width() or\
48-
pd.logicalDpiY() != screenResolution.height():
49-
pixelFont = QFont(painter.font(), QApplication.desktop())
50-
pixelFont.setPixelSize(QFontInfo(pixelFont).pixelSize())
51-
painter.setFont(pixelFont)
52-
53-
5427
def isX11GraphicsSystem():
5528
pm = QPixmap(1, 1)
5629
painter = QPainter(pm)
5730
isX11 = painter.paintEngine().type() == QPaintEngine.X11
5831
del painter
5932
return isX11
6033

61-
62-
class QwtPainterClass(object):
63-
"""A collection of `QPainter` workarounds"""
64-
65-
def drawText(self, *args):
66-
if len(args) == 4:
67-
if isinstance(args[1], (QRectF, QRect)):
68-
painter, rect, flags, text = args
69-
painter.save()
70-
qwtUnscaleFont(painter)
71-
painter.drawText(rect, flags, text)
72-
painter.restore()
73-
else:
74-
painter, x, y, text = args
75-
self.drawText(painter, QPointF(x, y), text)
76-
elif len(args) == 3:
77-
painter, pos, text = args
78-
painter.save()
79-
qwtUnscaleFont(painter)
80-
painter.drawText(pos, text)
81-
painter.restore()
82-
elif len(args) == 7:
83-
painter, x, y, w, h, flags, text = args
84-
self.drawText(painter, QRectF( x, y, w, h ), flags, text)
85-
else:
86-
raise TypeError("QwtPainter.drawText() takes 3, 4 or 7 argument"\
87-
"(s) (%s given)" % len(args))
88-
89-
def drawSimpleRichText(self, painter, rect, flags, text):
90-
"""
91-
Draw a text document into a rectangle
92-
93-
:param QPainter painter: Painter
94-
:param QRectF rect: Target rectangle
95-
:param int flags: Alignments/Text flags, see `QPainter.drawText()`
96-
:param QTextDocument text: Text document
97-
"""
98-
txt = text.clone()
34+
def qwtFillRect(widget, painter, rect, brush):
35+
if brush.style() == Qt.TexturePattern:
9936
painter.save()
100-
unscaledRect = QRectF(rect)
101-
if painter.font().pixelSize() < 0:
102-
res = qwtScreenResolution()
103-
pd = painter.device()
104-
if pd.logicalDpiX() != res.width()\
105-
or pd.logicalDpiY() != res.height():
106-
transform = QTransform()
107-
transform.scale(res.width()/float(pd.logicalDpiX()),
108-
res.height()/float(pd.logicalDpiY()))
109-
painter.setWorldTransform(transform, True)
110-
invtrans, _ok = transform.inverted()
111-
unscaledRect = invtrans.mapRect(rect)
112-
txt.setDefaultFont(painter.font())
113-
txt.setPageSize(QSizeF(unscaledRect.width(), QWIDGETSIZE_MAX))
114-
layout = txt.documentLayout()
115-
height = layout.documentSize().height()
116-
y = unscaledRect.y()
117-
if flags & Qt.AlignBottom:
118-
y += unscaledRect.height()-height
119-
elif flags & Qt.AlignVCenter:
120-
y += (unscaledRect.height()-height)/2
121-
context = QAbstractTextDocumentLayout.PaintContext()
122-
context.palette.setColor(QPalette.Text, painter.pen().color())
123-
painter.translate(unscaledRect.x(), y)
124-
layout.draw(painter, context)
37+
painter.setClipRect(rect)
38+
painter.drawTiledPixmap(rect, brush.texture(), rect.topLeft())
12539
painter.restore()
126-
127-
def drawLine(self, *args):
128-
if len(args) == 3:
129-
painter, p1, p2 = args
130-
if isinstance(p1, QPointF):
131-
p1 = p1.toPoint()
132-
if isinstance(p2, QPointF):
133-
p2 = p2.toPoint()
134-
painter.drawLine(p1, p2)
135-
elif len(args) == 5:
136-
painter, x1, y1, x2, y2 = args
137-
self.drawLine(painter, QPointF(x1, y1), QPointF(x2, y2))
138-
elif len(args) == 2:
139-
painter, line = args
140-
self.drawLine(painter, line.p1(), line.p2())
141-
else:
142-
raise TypeError("QwtPainter.drawLine() takes 2, 3 or 5 argument"\
143-
"(s) (%s given)" % len(args))
40+
elif brush.gradient():
41+
painter.save()
42+
painter.setClipRect(rect)
43+
painter.fillRect(0, 0, widget.width(), widget.height(), brush)
44+
painter.restore()
45+
else:
46+
painter.fillRect(rect, brush)
14447

145-
def drawPoints(self, painter, *args):
146-
if len(args) == 2:
147-
points, pointCount = args
148-
else:
149-
polygon, = args
150-
points, pointCount = polygon.data(), polygon.size()
151-
if isinstance(polygon, QPolygonF):
152-
points.setsize(pointCount*np.finfo(float).dtype.itemsize)
153-
else:
154-
points.setsize(pointCount*np.iinfo(int).dtype.itemsize)
155-
painter.drawPoints(polygon)
48+
49+
class QwtPainterClass(object):
50+
"""A collection of `QPainter` workarounds"""
15651

15752
def drawImage(self, painter, rect, image):
15853
alignedRect = rect.toAlignedRect()
@@ -548,19 +443,4 @@ def backingStore(self, widget, size):
548443
pm.x11SetScreen(widget.x11Info().screen())
549444
return pm
550445

551-
QwtPainter = QwtPainterClass()
552-
553-
554-
def qwtFillRect(widget, painter, rect, brush):
555-
if brush.style() == Qt.TexturePattern:
556-
painter.save()
557-
painter.setClipRect(rect)
558-
painter.drawTiledPixmap(rect, brush.texture(), rect.topLeft())
559-
painter.restore()
560-
elif brush.gradient():
561-
painter.save()
562-
painter.setClipRect(rect)
563-
painter.fillRect(0, 0, widget.width(), widget.height(), brush)
564-
painter.restore()
565-
else:
566-
painter.fillRect(rect, brush)
446+
QwtPainter = QwtPainterClass()

qwt/plot_curve.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,13 @@ def qwtVerifyRange(size, i1, i2):
5151
return i2-i1+1
5252

5353

54-
def series_to_polyline(xMap, yMap, series, from_, to, Polygon=None):
54+
def series_to_polyline(xMap, yMap, series, from_, to):
5555
"""
5656
Convert series data to QPolygon(F) polyline
5757
"""
58-
if Polygon is None:
59-
Polygon = QPolygonF
60-
polyline = Polygon(to-from_+1)
58+
polyline = QPolygonF(to-from_+1)
6159
pointer = polyline.data()
62-
if Polygon is QPolygonF:
63-
dtype, tinfo = np.float, np.finfo
64-
else:
65-
dtype, tinfo = np.int, np.iinfo
60+
dtype, tinfo = np.float, np.finfo # integers: = np.int, np.iinfo
6661
pointer.setsize(2*polyline.size()*tinfo(dtype).dtype.itemsize)
6762
memory = np.frombuffer(pointer, dtype)
6863
memory[:(to-from_)*2+1:2] = xMap.transform(series.xData()[from_:to+1])
@@ -525,9 +520,9 @@ def drawSticks(self, painter, xMap, yMap, canvasRect, from_, to):
525520
xi = xMap.transform(sample.x())
526521
yi = yMap.transform(sample.y())
527522
if o == Qt.Horizontal:
528-
QwtPainter.drawLine(painter, x0, yi, xi, yi)
523+
painter.drawLine(x0, yi, xi, yi)
529524
else:
530-
QwtPainter.drawLine(painter, xi, y0, xi, yi)
525+
painter.drawLine(xi, y0, xi, yi)
531526
painter.restore()
532527

533528
def drawDots(self, painter, xMap, yMap, canvasRect, from_, to):
@@ -546,15 +541,12 @@ def drawDots(self, painter, xMap, yMap, canvasRect, from_, to):
546541
:py:meth:`draw()`, :py:meth:`drawSticks()`,
547542
:py:meth:`drawSteps()`, :py:meth:`drawLines()`
548543
"""
549-
color = painter.pen().color()
550-
if painter.pen().style() == Qt.NoPen or color.alpha() == 0:
551-
return
552544
doFill = self.__data.brush.style() != Qt.NoBrush\
553545
and self.__data.brush.color().alpha() > 0
554-
points = series_to_polyline(xMap, yMap, self.data(), from_, to)
555-
QwtPainter.drawPoints(painter, points)
546+
polyline = series_to_polyline(xMap, yMap, self.data(), from_, to)
547+
painter.drawPoints(polyline)
556548
if doFill:
557-
self.fillCurve(painter, xMap, yMap, canvasRect, points)
549+
self.fillCurve(painter, xMap, yMap, canvasRect, polyline)
558550

559551
def drawSteps(self, painter, xMap, yMap, canvasRect, from_, to):
560552
"""
@@ -814,7 +806,7 @@ def legendIcon(self, index, size):
814806
# pn.setCapStyle(Qt.FlatCap)
815807
painter.setPen(pn)
816808
y = .5*size.height()
817-
QwtPainter.drawLine(painter, 0., y, size.width(), y)
809+
painter.drawLine(0., y, size.width(), y)
818810
if self.__data.legendAttributes & QwtPlotCurve.LegendShowSymbol:
819811
if self.__data.symbol:
820812
r = QRectF(0, 0, size.width(), size.height())

qwt/plot_grid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ def drawLines(self, painter, canvasRect, orientation, scaleMap, values):
307307
if orientation == Qt.Horizontal:
308308
if qwtFuzzyGreaterOrEqual(value, y1) and\
309309
qwtFuzzyLessOrEqual(value, y2):
310-
QwtPainter.drawLine(painter, x1, value, x2, value)
310+
painter.drawLine(x1, value, x2, value)
311311
else:
312312
if qwtFuzzyGreaterOrEqual(value, x1) and\
313313
qwtFuzzyLessOrEqual(value, x2):
314-
QwtPainter.drawLine(painter, value, y1, value, y2)
314+
painter.drawLine(value, y1, value, y2)
315315

316316
def majorPen(self):
317317
"""

qwt/plot_marker.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,10 @@ def drawLines(self, painter, canvasRect, pos):
173173
painter.setPen(self.__data.pen)
174174
if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross):
175175
y = pos.y()
176-
QwtPainter.drawLine(painter, canvasRect.left(),
177-
y, canvasRect.right()-1., y)
176+
painter.drawLine(canvasRect.left(), y, canvasRect.right()-1., y)
178177
if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross):
179178
x = pos.x()
180-
QwtPainter.drawLine(painter, x,
181-
canvasRect.top(), x, canvasRect.bottom()-1.)
179+
painter.drawLine(x, canvasRect.top(), x, canvasRect.bottom()-1.)
182180

183181
def drawLabel(self, painter, canvasRect, pos):
184182
"""
@@ -522,10 +520,10 @@ def legendIcon(self, index, size):
522520
painter.setPen(self.__data.pen)
523521
if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross):
524522
y = .5*size.height()
525-
QwtPainter.drawLine(painter, 0., y, size.width(), y)
523+
painter.drawLine(0., y, size.width(), y)
526524
if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross):
527525
x = .5*size.width()
528-
QwtPainter.drawLine(painter, x, 0., x, size.height())
526+
painter.drawLine(x, 0., x, size.height())
529527
if self.__data.symbol:
530528
r = QRect(0, 0, size.width(), size.height())
531529
self.__data.symbol.drawSymbol(painter, r)

qwt/scale_draw.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -769,19 +769,19 @@ def drawTick(self, painter, value, len_):
769769
if self.alignment() == self.LeftScale:
770770
x1 = pos.x() + a
771771
x2 = pos.x() + a - pw - len_
772-
QwtPainter.drawLine(painter, x1, tval, x2, tval)
772+
painter.drawLine(x1, tval, x2, tval)
773773
elif self.alignment() == self.RightScale:
774774
x1 = pos.x()
775775
x2 = pos.x() + pw + len_
776-
QwtPainter.drawLine(painter, x1, tval, x2, tval)
776+
painter.drawLine(x1, tval, x2, tval)
777777
elif self.alignment() == self.BottomScale:
778778
y1 = pos.y()
779779
y2 = pos.y() + pw + len_
780-
QwtPainter.drawLine(painter, tval, y1, tval, y2)
780+
painter.drawLine(tval, y1, tval, y2)
781781
elif self.alignment() == self.TopScale:
782782
y1 = pos.y() + a
783783
y2 = pos.y() - pw - len_ + a
784-
QwtPainter.drawLine(painter, tval, y1, tval, y2)
784+
painter.drawLine(tval, y1, tval, y2)
785785

786786
def drawBackbone(self, painter):
787787
"""
@@ -798,16 +798,16 @@ def drawBackbone(self, painter):
798798
off = .5*self.penWidth()
799799
if self.alignment() == self.LeftScale:
800800
x = pos.x() - off
801-
QwtPainter.drawLine(painter, x, pos.y(), x, pos.y()+len_)
801+
painter.drawLine(x, pos.y(), x, pos.y()+len_)
802802
elif self.alignment() == self.RightScale:
803803
x = pos.x() + off
804-
QwtPainter.drawLine(painter, x, pos.y(), x, pos.y()+len_)
804+
painter.drawLine(x, pos.y(), x, pos.y()+len_)
805805
elif self.alignment() == self.TopScale:
806806
y = pos.y() - off
807-
QwtPainter.drawLine(painter, pos.x(), y, pos.x()+len_, y)
807+
painter.drawLine(pos.x(), y, pos.x()+len_, y)
808808
elif self.alignment() == self.BottomScale:
809809
y = pos.y() + off
810-
QwtPainter.drawLine(painter, pos.x(), y, pos.x()+len_, y)
810+
painter.drawLine(pos.x(), y, pos.x()+len_, y)
811811

812812
def move(self, *args):
813813
"""

qwt/symbol.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ def qwtDrawLineSymbols(painter, orientations, points, numPoints, symbol):
214214
if orientations & Qt.Horizontal:
215215
x = round(pos.x())-sw2
216216
y = round(pos.y())
217-
QwtPainter.drawLine(painter, x, y, x+sw, y)
217+
painter.drawLine(x, y, x+sw, y)
218218
if orientations & Qt.Vertical:
219219
x = round(pos.x())
220220
y = round(pos.y())-sh2
221-
QwtPainter.drawLine(painter, x, y, x, y+sh)
221+
painter.drawLine(x, y, x, y+sh)
222222

223223

224224
def qwtDrawXCrossSymbols(painter, points, numPoints, symbol):
@@ -236,8 +236,8 @@ def qwtDrawXCrossSymbols(painter, points, numPoints, symbol):
236236
x2 = x1+sw
237237
y1 = pos.y()-sh2
238238
y2 = y1+sh
239-
QwtPainter.drawLine(painter, x1, y1, x2, y2)
240-
QwtPainter.drawLine(painter, x2, y1, x1, y2)
239+
painter.drawLine(x1, y1, x2, y2)
240+
painter.drawLine(x2, y1, x1, y2)
241241

242242

243243
def qwtDrawStar1Symbols(painter, points, numPoints, symbol):
@@ -249,12 +249,10 @@ def qwtDrawStar1Symbols(painter, points, numPoints, symbol):
249249
r.moveCenter(pos.toPoint())
250250
c = QPointF(r.center())
251251
d1 = r.width()/2.*(1.-sqrt1_2)
252-
QwtPainter.drawLine(painter, r.left()+d1, r.top()+d1,
253-
r.right()-d1, r.bottom()-d1)
254-
QwtPainter.drawLine(painter, r.left()+d1, r.bottom()-d1,
255-
r.right()-d1, r.top()+d1)
256-
QwtPainter.drawLine(painter, c.x(), r.top(), c.x(), r.bottom())
257-
QwtPainter.drawLine(painter, r.left(), c.y(), r.right(), c.y())
252+
painter.drawLine(r.left()+d1, r.top()+d1, r.right()-d1, r.bottom()-d1)
253+
painter.drawLine(r.left()+d1, r.bottom()-d1, r.right()-d1, r.top()+d1)
254+
painter.drawLine(c.x(), r.top(), c.x(), r.bottom())
255+
painter.drawLine(r.left(), c.y(), r.right(), c.y())
258256

259257

260258
def qwtDrawStar2Symbols(painter, points, numPoints, symbol):

0 commit comments

Comments
 (0)