@@ -147,7 +147,64 @@ def editorEvent(
147
147
return super ().editorEvent (event , model , option , index )
148
148
149
149
150
- class StatefulButton (QPushButton ):
150
+ class ColoredButton (QPushButton ):
151
+ """A QPushButton that can change color."""
152
+
153
+ def __init__ (
154
+ self ,
155
+ text : str ,
156
+ color : QColor | None = None ,
157
+ parent : QWidget | None = None ,
158
+ ** kwargs ,
159
+ ):
160
+ """
161
+ Initialize the ColoredButton.
162
+
163
+ Parameters
164
+ ----------
165
+ text : str
166
+ The text to be displayed.
167
+ color : QColor, optional
168
+ The color to use for the button.
169
+ parent : QWidget
170
+ The parent widget.
171
+ **kwargs : dict
172
+ Arbitrary keyword arguments (passed to QPushButton).
173
+ """
174
+ super ().__init__ (text , parent , ** kwargs )
175
+ self ._original_color = self .palette ().color (QPalette .Button )
176
+ if color is not None :
177
+ self .setColor (color )
178
+
179
+ def setColor (self , color : QColor | None ) -> None :
180
+ """
181
+ Set the color of the button.
182
+
183
+ Parameters
184
+ ----------
185
+ color : QColor, optional
186
+ The new color of the button. If None, the color will be reset to it's
187
+ default color.
188
+ """
189
+ palette = self .palette ()
190
+ palette .setColor (QPalette .Button , color or self ._original_color )
191
+ self .setPalette (palette )
192
+ self .setAutoFillBackground (True )
193
+ self .update ()
194
+
195
+ def color (self ) -> QColor :
196
+ """
197
+ Return the color of the button.
198
+
199
+ Returns
200
+ -------
201
+ QColor
202
+ The color of the button.
203
+ """
204
+ return self .palette ().color (QPalette .Button )
205
+
206
+
207
+ class StatefulButton (ColoredButton ):
151
208
"""A QPushButton that maintains an active/inactive state."""
152
209
153
210
clickedWhileActive = Signal () # type: Signal
@@ -166,8 +223,10 @@ def __init__(
166
223
textInactive : str | None = None ,
167
224
active : bool = False ,
168
225
parent : QWidget | None = None ,
226
+ ** kwargs : dict [str , Any ],
169
227
):
170
- """Initialize the StatefulButton with the specified active state.
228
+ """
229
+ Initialize the StatefulButton with the specified active state.
171
230
172
231
Parameters
173
232
----------
@@ -179,11 +238,18 @@ def __init__(
179
238
Initial state of the button (default is False).
180
239
parent : QWidget
181
240
The parent widget.
241
+ **kwargs : dict
242
+ Arbitrary keyword arguments (passed to ColoredButton).
182
243
"""
183
244
self ._isActive = active
184
245
self ._textActive = textActive or ''
185
246
self ._textInactive = textInactive or ''
186
- super ().__init__ (self ._textActive if active else self ._textInactive , parent )
247
+ super ().__init__ (
248
+ text = self ._textActive if active else self ._textInactive ,
249
+ color = None ,
250
+ parent = parent ,
251
+ ** kwargs ,
252
+ )
187
253
188
254
self .clicked .connect (self ._onClick )
189
255
self .stateChanged .connect (self ._onStateChange )
@@ -281,7 +347,10 @@ def _onClick(self):
281
347
@Slot (bool )
282
348
def _onStateChange (self , state : bool ):
283
349
"""Handle the state change event."""
284
- self .setText (self ._textActive if state is True else self ._textInactive )
350
+ if state is True :
351
+ self .setText (self ._textActive )
352
+ if state is False :
353
+ self .setText (self ._textInactive )
285
354
286
355
287
356
class UseTokenCache (IntEnum ):
@@ -913,11 +982,12 @@ def paintEvent(self, event: QPaintEvent):
913
982
# Determine colors based on state
914
983
palette = self .palette ()
915
984
if not self .isEnabled ():
916
- color_background = palette .shadow ().color ()
917
- color_foreground = palette .midlight ().color ()
985
+ color_background = palette .dark ().color ()
986
+ color_background .setAlphaF (0.6 )
987
+ color_foreground = palette .window ().color ()
918
988
elif self .isChecked ():
919
989
color_background = palette .highlight ().color ()
920
- color_foreground = palette .highlightedText ().color ()
990
+ color_foreground = palette .light ().color ()
921
991
else :
922
992
color_background = palette .dark ().color ()
923
993
color_foreground = palette .light ().color ()
0 commit comments