def empty_list(self): """ Creates an empty list entry (so the user can add a new item). """ # Connecting the new button to the mapper from pyface.qt import QtCore, QtGui from pyface.qt import qt_api from traitsui.qt4.helper import IconButton #control = IconButton('list_editor.png', self.mapper.map) if qt_api == 'pyside': control = IconButton('list_editor.png', self.mapper.map) elif qt_api == 'pyqt': control = IconButton( 'list_editor.png', lambda: self.popup_empty_menu(self._list_pane.layout().sender( ))) # Setting the mapping and asking it to send the sender to the # callback method self.mapper.setMapping(control, control) self.mapper.mapped[QtCore.QObject].connect(self.popup_empty_menu) control.is_empty = True self._cur_control = control from traitsui.editors.list_editor import ListItemProxy proxy = ListItemProxy(self.object, self.name, -1, None, None) pcontrol = QtGui.QLabel(' (Empty List)') pcontrol.proxy = control.proxy = proxy layout = self._list_pane.layout() layout.addWidget(control, 0, 0) layout.addWidget(pcontrol, 0, 1)
def update_editor(self): """ Updates the editor when the object trait changes externally to the editor. """ self.mapper = QtCore.QSignalMapper(self.control) # Disconnect the editor from any control about to be destroyed: self._dispose_items() list_pane = self._list_pane layout = list_pane.layout() # Create all of the list item trait editors: trait_handler = self._trait_handler resizable = ((trait_handler.minlen != trait_handler.maxlen) and self.mutable) item_trait = trait_handler.item_trait is_fake = (resizable and (len(self.value) == 0)) if is_fake: self.empty_list() else: # Asking the mapper to send the sender to the callback method self.mapper.mapped[QtCore.QObject].connect(self.popup_menu) editor = self._editor for index, value in enumerate(self.value): row, column = divmod(index, self.factory.columns) # Account for the fact that we have <columns> number of # pairs column = column * 2 if resizable: # Connecting the new button to the mapper control = IconButton('list_editor.png', self.mapper.map) # Setting the mapping and asking it to send the index of the # sender to the callback method self.mapper.setMapping(control, control) layout.addWidget(control, row, column) proxy = ListItemProxy(self.object, self.name, index, item_trait, value) if resizable: control.proxy = proxy peditor = editor(self.ui, proxy, 'value', self.description, list_pane).set(object_name='') peditor.prepare(list_pane) pcontrol = peditor.control pcontrol.proxy = proxy if isinstance(pcontrol, QtGui.QWidget): layout.addWidget(pcontrol, row, column + 1) else: layout.addLayout(pcontrol, row, column + 1) # QScrollArea can have problems if the widget being scrolled is set too # early (ie. before it contains something). if self.control.widget() is None: self.control.setWidget(list_pane)
def empty_list(self): """Creates an empty list entry (so the user can add a new item).""" control = ImageControl(self.control, self.bitmap, -1, self.popup_empty_menu) control.is_empty = True proxy = ListItemProxy(self.object, self.name, -1, None, None) pcontrol = wx.StaticText(self.control, -1, " (Empty List)") pcontrol.proxy = control.proxy = proxy self.reload_sizer([(control, pcontrol)])
def empty_list(self): """ Creates an empty list entry (so the user can add a new item). """ control = IconButton('list_editor.png', self.popup_empty_menu) control.is_empty = True self._cur_control = control proxy = ListItemProxy(self.object, self.name, -1, None, None) pcontrol = QtGui.QLabel(' (Empty List)') pcontrol.proxy = control.proxy = proxy layout = self._list_pane.layout() layout.addWidget(control, 0, 0) layout.addWidget(pcontrol, 0, 1)
def empty_list(self): """ Creates an empty list entry (so the user can add a new item). """ # Connecting the new button to the mapper control = IconButton('list_editor.png', self.mapper.map) # Setting the mapping and asking it to send the sender to the # callback method self.mapper.setMapping(control, control) self.mapper.mapped[QtCore.QObject].connect(self.popup_empty_menu) control.is_empty = True self._cur_control = control proxy = ListItemProxy(self.object, self.name, -1, None, None) pcontrol = QtGui.QLabel(' (Empty List)') pcontrol.proxy = control.proxy = proxy layout = self._list_pane.layout() layout.addWidget(control, 0, 0) layout.addWidget(pcontrol, 0, 1)
def empty_list(self): """ Creates an empty list entry (so the user can add a new item). """ # Connecting the new button to the mapper control = IconButton('list_editor.png', self.mapper.map) # Setting the mapping and asking it to send the index of the sender to # callback method. Unfortunately just sending the control does not # work for PyQt (tested on 4.11) self.mapper.setMapping(control, 0) self.mapper.mapped.connect(self.popup_empty_menu) control.is_empty = True self._cur_control = control self.buttons = [control] proxy = ListItemProxy(self.object, self.name, -1, None, None) pcontrol = QtGui.QLabel(' (Empty List)') pcontrol.proxy = control.proxy = proxy layout = self._list_pane.layout() layout.addWidget(control, 0, 1) layout.addWidget(pcontrol, 0, 0)
def update_editor(self): """ Updates the editor when the object trait changes externally to the editor. """ # Disconnect the editor from any control about to be destroyed: self._dispose_items() # Get rid of any previous contents: list_pane = self.control list_pane.SetSizer(None) list_pane.DestroyChildren() # Create all of the list item trait editors: trait_handler = self._trait_handler resizable = ((trait_handler.minlen != trait_handler.maxlen) and self.mutable) item_trait = trait_handler.item_trait factory = self.factory list_sizer = wx.FlexGridSizer(len(self.value), (1 + resizable) * factory.columns, 0, 0) j = resizable for i in range(factory.columns): list_sizer.AddGrowableCol(j) j += (1 + resizable) values = self.value index = 0 width, height = 0, 0 is_fake = (resizable and (values is None or len(values) == 0)) if is_fake: values = [item_trait.default_value()[1]] panel_height = 0 editor = self._editor for value in values: width1 = height = 0 if resizable: control = ImageControl(list_pane, self.bitmap, -1, self.popup_menu) width1, height = control.GetSize() width1 += 4 try: proxy = ListItemProxy(self.object, self.name, index, item_trait, value) if resizable: control.proxy = proxy peditor = editor(self.ui, proxy, 'value', self.description, list_pane).set(object_name='') peditor.prepare(list_pane) pcontrol = peditor.control pcontrol.proxy = proxy except: if not is_fake: raise pcontrol = wx.Button(list_pane, -1, 'sample') pcontrol.Fit() width2, height2 = size = pcontrol.GetSize() pcontrol.SetMinSize(size) width = max(width, width1 + width2) height = max(height, height2) panel_height += height if resizable: list_sizer.Add(control, 0, wx.LEFT | wx.RIGHT, 2) list_sizer.Add(pcontrol, 0, wx.EXPAND) index += 1 list_pane.SetSizer(list_sizer) if not self.mutable: #list_sizer.SetDimension(0,0,width, panel_height) list_pane.SetInitialSize(list_sizer.GetSize()) if is_fake: self._cur_control = control self.empty_list() control.Destroy() pcontrol.Destroy() rows = 1 if not self.single_row: rows = self.factory.rows # Make sure we have valid values set for width and height (in case there # was no data to base them on): if width == 0: width = 100 if panel_height == 0: panel_height = 20 list_pane.SetMinSize( wx.Size(width + ((trait_handler.maxlen > rows) * scrollbar_dx), panel_height)) list_pane.SetupScrolling() list_pane.GetParent().Layout()
def update_editor(self): """ Updates the editor when the object trait changes externally to the editor. """ self.mapper = QtCore.QSignalMapper(self.control) self.delete_mapper = QtCore.QSignalMapper(self.control) # Disconnect the editor from any control about to be destroyed: self._dispose_items() layout = self.control.layout() # Create all of the list item trait editors: trait_handler = self._trait_handler resizable = ((trait_handler.minlen != trait_handler.maxlen) and self.mutable) item_trait = trait_handler.item_trait is_fake = (resizable and (len(self.value) == 0)) if is_fake: self.empty_list() else: # Asking the mapper to send the sender to the callback method self.mapper.mapped.connect(self.popup_menu) self.delete_mapper.mapped.connect(self._delete_item) editor = self._editor for index, value in enumerate(self.value): row, column = divmod(index, self.factory.columns) # Account for the fact that we have <columns> number of # pairs column = column * 2 if resizable: # Connecting the new button to the mapper control = IconButton('list_editor.png', self.popup_mapper.map) self.mapper.setMapping(control, index) layout.addWidget(control, row, column) if self.factory.deletable: # Connecting the new button to the mapper control = IconButton(QtGui.QStyle.SP_TitleBarCloseButton, self.delete_mapper.map) self.delete_mapper.setMapping(control, index) layout.addWidget(control, row, column) proxy = ListItemProxy(self.object, self.name, index, item_trait, value) if resizable: control.proxy = proxy peditor = editor(self.ui, proxy, 'value', self.description, self.control).set(object_name='') peditor.prepare(self.control) pcontrol = peditor.control pcontrol.proxy = proxy if isinstance(pcontrol, QtGui.QWidget): layout.addWidget(pcontrol, row, column + 1) else: layout.addLayout(pcontrol, row, column + 1)