⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstracteditor.py

📁 Wxpython Implemented on Windows CE, Source code
💻 PY
📖 第 1 页 / 共 3 页
字号:
                    self._canvas.Refresh()
                    
                break


    def GetShape(self, model):
        for shape in self._diagram.GetShapeList():
            if hasattr(shape, "GetModel") and shape.GetModel() == model:
                return shape
        return None


    def GetShapeCount(self):
        return self._diagram.GetCount()


    def GetSelection(self):
        return filter(lambda shape: shape.Selected(), self._diagram.GetShapeList())


    def SetSelection(self, models, extendSelect = False):
        dc = wx.ClientDC(self._canvas)
        self._canvas.PrepareDC(dc)
        update = False
        if not isinstance(models, type([])) and not isinstance(models, type(())):
            models = [models]
        for shape in self._diagram.GetShapeList():
            if hasattr(shape, "GetModel"):
                if shape.Selected() and not shape.GetModel() in models:  # was selected, but not in new list, so deselect, unless extend select
                    if not extendSelect:
                        shape.Select(False, dc)
                        update = True
                elif not shape.Selected() and shape.GetModel() in models: # was not selected and in new list, so select
                    shape.Select(True, dc)
                    update = True
                elif extendSelect and shape.Selected() and shape.GetModel() in models: # was selected, but extend select means to deselect
                    shape.Select(False, dc)
                    update = True
        if update:
            self._canvas.Redraw(dc)


    def BringToFront(self, shape):
        if shape.GetParent() and isinstance(shape.GetParent(), ogl.CompositeShape):
            self._diagram.RemoveShape(shape.GetParent())
            self._diagram.AddShape(shape.GetParent())
        else:
            self._diagram.RemoveShape(shape)
            self._diagram.AddShape(shape)


    def SendToBack(self, shape):
        if shape.GetParent() and isinstance(shape.GetParent(), ogl.CompositeShape):
            self._diagram.RemoveShape(shape.GetParent())
            self._diagram.InsertShape(shape.GetParent())
        else:
            self._diagram.RemoveShape(shape)
            self._diagram.InsertShape(shape)


    def ScrollVisible(self, shape):
        if not shape:
            return
            
        xUnit, yUnit = self._canvas.GetScrollPixelsPerUnit()
        scrollX, scrollY = self._canvas.GetViewStart()  # in scroll units
        scrollW, scrollH = self._canvas.GetSize()  # in pixels
        w, h = shape.GetBoundingBoxMax() # in pixels
        x = shape.GetX() - w/2  # convert to upper left coordinate from center
        y = shape.GetY() - h/2  # convert to upper left coordinate from center

        if x >= scrollX*xUnit and x <= scrollX*xUnit + scrollW:  # don't scroll if already visible
            x = -1
        else:
            x = x/xUnit

        if y >= scrollY*yUnit and y <= scrollY*yUnit + scrollH:  # don't scroll if already visible
            y = -1
        else:
            y = y/yUnit

        self._canvas.Scroll(x, y)  # in scroll units


    def SetPropertyShape(self, shape):
        # no need to highlight if no PropertyService is running
        propertyService = wx.GetApp().GetService(PropertyService.PropertyService)
        if not propertyService:
            return

        if shape == self._propShape:
            return

        if hasattr(shape, "GetPropertyShape"):
            shape = shape.GetPropertyShape()

        dc = wx.ClientDC(self._canvas)
        self._canvas.PrepareDC(dc)
        dc.BeginDrawing()

        # erase old selection if it still exists
        if self._propShape and self._propShape in self._diagram.GetShapeList():
            if hasattr(self._propShape, "DEFAULT_BRUSH"):
                self._propShape.SetBrush(self._propShape.DEFAULT_BRUSH)
            else:
                self._propShape.SetBrush(self._brush)
            if (self._propShape._textColourName in ["BLACK", "WHITE"]):  # Would use GetTextColour() but it is broken
                self._propShape.SetTextColour("BLACK", 0)
            self._propShape.Draw(dc)

        # set new selection
        self._propShape = shape

        # draw new selection
        if self._propShape and self._propShape in self._diagram.GetShapeList():
            if self.HasFocus():
                self._propShape.SetBrush(SELECT_BRUSH)
            else:
                self._propShape.SetBrush(INACTIVE_SELECT_BRUSH)
            if (self._propShape._textColourName in ["BLACK", "WHITE"]):  # Would use GetTextColour() but it is broken
                self._propShape.SetTextColour("WHITE", 0)
            self._propShape.Draw(dc)

        dc.EndDrawing()


    def FocusColorPropertyShape(self, gotFocus=False):
        # no need to change highlight if no PropertyService is running
        propertyService = wx.GetApp().GetService(PropertyService.PropertyService)
        if not propertyService:
            return

        if not self._propShape:
            return

        dc = wx.ClientDC(self._canvas)
        self._canvas.PrepareDC(dc)
        dc.BeginDrawing()

        # draw deactivated selection
        if self._propShape and self._propShape in self._diagram.GetShapeList():
            if gotFocus:
                self._propShape.SetBrush(SELECT_BRUSH)
            else:
                self._propShape.SetBrush(INACTIVE_SELECT_BRUSH)
            if (self._propShape._textColourName in ["BLACK", "WHITE"]):  # Would use GetTextColour() but it is broken
                self._propShape.SetTextColour("WHITE", 0)
            self._propShape.Draw(dc)

            if hasattr(self._propShape, FORCE_REDRAW_METHOD):
                self._propShape.ForceRedraw()

        dc.EndDrawing()


    #----------------------------------------------------------------------------
    # Property Service methods
    #----------------------------------------------------------------------------

    def GetPropertyModel(self):
        if hasattr(self, "_propModel"):
            return self._propModel
        return None
        

    def SetPropertyModel(self, model):
        # no need to set the model if no PropertyService is running
        propertyService = wx.GetApp().GetService(PropertyService.PropertyService)
        if not propertyService:
            return
            
        if hasattr(self, "_propModel") and model == self._propModel:
            return
            
        self._propModel = model
        propertyService.LoadProperties(self._propModel, self.GetDocument())


class EditorCanvasShapeMixin:

    def GetModel(self):
        return self._model


    def SetModel(self, model):
        self._model = model


class EditorCanvasShapeEvtHandler(ogl.ShapeEvtHandler):

    """ wxBug: Bug in OLG package.  With wxShape.SetShadowMode() turned on, when we set the size,
        the width/height is larger by 6 pixels.  Need to subtract this value from width and height when we
        resize the object.
    """
    SHIFT_KEY = 1
    CONTROL_KEY = 2

    def __init__(self, view):
        ogl.ShapeEvtHandler.__init__(self)
        self._view = view


    def OnLeftClick(self, x, y, keys = 0, attachment = 0):
        shape = self.GetShape()
        if hasattr(shape, "GetModel"):  # Workaround, on drag, we should deselect all other objects and select the clicked on object
            model = shape.GetModel()
        else:
            shape = shape.GetParent()
            if shape:
                model = shape.GetModel()

        if model:
            self._view.SetSelection(model, keys == self.SHIFT_KEY or keys == self.CONTROL_KEY)
            self._view.SetPropertyShape(shape)
            self._view.SetPropertyModel(model)


    def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
        ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys, attachment)
        shape = self.GetShape()
        if hasattr(shape, "GetModel"):  # Workaround, on drag, we should deselect all other objects and select the clicked on object
            model = shape.GetModel()
        else:
            parentShape = shape.GetParent()
            if parentShape:
                model = parentShape.GetModel()
        self._view.SetSelection(model, keys == self.SHIFT_KEY or keys == self.CONTROL_KEY)


    def OnMovePre(self, dc, x, y, oldX, oldY, display):
        """ Prevent objects from being dragged outside of viewable area """
        if (x < 0) or (y < 0) or (x > self._view._maxWidth) or (y > self._view._maxHeight):
            return False

        return ogl.ShapeEvtHandler.OnMovePre(self, dc, x, y, oldX, oldY, display)


    def OnMovePost(self, dc, x, y, oldX, oldY, display):
        """ Update the model's record of where the shape should be.  Also enable redo/undo.  """
        if x == oldX and y == oldY:
            return
        if not self._view.GetDocument():
            return
        shape = self.GetShape()
        if isinstance(shape, EditorCanvasShapeMixin) and shape.Draggable():
            model = shape.GetModel()
            if hasattr(model, "getEditorBounds") and model.getEditorBounds():
                x, y, w, h = model.getEditorBounds()
                newX = shape.GetX() - shape.GetBoundingBoxMax()[0] / 2
                newY = shape.GetY() - shape.GetBoundingBoxMax()[1] / 2
                newWidth = shape.GetBoundingBoxMax()[0]
                newHeight = shape.GetBoundingBoxMax()[1]
                if shape._shadowMode != ogl.SHADOW_NONE:
                    newWidth -= shape._shadowOffsetX
                    newHeight -= shape._shadowOffsetY
                newbounds = (newX, newY, newWidth, newHeight)
    
                if x != newX or y != newY or w != newWidth or h != newHeight:
                    self._view.GetDocument().GetCommandProcessor().Submit(EditorCanvasUpdateShapeBoundariesCommand(self._view.GetDocument(), model, newbounds))


    def Draw(self, dc):
        pass


class EditorCanvasUpdateShapeBoundariesCommand(wx.lib.docview.Command):


    def __init__(self, canvasDocument, model, newbounds):
        wx.lib.docview.Command.__init__(self, canUndo = True)
        self._canvasDocument = canvasDocument
        self._model = model
        self._oldbounds = model.getEditorBounds()
        self._newbounds = newbounds


    def GetName(self):
        name = self._canvasDocument.GetNameForObject(self._model)
        if not name:
            name = ""
            print "ERROR: AbstractEditor.EditorCanvasUpdateShapeBoundariesCommand.GetName: unable to get name for ", self._model
        return _("Move/Resize %s") % name


    def Do(self):
        return self._canvasDocument.UpdateEditorBoundaries(self._model, self._newbounds)


    def Undo(self):
        return self._canvasDocument.UpdateEditorBoundaries(self._model, self._oldbounds)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -