class ExpressionColumn(ObjectColumn): #--------------------------------------------------------------------------- # Trait definitions: #--------------------------------------------------------------------------- # The Python expression used to return the value of the column expression = Expression # Is this column editable? editable = Constant(False) # The globals dictionary that should be passed to the expression evaluation: globals = Any({}) #--------------------------------------------------------------------------- # Gets the value of the column for a specified object: #--------------------------------------------------------------------------- def get_raw_value(self, object): """ Gets the unformatted value of the column for a specified object. """ try: return eval(self.expression_, self.globals, {'object': object}) except: logger.exception('Error evaluating table column expression: %s' % self.expression) return None
def bind(self, name, value, id=None): """ Binds a name to a value if it is not already bound. """ if id is None: id = name if not hasattr(self, name): self.add_trait(name, Constant(value)) if id != '': self.ui._names.append(id)
class ReportAdapter(TabularAdapter): columns = [('Name', 'name'), ('Age', 'age'), ('Address', 'address'), ('Spouse', 'spouse')] font = 'Courier 10' age_alignment = Constant('right') MarriedPerson_age_image = Property MarriedPerson_bg_color = Color(0xE0E0FF) MarriedPerson_spouse_text = Property Person_spouse_text = Constant('') def _get_MarriedPerson_age_image(self): if self.item.age < 18: return '@icons:red_ball' return None def _get_MarriedPerson_spouse_text(self): return self.item.partner.name
class UserPerspectiveName(HasTraits): """ Object with views for naming or renaming a user perspective. """ ########################################################################### # 'UserPerspectiveName' interface. ########################################################################### # The name of the new user perspective. name = NotEmptyString # Should the editor area be shown in this perpsective? show_editor_area = Bool(True) # Help notes when creating a new view. new_help = Constant("""Note: - The new perspective will initially be empty. - Add new views to the perspective by selecting them from the 'View' menu. - Drag the notebook tabs and splitter bars to arrange the views within the perspective.""") #### Traits views ######################################################### new_view = View(VGroup( VGroup('name', 'show_editor_area'), VGroup('_', Item('new_help', style='readonly'), show_labels=False)), title='New User Perspective', id='enthought.envisage.workbench.action.' 'new_user_perspective_action.UserPerspectiveName', buttons=['OK', 'Cancel'], kind='livemodal', width=300) save_as_view = View('name', title='Save User Perspective As', id='enthought.envisage.workbench.action.' 'save_as_user_perspective_action.UserPerspectiveName', buttons=['OK', 'Cancel'], kind='livemodal', width=300) rename_view = View('name', title='Rename User Perspective', id='enthought.envisage.workbench.action.' 'rename_user_perspective_action.UserPerspectiveName', buttons=['OK', 'Cancel'], kind='livemodal', width=300)
class WorkbenchWindow(ApplicationWindow): """ A workbench window. """ #### 'IWorkbenchWindow' interface ######################################### # The view or editor that currently has the focus. active_part = Instance(IWorkbenchPart) # The editor manager is used to create/restore editors. editor_manager = Instance(IEditorManager) # The current selection within the window. selection = List # The workbench that the window belongs to. workbench = Instance('enthought.pyface.workbench.api.IWorkbench') #### Editors ####################### # The active editor. active_editor = Instance(IEditor) # The visible (open) editors. editors = List(IEditor) # The Id of the editor area. editor_area_id = Constant('enthought.pyface.workbench.editors') # The (initial) size of the editor area (the user is free to resize it of # course). editor_area_size = Tuple((100, 100)) # Fired when an editor is about to be opened (or restored). editor_opening = Delegate('layout') # Event(IEditor) # Fired when an editor has been opened (or restored). editor_opened = Delegate('layout') # Event(IEditor) # Fired when an editor is about to be closed. editor_closing = Delegate('layout') # Event(IEditor) # Fired when an editor has been closed. editor_closed = Delegate('layout') # Event(IEditor) #### Views ######################### # The active view. active_view = Instance(IView) # The available views (note that this is *all* of the views, not just those # currently visible). # # Views *cannot* be shared between windows as each view has a reference to # its toolkit-specific control etc. views = List(IView) #### Perspectives ################## # The active perspective. active_perspective = Instance(IPerspective) # The available perspectives. If no perspectives are specified then the # a single instance of the 'Perspective' class is created. perspectives = List(IPerspective) # The Id of the default perspective. # # There are two situations in which this is used: # # 1. When the window is being created from scratch (i.e., not restored). # # If this is the empty string, then the first perspective in the list of # perspectives is shown (if there are no perspectives then an instance # of the default 'Perspective' class is used). If this is *not* the # empty string then the perspective with this Id is shown. # # 2. When the window is being restored. # # If this is the empty string, then the last perspective that was # visible when the window last closed is shown. If this is not the empty # string then the perspective with this Id is shown. # default_perspective_id = Str #### 'WorkbenchWindow' interface ########################################## # The window layout is responsible for creating and managing the internal # structure of the window (i.e., it knows how to add and remove views and # editors etc). layout = Instance(WorkbenchWindowLayout) #### 'Private' interface ################################################## # The state of the window suitable for pickling etc. _memento = Instance(WorkbenchWindowMemento) ########################################################################### # 'Window' interface. ########################################################################### def open(self): """ Open the window. Overridden to make the 'opening' event vetoable. Return True if the window opened successfully; False if the open event was vetoed. """ logger.debug('window %s opening', self) # Trait notification. self.opening = event = Vetoable() if not event.veto: if self.control is None: self._create() self.show(True) # Trait notification. self.opened = self logger.debug('window %s opened', self) else: logger.debug('window %s open was vetoed', self) # fixme: This is not actually part of the Pyface 'Window' API (but # maybe it should be). We return this to indicate whether the window # actually opened. return self.control is not None def close(self): """ Closes the window. Overridden to make the 'closing' event vetoable. Return True if the window closed successfully (or was not even open!), False if the close event was vetoed. """ logger.debug('window %s closing', self) if self.control is not None: # Trait notification. self.closing = event = Vetoable() # fixme: Hack to mimic vetoable events! if not event.veto: # Give views and editors a chance to cleanup after themselves. self.destroy_views(self.views) self.destroy_editors(self.editors) # Cleanup the window layout (event handlers, etc.) self.layout.close() # Cleanup the toolkit-specific control. self.destroy() # Cleanup our reference to the control so that we can (at least # in theory!) be opened again. self.control = None # Trait notification. self.closed = self logger.debug('window %s closed', self) else: logger.debug('window %s close was vetoed', self) else: logger.debug('window %s is not open', self) # FIXME v3: This is not actually part of the Pyface 'Window' API (but # maybe it should be). We return this to indicate whether the window # actually closed. return self.control is None ########################################################################### # Protected 'Window' interface. ########################################################################### def _create_contents(self, parent): """ Create and return the window contents. """ # Create the initial window layout. contents = self.layout.create_initial_layout(parent) # Save the initial window layout so that we can reset it when changing # to a perspective that has not been seen yet. self._initial_layout = self.layout.get_view_memento() # Are we creating the window from scratch or restoring it from a # memento? if self._memento is None: self._memento = WorkbenchWindowMemento() else: self._restore_contents() # Set the initial perspective. self.active_perspective = self._get_initial_perspective() return contents ########################################################################### # 'WorkbenchWindow' interface. ########################################################################### #### Initializers ######################################################### def _editor_manager_default(self): """ Trait initializer. """ from editor_manager import EditorManager return EditorManager(window=self) def _layout_default(self): """ Trait initializer. """ return WorkbenchWindowLayout(window=self) #### Methods ############################################################## def activate_editor(self, editor): """ Activates an editor. """ self.layout.activate_editor(editor) return def activate_view(self, view): """ Activates a view. """ self.layout.activate_view(view) return def add_editor(self, editor, title=None): """ Adds an editor. If no title is specified, the editor's name is used. """ if title is None: title = editor.name self.layout.add_editor(editor, title) self.editors.append(editor) return def add_view(self, view, position=None, relative_to=None, size=(-1, -1)): """ Adds a view. """ self.layout.add_view(view, position, relative_to, size) # This case allows for views that are created and added dynamically # (i.e. they were not even known about when the window was created). if not view in self.views: self.views.append(view) return def close_editor(self, editor): """ Closes an editor. """ self.layout.close_editor(editor) return def close_view(self, view): """ Closes a view. fixme: Currently views are never 'closed' in the same sense as an editor is closed. Views are merely hidden. """ self.hide_view(view) return def create_editor(self, obj, kind=None): """ Create an editor for an object. Return None if no editor can be created for the object. """ return self.editor_manager.create_editor(self, obj, kind) def destroy_editors(self, editors): """ Destroy a list of editors. """ for editor in editors: if editor.control is not None: editor.destroy_control() return def destroy_views(self, views): """ Destroy a list of views. """ for view in views: if view.control is not None: view.destroy_control() return def edit(self, obj, kind=None, use_existing=True): """ Edit an object. 'kind' is simply passed through to the window's editor manager to allow it to create a particular kind of editor depending on context etc. If 'use_existing' is True and the object is already being edited in the window then the existing editor will be activated (i.e., given focus, brought to the front, etc.). If 'use_existing' is False, then a new editor will be created even if one already exists. """ if use_existing: # Is the object already being edited in the window? editor = self.get_editor(obj, kind) if editor is not None: # If so, activate the existing editor (i.e., bring it to the # front, give it the focus etc). self.activate_editor(editor) return editor # Otherwise, create an editor for it. editor = self.create_editor(obj, kind) if editor is None: logger.warn('no editor for object %s', obj) self.add_editor(editor) self.activate_editor(editor) return editor def get_editor(self, obj, kind=None): """ Return the editor that is editing an object. Return None if no such editor exists. """ return self.editor_manager.get_editor(self, obj, kind) def get_editor_by_id(self, id): """ Return the editor with the specified Id. Return None if no such editor exists. """ for editor in self.editors: if editor.id == id: break else: editor = None return editor def get_part_by_id(self, id): """ Return the workbench part with the specified Id. Return None if no such part exists. """ return self.get_view_by_id(id) or self.get_editor_by_id(id) def get_perspective_by_id(self, id): """ Return the perspective with the specified Id. Return None if no such perspective exists. """ for perspective in self.perspectives: if perspective.id == id: break else: if id == Perspective.DEFAULT_ID: perspective = Perspective() else: perspective = None return perspective def get_perspective_by_name(self, name): """ Return the perspective with the specified name. Return None if no such perspective exists. """ for perspective in self.perspectives: if perspective.name == name: break else: perspective = None return perspective def get_view_by_id(self, id): """ Return the view with the specified Id. Return None if no such view exists. """ for view in self.views: if view.id == id: break else: view = None return view def hide_editor_area(self): """ Hide the editor area. """ self.layout.hide_editor_area() return def hide_view(self, view): """ Hide a view. """ self.layout.hide_view(view) return def refresh(self): """ Refresh the window to reflect any changes. """ self.layout.refresh() return def reset_active_perspective(self): """ Reset the active perspective back to its original contents. """ perspective = self.active_perspective # If the perspective has been seen before then delete its memento. if perspective.id in self._memento.perspective_mementos: # Remove the perspective's memento. del self._memento.perspective_mementos[perspective.id] # Re-display the perspective (because a memento no longer exists for # the perspective, its 'create_contents' method will be called again). self._show_perspective(perspective, perspective) return def reset_all_perspectives(self): """ Reset all perspectives back to their original contents. """ # Remove all perspective mementos (except user perspectives). for id in self._memento.perspective_mementos.keys(): if not id.startswith('__user_perspective'): del self._memento.perspective_mementos[id] # Re-display the active perspective. self._show_perspective(self.active_perspective,self.active_perspective) return def reset_editors(self): """ Activate the first editor in every tab. """ self.layout.reset_editors() return def reset_views(self): """ Activate the first view in every tab. """ self.layout.reset_views() return def show_editor_area(self): """ Show the editor area. """ self.layout.show_editor_area() return def show_view(self, view): """ Show a view. """ # If the view is already in the window layout, but hidden, then just # show it. # # fixme: This is a little gorpy, reaching into the window layout here, # but currently this is the only thing that knows whether or not the # view exists but is hidden. if self.layout.contains_view(view): self.layout.show_view(view) # Otherwise, we have to add the view to the layout. else: self._add_view_in_default_position(view) self.refresh() return #### Methods for saving and restoring the layout ########################## def get_memento(self): """ Return the state of the window suitable for pickling etc. """ # The size and position of the window. self._memento.size = self.size self._memento.position = self.position # The Id of the active perspective. self._memento.active_perspective_id = self.active_perspective.id # The layout of the active perspective. self._memento.perspective_mementos[self.active_perspective.id] = ( self.layout.get_view_memento(), self.active_view and self.active_view.id or None ) # The layout of the editor area. self._memento.editor_area_memento = self.layout.get_editor_memento() return self._memento def set_memento(self, memento): """ Restore the state of the window from a memento. """ # All we do here is save a reference to the memento - we don't actually # do anything with it until the window is opened. # # This obviously means that you can't set the memento of a window # that is already open, but I can't see a use case for that anyway! self._memento = memento return ########################################################################### # Private interface. ########################################################################### def _add_view_in_default_position(self, view): """ Adds a view in its 'default' position. """ # Is the view in the current perspectives contents list? If it is then # we use the positioning information in the perspective item. Otherwise # we will use the default positioning specified in the view itself. item = self._get_perspective_item(self.active_perspective, view) if item is None: item = view # fixme: This only works because 'PerspectiveItem' and 'View' have the # identical 'position', 'relative_to', 'width' and 'height' traits! We # need to unify these somehow! relative_to = self.get_view_by_id(item.relative_to) size = (item.width, item.height) self.add_view(view, item.position, relative_to, size) return def _get_initial_perspective(self, *methods): """ Return the initial perspective. """ methods = [ # If a default perspective was specified then we prefer that over # any other perspective. self._get_default_perspective, # If there was no default perspective then try the perspective that # was active the last time the application was run. self._get_previous_perspective, # If there was no previous perspective, then try the first one that # we know about. self._get_first_perspective ] for method in methods: perspective = method() if perspective is not None: break # If we have no known perspectives, make a new blank one up. else: logger.warn('no known perspectives - creating a new one') perspective = Perspective() return perspective def _get_default_perspective(self): """ Return the default perspective. Return None if no default perspective was specified or it no longer exists. """ id = self.default_perspective_id if len(id) > 0: perspective = self.get_perspective_by_id(id) if perspective is None: logger.warn('default perspective %s no longer available', id) else: perspective = None return perspective def _get_previous_perspective(self): """ Return the previous perspective. Return None if there has been no previous perspective or it no longer exists. """ id = self._memento.active_perspective_id if len(id) > 0: perspective = self.get_perspective_by_id(id) if perspective is None: logger.warn('previous perspective %s no longer available', id) else: perspective = None return perspective def _get_first_perspective(self): """ Return the first perspective in our list of perspectives. Return None if no perspectives have been defined. """ if len(self.perspectives) > 0: perspective = self.perspectives[0] else: perspective = None return perspective def _get_perspective_item(self, perspective, view): """ Return the perspective item for a view. Return None if the view is not mentioned in the perspectives contents. """ # fixme: Errrr, shouldn't this be a method on the window?!? for item in perspective.contents: if item.id == view.id: break else: item = None return item def _hide_perspective(self, perspective): """ Hide a perspective. """ # fixme: This is a bit ugly but... when we restore the layout we ignore # the default view visibility. for view in self.views: view.visible = False # Save the current layout of the perspective. self._memento.perspective_mementos[perspective.id] = ( self.layout.get_view_memento(), self.active_view and self.active_view.id or None ) return def _show_perspective(self, old, new): """ Show a perspective. """ # If the perspective has been seen before then restore it. memento = self._memento.perspective_mementos.get(new.id) if memento is not None: view_memento, active_view_id = memento self.layout.set_view_memento(view_memento) # Make sure the active part, view and editor reflect the new # perspective. view = self.get_view_by_id(active_view_id) if view is not None: self.active_view = view # Otherwise, this is the first time the perspective has been seen # so create it. else: if old is not None: # Reset the window layout to its initial state. self.layout.set_view_memento(self._initial_layout) # Create the perspective in the window. new.create(self) # Make sure the active part, view and editor reflect the new # perspective. self.active_view = None # Show the editor area? if new.show_editor_area: self.show_editor_area() else: self.hide_editor_area() self.active_editor = None # Inform the perspective that it has been shown. new.show(self) # This forces the dock window to update its layout. if old is not None: self.refresh() return def _restore_contents(self): """ Restore the contents of the window. """ self.layout.set_editor_memento(self._memento.editor_area_memento) self.size = self._memento.size self.position = self._memento.position return #### Trait change handlers ################################################ #### Static #### def _active_perspective_changed(self, old, new): """ Static trait change handler. """ logger.debug('active perspective changed from <%s> to <%s>', old, new) # Hide the old perspective... if old is not None: self._hide_perspective(old) # ... and show the new one. if new is not None: self._show_perspective(old, new) return def _active_editor_changed(self, old, new): """ Static trait change handler. """ logger.debug('active editor changed from <%s> to <%s>', old, new) self.active_part = new return def _active_part_changed(self, old, new): """ Static trait change handler. """ if new is None: self.selection = [] else: self.selection = new.selection logger.debug('active part changed from <%s> to <%s>', old, new) return def _active_view_changed(self, old, new): """ Static trait change handler. """ logger.debug('active view changed from <%s> to <%s>', old, new) self.active_part = new return def _views_changed(self, old, new): """ Static trait change handler. """ # Cleanup any old views. for view in old: view.window = None # Initialize any new views. for view in new: view.window = self return def _views_items_changed(self, event): """ Static trait change handler. """ # Cleanup any old views. for view in event.removed: view.window = None # Initialize any new views. for view in event.added: view.window = self return #### Dynamic #### @on_trait_change('layout.editor_closed') def _on_editor_closed(self, editor): """ Dynamic trait change handler. """ index = self.editors.index(editor) del self.editors[index] if editor is self.active_editor: if len(self.editors) > 0: index = min(index, len(self.editors) - 1) # If the user closed the editor manually then this method is # being called from a toolkit-specific event handler. Because # of that we have to make sure that we don't change the focus # from within this method directly hence we activate the editor # later in the GUI thread. GUI.invoke_later(self.activate_editor, self.editors[index]) else: self.active_editor = None return @on_trait_change('editors.has_focus') def _on_editor_has_focus_changed(self, obj, trait_name, old, new): """ Dynamic trait change handler. """ if trait_name == 'has_focus' and new: self.active_editor = obj return @on_trait_change('views.has_focus') def _has_focus_changed_for_view(self, obj, trait_name, old, new): """ Dynamic trait change handler. """ if trait_name == 'has_focus' and new: self.active_view = obj return @on_trait_change('views.visible') def _visible_changed_for_view(self, obj, trait_name, old, new): """ Dynamic trait change handler. """ if trait_name == 'visible': if not new: if obj is self.active_view: self.active_view = None return
class ActionManager(HasTraits): """ Abstract base class for all action managers. An action manager contains a list of groups, with each group containing a list of items. There are currently three concrete sub-classes:- 1) 'MenuBarManager' 2) 'MenuManager' 3) 'ToolBarManager' """ #### 'ActionManager' interface ############################################ # The Id of the default group. DEFAULT_GROUP = Constant('additions') # The action controller (if any) used to control how actions are performed. controller = Instance(ActionController) # Is the action manager enabled? enabled = Bool(True) # All of the contribution groups in the manager. groups = Property(List(Group)) # The manager's unique identifier (if it has one). id = Str # Is the action manager visible? visible = Bool(True) #### Events #### # fixme: We probably need more granular events than this! changed = Event #### Private interface #################################################### # All of the contribution groups in the manager. _groups = List(Group) ########################################################################### # 'object' interface. ########################################################################### def __init__(self, *args, **traits): """ Creates a new menu manager. """ # Base class constructor. super(ActionManager, self).__init__(**traits) # The last group in every manager is the group with Id 'additions'. # # fixme: The side-effect of this is to ensure that the 'additions' # group has been created. Is the 'additions' group even a good idea? group = self._get_default_group() # Add all items to the manager. for arg in args: # We allow a group to be defined by simply specifying a string (its # Id). if isinstance(arg, basestring): # Create a group with the specified Id. arg = Group(id=arg) # If the item is a group then add it just before the default group # (ie. we always keep the default group as the last group in the # manager). if isinstance(arg, Group): self.insert(-1, arg) group = arg # Otherwise, the item is an action manager item so add it to the # current group. else: ## # If no group has been created then add one. This is only ## # relevant when using the 'shorthand' way to define menus. ## if group is None: ## group = Group(id='__first__') ## self.insert(-1, group) group.append(arg) return ########################################################################### # 'ActionManager' interface. ########################################################################### #### Trait properties ##################################################### def _get_groups(self): """ Returns the groups in the manager. """ return self._groups[:] #### Trait change handlers ################################################ def _enabled_changed(self, trait_name, old, new): """ Static trait change handler. """ for group in self._groups: group.enabled = new return def _visible_changed(self, trait_name, old, new): """ Static trait change handler. """ for group in self._groups: group.visible = new return #### Methods ############################################################## def append(self, item): """ Append an item to the manager. See the documentation for 'insert'. """ return self.insert(len(self._groups), item) def destroy(self): """ Called when the manager is no longer required. By default this method simply calls 'destroy' on all of the manager's groups. """ for group in self.groups: group.destroy() return def insert(self, index, item): """ Insert an item into the manager at the specified index. The item can be:- 1) A 'Group' instance. In which case the group is inserted into the manager's list of groups. 2) A string. In which case a 'Group' instance is created with that Id, and then inserted into the manager's list of groups. 3) An 'ActionManagerItem' instance. In which case the item is inserted into the manager's default group. """ # 1) The item is a 'Group' instance. if isinstance(item, Group): group = item # Insert the group into the manager. group.parent = self self._groups.insert(index, item) # 2) The item is a string. elif isinstance(item, basestring): # Create a group with that Id. group = Group(id=item) # Insert the group into the manager. group.parent = self self._groups.insert(index, group) # 3) The item is an 'ActionManagerItem' instance. else: # Find the default group. group = self._get_default_group() # Insert the item into the default group. group.insert(index, item) return group def find_group(self, id): """ Return the group with the specified Id. Return None if no such group exists. """ for group in self._groups: if group.id == id: break else: group = None return group def find_item(self, path): """ Return the item found at the specified path. 'path' is a '/' separated list of contribution Ids. Returns None if any component of the path is not found. """ components = path.split('/') # If there is only one component, then the path is just an Id so look # it up in this manager. if len(components) > 0: item = self._find_item(components[0]) if len(components) > 1 and item is not None: item = item.find_item('/'.join(components[1:])) else: item = None return item def walk(self, fn): """ Walk the manager applying a function at every item. """ fn(self) for group in self._groups: self.walk_group(group, fn) return def walk_group(self, group, fn): """ Walk a group applying a function at every item. """ fn(group) for item in group.items: if isinstance(item, Group): self.walk_group(item, fn) else: self.walk_item(item, fn) return def walk_item(self, item, fn): """ Walk an item (may be a sub-menu manager remember!). """ if hasattr(item, 'groups'): item.walk(fn) else: fn(item) return ########################################################################### # Private interface. ########################################################################### def _get_default_group(self): """ Returns the manager's default group. """ group = self.find_group(self.DEFAULT_GROUP) if group is None: group = Group(id=self.DEFAULT_GROUP) self.append(group) return group def _find_item(self, id): """ Returns the item with the specified Id. Returns None if no such item exists. """ for group in self.groups: item = group.find(id) if item is not None: break else: item = None return item ########################################################################### # Debugging interface. ########################################################################### def dump(self, indent=''): """ Render a manager! """ print indent, 'Manager', self.id indent += ' ' for group in self._groups: self.render_group(group, indent) return def render_group(self, group, indent=''): """ Render a group! """ print indent, 'Group', group.id indent += ' ' for item in group.items: if isinstance(item, Group): print 'Surely, a group cannot contain another group!!!!' self.render_group(item, indent) else: self.render_item(item, indent) return def render_item(self, item, indent=''): """ Render an item! """ if hasattr(item, 'groups'): item.dump(indent) else: print indent, 'Item', item.id return