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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2914 - (hide annotations) (download) (as text)
Fri Apr 17 22:08:28 2015 UTC (4 years ago) by adrian
File MIME type: text/x-python
File size: 7437 byte(s)
Fixed context menu tool issue
1 jpye 1945 from gaphas.tool import Tool
2 adrian 2891 import gi
3 saheb 2686
4 adrian 2891 gi.require_version('Gtk', '3.0')
5 adrian 2914 from gi.repository import Gtk, Gdk
6 arijit 2074 import blockproperties
7 jpye 2394 import canvasproperties
8     import undo
9 jpye 1945
10 grivan 2628
11 jpye 1945 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 jpye 2394 def __init__(self,view=None):
20     super(ContextMenuTool, self).__init__(view)
21 saheb 2686
22 grivan 2200 def on_button_press(self, event):
23     context = self.view.tool
24 adrian 2914 if event.button.button != 3:
25 grivan 2200 context.ungrab(self.view.tool)
26 jpye 1945 return False
27 saheb 2686
28 adrian 2914 self.menu = Gtk.Menu()
29     self.menu.connect("deactivate",self.deactivate,context)
30 saheb 2686 '''
31 adrian 2891 menublockstreams = Gtk.MenuItem("_Streams")
32 saheb 2686 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
33     menu.add(menublockstreams)
34     '''
35 adrian 2914 window = context.view.get_parent().get_parent().get_parent().get_parent().get_parent()
36 adrian 2891 menurename = Gtk.MenuItem("Re_name",True)
37 adrian 2914 menurename.set_use_underline(True)
38 jpye 2394 menurename.connect("activate",self.rename,context.view.hovered_item,window)
39 adrian 2914 self.menu.add(menurename)
40 saheb 2686 '''menublockstreams.set_sensitive(False)'''
41 arijit 2074
42 adrian 2891 menudelete = Gtk.MenuItem("_Delete",True)
43 jpye 2394 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
44 adrian 2914 menudelete.set_use_underline(True)
45     self.menu.add(menudelete)
46 jpye 1945
47 adrian 2914 self.menu.add(Gtk.SeparatorMenuItem())
48 saheb 2686
49 adrian 2914 menurotate_clock = Gtk.MenuItem("Rotate clockwise",True)
50 saheb 2686 menurotate_clock.connect("activate",self.blockrotate_clock,context.view.hovered_item,window)
51 adrian 2914 self.menu.add(menurotate_clock)
52 saheb 2686
53 adrian 2914 menurotate_anti = Gtk.MenuItem("Rotate counterclockwise",True)
54 saheb 2686 menurotate_anti.connect("activate",self.blockrotate_anti,context.view.hovered_item,window)
55 adrian 2914 self.menu.add(menurotate_anti)
56 saheb 2686
57 adrian 2891 menuflip = Gtk.MenuItem("Flip",True)
58 saheb 2686 menuflip.connect("activate",self.blockflip,context.view.hovered_item,window)
59 adrian 2914 self.menu.add(menuflip)
60 saheb 2686
61 adrian 2914 self.menu.add(Gtk.SeparatorMenuItem())
62 saheb 2686
63 adrian 2891 menublockproperties = Gtk.MenuItem("_Properties")
64 adrian 2914 menublockproperties.set_use_underline(True)
65 jpye 2394 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 0)
66 adrian 2914 self.menu.add(menublockproperties)
67 saheb 2686
68 adrian 2914 menublockparams = Gtk.MenuItem("Parameters")
69 jpye 2394 menublockparams.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 1)
70 adrian 2914 self.menu.add(menublockparams)
71 saheb 2686
72 adrian 2891 menudefault = Gtk.MenuItem("_Set Default Values")
73 adrian 2914 menudefault.set_use_underline(True)
74 grivan 2628 menudefault.connect("activate",self.defaultvalues,window ,context,context.view.hovered_item)
75 adrian 2914 self.menu.add(menudefault)
76 saheb 2686
77 adrian 2891 #menublockmethod = Gtk.MenuItem("_Custom Method(s)")
78 jpye 2394 #menublockmethod.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 2)
79     #menu.add(menublockmethod)
80 saheb 2686
81 adrian 2891 menublockinstance = Gtk.MenuItem("_Instance")
82 adrian 2914 menublockinstance.set_use_underline(True)
83 jpye 2394 menublockinstance.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 3)
84 adrian 2914 self.menu.add(menublockinstance)
85 grivan 2628 '''
86 adrian 2891 menublockstreams = Gtk.MenuItem("_Streams")
87 grivan 2628 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
88     menu.add(menublockstreams)
89 saheb 2686 '''
90 adrian 2891 #menuinfo = Gtk.MenuItem("_Info",True)
91 saheb 2686 #menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
92 jpye 2394 #menu.add(menuinfo)
93 saheb 2686
94 adrian 2914 self.menu.add(Gtk.SeparatorMenuItem())
95 saheb 2686
96 adrian 2891 menucanvas = Gtk.MenuItem("_Canvas Properties",True)
97 adrian 2914 menucanvas.set_use_underline(True)
98 jpye 2394 menucanvas.connect("activate",self.canvasproperties,window ,context)
99 adrian 2914 self.menu.add(menucanvas)
100 saheb 2686
101 jpye 2394 if not context.view.hovered_item:
102     menurename.set_sensitive(False)
103     #menuinfo.set_sensitive(False)
104     menudelete.set_sensitive(False)
105 saheb 2686 menurotate_clock.set_sensitive(False)
106     menurotate_anti.set_sensitive(False)
107     menuflip.set_sensitive(False)
108 jpye 2394 menublockproperties.set_sensitive(False)
109     menublockinstance.set_sensitive(False)
110     #menublockmethod.set_sensitive(False)
111     menublockparams.set_sensitive(False)
112 grivan 2628 menudefault.set_sensitive(False)
113     '''menublockstreams.set_sensitive(False)'''
114 jpye 2394
115 saheb 2686
116 jpye 2394 if not hasattr(context.view.hovered_item,'blockinstance'):
117     menurename.set_sensitive(False)
118     #menuinfo.set_sensitive(False)
119 saheb 2686
120 jpye 2394 if context.view.hovered_item:
121     if not context.view.hovered_item.blockinstance.instance:
122     menublockinstance.set_sensitive(False)
123 saheb 2686
124 adrian 2914 self.menu.show_all()
125     self.menu.popup(None, None, None, None, event.button.button, event.time)
126 jpye 2394 #self.view.tool.ungrab(self.view.tool)
127     return False
128 saheb 2686
129 jpye 2394 #def on_button_release(self, event):
130     #print event
131     #self.view.hovered_item = None
132     #return False
133 saheb 2686
134 jpye 2394 def on_double_click(self,event):
135     context = self.view.tool
136 adrian 2914 if event.button.button != 1 or not context.view.hovered_item:
137 jpye 2394 context.ungrab(self.view.tool)
138     return False
139 saheb 2686
140 adrian 2914 window = context.view.get_parent().get_parent().get_parent().get_parent().get_parent()
141 jpye 2394 self.blockproperties(None , window, context, context.view.hovered_item)
142 saheb 2686
143 jpye 2005 def deactivate(self,widget,context):
144 jpye 2394 #print 'Hovered Item Set To None'
145 jpye 2005 context.view.hovered_item = None
146    
147 jpye 1945 def rename(self,widget,item,window):
148     if hasattr(item,'blockinstance'):
149     bi = item.blockinstance
150 adrian 2891 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 jpye 1945 ent.set_text(bi.name)
154 jpye 2004 def key_press(ent,event):
155 adrian 2891 key = Gdk.keyval_name(event.keyval)
156 jpye 2004 if key == 'Return':
157 adrian 2891 dia.response(Gtk.ResponseType.OK)
158 jpye 2004 return True
159     return False
160     ent.connect("key-press-event",key_press)
161 jpye 1945 dia.vbox.add(ent)
162     dia.show_all()
163     res = dia.run()
164 adrian 2891 if res == Gtk.ResponseType.OK:
165 jpye 1945 bi.name = ent.get_text()
166     dia.destroy()
167 saheb 2686
168 jpye 2394 @undo.block_observed
169 jpye 1969 def delete(self,widget,item,view):
170 saheb 2686 view.canvas.remove(item)
171 jpye 1945
172 jpye 2394 def blockproperties(self, widget = None, window = None, context = None, item = None, tab = None):
173 saheb 2686 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 jpye 2394 def canvasproperties(self, widget, window, context):
186     canvasproperties.CanvasProperties(window).run()
187 grivan 2628 '''
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 saheb 2686
194 grivan 2628 model = str(self.view.canvas)
195     #print model
196     self.view.canvas.saved_model = model
197     self.view.canvas.saved_data = {}
198 saheb 2686
199 grivan 2628 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 saheb 2686 item.blockinstance.params[param].fix = True

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