2 # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ---------------------------------------------------------------------------------------------------------------------
8 from qt_compat
import qt_config
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
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 # ---------------------------------------------------------------------------------------------------------------------
24 class LEDButton(QPushButton
):
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)
49 self
.setColor(self
.BLUE
)
51 def setColor(self
, 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()
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
)
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
)
77 size
= self
.fImage
.sizeHint().width()
78 rect
= QRectF(0, 0, size
, size
)
79 self
.fImage
.renderer().render(painter
, rect
)
81 def _loadImageNowIfNeeded(self
):
83 if self
.fLastColor
== self
.fColor
:
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"
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
108 if img
.endswith(".png"):
109 if not isinstance(self
.fImage
, QPixmap
):
110 self
.fImage
= QPixmap()
112 if not isinstance(self
.fImage
, QSvgWidget
):
113 self
.fImage
= QSvgWidget()
115 self
.fImage
.load(img
)
120 # ---------------------------------------------------------------------------------------------------------------------