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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1951 - (hide annotations) (download) (as text)
Mon Nov 10 02:09:23 2008 UTC (11 years, 10 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 jpye 1945 #!/usr/bin/env python
2 jpye 1935 from __future__ import with_statement
3 jpye 1937 import os, sys
4 jpye 1929
5 jpye 1940 os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
6 jpye 1951
7 jpye 1937 os.environ['ASCENDLIBRARY'] = "../../models"
8 jpye 1951
9     if sys.platform.startswith("win"):
10     os.environ['PATH'] += ";..\.."
11     else:
12     os.environ['LD_LIBRARY_PATH'] = "../.."
13    
14 jpye 1937 sys.path.append("..")
15    
16 jpye 1929 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 jpye 1801 import ascpy
39    
40     L = ascpy.Library()
41    
42 jpye 1929 # FIXME need to add way to add/remove modules from the Library?
43     L.load('test/canvas/blocktypes.a4c')
44 jpye 1801
45     D = L.getAnnotationDatabase()
46    
47     M = L.getModules()
48    
49     blocktypes = set()
50    
51 jpye 1813 for m in M:
52     T = L.getModuleTypes(m)
53     for t in T:
54 jpye 1925 # '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 jpye 1893 continue
59 jpye 1925 x = D.getNotes(t,ascpy.SymChar("block"),ascpy.SymChar("SELF"))
60     if x:
61 jpye 1813 blocktypes.add(t)
62    
63 jpye 1935 blocks = []
64    
65 jpye 1945 from blocktype import *
66    
67 jpye 1813 print "block types:"
68     if not blocktypes:
69     print "NONE FOUND"
70     for t in blocktypes:
71    
72 jpye 1945 b = BlockType(t,D)
73 jpye 1926
74 jpye 1935 blocks += [b]
75 jpye 1925
76 jpye 1935 # render icon table
77     import threading
78     import gtk
79     import os, os.path, re
80 jpye 1936
81     import cairo
82    
83 jpye 1951 #gtk.gdk.threads_init()
84 jpye 1935
85 jpye 1937 class BlockIconView(gtk.IconView):
86 jpye 1940 """
87 jpye 1945 IconView containing the palette of BlockTypes available for use in the
88 jpye 1940 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 jpye 1937 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 jpye 1951 #thread = threading.RLock()
100 jpye 1937 n = 0
101 jpye 1951 #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 jpye 1937
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 jpye 1940 def item_activated(self,iconview, path):
125     self.app.set_placement_tool(self.otank[path])
126    
127 jpye 1945 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 jpye 1943 from connectortool import *
134 jpye 1945 from portconnectinghandletool import *
135     from blockcanvas import *
136     from panzoom import *
137     from blockinstance import *
138 jpye 1943
139 jpye 1939 def BlockToolChain():
140 jpye 1942 """
141     The default tool chain build from HoverTool, ItemTool and HandleTool.
142     """
143     chain = ToolChain()
144     chain.append(HoverTool())
145     chain.append(PortConnectingHandleTool())
146 jpye 1943 chain.append(ConnectorTool())
147 jpye 1942 chain.append(ContextMenuTool())
148     chain.append(ItemTool())
149     chain.append(ZoomTool())
150     chain.append(PanTool())
151     chain.append(RubberbandTool())
152     return chain
153 jpye 1939
154 jpye 1935 class app(gtk.Window):
155     def __init__(self):
156 jpye 1937 self.status = gtk.Statusbar()
157 jpye 1936
158 jpye 1937 # the Gaphas canvas
159 jpye 1936 canvas = BlockCanvas()
160    
161 jpye 1937 # the main window
162 jpye 1935 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 jpye 1938 self.connect("key-press-event", self.key_press_event)
167 jpye 1935
168 jpye 1943 windowicon = gtk.Image()
169     windowicon.set_from_file(os.path.join("../glade/ascend.svg"))
170     self.set_icon(windowicon.get_pixbuf())
171    
172 jpye 1937 # vbox containing the main view and the status bar at the bottom
173 jpye 1935 vbox = gtk.VBox()
174    
175 jpye 1937 # hbox occupies top part of vbox, with icons on left & canvas on right.
176 jpye 1936 hbox = gtk.HBox()
177    
178 jpye 1937 # the 'view' widget implemented by Gaphas
179     self.view = GtkView()
180 jpye 1939 self.view.tool = BlockToolChain()
181 jpye 1936
182 jpye 1937 # table containing scrollbars and main canvas
183 jpye 1936 t = gtk.Table(2,2)
184 jpye 1937 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 jpye 1936 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 jpye 1937 # 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 jpye 1936 hbox.pack_start(scroll, True, True)
204     hbox.pack_start(t, True, True)
205     vbox.pack_start(hbox, True, True)
206 jpye 1937 vbox.pack_start(self.status, False, False)
207 jpye 1935 self.add(vbox)
208     self.show_all()
209    
210 jpye 1937 # a message about the found blocks
211     self.status.push(0, "Found %d block types." % (len(blocks)))
212    
213 jpye 1945 def set_placement_tool(self,blocktype):
214 jpye 1938 # TODO: add undo handler
215 jpye 1945 label = blocktype.type.getName()
216 jpye 1937 def my_block_factory():
217     def wrapper():
218 jpye 1945 b = BlockInstance(blocktype)
219     bi = DefaultBlockItem(b)
220     self.view.canvas.add(bi)
221     return bi
222 jpye 1937 return wrapper
223     self.view.tool.grab(PlacementTool(my_block_factory(), HandleTool(), 2))
224 jpye 1945 self.status.push(0,"Selected '%s'..." % blocktype.type.getName())
225 jpye 1938
226 jpye 1940 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 jpye 1938 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 jpye 1940 elif key == 'l' or key == 'L':
242     self.set_connector_tool()
243     self.status.push(0,"Line draw mode...")
244 jpye 1935
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