Skip to content

Commit 7463afb

Browse files
committed
Added convenience functions for creating usual objects (curve, grid, marker, ...)
1 parent 5a825dd commit 7463afb

15 files changed

Lines changed: 492 additions & 168 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
### Version 0.7.0 ###
55

6+
- Added convenience functions for creating usual objects (curve, grid, marker, ...):
7+
8+
- `QwtPlotCurve.make`
9+
- `QwtPlotMarker.make`
10+
- `QwtPlotGrid.make`
11+
- `QwtSymbol.make`
12+
- `QwtText.make`
13+
614
- Added new test launcher with screenshots (automatically generated)
715
- Removed `guidata` dependency thanks to the new specific GUI-based test launcher
816
- Updated documentation (added more examples, using automatically generated screenshots)

qwt/plot_curve.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""
1515

1616
from .text import QwtText
17-
from .plot import QwtPlotItem, QwtPlotItem_PrivateData
17+
from .plot import QwtPlot, QwtPlotItem, QwtPlotItem_PrivateData
1818
from ._math import qwtSqr
1919
from .graphic import QwtGraphic
2020
from .plot_series import (
@@ -189,6 +189,84 @@ def __init__(self, title=None):
189189
QwtSeriesStore.__init__(self)
190190
self.init()
191191

192+
@classmethod
193+
def make(
194+
cls,
195+
xdata=None,
196+
ydata=None,
197+
title=None,
198+
plot=None,
199+
z=None,
200+
x_axis=None,
201+
y_axis=None,
202+
style=None,
203+
symbol=None,
204+
linecolor=None,
205+
linewidth=None,
206+
linestyle=None,
207+
antialiased=False,
208+
size=None,
209+
finite=None,
210+
):
211+
"""
212+
Create and setup a new `QwtPlotCurve` object (convenience function).
213+
214+
:param xdata: List/array of x values
215+
:param ydata: List/array of y values
216+
:param title: Curve title
217+
:type title: qwt.text.QwtText or str or None
218+
:param plot: Plot to attach the curve to
219+
:type plot: qwt.plot.QwtPlot or None
220+
:param z: Z-value
221+
:type z: float or None
222+
:param x_axis: curve X-axis (default: QwtPlot.yLeft)
223+
:type x_axis: int or None
224+
:param y_axis: curve Y-axis (default: QwtPlot.xBottom)
225+
:type y_axis: int or None
226+
:param style: curve style (`QwtPlotCurve.NoCurve`, `QwtPlotCurve.Lines`, `QwtPlotCurve.Sticks`, `QwtPlotCurve.Steps`, `QwtPlotCurve.Dots`, `QwtPlotCurve.UserCurve`)
227+
:type style: int or None
228+
:param symbol: curve symbol
229+
:type symbol: qwt.symbol.QwtSymbol or None
230+
:param linecolor: curve line color
231+
:type linecolor: QColor or None
232+
:param linewidth: curve line width
233+
:type linewidth: float or None
234+
:param linestyle: curve pen style
235+
:type linestyle: Qt.PenStyle or None
236+
:param bool antialiased: if True, enable antialiasing rendering
237+
:param size: size of xData and yData
238+
:type size: int or None
239+
:param bool finite: if True, keep only finite array elements (remove all infinity and not a number values), otherwise do not filter array elements
240+
241+
.. seealso::
242+
243+
:py:meth:`setData()`, :py:meth:`setPen()`, :py:meth:`attach()`
244+
"""
245+
item = cls(title)
246+
if z is not None:
247+
item.setZ(z)
248+
if xdata is not None or ydata is not None:
249+
if xdata is None:
250+
raise ValueError("Missing xdata parameter")
251+
if ydata is None:
252+
raise ValueError("Missing ydata parameter")
253+
item.setData(xdata, ydata, size=size, finite=finite)
254+
x_axis = QwtPlot.xBottom if x_axis is None else x_axis
255+
y_axis = QwtPlot.yLeft if y_axis is None else y_axis
256+
item.setAxes(x_axis, y_axis)
257+
if style is not None:
258+
item.setStyle(style)
259+
if symbol is not None:
260+
item.setSymbol(symbol)
261+
linecolor = Qt.black if linecolor is None else linecolor
262+
linewidth = 1.0 if linewidth is None else linewidth
263+
linestyle = Qt.SolidLine if linestyle is None else linestyle
264+
item.setPen(QPen(linecolor, linewidth, linestyle))
265+
item.setRenderHint(cls.RenderAntialiased, antialiased)
266+
if plot is not None:
267+
item.attach(plot)
268+
return item
269+
192270
def init(self):
193271
"""Initialize internal members"""
194272
self.__data = QwtPlotCurve_PrivateData()

qwt/plot_grid.py

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""
1515

1616
from .scale_div import QwtScaleDiv
17-
from .plot import QwtPlotItem
17+
from .plot import QwtPlot, QwtPlotItem
1818
from .text import QwtText
1919
from ._math import qwtFuzzyGreaterOrEqual, qwtFuzzyLessOrEqual
2020

@@ -47,12 +47,88 @@ class QwtPlotGrid(QwtPlotItem):
4747
rectangle.
4848
"""
4949

50-
def __init__(self):
51-
QwtPlotItem.__init__(self, QwtText("Grid"))
50+
def __init__(self, title="Grid"):
51+
QwtPlotItem.__init__(self, title)
5252
self.__data = QwtPlotGrid_PrivateData()
5353
self.setItemInterest(QwtPlotItem.ScaleInterest, True)
5454
self.setZ(10.0)
5555

56+
@classmethod
57+
def make(
58+
cls,
59+
plot=None,
60+
z=None,
61+
enablemajor=None,
62+
enableminor=None,
63+
color=None,
64+
width=None,
65+
style=None,
66+
mincolor=None,
67+
minwidth=None,
68+
minstyle=None,
69+
):
70+
"""
71+
Create and setup a new `QwtPlotGrid` object (convenience function).
72+
73+
:param plot: Plot to attach the curve to
74+
:type plot: qwt.plot.QwtPlot or None
75+
:param z: Z-value
76+
:type z: float or None
77+
:param enablemajor: Tuple of two boolean values (x, y) for enabling major grid lines
78+
:type enablemajor: bool or None
79+
:param enableminor: Tuple of two boolean values (x, y) for enabling minor grid lines
80+
:type enableminor: bool or None
81+
:param color: Pen color for both major and minor grid lines (default: Qt.gray)
82+
:type color: QColor or None
83+
:param width: Pen width for both major and minor grid lines (default: 1.0)
84+
:type width: float or None
85+
:param style: Pen style for both major and minor grid lines (default: Qt.DotLine)
86+
:type style: Qt.PenStyle or None
87+
:param mincolor: Pen color for minor grid lines only (default: Qt.gray)
88+
:type mincolor: QColor or None
89+
:param minwidth: Pen width for minor grid lines only (default: 1.0)
90+
:type minwidth: float or None
91+
:param minstyle: Pen style for minor grid lines only (default: Qt.DotLine)
92+
:type minstyle: Qt.PenStyle or None
93+
94+
.. seealso::
95+
96+
:py:meth:`setMinorPen()`, :py:meth:`setMajorPen()`
97+
"""
98+
item = cls()
99+
if z is not None:
100+
item.setZ(z)
101+
color = Qt.gray if color is None else color
102+
width = 1.0 if width is None else width
103+
style = Qt.DotLine if style is None else style
104+
item.setPen(QPen(color, width, style))
105+
if mincolor is not None or minwidth is not None or minstyle is not None:
106+
mincolor = Qt.gray if mincolor is None else mincolor
107+
minwidth = 1.0 if width is None else minwidth
108+
minstyle = Qt.DotLine if style is None else minstyle
109+
item.setMinorPen(QPen(mincolor, minwidth, minstyle))
110+
if enablemajor is not None:
111+
if isinstance(enablemajor, tuple) and len(enablemajor) == 2:
112+
item.enableX(enablemajor[0])
113+
item.enableY(enablemajor[1])
114+
else:
115+
raise TypeError(
116+
"Invalid enablemajor %r (expecting tuple of two booleans)"
117+
% enablemajor
118+
)
119+
if enableminor is not None:
120+
if isinstance(enableminor, tuple) and len(enableminor) == 2:
121+
item.enableXMin(enableminor[0])
122+
item.enableYMin(enableminor[1])
123+
else:
124+
raise TypeError(
125+
"Invalid enableminor %r (expecting tuple of two booleans)"
126+
% enableminor
127+
)
128+
if plot is not None:
129+
item.attach(plot)
130+
return item
131+
56132
def rtti(self):
57133
"""
58134
:return: Return `QwtPlotItem.Rtti_PlotGrid`

qwt/plot_marker.py

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
:members:
1414
"""
1515

16-
from .plot import QwtPlotItem
16+
from .plot import QwtPlot, QwtPlotItem
1717
from .text import QwtText
1818
from .painter import QwtPainter
1919
from .graphic import QwtGraphic
@@ -76,6 +76,93 @@ def __init__(self, title=None):
7676
self.__data = QwtPlotMarker_PrivateData()
7777
self.setZ(30.0)
7878

79+
@classmethod
80+
def make(
81+
cls,
82+
xvalue=None,
83+
yvalue=None,
84+
title=None,
85+
label=None,
86+
symbol=None,
87+
plot=None,
88+
z=None,
89+
x_axis=None,
90+
y_axis=None,
91+
align=None,
92+
orientation=None,
93+
spacing=None,
94+
linestyle=None,
95+
color=None,
96+
width=None,
97+
style=None,
98+
antialiased=False,
99+
):
100+
"""
101+
Create and setup a new `QwtPlotMarker` object (convenience function).
102+
103+
:param xvalue: x position (optional, default: None)
104+
:type xvalue: float or None
105+
:param yvalue: y position (optional, default: None)
106+
:type yvalue: float or None
107+
:param title: Marker title
108+
:type title: qwt.text.QwtText or str or None
109+
:param label: Label text
110+
:type label: qwt.text.QwtText or str or None
111+
:param symbol: New symbol
112+
:type symbol: qwt.symbol.QwtSymbol or None
113+
:param plot: Plot to attach the curve to
114+
:type plot: qwt.plot.QwtPlot or None
115+
:param z: Z-value
116+
:type z: float or None
117+
:param int x_axis: curve X-axis (default: QwtPlot.yLeft)
118+
:param int y_axis: curve Y-axis (default: QwtPlot.xBottom)
119+
:param align: Alignment of the label
120+
:type align: Qt.Alignment or None
121+
:param orientation: Orientation of the label
122+
:type orientation: Qt.Orientation or None
123+
:param spacing: Spacing (distance between the position and the label)
124+
:type spacing: int or None
125+
:param int linestyle: Line style
126+
:param QColor color: Pen color
127+
:param float width: Pen width
128+
:param Qt.PenStyle style: Pen style
129+
:param bool antialiased: if True, enable antialiasing rendering
130+
131+
.. seealso::
132+
133+
:py:meth:`setData()`, :py:meth:`setPen()`, :py:meth:`attach()`
134+
"""
135+
item = cls(title)
136+
if z is not None:
137+
item.setZ(z)
138+
if symbol is not None:
139+
item.setSymbol(symbol)
140+
if xvalue is not None:
141+
item.setXValue(xvalue)
142+
if yvalue is not None:
143+
item.setYValue(yvalue)
144+
if label is not None:
145+
item.setLabel(label)
146+
x_axis = QwtPlot.xBottom if x_axis is None else x_axis
147+
y_axis = QwtPlot.yLeft if y_axis is None else y_axis
148+
item.setAxes(x_axis, y_axis)
149+
if align is not None:
150+
item.setLabelAlignment(align)
151+
if orientation is not None:
152+
item.setLabelOrientation(orientation)
153+
if spacing is not None:
154+
item.setSpacing(spacing)
155+
color = Qt.black if color is None else color
156+
width = 1.0 if width is None else width
157+
style = Qt.SolidLine if style is None else style
158+
item.setLinePen(QPen(color, width, style))
159+
item.setRenderHint(cls.RenderAntialiased, antialiased)
160+
if linestyle is not None:
161+
item.setLineStyle(linestyle)
162+
if plot is not None:
163+
item.attach(plot)
164+
return item
165+
79166
def rtti(self):
80167
""":return: `QwtPlotItem.Rtti_PlotMarker`"""
81168
return QwtPlotItem.Rtti_PlotMarker
@@ -338,6 +425,8 @@ def setLabel(self, label):
338425
339426
:py:meth:`label()`
340427
"""
428+
if not isinstance(label, QwtText):
429+
label = QwtText(label)
341430
if label != self.__data.label:
342431
self.__data.label = label
343432
self.itemChanged()
@@ -444,7 +533,7 @@ def setLinePen(self, *args):
444533
"""
445534
Build and/or assigna a line pen, depending on the arguments.
446535
447-
.. py:method:: setPen(color, width, style)
536+
.. py:method:: setLinePen(color, width, style)
448537
449538
Build and assign a line pen
450539
@@ -456,7 +545,7 @@ def setLinePen(self, *args):
456545
:param float width: Pen width
457546
:param Qt.PenStyle style: Pen style
458547
459-
.. py:method:: setPen(pen)
548+
.. py:method:: setLinePen(pen)
460549
461550
Specify a pen for the line.
462551

qwt/symbol.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,57 @@ def __init__(self, *args):
538538
% (self.__class__.__name__, len(args))
539539
)
540540

541+
@classmethod
542+
def make(
543+
cls, style=None, brush=None, pen=None, size=None, path=None, pixmap=None, graphic=None, svgdocument=None, pinpoint=None,
544+
):
545+
"""
546+
Create and setup a new `QwtSymbol` object (convenience function).
547+
548+
:param style: Symbol Style
549+
:type style: int or None
550+
:param brush: Brush to fill the interior
551+
:type brush: QBrush or None
552+
:param pen: Outline pen
553+
:type pen: QPen or None
554+
:param size: Size
555+
:type size: QSize or None
556+
:param path: Painter path
557+
:type path: QPainterPath or None
558+
:param path: Painter path
559+
:type path: QPainterPath or None
560+
:param pixmap: Pixmap as symbol
561+
:type pixmap: QPixmap or None
562+
:param graphic: Graphic
563+
:type graphic: qwt.graphic.QwtGraphic or None
564+
:param svgdocument: SVG icon as symbol
565+
566+
.. seealso::
567+
568+
:py:meth:`setPixmap()`, :py:meth:`setGraphic()`, :py:meth:`setPath()`
569+
"""
570+
style = QwtSymbol.NoSymbol if style is None else style
571+
brush = QBrush(Qt.gray) if brush is None else QBrush(brush)
572+
pen = QPen(Qt.black, 0) if pen is None else QPen(pen)
573+
size = QSize() if size is None else size
574+
if not isinstance(size, QSize):
575+
if isinstance(size, tuple) and len(size) == 2:
576+
size = QSize(size[0], size[1])
577+
else:
578+
raise TypeError("Invalid size %r" % size)
579+
item = cls(style, brush, pen, size)
580+
if path is not None:
581+
item.setPath(path)
582+
elif pixmap is not None:
583+
item.setPixmap(pixmap)
584+
elif graphic is not None:
585+
item.setGraphic(graphic)
586+
elif svgdocument is not None:
587+
item.setSvgDocument(svgdocument)
588+
if pinpoint is not None:
589+
item.setPinPoint(pinpoint)
590+
return item
591+
541592
def setCachePolicy(self, policy):
542593
"""
543594
Change the cache policy

0 commit comments

Comments
 (0)