comparison urwid_satext/sat_widgets.py @ 115:2b7eafea8bdb

GenericDialog: buttons callbacks can now be set after widget creation
author Goffi <goffi@goffi.org>
date Wed, 11 Nov 2015 18:17:15 +0100
parents bf38c1c0db3b
children 8e9a6a9c727e
comparison
equal deleted inserted replaced
114:bf38c1c0db3b 115:2b7eafea8bdb
1120 def __init__(self, widgets_lst, title, style=None, **kwargs): 1120 def __init__(self, widgets_lst, title, style=None, **kwargs):
1121 if style is None: 1121 if style is None:
1122 style = [] 1122 style = []
1123 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') 1123 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title')
1124 1124
1125 buttons = None 1125 self.buttons = collections.OrderedDict()
1126 1126
1127 if "OK/CANCEL" in style: 1127 if "OK/CANCEL" in style:
1128 cancel_arg = [kwargs['cancel_value']] if kwargs.has_key('cancel_value') else [] 1128 self.buttons['cancel'] = urwid.Button(_("Cancel"), kwargs.get('cancel_cb'), kwargs.get('cancel_value'))
1129 ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else [] 1129 self.buttons['ok'] = urwid.Button(_("Ok"), kwargs.get('ok_cb'), kwargs.get('ok_value'))
1130 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb'], *cancel_arg),
1131 urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)]
1132 elif "YES/NO" in style: 1130 elif "YES/NO" in style:
1133 yes_arg = [kwargs['yes_value']] if kwargs.has_key('yes_value') else [] 1131 self.buttons['yes'] = urwid.Button(_("Yes"), kwargs.get('yes_cb'), kwargs.get('yes_value'))
1134 no_arg = [kwargs['no_value']] if kwargs.has_key('no_value') else [] 1132 self.buttons['no'] = urwid.Button(_("No"), kwargs.get('no_cb'), kwargs.get('no_value'))
1135 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb'], *yes_arg),
1136 urwid.Button(_("No"), kwargs['no_cb'], *no_arg)]
1137 if "OK" in style: 1133 if "OK" in style:
1138 ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else [] 1134 self.buttons['ok'] = urwid.Button(_("Ok"), kwargs.get('ok_cb'), kwargs.get('ok_value'))
1139 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)] 1135 if self.buttons:
1140 if buttons: 1136 buttons_flow = urwid.GridFlow(self.buttons.values(), max([len(button.get_label()) for button in self.buttons.itervalues()])+4, 1, 1, 'center')
1141 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
1142 body_content = urwid.SimpleListWalker(widgets_lst) 1137 body_content = urwid.SimpleListWalker(widgets_lst)
1143 frame_body = UnselectableListBox(body_content) 1138 frame_body = UnselectableListBox(body_content)
1144 frame = FocusFrame(frame_body, frame_header, buttons_flow if buttons else None, 'footer' if buttons else 'body') 1139 frame = FocusFrame(frame_body, frame_header, buttons_flow if self.buttons else None, 'footer' if self.buttons else 'body')
1145 decorated_frame = urwid.LineBox(frame) 1140 decorated_frame = urwid.LineBox(frame)
1146 urwid.WidgetWrap.__init__(self, decorated_frame) 1141 urwid.WidgetWrap.__init__(self, decorated_frame)
1142
1143 def setCallback(self, name, callback, data=None):
1144 """Set the callback associated with a button press
1145
1146 @param name: one of "ok", "cancel", "yest", "no"
1147 @aram callback(callable): method to call on requested action
1148 @param args*: argument to send to the callback (first one will be the button widget
1149 @raise KeyError if name is invalid
1150 """
1151 button = self.buttons[name]
1152 urwid.connect_signal(button, 'click', callback, data)
1147 1153
1148 1154
1149 class InputDialog(GenericDialog): 1155 class InputDialog(GenericDialog):
1150 """Dialog with an edit box""" 1156 """Dialog with an edit box"""
1151 1157
1172 1178
1173 1179
1174 class Alert(GenericDialog): 1180 class Alert(GenericDialog):
1175 """Dialog with just a message and a OK button""" 1181 """Dialog with just a message and a OK button"""
1176 1182
1177 def __init__(self, title, message, style=['OK'], **kwargs): 1183 def __init__(self, title, message, style=None, **kwargs):
1184 if style is None:
1185 style= ['OK']
1178 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) 1186 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
1179 1187
1180 ## CONTAINERS ## 1188 ## CONTAINERS ##
1181 1189
1182 class ColumnsRoller(urwid.Widget): 1190 class ColumnsRoller(urwid.Widget):