-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcnQPushButton.py
149 lines (125 loc) · 5.8 KB
/
cnQPushButton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: UTF-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Copyright 2018-2024 Gauthier Brière (gauthier.briere "at" gmail.com) '
' '
' This file is part of cn5X++ '
' '
' cn5X++ is free software: you can redistribute it and/or modify it '
' under the terms of the GNU General Public License as published by '
' the Free Software Foundation, either version 3 of the License, or '
' (at your option) any later version. '
' '
' cn5X++ is distributed in the hope that it will be useful, but '
' WITHOUT ANY WARRANTY; without even the implied warranty of '
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the '
' GNU General Public License for more details. '
' '
' You should have received a copy of the GNU General Public License '
' along with this program. If not, see <http://www.gnu.org/licenses/>. '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import sys, os
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import Qt, pyqtSignal, QEvent, QResource, QSize
class cnQPushButton(QtWidgets.QPushButton):
'''
QPushButton avec gestion d'images en fonction de l'etat
'''
keyPressed = pyqtSignal(QtGui.QKeyEvent)
mousePress = pyqtSignal(QtWidgets.QPushButton, QtGui.QMouseEvent)
mouseRelease = pyqtSignal(QtWidgets.QPushButton, QtGui.QMouseEvent)
mouseDoubleClick = pyqtSignal(QtGui.QMouseEvent)
def __init__(self, parent=None):
super(cnQPushButton, self).__init__(parent=parent)
self.installEventFilter(self)
self.__myName = ""
self.__mouseIsDown = False
self.__buttonStatus = False
# Chemin des images dans le fichier de resources
self.__imagePath = os.path.join(os.path.dirname(__file__), "images/")
self.icon = QtGui.QIcon()
self.iconDown = QtGui.QIcon()
self.iconLight = QtGui.QIcon()
self.__imagesOk = False
def eventFilter(self, object, event):
'''
On utilise eventFilter() pour "trapper" un evennement unique au plus tot apres la creation
du controle car le nom n'est pas definit lors de l'appel d'__init() et je n'ai pas reussi
a recuperer le signal objectNameChanged.
'''
if event.type() == QEvent.Type.DynamicPropertyChange:
self.__myName = object.objectName()
pictureBaseName = self.__imagePath + self.__myName.split("_")[0]
if QResource(pictureBaseName + ".svg").isValid():
self.icon.addPixmap (QtGui.QPixmap(pictureBaseName + ".svg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.On)
if QResource(pictureBaseName + "_down.svg").isValid():
self.iconDown.addPixmap (QtGui.QPixmap(pictureBaseName + "_down.svg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.On)
else:
self.iconDown.addPixmap (QtGui.QPixmap(pictureBaseName + ".svg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.On)
if QResource(pictureBaseName + "_light.svg").isValid():
self.iconLight.addPixmap(QtGui.QPixmap(pictureBaseName + "_light.svg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.On)
else:
self.iconLight.addPixmap (QtGui.QPixmap(pictureBaseName + ".svg"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.On)
self.__imagesOk = True
if event.type() == QEvent.Type.EnabledChange:
if self.__imagesOk:
if self.isEnabled():
if self.__buttonStatus:
self.setIcon(self.iconLight)
else:
self.setIcon(self.icon)
else:
self.setIcon(self.icon)
if event.type() == QEvent.Type.Resize:
self.setIconSize(QSize(self.size().width()-2, self.size().height()-2))
return False
def changeIcon(self, icon):
newIcon = QtGui.QIcon()
newIcon.addPixmap(QtGui.QPixmap(icon), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.On)
self.setIcon(newIcon)
def mousePressEvent(self, e):
super(cnQPushButton, self).mousePressEvent(e)
if e.button() == Qt.MouseButton.LeftButton:
self.__mouseIsDown = True
if self.__imagesOk:
self.setIcon(self.iconDown)
self.mousePress.emit(self, e)
def mouseReleaseEvent(self, e):
if e.button() == Qt.MouseButton.LeftButton:
self.__mouseIsDown = False
if self.isCheckable():
self.__buttonStatus = True
if self.__imagesOk:
if self.isEnabled():
if self.__buttonStatus:
self.setIcon(self.iconLight)
else:
self.setIcon(self.icon)
else:
self.setIcon(self.icon)
self.mouseRelease.emit(self, e)
super(cnQPushButton, self).mouseReleaseEvent(e)
def setButtonStatus(self, value: bool):
self.__buttonStatus = value
if not self.__mouseIsDown:
if self.__imagesOk:
if self.isEnabled():
if self.__buttonStatus:
self.setIcon(self.iconLight)
else:
self.setIcon(self.icon)
else:
self.setIcon(self.icon)
def isMouseDown(self):
return self.__mouseIsDown
def getButtonStatus(self):
return self.__buttonStatus
def name(self):
return self.__myName
def mouseDoubleClickEvent(self, e):
super(cnQPushButton, self).mouseDoubleClickEvent(e)
self.mouseDoubleClick.emit(e)
def keyPressEvent(self, e):
super(cnQPushButton, self).keyPressEvent(e)
self.keyPressed.emit(e)