-
try:
- from Numeric import array,asarray,Float,cos,pi,sum,minimum,maximum,Int32,zeros
+ from Numeric import array,asarray,Float,cos, sin, pi,sum,minimum,maximum,Int32,zeros, ones, concatenate, sqrt, argmin, power, absolute, matrixmultiply, transpose, sometrue
except ImportError:
- from numarray import array, asarray, Float, cos, pi, sum, minimum, maximum, Int32, zeros
+ try:
+ from numarray import array, asarray, Float, cos, sin, pi, sum, minimum, maximum, Int32, zeros, concatenate, matrixmultiply, transpose, sometrue
+ except ImportError:
+ raise ImportError("I could not import either Numeric or numarray")
from time import clock, sleep
import types
import os
-import Resources
-
## A global variable to hold the Pixels per inch that wxWindows thinks is in use
## This is used for scaling fonts.
## This can't be computed on module __init__, because a wx.App might not have iniitalized yet.
-global ScreenPPI
+global ScreenPPI
## a custom Exceptions:
-class FloatCanvasException(Exception):
+class FloatCanvasError(Exception):
pass
-## All the mouse events
+## Create all the mouse events
+# I don't see a need for these two, but maybe some day!
#EVT_FC_ENTER_WINDOW = wx.NewEventType()
#EVT_FC_LEAVE_WINDOW = wx.NewEventType()
EVT_FC_LEFT_DOWN = wx.NewEventType()
EVT_FC_ENTER_OBJECT = wx.NewEventType()
EVT_FC_LEAVE_OBJECT = wx.NewEventType()
+##Create all mouse event binding functions
#def EVT_ENTER_WINDOW( window, function ):
# window.Connect( -1, -1, EVT_FC_ENTER_WINDOW, function )
#def EVT_LEAVE_WINDOW( window, function ):
def EVT_MOUSEWHEEL( window, function ):
window.Connect( -1, -1,EVT_FC_MOUSEWHEEL , function )
-class MouseEvent(wx.PyCommandEvent):
+class _MouseEvent(wx.PyCommandEvent):
"""
GetCoords() , which returns and (x,y) tuple in world coordinates.
- Another differnce is that it is a CommandEvent, which propagates up
+ Another difference is that it is a CommandEvent, which propagates up
the window hierarchy until it is handled.
"""
self._NativeEvent = NativeEvent
self.Coords = Coords
- def SetCoords(self,Coords):
- self.Coords = Coords
+# I don't think this is used.
+# def SetCoords(self,Coords):
+# self.Coords = Coords
def GetCoords(self):
return self.Coords
#return eval(self.NativeEvent.__getattr__(name) )
return getattr(self._NativeEvent, name)
-#### ColorGEnerator class is now obsolete. I'm using a python generator function instead.
-##class ColorGenerator:
+def _cycleidxs(indexcount, maxvalue, step):
-## """
-
-## An instance of this class generates a unique color each time
-## GetNextColor() is called. Someday I will use a proper Python
-## generator for this class.
-
-## The point of this generator is for the hit-test bitmap, each object
-## needs to be a unique color. Also, each system can be running a
-## different number of colors, and it doesn't appear to be possible to
-## have a wxMemDC with a different colordepth as the screen so this
-## generates colors far enough apart that they can be distinguished on
-## a 16bit screen. Anything less than 16bits won't work. It could, but
-## I havn't written the code that way. You also wouldn't get many
-## distict colors
-
-## """
+ """
+ Utility function used by _colorGenerator
-## def __init__(self):
-## import sys
-## ## figure out the color depth of the screen
-## ## for some bizare reason, thisdoesn't work on OS-X
-## if sys.platform == 'darwin':
-## depth = 24
-## else:
-## b = wx.EmptyBitmap(1,1)
-## depth = b.GetDepth()
-## self.r = 0
-## self.g = 0
-## self.b = 0
-## if depth == 16:
-## self.step = 8
-## elif depth >= 24:
-## self.step = 1
-## else:
-## raise FloatCanvasException("ColorGenerator does not work with depth = %s"%depth )
-
-## def GetNextColor(self):
-## step = self.step
-## ##r,g,b = self.r,self.g,self.b
-## self.r += step
-## if self.r > 255:
-## self.r = step
-## self.g += step
-## if self.g > 255:
-## self.g = step
-## self.b += step
-## if self.b > 255:
-## ## fixme: this should be a derived exception
-## raise FloatCanvasException("Too many objects in colorgenerator for HitTest")
-## return (self.r,self.g,self.b)
-
-## def Reset(self):
-## self.r = 0
-## self.g = 0
-## self.b = 0
-
-def cycleidxs(indexcount, maxvalue, step):
+ """
if indexcount == 0:
yield ()
else:
for idx in xrange(0, maxvalue, step):
- for tail in cycleidxs(indexcount - 1, maxvalue, step):
+ for tail in _cycleidxs(indexcount - 1, maxvalue, step):
yield (idx, ) + tail
-def colorGenerator():
+def _colorGenerator():
+
+ """
+
+ Generates a seris of unique colors used to do hit-tests with the HIt
+ Test bitmap
+
+ """
import sys
if sys.platform == 'darwin':
depth = 24
step = 1
else:
raise "ColorGenerator does not work with depth = %s" % depth
- return cycleidxs(indexcount=3, maxvalue=256, step=step)
+ return _cycleidxs(indexcount=3, maxvalue=256, step=step)
-#### I don't know if the Set objects are useful, beyond the pointset object
-#### The problem is that when zoomed in, the BB is checked to see whether to draw the object.
-#### A Set object can defeat this
+#### I don't know if the Set objects are useful, beyond the pointset
+#### object The problem is that when zoomed in, the BB is checked to see
+#### whether to draw the object. A Set object can defeat this. ONe day
+#### I plan to write some custon C++ code to draw sets of objects
##class ObjectSetMixin:
## """
## if length == 1:
## self.Pens = self.Pens[0]
-
-
class DrawObject:
"""
This is the base class for all the objects that can be drawn.
+ One must subclass from this (and an assortment of Mixins) to create
+ a new DrawObject.
+
"""
def __init__(self,InForeground = False):
self.HitFill = True
self.MinHitLineWidth = 3
self.HitLineWidth = 3 ## this gets re-set by the subclasses if necessary
+
+ self.Brush = None
+ self.Pen = None
+
+ self.FillStyle = "Solid"
# I pre-define all these as class variables to provide an easier
# interface, and perhaps speed things up by caching all the Pens
self._Canvas.MakeNewHTdc()
if not self.HitColor:
if not self._Canvas.HitColorGenerator:
- self._Canvas.HitColorGenerator = colorGenerator()
+ self._Canvas.HitColorGenerator = _colorGenerator()
self._Canvas.HitColorGenerator.next() # first call to prevent the background color from being used.
self.HitColor = self._Canvas.HitColorGenerator.next()
self.SetHitPen(self.HitColor,self.HitLineWidth)
self._Canvas.MakeHitDict()
self._Canvas.HitDict[Event][self.HitColor] = (self) # put the object in the hit dict, indexed by it's color
-
def UnBindAll(self):
## fixme: this only removes one from each list, there could be more.
if self._Canvas.HitDict:
pass
self.HitAble = False
+
def SetBrush(self,FillColor,FillStyle):
if FillColor is None or FillStyle is None:
self.Brush = wx.TRANSPARENT_BRUSH
if not self.HitLine:
self.HitPen = wx.TRANSPARENT_PEN
else:
- self.HitPen = self.PenList.setdefault( (HitColor, "solid", LineWidth), wx.Pen(HitColor, LineWidth, self.LineStyleList["Solid"]) )
+ self.HitPen = self.PenList.setdefault( (HitColor, "solid", self.HitLineWidth), wx.Pen(HitColor, self.HitLineWidth, self.LineStyleList["Solid"]) )
def PutInBackground(self):
if self._Canvas and self.InForeground:
self._Canvas._BackgroundDirty = True
self.InForeground = True
+class ColorOnlyMixin:
+ """
+
+ Mixin class for objects that have just one color, rather than a fill
+ color and line color
+
+ """
+
+ def SetColor(self, Color):
+ self.SetPen(Color,"Solid",1)
+ self.SetBrush(Color,"Solid")
+
+ SetFillColor = SetColor # Just to provide a consistant interface
+
+class LineOnlyMixin:
+ """
+
+ Mixin class for objects that have just one color, rather than a fill
+ color and line color
+
+ """
+
+ def SetLineColor(self, LineColor):
+ self.LineColor = LineColor
+ self.SetPen(LineColor,self.LineStyle,self.LineWidth)
+
+ def SetLineStyle(self, LineStyle):
+ self.LineStyle = LineStyle
+ self.SetPen(self.LineColor,LineStyle,self.LineWidth)
+
+ def SetLineWidth(self, LineWidth):
+ self.LineWidth = LineWidth
+ self.SetPen(self.LineColor,self.LineStyle,LineWidth)
+
+class LineAndFillMixin(LineOnlyMixin):
+ """
+
+ Mixin class for objects that have both a line and a fill color and
+ style.
+
+ """
+ def SetFillColor(self, FillColor):
+ self.FillColor = FillColor
+ self.SetBrush(FillColor,self.FillStyle)
+
+ def SetFillStyle(self, FillStyle):
+ self.FillStyle = FillStyle
+ self.SetBrush(self.FillColor,FillStyle)
+
class XYObjectMixin:
"""
if self._Canvas:
self._Canvas.BoundingBoxDirty = True
+ def SetXY(self, x, y):
+ self.XY = array( (x, y), Float)
+ self.CalcBoundingBox()
+
+ def CalcBoundingBox(self):
+ ## This may get overwritten in some subclasses
+ self.BoundingBox = array( (self.XY, self.XY), Float )
+
+ def SetPoint(self, xy):
+ self.XY = array( xy, Float)
+ self.XY.shape = (2,)
+ self.CalcBoundingBox()
+
class PointsObjectMixin:
"""
"""
-## This is code for the XYMixin object, it needs to be adapeted and tested.
+
+## This is code for the PointsObjectMixin object, it needs to be adapted and tested.
+## Is the neccesary at all: you can always do:
+## Object.SetPoints( Object.Points + delta, copy = False)
## def Move(self, Delta ):
## """
## Move(Delta): moves the object by delta, where delta is an (dx,
-## dy) pair. Ideally a Numpy array or shape (2,)
+## dy) pair. Ideally a Numpy array of shape (2,)
## """
## if self._Canvas:
## self._Canvas.BoundingBoxDirty = True
- def SetPoints(self,Points):
- self.Points = Points
- self.BoundingBox = array(((min(self.Points[:,0]),min(self.Points[:,1])),(max(self.Points[:,0]),max(self.Points[:,1]))),Float)
+ def CalcBoundingBox(self):
+ self.BoundingBox = array(((min(self.Points[:,0]),
+ min(self.Points[:,1]) ),
+ (max(self.Points[:,0]),
+ max(self.Points[:,1]) ) ), Float )
if self._Canvas:
self._Canvas.BoundingBoxDirty = True
+ def SetPoints(self, Points, copy = True):
+ """
+ Sets the coordinates of the points of the object to Points (NX2 array).
+
+ By default, a copy is made, if copy is set to False, a reference
+ is used, iff Points is a NumPy array of Floats. This allows you
+ to change some or all of the points without making any copies.
+
+ For example:
+
+ Points = Object.Points
+ Points += (5,10) # shifts the points 5 in the x dir, and 10 in the y dir.
+ Object.SetPoints(Points, False) # Sets the points to the same array as it was
+
+ """
+ if copy:
+ self.Points = array(Points, Float)
+ self.Points.shape = (-1,2) # Make sure it is a NX2 array, even if there is only one point
+ else:
+ self.Points = asarray(Points, Float)
+ self.CalcBoundingBox()
-class Polygon(DrawObject,PointsObjectMixin):
+class Polygon(DrawObject,PointsObjectMixin,LineAndFillMixin):
"""
x-coordinate of point N and Points[N,1] is the y-coordinate for
arrays.
+ The other parameters specify various properties of the Polygon, and
+ should be self explanatory.
+
"""
def __init__(self,
Points,
InForeground = False):
DrawObject.__init__(self,InForeground)
self.Points = array(Points,Float) # this DOES need to make a copy
- self.BoundingBox = array(((min(self.Points[:,0]),min(self.Points[:,1])),(max(self.Points[:,0]),max(self.Points[:,1]))),Float)
+ self.CalcBoundingBox()
self.LineColor = LineColor
self.LineStyle = LineStyle
## dc.DrawLineList(Points,self.Pens)
-class Line(DrawObject,PointsObjectMixin):
+class Line(DrawObject,PointsObjectMixin,LineOnlyMixin):
"""
- The Line class takes a list of 2-tuples, or a NX2 NumPy array of point coordinates.
- so that Points[N][0] is the x-coordinate of point N and Points[N][1] is the y-coordinate
- or Points[N,0] is the x-coordinate of point N and Points[N,1] is the y-coordinate for arrays.
- It will draw a straight line if there are two points, and a polyline if there are more than two.
+ The Line class takes a list of 2-tuples, or a NX2 NumPy Float array
+ of point coordinates.
+
+ It will draw a straight line if there are two points, and a polyline
+ if there are more than two.
"""
def __init__(self,Points,
self.Points = array(Points,Float)
- self.BoundingBox = array(((min(self.Points[:,0]),min(self.Points[:,1])),(max(self.Points[:,0]),max(self.Points[:,1]))),Float)
+ self.CalcBoundingBox()
self.LineColor = LineColor
self.LineStyle = LineStyle
HTdc.SetPen(self.HitPen)
HTdc.DrawLines(Points)
+class Arrow(DrawObject,XYObjectMixin,LineOnlyMixin):
+ """
+
+ Arrow(XY, # coords of origin of arrow (x,y)
+ Length, # length of arrow in pixels
+ theta, # angle of arrow in degrees: zero is straight up
+ # angle is to the right
+ LineColor = "Black",
+ LineStyle = "Solid",
+ LineWidth = 1,
+ ArrowHeadSize = 4,
+ ArrowHeadAngle = 45,
+ InForeground = False):
+
+ It will draw an arrow , starting at the point, (X,Y) pointing in
+ direction, theta.
+
+
+ """
+ def __init__(self,
+ XY,
+ Length,
+ Direction,
+ LineColor = "Black",
+ LineStyle = "Solid",
+ LineWidth = 2, # pixels
+ ArrowHeadSize = 8, # pixels
+ ArrowHeadAngle = 30, # degrees
+ InForeground = False):
+
+ DrawObject.__init__(self, InForeground)
+
+ self.XY = array(XY, Float)
+ self.XY.shape = (2,) # Make sure it is a 1X2 array, even if there is only one point
+ self.Length = Length
+ self.Direction = float(Direction)
+ self.ArrowHeadSize = ArrowHeadSize
+ self.ArrowHeadAngle = float(ArrowHeadAngle)
+
+ self.CalcArrowPoints()
+ self.CalcBoundingBox()
+
+ self.LineColor = LineColor
+ self.LineStyle = LineStyle
+ self.LineWidth = LineWidth
+
+ self.SetPen(LineColor,LineStyle,LineWidth)
+
+ ##fixme: How should the HitTest be drawn?
+ self.HitLineWidth = max(LineWidth,self.MinHitLineWidth)
+
+ def SetDirection(self, Direction):
+ self.Direction = float(Direction)
+ self.CalcArrowPoints()
+
+ def SetLength(self, Length):
+ self.Length = Length
+ self.CalcArrowPoints()
+
+ def SetLengthDirection(self, Length, Direction):
+ self.Direction = float(Direction)
+ self.Length = Length
+ self.CalcArrowPoints()
+
+ def SetLength(self, Length):
+ self.Length = Length
+ self.CalcArrowPoints()
+
+ def CalcArrowPoints(self):
+ L = self.Length
+ S = self.ArrowHeadSize
+ phi = self.ArrowHeadAngle * pi / 360
+ theta = (self.Direction-90.0) * pi / 180
+ ArrowPoints = array( ( (0, L, L - S*cos(phi),L, L - S*cos(phi) ),
+ (0, 0, S*sin(phi), 0, -S*sin(phi) ) ),
+ Float )
+ RotationMatrix = array( ( ( cos(theta), -sin(theta) ),
+ ( sin(theta), cos(theta) ) ),
+ Float
+ )
+ ArrowPoints = matrixmultiply(RotationMatrix, ArrowPoints)
+ self.ArrowPoints = transpose(ArrowPoints)
+
+ def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
+ dc.SetPen(self.Pen)
+ xy = WorldToPixel(self.XY)
+ ArrowPoints = xy + self.ArrowPoints
+ dc.DrawLines(ArrowPoints)
+ if HTdc and self.HitAble:
+ HTdc.SetPen(self.HitPen)
+ HTdc.DrawLines(ArrowPoints)
+
##class LineSet(DrawObject, ObjectSetMixin):
## """
## The LineSet class takes a list of 2-tuples, or a NX2 NumPy array of point coordinates.
## Points.shape = (-1,4)
## dc.DrawLineList(Points,self.Pens)
-class PointSet(DrawObject):
+class PointSet(DrawObject,PointsObjectMixin, ColorOnlyMixin):
"""
- The PointSet class takes a list of 2-tuples, or a NX2 NumPy array of point coordinates.
- so that Points[N][0] is the x-coordinate of point N and Points[N][1] is the y-coordinate
- or Points[N,0] is the x-coordinate of point N and Points[N,1] is the y-coordinate for arrays.
- Each point will be drawn the same color and Diameter. The Diameter is in screen points,
- not world coordinates.
+ The PointSet class takes a list of 2-tuples, or a NX2 NumPy array of
+ point coordinates.
+
+ If Points is a sequence of tuples: Points[N][0] is the x-coordinate of
+ point N and Points[N][1] is the y-coordinate.
+
+ If Points is a NumPy array: Points[N,0] is the x-coordinate of point
+ N and Points[N,1] is the y-coordinate for arrays.
+
+ Each point will be drawn the same color and Diameter. The Diameter
+ is in screen pixels, not world coordinates.
- At this point, the hit-test code does not distingish between the
- points, you will only know that one of the poins got hit, not which
- one.
+ The hit-test code does not distingish between the points, you will
+ only know that one of the points got hit, not which one. You can use
+ PointSet.FindClosestPoint(WorldPoint) to find out which one
In the case of points, the HitLineWidth is used as diameter.
self.Points = array(Points,Float)
self.Points.shape = (-1,2) # Make sure it is a NX2 array, even if there is only one point
- self.BoundingBox = array(((min(self.Points[:,0]),
- min(self.Points[:,1])),
- (max(self.Points[:,0]),
- max(self.Points[:,1]))),Float)
-
- self.Color = Color
+ self.CalcBoundingBox()
self.Diameter = Diameter
self.HitLineWidth = self.MinHitLineWidth
- self.SetPen(Color,"Solid",1)
- self.SetBrush(Color,"Solid")
+ self.SetColor(Color)
- def SetPoints(self,Points):
- self.Points = array(Points, Float)
- self.Points.shape = (-1,2) # Make sure it is a NX2 array, even if there is only one point
- self.BoundingBox = array(((min(self.Points[:,0]),
- min(self.Points[:,1]) ),
- (max(self.Points[:,0]),
- max(self.Points[:,1]) ) ) )
- if self._Canvas:
- self._Canvas.BoundingBoxDirty = True
+ def SetDiameter(self,Diameter):
+ self.Diameter = Diameter
+
+
+ def FindClosestPoint(self, XY):
+ """
+
+ Returns the index of the closest point to the point, XY, given
+ in World coordinates. It's essentially random which you get if
+ there are more than one that are the same.
+
+ This can be used to figure out which point got hit in a mouse
+ binding callback, for instance. It's a lot faster that using a
+ lot of separate points.
+
+ """
+ ## kind of ugly to minimize data copying
+ d = self.Points - XY
+ d = sum( power(d,2,d), 1 )
+ d = absolute( d, d ) # don't need the real distance, just which is smallest
+ #dist = sqrt( sum( (self.Points - XY)**2), 1) )
+ return argmin(d)
def DrawD2(self, dc, Points):
# A Little optimization for a diameter2 - point
else:
dc.SetBrush(self.Brush)
radius = int(round(self.Diameter/2))
- for xy in Points:
- dc.DrawEllipsePointSize( (xy - radius), (self.Diameter, self.Diameter) )
+ if len(Points) > 100:
+ xy = Points
+ xywh = concatenate((xy-radius, ones(xy.shape) * self.Diameter ), 1 )
+ dc.DrawEllipseList(xywh)
+ else:
+ for xy in Points:
+ dc.DrawCircle(xy[0],xy[1], radius)
if HTdc and self.HitAble:
HTdc.SetPen(self.HitPen)
+ HTdc.SetBrush(self.HitBrush)
if self.Diameter <= 1:
HTdc.DrawPointList(Points)
elif self.Diameter <= 2:
self.DrawD2(HTdc, Points)
else:
- HTdc.SetBrush(self.HitBrush)
- radius = int(round(self.Diameter/2))
- for xy in Points:
- HTdc.DrawEllipsePointSize( (xy - radius), (self.Diameter, self.Diameter) )
-
-#### Does anyone need this?
-##class Dot(DrawObject):
-## """
-## The Dot class takes an x.y coordinate pair, and the Diameter of the circle.
-## The Diameter is in pixels, so it won't change with zoom.
+ if len(Points) > 100:
+ xy = Points
+ xywh = concatenate((xy-radius, ones(xy.shape) * self.Diameter ), 1 )
+ HTdc.DrawEllipseList(xywh)
+ else:
+ for xy in Points:
+ HTdc.DrawCircle(xy[0],xy[1], radius)
-## Also Fill and line data
+class Point(DrawObject,XYObjectMixin,ColorOnlyMixin):
+ """
+
+ The Point class takes a 2-tuple, or a (2,) NumPy array of point
+ coordinates.
-## """
-## def __init__(self,x,y,Diameter,LineColor,LineStyle,LineWidth,FillColor,FillStyle,InForeground = False):
-## DrawObject.__init__(self,InForeground)
+ The Diameter is in screen points, not world coordinates, So the
+ Bounding box is just the point, and doesn't include the Diameter.
+
+ The HitLineWidth is used as diameter for the
+ Hit Test.
+
+ """
+ def __init__(self, XY, Color = "Black", Diameter = 1, InForeground = False):
+ DrawObject.__init__(self, InForeground)
-## self.X = x
-## self.Y = y
-## self.Diameter = Diameter
-## # NOTE: the bounding box does not include the diameter of the dot, as that is in pixel coords.
-## # If this is a problem, perhaps you should use a circle, instead!
-## self.BoundingBox = array(((x,y),(x,y)),Float)
+ self.XY = array(XY, Float)
+ self.XY.shape = (2,) # Make sure it is a 1X2 array, even if there is only one point
+ self.CalcBoundingBox()
+ self.SetColor(Color)
+ self.Diameter = Diameter
-## self.LineColor = LineColor
-## self.LineStyle = LineStyle
-## self.LineWidth = LineWidth
-## self.FillColor = FillColor
-## self.FillStyle = FillStyle
+ self.HitLineWidth = self.MinHitLineWidth
-## self.SetPen(LineColor,LineStyle,LineWidth)
-## self.SetBrush(FillColor,FillStyle)
+ def SetDiameter(self,Diameter):
+ self.Diameter = Diameter
-## def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
-## #def _Draw(self,dc,WorldToPixel,ScaleWorldToPixel):
-## dc.SetPen(self.Pen)
-## dc.SetBrush(self.Brush)
-## radius = int(round(self.Diameter/2))
-## (X,Y) = WorldToPixel((self.X,self.Y))
-## dc.DrawEllipse((X - radius), (Y - radius), self.Diameter, self.Diameter)
-class RectEllipse(DrawObject, XYObjectMixin):
+ def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
+ dc.SetPen(self.Pen)
+ xy = WorldToPixel(self.XY)
+ if self.Diameter <= 1:
+ dc.DrawPoint(xy[0], xy[1])
+ else:
+ dc.SetBrush(self.Brush)
+ radius = int(round(self.Diameter/2))
+ dc.DrawCircle(xy[0],xy[1], radius)
+ if HTdc and self.HitAble:
+ HTdc.SetPen(self.HitPen)
+ if self.Diameter <= 1:
+ HTdc.DrawPoint(xy[0], xy[1])
+ else:
+ HTdc.SetBrush(self.HitBrush)
+ HTdc.DrawCircle(xy[0],xy[1], radius)
+
+class RectEllipse(DrawObject, XYObjectMixin,LineAndFillMixin):
def __init__(self,x,y,width,height,
LineColor = "Black",
LineStyle = "Solid",
self.SetPen(LineColor,LineStyle,LineWidth)
self.SetBrush(FillColor,FillStyle)
+ def SetShape(self,x,y,width,height):
+ self.XY = array( (x, y), Float)
+ self.WH = array( (width, height), Float )
+ self.CalcBoundingBox()
+
def SetUpDraw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc):
dc.SetPen(self.Pen)
return ( WorldToPixel(self.XY),
ScaleWorldToPixel(self.WH) )
- def SetXY(self, x, y):
- self.XY = array( (x, y), Float)
+ def CalcBoundingBox(self):
self.BoundingBox = array((self.XY, (self.XY + self.WH) ), Float)
- if self._Canvas:
- self._Canvas.BoundingBoxDirty = True
+ self._Canvas.BoundingBoxDirty = True
class Rectangle(RectEllipse):
-# def __init__(*args, **kwargs):
-# RectEllipse.__init__(*args, **kwargs)
-# raise "an error"
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
( XY, WH ) = self.SetUpDraw(dc,
class Circle(Ellipse):
def __init__(self, x ,y, Diameter, **kwargs):
+ self.Center = array((x,y),Float)
RectEllipse.__init__(self ,
x-Diameter/2.,
y-Diameter/2.,
Diameter,
Diameter,
**kwargs)
+
+ def SetDiameter(self, Diameter):
+ x,y = self.Center - (Diameter/2.)
+ self.SetShape(x,
+ y,
+ Diameter,
+ Diameter)
-class TextObjectMixin:
+class TextObjectMixin(XYObjectMixin):
"""
A mix in class that holds attributes and methods that are needed by
FaceName) )
return self.Font
+ def SetColor(self, Color):
+ self.Color = Color
+
+ def SetBackgroundColor(self, BackgroundColor):
+ self.BackgroundColor = BackgroundColor
+
## store the function that shift the coords for drawing text. The
## "c" parameter is the correction for world coordinates, rather
## than pixel coords as the y axis is reversed
self.XY = ( x,y )
- # use a memDC -- ScreenDC doesn't work with 2.5.1 and GTK2
- #dc = wx.MemoryDC()
- #bitmap = wx.EmptyBitmap(1, 1)
- #dc.SelectObject(bitmap)
- #dc.SetFont(self.Font)
- #(self.TextWidth, self.TextHeight) = dc.GetTextExtent(self.String)
(self.TextWidth, self.TextHeight) = (None, None)
self.ShiftFun = self.ShiftFunDict[Position]
- def SetXY(self, x, y):
- self.XY = ( x,y )
- self.BoundingBox = array((self.XY, self.XY),Float)
- if self._Canvas:
- self._Canvas.BoundingBoxDirty = True
-
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
XY = WorldToPixel(self.XY)
dc.SetFont(self.Font)
HTdc.SetBrush(self.HitBrush)
HTdc.DrawRectanglePointSize(XY, (self.TextWidth, self.TextHeight) )
-class ScaledText(DrawObject, TextObjectMixin, XYObjectMixin):
+class ScaledText(DrawObject, TextObjectMixin):
"""
This class creates a text object that is scaled when zoomed. It is
placed at the coordinates, x,y. the "Position" argument is a two
self.Weight = Font.GetWeight()
# Experimental max font size value on wxGTK2: this works OK on
- # my system If it's any larger, there is a crash, with the
- # message: The application 'FloatCanvasDemo.py' lost its
+ # my system. If it's a lot larger, there is a crash, with the
+ # message:
+ #
+ # The application 'FloatCanvasDemo.py' lost its
# connection to the display :0.0; most likely the X server was
# shut down or you killed/destroyed the application.
- self.MaxSize = 2750
+ #
+ # Windows and OS-X seem to be better behaved in this regard.
+ # They may not draw it, but they don't crash either!
+ self.MaxFontSize = 1000
self.ShiftFun = self.ShiftFunDict[Position]
- ## Compute the BB
+ self.CalcBoundingBox()
+
+
+ def CalcBoundingBox(self):
## this isn't exact, as fonts don't scale exactly.
dc = wx.MemoryDC()
bitmap = wx.EmptyBitmap(1, 1)
dc.SelectObject(bitmap) #wxMac needs a Bitmap selected for GetTextExtent to work.
DrawingSize = 40 # pts This effectively determines the resolution that the BB is computed to.
- ScaleFactor = float(Size) / DrawingSize
+ ScaleFactor = float(self.Size) / DrawingSize
dc.SetFont(self.SetFont(DrawingSize, self.Family, self.Style, self.Weight, self.Underline, self.FaceName) )
(w,h) = dc.GetTextExtent(self.String)
w = w * ScaleFactor
h = h * ScaleFactor
- x, y = self.ShiftFun(x, y, w, h, world = 1)
+ x, y = self.ShiftFun(self.XY[0], self.XY[1], w, h, world = 1)
self.BoundingBox = array(((x, y-h ),(x + w, y)),Float)
-
- # the new coords are set to the corner of the BB:
- #self.X = self.BoundingBox[0,0]
- #self.Y = self.BoundingBox[1,1]
+
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
(X,Y) = WorldToPixel( (self.XY) )
## Check to see if the font size is large enough to blow up the X font server
## If so, limit it. Would it be better just to not draw it?
## note that this limit is dependent on how much memory you have, etc.
- if Size > self.MaxSize:
- Size = self.MaxSize
+ Size = min(Size, self.MaxFontSize)
dc.SetFont(self.SetFont(Size, self.Family, self.Style, self.Weight, self.Underline, self.FaceName))
dc.SetTextForeground(self.Color)
if self.BackgroundColor:
elif ProjectionFun is None:
self.ProjectionFun = lambda x=None: array( (1,1), Float)
else:
- raise FloatCanvasException('Projectionfun must be either: "FlatEarth", None, or a function that takes the ViewPortCenter and returns a MapProjectionVector')
+ raise FloatCanvasError('Projectionfun must be either: "FlatEarth", None, or a function that takes the ViewPortCenter and returns a MapProjectionVector')
def FlatEarthProjection(self,CenterPoint):
return array((cos(pi*CenterPoint[1]/180),1),Float)
if Mode in ["ZoomIn","ZoomOut","Move","Mouse",None]:
self.GUIMode = Mode
else:
- raise FloatCanvasException('"%s" is Not a valid Mode'%Mode)
+ raise FloatCanvasError('"%s" is Not a valid Mode'%Mode)
def MakeHitDict(self):
##fixme: Should this just be None if nothing has been bound?
EVT_FC_LEAVE_OBJECT: {},
}
- def RaiseMouseEvent(self, Event, EventType):
+ def _RaiseMouseEvent(self, Event, EventType):
"""
This is called in various other places to raise a Mouse Event
"""
#print "in Raise Mouse Event", Event
pt = self.PixelToWorld( Event.GetPosition() )
- evt = MouseEvent(EventType, Event, self.GetId(), pt)
+ evt = _MouseEvent(EventType, Event, self.GetId(), pt)
self.GetEventHandler().ProcessEvent(evt)
def HitTest(self, event, HitEvent):
if self.GUIMode == "Mouse":
EventType = EVT_FC_LEFT_DCLICK
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
def MiddleDownEvent(self,event):
if self.GUIMode == "Mouse":
EventType = EVT_FC_MIDDLE_DOWN
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
def MiddleUpEvent(self,event):
if self.GUIMode == "Mouse":
EventType = EVT_FC_MIDDLE_UP
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
def MiddleDoubleClickEvent(self,event):
if self.GUIMode == "Mouse":
EventType = EVT_FC_MIDDLE_DCLICK
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
def RightUpEvent(self,event):
if self.GUIMode == "Mouse":
EventType = EVT_FC_RIGHT_UP
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
def RightDoubleCLickEvent(self,event):
if self.GUIMode == "Mouse":
EventType = EVT_FC_RIGHT_DCLICK
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
def WheelEvent(self,event):
- if self.GUIMode == "Mouse":
- self.RaiseMouseEvent(event, EVT_FC_MOUSEWHEEL)
+ ##if self.GUIMode == "Mouse":
+ ## Why not always raise this?
+ self._RaiseMouseEvent(event, EVT_FC_MOUSEWHEEL)
def LeftDownEvent(self,event):
elif self.GUIMode == "Mouse":
## check for a hit
if not self.HitTest(event, EVT_FC_LEFT_DOWN):
- self.RaiseMouseEvent(event,EVT_FC_LEFT_DOWN)
+ self._RaiseMouseEvent(event,EVT_FC_LEFT_DOWN)
else:
pass
def LeftUpEvent(self,event):
- if self.HasCapture():
+ if self.HasCapture():
self.ReleaseMouse()
if self.GUIMode:
if self.GUIMode == "ZoomIn":
StartMove = self.StartMove
EndMove = array((event.GetX(),event.GetY()))
if sum((StartMove-EndMove)**2) > 16:
- self.Move(StartMove-EndMove,'Pixel')
+ self.MoveImage(StartMove-EndMove,'Pixel')
self.StartMove = None
elif self.GUIMode == "Mouse":
EventType = EVT_FC_LEFT_UP
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
else:
pass
## Only do something if there are mouse over events bound
if self.HitDict and (self.HitDict[ EVT_FC_ENTER_OBJECT ] or self.HitDict[ EVT_FC_LEAVE_OBJECT ] ):
if not self.MouseOverTest(event):
- self.RaiseMouseEvent(event,EVT_FC_MOTION)
+ self._RaiseMouseEvent(event,EVT_FC_MOTION)
else:
pass
- self.RaiseMouseEvent(event,EVT_FC_MOTION)
+ self._RaiseMouseEvent(event,EVT_FC_MOTION)
else:
pass
elif self.GUIMode == "Mouse":
EventType = EVT_FC_RIGHT_DOWN
if not self.HitTest(event, EventType):
- self.RaiseMouseEvent(event, EventType)
+ self._RaiseMouseEvent(event, EventType)
else:
pass
animation, for instance.
"""
- #print "In Draw"
- if self.PanelSize < (1,1):
+# print "in Draw", self.PanelSize
+ if sometrue(self.PanelSize < 1 ): # it's possible for this to get called before being properly initialized.
+# if self.PanelSize < (1,1): # it's possible for this to get called before being properly initialized.
return
if self.Debug: start = clock()
ScreenDC = wx.ClientDC(self)
ViewPortBB,
self._ForegroundHTdc)
ScreenDC.Blit(0, 0, self.PanelSize[0],self.PanelSize[1], dc, 0, 0)
-## wx.GetApp().Yield(True)
# If the canvas is in the middle of a zoom or move, the Rubber Band box needs to be re-drawn
# This seeems out of place, but it works.
if self.PrevRBBox:
## else:
## return False
- def Move(self,shift,CoordType):
+ def MoveImage(self,shift,CoordType):
"""
move the image in the window.
"""
- shift = array(shift,Float)
+ shift = asarray(shift,Float)
+ #print "shifting by:", shift
if CoordType == 'Panel':# convert from panel coordinates
shift = shift * array((-1,1),Float) *self.PanelSize/self.TransformVector
elif CoordType == 'Pixel': # convert from pixel coordinates
elif CoordType == 'World': # No conversion
pass
else:
- raise FloatCanvasException('CoordType must be either "Panel", "Pixel", or "World"')
-
+ raise FloatCanvasError('CoordType must be either "Panel", "Pixel", or "World"')
+
+ #print "shifting by:", shift
+
self.ViewPortCenter = self.ViewPortCenter + shift
self.MapProjectionVector = self.ProjectionFun(self.ViewPortCenter)
self.TransformVector = array((self.Scale,-self.Scale),Float) * self.MapProjectionVector
self.BoundingBox = None
self.ViewPortCenter= array( (0,0), Float)
self.TransformVector = array( (1,-1), Float)
- self.MapProjectionVector = array( (1,1), Float)
+ self.MapProjectionVector = array( (1,1), Float)
self.Scale = 1
self.BoundingBoxDirty = False
a 2-tuple, or sequence of 2-tuples.
"""
#Note: this can be called by users code for various reasons, so asarray is needed.
- return (((asarray(Coordinates,Float) - self.ViewPortCenter)*self.TransformVector)+(self.HalfPanelSize)).astype('i')
+ return (((asarray(Coordinates,Float) -
+ self.ViewPortCenter)*self.TransformVector)+
+ (self.HalfPanelSize)).astype('i')
def ScaleWorldToPixel(self,Lengths):
"""
NumBetweenBlits = self.NumBetweenBlits # for speed
for i, Object in enumerate(self._ShouldRedraw(DrawList, ViewPortBB)):
Object._Draw(dc, WorldToPixel, ScaleWorldToPixel, HTdc)
- if i % NumBetweenBlits == 0:
+ if i+1 % NumBetweenBlits == 0:
Blit(0, 0, PanelSize0, PanelSize1, dc, 0, 0)
dc.EndDrawing()
def _makeFloatCanvasAddMethods(): ## lrk's code for doing this in module __init__
classnames = ["Circle", "Ellipse", "Rectangle", "ScaledText", "Polygon",
- "Line", "Text", "PointSet"]
+ "Line", "Text", "PointSet","Point", "Arrow"]
for classname in classnames:
klass = globals()[classname]
def getaddshapemethod(klass=klass):