1''' Simple extension of Gtk.MessageDialog for consistent formating. Also 2 supports remembering the dialog result. 3''' 4 5from gi.repository import Gtk 6 7from mcomix.preferences import prefs 8 9 10class MessageDialog(Gtk.MessageDialog): 11 12 def __init__(self, parent, flags=0, message_type=0, buttons=0): 13 ''' Creates a dialog window. 14 @param parent: Parent window 15 @param flags: Dialog flags 16 @param type: Dialog icon/type 17 @param buttons: Dialog buttons. Can only be a predefined BUTTONS_XXX constant. 18 ''' 19 super(MessageDialog, self).__init__( 20 message_type=message_type, buttons=buttons, 21 modal=flags & Gtk.DialogFlags.MODAL, 22 destroy_with_parent=flags & Gtk.DialogFlags.DESTROY_WITH_PARENT, 23 ) 24 self.set_transient_for(parent) 25 26 #: Unique dialog identifier (for storing 'Do not ask again') 27 self.dialog_id = None 28 #: List of response IDs that should be remembered 29 self.choices = [] 30 #: Automatically destroy dialog after run? 31 self.auto_destroy = True 32 33 self.remember_checkbox = Gtk.CheckButton(label=_('Do not ask again.')) 34 self.remember_checkbox.set_no_show_all(True) 35 self.remember_checkbox.set_can_focus(False) 36 self.get_message_area().pack_end(self.remember_checkbox, True, True, 6) 37 38 def set_text(self, primary, secondary=None): 39 ''' Formats the dialog's text fields. 40 @param primary: Main text. 41 @param secondary: Descriptive text. 42 ''' 43 if primary: 44 self.set_markup('<span weight="bold" size="larger">' + 45 primary + '</span>') 46 if secondary: 47 self.format_secondary_markup(secondary) 48 49 def should_remember_choice(self): 50 ''' Returns True when the dialog choice should be remembered. ''' 51 return self.remember_checkbox.get_active() 52 53 def set_should_remember_choice(self, dialog_id, choices): 54 ''' This method enables the 'Do not ask again' checkbox. 55 @param dialog_id: Unique identifier for the dialog (a string). 56 @param choices: List of response IDs that should be remembered 57 ''' 58 self.remember_checkbox.show() 59 self.dialog_id = dialog_id 60 self.choices = [int(choice) for choice in choices] 61 62 def set_auto_destroy(self, auto_destroy): 63 ''' Determines if the dialog should automatically destroy itself 64 after run(). ''' 65 self.auto_destroy = auto_destroy 66 67 def run(self): 68 ''' Makes the dialog visible and waits for a result. Also destroys 69 the dialog after the result has been returned. ''' 70 71 if self.dialog_id in prefs['stored dialog choices']: 72 self.destroy() 73 return prefs['stored dialog choices'][self.dialog_id] 74 else: 75 self.show_all() 76 # Prevent checkbox from grabbing focus by only enabling it after show 77 self.remember_checkbox.set_can_focus(True) 78 result = super(MessageDialog, self).run() 79 80 if (self.should_remember_choice() and int(result) in self.choices): 81 prefs['stored dialog choices'][self.dialog_id] = int(result) 82 83 if self.auto_destroy: 84 self.destroy() 85 return result 86 87 88# vim: expandtab:sw=4:ts=4 89