示例#1
0
class PanelBasicExample(VerticalLayout, IClickListener):

    def __init__(self):
        super(PanelBasicExample, self).__init__()

        self.setSpacing(True)

        # Panel 1 - with caption
        self._panel = Panel('This is a standard Panel')
        self._panel.setHeight('200px')  # we want scrollbars
        # let's adjust the panels default layout (a VerticalLayout)
        layout = self._panel.getContent()
        layout.setMargin(True)  # we want a margin
        layout.setSpacing(True)  # and spacing between components
        self.addComponent(self._panel)

        # Let's add a few rows to provoke scrollbars:
        for _ in range(20):
            l = Label('The quick brown fox jumps over the lazy dog.')
            self._panel.addComponent(l)

        # Caption toggle:
        b = Button('Toggle caption')
        b.addListener(self, IClickListener)
        self.addComponent(b)


    def buttonClick(self, event):
        if self._panel.getCaption() == '':
            self._panel.setCaption('This is a standard Panel')
        else:
            self._panel.setCaption('')
示例#2
0
class PanelLightExample(VerticalLayout, IClickListener):

    def __init__(self):
        super(PanelLightExample, self).__init__()

        self.setSpacing(True)
        self.setSpacing(True)

        # Panel 1 - with caption
        self._panel = Panel('This is a light Panel')
        self._panel.setStyleName(Reindeer.PANEL_LIGHT)
        self._panel.setHeight('200px')  # we want scrollbars
        # let's adjust the panels default layout (a VerticalLayout)
        layout = self._panel.getContent()
        layout.setMargin(True)  # we want a margin
        layout.setSpacing(True)
        # and spacing between components
        self.addComponent(self._panel)

        # Let's add a few rows to provoke scrollbars:
        for _ in range(20):
            l = Label('The quick brown fox jumps over the lazy dog.')
            self._panel.addComponent(l)

        # Caption toggle:
        b = Button('Toggle caption')
        b.addListener(self, IClickListener)
        self.addComponent(b)


    def buttonClick(self, event):
        if self._panel.getCaption() is None:
            self._panel.setCaption('This is a light Panel')
        else:
            self._panel.setCaption(None)
示例#3
0
    def __init__(self):
        super(ApplicationLayoutWindow, self).__init__()

        # Our main layout is a horizontal layout
        main = HorizontalLayout()
        main.setSizeFull()
        self.setContent(main)

        # Tree to the left
        treePanel = Panel()  # for scrollbars
        treePanel.setStyleName(Reindeer.PANEL_LIGHT)
        treePanel.setHeight('100%')
        treePanel.setWidth(None)
        treePanel.getContent().setSizeUndefined()
        self.addComponent(treePanel)

        tree = Tree()
        tree.setContainerDataSource(ExampleUtil.getHardwareContainer())
        tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME)
        for idd in tree.rootItemIds():
            tree.expandItemsRecursively(idd)
        treePanel.addComponent(tree)

        # vertically divide the right area
        left = VerticalLayout()
        left.setSizeFull()
        self.addComponent(left)
        main.setExpandRatio(left, 1.0)  # use all available space

        # table on top
        tbl = Table()
        tbl.setWidth('100%')
        tbl.setContainerDataSource(ExampleUtil.getISO3166Container())
        tbl.setSortDisabled(True)
        tbl.setPageLength(7)
        left.addComponent(tbl)

        # Label on bottom
        textPanel = Panel()  # for scrollbars
        textPanel.setStyleName(Reindeer.PANEL_LIGHT)
        textPanel.setSizeFull()
        left.addComponent(textPanel)
        left.setExpandRatio(textPanel, 1.0)  # use all available space

        text = Label(ExampleUtil.lorem, Label.CONTENT_XHTML)
        text.setWidth('500px')  # some limit is good for text
        textPanel.addComponent(text)
    def __init__(self):
        super(ApplicationLayoutWindow, self).__init__()

        # Our main layout is a horizontal layout
        main = HorizontalLayout()
        main.setSizeFull()
        self.setContent(main)

        # Tree to the left
        treePanel = Panel()  # for scrollbars
        treePanel.setStyleName(Reindeer.PANEL_LIGHT)
        treePanel.setHeight('100%')
        treePanel.setWidth(None)
        treePanel.getContent().setSizeUndefined()
        self.addComponent(treePanel)

        tree = Tree()
        tree.setContainerDataSource(ExampleUtil.getHardwareContainer())
        tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME)
        for idd in tree.rootItemIds():
            tree.expandItemsRecursively(idd)
        treePanel.addComponent(tree)

        # vertically divide the right area
        left = VerticalLayout()
        left.setSizeFull()
        self.addComponent(left)
        main.setExpandRatio(left, 1.0)  # use all available space

        # table on top
        tbl = Table()
        tbl.setWidth('100%')
        tbl.setContainerDataSource(ExampleUtil.getISO3166Container())
        tbl.setSortDisabled(True)
        tbl.setPageLength(7)
        left.addComponent(tbl)

        # Label on bottom
        textPanel = Panel()  # for scrollbars
        textPanel.setStyleName(Reindeer.PANEL_LIGHT)
        textPanel.setSizeFull()
        left.addComponent(textPanel)
        left.setExpandRatio(textPanel, 1.0)  # use all available space

        text = Label(ExampleUtil.lorem, Label.CONTENT_XHTML)
        text.setWidth('500px')  # some limit is good for text
        textPanel.addComponent(text)
示例#5
0
    def __init__(self):
        super(CssLayoutsExample, self).__init__()

        self.setMargin(True)

        # Note, that this code example may not be self explanatory without
        # checking out the related CSS file in the sampler theme.

        panel = Panel('Panel')
        panel.setStyleName('floatedpanel')
        panel.setWidth('30%')
        panel.setHeight('370px')
        panel.addComponent(
            Label('This panel is 30% wide ' +
                  'and 370px high (defined on the server side) ' +
                  'and floated right (with custom css). ' +
                  'Try resizing the browser window to see ' +
                  'how the black boxes (floated left) ' +
                  'behave. Every third of them has colored text ' +
                  'to demonstrate the dynamic css injection.'))

        bottomCenter = Label(
            'I\'m a 3 inches wide footer at the bottom of the layout')
        bottomCenter.setSizeUndefined()  # disable 100% default width
        bottomCenter.setStyleName('footer')

        cssLayout = MyCssLayout()
        cssLayout.setWidth('100%')

        cssLayout.addComponent(panel)
        for _ in range(15):
            # add black labels that float left
            cssLayout.addComponent(Brick())

        cssLayout.addComponent(bottomCenter)

        self.addComponent(cssLayout)
示例#6
0
    def __init__(self):
        super(CssLayoutsExample, self).__init__()

        self.setMargin(True)

        # Note, that this code example may not be self explanatory without
        # checking out the related CSS file in the sampler theme.

        panel = Panel('Panel')
        panel.setStyleName('floatedpanel')
        panel.setWidth('30%')
        panel.setHeight('370px')
        panel.addComponent(Label('This panel is 30% wide '
                + 'and 370px high (defined on the server side) '
                + 'and floated right (with custom css). '
                + 'Try resizing the browser window to see '
                + 'how the black boxes (floated left) '
                + 'behave. Every third of them has colored text '
                + 'to demonstrate the dynamic css injection.'))

        bottomCenter = Label(
                'I\'m a 3 inches wide footer at the bottom of the layout')
        bottomCenter.setSizeUndefined()  # disable 100% default width
        bottomCenter.setStyleName('footer')

        cssLayout = MyCssLayout()
        cssLayout.setWidth('100%')

        cssLayout.addComponent(panel)
        for _ in range(15):
            # add black labels that float left
            cssLayout.addComponent(Brick())

        cssLayout.addComponent(bottomCenter)

        self.addComponent(cssLayout)
示例#7
0
class MuntjacComponent(MuntjacBaseComponent, AbstractTkComponent):
    """ A Muntjac implementation of Component.

    A MuntjacComponent is not meant to be used directly. It provides some
    common functionality that is useful to all widgets and should
    serve as the base class for all other classes.

    .. note:: This is not a HasTraits class.

    """
    #: The Muntjac widget created by the component
    widget = None

    #--------------------------------------------------------------------------
    # Setup Methods
    #--------------------------------------------------------------------------
    def create(self, parent):
        """ Creates the underlying Muntjac widget.

        """
        self.widget = Panel()
        parent.addComponent(self.widget)

    def initialize(self):
        """ Initializes the attributes of the Qt widget.

        """
        super(MuntjacComponent, self).initialize()
        shell = self.shell_obj
        self.set_enabled(shell.enabled)
        self.set_visible(shell.visible)

    #--------------------------------------------------------------------------
    # Abstract Implementation
    #--------------------------------------------------------------------------
    @property
    def toolkit_widget(self):
        """ A property that returns the toolkit specific widget for this
        component.

        """
        return self.widget

    def size(self):
        """ Returns the size of the internal toolkit widget, ignoring any
        windowing decorations, as a (width, height) tuple of integers.

        """
        widget = self.widget
        return (widget.getWidth(), widget.getHeight())

    def size_hint(self):
        """ Returns a (width, height) tuple of integers which represent
        the suggested size of the widget for its current state, ignoring
        any windowing decorations. This value is used by the layout
        manager to determine how much space to allocate the widget.

        """
        return self.size()

    def resize(self, width, height):
        """ Resizes the internal toolkit widget according the given
        width and height integers, ignoring any windowing decorations.

        """
        self.widget.setWidth(width)
        self.widget.setHeight(height)

    def min_size(self):
        """ Returns the hard minimum (width, height) of the widget,
        ignoring any windowing decorations. A widget will not be able
        to be resized smaller than this value

        """
        widget = self.widget
        return (widget.getWidth(), widget.getHeight())

    def set_min_size(self, min_width, min_height):
        """ Set the hard minimum width and height of the widget, ignoring
        any windowing decorations. A widget will not be able to be resized
        smaller than this value.

        """
        self.widget.setWidth(min_width)
        self.widget.setHeight(min_height)

    def pos(self):
        """ Returns the position of the internal toolkit widget as an
        (x, y) tuple of integers, including any windowing decorations.
        The coordinates should be relative to the origin of the widget's
        parent, or to the screen if the widget is toplevel.

        """
        widget = self.widget
        return (widget.getPositionX(), widget.getPositionY())

    def move(self, x, y):
        """ Moves the internal toolkit widget according to the given
        x and y integers which are relative to the origin of the
        widget's parent and includes any windowing decorations.

        """
        self.widget.setPositionX(x)
        self.widget.setPositionY(y)

    def frame_geometry(self):
        """ Returns an (x, y, width, height) tuple of geometry info
        for the internal toolkit widget, including any windowing
        decorations.

        """
        widget = self.widget
        x = widget.getPositionX()
        y = widget.getPositionY()
        width = widget.getWidth()
        height = widget.getHeight()
        return (x, y, width, height)

    def geometry(self):
        """ Returns an (x, y, width, height) tuple of geometry info
        for the internal toolkit widget, ignoring any windowing
        decorations.

        """
        widget = self.widget
        x = widget.getPositionX()
        y = widget.getPositionY()
        width = widget.getWidth()
        height = widget.getHeight()
        return (x, y, width, height)

    def set_geometry(self, x, y, width, height):
        """ Sets the geometry of the internal widget to the given
        x, y, width, and height values, ignoring any windowing
        decorations.

        """
        widget = self.widget
        widget.setPositionX(x)
        widget.setPositionY(y)
        widget.setWidth(width)
        widget.setHeight(height)

    #--------------------------------------------------------------------------
    # Shell Object Change Handlers
    #--------------------------------------------------------------------------
    def shell_enabled_changed(self, enabled):
        """ The change handler for the 'enabled' attribute on the shell
        object.

        """
        self.set_enabled(enabled)

    def shell_visible_changed(self, visible):
        """ The change handler for the 'visible' attribute on the shell
        object.

        """
        self.set_visible(visible)

    def shell_bg_color_changed(self, color):
        """ The change handler for the 'bg_color' attribute on the shell
        object. Sets the background color of the internal widget to the
        given color.

        """
        self.set_bg_color(color)

    def shell_fg_color_changed(self, color):
        """ The change handler for the 'fg_color' attribute on the shell
        object. Sets the foreground color of the internal widget to the
        given color.

        """
        self.set_fb_color(color)

    def shell_font_changed(self, font):
        """ The change handler for the 'font' attribute on the shell
        object. Sets the font of the internal widget to the given font.

        """
        self.set_font(font)

    #--------------------------------------------------------------------------
    # Widget Update Methods
    #--------------------------------------------------------------------------
    def set_enabled(self, enabled):
        """ Enable or disable the widget.

        """
        self.widget.setEnabled(enabled)

    def set_visible(self, visible):
        """ Show or hide the widget.

        """
        self.shell_obj.parent.set_needs_update_constraints()
        self.widget.setVisible(visible)

    def set_bg_color(self, color):
        """ Set the background color of the widget.

        """
        pass

    def set_fg_color(self, color):
        """ Set the foreground color of the widget.

        """
        pass

    def set_font(self, font):
        """ Set the font of the underlying toolkit widget to an
        appropriate QFont.

        """
        pass

    #--------------------------------------------------------------------------
    # Convenienence methods
    #--------------------------------------------------------------------------

    def child_widgets(self):
        """ Iterates over the shell widget's children and yields the
        toolkit widgets for those children.

        """
        for child in self.shell_obj.children:
            yield child.toolkit_widget

    def _get_layout_margins(self, widget):
        """ Compute the size of the margins between the layout rectangle and the
        widget drawing rectangle.

        """
        pass

    def _reset_layout_margins(self):
        """ Reset the layout margins for this widget.

        """
        pass
示例#8
0
文件: ui_panel.py 项目: rwl/traitsui
    def _add_items(self, content, outer=None):
        """Adds a list of Item objects, creating a layout if needed.  Return
           the outermost layout.
        """
        # Get local references to various objects we need:
        ui = self.ui
        info = ui.info
        handler = ui.handler

        group = self.group
        show_left = group.show_left
        padding = group.padding
        columns = group.columns

        # See if a label is needed.
        show_labels = False
        for item in content:
            show_labels |= item.show_label

        # See if a grid layout is needed.
        if show_labels or columns > 1:
            inner = GridLayout(columns * 2, len(content))
            inner.setSpacing(True)
            inner.setSizeUndefined()

            if outer is None:
                outer = inner
            else:
                outer.addComponent(inner)

            row = 0
            if show_left:
                label_alignment = Alignment.MIDDLE_RIGHT
            else:
                label_alignment = Alignment.MIDDLE_LEFT

        else:
            # Use the existing layout if there is one.
            if outer is None:
                if self.horizontal:
                    outer = HorizontalLayout()
                else:
                    outer = VerticalLayout()
                outer.setSizeUndefined()

            inner = outer

            row = -1
            label_alignment = 0

        # Process each Item in the list:
        col = -1
        for item in content:

            # Keep a track of the current logical row and column unless the
            # layout is not a grid.
            col += 1
            if row >= 0 and col >= columns:
                col = 0
                row += 1

            # Get the name in order to determine its type:
            name = item.name

            # Check if is a label:
            if name == '':
                label = item.label
                if label != "":

                    # Create the label widget.
                    if item.style == 'simple':
                        label = Label(label)
                    else:
                        label = heading_text(None, text=label).control

                    self._add_widget(inner, label, row, col, show_labels)

                    if item.emphasized:
                        self._add_emphasis(label)

                # Continue on to the next Item in the list:
                continue

            # Check if it is a separator:
            if name == '_':
                cols = columns

                # See if the layout is a grid.
                if row >= 0:
                    # Move to the start of the next row if necessary.
                    if col > 0:
                        col = 0
                        row += 1

#                    # Skip the row we are about to do.
#                    row += 1

                    # Allow for the columns.
                    if show_labels:
                        cols *= 2

                for i in range(cols):
                    if self.horizontal:
                        # Add a vertical separator:
                        line = Panel()
                        line.setWidth('2px')
                        line.setHeight('-1px')
                        if row < 0:
                            inner.addComponent(line)
                        else:
                            inner.addComponent(line, row, i)
                    else:
                        # Add a horizontal separator:
                        line = Label('<hr />', Label.CONTENT_XHTML)
                        line.setWidth('100%')  # FIXME: explicit container size
                        if row < 0:
                            inner.addComponent(line)
                        else:
                            inner.addComponent(line, i, row)

                # Continue on to the next Item in the list:
                continue

            # Convert a blank to a 5 pixel spacer:
            if name == ' ':
                name = '5'

            # Check if it is a spacer:
            if all_digits.match( name ):

                # If so, add the appropriate amount of space to the layout:
                spacer = Label('')
                if self.horizontal:
                    # Add a horizontal spacer:
                    spacer.setWidth(name + 'px')
                else:
                    # Add a vertical spacer:
                    spacer.setHeight(name + 'px')

                self._add_widget(inner, spacer, row, col, show_labels)

                # Continue on to the next Item in the list:
                continue

            # Otherwise, it must be a trait Item:
            object      = eval( item.object_, globals(), ui.context )
            trait       = object.base_trait( name )
            desc        = trait.desc or ''
            fixed_width = False

            # Handle any label.
            if item.show_label:
                label = self._create_label(item, ui, desc)
                self._add_widget(inner, label, row, col, show_labels,
                                 label_alignment)
            else:
                label = None

            # Get the editor factory associated with the Item:
            editor_factory = item.editor
            if editor_factory is None:
                editor_factory = trait.get_editor()

                # If still no editor factory found, use a default text editor:
                if editor_factory is None:
                    from text_editor import ToolkitEditorFactory
                    editor_factory = ToolkitEditorFactory()

                # If the item has formatting traits set them in the editor
                # factory:
                if item.format_func is not None:
                    editor_factory.format_func = item.format_func

                if item.format_str != '':
                    editor_factory.format_str = item.format_str

                # If the item has an invalid state extended trait name, set it
                # in the editor factory:
                if item.invalid != '':
                    editor_factory.invalid = item.invalid

            # Create the requested type of editor from the editor factory:
            factory_method = getattr( editor_factory, item.style + '_editor' )
            editor         = factory_method( ui, object, name, item.tooltip,
                                        None).set(
                                 item        = item,
                                 object_name = item.object )

            # Tell the editor to actually build the editing widget.  Note that
            # "inner" is a layout.  This shouldn't matter as individual editors
            # shouldn't be using it as a parent anyway.  The important thing is
            # that it is not None (otherwise the main TraitsUI code can change
            # the "kind" of the created UI object).
            editor.prepare(inner)
            control = editor.control

            # Set the initial 'enabled' state of the editor from the factory:
            editor.enabled = editor_factory.enabled

            # Add emphasis to the editor control if requested:
            if item.emphasized:
                self._add_emphasis(control)

            # Give the editor focus if it requested it:
            if item.has_focus:
                control.focus()

            # Set the correct size on the control, as specified by the user:
            stretch = 0
            scrollable = editor.scrollable
            item_width = item.width
            item_height = item.height
            if (item_width != -1) or (item_height != -1):
                is_horizontal = self.horizontal

                min_size = control.minimumSizeHint()
                width = min_size.width()
                height = min_size.height()

                if (0.0 < item_width <= 1.0) and is_horizontal:
                    stretch = int(100 * item_width)

                item_width = int(item_width)
                if item_width < -1:
                    item_width  = -item_width
                else:
                    item_width = max(item_width, width)

                if (0.0 < item_height <= 1.0) and (not is_horizontal):
                    stretch = int(100 * item_height)

                item_height = int(item_height)
                if item_height < -1:
                    item_height = -item_height
                else:
                    item_height = max(item_height, height)

                control.setWidth(max(item_width, 0))
                control.setHeight(max(item_height, 0))

            # Bind the editor into the UIInfo object name space so it can be
            # referred to by a Handler while the user interface is active:
            Id = item.id or name
            info.bind( Id, editor, item.id )

            # Also, add the editors to the list of editors used to construct
            # the user interface:
            ui._editors.append( editor )

            # If the handler wants to be notified when the editor is created,
            # add it to the list of methods to be called when the UI is
            # complete:
            defined = getattr( handler, Id + '_defined', None )
            if defined is not None:
                ui.add_defined( defined )

            # If the editor is conditionally visible, add the visibility
            # 'expression' and the editor to the UI object's list of monitored
            # objects:
            if item.visible_when != '':
                ui.add_visible( item.visible_when, editor )

            # If the editor is conditionally enabled, add the enabling
            # 'expression' and the editor to the UI object's list of monitored
            # objects:
            if item.enabled_when != '':
                ui.add_enabled( item.enabled_when, editor )

            # Add the created editor control to the layout with the appropriate
            # size and stretch policies:
            ui._scrollable |= scrollable
#            item_resizable  = ((item.resizable is True) or
#                               ((item.resizable is Undefined) and scrollable))
#            if item_resizable:
#                stretch = stretch or 50
#                self.resizable = True
#            elif item.springy:
#                stretch = stretch or 50
#            policy = control.sizePolicy()
#            if self.horizontal:
#                policy.setHorizontalStretch(stretch)
#                if item_resizable or item.springy:
#                    policy.setHorizontalPolicy(QtGui.QSizePolicy.Expanding)
#            else:
#                policy.setVerticalStretch(stretch)
#                if item_resizable or item.springy:
#                    policy.setVerticalPolicy(QtGui.QSizePolicy.Expanding)
#            control.setSizePolicy(policy)

            # FIXME: Need to decide what to do about border_size and padding
            self._add_widget(inner, control, row, col, show_labels)

            # Save the reference to the label control (if any) in the editor:
            editor.label_control = label

        return outer