/[ascend]/branches/adrian/pygtk/canvas/contextmenutool.py
ViewVC logotype

Contents of /branches/adrian/pygtk/canvas/contextmenutool.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2822 - (show annotations) (download) (as text)
Tue Feb 17 06:36:00 2015 UTC (4 years, 2 months ago) by jpye
Original Path: branches/pallav/pygtk/canvas/contextmenutool.py
File MIME type: text/x-python
File size: 7291 byte(s)
restoring branches...

1 import gaphas
2 from gaphas.tool import Tool
3 import pygtk
4 import math
5
6 pygtk.require('2.0')
7 import gtk
8 import blockinstance
9 import blockproperties
10 import canvasproperties
11 import undo
12 from blockitem import CustomBlockItem_turbine
13
14 import ascpy
15
16 class ContextMenuTool(Tool):
17 """
18 Context menu for blocks and connectors on the canvas, intended to be
19 the main mouse-based way by which interaction with blocks occurs (blocks
20 renamed, parameters specified, values examined, etc).
21 Code for performing these tasks should not be here; this should just
22 hook into the appropriate code in the Application layer.
23 """
24 def __init__(self,view=None):
25 super(ContextMenuTool, self).__init__(view)
26
27 def on_button_press(self, event):
28 context = self.view.tool
29 if event.button != 3:
30 context.ungrab(self.view.tool)
31 return False
32
33 menu = gtk.Menu()
34 menu.connect("deactivate",self.deactivate,context)
35 '''
36 menublockstreams = gtk.MenuItem("_Streams")
37 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
38 menu.add(menublockstreams)
39 '''
40 menurename = gtk.MenuItem("Re_name",True)
41 window = context.view.parent.parent.parent.parent.parent
42 menurename.connect("activate",self.rename,context.view.hovered_item,window)
43 menu.add(menurename)
44 menudefault.set_sensitive(False)
45 '''menublockstreams.set_sensitive(False)'''
46
47 menudelete = gtk.MenuItem("_Delete",True)
48 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
49 menu.add(menudelete)
50
51 menu.add(gtk.SeparatorMenuItem())
52
53 menurotate_clock = gtk.MenuItem("_Rotate_clockwise",True)
54 window = context.view.parent.parent.parent.parent.parent
55 menurotate_clock.connect("activate",self.blockrotate_clock,context.view.hovered_item,window)
56 menu.add(menurotate_clock)
57
58 menurotate_anti = gtk.MenuItem("_Rotate_anti_clockwise",True)
59 window = context.view.parent.parent.parent.parent.parent
60 menurotate_anti.connect("activate",self.blockrotate_anti,context.view.hovered_item,window)
61 menu.add(menurotate_anti)
62
63 menuflip = gtk.MenuItem("Flip",True)
64 window = context.view.parent.parent.parent.parent.parent
65 menuflip.connect("activate",self.blockflip,context.view.hovered_item,window)
66 menu.add(menuflip)
67
68 menu.add(gtk.SeparatorMenuItem())
69
70 menublockproperties = gtk.MenuItem("_Properties")
71 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 0)
72 menu.add(menublockproperties)
73
74 menublockparams = gtk.MenuItem("_Parameters")
75 menublockparams.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 1)
76 menu.add(menublockparams)
77
78 menudefault = gtk.MenuItem("_Set Default Values")
79 menudefault.connect("activate",self.defaultvalues,window ,context,context.view.hovered_item)
80 menu.add(menudefault)
81
82 #menublockmethod = gtk.MenuItem("_Custom Method(s)")
83 #menublockmethod.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 2)
84 #menu.add(menublockmethod)
85
86 menublockinstance = gtk.MenuItem("_Instance")
87 menublockinstance.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 3)
88 menu.add(menublockinstance)
89 '''
90 menublockstreams = gtk.MenuItem("_Streams")
91 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
92 menu.add(menublockstreams)
93 '''
94 #menuinfo = gtk.MenuItem("_Info",True)
95 #menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
96 #menu.add(menuinfo)
97
98 menu.add(gtk.SeparatorMenuItem())
99
100 menucanvas = gtk.MenuItem("_Canvas Properties",True)
101 menucanvas.connect("activate",self.canvasproperties,window ,context)
102 menu.add(menucanvas)
103
104 if not context.view.hovered_item:
105 menurename.set_sensitive(False)
106 #menuinfo.set_sensitive(False)
107 menudelete.set_sensitive(False)
108 menurotate_clock.set_sensitive(False)
109 menurotate_anti.set_sensitive(False)
110 menuflip.set_sensitive(False)
111 menublockproperties.set_sensitive(False)
112 menublockinstance.set_sensitive(False)
113 #menublockmethod.set_sensitive(False)
114 menublockparams.set_sensitive(False)
115 menudefault.set_sensitive(False)
116 '''menublockstreams.set_sensitive(False)'''
117
118
119 if not hasattr(context.view.hovered_item,'blockinstance'):
120 menurename.set_sensitive(False)
121 #menuinfo.set_sensitive(False)
122
123 if context.view.hovered_item:
124 if not context.view.hovered_item.blockinstance.instance:
125 menublockinstance.set_sensitive(False)
126
127 menu.show_all()
128 menu.popup( None, None, None, event.button, event.time)
129 #self.view.tool.ungrab(self.view.tool)
130 return False
131
132 #def on_button_release(self, event):
133 #print event
134 #self.view.hovered_item = None
135 #return False
136
137 def on_double_click(self,event):
138 context = self.view.tool
139 if event.button != 1 or not context.view.hovered_item:
140 context.ungrab(self.view.tool)
141 return False
142
143 window = context.view.parent.parent.parent.parent.parent
144 self.blockproperties(None , window, context, context.view.hovered_item)
145
146 def deactivate(self,widget,context):
147 #print 'Hovered Item Set To None'
148 context.view.hovered_item = None
149
150 def rename(self,widget,item,window):
151 if hasattr(item,'blockinstance'):
152 bi = item.blockinstance
153 dia = gtk.Dialog("Rename %s '%s'" % (bi.blocktype.type.getName(),bi.name), window,gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,(gtk.STOCK_CANCEL, 0, gtk.STOCK_OK, gtk.RESPONSE_OK))
154 dia.set_default_response(gtk.RESPONSE_OK)
155 ent = gtk.Entry()
156 ent.set_text(bi.name)
157 def key_press(ent,event):
158 key = gtk.gdk.keyval_name(event.keyval)
159 if key == 'Return':
160 dia.response(gtk.RESPONSE_OK)
161 return True
162 return False
163 ent.connect("key-press-event",key_press)
164 dia.vbox.add(ent)
165 dia.show_all()
166 res = dia.run()
167 if res == gtk.RESPONSE_OK:
168 bi.name = ent.get_text()
169 dia.destroy()
170
171 @undo.block_observed
172 def delete(self,widget,item,view):
173 view.canvas.remove(item)
174
175 def blockproperties(self, widget = None, window = None, context = None, item = None, tab = None):
176 blockproperties.BlockProperties(window, item, tab = tab).run()
177
178 def blockrotate_clock(self, widget, item, window):
179 ''' handler for rotating an item '''
180 item.rotate_clock()
181
182 def blockrotate_anti(self,widget, item, window):
183 item.rotate_anti()
184
185 def blockflip(self,widget, item, window):
186 item.flip()
187
188 def canvasproperties(self, widget, window, context):
189 canvasproperties.CanvasProperties(window).run()
190 '''
191 def setstream(self, widget, window, context, item):
192 print window.ascwrap.streams
193 '''
194 def defaultvalues(self,widget,window,context,item):
195 print widget,window,context,item.blockinstance
196
197 model = str(self.view.canvas)
198 #print model
199 self.view.canvas.saved_model = model
200 self.view.canvas.saved_data = {}
201
202 window.ascwrap.library.loadString(model,"canvasmodel")
203 t = window.ascwrap.library.findType(str(item.blockinstance.blocktype.type.getName()));
204 try:
205 m =t.getMethod('default_self');
206 except:
207 return
208 i = t.getSimulation('sim',False);
209 i.build()
210 i.run(m);
211 fv = i.getFixedVariables();
212 for i in fv:
213 for param in item.blockinstance.params:
214 if param == i.getName():
215 item.blockinstance.params[param].value = i.getValue();
216 item.blockinstance.params[param].fix = True

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