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 QPointF
, QRectF
12 from PyQt5
.QtGui
import QColor
, QFont
, QPainter
, QPixmap
13 from PyQt5
.QtSvg
import QSvgWidget
14 from PyQt5
.QtWidgets
import QPushButton
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 # ---------------------------------------------------------------------------------------------------------------------
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
34 self
.fTopTextColor
= QColor()
35 self
.fTopTextFont
= QFont()
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()
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
):
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
)
84 if self
.fImageNormal
is None:
87 if isinstance(self
.fImageNormal
, QPixmap
):
88 self
.paintEventPixmap(painter
)
90 self
.paintEventSvg(painter
)
96 painter
.setPen(self
.fTopTextColor
)
97 painter
.setBrush(self
.fTopTextColor
)
98 painter
.setFont(self
.fTopTextFont
)
99 painter
.drawText(QPointF(10, 16), self
.fTopText
)
102 def paintEventPixmap(self
, painter
):
103 if not self
.isEnabled():
105 painter
.setOpacity(0.2)
106 painter
.drawPixmap(self
.fImageRect
, self
.fImageNormal
, self
.fImageRect
)
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
)
116 painter
.drawPixmap(self
.fImageRect
, self
.fImageNormal
, self
.fImageRect
)
118 def paintEventSvg(self
, painter
):
119 if not self
.isEnabled():
121 painter
.setOpacity(0.2)
122 self
.fImageNormal
.renderer().render(painter
, self
.fImageRect
)
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
)
132 self
.fImageNormal
.renderer().render(painter
, self
.fImageRect
)
134 # ---------------------------------------------------------------------------------------------------------------------