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

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