1111 |------StateButton
1212"""
1313import 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
1618
1719
1820__all__ = ['TextButton' , 'IconButton' , 'RectButton' , 'RoundButton' , 'StateButton' ]
1921
2022
2123class 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 ):
2326 super (BaseButton , self ).__init__ (parent )
2427 if isinstance (shortCut , str ) and len (shortCut ):
2528 self .setShortcut (QKeySequence (self .tr (shortCut )))
@@ -33,10 +36,10 @@ def __init__(self, width=0, height=0, shortCut="", styleSheet="", tips="", paren
3336 self .setToolTip (tips )
3437 self .setStatusTip (tips )
3538
36- def getState (self ):
39+ def getState (self ) -> bool :
3740 return self .isChecked () if self .isCheckable () else False
3841
39- def slotChangeView (self , ck ):
42+ def slotChangeView (self , ck : bool ):
4043 """Change button view
4144
4245 :param ck:
@@ -47,7 +50,8 @@ def slotChangeView(self, ck):
4750
4851
4952class 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 ):
5155 super (TextButton , self ).__init__ (width , height , shortCut , styleSheet , tips , parent )
5256 self .setCheckable (True )
5357 self .toggled .connect (self .slotChangeView )
@@ -57,7 +61,7 @@ def __init__(self, width=0, height=0, text=("", ""), shortCut="", styleSheet="",
5761 self .text = text
5862 self .setText (self .tr (text [0 ]))
5963
60- def slotChangeView (self , ck ):
64+ def slotChangeView (self , ck : bool ):
6165 """When button is clicked, change button text
6266
6367 :param ck: Button clicked
@@ -68,7 +72,7 @@ def slotChangeView(self, ck):
6872
6973
7074class 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 ):
7276 if not isinstance (icons , (list , tuple )):
7377 raise TypeError ("icons require a list or tuple type" )
7478
@@ -99,7 +103,7 @@ def __init__(self, icons, shortCut="", tips="", parent=None):
99103 else :
100104 print ("Icon size mismatched or icon is not a image!" )
101105
102- def paintEvent (self , ev ):
106+ def paintEvent (self , ev : QPaintEvent ):
103107 pixmap = QPixmap ()
104108 painter = QPainter (self )
105109 idx = self .isChecked ()
@@ -110,7 +114,9 @@ def paintEvent(self, ev):
110114
111115
112116class 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 ):
114120 super (RectButton , self ).__init__ (width , height , shortCut , tips = tips , parent = parent )
115121 self .setCheckable (True )
116122
@@ -123,12 +129,12 @@ def __init__(self, width=0, height=0, text=("", ""), shortCut="", color=(Qt.red,
123129 self .setColor (color )
124130 self .textLength = max (len (self .text [0 ]), len (self .text [1 ]), 1 )
125131
126- def draw (self , painter , rect ):
132+ def draw (self , painter : QPainter , rect : QRect ):
127133 painter .setPen (self .textColor [self .getState ()])
128134 painter .setFont (QFont ("Times New Roman" , min (rect .width () / self .textLength / 0.618 , rect .height () * 0.618 )))
129135 painter .drawText (rect , Qt .AlignCenter , self .tr (self .text [self .getState ()]))
130136
131- def setText (self , text ):
137+ def setText (self , text : Sequence [ str ] ):
132138 if not isinstance (text , (list , tuple )) or len (text ) != 2 :
133139 return False
134140
@@ -139,7 +145,7 @@ def setText(self, text):
139145 self .update ()
140146 return True
141147
142- def setColor (self , colors ):
148+ def setColor (self , colors : Sequence [ Union [ Qt . GlobalColor , QColor ]] ):
143149 if not isinstance (colors , (list , tuple )) or len (colors ) != 2 :
144150 return False
145151
@@ -150,10 +156,10 @@ def setColor(self, colors):
150156 self .textColor = self .drawColor [1 ], self .drawColor [0 ]
151157 self .update ()
152158
153- def getBrush (self ):
159+ def getBrush (self ) -> QBrush :
154160 return QBrush (self .drawColor [self .getState ()], Qt .SolidPattern )
155161
156- def paintEvent (self , ev ):
162+ def paintEvent (self , ev : QPaintEvent ):
157163 painter = QPainter (self )
158164 painter .setRenderHint (QPainter .Antialiasing )
159165 rect = self .rect ()
@@ -168,10 +174,12 @@ def paintEvent(self, ev):
168174
169175
170176class 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 ):
172180 super (RoundButton , self ).__init__ (diameter , diameter , text , shortCut , color , tips , parent )
173181
174- def paintEvent (self , ev ):
182+ def paintEvent (self , ev : QPaintEvent ):
175183 painter = QPainter (self )
176184 painter .setRenderHint (QPainter .Antialiasing )
177185 rect = self .rect ()
@@ -192,14 +200,16 @@ class StateButton(RoundButton):
192200 # Single when state changed
193201 stateChanged = Signal (bool )
194202
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 ):
196206 super (StateButton , self ).__init__ (diameter , text , shortCut , color , tips , parent )
197207 self .setCheckable (False )
198208
199209 # Internal state
200210 self .state = False
201211
202- def getState (self ):
212+ def getState (self ) -> bool :
203213 return self .state
204214
205215 def turnOn (self ):
@@ -212,7 +222,7 @@ def turnOff(self):
212222 self .stateChanged .emit (False )
213223 self .update ()
214224
215- def slotChangeView (self , ck ):
225+ def slotChangeView (self , ck : bool ):
216226 if ck :
217227 self .turnOn ()
218228 else :
0 commit comments