/[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 2895 - (hide annotations) (download) (as text)
Sun Apr 12 14:32:00 2015 UTC (3 years, 11 months ago) by adrian
File MIME type: text/x-python
File size: 7235 byte(s)
Removed unecessary imports
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     from gi.repository import Gtk
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 jpye 1945 if event.button != 3:
25 grivan 2200 context.ungrab(self.view.tool)
26 jpye 1945 return False
27 saheb 2686
28 adrian 2891 menu = Gtk.Menu()
29 jpye 2394 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 2891 menurename = Gtk.MenuItem("Re_name",True)
36 jpye 2394 window = context.view.parent.parent.parent.parent.parent
37     menurename.connect("activate",self.rename,context.view.hovered_item,window)
38     menu.add(menurename)
39 saheb 2686 menudefault.set_sensitive(False)
40     '''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     menu.add(menudelete)
45 jpye 1945
46 adrian 2891 menu.add(Gtk.SeparatorMenuItem())
47 saheb 2686
48 adrian 2891 menurotate_clock = Gtk.MenuItem("_Rotate_clockwise",True)
49 saheb 2686 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 adrian 2891 menurotate_anti = Gtk.MenuItem("_Rotate_anti_clockwise",True)
54 saheb 2686 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 adrian 2891 menuflip = Gtk.MenuItem("Flip",True)
59 saheb 2686 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 adrian 2891 menu.add(Gtk.SeparatorMenuItem())
64 saheb 2686
65 adrian 2891 menublockproperties = Gtk.MenuItem("_Properties")
66 jpye 2394 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 0)
67     menu.add(menublockproperties)
68 saheb 2686
69 adrian 2891 menublockparams = Gtk.MenuItem("_Parameters")
70 jpye 2394 menublockparams.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 1)
71     menu.add(menublockparams)
72 saheb 2686
73 adrian 2891 menudefault = Gtk.MenuItem("_Set Default Values")
74 grivan 2628 menudefault.connect("activate",self.defaultvalues,window ,context,context.view.hovered_item)
75     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 jpye 2394 menublockinstance.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 3)
83     menu.add(menublockinstance)
84 grivan 2628 '''
85 adrian 2891 menublockstreams = Gtk.MenuItem("_Streams")
86 grivan 2628 menublockstreams.connect("activate",self.setstream, window, context, context.view.hovered_item)
87     menu.add(menublockstreams)
88 saheb 2686 '''
89 adrian 2891 #menuinfo = Gtk.MenuItem("_Info",True)
90 saheb 2686 #menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
91 jpye 2394 #menu.add(menuinfo)
92 saheb 2686
93 adrian 2891 menu.add(Gtk.SeparatorMenuItem())
94 saheb 2686
95 adrian 2891 menucanvas = Gtk.MenuItem("_Canvas Properties",True)
96 jpye 2394 menucanvas.connect("activate",self.canvasproperties,window ,context)
97     menu.add(menucanvas)
98 saheb 2686
99 jpye 2394 if not context.view.hovered_item:
100     menurename.set_sensitive(False)
101     #menuinfo.set_sensitive(False)
102     menudelete.set_sensitive(False)
103 saheb 2686 menurotate_clock.set_sensitive(False)
104     menurotate_anti.set_sensitive(False)
105     menuflip.set_sensitive(False)
106 jpye 2394 menublockproperties.set_sensitive(False)
107     menublockinstance.set_sensitive(False)
108     #menublockmethod.set_sensitive(False)
109     menublockparams.set_sensitive(False)
110 grivan 2628 menudefault.set_sensitive(False)
111     '''menublockstreams.set_sensitive(False)'''
112 jpye 2394
113 saheb 2686
114 jpye 2394 if not hasattr(context.view.hovered_item,'blockinstance'):
115     menurename.set_sensitive(False)
116     #menuinfo.set_sensitive(False)
117 saheb 2686
118 jpye 2394 if context.view.hovered_item:
119     if not context.view.hovered_item.blockinstance.instance:
120     menublockinstance.set_sensitive(False)
121 saheb 2686
122 jpye 2394 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 saheb 2686
127 jpye 2394 #def on_button_release(self, event):
128     #print event
129     #self.view.hovered_item = None
130     #return False
131 saheb 2686
132 jpye 2394 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 saheb 2686
138     window = context.view.parent.parent.parent.parent.parent
139 jpye 2394 self.blockproperties(None , window, context, context.view.hovered_item)
140 saheb 2686
141 jpye 2005 def deactivate(self,widget,context):
142 jpye 2394 #print 'Hovered Item Set To None'
143 jpye 2005 context.view.hovered_item = None
144    
145 jpye 1945 def rename(self,widget,item,window):
146     if hasattr(item,'blockinstance'):
147     bi = item.blockinstance
148 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))
149     dia.set_default_response(Gtk.ResponseType.OK)
150     ent = Gtk.Entry()
151 jpye 1945 ent.set_text(bi.name)
152 jpye 2004 def key_press(ent,event):
153 adrian 2891 key = Gdk.keyval_name(event.keyval)
154 jpye 2004 if key == 'Return':
155 adrian 2891 dia.response(Gtk.ResponseType.OK)
156 jpye 2004 return True
157     return False
158     ent.connect("key-press-event",key_press)
159 jpye 1945 dia.vbox.add(ent)
160     dia.show_all()
161     res = dia.run()
162 adrian 2891 if res == Gtk.ResponseType.OK:
163 jpye 1945 bi.name = ent.get_text()
164     dia.destroy()
165 saheb 2686
166 jpye 2394 @undo.block_observed
167 jpye 1969 def delete(self,widget,item,view):
168 saheb 2686 view.canvas.remove(item)
169 jpye 1945
170 jpye 2394 def blockproperties(self, widget = None, window = None, context = None, item = None, tab = None):
171 saheb 2686 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 jpye 2394 def canvasproperties(self, widget, window, context):
184     canvasproperties.CanvasProperties(window).run()
185 grivan 2628 '''
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 saheb 2686
192 grivan 2628 model = str(self.view.canvas)
193     #print model
194     self.view.canvas.saved_model = model
195     self.view.canvas.saved_data = {}
196 saheb 2686
197 grivan 2628 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 saheb 2686 item.blockinstance.params[param].fix = True

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