Cleanup
[carla.git] / source / frontend / widgets / scalablebutton.py
blob494b24934b043873b211f1f13ab5d7addda2463d
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 QPointF, QRectF
12 from PyQt5.QtGui import QColor, QFont, QPainter, QPixmap
13 from PyQt5.QtSvg import QSvgWidget
14 from PyQt5.QtWidgets import QPushButton
15 elif qt_config == 6:
16 from PyQt6.QtCore import QPointF, QRectF
17 from PyQt6.QtGui import QColor, QFont, QPainter, QPixmap
18 from PyQt6.QtSvgWidgets import QSvgWidget
19 from PyQt6.QtWidgets import QPushButton
21 # ---------------------------------------------------------------------------------------------------------------------
22 # Widget Class
24 class ScalableButton(QPushButton):
25 def __init__(self, parent):
26 QPushButton.__init__(self, parent)
28 self.fImageNormal = None
29 self.fImageDown = None
30 self.fImageHover = None
31 self.fImageRect = QRectF()
32 self.fIsHovered = False
33 self.fTopText = ""
34 self.fTopTextColor = QColor()
35 self.fTopTextFont = QFont()
36 self.setText("")
38 def setPixmaps(self, normal, down, hover):
39 self.fImageNormal = QPixmap()
40 self.fImageDown = QPixmap()
41 self.fImageHover = QPixmap()
42 self._loadImages(normal, down, hover)
44 def setSvgs(self, normal, down, hover):
45 self.fImageNormal = QSvgWidget()
46 self.fImageDown = QSvgWidget()
47 self.fImageHover = QSvgWidget()
48 self._loadImages(normal, down, hover)
50 def _loadImages(self, normal, down, hover):
51 self.fImageNormal.load(normal)
52 self.fImageDown.load(down)
53 self.fImageHover.load(hover)
55 if isinstance(self.fImageNormal, QPixmap):
56 width = self.fImageNormal.width()
57 height = self.fImageNormal.height()
58 else:
59 width = self.fImageNormal.sizeHint().width()
60 height = self.fImageNormal.sizeHint().height()
62 self.fImageRect = QRectF(0, 0, width, height)
64 self.setMinimumSize(width, height)
65 self.setMaximumSize(width, height)
67 def setTopText(self, text, color, font):
68 self.fTopText = text
69 self.fTopTextColor = color
70 self.fTopTextFont = font
72 def enterEvent(self, event):
73 self.fIsHovered = True
74 QPushButton.enterEvent(self, event)
76 def leaveEvent(self, event):
77 self.fIsHovered = False
78 QPushButton.leaveEvent(self, event)
80 def paintEvent(self, event):
81 painter = QPainter(self)
82 event.accept()
84 if self.fImageNormal is None:
85 return
87 if isinstance(self.fImageNormal, QPixmap):
88 self.paintEventPixmap(painter)
89 else:
90 self.paintEventSvg(painter)
92 if not self.fTopText:
93 return
95 painter.save()
96 painter.setPen(self.fTopTextColor)
97 painter.setBrush(self.fTopTextColor)
98 painter.setFont(self.fTopTextFont)
99 painter.drawText(QPointF(10, 16), self.fTopText)
100 painter.restore()
102 def paintEventPixmap(self, painter):
103 if not self.isEnabled():
104 painter.save()
105 painter.setOpacity(0.2)
106 painter.drawPixmap(self.fImageRect, self.fImageNormal, self.fImageRect)
107 painter.restore()
109 elif self.isChecked() or self.isDown():
110 painter.drawPixmap(self.fImageRect, self.fImageDown, self.fImageRect)
112 elif self.fIsHovered:
113 painter.drawPixmap(self.fImageRect, self.fImageHover, self.fImageRect)
115 else:
116 painter.drawPixmap(self.fImageRect, self.fImageNormal, self.fImageRect)
118 def paintEventSvg(self, painter):
119 if not self.isEnabled():
120 painter.save()
121 painter.setOpacity(0.2)
122 self.fImageNormal.renderer().render(painter, self.fImageRect)
123 painter.restore()
125 elif self.isChecked() or self.isDown():
126 self.fImageDown.renderer().render(painter, self.fImageRect)
128 elif self.fIsHovered:
129 self.fImageHover.renderer().render(painter, self.fImageRect)
131 else:
132 self.fImageNormal.renderer().render(painter, self.fImageRect)
134 # ---------------------------------------------------------------------------------------------------------------------