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

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