/[ascend]/trunk/pygtk/canvas/blocklist.py
ViewVC logotype

Contents of /trunk/pygtk/canvas/blocklist.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1951 - (show annotations) (download) (as text)
Mon Nov 10 02:09:23 2008 UTC (11 years, 9 months ago) by jpye
File MIME type: text/x-python
File size: 6830 byte(s)
Removed threading calls from blocklist.py, seems to 
get things working on Windows.
1 #!/usr/bin/env python
2 from __future__ import with_statement
3 import os, sys
4
5 os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
6
7 os.environ['ASCENDLIBRARY'] = "../../models"
8
9 if sys.platform.startswith("win"):
10 os.environ['PATH'] += ";..\.."
11 else:
12 os.environ['LD_LIBRARY_PATH'] = "../.."
13
14 sys.path.append("..")
15
16 if sys.platform.startswith("win"):
17 # Fetchs gtk2 path from registry
18 import _winreg
19 import msvcrt
20 try:
21 k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "Software\\GTK\\2.0")
22 except EnvironmentError:
23 # use TkInter to report the error :-)
24 from TkInter import *
25 root = Tk()
26 w = Label(root,"You must install the Gtk+ 2.2 Runtime Environment to run this program")
27 w.pack()
28 root.mainloop()
29 sys.exit(1)
30 else:
31 gtkdir = _winreg.QueryValueEx(k, "Path")
32 import os
33 # we must make sure the gtk2 path is the first thing in the path
34 # otherwise, we can get errors if the system finds other libs with
35 # the same name in the path...
36 os.environ['PATH'] = "%s/lib;%s/bin;" % (gtkdir[0], gtkdir[0]) + os.environ['PATH']
37
38 import ascpy
39
40 L = ascpy.Library()
41
42 # FIXME need to add way to add/remove modules from the Library?
43 L.load('test/canvas/blocktypes.a4c')
44
45 D = L.getAnnotationDatabase()
46
47 M = L.getModules()
48
49 blocktypes = set()
50
51 for m in M:
52 T = L.getModuleTypes(m)
53 for t in T:
54 # 'block' types must not be parametric, because they must be able to
55 # exist even without being connected, and parametric models impose
56 # restrictions on the use of ARE_THE_SAME and similar.
57 if t.hasParameters():
58 continue
59 x = D.getNotes(t,ascpy.SymChar("block"),ascpy.SymChar("SELF"))
60 if x:
61 blocktypes.add(t)
62
63 blocks = []
64
65 from blocktype import *
66
67 print "block types:"
68 if not blocktypes:
69 print "NONE FOUND"
70 for t in blocktypes:
71
72 b = BlockType(t,D)
73
74 blocks += [b]
75
76 # render icon table
77 import threading
78 import gtk
79 import os, os.path, re
80
81 import cairo
82
83 #gtk.gdk.threads_init()
84
85 class BlockIconView(gtk.IconView):
86 """
87 IconView containing the palette of BlockTypes available for use in the
88 canvas. The list of blocks is supplied currently as an initialisation
89 parameter, but it is intended that this would be dynamic in a final system.
90
91 It should be possible drag icons from the palette into the canvas, but
92 that is not yet implemented.
93 """
94 def __init__(self,blocks,app):
95 # the mode containing the icons themselves...
96 self.model = gtk.ListStore(str, gtk.gdk.Pixbuf)
97 self.app = app
98 self.otank = {}
99 #thread = threading.RLock()
100 n = 0
101 #with thread:
102 for b in blocks:
103 n += 1
104 pixbuf = b.get_icon(64,64)
105 iter = self.model.append([b.type.getName(), pixbuf])
106 path = self.model.get_path(iter)
107 self.otank[path] = b
108
109 gtk.IconView.__init__(self)
110 self.set_model(self.model)
111 self.set_text_column(0)
112 self.set_pixbuf_column(1)
113 self.set_columns(-1)
114 self.set_size_request(180,100)
115 self.connect("item-activated", self.item_activated)
116 self.connect("selection-changed", self.selection_changed)
117
118 def selection_changed(self,iconview):
119 s = self.get_selected_items()
120 if len(s)==1:
121 b = self.otank[s[0]]
122 self.app.set_placement_tool(b)
123
124 def item_activated(self,iconview, path):
125 self.app.set_placement_tool(self.otank[path])
126
127 from gaphas import GtkView, View
128 from gaphas.tool import HoverTool, PlacementTool, HandleTool, ToolChain
129 from gaphas.tool import Tool, ItemTool, RubberbandTool
130 from gaphas.item import Line
131 from blockitem import *
132 from contextmenutool import *
133 from connectortool import *
134 from portconnectinghandletool import *
135 from blockcanvas import *
136 from panzoom import *
137 from blockinstance import *
138
139 def BlockToolChain():
140 """
141 The default tool chain build from HoverTool, ItemTool and HandleTool.
142 """
143 chain = ToolChain()
144 chain.append(HoverTool())
145 chain.append(PortConnectingHandleTool())
146 chain.append(ConnectorTool())
147 chain.append(ContextMenuTool())
148 chain.append(ItemTool())
149 chain.append(ZoomTool())
150 chain.append(PanTool())
151 chain.append(RubberbandTool())
152 return chain
153
154 class app(gtk.Window):
155 def __init__(self):
156 self.status = gtk.Statusbar()
157
158 # the Gaphas canvas
159 canvas = BlockCanvas()
160
161 # the main window
162 gtk.Window.__init__(self)
163 self.set_title("ASCEND Blocks")
164 self.set_default_size(400, 500)
165 self.connect("destroy", gtk.main_quit)
166 self.connect("key-press-event", self.key_press_event)
167
168 windowicon = gtk.Image()
169 windowicon.set_from_file(os.path.join("../glade/ascend.svg"))
170 self.set_icon(windowicon.get_pixbuf())
171
172 # vbox containing the main view and the status bar at the bottom
173 vbox = gtk.VBox()
174
175 # hbox occupies top part of vbox, with icons on left & canvas on right.
176 hbox = gtk.HBox()
177
178 # the 'view' widget implemented by Gaphas
179 self.view = GtkView()
180 self.view.tool = BlockToolChain()
181
182 # table containing scrollbars and main canvas
183 t = gtk.Table(2,2)
184 self.view.canvas = canvas
185 self.view.zoom(1)
186 self.view.set_size_request(600, 500)
187 hs = gtk.HScrollbar(self.view.hadjustment)
188 vs = gtk.VScrollbar(self.view.vadjustment)
189 t.attach(self.view, 0, 1, 0, 1)
190 t.attach(hs, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL)
191 t.attach(vs, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL)
192
193 # a scrolling window to contain the icon palette
194 scroll = gtk.ScrolledWindow()
195 scroll.set_border_width(2)
196 scroll.set_shadow_type(gtk.SHADOW_ETCHED_IN)
197 scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
198
199 # icon palette
200 self.blockiconview = BlockIconView(blocks, self)
201 scroll.add(self.blockiconview)
202
203 hbox.pack_start(scroll, True, True)
204 hbox.pack_start(t, True, True)
205 vbox.pack_start(hbox, True, True)
206 vbox.pack_start(self.status, False, False)
207 self.add(vbox)
208 self.show_all()
209
210 # a message about the found blocks
211 self.status.push(0, "Found %d block types." % (len(blocks)))
212
213 def set_placement_tool(self,blocktype):
214 # TODO: add undo handler
215 label = blocktype.type.getName()
216 def my_block_factory():
217 def wrapper():
218 b = BlockInstance(blocktype)
219 bi = DefaultBlockItem(b)
220 self.view.canvas.add(bi)
221 return bi
222 return wrapper
223 self.view.tool.grab(PlacementTool(my_block_factory(), HandleTool(), 2))
224 self.status.push(0,"Selected '%s'..." % blocktype.type.getName())
225
226 def set_connector_tool(self):
227 def my_line_factory():
228 def wrapper():
229 l = Line()
230 self.view.canvas.add(l)
231 return l
232 return wrapper
233 self.view.tool.grab(PlacementTool(my_line_factory(), HandleTool(), 1))
234
235 def key_press_event(self,widget,event):
236 # TODO: add undo handler
237 key = gtk.gdk.keyval_name(event.keyval)
238 if key == 'Delete' and self.view.focused_item:
239 self.view.canvas.remove(self.view.focused_item)
240 self.status.push(0,"Item deleted.")
241 elif key == 'l' or key == 'L':
242 self.set_connector_tool()
243 self.status.push(0,"Line draw mode...")
244
245 a = app()
246 gtk.main()
247

Properties

Name Value
svn:executable *

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