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

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