-
Notifications
You must be signed in to change notification settings - Fork 65
/
bar_widget.py
76 lines (58 loc) · 1.99 KB
/
bar_widget.py
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
from PySide import QtGui
class BarWidget(QtGui.QWidget):
def __init__(self):
super(BarWidget, self).__init__()
self.bars_number = 16
self.bars = [1] * self.bars_number
self.padding = 2
self.resolution = 255
self.setMinimumSize(240, 320)
def setBars(self, bars):
self.bars_number = len(bars)
for index, value in enumerate(bars):
if value > self.resolution:
bars[index] = self.resolution
self.bars = bars
self.update()
def paintEvent(self, e):
painter = QtGui.QPainter()
painter.begin(self)
self.drawBars(painter)
painter.end()
def drawBars(self, painter):
size = self.size()
width = size.width()
height = size.height()
bar_width = float(width - self.padding) / self.bars_number
color = QtGui.QColor(0, 0, 0)
painter.setPen(color)
painter.setBrush(color)
painter.drawRect(0, 0, width, height)
for bar, value in enumerate(self.bars):
bar_height = (height - self.padding) * value / self.resolution
if not bar_height:
bar_height = 1
painter.setBrush(self.barColor(bar))
painter.drawRect(
bar * bar_width + self.padding,
height - bar_height,
bar_width - self.padding,
bar_height - self.padding)
def barColor(self, bar):
position = int((bar + 0.5) * 255 / self.bars_number)
return self.palette(position)
def blue2red(self, position):
position &= 0xFF
if position < 128:
return QtGui.QColor(0, position * 2, 255 - position * 2)
else:
position -= 128
return QtGui.QColor(position * 2, 255 - position * 2, 0)
palette = blue2red
def main():
import sys
app = QtGui.QApplication(sys.argv)
BarWidget().show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()