/[ascend]/trunk/pygtk/canvas/gaphas-test.py
ViewVC logotype

Contents of /trunk/pygtk/canvas/gaphas-test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1785 - (show annotations) (download) (as text)
Sun Jun 1 03:41:26 2008 UTC (16 years ago) by jpye
File MIME type: text/x-python
File size: 3071 byte(s)
Removed 'Box' stuff from test code.
Rewrote PointConstraint to ignore the 'var' arg of 'solve_for'.
1 import pygtk
2 pygtk.require('2.0')
3
4 import math
5 import gtk
6 import cairo
7 from gaphas import Canvas, GtkView, View
8 from gaphas.item import Line, SW, NE, NW, SE, Element, Handle
9 from gaphas.tool import HoverTool, PlacementTool, HandleTool, ToolChain
10 from gaphas.tool import ItemTool, RubberbandTool
11 from gaphas.geometry import point_on_rectangle, distance_rectangle_point
12 from gaphas.constraint import LineConstraint, LessThanConstraint, EqualsConstraint
13 from gaphas.canvas import CanvasProjection
14
15 from gaphas.painter import ItemPainter
16 from gaphas import state
17 from gaphas.util import text_extents
18
19 from gaphas import painter
20 #painter.DEBUG_DRAW_BOUNDING_BOX = True
21
22 from port import *
23
24 def factory(view, cls):
25 """
26 Simple canvas item factory.
27 """
28 def wrapper():
29 item = cls()
30 view.canvas.add(item)
31 return item
32 return wrapper
33
34
35 def create_window(canvas, title, zoom=1.0):
36 view = GtkView()
37 view.tool = DefaultExampleTool()
38
39 w = gtk.Window()
40 w.set_title(title)
41 #w.set_default_size(800,600)
42 h = gtk.HBox()
43 w.add(h)
44
45 # VBox contains buttons that can be used to manipulate the canvas:
46 v = gtk.VBox()
47 v.set_property('border-width', 3)
48 v.set_property('spacing', 2)
49 f = gtk.Frame()
50 f.set_property('border-width', 1)
51 f.add(v)
52 h.pack_start(f, expand=False)
53
54 b = gtk.Button('Add block')
55
56 def on_clicked(button, view):
57 #view.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.CROSSHAIR))
58 view.tool.grab(PlacementTool(factory(view, DefaultBlock), HandleTool(), 2))
59
60 b.connect('clicked', on_clicked, view)
61 v.add(b)
62
63 b = gtk.Button('Add line')
64
65 def on_clicked(button):
66 view.tool.grab(PlacementTool(factory(view, Line), HandleTool(), 1))
67
68 b.connect('clicked', on_clicked)
69 v.add(b)
70
71
72 b = gtk.Button('Delete focused')
73
74 def on_clicked(button):
75 if view.focused_item:
76 canvas.remove(view.focused_item)
77 print 'items:', canvas.get_all_items()
78
79 b.connect('clicked', on_clicked)
80 v.add(b)
81
82 # Add the actual View:
83
84 t = gtk.Table(2,2)
85 h.add(t)
86
87 w.connect('destroy', gtk.main_quit)
88
89 view.canvas = canvas
90 view.zoom(zoom)
91 view.set_size_request(800, 600)
92 hs = gtk.HScrollbar(view.hadjustment)
93 vs = gtk.VScrollbar(view.vadjustment)
94 t.attach(view, 0, 1, 0, 1)
95 t.attach(hs, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL)
96 t.attach(vs, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL)
97
98 w.show_all()
99
100 def handle_changed(view, item, what):
101 print what, 'changed: ', item
102
103 view.connect('focus-changed', handle_changed, 'focus')
104 view.connect('hover-changed', handle_changed, 'hover')
105 view.connect('selection-changed', handle_changed, 'selection')
106
107 def main():
108 c=Canvas()
109
110 create_window(c, 'ASCEND model canvas')
111
112 # Add stuff to the canvas:
113
114 b=DefaultBlock(inputs=1,outputs=1)
115 b.width = 100
116 b.height = 100
117 c.add(b)
118
119 # run
120 gtk.main()
121
122 if __name__ == '__main__':
123 main()
124
125 # vim: sw=4:et:ai
126

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22