7
7
import os
8
8
import webbrowser
9
9
from functools import partial , lru_cache , wraps
10
- from typing import (NamedTuple , Callable , Optional , TYPE_CHECKING , List , Any , Sequence , Tuple )
10
+ from typing import (NamedTuple , Callable , Optional , TYPE_CHECKING , List , Any , Sequence , Tuple , Union )
11
11
12
12
from PyQt6 import QtCore
13
13
from PyQt6 .QtGui import (QFont , QColor , QCursor , QPixmap , QImage ,
17
17
QStyle , QDialog , QGroupBox , QButtonGroup , QRadioButton ,
18
18
QFileDialog , QWidget , QToolButton , QPlainTextEdit , QApplication , QToolTip ,
19
19
QGraphicsEffect , QGraphicsScene , QGraphicsPixmapItem , QLayoutItem , QLayout , QMenu ,
20
- QFrame )
20
+ QFrame , QAbstractButton )
21
21
22
22
from electrum .i18n import _
23
23
from electrum .util import (FileImportFailed , FileExportFailed , resource_path , EventListener , event_listener ,
@@ -262,13 +262,13 @@ def top_level_window(self, test_func=None):
262
262
return self .top_level_window_recurse (test_func )
263
263
264
264
def question (self , msg , parent = None , title = None , icon = None , ** kwargs ) -> bool :
265
- Yes , No = QMessageBox .StandardButton .Yes , QMessageBox .StandardButton .No
266
- return Yes == self .msg_box (icon = icon or QMessageBox .Icon .Question ,
265
+ yes , no = QMessageBox .StandardButton .Yes , QMessageBox .StandardButton .No
266
+ return yes == self .msg_box (icon = icon or QMessageBox .Icon .Question ,
267
267
parent = parent ,
268
268
title = title or '' ,
269
269
text = msg ,
270
- buttons = Yes | No ,
271
- defaultButton = No ,
270
+ buttons = yes | no ,
271
+ defaultButton = no ,
272
272
** kwargs )
273
273
274
274
def show_warning (self , msg , parent = None , title = None , ** kwargs ):
@@ -283,22 +283,27 @@ def show_critical(self, msg, parent=None, title=None, **kwargs):
283
283
return self .msg_box (QMessageBox .Icon .Critical , parent ,
284
284
title or _ ('Critical Error' ), msg , ** kwargs )
285
285
286
- def show_message (self , msg , parent = None , title = None , ** kwargs ):
287
- return self .msg_box (QMessageBox .Icon .Information , parent ,
288
- title or _ ('Information' ), msg , ** kwargs )
286
+ def show_message (self , msg , parent = None , title = None , icon = QMessageBox .Icon .Information , ** kwargs ):
287
+ return self .msg_box (icon , parent , title or _ ('Information' ), msg , ** kwargs )
289
288
290
- def msg_box (self , icon , parent , title , text , * , buttons = QMessageBox .StandardButton .Ok ,
291
- defaultButton = QMessageBox .StandardButton .NoButton , rich_text = False ,
292
- checkbox = None ):
289
+ def msg_box (
290
+ self ,
291
+ icon : Union [QMessageBox .Icon , QPixmap ],
292
+ parent : QWidget ,
293
+ title : str ,
294
+ text : str ,
295
+ * ,
296
+ buttons : Union [QMessageBox .StandardButton ,
297
+ List [Union [QMessageBox .StandardButton , Tuple [QAbstractButton , QMessageBox .ButtonRole ]]]] = QMessageBox .StandardButton .Ok ,
298
+ defaultButton : QMessageBox .StandardButton = QMessageBox .StandardButton .NoButton ,
299
+ rich_text : bool = False ,
300
+ checkbox : Optional [bool ] = None
301
+ ):
293
302
parent = parent or self .top_level_window ()
294
- return custom_message_box (icon = icon ,
295
- parent = parent ,
296
- title = title ,
297
- text = text ,
298
- buttons = buttons ,
299
- defaultButton = defaultButton ,
300
- rich_text = rich_text ,
301
- checkbox = checkbox )
303
+ return custom_message_box (
304
+ icon = icon , parent = parent , title = title , text = text , buttons = buttons , defaultButton = defaultButton ,
305
+ rich_text = rich_text , checkbox = checkbox
306
+ )
302
307
303
308
def query_choice (self ,
304
309
msg : Optional [str ],
@@ -327,15 +332,35 @@ def password_dialog(self, msg=None, parent=None):
327
332
return d .run ()
328
333
329
334
330
-
331
- def custom_message_box (* , icon , parent , title , text , buttons = QMessageBox .StandardButton .Ok ,
332
- defaultButton = QMessageBox .StandardButton .NoButton , rich_text = False ,
333
- checkbox = None ):
335
+ def custom_message_box (
336
+ * ,
337
+ icon : Union [QMessageBox .Icon , QPixmap ],
338
+ parent : QWidget ,
339
+ title : str ,
340
+ text : str ,
341
+ buttons : Union [QMessageBox .StandardButton ,
342
+ List [Union [QMessageBox .StandardButton , Tuple [QAbstractButton , QMessageBox .ButtonRole , int ]]]] = QMessageBox .StandardButton .Ok ,
343
+ defaultButton : QMessageBox .StandardButton = QMessageBox .StandardButton .NoButton ,
344
+ rich_text : bool = False ,
345
+ checkbox : Optional [bool ] = None
346
+ ) -> int :
347
+ custom_buttons = []
348
+ standard_buttons = QMessageBox .StandardButton .NoButton
349
+ if buttons :
350
+ if not isinstance (buttons , list ):
351
+ buttons = [buttons ]
352
+ for button in buttons :
353
+ if isinstance (button , QMessageBox .StandardButton ):
354
+ standard_buttons |= button
355
+ else :
356
+ custom_buttons .append (button )
334
357
if type (icon ) is QPixmap :
335
- d = QMessageBox (QMessageBox .Icon .Information , title , str (text ), buttons , parent )
358
+ d = QMessageBox (QMessageBox .Icon .Information , title , str (text ), standard_buttons , parent )
336
359
d .setIconPixmap (icon )
337
360
else :
338
- d = QMessageBox (icon , title , str (text ), buttons , parent )
361
+ d = QMessageBox (icon , title , str (text ), standard_buttons , parent )
362
+ for button , role , _ in custom_buttons :
363
+ d .addButton (button , role )
339
364
d .setWindowModality (Qt .WindowModality .WindowModal )
340
365
d .setDefaultButton (defaultButton )
341
366
if rich_text :
@@ -350,7 +375,11 @@ def custom_message_box(*, icon, parent, title, text, buttons=QMessageBox.Standar
350
375
d .setTextFormat (Qt .TextFormat .PlainText )
351
376
if checkbox is not None :
352
377
d .setCheckBox (checkbox )
353
- return d .exec ()
378
+ result = d .exec ()
379
+ for button , _ , value in custom_buttons :
380
+ if button == d .clickedButton ():
381
+ return value
382
+ return result
354
383
355
384
356
385
class WindowModalDialog (QDialog , MessageBoxMixin ):
0 commit comments