Use "proper" colors
[klaudia.git] / src / qpatchbay.py
blob351daa46c0cd6964f01ea05c63228f0f2033c0ea
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
5 # Imports
6 from PyQt4.QtCore import Qt, QRectF
7 from PyQt4.QtGui import QColor, QGraphicsItem, QGraphicsRectItem, QGraphicsTextItem, QGraphicsScene, QPen
8 from PyQt4.QtGui import QFont, QFontMetrics
11 # Testing Box
12 class AudioTestBox(QGraphicsItem):
13 def __init__(self, p_min, p_mout, p_ain, p_aout, name):
14 QGraphicsItem.__init__(self)
16 # Base Variables
17 self.box_width = 50
18 self.box_height = 50
19 self.max_p_size = 50
20 self.max_in_size = 50
21 self.max_out_size = 50
22 self.box_font = QFont("Deja Vu Sans", 8, QFont.Normal)
24 # Make the App Name fit the Box
25 app_name_size = QFontMetrics(self.box_font).width(name)
26 if (app_name_size > self.box_width-10):
27 self.box_width = app_name_size
29 # Get Max Midi Name size
30 if (p_min > p_mout):
31 p_sel = p_min
32 else:
33 p_sel = p_mout
35 for i in range(len(p_sel)):
36 size = 20
37 if (len(p_sel) <= len(p_min)):
38 size += QFontMetrics(self.box_font).width(p_min[i])
39 if (len(p_sel) <= len(p_mout)):
40 size += QFontMetrics(self.box_font).width(p_mout[i])
41 if (size > self.max_p_size):
42 self.max_p_size = size
44 # Get Max Audio Name size
45 if (p_ain > p_aout):
46 p_sel = p_ain
47 else:
48 p_sel = p_aout
50 for i in range(len(p_sel)):
51 size = 20
52 if (len(p_sel) <= len(p_ain)):
53 size += QFontMetrics(self.box_font).width(p_ain[i])
54 if (len(p_sel) <= len(p_aout)):
55 size += QFontMetrics(self.box_font).width(p_aout[i])
56 if (size > self.max_p_size):
57 self.max_p_size = size
59 # Grow Box horizontally if needed
60 if (self.max_p_size > self.box_width-10):
61 self.box_width = self.max_p_size
63 # Grow Box vertically to fit all inputs
64 new_box_height = 30
65 for i in range(len(p_min)+len(p_ain)):
66 new_box_height += 20
68 if (new_box_height > self.box_height):
69 self.box_height = new_box_height
71 # Grow Box vertically to fit all outputs
72 new_box_height = 30
73 for i in range(len(p_mout)+len(p_aout)):
74 new_box_height += 20
76 if (new_box_height > self.box_height):
77 self.box_height = new_box_height
79 # Add main Box
80 self.box = QGraphicsRectItem(0, 0, self.box_width+10, self.box_height, self)
81 self.box.setBrush(QColor(10, 11, 12))
82 self.box.setPen(QPen(Qt.white, 0))
84 # App name
85 self.app_name = QGraphicsTextItem(name, self.box)
86 self.app_name.setFont(self.box_font)
88 # Get Max Midi Input Name size
89 for i in range(len(p_min)):
90 size = QFontMetrics(self.box_font).width(p_min[i])
91 if (size > self.max_in_size):
92 self.max_in_size = size
94 # Get Max Midi Output Name size
95 for i in range(len(p_mout)):
96 size = QFontMetrics(self.box_font).width(p_mout[i])
97 if (size > self.max_out_size):
98 self.max_out_size = size
100 # Get Max Audio Input Name size
101 for i in range(len(p_ain)):
102 size = QFontMetrics(self.box_font).width(p_ain[i])
103 if (size > self.max_in_size):
104 self.max_in_size = size
106 # Get Max Audio Output Name size
107 for i in range(len(p_aout)):
108 size = QFontMetrics(self.box_font).width(p_aout[i])
109 if (size > self.max_out_size):
110 self.max_out_size = size
112 # Show Input ports
113 last_pos = 30
114 for i in range(len(p_ain)):
115 self.tmp_box = QGraphicsRectItem(0, last_pos, self.max_in_size+5, 15, self.box)
116 self.tmp_box.setBrush(Qt.blue)
117 self.tmp_box.setPen(QPen(Qt.darkBlue, 0))
118 self.tmp_txt = QGraphicsTextItem(p_ain[i], self.box)
119 self.tmp_txt.setFont(self.box_font)
120 self.tmp_txt.setPos(0, last_pos-3)
121 last_pos += 20
123 for i in range(len(p_min)):
124 self.tmp_box = QGraphicsRectItem(0, last_pos, self.max_in_size+5, 15, self.box)
125 self.tmp_box.setBrush(Qt.red)
126 self.tmp_box.setPen(QPen(Qt.darkRed, 0))
127 self.tmp_txt = QGraphicsTextItem(p_min[i], self.box)
128 self.tmp_txt.setFont(self.box_font)
129 self.tmp_txt.setPos(0, last_pos-3)
130 last_pos += 20
132 # Show Output ports
133 last_pos = 30
134 for i in range(len(p_aout)):
135 self.tmp_box = QGraphicsRectItem(self.box_width-self.max_out_size+5, last_pos, self.max_out_size+5, 15, self.box)
136 self.tmp_box.setBrush(Qt.blue)
137 self.tmp_box.setPen(QPen(Qt.darkBlue, 0))
138 self.tmp_txt = QGraphicsTextItem(p_aout[i], self.box)
139 self.tmp_txt.setFont(self.box_font)
140 self.tmp_txt.setPos(self.box_width-self.max_out_size+3, last_pos-3)
141 last_pos += 20
143 for i in range(len(p_mout)):
144 self.tmp_box = QGraphicsRectItem(self.box_width-self.max_out_size+5, last_pos, self.max_out_size+5, 15, self.box)
145 self.tmp_box.setBrush(Qt.red)
146 self.tmp_box.setPen(QPen(Qt.darkRed, 0))
147 self.tmp_txt = QGraphicsTextItem(p_mout[i], self.box)
148 self.tmp_txt.setFont(self.box_font)
149 self.tmp_txt.setPos(self.box_width-self.max_out_size+3, last_pos-3)
150 last_pos += 20
152 # Final touches
153 self.box.setZValue(-1)
154 self.box.setFlag(QGraphicsItem.ItemIsMovable)
156 def boundingRect(self):
157 return QRectF(0, 0, 0, 0)
159 def paint(self, painter, option, widget):
160 pass