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

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