wxPython 2.0b9, first phase (win32)
Added gobs of stuff, see wxPython/README.txt for details git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@2310 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
1
utils/wxPython/lib/.cvsignore
Normal file
1
utils/wxPython/lib/.cvsignore
Normal file
@@ -0,0 +1 @@
|
||||
*.pyc
|
3
utils/wxPython/lib/__init__.py
Normal file
3
utils/wxPython/lib/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
|
106
utils/wxPython/lib/dialogs.py
Normal file
106
utils/wxPython/lib/dialogs.py
Normal file
@@ -0,0 +1,106 @@
|
||||
from wxPython.wx import *
|
||||
from layoutf import Layoutf
|
||||
import string
|
||||
|
||||
|
||||
|
||||
class wxScrolledMessageDialog(wxDialog):
|
||||
|
||||
def __init__(self, parent, msg, caption, pos = None, size = None):
|
||||
if not pos:
|
||||
pos = wxDefaultPosition
|
||||
if not size:
|
||||
size = wxSize(500,300)
|
||||
wxDialog.__init__(self, parent, -1, caption, pos, size)
|
||||
text = wxTextCtrl(self, -1, msg, wxDefaultPosition,
|
||||
wxDefaultSize,
|
||||
wxTE_MULTILINE | wxTE_READONLY)
|
||||
ok = wxButton(self, wxID_OK, "OK")
|
||||
text.SetConstraints(Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok)))
|
||||
ok.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self,)))
|
||||
self.SetAutoLayout(TRUE)
|
||||
|
||||
|
||||
|
||||
class wxMultipleChoiceDialog(wxDialog):
|
||||
|
||||
def __init__(self, parent, msg, title, lst, pos = None, size = None):
|
||||
if not pos:
|
||||
pos = wxDefaultPosition
|
||||
if not size:
|
||||
size = wxSize(200,200)
|
||||
wxDialog.__init__(self, parent, -1, title, pos, size)
|
||||
dc = wxClientDC(self)
|
||||
height = 0
|
||||
for line in string.split(msg,'\n'):
|
||||
height = height + dc.GetTextExtent(msg)[1] + 4
|
||||
stat = wxStaticText(self, -1, msg)
|
||||
self.lbox = wxListBox(self, 100, wxDefaultPosition,
|
||||
wxDefaultSize, lst, wxLB_MULTIPLE)
|
||||
ok = wxButton(self, wxID_OK, "OK")
|
||||
cancel = wxButton(self, wxID_CANCEL, "Cancel")
|
||||
stat.SetConstraints(Layoutf('t=t10#1;l=l5#1;r=r5#1;h!%d' % (height,),
|
||||
(self,)))
|
||||
self.lbox.SetConstraints(Layoutf('t=b10#2;l=l5#1;r=r5#1;b=t5#3',
|
||||
(self, stat, ok)))
|
||||
ok.SetConstraints(Layoutf('b=b5#1;x%w25#1;w!80;h!25', (self,)))
|
||||
cancel.SetConstraints(Layoutf('b=b5#1;x%w75#1;w!80;h!25', (self,)))
|
||||
self.SetAutoLayout(TRUE)
|
||||
self.lst = lst
|
||||
|
||||
def OnSize(self, event):
|
||||
self.Layout()
|
||||
|
||||
def GetValue(self):
|
||||
return self.lbox.GetSelections()
|
||||
|
||||
def GetValueString(self):
|
||||
sel = self.lbox.GetSelections()
|
||||
val = []
|
||||
for i in sel:
|
||||
val.append(self.lst[i])
|
||||
return tuple(val)
|
||||
|
||||
if __name__ == '__main__':
|
||||
class MyFrame(wxFrame):
|
||||
def __init__(self):
|
||||
wxFrame.__init__(self, NULL, -1, "hello",
|
||||
wxDefaultPosition, wxSize(200,200))
|
||||
wxButton(self, 100, "Multiple Test",wxPoint(0,0))
|
||||
wxButton(self, 101, "Message Test", wxPoint(0,100))
|
||||
EVT_BUTTON(self, 100, self.OnMultipleTest)
|
||||
EVT_BUTTON(self, 101, self.OnMessageTest)
|
||||
|
||||
def OnMultipleTest(self, event):
|
||||
self.lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange',
|
||||
'etc', 'etc..', 'etc...' ]
|
||||
dlg = wxMultipleChoiceDialog(self,
|
||||
"Pick some from\n this list\nblabla",
|
||||
"m.s.d.", self.lst)
|
||||
if (dlg.ShowModal() == wxID_OK):
|
||||
print "Selection:", dlg.GetValue(), " -> ", dlg.GetValueString()
|
||||
|
||||
def OnMessageTest(self, event):
|
||||
import sys;
|
||||
f = open(sys.argv[0],"r")
|
||||
msg = f.read()
|
||||
dlg = wxScrolledMessageDialog(self, msg, "message test")
|
||||
dlg.ShowModal()
|
||||
|
||||
|
||||
class MyApp(wxApp):
|
||||
def OnInit(self):
|
||||
frame = MyFrame()
|
||||
frame.Show(TRUE)
|
||||
self.SetTopWindow(frame)
|
||||
return TRUE
|
||||
|
||||
app = MyApp(0)
|
||||
app.MainLoop()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
262
utils/wxPython/lib/layoutf.py
Normal file
262
utils/wxPython/lib/layoutf.py
Normal file
@@ -0,0 +1,262 @@
|
||||
from wxPython.wx import wxLayoutConstraints,\
|
||||
wxTop, wxLeft, wxBottom, wxRight, \
|
||||
wxHeight, wxWidth, wxCentreX, wxCentreY
|
||||
import re,string
|
||||
|
||||
class Layoutf(wxLayoutConstraints):
|
||||
"""
|
||||
The class Layoutf(wxLayoutConstraints) presents a simplification
|
||||
of the wxLayoutConstraints syntax. The name Layoutf is choosen
|
||||
because of the similarity with C's printf function.
|
||||
|
||||
Quick Example:
|
||||
|
||||
lc = Layoutf('t=t#1;l=r10#2;r!100;h%h50#1', (self, self.panel))
|
||||
|
||||
is equivalent to
|
||||
|
||||
lc = wxLayoutContraints()
|
||||
lc.top.SameAs(self, wxTop)
|
||||
lc.left.SameAs(self.panel, wxRight, 10)
|
||||
lc.right.Absolute(100)
|
||||
lc.height.PercentOf(self, wxHeight, 50)
|
||||
|
||||
Usage:
|
||||
|
||||
You can give a constraint string to the Layoutf constructor,
|
||||
or use the 'pack' method. The following are equivalent:
|
||||
|
||||
lc = Layoutf('t=t#1;l=r#2;r!100;h%h50#1', (self, self.panel))
|
||||
|
||||
and
|
||||
|
||||
lc = Layoutf()
|
||||
lc.pack('t=t#1;l=r#2;r!100;h%h50#1', (self, self.panel))
|
||||
|
||||
Besides 'pack' there's also 'debug_pack' which does not set
|
||||
constraints, but prints traditional wxLayoutConstraint calls to
|
||||
stdout.
|
||||
|
||||
The calls to the Layoutf constructor and pack methods have
|
||||
the following argument list:
|
||||
|
||||
(constraint_string, objects_tuple)
|
||||
|
||||
Constraint String syntax:
|
||||
|
||||
Constraint directives are separated by semi-colons. You
|
||||
generally (always?) need four directives to completely describe a
|
||||
subwindow's location.
|
||||
|
||||
A single directive has either of the following forms:
|
||||
|
||||
1. <own attribute><compare operation>[numerical argument]
|
||||
for example r!100 -> lc.right.Absolute(100) )
|
||||
and w* -> lc.width.AsIs()
|
||||
|
||||
2. <own attribute><compare operation>[numerical argument]
|
||||
#<compare object nr.>
|
||||
for example t_10#2 (lc.top.Below(<second obj>, 10)
|
||||
|
||||
3. <own attribute><compare operation><compare attribute>
|
||||
[numerical argument]#<compare object nr.>
|
||||
for example w%h50#2 ( lc.width.PercentOf(<second obj>,
|
||||
wxHeight, 50) and t=b#1 ( lc.top.SameAs(<first obj>,
|
||||
wxBottom) )
|
||||
|
||||
Which one you need is defined by the <compare operation>
|
||||
type. The following take type 1 (no object to compare with):
|
||||
|
||||
'!': 'Absolute', '?': 'Unconstrained', '*': 'AsIs'
|
||||
|
||||
These take type 2 (need to be compared with another object)
|
||||
|
||||
'<': 'LeftOf', '>': 'RightOf', '^': 'Above', '_': 'Below'
|
||||
|
||||
These take type 3 (need to be compared to another object
|
||||
attribute)
|
||||
|
||||
'=': 'SameAs', '%': 'PercentOf'
|
||||
|
||||
For all types, the <own attribute> letter can be any of
|
||||
|
||||
't': 'top', 'l': 'left', 'b': 'bottom',
|
||||
'r': 'right', 'h': 'height', 'w': 'width',
|
||||
'x': 'centreX', 'y': 'centreY'
|
||||
|
||||
If the operation takes an (optional) numerical argument, place it
|
||||
in [numerical argument]. For type 3 directives, the <compare
|
||||
attribute> letter can be any of
|
||||
|
||||
't': 'wxTop', 'l': 'wxLeft', 'b': 'wxBottom'
|
||||
'r': 'wxRight', 'h': 'wxHeight', 'w': 'wxWidth',
|
||||
'x': 'wxCentreX', 'y': 'wxCentreY'
|
||||
|
||||
Note that these are the same letters as used for <own attribute>,
|
||||
so you'll only need to remember one set. Finally, the object
|
||||
whose attribute is refered to, is specified by #<compare object
|
||||
nr>, where <compare object nr> is the 1-based (stupid, I know,
|
||||
but I've gotten used to it) index of the object in the
|
||||
objects_tuple argument.
|
||||
|
||||
Bugs:
|
||||
|
||||
Not entirely happy about the logic in the order of arguments
|
||||
after the <compare operation> character.
|
||||
|
||||
Not all wxLayoutConstraint methods are included in the
|
||||
syntax. However, the type 3 directives are generally the most
|
||||
used. Further excuse: wxWindows layout constraints are at the
|
||||
time of this writing not documented.
|
||||
|
||||
"""
|
||||
|
||||
attr_d = { 't': 'top', 'l': 'left', 'b': 'bottom',
|
||||
'r': 'right', 'h': 'height', 'w': 'width',
|
||||
'x': 'centreX', 'y': 'centreY' }
|
||||
op_d = { '=': 'SameAs', '%': 'PercentOf', '<': 'LeftOf',
|
||||
'>': 'RightOf', '^': 'Above', '_': 'Below',
|
||||
'!': 'Absolute', '?': 'Unconstrained', '*': 'AsIs' }
|
||||
cmp_d = { 't': 'wxTop', 'l': 'wxLeft', 'b': 'wxBottom',
|
||||
'r': 'wxRight', 'h': 'wxHeight', 'w': 'wxWidth',
|
||||
'x': 'wxCentreX', 'y': 'wxCentreY' }
|
||||
|
||||
rexp1 = re.compile('^\s*([tlrbhwxy])\s*([!\?\*])\s*(\d*)\s*$')
|
||||
rexp2 = re.compile('^\s*([tlrbhwxy])\s*([=%<>^_])\s*([tlrbhwxy]?)\s*(\d*)\s*#(\d+)\s*$')
|
||||
|
||||
def __init__(self,pstr=None,winlist=None):
|
||||
wxLayoutConstraints.__init__(self)
|
||||
if pstr:
|
||||
self.pack(pstr,winlist)
|
||||
|
||||
def pack(self, pstr, winlist):
|
||||
pstr = string.lower(pstr)
|
||||
for item in string.split(pstr,';'):
|
||||
m = self.rexp1.match(item)
|
||||
if m:
|
||||
g = list(m.groups())
|
||||
attr = getattr(self, self.attr_d[g[0]])
|
||||
func = getattr(attr, self.op_d[g[1]])
|
||||
if g[1] == '!':
|
||||
func(int(g[2]))
|
||||
else:
|
||||
func()
|
||||
continue
|
||||
m = self.rexp2.match(item)
|
||||
if not m: raise ValueError
|
||||
g = list(m.groups())
|
||||
attr = getattr(self, self.attr_d[g[0]])
|
||||
func = getattr(attr, self.op_d[g[1]])
|
||||
if g[3]: g[3] = int(g[3])
|
||||
else: g[3] = None;
|
||||
g[4] = int(g[4]) - 1
|
||||
if g[1] in '<>^_':
|
||||
if g[3]: func(winlist[g[4]], g[3])
|
||||
else: func(winlist[g[4]])
|
||||
else:
|
||||
cmp = eval(self.cmp_d[g[2]])
|
||||
if g[3]: func(winlist[g[4]], cmp, g[3])
|
||||
else: func(winlist[g[4]], cmp)
|
||||
|
||||
def debug_pack(self, pstr, winlist):
|
||||
pstr = string.lower(pstr)
|
||||
for item in string.split(pstr,';'):
|
||||
m = self.rexp1.match(item)
|
||||
if m:
|
||||
g = list(m.groups())
|
||||
attr = getattr(self, self.attr_d[g[0]])
|
||||
func = getattr(attr, self.op_d[g[1]])
|
||||
if g[1] == '!':
|
||||
print "%s.%s.%s(%s)" % \
|
||||
('self',self.attr_d[g[0]],self.op_d[g[1]],g[2])
|
||||
else:
|
||||
print "%s.%s.%s()" % \
|
||||
('self',self.attr_d[g[0]],self.op_d[g[1]])
|
||||
continue
|
||||
m = self.rexp2.match(item)
|
||||
if not m: raise ValueError
|
||||
g = list(m.groups())
|
||||
if g[3]: g[3] = int(g[3])
|
||||
else: g[3] = 0;
|
||||
g[4] = int(g[4]) - 1
|
||||
if g[1] in '<>^_':
|
||||
if g[3]: print "%s.%s.%s(%s,%d)" % \
|
||||
('self',self.attr_d[g[0]],self.op_d[g[1]],winlist[g[4]],
|
||||
g[3])
|
||||
else: print "%s.%s.%s(%s)" % \
|
||||
('self',self.attr_d[g[0]],self.op_d[g[1]],winlist[g[4]])
|
||||
else:
|
||||
if g[3]: print "%s.%s.%s(%s,%s,%d)" % \
|
||||
('self',self.attr_d[g[0]],self.op_d[g[1]],winlist[g[4]],
|
||||
self.cmp_d[g[2]],g[3])
|
||||
else: print "%s.%s.%s(%s,%s)" % \
|
||||
('self',self.attr_d[g[0]],self.op_d[g[1]],winlist[g[4]],
|
||||
self.cmp_d[g[2]])
|
||||
|
||||
if __name__=='__main__':
|
||||
from wxPython.wx import *
|
||||
|
||||
class TestLayoutf(wxFrame):
|
||||
def __init__(self, parent):
|
||||
wxFrame.__init__(self, parent, -1, 'Test Layout Constraints',
|
||||
wxPyDefaultPosition, wxSize(500, 300))
|
||||
|
||||
self.SetAutoLayout(true)
|
||||
EVT_BUTTON(self, 100, self.OnButton)
|
||||
EVT_BUTTON(self, 101, self.OnAbout)
|
||||
|
||||
self.panelA = wxWindow(self, -1, wxPyDefaultPosition, wxPyDefaultSize, wxSIMPLE_BORDER)
|
||||
self.panelA.SetBackgroundColour(wxBLUE)
|
||||
self.panelA.SetConstraints(Layoutf('t=t10#1;l=l10#1;b=b10#1;r%r50#1',(self,)))
|
||||
|
||||
self.panelB = wxWindow(self, -1, wxPyDefaultPosition, wxPyDefaultSize, wxSIMPLE_BORDER)
|
||||
self.panelB.SetBackgroundColour(wxRED)
|
||||
self.panelB.SetConstraints(Layoutf('t=t10#1;r=r10#1;b%b30#1;l>10#2', (self,self.panelA)))
|
||||
|
||||
self.panelC = wxWindow(self, -1, wxPyDefaultPosition, wxPyDefaultSize, wxSIMPLE_BORDER)
|
||||
self.panelC.SetBackgroundColour(wxWHITE)
|
||||
self.panelC.SetConstraints(Layoutf('t_10#3;r=r10#1;b=b10#1;l>10#2', (self,self.panelA,self.panelB)))
|
||||
|
||||
b = wxButton(self.panelA, 101, ' About: ')
|
||||
b.SetConstraints(Layoutf('X=X#1;Y=Y#1;h*;w%w50#1', (self.panelA,)))
|
||||
|
||||
b = wxButton(self.panelB, 100, ' Panel B ')
|
||||
b.SetConstraints(Layoutf('t=t2#1;r=r4#1;h*;w*', (self.panelB,)))
|
||||
|
||||
self.panelD = wxWindow(self.panelC, -1, wxPyDefaultPosition, wxPyDefaultSize, wxSIMPLE_BORDER)
|
||||
self.panelD.SetBackgroundColour(wxGREEN)
|
||||
self.panelD.SetConstraints(Layoutf('b%h50#1;r%w50#1;h=h#2;w=w#2', (self.panelC, b)))
|
||||
|
||||
b = wxButton(self.panelC, 100, ' Panel C ')
|
||||
b.SetConstraints(Layoutf('t_#1;l>#1;h*;w*', (self.panelD,)))
|
||||
|
||||
wxStaticText(self.panelD, -1, "Panel D", wxPoint(4, 4)).SetBackgroundColour(wxGREEN)
|
||||
|
||||
def OnButton(self, event):
|
||||
self.Close(true)
|
||||
|
||||
def OnAbout(self, event):
|
||||
try:
|
||||
from dialogs import wxScrolledMessageDialog
|
||||
msg = wxScrolledMessageDialog(self, Layoutf.__doc__, "about")
|
||||
msg.ShowModal()
|
||||
except:
|
||||
print msg
|
||||
|
||||
def OnCloseWindow(self, event):
|
||||
self.Destroy()
|
||||
|
||||
class TestApp(wxApp):
|
||||
def OnInit(self):
|
||||
frame = TestLayoutf(NULL)
|
||||
frame.Show(1)
|
||||
self.SetTopWindow(frame)
|
||||
return 1
|
||||
|
||||
app = TestApp(0)
|
||||
app.MainLoop()
|
||||
|
||||
|
||||
|
||||
|
||||
|
468
utils/wxPython/lib/wxPlotCanvas.py
Normal file
468
utils/wxPython/lib/wxPlotCanvas.py
Normal file
@@ -0,0 +1,468 @@
|
||||
"""
|
||||
This is a port of Konrad Hinsen's tkPlotCanvas.py plotting module.
|
||||
After thinking long and hard I came up with the name "wxPlotCanvas.py".
|
||||
|
||||
This file contains two parts; first the re-usable library stuff, then, after
|
||||
a "if __name__=='__main__'" test, a simple frame and a few default plots
|
||||
for testing.
|
||||
|
||||
Harm van der Heijden, feb 1999
|
||||
|
||||
Original comment follows below:
|
||||
# This module defines a plot widget for Tk user interfaces.
|
||||
# It supports only elementary line plots at the moment.
|
||||
# See the example at the end for documentation...
|
||||
#
|
||||
# Written by Konrad Hinsen <hinsen@cnrs-orleans.fr>
|
||||
# With contributions from RajGopal Srinivasan <raj@cherubino.med.jhmi.edu>
|
||||
# Last revision: 1998-7-28
|
||||
#
|
||||
"""
|
||||
|
||||
from wxPython import wx
|
||||
import string
|
||||
|
||||
# Not everybody will have Numeric, so let's be cool about it...
|
||||
try:
|
||||
import Numeric
|
||||
except:
|
||||
# bummer!
|
||||
d = wx.wxMessageDialog(wx.NULL,
|
||||
"""This module requires the Numeric module, which could not be imported.
|
||||
It probably is not installed (it's not part of the standard Python
|
||||
distribution). See the Python site (http://www.python.org) for
|
||||
information on downloading source or binaries.""",
|
||||
"Numeric not found")
|
||||
if d.ShowModal() == wx.wxID_CANCEL:
|
||||
d = wx.wxMessageDialog(wx.NULL, "I kid you not! Pressing Cancel won't help you!", "Not a joke", wx.wxOK)
|
||||
d.ShowModal()
|
||||
import sys
|
||||
sys.exit()
|
||||
|
||||
#
|
||||
# Plotting classes...
|
||||
#
|
||||
class PolyPoints:
|
||||
|
||||
def __init__(self, points, attr):
|
||||
self.points = Numeric.array(points)
|
||||
self.scaled = self.points
|
||||
self.attributes = {}
|
||||
for name, value in self._attributes.items():
|
||||
try:
|
||||
value = attr[name]
|
||||
except KeyError: pass
|
||||
self.attributes[name] = value
|
||||
|
||||
def boundingBox(self):
|
||||
return Numeric.minimum.reduce(self.points), \
|
||||
Numeric.maximum.reduce(self.points)
|
||||
|
||||
def scaleAndShift(self, scale=1, shift=0):
|
||||
self.scaled = scale*self.points+shift
|
||||
|
||||
|
||||
class PolyLine(PolyPoints):
|
||||
|
||||
def __init__(self, points, **attr):
|
||||
PolyPoints.__init__(self, points, attr)
|
||||
|
||||
_attributes = {'color': 'black',
|
||||
'width': 1}
|
||||
|
||||
def draw(self, dc):
|
||||
color = self.attributes['color']
|
||||
width = self.attributes['width']
|
||||
arguments = []
|
||||
dc.SetPen(wx.wxPen(wx.wxNamedColour(color), width))
|
||||
dc.DrawLines(map(tuple,self.scaled))
|
||||
|
||||
|
||||
class PolyMarker(PolyPoints):
|
||||
|
||||
def __init__(self, points, **attr):
|
||||
|
||||
PolyPoints.__init__(self, points, attr)
|
||||
|
||||
_attributes = {'color': 'black',
|
||||
'width': 1,
|
||||
'fillcolor': None,
|
||||
'size': 2,
|
||||
'fillstyle': wx.wxSOLID,
|
||||
'outline': 'black',
|
||||
'marker': 'circle'}
|
||||
|
||||
def draw(self, dc):
|
||||
color = self.attributes['color']
|
||||
width = self.attributes['width']
|
||||
size = self.attributes['size']
|
||||
fillcolor = self.attributes['fillcolor']
|
||||
fillstyle = self.attributes['fillstyle']
|
||||
marker = self.attributes['marker']
|
||||
|
||||
dc.SetPen(wx.wxPen(wx.wxNamedColour(color),width))
|
||||
if fillcolor:
|
||||
dc.SetBrush(wx.wxBrush(wx.wxNamedColour(fillcolor),fillstyle))
|
||||
else:
|
||||
dc.SetBrush(wx.wxBrush(wx.wxNamedColour('black'), wx.wxTRANSPARENT))
|
||||
|
||||
self._drawmarkers(dc, self.scaled, marker, size)
|
||||
|
||||
def _drawmarkers(self, dc, coords, marker,size=1):
|
||||
f = eval('self._' +marker)
|
||||
for xc, yc in coords:
|
||||
f(dc, xc, yc, size)
|
||||
|
||||
def _circle(self, dc, xc, yc, size=1):
|
||||
dc.DrawEllipse(xc-2.5*size,yc-2.5*size,5.*size,5.*size)
|
||||
|
||||
def _dot(self, dc, xc, yc, size=1):
|
||||
dc.DrawPoint(xc,yc)
|
||||
|
||||
def _square(self, dc, xc, yc, size=1):
|
||||
dc.DrawRectangle(xc-2.5*size,yc-2.5*size,5.*size,5.*size)
|
||||
|
||||
def _triangle(self, dc, xc, yc, size=1):
|
||||
dc.DrawPolygon([(-0.5*size*5,0.2886751*size*5),
|
||||
(0.5*size*5,0.2886751*size*5),
|
||||
(0.0,-0.577350*size*5)],xc,yc)
|
||||
|
||||
def _triangle_down(self, dc, xc, yc, size=1):
|
||||
dc.DrawPolygon([(-0.5*size*5,-0.2886751*size*5),
|
||||
(0.5*size*5,-0.2886751*size*5),
|
||||
(0.0,0.577350*size*5)],xc,yc)
|
||||
|
||||
def _cross(self, dc, xc, yc, size=1):
|
||||
dc.DrawLine(xc-2.5*size,yc-2.5*size,xc+2.5*size,yc+2.5*size)
|
||||
dc.DrawLine(xc-2.5*size,yc+2.5*size,xc+2.5*size,yc-2.5*size)
|
||||
|
||||
def _plus(self, dc, xc, yc, size=1):
|
||||
dc.DrawLine(xc-2.5*size,yc,xc+2.5*size,yc)
|
||||
dc.DrawLine(xc,yc-2.5*size,xc,yc+2.5*size)
|
||||
|
||||
class PlotGraphics:
|
||||
|
||||
def __init__(self, objects):
|
||||
self.objects = objects
|
||||
|
||||
def boundingBox(self):
|
||||
p1, p2 = self.objects[0].boundingBox()
|
||||
for o in self.objects[1:]:
|
||||
p1o, p2o = o.boundingBox()
|
||||
p1 = Numeric.minimum(p1, p1o)
|
||||
p2 = Numeric.maximum(p2, p2o)
|
||||
return p1, p2
|
||||
|
||||
def scaleAndShift(self, scale=1, shift=0):
|
||||
for o in self.objects:
|
||||
o.scaleAndShift(scale, shift)
|
||||
|
||||
def draw(self, canvas):
|
||||
for o in self.objects:
|
||||
o.draw(canvas)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.objects)
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.objects[item]
|
||||
|
||||
|
||||
class PlotCanvas(wx.wxWindow):
|
||||
|
||||
def __init__(self, parent, id = -1):
|
||||
wx.wxWindow.__init__(self, parent, id, wx.wxPyDefaultPosition, wx.wxPyDefaultSize)
|
||||
self.border = (1,1)
|
||||
self.SetClientSizeWH(400,400)
|
||||
self.SetBackgroundColour(wx.wxNamedColour("white"))
|
||||
|
||||
wx.EVT_SIZE(self,self.reconfigure)
|
||||
self._setsize()
|
||||
self.last_draw = None
|
||||
# self.font = self._testFont(font)
|
||||
|
||||
def OnPaint(self, event):
|
||||
pdc = wx.wxPaintDC(self)
|
||||
if self.last_draw is not None:
|
||||
apply(self.draw, self.last_draw + (pdc,))
|
||||
|
||||
def reconfigure(self, event):
|
||||
(new_width,new_height) = self.GetClientSizeTuple()
|
||||
if new_width == self.width and new_height == self.height:
|
||||
return
|
||||
self._setsize()
|
||||
# self.redraw()
|
||||
|
||||
def _testFont(self, font):
|
||||
if font is not None:
|
||||
bg = self.canvas.cget('background')
|
||||
try:
|
||||
item = CanvasText(self.canvas, 0, 0, anchor=NW,
|
||||
text='0', fill=bg, font=font)
|
||||
self.canvas.delete(item)
|
||||
except TclError:
|
||||
font = None
|
||||
return font
|
||||
|
||||
def _setsize(self):
|
||||
(self.width,self.height) = self.GetClientSizeTuple();
|
||||
self.plotbox_size = 0.97*Numeric.array([self.width, -self.height])
|
||||
xo = 0.5*(self.width-self.plotbox_size[0])
|
||||
yo = self.height-0.5*(self.height+self.plotbox_size[1])
|
||||
self.plotbox_origin = Numeric.array([xo, yo])
|
||||
|
||||
def draw(self, graphics, xaxis = None, yaxis = None, dc = None):
|
||||
if dc == None: dc = wx.wxClientDC(self)
|
||||
dc.BeginDrawing()
|
||||
dc.Clear()
|
||||
self.last_draw = (graphics, xaxis, yaxis)
|
||||
p1, p2 = graphics.boundingBox()
|
||||
xaxis = self._axisInterval(xaxis, p1[0], p2[0])
|
||||
yaxis = self._axisInterval(yaxis, p1[1], p2[1])
|
||||
text_width = [0., 0.]
|
||||
text_height = [0., 0.]
|
||||
if xaxis is not None:
|
||||
p1[0] = xaxis[0]
|
||||
p2[0] = xaxis[1]
|
||||
xticks = self._ticks(xaxis[0], xaxis[1])
|
||||
bb = dc.GetTextExtent(xticks[0][1])
|
||||
text_height[1] = bb[1]
|
||||
text_width[0] = 0.5*bb[0]
|
||||
bb = dc.GetTextExtent(xticks[-1][1])
|
||||
text_width[1] = 0.5*bb[0]
|
||||
else:
|
||||
xticks = None
|
||||
if yaxis is not None:
|
||||
p1[1] = yaxis[0]
|
||||
p2[1] = yaxis[1]
|
||||
yticks = self._ticks(yaxis[0], yaxis[1])
|
||||
for y in yticks:
|
||||
bb = dc.GetTextExtent(y[1])
|
||||
text_width[0] = max(text_width[0],bb[0])
|
||||
h = 0.5*bb[1]
|
||||
text_height[0] = h
|
||||
text_height[1] = max(text_height[1], h)
|
||||
else:
|
||||
yticks = None
|
||||
text1 = Numeric.array([text_width[0], -text_height[1]])
|
||||
text2 = Numeric.array([text_width[1], -text_height[0]])
|
||||
scale = (self.plotbox_size-text1-text2) / (p2-p1)
|
||||
shift = -p1*scale + self.plotbox_origin + text1
|
||||
self._drawAxes(dc, xaxis, yaxis, p1, p2,
|
||||
scale, shift, xticks, yticks)
|
||||
graphics.scaleAndShift(scale, shift)
|
||||
graphics.draw(dc)
|
||||
dc.EndDrawing()
|
||||
|
||||
def _axisInterval(self, spec, lower, upper):
|
||||
if spec is None:
|
||||
return None
|
||||
if spec == 'minimal':
|
||||
if lower == upper:
|
||||
return lower-0.5, upper+0.5
|
||||
else:
|
||||
return lower, upper
|
||||
if spec == 'automatic':
|
||||
range = upper-lower
|
||||
if range == 0.:
|
||||
return lower-0.5, upper+0.5
|
||||
log = Numeric.log10(range)
|
||||
power = Numeric.floor(log)
|
||||
fraction = log-power
|
||||
if fraction <= 0.05:
|
||||
power = power-1
|
||||
grid = 10.**power
|
||||
lower = lower - lower % grid
|
||||
mod = upper % grid
|
||||
if mod != 0:
|
||||
upper = upper - mod + grid
|
||||
return lower, upper
|
||||
if type(spec) == type(()):
|
||||
lower, upper = spec
|
||||
if lower <= upper:
|
||||
return lower, upper
|
||||
else:
|
||||
return upper, lower
|
||||
raise ValueError, str(spec) + ': illegal axis specification'
|
||||
|
||||
def _drawAxes(self, dc, xaxis, yaxis,
|
||||
bb1, bb2, scale, shift, xticks, yticks):
|
||||
dc.SetPen(wx.wxPen(wx.wxNamedColour('BLACK'),1))
|
||||
if xaxis is not None:
|
||||
lower, upper = xaxis
|
||||
text = 1
|
||||
for y, d in [(bb1[1], -3), (bb2[1], 3)]:
|
||||
p1 = scale*Numeric.array([lower, y])+shift
|
||||
p2 = scale*Numeric.array([upper, y])+shift
|
||||
dc.DrawLine(p1[0],p1[1],p2[0],p2[1])
|
||||
for x, label in xticks:
|
||||
p = scale*Numeric.array([x, y])+shift
|
||||
dc.DrawLine(p[0],p[1],p[0],p[1]+d)
|
||||
if text:
|
||||
dc.DrawText(label,p[0],p[1])
|
||||
text = 0
|
||||
|
||||
if yaxis is not None:
|
||||
lower, upper = yaxis
|
||||
text = 1
|
||||
h = dc.GetCharHeight()
|
||||
for x, d in [(bb1[0], -3), (bb2[0], 3)]:
|
||||
p1 = scale*Numeric.array([x, lower])+shift
|
||||
p2 = scale*Numeric.array([x, upper])+shift
|
||||
dc.DrawLine(p1[0],p1[1],p2[0],p2[1])
|
||||
for y, label in yticks:
|
||||
p = scale*Numeric.array([x, y])+shift
|
||||
dc.DrawLine(p[0],p[1],p[0]-d,p[1])
|
||||
if text:
|
||||
dc.DrawText(label,p[0]-dc.GetTextExtent(label)[0],
|
||||
p[1]-0.5*h)
|
||||
text = 0
|
||||
|
||||
def _ticks(self, lower, upper):
|
||||
ideal = (upper-lower)/7.
|
||||
log = Numeric.log10(ideal)
|
||||
power = Numeric.floor(log)
|
||||
fraction = log-power
|
||||
factor = 1.
|
||||
error = fraction
|
||||
for f, lf in self._multiples:
|
||||
e = Numeric.fabs(fraction-lf)
|
||||
if e < error:
|
||||
error = e
|
||||
factor = f
|
||||
grid = factor * 10.**power
|
||||
if power > 3 or power < -3:
|
||||
format = '%+7.0e'
|
||||
elif power >= 0:
|
||||
digits = max(1, int(power))
|
||||
format = '%' + `digits`+'.0f'
|
||||
else:
|
||||
digits = -int(power)
|
||||
format = '%'+`digits+2`+'.'+`digits`+'f'
|
||||
ticks = []
|
||||
t = -grid*Numeric.floor(-lower/grid)
|
||||
while t <= upper:
|
||||
ticks.append(t, format % (t,))
|
||||
t = t + grid
|
||||
return ticks
|
||||
|
||||
_multiples = [(2., Numeric.log10(2.)), (5., Numeric.log10(5.))]
|
||||
|
||||
def redraw(self,dc=None):
|
||||
if self.last_draw is not None:
|
||||
apply(self.draw, self.last_draw + (dc,))
|
||||
|
||||
def clear(self):
|
||||
self.canvas.delete('all')
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# if running standalone...
|
||||
#
|
||||
# ...a sample implementation using the above
|
||||
#
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
def _InitObjects():
|
||||
# 100 points sin function, plotted as green circles
|
||||
data1 = 2.*Numeric.pi*Numeric.arange(200)/200.
|
||||
data1.shape = (100, 2)
|
||||
data1[:,1] = Numeric.sin(data1[:,0])
|
||||
markers1 = PolyMarker(data1, color='green', marker='circle',size=1)
|
||||
|
||||
# 50 points cos function, plotted as red line
|
||||
data1 = 2.*Numeric.pi*Numeric.arange(100)/100.
|
||||
data1.shape = (50,2)
|
||||
data1[:,1] = Numeric.cos(data1[:,0])
|
||||
lines = PolyLine(data1, color='red')
|
||||
|
||||
# A few more points...
|
||||
pi = Numeric.pi
|
||||
markers2 = PolyMarker([(0., 0.), (pi/4., 1.), (pi/2, 0.),
|
||||
(3.*pi/4., -1)], color='blue',
|
||||
fillcolor='green', marker='cross')
|
||||
|
||||
return PlotGraphics([markers1, lines, markers2])
|
||||
|
||||
|
||||
class AppFrame(wx.wxFrame):
|
||||
def __init__(self, parent, id, title):
|
||||
wx.wxFrame.__init__(self, parent, id, title,
|
||||
wx.wxPyDefaultPosition, wx.wxSize(400, 400))
|
||||
|
||||
# Now Create the menu bar and items
|
||||
self.mainmenu = wx.wxMenuBar()
|
||||
|
||||
menu = wx.wxMenu()
|
||||
menu.Append(200, '&Print...', 'Print the current plot')
|
||||
wx.EVT_MENU(self, 200, self.OnFilePrint)
|
||||
menu.Append(209, 'E&xit', 'Enough of this already!')
|
||||
wx.EVT_MENU(self, 209, self.OnFileExit)
|
||||
self.mainmenu.Append(menu, '&File')
|
||||
|
||||
menu = wx.wxMenu()
|
||||
menu.Append(210, '&Draw', 'Draw plots')
|
||||
wx.EVT_MENU(self,210,self.OnPlotDraw)
|
||||
menu.Append(211, '&Redraw', 'Redraw plots')
|
||||
wx.EVT_MENU(self,211,self.OnPlotRedraw)
|
||||
menu.Append(212, '&Clear', 'Clear canvas')
|
||||
wx.EVT_MENU(self,212,self.OnPlotClear)
|
||||
self.mainmenu.Append(menu, '&Plot')
|
||||
|
||||
menu = wx.wxMenu()
|
||||
menu.Append(220, '&About', 'About this thing...')
|
||||
wx.EVT_MENU(self, 220, self.OnHelpAbout)
|
||||
self.mainmenu.Append(menu, '&Help')
|
||||
|
||||
self.SetMenuBar(self.mainmenu)
|
||||
|
||||
# A status bar to tell people what's happening
|
||||
self.CreateStatusBar(1)
|
||||
|
||||
self.client = PlotCanvas(self)
|
||||
|
||||
def OnFilePrint(self, event):
|
||||
d = wx.wxMessageDialog(self,
|
||||
"""As of this writing, printing support in wxPython is shaky at best.
|
||||
Are you sure you want to do this?""", "Danger!", wx.wxYES_NO)
|
||||
if d.ShowModal() == wx.wxID_YES:
|
||||
psdc = wx.wxPostScriptDC("out.ps", wx.TRUE, self)
|
||||
self.client.redraw(psdc)
|
||||
|
||||
def OnFileExit(self, event):
|
||||
self.Close()
|
||||
|
||||
def OnPlotDraw(self, event):
|
||||
self.client.draw(_InitObjects(),'automatic','automatic');
|
||||
|
||||
def OnPlotRedraw(self,event):
|
||||
self.client.redraw()
|
||||
|
||||
def OnPlotClear(self,event):
|
||||
self.client.last_draw = None
|
||||
dc = wx.wxClientDC(self.client)
|
||||
dc.Clear()
|
||||
|
||||
def OnHelpAbout(self, event):
|
||||
about = wx.wxMessageDialog(self, __doc__, "About...", wx.wxOK)
|
||||
about.ShowModal()
|
||||
|
||||
def OnCloseWindow(self, event):
|
||||
self.Destroy()
|
||||
|
||||
|
||||
class MyApp(wx.wxApp):
|
||||
def OnInit(self):
|
||||
frame = AppFrame(wx.NULL, -1, "wxPlotCanvas")
|
||||
frame.Show(wx.TRUE)
|
||||
self.SetTopWindow(frame)
|
||||
return wx.TRUE
|
||||
|
||||
|
||||
app = MyApp(0)
|
||||
app.MainLoop()
|
||||
|
||||
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
Reference in New Issue
Block a user