11
11
|------StateButton
12
12
"""
13
13
import os .path
14
- from PySide .QtCore import Signal , Qt , QSize
15
- from PySide .QtGui import QPushButton , QKeySequence , QImageReader , QPixmap , QPainter , QFont , QColor , QBrush , QPen
14
+ from typing import Optional , Union , Tuple , Sequence
15
+ from PySide .QtCore import Signal , Qt , QSize , QRect
16
+ from PySide .QtGui import QPushButton , QKeySequence , QImageReader , QPixmap , QPainter , QFont , QColor , QBrush , QPen , \
17
+ QWidget , QPaintEvent
16
18
17
19
18
20
__all__ = ['TextButton' , 'IconButton' , 'RectButton' , 'RoundButton' , 'StateButton' ]
19
21
20
22
21
23
class BaseButton (QPushButton ):
22
- def __init__ (self , width = 0 , height = 0 , shortCut = "" , styleSheet = "" , tips = "" , parent = None ):
24
+ def __init__ (self , width : int = 0 , height : int = 0 , shortCut : str = "" ,
25
+ styleSheet : str = "" , tips : str = "" , parent : Optional [QWidget ] = None ):
23
26
super (BaseButton , self ).__init__ (parent )
24
27
if isinstance (shortCut , str ) and len (shortCut ):
25
28
self .setShortcut (QKeySequence (self .tr (shortCut )))
@@ -33,10 +36,10 @@ def __init__(self, width=0, height=0, shortCut="", styleSheet="", tips="", paren
33
36
self .setToolTip (tips )
34
37
self .setStatusTip (tips )
35
38
36
- def getState (self ):
39
+ def getState (self ) -> bool :
37
40
return self .isChecked () if self .isCheckable () else False
38
41
39
- def slotChangeView (self , ck ):
42
+ def slotChangeView (self , ck : bool ):
40
43
"""Change button view
41
44
42
45
:param ck:
@@ -47,7 +50,8 @@ def slotChangeView(self, ck):
47
50
48
51
49
52
class TextButton (BaseButton ):
50
- def __init__ (self , width = 0 , height = 0 , text = ("" , "" ), shortCut = "" , styleSheet = "" , tips = "" , parent = None ):
53
+ def __init__ (self , width : int = 0 , height : int = 0 , text : Tuple [str , str ] = ("" , "" ),
54
+ shortCut : str = "" , styleSheet : str = "" , tips : str = "" , parent : Optional [QWidget ] = None ):
51
55
super (TextButton , self ).__init__ (width , height , shortCut , styleSheet , tips , parent )
52
56
self .setCheckable (True )
53
57
self .toggled .connect (self .slotChangeView )
@@ -57,7 +61,7 @@ def __init__(self, width=0, height=0, text=("", ""), shortCut="", styleSheet="",
57
61
self .text = text
58
62
self .setText (self .tr (text [0 ]))
59
63
60
- def slotChangeView (self , ck ):
64
+ def slotChangeView (self , ck : bool ):
61
65
"""When button is clicked, change button text
62
66
63
67
:param ck: Button clicked
@@ -68,7 +72,7 @@ def slotChangeView(self, ck):
68
72
69
73
70
74
class IconButton (BaseButton ):
71
- def __init__ (self , icons , shortCut = "" , tips = "" , parent = None ):
75
+ def __init__ (self , icons : Sequence [ str ] , shortCut : str = "" , tips : str = "" , parent : Optional [ QWidget ] = None ):
72
76
if not isinstance (icons , (list , tuple )):
73
77
raise TypeError ("icons require a list or tuple type" )
74
78
@@ -99,7 +103,7 @@ def __init__(self, icons, shortCut="", tips="", parent=None):
99
103
else :
100
104
print ("Icon size mismatched or icon is not a image!" )
101
105
102
- def paintEvent (self , ev ):
106
+ def paintEvent (self , ev : QPaintEvent ):
103
107
pixmap = QPixmap ()
104
108
painter = QPainter (self )
105
109
idx = self .isChecked ()
@@ -110,7 +114,9 @@ def paintEvent(self, ev):
110
114
111
115
112
116
class RectButton (BaseButton ):
113
- def __init__ (self , width = 0 , height = 0 , text = ("" , "" ), shortCut = "" , color = (Qt .red , Qt .green ), tips = "" , parent = None ):
117
+ def __init__ (self , width : int = 0 , height : int = 0 , text : Tuple [str , str ] = ("" , "" ), shortCut : str = "" ,
118
+ color : Sequence [Union [Qt .GlobalColor , QColor ]] = (Qt .red , Qt .green ),
119
+ tips : str = "" , parent : Optional [QWidget ] = None ):
114
120
super (RectButton , self ).__init__ (width , height , shortCut , tips = tips , parent = parent )
115
121
self .setCheckable (True )
116
122
@@ -123,12 +129,12 @@ def __init__(self, width=0, height=0, text=("", ""), shortCut="", color=(Qt.red,
123
129
self .setColor (color )
124
130
self .textLength = max (len (self .text [0 ]), len (self .text [1 ]), 1 )
125
131
126
- def draw (self , painter , rect ):
132
+ def draw (self , painter : QPainter , rect : QRect ):
127
133
painter .setPen (self .textColor [self .getState ()])
128
134
painter .setFont (QFont ("Times New Roman" , min (rect .width () / self .textLength / 0.618 , rect .height () * 0.618 )))
129
135
painter .drawText (rect , Qt .AlignCenter , self .tr (self .text [self .getState ()]))
130
136
131
- def setText (self , text ):
137
+ def setText (self , text : Sequence [ str ] ):
132
138
if not isinstance (text , (list , tuple )) or len (text ) != 2 :
133
139
return False
134
140
@@ -139,7 +145,7 @@ def setText(self, text):
139
145
self .update ()
140
146
return True
141
147
142
- def setColor (self , colors ):
148
+ def setColor (self , colors : Sequence [ Union [ Qt . GlobalColor , QColor ]] ):
143
149
if not isinstance (colors , (list , tuple )) or len (colors ) != 2 :
144
150
return False
145
151
@@ -150,10 +156,10 @@ def setColor(self, colors):
150
156
self .textColor = self .drawColor [1 ], self .drawColor [0 ]
151
157
self .update ()
152
158
153
- def getBrush (self ):
159
+ def getBrush (self ) -> QBrush :
154
160
return QBrush (self .drawColor [self .getState ()], Qt .SolidPattern )
155
161
156
- def paintEvent (self , ev ):
162
+ def paintEvent (self , ev : QPaintEvent ):
157
163
painter = QPainter (self )
158
164
painter .setRenderHint (QPainter .Antialiasing )
159
165
rect = self .rect ()
@@ -168,10 +174,12 @@ def paintEvent(self, ev):
168
174
169
175
170
176
class RoundButton (RectButton ):
171
- def __init__ (self , diameter = 0 , text = ("" , "" ), shortCut = "" , color = (Qt .red , Qt .green ), tips = "" , parent = None ):
177
+ def __init__ (self , diameter : int = 0 , text : Tuple [str , str ] = ("" , "" ),
178
+ shortCut : str = "" , color : Sequence [Union [Qt .GlobalColor , QColor ]] = (Qt .red , Qt .green ),
179
+ tips : str = "" , parent : Optional [QWidget ] = None ):
172
180
super (RoundButton , self ).__init__ (diameter , diameter , text , shortCut , color , tips , parent )
173
181
174
- def paintEvent (self , ev ):
182
+ def paintEvent (self , ev : QPaintEvent ):
175
183
painter = QPainter (self )
176
184
painter .setRenderHint (QPainter .Antialiasing )
177
185
rect = self .rect ()
@@ -192,14 +200,16 @@ class StateButton(RoundButton):
192
200
# Single when state changed
193
201
stateChanged = Signal (bool )
194
202
195
- def __init__ (self , diameter = 0 , text = ("" , "" ), shortCut = "" , color = (Qt .red , Qt .green ), tips = "" , parent = None ):
203
+ def __init__ (self , diameter : int = 0 , text : Tuple [str , str ] = ("" , "" ),
204
+ shortCut : str = "" , color : Sequence [Union [Qt .GlobalColor , QColor ]] = (Qt .red , Qt .green ),
205
+ tips : str = "" , parent : Optional [QWidget ] = None ):
196
206
super (StateButton , self ).__init__ (diameter , text , shortCut , color , tips , parent )
197
207
self .setCheckable (False )
198
208
199
209
# Internal state
200
210
self .state = False
201
211
202
- def getState (self ):
212
+ def getState (self ) -> bool :
203
213
return self .state
204
214
205
215
def turnOn (self ):
@@ -212,7 +222,7 @@ def turnOff(self):
212
222
self .stateChanged .emit (False )
213
223
self .update ()
214
224
215
- def slotChangeView (self , ck ):
225
+ def slotChangeView (self , ck : bool ):
216
226
if ck :
217
227
self .turnOn ()
218
228
else :
0 commit comments