Exemple #1
0
    def createEditor(self, parent, option, index):
        """ Reimplemented to return the editor for a given index."""

        model = index.model()
        index = model.mapToSource(index)
        table_editor = model._editor
        column = table_editor.columns[index.column()]
        obj = table_editor.items()[index.row()]

        factory = column.get_editor(obj)
        style = column.get_style(obj)
        if factory is None:
            return None

        target, name = column.target_name(obj)
        handler = default_handler()
        if table_editor.ui.context is None:
            ui = UI(handler=handler)
        else:
            context = table_editor.ui.context.copy()
            context['table_editor_object'] = context['object']
            context['object'] = target
            ui = UI(handler=handler, context=context)

        # Create and initialize the editor
        factory_method = getattr(factory, style + '_editor')
        editor = factory_method(ui, target, name, '', parent)
        editor.prepare(parent)
        control = editor.control
        control.setParent(parent)

        # Required for QMouseEvents to propagate to the widget
        control.setFocusPolicy(QtCore.Qt.StrongFocus)

        # The table view's background will shine through unless the editor
        # paints its own background
        control.setAutoFillBackground(True)

        # Make sure that editors are disposed of correctly
        # will be disposed in closeEditor of the TableView
        control._editor = editor
        return control
Exemple #2
0
    def test_perform_pyface_action(self):
        object = SampleObject()
        handler = SampleHandler()
        action = PyfaceAction()
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(action.performed)
Exemple #3
0
    def test_help_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = HelpAction
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.help_performed)
Exemple #4
0
    def test_close_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = CloseAction
        event = ActionEvent()
        ui = UI(handler=handler, context={'object': object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.close_performed)
Exemple #5
0
    def test_demo_init_no_children_to_be_set(self):
        # Test if there are no children, nothing is selected.
        model = DemoVirtualDirectory(resources=[])
        demo = Demo(model=model)
        demo.selected_node = None

        # when
        info = UIInfo(ui=UI(handler=Handler()))
        demo.init(info)

        # then
        self.assertIsNone(demo.selected_node)
Exemple #6
0
    def test_perform_object_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction(name="action", action="object_action_handler")
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(object.object_action_performed)
        self.assertFalse(action.performed)
Exemple #7
0
    def test_demo_init_set_children(self):
        # Test if there are children, the first one will be selected.
        resources = [DemoPath(), DemoPath()]
        model = DemoVirtualDirectory(resources=resources)
        demo = Demo(model=model)
        demo.selected_node = None

        # when
        info = UIInfo(ui=UI(handler=Handler()))
        demo.init(info)

        # then
        self.assertIs(demo.selected_node, resources[0])
Exemple #8
0
    def test_perform_traitsui_action(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction()
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(action.performed)
        self.assertFalse(handler.action_performed)
        self.assertFalse(handler.info_action_performed)
        self.assertFalse(handler.click_performed)
        self.assertFalse(object.action_performed)
        self.assertFalse(object.info_action_performed)
        self.assertFalse(object.click_performed)
Exemple #9
0
    def Create(self, parent, id, evtHandler):
        """ Called to create the control, which must derive from wxControl. """
        from traitsui.api import UI, default_handler

        # If the editor has already been created, ignore the request:
        if hasattr(self, '_control'):
            return

        handler = self._handler
        if handler is None:
            handler = default_handler()

        if self._context is None:
            ui = UI(handler=handler)
        else:
            context = self._context.copy()
            context['table_editor_object'] = context['object']
            context['object'] = self._obj
            ui = UI(handler=handler, context=context)

        # Link the editor's undo history in to the main ui undo history if the
        # UI object is available:
        factory = self._factory
        if factory._ui is not None:
            ui.history = factory._ui.history

        # make sure the factory knows this is a grid_cell editor
        factory.is_grid_cell = True
        factory_method = getattr(factory, self._style + '_editor')
        self._editor = factory_method(ui, self._obj, self._name,
                                      self._description, parent)

        # Tell the editor to actually build the editing widget:
        self._editor.prepare(parent)

        # Find the control to use as the editor:
        self._control = control = self._editor.control

        # Calculate and save the required editor height:
        grid, row, col = getattr(self, '_grid_info', (None, None, None))
        width, height = control.GetBestSize()

        self_height = self._height
        if self_height > 1.0:
            height = int(self_height)
        elif (self_height >= 0.0) and (grid is not None):
            height = int(self_height * grid.GetSize()[1])

        self_width = self._width
        if self_width > 1.0:
            width = int(self_width)
        elif (self_width >= 0.0) and (grid is not None):
            width = int(self_width * grid.GetSize()[0])

        self._edit_width, self._edit_height = width, height

        # Set up the event handler for each window in the cell editor:
        push_control(control, grid)

        # Set up the first control found within the cell editor as the cell
        # editor control:
        control = get_control(control)
        if control is not None:
            self.SetControl(control)
    def Create(self, parent, id, evtHandler):
        """ Called to create the control, which must derive from wxControl. """
        # If the editor has already been created, ignore the request:
        if hasattr( self, '_control' ):
            return

        handler = self._handler
        if handler is None:
            handler = default_handler()

        if self._context is None:
            ui = UI(handler = handler)
        else:
            context = self._context.copy()
            context['table_editor_object'] = context['object']
            context['object'] = self._obj
            ui = UI(handler = handler, context = context)

        # Link the editor's undo history in to the main ui undo history if the
        # UI object is available:
        factory = self._factory
        if factory._ui is not None:
            ui.history = factory._ui.history

        # make sure the factory knows this is a grid_cell editor
        factory.is_grid_cell = True
        factory_method = getattr(factory, self._style + '_editor')
        self._editor   = factory_method(ui,
                                        self._obj,
                                        self._name,
                                        self._description,
                                        parent)

        # Tell the editor to actually build the editing widget:
        self._editor.prepare(parent)

        # Find the control to use as the editor:
        self._control = control = self._editor.control

        # Calculate and save the required editor height:
        grid, row, col = getattr(self, '_grid_info', (None, None, None))
        width, height  = control.GetBestSize()

        self_height    = self._height
        if self_height > 1.0:
            height = int( self_height )
        elif (self_height >= 0.0) and (grid is not None):
            height = int( self_height * grid.GetSize()[1] )

        self_width = self._width
        if self_width > 1.0:
            width = int( self_width )
        elif (self_width >= 0.0) and (grid is not None):
            width = int( self_width * grid.GetSize()[0] )

        self._edit_width, self._edit_height = width, height

        # Set up the event handler for each window in the cell editor:
        push_control(control, grid)

        # Set up the first control found within the cell editor as the cell
        # editor control:
        control = get_control(control)
        if control is not None:
            self.SetControl(control)
 def edit_traits(self, *args, **kws):
     main(embedded=True)
     return UI(handler=Handler())