def __init__(self, items=None, title=None, message=None, parent=None, size=None): super(SelectDialog, self).__init__() self.sview = SelectView(items) self._create_gui(title, message, parent) self._connect_signals() if size and len(size) == 2: w, h = -1, -1 if size[0] > 0: w = size[0] if size[1] > 0: h = size[1] self.dialog.set_size_request(w, h)
def create_selectview(self): self.items = ( { 'enabled': True, 'name': 'item1', 'desc': 'desc1' }, { 'enabled': False, 'name': 'item2' }, { 'enabled': True, 'desc': 'desc3' }, { 'name': 'item4', 'desc': 'desc4' }, { 'enabled': True, 'name': 'item5', 'desc': '' }, { 'enabled': False, 'name': '', 'desc': 'desc6' }, ) self.selectview = SelectView(self.items) self.selectview.connect('item-enabled', self._on_item_action, 'enabled') self.selectview.connect('item-disabled', self._on_item_action, 'disabled') self.selectview.connect('item-selected', self._on_item_action, 'selected') return self.selectview
def create_selectview(self): self.items = ( {'enabled': True, 'name': 'item1', 'desc': 'desc1'}, {'enabled': False, 'name': 'item2' }, {'enabled': True, 'desc': 'desc3'}, { 'name': 'item4', 'desc': 'desc4'}, {'enabled': True, 'name': 'item5', 'desc': '' }, {'enabled': False, 'name': '', 'desc': 'desc6'}, ) self.selectview = SelectView(self.items) self.selectview.connect('item-enabled', self._on_item_action, 'enabled') self.selectview.connect('item-disabled', self._on_item_action, 'disabled') self.selectview.connect('item-selected', self._on_item_action, 'selected') return self.selectview
class SelectViewTestWindow(gtk.Window): def __init__(self): super(SelectViewTestWindow, self).__init__() self.connect('destroy', lambda *args: gtk.main_quit()) self.add(self.create_selectview()) def create_selectview(self): self.items = ( {'enabled': True, 'name': 'item1', 'desc': 'desc1'}, {'enabled': False, 'name': 'item2' }, {'enabled': True, 'desc': 'desc3'}, { 'name': 'item4', 'desc': 'desc4'}, {'enabled': True, 'name': 'item5', 'desc': '' }, {'enabled': False, 'name': '', 'desc': 'desc6'}, ) self.selectview = SelectView(self.items) self.selectview.connect('item-enabled', self._on_item_action, 'enabled') self.selectview.connect('item-disabled', self._on_item_action, 'disabled') self.selectview.connect('item-selected', self._on_item_action, 'selected') return self.selectview def _on_item_action(self, sender, item_info, action): print 'Item %s: %s' % (action, item_info)
class SelectViewTestWindow(gtk.Window): def __init__(self): super(SelectViewTestWindow, self).__init__() self.connect('destroy', lambda *args: gtk.main_quit()) self.add(self.create_selectview()) def create_selectview(self): self.items = ( { 'enabled': True, 'name': 'item1', 'desc': 'desc1' }, { 'enabled': False, 'name': 'item2' }, { 'enabled': True, 'desc': 'desc3' }, { 'name': 'item4', 'desc': 'desc4' }, { 'enabled': True, 'name': 'item5', 'desc': '' }, { 'enabled': False, 'name': '', 'desc': 'desc6' }, ) self.selectview = SelectView(self.items) self.selectview.connect('item-enabled', self._on_item_action, 'enabled') self.selectview.connect('item-disabled', self._on_item_action, 'disabled') self.selectview.connect('item-selected', self._on_item_action, 'selected') return self.selectview def _on_item_action(self, sender, item_info, action): print 'Item %s: %s' % (action, item_info)
class SelectDialog(GObjectWrapper): """ A dialog wrapper to easily select items from a list. """ __gtype_name__ = 'SelectDialog' __gsignals__ = { 'item-enabled': (SIGNAL_RUN_FIRST, None, (TYPE_PYOBJECT,)), 'item-disabled': (SIGNAL_RUN_FIRST, None, (TYPE_PYOBJECT,)), 'item-selected': (SIGNAL_RUN_FIRST, None, (TYPE_PYOBJECT,)), 'selection-done': (SIGNAL_RUN_FIRST, None, (TYPE_PYOBJECT,)), } # INITIALIZERS # def __init__(self, items=None, title=None, message=None, parent=None, size=None): super(SelectDialog, self).__init__() self.sview = SelectView(items) self._create_gui(title, message, parent) self._connect_signals() if size and len(size) == 2: w, h = -1, -1 if size[0] > 0: w = size[0] if size[1] > 0: h = size[1] self.dialog.set_size_request(w, h) def _connect_signals(self): self.sview.connect('item-enabled', self._on_item_enabled) self.sview.connect('item-disabled', self._on_item_disabled) self.sview.connect('item-selected', self._on_item_selected) def _create_gui(self, title, message, parent): self.dialog = gtk.Dialog() self.dialog.set_modal(True) if isinstance(parent, gtk.Widget): self.set_transient_for(parent) self.dialog.set_title(title is not None and title or 'Select items') self.message = gtk.Label(message is not None and message or '') self.dialog.child.pack_start(self.message, expand=False, fill=False, padding=10) scrolled_window = gtk.ScrolledWindow() scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) scrolled_window.add(self.sview) self.dialog.child.pack_end(scrolled_window, expand=True, fill=True) self.dialog.add_buttons(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE) # METHODS # def get_message(self): return self.message.get_text() def set_icon(self, icon): """Simple proxy method to C{self.dialog.set_icon(icon)}.""" self.dialog.set_icon(icon) def set_message(self, msg): self.message.set_text(msg) def set_transient_for(self, parent): """Simple proxy method to C{self.dialog.set_transient_for(parent)}.""" self.dialog.set_transient_for(parent) def run(self, items=None, parent=None): if items is not None: self.sview.set_model(items) if isinstance(parent, gtk.Widget): self.dialog.reparent(parent) self.dialog.show_all() self.response = self.dialog.run() self.dialog.hide() self.emit('selection-done', self.sview.get_all_items()) return self.response # EVENT HANDLERS # def _on_item_enabled(self, selectview, item): self.emit('item-enabled', item) def _on_item_disabled(self, selectview, item): self.emit('item-disabled', item) def _on_item_selected(self, selectview, item): self.emit('item-selected', item)