def get_label ( self, ui ): """ Gets the label to use for a specified Item. """ # Return 'None' if the Item is a separator or spacer: if self.is_spacer(): return None label = self.label if label != '': return label name = self.name object = eval( self.object_, globals(), ui.context ) trait = object.base_trait( name ) label = user_name_for( name ) tlabel = trait.label if tlabel is None: return label if isinstance( tlabel, basestring ): if tlabel[0:3] == '...': return label + tlabel[3:] if tlabel[-3:] == '...': return tlabel[:-3] + label if self.label != '': return self.label return tlabel return tlabel( object, name, label )
def insert(self, index, item): """ Inserts an item into the group at the specified index. 1) An 'ActionManagerItem' instance. In which case the item is simply inserted into the group. 2) An 'Action' instance. In which case an 'ActionItem' instance is created with the action and then inserted into the group. 3) A Python callable (ie.'callable(item)' returns True). In which case an 'Action' is created that calls the callable when it is performed, and the action is then wrapped as in 2). """ if isinstance(item, Action): item = ActionItem(action=item) elif callable(item): text = user_name_for(item.func_name) item = ActionItem(action=Action(text=text, on_perform=item)) item.parent = self self._items.insert(index, item) return item
def insert(self, index, item): """ Inserts an item into the group at the specified index. Parameters ---------- index : int The position to insert the item. item : ActionManagerItem, Action or callable The item to insert. Returns ------- item : ActionManagerItem The actually inserted item. Notes ----- If the item is an ActionManagerItem instance it is simply inserted. If the item is an Action instance, an ActionItem is created for the action, and that is inserted. If the item is a callable, then an Action is created for the callable, and then that is handled as above. """ if isinstance(item, Action): item = ActionItem(action=item) elif callable(item): name = user_name_for(item.func_name) item = ActionItem(action=Action(name=name, on_perform=item)) item.parent = self self._items.insert(index, item) return item
def insert(self, index, item): """ Inserts an item into the group at the specified index. Parameters ---------- index : int The position to insert the item. item : ActionManagerItem, Action or callable The item to insert. Returns ------- item : ActionManagerItem The actually inserted item. Notes ----- If the item is an ActionManagerItem instance it is simply inserted. If the item is an Action instance, an ActionItem is created for the action, and that is inserted. If the item is a callable, then an Action is created for the callable, and then that is handled as above. """ if isinstance(item, Action): item = ActionItem(action=item) elif callable(item): name = user_name_for(item.__name__) item = ActionItem(action=Action(name=name, on_perform=item)) item.parent = self self._items.insert(index, item) return item
def _create_page(self, obj): """ Creates and returns a notebook page for a specified object with traits. """ # Create a new notebook page: page = self.notebook.create_page().set(data=obj) # Create the Traits UI for the object to put in the notebook page: ui = obj.edit_traits(parent=page.parent, view=self.factory.view, kind='subpanel').set(parent=self.ui) # Get the name of the page being added to the notebook: name = '' page_name = self.factory.page_name if page_name[0:1] == '.': if getattr(obj, page_name[1:], Undefined) is not Undefined: page.register_name_listener(obj, page_name[1:]) else: name = page_name if name == '': name = user_name_for(obj.__class__.__name__) # Make sure the name is not a duplicate, then save it in the page: if page.name == '': self.pages[name] = count = self.pages.get(name, 0) + 1 if count > 1: name += (' %d' % count) page.name = name # Get the page description page_desc = self.factory.page_description if page_desc[0:1] == '.': if getattr(obj, page_desc[1:], Undefined) is not Undefined: page.register_description_listener(obj, page_desc[1:]) # Get the page icon page_icon = self.factory.page_icon if page_icon[0:1] == '.': if getattr(obj, page_icon[1:], Undefined) is not Undefined: page.register_icon_listener(obj, page_icon[1:]) # Is the page deletable? page_deletable = self.factory.page_deletable if page_deletable[0:1] == '.': if getattr(obj, page_deletable[1:], Undefined) is not Undefined: page.register_deletable_listener(obj, page_deletable[1:]) # Save the Traits UI in the page so it can dispose of it later: page.ui = ui # Return the new notebook page return page
def _create_page(self, obj): """ Creates and returns a notebook page for a specified object with traits. """ # Create a new notebook page: page = self.notebook.create_page().set(data = obj) # Create the Traits UI for the object to put in the notebook page: ui = obj.edit_traits(parent=page.parent, view=self.factory.view, kind='subpanel').set(parent=self.ui) # Get the name of the page being added to the notebook: name = '' page_name = self.factory.page_name if page_name[0:1] == '.': if getattr(obj, page_name[1:], Undefined) is not Undefined: page.register_name_listener(obj, page_name[1:]) else: name = page_name if name == '': name = user_name_for(obj.__class__.__name__) # Make sure the name is not a duplicate, then save it in the page: if page.name == '': self.pages[name] = count = self.pages.get(name, 0) + 1 if count > 1: name += (' %d' % count) page.name = name # Get the page description page_desc = self.factory.page_description if page_desc[0:1] == '.': if getattr(obj, page_desc[1:], Undefined) is not Undefined: page.register_description_listener(obj, page_desc[1:]) # Get the page icon page_icon = self.factory.page_icon if page_icon[0:1] == '.': if getattr(obj, page_icon[1:], Undefined) is not Undefined: page.register_icon_listener(obj, page_icon[1:]) # Is the page deletable? page_deletable = self.factory.page_deletable if page_deletable[0:1] == '.': if getattr(obj, page_deletable[1:], Undefined) is not Undefined: page.register_deletable_listener(obj, page_deletable[1:]) # Save the Traits UI in the page so it can dispose of it later: page.ui = ui # Return the new notebook page return page
def get_label(self, ui): """ Gets the label to use for a specified Item. If not specified, the label is set as the name of the corresponding trait, replacing '_' with ' ', and capitalizing the first letter (see :func:`user_name_for`). This is called the *user name*. Magic: - if attr:`item.label` is specified, and it begins with '...', the final label is the user name followed by the item label - if attr:`item.label` is specified, and it ends with '...', the final label is the item label followed by the user name """ # Return 'None' if the Item is a separator or spacer: if self.is_spacer(): return None label = self.label if label != '': return label name = self.name object = eval(self.object_, globals(), ui.context) trait = object.base_trait(name) label = user_name_for(name) tlabel = trait.label if tlabel is None: return label if isinstance(tlabel, basestring): if tlabel[0:3] == '...': return label + tlabel[3:] if tlabel[-3:] == '...': return tlabel[:-3] + label if self.label != '': return self.label return tlabel return tlabel(object, name, label)
def get_label(self, ui): """ Gets the label to use for a specified Item. """ # Return 'None' if the Item is a separator or spacer: if self.is_spacer(): return None label = self.label if label != '': return label name = self.name object_ = eval(self.object_, globals(), ui.context) trait = object_.base_trait(name) # -------------------------------- # append metadata 'unit' to the label string if it exists: dict_ = trait.__dict__ if dict_.has_key('unit'): unit_str = ' [' + dict_['unit'] + ']' else: unit_str = ' ' label = user_name_for(name) + unit_str #--------------------------------- tlabel = trait.label if tlabel is None: return label if isinstance(tlabel, basestring): if tlabel[0:3] == '...': return label + tlabel[3:] if tlabel[-3:] == '...': return tlabel[:-3] + label if self.label != '': return self.label return tlabel return tlabel(object, name, label)
def _create_page(self, object): # Create the view for the object: view_object = object factory = self.factory if factory.factory is not None: view_object = factory.factory(object) ui = view_object.edit_traits(parent=self.control, view=factory.view, kind=factory.ui_kind).trait_set( parent=self.ui) # Get the name of the page being added to the notebook: name = '' monitoring = False prefix = '%s_%s_page_' % (self.object_name, self.name) page_name = factory.page_name if page_name[0:1] == '.': name = xgetattr(view_object, page_name[1:], None) monitoring = (name is not None) if monitoring: handler_name = None method = getattr(self.ui.handler, prefix + 'name', None) if method is not None: handler_name = method(self.ui.info, object) if handler_name is not None: name = handler_name else: name = str(name) or '???' view_object.on_trait_change(self.update_page_name, page_name[1:], dispatch='ui') else: name = '' elif page_name != '': name = page_name if name == '': name = user_name_for(view_object.__class__.__name__) # Make sure the name is not a duplicate: if not monitoring: self._pages[name] = count = self._pages.get(name, 0) + 1 if count > 1: name += (' %d' % count) # Return the control for the ui, and whether or not its name is being # monitored: image = None method = getattr(self.ui.handler, prefix + 'image', None) if method is not None: image = method(self.ui.info, object) if image is None: self.control.addTab(ui.control, name) else: self.control.addTab(ui.control, image, name) if self.factory.show_notebook_menu: newaction = self._context_menu.addAction(name) newaction.setText(name) newaction.setCheckable(True) newaction.setChecked(True) newaction.triggered.connect( lambda e, name=name: self._menu_action( e, name=name)) self._action_dict[name] = newaction self._pagewidgets[name] = ui.control return (ui, view_object, monitoring)
def open_file_view ( self ): """ Returns the file dialog view to use. """ # Set up the default file dialog view and size information: item = Item( 'file_name', id = 'file_tree', style = 'custom', show_label = False, width = 0.5, editor = FileEditor( filter = self.filter, allow_dir = True, reload_name = 'reload', dclick_name = 'dclick' ) ) width = height = 0.20 # Check to see if we have any extensions being added: if len( self.extensions ) > 0: # fixme: We should use the actual values of the view's Width and # height traits here to adjust the overall width and height... width *= 2.0 # Assume we can used a fixed width Group: klass = HGroup # Set up to build a list of view Item objects: items = [] # Add each extension to the dialog: for i, extension in enumerate( self.extensions ): # Save the extension in a new trait (for use by the View): name = 'extension_%d' % i setattr( self, name, extension ) extension_view = extension # Sync up the 'file_name' trait with the extension: self.sync_trait( 'file_name', extension, mutual = True ) # Check to see if it also defines the optional IFileDialogView # interface, and if not, use the default view information: if not extension.has_traits_interface( IFileDialogView ): extension_view = default_view # Get the view that the extension wants to use: view = extension.trait_view( extension_view.view ) # Determine if we should use a splitter for the dialog: if not extension_view.is_fixed: klass = HSplit # Add the extension as a new view item: items.append( Item( name, label = user_name_for( extension.__class__.__name__ ), show_label = False, style = 'custom', width = 0.5, height = 0.5, dock = 'horizontal', resizable = True, editor = InstanceEditor( view = view, id = name ) ) ) # Finally, combine the normal view element with the extensions: item = klass( item, VSplit( id = 'splitter2', springy = True, *items ), id = 'splitter' ) # Return the resulting view: return View( VGroup( VGroup( item ), HGroup( Item( 'create', id = 'create', show_label = False, style = 'custom', defined_when = 'is_save_file', enabled_when = 'can_create_dir', tooltip = 'Create a new directory' ), Item( 'file_name', id = 'history', editor = HistoryEditor( entries = self.entries, auto_set = True ), springy = True ), Item( 'ok', id = 'ok', show_label = False, enabled_when = 'is_valid_file' ), Item( 'cancel', show_label = False ) ) ), title = self.title, id = self.id, kind = 'livemodal', width = width, height = height, close_result = False, resizable = True )
def _create_page ( self, object ): """ Creates a DockControl for a specified object. """ # Create the view for the object: view_object = object factory = self.factory if factory.factory is not None: view_object = factory.factory( object ) ui = view_object.edit_traits( parent = self.control, view = factory.view, kind = factory.ui_kind ).set( parent = self.ui ) # Get the name of the page being added to the notebook: name = '' monitoring = False prefix = '%s_%s_page_' % ( self.object_name, self.name ) page_name = self.factory.page_name if page_name[0:1] == '.': name = xgetattr( view_object, page_name[1:], None ) monitoring = (name is not None) if monitoring: handler_name = None method = getattr( self.ui.handler, prefix + 'name', None ) if method is not None: handler_name = method( self.ui.info, object ) if handler_name is not None: name = handler_name else: name = unicode( name ) or u'???' view_object.on_trait_change( self.update_page_name, page_name[1:], dispatch = 'ui' ) else: name = '' elif page_name != '': name = page_name if name == '': name = user_name_for( view_object.__class__.__name__ ) # Make sure the name is not a duplicate: if not monitoring: self._pages[ name ] = count = self._pages.get( name, 0 ) + 1 if count > 1: name += (' %d' % count) # Return a new DockControl for the ui, and whether or not its name is # being monitored: image = None method = getattr( self.ui.handler, prefix + 'image', None ) if method is not None: image = method( self.ui.info, object ) dock_control = DockControl( control = ui.control, id = str( id( ui.control ) ), name = name, style = factory.dock_style, image = image, export = factory.export, closeable = factory.deletable, dockable = DockableListElement( ui = ui, editor = self ) ) return ( dock_control, view_object, monitoring )
def _get_label(self): """ Gets the label of the column. """ if self._label is not None: return self._label return user_name_for(self.name)
def _create_page(self, object): # Create the view for the object: view_object = object factory = self.factory if factory.factory is not None: view_object = factory.factory(object) ui = view_object.edit_traits(parent=self.control, view=factory.view, kind=factory.ui_kind).set(parent=self.ui) # Get the name of the page being added to the notebook: name = '' monitoring = False prefix = '%s_%s_page_' % (self.object_name, self.name) page_name = factory.page_name if page_name[0:1] == '.': name = xgetattr(view_object, page_name[1:], None) monitoring = (name is not None) if monitoring: handler_name = None method = getattr(self.ui.handler, prefix + 'name', None) if method is not None: handler_name = method(self.ui.info, object) if handler_name is not None: name = handler_name else: name = str(name) or '???' view_object.on_trait_change(self.update_page_name, page_name[1:], dispatch='ui') else: name = '' elif page_name != '': name = page_name if name == '': name = user_name_for(view_object.__class__.__name__) # Make sure the name is not a duplicate: if not monitoring: self._pages[name] = count = self._pages.get(name, 0) + 1 if count > 1: name += (' %d' % count) # Return the control for the ui, and whether or not its name is being # monitored: image = None method = getattr(self.ui.handler, prefix + 'image', None) if method is not None: image = method(self.ui.info, object) if image is None: self.control.addTab(ui.control, name) else: self.control.addTab(ui.control, image, name) if self.factory.show_notebook_menu: newaction = self._context_menu.addAction(name) newaction.setText(name) newaction.setCheckable(True) newaction.setChecked(True) newaction.triggered.connect( lambda e, name=name: self._menu_action(e, name=name)) self._action_dict[name] = newaction self._pagewidgets[name] = ui.control return (ui, view_object, monitoring)
def _create_page(self, object): """ Creates a DockControl for a specified object. """ # Create the view for the object: view_object = object factory = self.factory if factory.factory is not None: view_object = factory.factory(object) ui = view_object.edit_traits(parent=self.control, view=factory.view, kind=factory.ui_kind).set(parent=self.ui) # Get the name of the page being added to the notebook: name = '' monitoring = False prefix = '%s_%s_page_' % (self.object_name, self.name) page_name = self.factory.page_name if page_name[0:1] == '.': name = xgetattr(view_object, page_name[1:], None) monitoring = (name is not None) if monitoring: handler_name = None method = getattr(self.ui.handler, prefix + 'name', None) if method is not None: handler_name = method(self.ui.info, object) if handler_name is not None: name = handler_name else: name = unicode(name) or u'???' view_object.on_trait_change(self.update_page_name, page_name[1:], dispatch='ui') else: name = '' elif page_name != '': name = page_name if name == '': name = user_name_for(view_object.__class__.__name__) # Make sure the name is not a duplicate: if not monitoring: self._pages[name] = count = self._pages.get(name, 0) + 1 if count > 1: name += (' %d' % count) # Return a new DockControl for the ui, and whether or not its name is # being monitored: image = None method = getattr(self.ui.handler, prefix + 'image', None) if method is not None: image = method(self.ui.info, object) dock_control = DockControl(control=ui.control, id=str(id(ui.control)), name=name, style=factory.dock_style, image=image, export=factory.export, closeable=factory.deletable, dockable=DockableListElement(ui=ui, editor=self)) return (dock_control, view_object, monitoring)