File: null_paintdevice.py

package info (click to toggle)
python-qwt 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,396 kB
  • sloc: python: 12,138; makefile: 19; sh: 10
file content (310 lines) | stat: -rw-r--r-- 9,144 bytes parent folder | download | duplicates (3)
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
# -*- 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)

"""
QwtNullPaintDevice
------------------

.. autoclass:: QwtNullPaintDevice
   :members:
"""

import os

from qtpy.QtCore import QObject
from qtpy.QtGui import QPaintDevice, QPaintEngine, QPainterPath

QT_API = os.environ["QT_API"]


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

        self.mode = QwtNullPaintDevice.NormalMode


class QwtNullPaintDevice_PaintEngine(QPaintEngine):
    def __init__(self, paintdevice):
        super(QwtNullPaintDevice_PaintEngine, self).__init__(QPaintEngine.AllFeatures)
        self.__paintdevice = paintdevice

    def begin(self, paintdevice):
        self.setActive(True)
        return True

    def end(self):
        self.setActive(False)
        return True

    def type(self):
        return QPaintEngine.User

    def drawRects(self, rects, rectCount=None):
        if rectCount is None:
            rectCount = len(rects)
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() != QwtNullPaintDevice.NormalMode:
            try:
                QPaintEngine.drawRects(self, rects, rectCount)
            except TypeError:
                QPaintEngine.drawRects(self, rects)
            return
        device.drawRects(rects, rectCount)

    def drawLines(self, lines, lineCount=None):
        if lineCount is None:
            lineCount = len(lines)
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() != QwtNullPaintDevice.NormalMode and QT_API.startswith("pyqt"):
            try:
                QPaintEngine.drawLines(self, lines, lineCount)
            except TypeError:
                QPaintEngine.drawLines(self, lines)
            return
        device.drawLines(lines, lineCount)

    def drawEllipse(self, rect):
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() != QwtNullPaintDevice.NormalMode:
            QPaintEngine.drawEllipse(self, rect)
            return
        device.drawEllipse(rect)

    def drawPath(self, path):
        device = self.nullDevice()
        if device is None:
            return
        device.drawPath(path)

    def drawPoints(self, points, pointCount=None):
        if pointCount is None:
            pointCount = len(points)
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() != QwtNullPaintDevice.NormalMode:
            try:
                QPaintEngine.drawPoints(self, points, pointCount)
            except TypeError:
                QPaintEngine.drawPoints(self, points)
            return
        device.drawPoints(points, pointCount)

    def drawPolygon(self, *args):
        if len(args) == 3:
            points, pointCount, mode = args
        elif len(args) == 2:
            points, mode = args
            pointCount = len(points)
        else:
            raise TypeError("Unexpected arguments")
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() == QwtNullPaintDevice.PathMode:
            path = QPainterPath()
            if pointCount > 0:
                path.moveTo(points[0])
                for i in range(1, pointCount):
                    path.lineTo(points[i])
                if mode != QPaintEngine.PolylineMode:
                    path.closeSubpath()
            device.drawPath(path)
            return
        device.drawPolygon(points, pointCount, mode)

    def drawPixmap(self, rect, pm, subRect):
        device = self.nullDevice()
        if device is None:
            return
        device.drawPixmap(rect, pm, subRect)

    def drawTextItem(self, pos, textItem):
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() != QwtNullPaintDevice.NormalMode:
            QPaintEngine.drawTextItem(self, pos, textItem)
            return
        device.drawTextItem(pos, textItem)

    def drawTiledPixmap(self, rect, pixmap, subRect):
        device = self.nullDevice()
        if device is None:
            return
        if device.mode() != QwtNullPaintDevice.NormalMode:
            QPaintEngine.drawTiledPixmap(self, rect, pixmap, subRect)
            return
        device.drawTiledPixmap(rect, pixmap, subRect)

    def drawImage(self, rect, image, subRect, flags):
        device = self.nullDevice()
        if device is None:
            return
        device.drawImage(rect, image, subRect, flags)

    def updateState(self, state):
        device = self.nullDevice()
        if device is None:
            return
        device.updateState(state)

    def nullDevice(self):
        if not self.isActive():
            return
        return self.__paintdevice


class QwtNullPaintDevice(QPaintDevice):
    """
    A null paint device doing nothing

    Sometimes important layout/rendering geometries are not
    available or changeable from the public Qt class interface.
    ( f.e hidden in the style implementation ).

    `QwtNullPaintDevice` can be used to manipulate or filter out
    this information by analyzing the stream of paint primitives.

    F.e. `QwtNullPaintDevice` is used by `QwtPlotCanvas` to identify
    styled backgrounds with rounded corners.

    Modes:

        * `NormalMode`:

           All vector graphic primitives are painted by
           the corresponding draw methods

        * `PolygonPathMode`:

           Vector graphic primitives ( beside polygons ) are mapped to a
           `QPainterPath` and are painted by `drawPath`. In `PolygonPathMode`
           mode only a few draw methods are called:

               - `drawPath()`
               - `drawPixmap()`
               - `drawImage()`
               - `drawPolygon()`

        * `PathMode`:

           Vector graphic primitives are mapped to a `QPainterPath`
           and are painted by `drawPath`. In `PathMode` mode
           only a few draw methods are called:

               - `drawPath()`
               - `drawPixmap()`
               - `drawImage()`
    """

    # enum Mode
    NormalMode, PolygonPathMode, PathMode = list(range(3))

    def __init__(self):
        super(QwtNullPaintDevice, self).__init__()
        self.__engine = None
        self.__data = QwtNullPaintDevice_PrivateData()

    def setMode(self, mode):
        """
        Set the render mode

        :param int mode: New mode

        .. seealso::

            :py:meth:`mode()`
        """
        self.__data.mode = mode

    def mode(self):
        """
        :return: Render mode

        .. seealso::

            :py:meth:`setMode()`
        """
        return self.__data.mode

    def paintEngine(self):
        if self.__engine is None:
            self.__engine = QwtNullPaintDevice_PaintEngine(self)
        return self.__engine

    def metric(self, deviceMetric):
        if deviceMetric == QPaintDevice.PdmWidth:
            value = self.sizeMetrics().width()
        elif deviceMetric == QPaintDevice.PdmHeight:
            value = self.sizeMetrics().height()
        elif deviceMetric == QPaintDevice.PdmNumColors:
            value = 0xFFFFFFFF
        elif deviceMetric == QPaintDevice.PdmDepth:
            value = 32
        elif deviceMetric in (
            QPaintDevice.PdmPhysicalDpiX,
            QPaintDevice.PdmPhysicalDpiY,
            QPaintDevice.PdmDpiY,
            QPaintDevice.PdmDpiX,
        ):
            value = 72
        elif deviceMetric == QPaintDevice.PdmWidthMM:
            value = round(
                self.metric(QPaintDevice.PdmWidth)
                * 25.4
                / self.metric(QPaintDevice.PdmDpiX)
            )
        elif deviceMetric == QPaintDevice.PdmHeightMM:
            value = round(
                self.metric(QPaintDevice.PdmHeight)
                * 25.4
                / self.metric(QPaintDevice.PdmDpiY)
            )
        else:
            value = 0
        return value

    def drawRects(self, rects, rectCount):
        pass

    def drawLines(self, lines, lineCount):
        pass

    def drawEllipse(self, rect):
        pass

    def drawPath(self, path):
        pass

    def drawPoints(self, points, pointCount):
        pass

    def drawPolygon(self, points, pointCount, mode):
        pass

    def drawPixmap(self, rect, pm, subRect):
        pass

    def drawTextItem(self, pos, textItem):
        pass

    def drawTiledPixmap(self, rect, pm, subRect):
        pass

    def drawImage(self, rect, image, subRect, flags):
        pass

    def updateState(self, state):
        pass