File: column_symbol.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 (167 lines) | stat: -rw-r--r-- 5,760 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
# -*- 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)

from qtpy.QtCore import QLineF, QObject, QRectF, Qt
from qtpy.QtGui import QPalette, QPolygonF

from qwt.interval import QwtInterval


def qwtDrawBox(p, rect, pal, lw):
    if lw > 0.0:
        if rect.width() == 0.0:
            p.setPen(pal.dark().color())
            p.drawLine(QLineF(rect.topLeft(), rect.bottomLeft()))
            return
        if rect.height() == 0.0:
            p.setPen(pal.dark().color())
            p.drawLine(QLineF(rect.topLeft(), rect.topRight()))
            return
        lw = min([lw, rect.height() / 2.0 - 1.0])
        lw = min([lw, rect.width() / 2.0 - 1.0])
        outerRect = rect.adjusted(0, 0, 1, 1)
        polygon = QPolygonF(outerRect)
        if outerRect.width() > 2 * lw and outerRect.height() > 2 * lw:
            innerRect = outerRect.adjusted(lw, lw, -lw, -lw)
            polygon = polygon.subtracted(innerRect)
        p.setPen(Qt.NoPen)
        p.setBrush(pal.dark())
        p.drawPolygon(polygon)
    windowRect = rect.adjusted(lw, lw, -lw + 1, -lw + 1)
    if windowRect.isValid():
        p.fillRect(windowRect, pal.window())


def qwtDrawPanel(painter, rect, pal, lw):
    if lw > 0.0:
        if rect.width() == 0.0:
            painter.setPen(pal.window().color())
            painter.drawLine(QLineF(rect.topLeft(), rect.bottomLeft()))
            return
        if rect.height() == 0.0:
            painter.setPen(pal.window().color())
            painter.drawLine(QLineF(rect.topLeft(), rect.topRight()))
            return
        lw = min([lw, rect.height() / 2.0 - 1.0])
        lw = min([lw, rect.width() / 2.0 - 1.0])
        outerRect = rect.adjusted(0, 0, 1, 1)
        innerRect = outerRect.adjusted(lw, lw, -lw, -lw)
        lines = [QPolygonF(), QPolygonF()]
        lines[0] += outerRect.bottomLeft()
        lines[0] += outerRect.topLeft()
        lines[0] += outerRect.topRight()
        lines[0] += innerRect.topRight()
        lines[0] += innerRect.topLeft()
        lines[0] += innerRect.bottomLeft()
        lines[1] += outerRect.topRight()
        lines[1] += outerRect.bottomRight()
        lines[1] += outerRect.bottomLeft()
        lines[1] += innerRect.bottomLeft()
        lines[1] += innerRect.bottomRight()
        lines[1] += innerRect.topRight()
        painter.setPen(Qt.NoPen)
        painter.setBrush(pal.light())
        painter.drawPolygon(lines[0])
        painter.setBrush(pal.dark())
        painter.drawPolygon(lines[1])
    painter.fillRect(rect.adjusted(lw, lw, -lw + 1, -lw + 1), pal.window())


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

        self.style = QwtColumnSymbol.Box
        self.frameStyle = QwtColumnSymbol.Raised
        self.lineWidth = 2
        self.palette = QPalette(Qt.gray)


class QwtColumnSymbol(object):
    # enum Style
    NoStyle = -1
    Box = 0
    UserStyle = 1000

    # enum FrameStyle
    NoFrame, Plain, Raised = list(range(3))

    def __init__(self, style):
        self.__data = QwtColumnSymbol_PrivateData()
        self.__data.style = style

    def setStyle(self, style):
        self.__data.style = style

    def style(self):
        return self.__data.style

    def setPalette(self, palette):
        self.__data.palette = palette

    def palette(self):
        return self.__data.palette

    def setFrameStyle(self, frameStyle):
        self.__data.frameStyle = frameStyle

    def frameStyle(self):
        return self.__data.frameStyle

    def setLineWidth(self, width):
        self.__data.lineWidth = width

    def lineWidth(self):
        return self.__data.lineWidth

    def draw(self, painter, rect):
        painter.save()
        if self.__data.style == QwtColumnSymbol.Box:
            self.drawBox(painter, rect)
        painter.restore()

    def drawBox(self, painter, rect):
        r = rect.toRect()
        if self.__data.frameStyle == QwtColumnSymbol.Raised:
            qwtDrawPanel(painter, r, self.__data.palette, self.__data.lineWidth)
        elif self.__data.frameStyle == QwtColumnSymbol.Plain:
            qwtDrawBox(painter, r, self.__data.palette, self.__data.lineWidth)
        else:
            painter.fillRect(r.adjusted(0, 0, 1, 1), self.__data.palette.window())


class QwtColumnRect(object):
    # enum Direction
    LeftToRight, RightToLeft, BottomToTop, TopToBottom = list(range(4))

    def __init__(self):
        self.hInterval = QwtInterval()
        self.vInterval = QwtInterval()
        self.direction = 0

    def toRect(self):
        r = QRectF(
            self.hInterval.minValue(),
            self.vInterval.minValue(),
            self.hInterval.maxValue() - self.hInterval.minValue(),
            self.vInterval.maxValue() - self.vInterval.minValue(),
        )
        r = r.normalized()
        if self.hInterval.borderFlags() & QwtInterval.ExcludeMinimum:
            r.adjust(1, 0, 0, 0)
        if self.hInterval.borderFlags() & QwtInterval.ExcludeMaximum:
            r.adjust(0, 0, -1, 0)
        if self.vInterval.borderFlags() & QwtInterval.ExcludeMinimum:
            r.adjust(0, 1, 0, 0)
        if self.vInterval.borderFlags() & QwtInterval.ExcludeMaximum:
            r.adjust(0, 0, 0, -1)
        return r

    def orientation(self):
        if self.direction in (self.LeftToRight, self.RightToLeft):
            return Qt.Horizontal
        return Qt.Vertical