dabo Commit
Revision 3241
Date: 2007-07-12 12:35:31 -0700 (Thu, 12 Jul 2007)
Author: Ed
Trac: http://svn.dabodev.com/trac/dabo/changeset/3241
Changed:
U trunk/dabo/ui/uiwx/dPemMixin.py
Log:
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.
Diff:
Modified: trunk/dabo/ui/uiwx/dPemMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dPemMixin.py 2007-07-12 05:13:33 UTC (rev 3240)
+++ trunk/dabo/ui/uiwx/dPemMixin.py 2007-07-12 19:35:31 UTC (rev 3241)
@@ -2,6 +2,7 @@
import sys
import time
import types
+import math
import wx
from wx._core import PyAssertionError
import dabo
@@ -1297,6 +1298,24 @@
return obj
+ def drawPolyLines(self, points, penColor="black", penWidth=1,
+ lineStyle=None, mode=None, persist=True):
+ """Draws a series of connected line segments defined by the
specified points.
+
+ The 'points' parameter should be a tuple of (x,y) pairs
defining the shape. Lines
+ are drawn connecting the points sequentially, but a segment
from the last
+ point to the first is not drawn, leaving an 'open' polygon. As
a result, there is no
+ FillColor or HatchStyle defined for this.
+
+ See the 'drawCircle()' method above for more details.
+ """
+ obj = DrawObject(self, PenColor=penColor, PenWidth=penWidth,
LineStyle=lineStyle,
+ Shape="polylines", Points=points, DrawMode=mode)
+ # Add it to the list of drawing objects
+ obj = self._addToDrawnObjects(obj, persist)
+ return obj
+
+
def drawLine(self, x1, y1, x2, y2, penColor="black", penWidth=1,
fillColor=None, lineStyle=None, mode=None,
persist=True):
"""Draws a line between (x1,y1) and (x2, y2).
@@ -2694,6 +2713,7 @@
if self.Shape == "circle":
dc.DrawCircle(x, y, self.Radius)
+ self._width = self._height = self.Radius * 2
elif self.Shape == "rect":
w, h = self.Width, self.Height
# If any of these values is -1, use the parent object's
size
@@ -2702,12 +2722,25 @@
if h < 0:
h = self.Parent.Height
dc.DrawRectangle(x, y, w, h)
- elif self.Shape == "polygon":
- dc.DrawPolygon(self.Points)
+ elif self.Shape in ("polygon", "polylines"):
+ if self.Shape == "polygon":
+ dc.DrawPolygon(self.Points)
+ else:
+ dc.DrawLines(self.Points)
+ xs = [pt[0] for pt in self.Points]
+ ys = [pt[1] for pt in self.Points]
+ self._xPos = min(xs)
+ self._yPos = min(ys)
+ self._width = max(xs) - self._xPos
+ self._height = max(ys) - self._yPos
elif self.Shape == "line":
x1, y1 = self.Points[0]
x2, y2 = self.Points[1]
dc.DrawLine(x1, y1, x2, y2)
+ self._xPos = min(x1, x2)
+ self._yPos = min(y1, y2)
+ self._width = abs(x1 - x2)
+ self._height = abs(y1 - y2)
elif self.Shape == "gradient":
self._drawGradient(dc, x, y)
elif self.Shape == "text":
@@ -2747,6 +2780,18 @@
dc.DrawText(txt, x, y)
else:
dc.DrawRotatedText(txt, x, y, self._angle)
+ w, h = dabo.ui.fontMetricFromDrawObject(self)
+ angle = self._angle % 360
+ if angle % 90 == 0:
+ if angle % 180 == 0:
+ self._width, self._height = w, h
+ else:
+ # 90 degree variant; switch the values.
+ self._width, self._height = h, w
+ else:
+ rad = math.radians(angle)
+ self._width = abs(math.cos(rad) * w) +
abs(math.sin(rad) * h)
+ self._height = abs(math.sin(rad) * w) +
abs(math.cos(rad) * h)
def _drawGradient(self, dc, xpos, ypos):
@@ -3024,6 +3069,15 @@
self.update()
+ def _getPosition(self):
+ return (self._xPos, self._yPos)
+
+ def _setPosition(self, val):
+ if (self._xPos, self._yPos) != val:
+ self._xPos, self._yPos = val
+ self.update()
+
+
def _getRadius(self):
return self._radius
@@ -3031,8 +3085,47 @@
if self._radius != val:
self._radius = val
self.update()
-
-
+
+
+ def _getRect(self):
+ x, y, w, h = self._xPos, self._yPos, self._width, self._height
+ if self._shape == "circle":
+ # x and y are the center, so correct for that.
+ x = x - self._radius
+ y = y - self._radius
+ elif (self._shape == "text") and (self._angle % 360 != 0):
+ tw, th = dabo.ui.fontMetricFromDrawObject(self)
+ angle = self._angle % 360
+ if 0 < angle <= 90:
+ rad = math.radians(angle)
+ cos = math.cos(rad)
+ y -= (h - cos*th)
+ elif 90 < angle <= 180:
+ rad = math.radians(90 - angle)
+ cos = math.cos(rad)
+ y -= h
+ x -= (w - cos*th)
+ elif 180 < angle <= 270:
+ rad = math.radians(180 - angle)
+ cos = math.cos(rad)
+ y -= cos*th
+ x -= w
+ else:
+ rad = math.radians(270 - angle)
+ cos = math.cos(rad)
+ x -= cos*th
+ return wx.Rect(x, y, w, h)
+
+
+ def _getSize(self):
+ return (self._width, self._height)
+
+ def _setSize(self, val):
+ if (self._width, self._height) != val:
+ self._width, self._height = val
+ self.update()
+
+
def _getShape(self):
return self._shape
@@ -3180,9 +3273,18 @@
Points = property(_getPoints, _setPoints, None,
_("Tuple of (x,y) pairs defining a polygon. (tuple)"))
+ Position = property(_getPosition, _setPosition, None,
+ _("Shorthand for (Xpos, Ypos). (2-tuple)"))
+
Radius = property(_getRadius, _setRadius, None,
_("For circles, the radius of the shape (int)"))
+ Rect = property(_getRect, None, None,
+ _("Reference to a wx.Rect that encompasses the drawn
object (read-only) (wx.Rect)"))
+
+ Size = property(_getSize, _setSize, None,
+ _("Convenience property, equivalent to (Width, Height)
(2-tuple)"))
+
Shape = property(_getShape, _setShape, None,
_("Type of shape to draw (str)"))
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: http://leafe.com/archives/byMID/dabo-dev/[EMAIL PROTECTED]