Cleanup
[carla.git] / source / frontend / widgets / ledbutton.py
blob64fa56c95352c5b231c03e8373dedd355fdfe886
1 #!/usr/bin/env python3
2 # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ---------------------------------------------------------------------------------------------------------------------
6 # Imports (Global)
8 from qt_compat import qt_config
10 if qt_config == 5:
11 from PyQt5.QtCore import QRectF
12 from PyQt5.QtGui import QPainter, QPixmap
13 from PyQt5.QtSvg import QSvgWidget
14 from PyQt5.QtWidgets import QPushButton
15 elif qt_config == 6:
16 from PyQt6.QtCore import QRectF
17 from PyQt6.QtGui import QPainter, QPixmap
18 from PyQt6.QtSvgWidgets import QSvgWidget
19 from PyQt6.QtWidgets import QPushButton
21 # ---------------------------------------------------------------------------------------------------------------------
22 # Widget Class
24 class LEDButton(QPushButton):
25 # unset
26 UNSET = -1
27 # normal
28 OFF = 0
29 BLUE = 1
30 GREEN = 2
31 RED = 3
32 YELLOW = 4
33 # extra
34 CALF = 5
36 def __init__(self, parent):
37 QPushButton.__init__(self, parent)
39 self.fLastColor = self.UNSET
40 self.fColor = self.UNSET
42 self.fImage = QSvgWidget()
43 self.fImage.load(":/scalable/led_off.svg")
44 self.fRect = QRectF(self.fImage.rect())
46 self.setCheckable(True)
47 self.setText("")
49 self.setColor(self.BLUE)
51 def setColor(self, color):
52 self.fColor = color
54 if color == self.CALF:
55 self.fLastColor = self.UNSET
57 if self._loadImageNowIfNeeded():
58 #if isinstance(self.fImage, QPixmap):
59 #size = self.fImage.width()
60 #else:
61 #size = self.fImage.sizeHint().width()
63 self.fRect = QRectF(self.fImage.rect())
64 self.setFixedSize(self.fImage.size())
66 def paintEvent(self, event):
67 painter = QPainter(self)
68 event.accept()
70 self._loadImageNowIfNeeded()
72 if isinstance(self.fImage, QPixmap):
73 size = self.fImage.width()
74 rect = QRectF(0, 0, size, size)
75 painter.drawPixmap(rect, self.fImage, rect)
76 else:
77 size = self.fImage.sizeHint().width()
78 rect = QRectF(0, 0, size, size)
79 self.fImage.renderer().render(painter, rect)
81 def _loadImageNowIfNeeded(self):
82 if self.isChecked():
83 if self.fLastColor == self.fColor:
84 return False
85 if self.fColor == self.OFF:
86 img = ":/scalable/led_off.svg"
87 elif self.fColor == self.BLUE:
88 img = ":/scalable/led_blue.svg"
89 elif self.fColor == self.GREEN:
90 img = ":/scalable/led_green.svg"
91 elif self.fColor == self.RED:
92 img = ":/scalable/led_red.svg"
93 elif self.fColor == self.YELLOW:
94 img = ":/scalable/led_yellow.svg"
95 elif self.fColor == self.CALF:
96 img = ":/bitmaps/led_calf_on.png"
97 else:
98 return False
99 self.fLastColor = self.fColor
101 elif self.fLastColor != self.OFF:
102 img = ":/bitmaps/led_calf_off.png" if self.fColor == self.CALF else ":/scalable/led_off.svg"
103 self.fLastColor = self.OFF
105 else:
106 return False
108 if img.endswith(".png"):
109 if not isinstance(self.fImage, QPixmap):
110 self.fImage = QPixmap()
111 else:
112 if not isinstance(self.fImage, QSvgWidget):
113 self.fImage = QSvgWidget()
115 self.fImage.load(img)
116 self.update()
118 return True
120 # ---------------------------------------------------------------------------------------------------------------------