File: plot_marker.py

package info (click to toggle)
python-qwt 0.12.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,376 kB
  • sloc: python: 11,953; makefile: 19; sh: 10
file content (633 lines) | stat: -rw-r--r-- 21,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the Qwt License
# Copyright (c) 2002 Uwe Rathmann, for the original C++ code
# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
# (see LICENSE file for more details)

"""
QwtPlotMarker
-------------

.. autoclass:: QwtPlotMarker
   :members:
"""

from qtpy.QtCore import QLineF, QObject, QPointF, QRect, QRectF, QSizeF, Qt
from qtpy.QtGui import QPainter, QPen

from qwt.graphic import QwtGraphic
from qwt.plot import QwtPlot, QwtPlotItem
from qwt.qthelpers import qcolor_from_str
from qwt.symbol import QwtSymbol
from qwt.text import QwtText


class QwtPlotMarker_PrivateData(QObject):
    def __init__(self):
        QObject.__init__(self)

        self.labelAlignment = Qt.AlignCenter
        self.labelOrientation = Qt.Horizontal
        self.spacing = 2
        self.symbol = None
        self.style = QwtPlotMarker.NoLine
        self.xValue = 0.0
        self.yValue = 0.0
        self.label = QwtText()
        self.pen = QPen()


class QwtPlotMarker(QwtPlotItem):
    """
    A class for drawing markers

    A marker can be a horizontal line, a vertical line,
    a symbol, a label or any combination of them, which can
    be drawn around a center point inside a bounding rectangle.

    The `setSymbol()` member assigns a symbol to the marker.
    The symbol is drawn at the specified point.

    With `setLabel()`, a label can be assigned to the marker.
    The `setLabelAlignment()` member specifies where the label is drawn. All
    the Align*-constants in `Qt.AlignmentFlags` (see Qt documentation)
    are valid. The interpretation of the alignment depends on the marker's
    line style. The alignment refers to the center point of
    the marker, which means, for example, that the label would be printed
    left above the center point if the alignment was set to
    `Qt.AlignLeft | Qt.AlignTop`.

    Line styles:

      * `QwtPlotMarker.NoLine`: No line
      * `QwtPlotMarker.HLine`: A horizontal line
      * `QwtPlotMarker.VLine`: A vertical line
      * `QwtPlotMarker.Cross`: A crosshair
    """

    # enum LineStyle
    NoLine, HLine, VLine, Cross = list(range(4))

    def __init__(self, title=None):
        if title is None:
            title = ""
        if not isinstance(title, QwtText):
            title = QwtText(title)
        QwtPlotItem.__init__(self, title)
        self.__data = QwtPlotMarker_PrivateData()
        self.setZ(30.0)

    @classmethod
    def make(
        cls,
        xvalue=None,
        yvalue=None,
        title=None,
        label=None,
        symbol=None,
        plot=None,
        z=None,
        x_axis=None,
        y_axis=None,
        align=None,
        orientation=None,
        spacing=None,
        linestyle=None,
        color=None,
        width=None,
        style=None,
        antialiased=False,
    ):
        """
        Create and setup a new `QwtPlotMarker` object (convenience function).

        :param xvalue: x position (optional, default: None)
        :type xvalue: float or None
        :param yvalue: y position (optional, default: None)
        :type yvalue: float or None
        :param title: Marker title
        :type title: qwt.text.QwtText or str or None
        :param label: Label text
        :type label: qwt.text.QwtText or str or None
        :param symbol: New symbol
        :type symbol: qwt.symbol.QwtSymbol or None
        :param plot: Plot to attach the curve to
        :type plot: qwt.plot.QwtPlot or None
        :param z: Z-value
        :type z: float or None
        :param int x_axis: curve X-axis (default: QwtPlot.yLeft)
        :param int y_axis: curve Y-axis (default: QwtPlot.xBottom)
        :param align: Alignment of the label
        :type align: Qt.Alignment or None
        :param orientation: Orientation of the label
        :type orientation: Qt.Orientation or None
        :param spacing: Spacing (distance between the position and the label)
        :type spacing: int or None
        :param int linestyle: Line style
        :param color: Pen color
        :type color: QColor or str or None
        :param float width: Pen width
        :param Qt.PenStyle style: Pen style
        :param bool antialiased: if True, enable antialiasing rendering

        .. seealso::

            :py:meth:`setData()`, :py:meth:`setPen()`, :py:meth:`attach()`
        """
        item = cls(title)
        if z is not None:
            item.setZ(z)
        if symbol is not None:
            item.setSymbol(symbol)
        if xvalue is not None:
            item.setXValue(xvalue)
        if yvalue is not None:
            item.setYValue(yvalue)
        if label is not None:
            item.setLabel(label)
        x_axis = QwtPlot.xBottom if x_axis is None else x_axis
        y_axis = QwtPlot.yLeft if y_axis is None else y_axis
        item.setAxes(x_axis, y_axis)
        if align is not None:
            item.setLabelAlignment(align)
        if orientation is not None:
            item.setLabelOrientation(orientation)
        if spacing is not None:
            item.setSpacing(spacing)
        color = qcolor_from_str(color, Qt.black)
        width = 1.0 if width is None else width
        style = Qt.SolidLine if style is None else style
        item.setLinePen(QPen(color, width, style))
        item.setRenderHint(cls.RenderAntialiased, antialiased)
        if linestyle is not None:
            item.setLineStyle(linestyle)
        if plot is not None:
            item.attach(plot)
        return item

    def rtti(self):
        """:return: `QwtPlotItem.Rtti_PlotMarker`"""
        return QwtPlotItem.Rtti_PlotMarker

    def value(self):
        """:return: Value"""
        return QPointF(self.__data.xValue, self.__data.yValue)

    def xValue(self):
        """:return: x Value"""
        return self.__data.xValue

    def yValue(self):
        """:return: y Value"""
        return self.__data.yValue

    def setValue(self, *args):
        """
        Set Value

        .. py:method:: setValue(pos):

            :param QPointF pos: Position

        .. py:method:: setValue(x, y):

            :param float x: x position
            :param float y: y position
        """
        if len(args) == 1:
            (pos,) = args
            self.setValue(pos.x(), pos.y())
        elif len(args) == 2:
            x, y = args
            if x != self.__data.xValue or y != self.__data.yValue:
                self.__data.xValue = x
                self.__data.yValue = y
                self.itemChanged()
        else:
            raise TypeError(
                "%s() takes 1 or 2 argument(s) (%s given)"
                % (self.__class__.__name__, len(args))
            )

    def setXValue(self, x):
        """
        Set X Value

        :param float x: x position
        """
        self.setValue(x, self.__data.yValue)

    def setYValue(self, y):
        """
        Set Y Value

        :param float y: y position
        """
        self.setValue(self.__data.xValue, y)

    def draw(self, painter, xMap, yMap, canvasRect):
        """
        Draw the marker

        :param QPainter painter: Painter
        :param qwt.scale_map.QwtScaleMap xMap: x Scale Map
        :param qwt.scale_map.QwtScaleMap yMap: y Scale Map
        :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates
        """
        pos = QPointF(
            xMap.transform(self.__data.xValue), yMap.transform(self.__data.yValue)
        )
        self.drawLines(painter, canvasRect, pos)
        if self.__data.symbol and self.__data.symbol.style() != QwtSymbol.NoSymbol:
            sz = self.__data.symbol.size()
            width, height = int(sz.width()), int(sz.height())
            clipRect = QRectF(canvasRect.adjusted(-width, -height, width, height))
            if clipRect.contains(pos):
                self.__data.symbol.drawSymbols(painter, [pos])
        self.drawLabel(painter, canvasRect, pos)

    def drawLines(self, painter, canvasRect, pos):
        """
        Draw the lines marker

        :param QPainter painter: Painter
        :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates
        :param QPointF pos: Position of the marker, translated into widget coordinates

        .. seealso::

            :py:meth:`drawLabel()`,
            :py:meth:`qwt.symbol.QwtSymbol.drawSymbol()`
        """
        if self.__data.style == self.NoLine:
            return
        painter.setPen(self.__data.pen)
        if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross):
            y = pos.y()
            painter.drawLine(QLineF(canvasRect.left(), y, canvasRect.right() - 1.0, y))
        if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross):
            x = pos.x()
            painter.drawLine(QLineF(x, canvasRect.top(), x, canvasRect.bottom() - 1.0))

    def drawLabel(self, painter, canvasRect, pos):
        """
        Align and draw the text label of the marker

        :param QPainter painter: Painter
        :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates
        :param QPointF pos: Position of the marker, translated into widget coordinates

        .. seealso::

            :py:meth:`drawLabel()`,
            :py:meth:`qwt.symbol.QwtSymbol.drawSymbol()`
        """
        if self.__data.label.isEmpty():
            return
        align = self.__data.labelAlignment
        alignPos = QPointF(pos)
        symbolOff = QSizeF(0, 0)
        if self.__data.style == QwtPlotMarker.VLine:
            #  In VLine-style the y-position is pointless and
            #  the alignment flags are relative to the canvas
            if bool(self.__data.labelAlignment & Qt.AlignTop):
                alignPos.setY(canvasRect.top())
                align &= ~Qt.AlignTop
                align |= Qt.AlignBottom
            elif bool(self.__data.labelAlignment & Qt.AlignBottom):
                #  In HLine-style the x-position is pointless and
                #  the alignment flags are relative to the canvas
                alignPos.setY(canvasRect.bottom() - 1)
                align &= ~Qt.AlignBottom
                align |= Qt.AlignTop
            else:
                alignPos.setY(canvasRect.center().y())
        elif self.__data.style == QwtPlotMarker.HLine:
            if bool(self.__data.labelAlignment & Qt.AlignLeft):
                alignPos.setX(canvasRect.left())
                align &= ~Qt.AlignLeft
                align |= Qt.AlignRight
            elif bool(self.__data.labelAlignment & Qt.AlignRight):
                alignPos.setX(canvasRect.right() - 1)
                align &= ~Qt.AlignRight
                align |= Qt.AlignLeft
            else:
                alignPos.setX(canvasRect.center().x())
        else:
            if self.__data.symbol and self.__data.symbol.style() != QwtSymbol.NoSymbol:
                symbolOff = QSizeF(self.__data.symbol.size()) + QSizeF(1, 1)
                symbolOff /= 2
        pw2 = self.__data.pen.widthF() / 2.0
        if pw2 == 0.0:
            pw2 = 0.5
        spacing = self.__data.spacing
        xOff = max([pw2, symbolOff.width()])
        yOff = max([pw2, symbolOff.height()])
        textSize = self.__data.label.textSize(painter.font())
        if align & Qt.AlignLeft:
            alignPos.setX(alignPos.x() - (xOff + spacing))
            if self.__data.labelOrientation == Qt.Vertical:
                alignPos.setX(alignPos.x() - textSize.height())
            else:
                alignPos.setX(alignPos.x() - textSize.width())
        elif align & Qt.AlignRight:
            alignPos.setX(alignPos.x() + xOff + spacing)
        else:
            if self.__data.labelOrientation == Qt.Vertical:
                alignPos.setX(alignPos.x() - textSize.height() / 2)
            else:
                alignPos.setX(alignPos.x() - textSize.width() / 2)
        if align & Qt.AlignTop:
            alignPos.setY(alignPos.y() - (yOff + spacing))
            if self.__data.labelOrientation != Qt.Vertical:
                alignPos.setY(alignPos.y() - textSize.height())
        elif align & Qt.AlignBottom:
            alignPos.setY(alignPos.y() + yOff + spacing)
            if self.__data.labelOrientation == Qt.Vertical:
                alignPos.setY(alignPos.y() + textSize.width())
        else:
            if self.__data.labelOrientation == Qt.Vertical:
                alignPos.setY(alignPos.y() + textSize.width() / 2)
            else:
                alignPos.setY(alignPos.y() - textSize.height() / 2)
        painter.translate(alignPos.x(), alignPos.y())
        if self.__data.labelOrientation == Qt.Vertical:
            painter.rotate(-90.0)
        textRect = QRectF(0, 0, textSize.width(), textSize.height())
        self.__data.label.draw(painter, textRect)

    def setLineStyle(self, style):
        """
        Set the line style

        :param int style: Line style

        Line styles:

          * `QwtPlotMarker.NoLine`: No line
          * `QwtPlotMarker.HLine`: A horizontal line
          * `QwtPlotMarker.VLine`: A vertical line
          * `QwtPlotMarker.Cross`: A crosshair

        .. seealso::

            :py:meth:`lineStyle()`
        """
        if style != self.__data.style:
            self.__data.style = style
            self.legendChanged()
            self.itemChanged()

    def lineStyle(self):
        """
        :return: the line style

        .. seealso::

            :py:meth:`setLineStyle()`
        """
        return self.__data.style

    def setSymbol(self, symbol):
        """
        Assign a symbol

        :param qwt.symbol.QwtSymbol symbol: New symbol

        .. seealso::

            :py:meth:`symbol()`
        """
        if symbol != self.__data.symbol:
            self.__data.symbol = symbol
            if symbol is not None:
                self.setLegendIconSize(symbol.boundingRect().size())
            self.legendChanged()
            self.itemChanged()

    def symbol(self):
        """
        :return: the symbol

        .. seealso::

            :py:meth:`setSymbol()`
        """
        return self.__data.symbol

    def setLabel(self, label):
        """
        Set the label

        :param label: Label text
        :type label: qwt.text.QwtText or str

        .. seealso::

            :py:meth:`label()`
        """
        if not isinstance(label, QwtText):
            label = QwtText(label)
        if label != self.__data.label:
            self.__data.label = label
            self.itemChanged()

    def label(self):
        """
        :return: the label

        .. seealso::

            :py:meth:`setLabel()`
        """
        return self.__data.label

    def setLabelAlignment(self, align):
        """
        Set the alignment of the label

        In case of `QwtPlotMarker.HLine` the alignment is relative to the
        y position of the marker, but the horizontal flags correspond to the
        canvas rectangle. In case of `QwtPlotMarker.VLine` the alignment is
        relative to the x position of the marker, but the vertical flags
        correspond to the canvas rectangle.

        In all other styles the alignment is relative to the marker's position.

        :param Qt.Alignment align: Alignment

        .. seealso::

            :py:meth:`labelAlignment()`, :py:meth:`labelOrientation()`
        """
        if align != self.__data.labelAlignment:
            self.__data.labelAlignment = align
            self.itemChanged()

    def labelAlignment(self):
        """
        :return: the label alignment

        .. seealso::

            :py:meth:`setLabelAlignment()`, :py:meth:`setLabelOrientation()`
        """
        return self.__data.labelAlignment

    def setLabelOrientation(self, orientation):
        """
        Set the orientation of the label

        When orientation is `Qt.Vertical` the label is rotated by 90.0 degrees
        (from bottom to top).

        :param Qt.Orientation orientation: Orientation of the label

        .. seealso::

            :py:meth:`labelOrientation()`, :py:meth:`setLabelAlignment()`
        """
        if orientation != self.__data.labelOrientation:
            self.__data.labelOrientation = orientation
            self.itemChanged()

    def labelOrientation(self):
        """
        :return: the label orientation

        .. seealso::

            :py:meth:`setLabelOrientation()`, :py:meth:`labelAlignment()`
        """
        return self.__data.labelOrientation

    def setSpacing(self, spacing):
        """
        Set the spacing

        When the label is not centered on the marker position, the spacing
        is the distance between the position and the label.

        :param int spacing: Spacing

        .. seealso::

            :py:meth:`spacing()`, :py:meth:`setLabelAlignment()`
        """
        if spacing < 0:
            spacing = 0
        if spacing != self.__data.spacing:
            self.__data.spacing = spacing
            self.itemChanged()

    def spacing(self):
        """
        :return: the spacing

        .. seealso::

            :py:meth:`setSpacing()`
        """
        return self.__data.spacing

    def setLinePen(self, *args):
        """
        Build and/or assigna a line pen, depending on the arguments.

        .. py:method:: setLinePen(color, width, style)
            :noindex:

            Build and assign a line pen

            In Qt5 the default pen width is 1.0 ( 0.0 in Qt4 ) what makes it
            non cosmetic (see `QPen.isCosmetic()`). This method signature has
            been introduced to hide this incompatibility.

            :param QColor color: Pen color
            :param float width: Pen width
            :param Qt.PenStyle style: Pen style

        .. py:method:: setLinePen(pen)
            :noindex:

            Specify a pen for the line.

            :param QPen pen: New pen

        .. seealso::

            :py:meth:`pen()`, :py:meth:`brush()`
        """
        if len(args) == 1 and isinstance(args[0], QPen):
            (pen,) = args
        elif len(args) in (1, 2, 3):
            color = args[0]
            width = 0.0
            style = Qt.SolidLine
            if len(args) > 1:
                width = args[1]
                if len(args) > 2:
                    style = args[2]
            pen = QPen(color, width, style)
            self.setLinePen(pen)
        else:
            raise TypeError(
                "%s().setLinePen() takes 1, 2 or 3 argument(s) "
                "(%s given)" % (self.__class__.__name__, len(args))
            )
        if pen != self.__data.pen:
            self.__data.pen = pen
            self.legendChanged()
            self.itemChanged()

    def linePen(self):
        """
        :return: the line pen

        .. seealso::

            :py:meth:`setLinePen()`
        """
        return self.__data.pen

    def boundingRect(self):
        if self.__data.style == QwtPlotMarker.HLine:
            return QRectF(self.__data.xValue, self.__data.yValue, -1.0, 0.0)
        elif self.__data.style == QwtPlotMarker.VLine:
            return QRectF(self.__data.xValue, self.__data.yValue, 0.0, -1.0)
        else:
            return QRectF(self.__data.xValue, self.__data.yValue, 0.0, 0.0)

    def legendIcon(self, index, size):
        """
        :param int index: Index of the legend entry (ignored as there is only one)
        :param QSizeF size: Icon size
        :return: Icon representing the marker on the legend

        .. seealso::

            :py:meth:`qwt.plot.QwtPlotItem.setLegendIconSize()`,
            :py:meth:`qwt.plot.QwtPlotItem.legendData()`
        """
        if size.isEmpty():
            return QwtGraphic()
        icon = QwtGraphic()
        icon.setDefaultSize(size)
        icon.setRenderHint(QwtGraphic.RenderPensUnscaled, True)
        painter = QPainter(icon)
        painter.setRenderHint(
            QPainter.Antialiasing, self.testRenderHint(QwtPlotItem.RenderAntialiased)
        )
        if self.__data.style != QwtPlotMarker.NoLine:
            painter.setPen(self.__data.pen)
            if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross):
                y = 0.5 * size.height()
                painter.drawLine(QLineF(0.0, y, size.width(), y))
            if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross):
                x = 0.5 * size.width()
                painter.drawLine(QLineF(x, 0.0, x, size.height()))
        if self.__data.symbol:
            r = QRect(0, 0, size.width(), size.height())
            self.__data.symbol.drawSymbol(painter, r)
        return icon