1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#!/usr/bin/env python
"""
A test and demo of the ScaledTextbox.
It also shows how one can use the Mouse to interact and change objects on a Canvas.
this really needs to be re-done with GUI-Modes.
"""
import wx
## import the installed version
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
## import a local version
#import sys
#sys.path.append("../")
#from floatcanvas import NavCanvas, FloatCanvas, Resources
import numpy as np
LongString = (
"""This is a long string. It is a bunch of text. I am using it to test how the nifty wrapping text box works when you want to re-size.
This is another paragraph. I am trying to make it long enough to wrap a reasonable amount. Let's see how it works.
This is a way to start a paragraph with indenting
"""
)
##LongString = (
##""" This is a not so long string
##Another line""")
class DrawFrame(wx.Frame):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)
self.CreateStatusBar()
# Add the Canvas
Canvas = NavCanvas.NavCanvas(self,-1,(500,500),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE",
).Canvas
self.Canvas = Canvas
self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove )
self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp )
self.Canvas.Bind(FloatCanvas.EVT_LEFT_DOWN, self.OnLeftDown)
Point = np.array((0,0), float)
Canvas.AddCircle(Point,
Diameter=40,
FillColor="Red",
LineStyle=None,
)
Width = 300
self.Box = Canvas.AddScaledTextBox(LongString,
Point,
10,
Color = "Black",
BackgroundColor = 'White',
LineStyle = "Solid",
LineWidth = 2,
Width = Width,
PadSize = 10.0,
Family = wx.ROMAN,
#Family = wx.TELETYPE,
Style = wx.NORMAL,
Weight = wx.NORMAL,
Underlined = False,
Position = 'tl',
LineSpacing = 0.8,
Alignment = "left",
#Alignment = "center",
#Alignment = "right",
InForeground = False)
self.Handle1 = Canvas.AddBitmap(Resources.getMoveCursorBitmap(), Point, Position='cc')
self.Handle2a = Canvas.AddBitmap(Resources.getMoveRLCursorBitmap(), Point, Position='cc')
self.Handle2b = Canvas.AddBitmap(Resources.getMoveRLCursorBitmap(), Point, Position='cc')
self.SetHandles()
self.Handle1.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.Handle1Hit)
self.Handle2a.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.Handle2Hit)
self.Handle2b.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.Handle2Hit)
self.Show(True)
self.Canvas.ZoomToBB()
self.Resizing = False
self.ResizeRect = None
self.Moving = False
return None
def Handle1Hit(self, object):
if not self.Moving:
self.Moving = True
self.StartPoint = object.HitCoordsPixel
def Handle2Hit(self,event=None):
if not self.Resizing:
self.Resizing = True
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
And moves a point if there is one
"""
self.SetStatusText("%.4f, %.4f"%tuple(event.Coords))
if self.Resizing:
((xy),(wh)) = self.Box.GetBoxRect()
(xp, yp) = self.Canvas.WorldToPixel(xy)
(wp, hp) = self.Canvas.ScaleWorldToPixel(wh)
hp = -hp
Corner = event.GetPosition()
if self.Box.Position[1] in 'lc':
wp = max(20, Corner[0]-xp) # don't allow the box to get narrower than 20 pixels
elif self.Box.Position[1] in 'r':
DeltaX = Corner[0]-xp
xp += DeltaX
wp -= DeltaX
# draw the RB box
dc = wx.ClientDC(self.Canvas)
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetLogicalFunction(wx.XOR)
if self.ResizeRect:
dc.DrawRectangle(*self.ResizeRect)
self.ResizeRect = (xp,yp,wp,hp)
dc.DrawRectangle(*self.ResizeRect)
elif self.Moving:
dxy = event.GetPosition() - self.StartPoint
((xy),(wh)) = self.Box.GetBoxRect()
xp, yp = self.Canvas.WorldToPixel(xy) + dxy
(wp, hp) = self.Canvas.ScaleWorldToPixel(wh)
hp = -hp
# draw the RB box
dc = wx.ClientDC(self.Canvas)
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetLogicalFunction(wx.XOR)
if self.ResizeRect:
dc.DrawRectangle(*self.ResizeRect)
self.ResizeRect = (xp,yp,wp,hp)
dc.DrawRectangle(*self.ResizeRect)
def OnLeftDown(self, event):
pass
def OnLeftUp(self, event):
if self.Resizing:
self.Resizing = False
if self.ResizeRect:
Point = self.Canvas.PixelToWorld(self.ResizeRect[:2])
W, H = self.Canvas.ScalePixelToWorld(self.ResizeRect[2:4])
self.ResizeRect = None
self.Box.ReWrap(W)
self.SetHandles()
elif self.Moving:
self.Moving = False
if self.ResizeRect:
dxy = event.GetPosition() - self.StartPoint
dxy = self.Canvas.ScalePixelToWorld(dxy)
self.Box.Move(dxy)
self.ResizeRect = None
# self.Box.SetPoint(Point1)
self.SetHandles()
self.Canvas.Draw(True)
def SetHandles(self):
((x,y),(w,h)) = self.Box.GetBoxRect()
if self.Box.Position[1] in "lc":
x += w
y -= h/3
self.Handle2a.SetPoint((x,y))
y -= h/3
self.Handle2b.SetPoint((x,y))
self.Handle1.SetPoint(self.Box.XY)
app = wx.App()
DrawFrame(None, -1, "FloatCanvas TextBox Test App", wx.DefaultPosition, (700,700) )
app.MainLoop()
|