/[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 2895 - (show annotations) (download) (as text)
Sun Apr 12 14:32:00 2015 UTC (4 years ago) by adrian
File MIME type: text/x-python
File size: 7235 byte(s)
Removed unecessary imports
1 from gaphas.tool import Tool
2 import gi
3
4 gi.require_version('Gtk', '3.0')
5 from gi.repository import Gtk
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 != 3:
25 context.ungrab(self.view.tool)
26 return False
27
28 menu = Gtk.Menu()
29 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 menurename = Gtk.MenuItem("Re_name",True)
36 window = context.view.parent.parent.parent.parent.parent
37 menurename.connect("activate",self.rename,context.view.hovered_item,window)
38 menu.add(menurename)
39 menudefault.set_sensitive(False)
40 '''menublockstreams.set_sensitive(False)'''
41
42 menudelete = Gtk.MenuItem("_Delete",True)
43 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
44 menu.add(menudelete)
45
46 menu.add(Gtk.SeparatorMenuItem())
47
48 menurotate_clock = Gtk.MenuItem("_Rotate_clockwise",True)
49 window = context.view.parent.parent.parent.parent.parent
50 menurotate_clock.connect("activate",self.blockrotate_clock,context.view.hovered_item,window)
51 menu.add(menurotate_clock)
52
53 menurotate_anti = Gtk.MenuItem("_Rotate_anti_clockwise",True)
54 window = context.view.parent.parent.parent.parent.parent
55 menurotate_anti.connect("activate",self.blockrotate_anti,context.view.hovered_item,window)
56 menu.add(menurotate_anti)
57
58 menuflip = Gtk.MenuItem("Flip",True)
59 window = context.view.parent.parent.parent.parent.parent
60 menuflip.connect("activate",self.blockflip,context.view.hovered_item,window)
61 menu.add(menuflip)
62
63 menu.add(Gtk.SeparatorMenuItem())
64
65 menublockproperties = Gtk.MenuItem("_Properties")
66 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 0)
67 menu.add(menublockproperties)
68
69 menublockparams = Gtk.MenuItem("_Parameters")
70 menublockparams.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 1)
71 menu.add(menublockparams)
72
73 menudefault = Gtk.MenuItem("_Set Default Values")
74 menudefault.connect("activate",self.defaultvalues,window ,context,context.view.hovered_item)
75 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.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 3)
83 menu.add(menublockinstance)
84 '''
85 menublockstreams = Gtk.MenuItem("_Streams")
86 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
87 menu.add(menublockstreams)
88 '''
89 #menuinfo = Gtk.MenuItem("_Info",True)
90 #menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
91 #menu.add(menuinfo)
92
93 menu.add(Gtk.SeparatorMenuItem())
94
95 menucanvas = Gtk.MenuItem("_Canvas Properties",True)
96 menucanvas.connect("activate",self.canvasproperties,window ,context)
97 menu.add(menucanvas)
98
99 if not context.view.hovered_item:
100 menurename.set_sensitive(False)
101 #menuinfo.set_sensitive(False)
102 menudelete.set_sensitive(False)
103 menurotate_clock.set_sensitive(False)
104 menurotate_anti.set_sensitive(False)
105 menuflip.set_sensitive(False)
106 menublockproperties.set_sensitive(False)
107 menublockinstance.set_sensitive(False)
108 #menublockmethod.set_sensitive(False)
109 menublockparams.set_sensitive(False)
110 menudefault.set_sensitive(False)
111 '''menublockstreams.set_sensitive(False)'''
112
113
114 if not hasattr(context.view.hovered_item,'blockinstance'):
115 menurename.set_sensitive(False)
116 #menuinfo.set_sensitive(False)
117
118 if context.view.hovered_item:
119 if not context.view.hovered_item.blockinstance.instance:
120 menublockinstance.set_sensitive(False)
121
122 menu.show_all()
123 menu.popup( None, None, None, event.button, event.time)
124 #self.view.tool.ungrab(self.view.tool)
125 return False
126
127 #def on_button_release(self, event):
128 #print event
129 #self.view.hovered_item = None
130 #return False
131
132 def on_double_click(self,event):
133 context = self.view.tool
134 if event.button != 1 or not context.view.hovered_item:
135 context.ungrab(self.view.tool)
136 return False
137
138 window = context.view.parent.parent.parent.parent.parent
139 self.blockproperties(None , window, context, context.view.hovered_item)
140
141 def deactivate(self,widget,context):
142 #print 'Hovered Item Set To None'
143 context.view.hovered_item = None
144
145 def rename(self,widget,item,window):
146 if hasattr(item,'blockinstance'):
147 bi = item.blockinstance
148 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))
149 dia.set_default_response(Gtk.ResponseType.OK)
150 ent = Gtk.Entry()
151 ent.set_text(bi.name)
152 def key_press(ent,event):
153 key = Gdk.keyval_name(event.keyval)
154 if key == 'Return':
155 dia.response(Gtk.ResponseType.OK)
156 return True
157 return False
158 ent.connect("key-press-event",key_press)
159 dia.vbox.add(ent)
160 dia.show_all()
161 res = dia.run()
162 if res == Gtk.ResponseType.OK:
163 bi.name = ent.get_text()
164 dia.destroy()
165
166 @undo.block_observed
167 def delete(self,widget,item,view):
168 view.canvas.remove(item)
169
170 def blockproperties(self, widget = None, window = None, context = None, item = None, tab = None):
171 blockproperties.BlockProperties(window, item, tab = tab).run()
172
173 def blockrotate_clock(self, widget, item, window):
174 ''' handler for rotating an item '''
175 item.rotate_clock()
176
177 def blockrotate_anti(self,widget, item, window):
178 item.rotate_anti()
179
180 def blockflip(self,widget, item, window):
181 item.flip()
182
183 def canvasproperties(self, widget, window, context):
184 canvasproperties.CanvasProperties(window).run()
185 '''
186 def setstream(self, widget, window, context, item):
187 print window.ascwrap.streams
188 '''
189 def defaultvalues(self,widget,window,context,item):
190 print widget,window,context,item.blockinstance
191
192 model = str(self.view.canvas)
193 #print model
194 self.view.canvas.saved_model = model
195 self.view.canvas.saved_data = {}
196
197 window.ascwrap.library.loadString(model,"canvasmodel")
198 t = window.ascwrap.library.findType(str(item.blockinstance.blocktype.type.getName()));
199 try:
200 m =t.getMethod('default_self');
201 except:
202 return
203 i = t.getSimulation('sim',False);
204 i.build()
205 i.run(m);
206 fv = i.getFixedVariables();
207 for i in fv:
208 for param in item.blockinstance.params:
209 if param == i.getName():
210 item.blockinstance.params[param].value = i.getValue();
211 item.blockinstance.params[param].fix = True

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