Example #1
0
    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 callable( item ):
            item = Action( text       = user_name_for( item.func_name ),
                           on_perform = item )

        if isinstance( item, Action ):
            item = ActionItem( action = item, parent = self )

        self._items.insert( index, item )

        return item
Example #2
0
    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 )
        facet  = object.base_facet( name )
        label  = user_name_for( name )
        tlabel = facet.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

            return tlabel

        return tlabel( object, name, label )
    def dock_control_for ( self, parent, object ):
        """ Returns the DockControl object for a specified object.
        """
        try:
            name = object.name
        except:
            try:
                name = object.label
            except:
                name = ''

        if name == '':
            name = user_name_for( object.__class__.__name__ )

        image  = None
        export = ''
        if isinstance( object, DockControl ):
            dock_control = object
            image        = dock_control.image
            export       = dock_control.export
            dockable     = dock_control.dockable
            close        = dockable.dockable_should_close()
            if close:
                dock_control.close( force = True )

            control = dockable.dockable_get_control( parent )

            # If DockControl was closed, then reset it to point to the new
            # control:
            if close:
                dock_control.set( control = control,
                                  style   = parent.owner.style )
                dockable.dockable_init_dockcontrol( dock_control )
                return dock_control

        elif isinstance( object, IDockable ):
            dockable = object
            control  = dockable.dockable_get_control( parent )
        else:
            ui       = object.get_dockable_ui( parent )
            dockable = DockableViewElement( ui = ui )
            export   = ui.view.export
            control  = ui.control

        dc = DockControl( control   = control,
                          name      = name,
                          export    = export,
                          style     = parent.owner.style,
                          image     = image,
                          closeable = True )

        dockable.dockable_init_dockcontrol( dc )

        return dc
    def _create_page ( self, object ):
        """ Creates and returns a notebook page for a specified object with
            facets.
        """
        # Create a new notebook page:
        page = self.notebook.create_page().set( data = object )

        # Create the Facets UI for the object to put in the notebook page:
        ui = object.edit_facets( parent = page.parent,
                                 view   = self.factory.view,
                                 kind   = 'editor' ).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[:1] == '.':
            if getattr( object, page_name[1:], Undefined ) is not Undefined:
                page.register_name_listener( object, page_name[1:] )
        else:
            name = page_name

        if name == '':
            name = user_name_for( object.__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

        # Save the Facets UI in the page so it can dispose of it later:
        page.ui = ui

        # Return the new notebook page:
        return page
Example #5
0
    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 facets 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 facet (for use by the View):
                name = 'extension_%d' % i
                setattr( self, name, extension )

                extension_view = extension

                # Sync up the 'file_name' facet with the extension:
                self.sync_facet( '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_facets_interface( IFileDialogView ):
                    extension_view = default_view

                # Get the view that the extension wants to use:
                view = extension.facet_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',
                          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
        )
Example #6
0
    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 )

        self.facet_setq( view = factory.view )
        self.sync_value( factory.view_name, 'view', 'from' )
        ui = view_object.edit_facets(
            parent = self.control,
            view   = self.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[: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_facet_set(
                    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(
            max_tab_length = ( 256, 30 )[ factory.allow_tabs ] ).set(
            control        = ui.control,
            id             = name,
            name           = name,
            image          = image,
            export         = factory.export,
            closeable      = factory.deletable,
            dockable       = DockableListElement( ui = ui, editor = self )
        )
        self.set_dock_control_listener( dock_control )

        return ( dock_control, view_object, monitoring )
 def _label_default ( self ):
     return user_name_for( self.name )
    def _list_for ( self, object, name, indices, values, label, depth ):
        """ Returns the list of all PropertySheetIndexedItem objects that belong
            to the specified list group.
        """
        adapter = self.property_sheet_adapter
        items   = []
        owner   = None
        if adapter.get_show_group( object, name ):
            owner = self._current_group_for( object, name )
            if owner is None:
                owner = PropertySheetGroup(
                    is_open = adapter.get_is_open( object, name )
                )
                self._current_group_for( object, name, owner )

            owner.set(
                label  = label,
                indent = depth * INDENT,
                count  = len( values )
            )
            items.append( owner )
            depth += 1

        editor      = adapter.get_editor( object, name )
        labels      = adapter.get_labels( object, name )
        change_mode = adapter.get_change_mode( object, name )
        mode        = 'readonly'
        if not self.factory.readonly:
            mode = adapter.get_mode( object, name )

        if indices is None:
            indices = range( len( values ) )
            if labels is None:
                labels  = [ '' ] * len( values )
        elif labels is None:
            labels = [ user_name_for( str( index ) ) for index in indices ]

        indexed_editor = isinstance( editor, IndexTypes )
        indexed_mode   = isinstance( mode,   IndexTypes )

        for label, index, value in zip( labels, indices, values ):
            item_editor = indexed_item( index, editor, indexed_editor )
            item_mode   = indexed_item( index, mode,   indexed_mode   )
            if ((item_editor is None) or
                (isinstance( item_editor, ItemEditors ) and
                 (item_mode == 'inline'))):
                item_editor = TextEditor( evaluate = type( value ),
                                          strict   = True )

            items.append( PropertySheetIndexedItem(
                object      = object,
                name        = name,
                adapter     = adapter,
                label       = label,
                index       = index,
                value       = value,
                editor      = item_editor,
                mode        = item_mode,
                change_mode = change_mode,
                indent      = depth * INDENT,
                owner       = owner
            ) )

        return items