Changeset 3241

Show
Ignore:
Timestamp:
07/12/07 12:35:31 (1 year ago)
Author:
ed
Message:

Lots of improvements to the DrawnObject? class:

- Expanded the Height and Width properties to apply to all shapes, even irregular ones such as polygons and rotated text.

- Added convenience properties Position and Size, which are simply shorthad for (Xpos, Ypos) and (Width, Height), respectively.

- Added the Rect property, which is read-only, and returns an instance of a wx.Rect whose dimensions contain the associated drawn object. If this seems to be a very useful thing, we could wrap it into a true Dabo object.

- Added the 'drawPolyLines()' method, which is similar to drawPolygon(), but creates an open-ended series of line segments.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/ui/uiwx/dPemMixin.py

    r3199 r3241  
    33import time 
    44import types 
     5import math 
    56import wx 
    67from wx._core import PyAssertionError 
     
    12931294                PenWidth=penWidth, LineStyle=lineStyle, HatchStyle=hatchStyle, 
    12941295                Shape="polygon", Points=points, DrawMode=mode) 
     1296        # Add it to the list of drawing objects 
     1297        obj = self._addToDrawnObjects(obj, persist) 
     1298        return obj 
     1299 
     1300 
     1301    def drawPolyLines(self, points, penColor="black", penWidth=1,  
     1302            lineStyle=None, mode=None, persist=True): 
     1303        """Draws a series of connected line segments defined by the specified points. 
     1304 
     1305        The 'points' parameter should be a tuple of (x,y) pairs defining the shape. Lines 
     1306        are drawn connecting the points sequentially, but a segment from the last 
     1307        point to the first is not drawn, leaving an 'open' polygon. As a result, there is no  
     1308        FillColor or HatchStyle defined for this. 
     1309 
     1310        See the 'drawCircle()' method above for more details. 
     1311        """ 
     1312        obj = DrawObject(self, PenColor=penColor, PenWidth=penWidth, LineStyle=lineStyle,  
     1313                Shape="polylines", Points=points, DrawMode=mode) 
    12951314        # Add it to the list of drawing objects 
    12961315        obj = self._addToDrawnObjects(obj, persist) 
     
    26952714        if self.Shape == "circle": 
    26962715            dc.DrawCircle(x, y, self.Radius) 
     2716            self._width = self._height = self.Radius * 2 
    26972717        elif self.Shape == "rect": 
    26982718            w, h = self.Width, self.Height 
     
    27032723                h = self.Parent.Height 
    27042724            dc.DrawRectangle(x, y, w, h) 
    2705         elif self.Shape == "polygon": 
    2706             dc.DrawPolygon(self.Points) 
     2725        elif self.Shape in ("polygon", "polylines"): 
     2726            if self.Shape == "polygon": 
     2727                dc.DrawPolygon(self.Points) 
     2728            else: 
     2729                dc.DrawLines(self.Points) 
     2730            xs = [pt[0] for pt in self.Points] 
     2731            ys = [pt[1] for pt in self.Points] 
     2732            self._xPos = min(xs) 
     2733            self._yPos = min(ys) 
     2734            self._width = max(xs) - self._xPos 
     2735            self._height = max(ys) - self._yPos 
    27072736        elif self.Shape == "line": 
    27082737            x1, y1 = self.Points[0] 
    27092738            x2, y2 = self.Points[1] 
    27102739            dc.DrawLine(x1, y1, x2, y2) 
     2740            self._xPos = min(x1, x2) 
     2741            self._yPos = min(y1, y2) 
     2742            self._width = abs(x1 - x2) 
     2743            self._height = abs(y1 - y2) 
    27112744        elif self.Shape == "gradient": 
    27122745            self._drawGradient(dc, x, y) 
     
    27482781            else: 
    27492782                dc.DrawRotatedText(txt, x, y, self._angle) 
     2783            w, h = dabo.ui.fontMetricFromDrawObject(self) 
     2784            angle = self._angle % 360 
     2785            if angle % 90 == 0: 
     2786                if angle % 180 == 0: 
     2787                    self._width, self._height = w, h 
     2788                else: 
     2789                    # 90 degree variant; switch the values. 
     2790                    self._width, self._height = h, w 
     2791            else: 
     2792                rad = math.radians(angle) 
     2793                self._width = abs(math.cos(rad) * w) + abs(math.sin(rad) * h) 
     2794                self._height = abs(math.sin(rad) * w) + abs(math.cos(rad) * h) 
    27502795 
    27512796 
     
    30253070             
    30263071 
     3072    def _getPosition(self): 
     3073        return (self._xPos, self._yPos) 
     3074 
     3075    def _setPosition(self, val): 
     3076        if (self._xPos, self._yPos) != val: 
     3077            self._xPos, self._yPos = val 
     3078            self.update() 
     3079 
     3080 
    30273081    def _getRadius(self): 
    30283082        return self._radius 
     
    30323086            self._radius = val 
    30333087            self.update() 
    3034              
    3035          
     3088 
     3089 
     3090    def _getRect(self): 
     3091        x, y, w, h = self._xPos, self._yPos, self._width, self._height 
     3092        if self._shape == "circle": 
     3093            # x and y are the center, so correct for that. 
     3094            x = x - self._radius 
     3095            y = y - self._radius 
     3096        elif (self._shape == "text") and (self._angle % 360 != 0): 
     3097            tw, th = dabo.ui.fontMetricFromDrawObject(self) 
     3098            angle = self._angle % 360 
     3099            if 0 < angle <= 90: 
     3100                rad = math.radians(angle) 
     3101                cos = math.cos(rad) 
     3102                y -= (h - cos*th) 
     3103            elif 90 < angle <= 180: 
     3104                rad = math.radians(90 - angle) 
     3105                cos = math.cos(rad) 
     3106                y -= h 
     3107                x -= (w - cos*th) 
     3108            elif 180 < angle <= 270: 
     3109                rad = math.radians(180 - angle) 
     3110                cos = math.cos(rad) 
     3111                y -= cos*th 
     3112                x -= w 
     3113            else: 
     3114                rad = math.radians(270 - angle) 
     3115                cos = math.cos(rad) 
     3116                x -= cos*th 
     3117        return wx.Rect(x, y, w, h) 
     3118 
     3119 
     3120    def _getSize(self): 
     3121        return (self._width, self._height) 
     3122 
     3123    def _setSize(self, val): 
     3124        if (self._width, self._height) != val: 
     3125            self._width, self._height = val 
     3126            self.update() 
     3127 
     3128 
    30363129    def _getShape(self): 
    30373130        return self._shape 
     
    31813274            _("Tuple of (x,y) pairs defining a polygon.  (tuple)")) 
    31823275 
     3276    Position = property(_getPosition, _setPosition, None, 
     3277            _("Shorthand for (Xpos, Ypos).  (2-tuple)")) 
     3278     
    31833279    Radius = property(_getRadius, _setRadius, None, 
    31843280            _("For circles, the radius of the shape  (int)")) 
    31853281 
     3282    Rect = property(_getRect, None, None, 
     3283            _("Reference to a wx.Rect that encompasses the drawn object (read-only) (wx.Rect)")) 
     3284     
     3285    Size = property(_getSize, _setSize, None, 
     3286            _("Convenience property, equivalent to (Width, Height)  (2-tuple)")) 
     3287     
    31863288    Shape = property(_getShape, _setShape, None, 
    31873289            _("Type of shape to draw  (str)"))