/[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 2628 - (show annotations) (download) (as text)
Wed May 16 20:40:56 2012 UTC (6 years, 11 months ago) by grivan
Original Path: trunk/pygtk/canvas/contextmenutool.py
File MIME type: text/x-python
File size: 5926 byte(s)
Merge changes from grivan2 branch (r3261-4126) to trunk, so that Saheb can use it.
1
2 from gaphas.tool import Tool
3 import pygtk
4 pygtk.require('2.0')
5 import gtk
6 import blockinstance
7 import blockproperties
8 import canvasproperties
9 import undo
10
11 import ascpy
12
13 class ContextMenuTool(Tool):
14 """
15 Context menu for blocks and connectors on the canvas, intended to be
16 the main mouse-based way by which interaction with blocks occurs (blocks
17 renamed, parameters specified, values examined, etc).
18 Code for performing these tasks should not be here; this should just
19 hook into the appropriate code in the Application layer.
20 """
21 def __init__(self,view=None):
22 super(ContextMenuTool, self).__init__(view)
23
24 def on_button_press(self, event):
25 context = self.view.tool
26 if event.button != 3:
27 context.ungrab(self.view.tool)
28 return False
29
30 menu = gtk.Menu()
31 menu.connect("deactivate",self.deactivate,context)
32
33 menurename = gtk.MenuItem("Re_name",True)
34 window = context.view.parent.parent.parent.parent.parent
35 menurename.connect("activate",self.rename,context.view.hovered_item,window)
36 menu.add(menurename)
37
38 menudelete = gtk.MenuItem("_Delete",True)
39 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
40 menu.add(menudelete)
41
42 menu.add(gtk.SeparatorMenuItem())
43
44 menublockproperties = gtk.MenuItem("_Properties")
45 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 0)
46 menu.add(menublockproperties)
47
48 menublockparams = gtk.MenuItem("_Parameters")
49 menublockparams.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 1)
50 menu.add(menublockparams)
51
52 menudefault = gtk.MenuItem("_Set Default Values")
53 menudefault.connect("activate",self.defaultvalues,window ,context,context.view.hovered_item)
54 menu.add(menudefault)
55
56 #menublockmethod = gtk.MenuItem("_Custom Method(s)")
57 #menublockmethod.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 2)
58 #menu.add(menublockmethod)
59
60 menublockinstance = gtk.MenuItem("_Instance")
61 menublockinstance.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 3)
62 menu.add(menublockinstance)
63 '''
64 menublockstreams = gtk.MenuItem("_Streams")
65 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
66 menu.add(menublockstreams)
67 '''
68 #menuinfo = gtk.MenuItem("_Info",True)
69 #menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
70 #menu.add(menuinfo)
71
72 menu.add(gtk.SeparatorMenuItem())
73
74 menucanvas = gtk.MenuItem("_Canvas Properties",True)
75 menucanvas.connect("activate",self.canvasproperties,window ,context)
76 menu.add(menucanvas)
77
78 if not context.view.hovered_item:
79 menurename.set_sensitive(False)
80 #menuinfo.set_sensitive(False)
81 menudelete.set_sensitive(False)
82 menublockproperties.set_sensitive(False)
83 menublockinstance.set_sensitive(False)
84 #menublockmethod.set_sensitive(False)
85 menublockparams.set_sensitive(False)
86 menudefault.set_sensitive(False)
87 '''menublockstreams.set_sensitive(False)'''
88
89
90 if not hasattr(context.view.hovered_item,'blockinstance'):
91 menurename.set_sensitive(False)
92 #menuinfo.set_sensitive(False)
93
94 if context.view.hovered_item:
95 if not context.view.hovered_item.blockinstance.instance:
96 menublockinstance.set_sensitive(False)
97
98 menu.show_all()
99 menu.popup( None, None, None, event.button, event.time)
100 #self.view.tool.ungrab(self.view.tool)
101 return False
102
103 #def on_button_release(self, event):
104 #print event
105 #self.view.hovered_item = None
106 #return False
107
108 def on_double_click(self,event):
109 context = self.view.tool
110 if event.button != 1 or not context.view.hovered_item:
111 context.ungrab(self.view.tool)
112 return False
113
114 window = context.view.parent.parent.parent.parent.parent
115 self.blockproperties(None , window, context, context.view.hovered_item)
116
117 def deactivate(self,widget,context):
118 #print 'Hovered Item Set To None'
119 context.view.hovered_item = None
120
121 def rename(self,widget,item,window):
122 if hasattr(item,'blockinstance'):
123 bi = item.blockinstance
124 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))
125 dia.set_default_response(gtk.RESPONSE_OK)
126 ent = gtk.Entry()
127 ent.set_text(bi.name)
128 def key_press(ent,event):
129 key = gtk.gdk.keyval_name(event.keyval)
130 if key == 'Return':
131 dia.response(gtk.RESPONSE_OK)
132 return True
133 return False
134 ent.connect("key-press-event",key_press)
135 dia.vbox.add(ent)
136 dia.show_all()
137 res = dia.run()
138 if res == gtk.RESPONSE_OK:
139 bi.name = ent.get_text()
140 dia.destroy()
141
142 @undo.block_observed
143 def delete(self,widget,item,view):
144 view.canvas.remove(item)
145
146 def blockproperties(self, widget = None, window = None, context = None, item = None, tab = None):
147 blockproperties.BlockProperties(window, item, tab = tab).run()
148
149 def canvasproperties(self, widget, window, context):
150 canvasproperties.CanvasProperties(window).run()
151 '''
152 def setstream(self, widget, window, context, item):
153 print window.ascwrap.streams
154 '''
155 def defaultvalues(self,widget,window,context,item):
156 print widget,window,context,item.blockinstance
157
158 model = str(self.view.canvas)
159 #print model
160 self.view.canvas.saved_model = model
161 self.view.canvas.saved_data = {}
162
163 window.ascwrap.library.loadString(model,"canvasmodel")
164 t = window.ascwrap.library.findType(str(item.blockinstance.blocktype.type.getName()));
165 try:
166 m =t.getMethod('default_self');
167 except:
168 return
169 i = t.getSimulation('sim',False);
170 i.build()
171 i.run(m);
172 fv = i.getFixedVariables();
173 for i in fv:
174 for param in item.blockinstance.params:
175 if param == i.getName():
176 item.blockinstance.params[param].value = i.getValue();
177 item.blockinstance.params[param].fix = True

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