src/eric7/EricWidgets/EricLed.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
18 18
19 class EricLedType(enum.Enum): 19 class EricLedType(enum.Enum):
20 """ 20 """
21 Class defining the LED types. 21 Class defining the LED types.
22 """ 22 """
23
23 RECTANGULAR = 0 24 RECTANGULAR = 0
24 CIRCULAR = 1 25 CIRCULAR = 1
25 26
26 27
27 class EricLed(QWidget): 28 class EricLed(QWidget):
28 """ 29 """
29 Class implementing a LED widget. 30 Class implementing a LED widget.
30 """ 31 """
31 def __init__(self, parent=None, color=None, shape=EricLedType.CIRCULAR, 32
32 rectRatio=1): 33 def __init__(
34 self, parent=None, color=None, shape=EricLedType.CIRCULAR, rectRatio=1
35 ):
33 """ 36 """
34 Constructor 37 Constructor
35 38
36 @param parent reference to parent widget 39 @param parent reference to parent widget
37 @type QWidget 40 @type QWidget
38 @param color color of the LED 41 @param color color of the LED
39 @type QColor 42 @type QColor
40 @param shape shape of the LED 43 @param shape shape of the LED
41 @type EricLedType 44 @type EricLedType
42 @param rectRatio ratio width to height, if shape is rectangular 45 @param rectRatio ratio width to height, if shape is rectangular
43 @type float 46 @type float
44 """ 47 """
45 super().__init__(parent) 48 super().__init__(parent)
46 49
47 if color is None: 50 if color is None:
48 color = QColor("green") 51 color = QColor("green")
49 52
50 self.__led_on = True 53 self.__led_on = True
51 self.__dark_factor = 300 54 self.__dark_factor = 300
52 self.__offcolor = color.darker(self.__dark_factor) 55 self.__offcolor = color.darker(self.__dark_factor)
53 self.__led_color = color 56 self.__led_color = color
54 self.__framedLed = True 57 self.__framedLed = True
55 self.__shape = shape 58 self.__shape = shape
56 self.__rectRatio = rectRatio 59 self.__rectRatio = rectRatio
57 60
58 self.setColor(color) 61 self.setColor(color)
59 62
60 def paintEvent(self, evt): 63 def paintEvent(self, evt):
61 """ 64 """
62 Protected slot handling the paint event. 65 Protected slot handling the paint event.
63 66
64 @param evt paint event object 67 @param evt paint event object
65 @type QPaintEvent 68 @type QPaintEvent
66 """ 69 """
67 if self.__shape == EricLedType.CIRCULAR: 70 if self.__shape == EricLedType.CIRCULAR:
68 self.__paintRound() 71 self.__paintRound()
69 elif self.__shape == EricLedType.RECTANGULAR: 72 elif self.__shape == EricLedType.RECTANGULAR:
70 self.__paintRectangular() 73 self.__paintRectangular()
71 74
72 def __getBestRoundSize(self): 75 def __getBestRoundSize(self):
73 """ 76 """
74 Private method to calculate the width of the LED. 77 Private method to calculate the width of the LED.
75 78
76 @return new width of the LED (integer) 79 @return new width of the LED (integer)
77 """ 80 """
78 width = min(self.width(), self.height()) 81 width = min(self.width(), self.height())
79 width -= 2 # leave one pixel border 82 width -= 2 # leave one pixel border
80 return width > -1 and width or 0 83 return width > -1 and width or 0
81 84
82 def __paintRound(self): 85 def __paintRound(self):
83 """ 86 """
84 Private method to paint a round raised LED. 87 Private method to paint a round raised LED.
85 """ 88 """
86 # Initialize coordinates, width and height of the LED 89 # Initialize coordinates, width and height of the LED
87 width = self.__getBestRoundSize() 90 width = self.__getBestRoundSize()
88 91
89 # Calculate the gradient for the LED 92 # Calculate the gradient for the LED
90 wh = width / 2 93 wh = width / 2
91 color = self.__led_on and self.__led_color or self.__offcolor 94 color = self.__led_on and self.__led_color or self.__offcolor
92 gradient = QRadialGradient(wh, wh, wh, 0.8 * wh, 0.8 * wh) 95 gradient = QRadialGradient(wh, wh, wh, 0.8 * wh, 0.8 * wh)
93 gradient.setColorAt(0.0, color.lighter(200)) 96 gradient.setColorAt(0.0, color.lighter(200))
94 gradient.setColorAt(0.6, color) 97 gradient.setColorAt(0.6, color)
95 if self.__framedLed: 98 if self.__framedLed:
96 gradient.setColorAt(0.9, color.darker()) 99 gradient.setColorAt(0.9, color.darker())
97 gradient.setColorAt( 100 gradient.setColorAt(1.0, self.palette().color(QPalette.ColorRole.Dark))
98 1.0, self.palette().color(QPalette.ColorRole.Dark))
99 else: 101 else:
100 gradient.setColorAt(1.0, color.darker()) 102 gradient.setColorAt(1.0, color.darker())
101 103
102 # now do the drawing 104 # now do the drawing
103 paint = QPainter(self) 105 paint = QPainter(self)
104 paint.setRenderHint(QPainter.RenderHint.Antialiasing, True) 106 paint.setRenderHint(QPainter.RenderHint.Antialiasing, True)
105 paint.setBrush(QBrush(gradient)) 107 paint.setBrush(QBrush(gradient))
106 paint.setPen(Qt.PenStyle.NoPen) 108 paint.setPen(Qt.PenStyle.NoPen)
107 paint.drawEllipse(1, 1, width, width) 109 paint.drawEllipse(1, 1, width, width)
108 paint.end() 110 paint.end()
109 111
110 def __paintRectangular(self): 112 def __paintRectangular(self):
111 """ 113 """
112 Private method to paint a rectangular raised LED. 114 Private method to paint a rectangular raised LED.
113 """ 115 """
114 # Initialize coordinates, width and height of the LED 116 # Initialize coordinates, width and height of the LED
115 width = self.height() * self.__rectRatio 117 width = self.height() * self.__rectRatio
116 left = max(0, int((self.width() - width) / 2) - 1) 118 left = max(0, int((self.width() - width) / 2) - 1)
117 right = min(int((self.width() + width) / 2), self.width()) 119 right = min(int((self.width() + width) / 2), self.width())
118 height = self.height() 120 height = self.height()
119 121
120 # now do the drawing 122 # now do the drawing
121 painter = QPainter(self) 123 painter = QPainter(self)
122 painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) 124 painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
123 color = self.__led_on and self.__led_color or self.__offcolor 125 color = self.__led_on and self.__led_color or self.__offcolor
124 126
131 painter.setPen(color.darker()) 133 painter.setPen(color.darker())
132 painter.drawLine(left + 1, height - 1, right - 1, height - 1) 134 painter.drawLine(left + 1, height - 1, right - 1, height - 1)
133 painter.drawLine(right - 1, 1, right - 1, height - 1) 135 painter.drawLine(right - 1, 1, right - 1, height - 1)
134 painter.fillRect(left + 1, 1, right - 2, height - 2, QBrush(color)) 136 painter.fillRect(left + 1, 1, right - 2, height - 2, QBrush(color))
135 painter.end() 137 painter.end()
136 138
137 def isOn(self): 139 def isOn(self):
138 """ 140 """
139 Public method to return the LED state. 141 Public method to return the LED state.
140 142
141 @return flag indicating the light state (boolean) 143 @return flag indicating the light state (boolean)
142 """ 144 """
143 return self.__led_on 145 return self.__led_on
144 146
145 def shape(self): 147 def shape(self):
146 """ 148 """
147 Public method to return the LED shape. 149 Public method to return the LED shape.
148 150
149 @return LED shape 151 @return LED shape
150 @rtype EricLedType 152 @rtype EricLedType
151 """ 153 """
152 return self.__shape 154 return self.__shape
153 155
154 def ratio(self): 156 def ratio(self):
155 """ 157 """
156 Public method to return the LED rectangular ratio [= width / height]. 158 Public method to return the LED rectangular ratio [= width / height].
157 159
158 @return LED rectangular ratio (float) 160 @return LED rectangular ratio (float)
159 """ 161 """
160 return self.__rectRatio 162 return self.__rectRatio
161 163
162 def color(self): 164 def color(self):
163 """ 165 """
164 Public method to return the LED color. 166 Public method to return the LED color.
165 167
166 @return color of the LED (QColor) 168 @return color of the LED (QColor)
167 """ 169 """
168 return self.__led_color 170 return self.__led_color
169 171
170 def setOn(self, state): 172 def setOn(self, state):
171 """ 173 """
172 Public method to set the LED to on. 174 Public method to set the LED to on.
173 175
174 @param state new state of the LED (boolean) 176 @param state new state of the LED (boolean)
175 """ 177 """
176 if self.__led_on != state: 178 if self.__led_on != state:
177 self.__led_on = state 179 self.__led_on = state
178 self.update() 180 self.update()
179 181
180 def setShape(self, shape): 182 def setShape(self, shape):
181 """ 183 """
182 Public method to set the LED shape. 184 Public method to set the LED shape.
183 185
184 @param shape new LED shape 186 @param shape new LED shape
185 @type EricLedType 187 @type EricLedType
186 """ 188 """
187 if self.__shape != shape: 189 if self.__shape != shape:
188 self.__shape = shape 190 self.__shape = shape
189 self.update() 191 self.update()
190 192
191 def setRatio(self, ratio): 193 def setRatio(self, ratio):
192 """ 194 """
193 Public method to set the LED rectangular ratio (width / height). 195 Public method to set the LED rectangular ratio (width / height).
194 196
195 @param ratio new LED rectangular ratio (float) 197 @param ratio new LED rectangular ratio (float)
196 """ 198 """
197 if self.__rectRatio != ratio: 199 if self.__rectRatio != ratio:
198 self.__rectRatio = ratio 200 self.__rectRatio = ratio
199 self.update() 201 self.update()
200 202
201 def setColor(self, color): 203 def setColor(self, color):
202 """ 204 """
203 Public method to set the LED color. 205 Public method to set the LED color.
204 206
205 @param color color for the LED (QColor) 207 @param color color for the LED (QColor)
206 """ 208 """
207 if self.__led_color != color: 209 if self.__led_color != color:
208 self.__led_color = color 210 self.__led_color = color
209 self.__offcolor = color.darker(self.__dark_factor) 211 self.__offcolor = color.darker(self.__dark_factor)
210 self.update() 212 self.update()
211 213
212 def setDarkFactor(self, darkfactor): 214 def setDarkFactor(self, darkfactor):
213 """ 215 """
214 Public method to set the dark factor. 216 Public method to set the dark factor.
215 217
216 @param darkfactor value to set for the dark factor (integer) 218 @param darkfactor value to set for the dark factor (integer)
217 """ 219 """
218 if self.__dark_factor != darkfactor: 220 if self.__dark_factor != darkfactor:
219 self.__dark_factor = darkfactor 221 self.__dark_factor = darkfactor
220 self.__offcolor = self.__led_color.darker(darkfactor) 222 self.__offcolor = self.__led_color.darker(darkfactor)
221 self.update() 223 self.update()
222 224
223 def darkFactor(self): 225 def darkFactor(self):
224 """ 226 """
225 Public method to return the dark factor. 227 Public method to return the dark factor.
226 228
227 @return the current dark factor (integer) 229 @return the current dark factor (integer)
228 """ 230 """
229 return self.__dark_factor 231 return self.__dark_factor
230 232
231 def toggle(self): 233 def toggle(self):
232 """ 234 """
233 Public slot to toggle the LED state. 235 Public slot to toggle the LED state.
234 """ 236 """
235 self.setOn(not self.__led_on) 237 self.setOn(not self.__led_on)
236 238
237 def on(self): 239 def on(self):
238 """ 240 """
239 Public slot to set the LED to on. 241 Public slot to set the LED to on.
240 """ 242 """
241 self.setOn(True) 243 self.setOn(True)
242 244
243 def off(self): 245 def off(self):
244 """ 246 """
245 Public slot to set the LED to off. 247 Public slot to set the LED to off.
246 """ 248 """
247 self.setOn(False) 249 self.setOn(False)
248 250
249 def setFramed(self, framed): 251 def setFramed(self, framed):
250 """ 252 """
251 Public slot to set the __framedLed attribute. 253 Public slot to set the __framedLed attribute.
252 254
253 @param framed flag indicating the framed state (boolean) 255 @param framed flag indicating the framed state (boolean)
254 """ 256 """
255 if self.__framedLed != framed: 257 if self.__framedLed != framed:
256 self.__framedLed = framed 258 self.__framedLed = framed
257 self.__off_map = None 259 self.__off_map = None
258 self.__on_map = None 260 self.__on_map = None
259 self.update() 261 self.update()
260 262
261 def isFramed(self): 263 def isFramed(self):
262 """ 264 """
263 Public method to return the framed state. 265 Public method to return the framed state.
264 266
265 @return flag indicating the current framed state (boolean) 267 @return flag indicating the current framed state (boolean)
266 """ 268 """
267 return self.__framedLed 269 return self.__framedLed
268 270
269 def sizeHint(self): 271 def sizeHint(self):
270 """ 272 """
271 Public method to give a hint about our desired size. 273 Public method to give a hint about our desired size.
272 274
273 @return size hint (QSize) 275 @return size hint (QSize)
274 """ 276 """
275 return QSize(18, 18) 277 return QSize(18, 18)
276 278
277 def minimumSizeHint(self): 279 def minimumSizeHint(self):
278 """ 280 """
279 Public method to give a hint about our minimum size. 281 Public method to give a hint about our minimum size.
280 282
281 @return size hint (QSize) 283 @return size hint (QSize)
282 """ 284 """
283 return QSize(18, 18) 285 return QSize(18, 18)
284 286
285 287
286 class EricClickableLed(EricLed): 288 class EricClickableLed(EricLed):
287 """ 289 """
288 Class implementing a clickable LED widget. 290 Class implementing a clickable LED widget.
289 291
290 @signal clicked(QPoint) emitted upon a click on the LED with the 292 @signal clicked(QPoint) emitted upon a click on the LED with the
291 left button 293 left button
292 @signal middleClicked(QPoint) emitted upon a click on the LED with 294 @signal middleClicked(QPoint) emitted upon a click on the LED with
293 the middle button or CTRL and left button 295 the middle button or CTRL and left button
294 """ 296 """
297
295 clicked = pyqtSignal(QPoint) 298 clicked = pyqtSignal(QPoint)
296 middleClicked = pyqtSignal(QPoint) 299 middleClicked = pyqtSignal(QPoint)
297 300
298 def __init__(self, parent=None, color=None, shape=EricLedType.CIRCULAR, 301 def __init__(
299 rectRatio=1): 302 self, parent=None, color=None, shape=EricLedType.CIRCULAR, rectRatio=1
303 ):
300 """ 304 """
301 Constructor 305 Constructor
302 306
303 @param parent reference to parent widget 307 @param parent reference to parent widget
304 @type QWidget 308 @type QWidget
305 @param color color of the LED 309 @param color color of the LED
306 @type QColor 310 @type QColor
307 @param shape shape of the LED 311 @param shape shape of the LED
308 @type EricLedType 312 @type EricLedType
309 @param rectRatio ratio width to height, if shape is rectangular 313 @param rectRatio ratio width to height, if shape is rectangular
310 @type float 314 @type float
311 """ 315 """
312 super().__init__(parent, color, shape, rectRatio) 316 super().__init__(parent, color, shape, rectRatio)
313 317
314 self.setCursor(Qt.CursorShape.PointingHandCursor) 318 self.setCursor(Qt.CursorShape.PointingHandCursor)
315 319
316 def mouseReleaseEvent(self, evt): 320 def mouseReleaseEvent(self, evt):
317 """ 321 """
318 Protected method handling mouse release events. 322 Protected method handling mouse release events.
319 323
320 @param evt mouse event (QMouseEvent) 324 @param evt mouse event (QMouseEvent)
321 """ 325 """
322 if ( 326 if evt.button() == Qt.MouseButton.LeftButton and self.rect().contains(
323 evt.button() == Qt.MouseButton.LeftButton and 327 evt.position().toPoint()
324 self.rect().contains(evt.position().toPoint())
325 ): 328 ):
326 if evt.modifiers() == Qt.KeyboardModifier.ControlModifier: 329 if evt.modifiers() == Qt.KeyboardModifier.ControlModifier:
327 self.middleClicked.emit(evt.globalPosition().toPoint()) 330 self.middleClicked.emit(evt.globalPosition().toPoint())
328 else: 331 else:
329 self.clicked.emit(evt.globalPosition().toPoint()) 332 self.clicked.emit(evt.globalPosition().toPoint())
330 elif ( 333 elif evt.button() == Qt.MouseButton.MiddleButton and self.rect().contains(
331 evt.button() == Qt.MouseButton.MiddleButton and 334 evt.position().toPoint()
332 self.rect().contains(evt.position().toPoint())
333 ): 335 ):
334 self.middleClicked.emit(evt.globalPosition().toPoint()) 336 self.middleClicked.emit(evt.globalPosition().toPoint())
335 else: 337 else:
336 super().mouseReleaseEvent(evt) 338 super().mouseReleaseEvent(evt)

eric ide

mercurial