/[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 2394 - (show annotations) (download) (as text)
Thu Feb 24 01:43:46 2011 UTC (8 years, 4 months ago) by jpye
Original Path: trunk/pygtk/canvas/contextmenutool.py
File MIME type: text/x-python
File size: 4649 byte(s)
Merged changes from Grivan's GSOC2010 project, branches/grivan, r2824:3253.
This brings the new canvas code into trunk.
1
2 from gaphas.tool import Tool
3 import pygtk
4 pygtk.require('2.0')
5 import gtk
6 import blockinstance
7 import blockproperties
8 import canvasproperties
9 import undo
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 menurename = gtk.MenuItem("Re_name",True)
32 window = context.view.parent.parent.parent.parent.parent
33 menurename.connect("activate",self.rename,context.view.hovered_item,window)
34 menu.add(menurename)
35
36 menudelete = gtk.MenuItem("_Delete",True)
37 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
38 menu.add(menudelete)
39
40 menu.add(gtk.SeparatorMenuItem())
41
42 menublockproperties = gtk.MenuItem("_Properties")
43 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 0)
44 menu.add(menublockproperties)
45
46 menublockparams = gtk.MenuItem("_Parameters")
47 menublockparams.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 1)
48 menu.add(menublockparams)
49
50 #menublockmethod = gtk.MenuItem("_Custom Method(s)")
51 #menublockmethod.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 2)
52 #menu.add(menublockmethod)
53
54 menublockinstance = gtk.MenuItem("_Instance")
55 menublockinstance.connect("activate",self.blockproperties, window, context, context.view.hovered_item, 3)
56 menu.add(menublockinstance)
57
58 #menuinfo = gtk.MenuItem("_Info",True)
59 #menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
60 #menu.add(menuinfo)
61
62 menu.add(gtk.SeparatorMenuItem())
63
64 menucanvas = gtk.MenuItem("_Canvas Properties",True)
65 menucanvas.connect("activate",self.canvasproperties,window ,context)
66 menu.add(menucanvas)
67
68 if not context.view.hovered_item:
69 menurename.set_sensitive(False)
70 #menuinfo.set_sensitive(False)
71 menudelete.set_sensitive(False)
72 menublockproperties.set_sensitive(False)
73 menublockinstance.set_sensitive(False)
74 #menublockmethod.set_sensitive(False)
75 menublockparams.set_sensitive(False)
76
77
78 if not hasattr(context.view.hovered_item,'blockinstance'):
79 menurename.set_sensitive(False)
80 #menuinfo.set_sensitive(False)
81
82 if context.view.hovered_item:
83 if not context.view.hovered_item.blockinstance.instance:
84 menublockinstance.set_sensitive(False)
85
86 menu.show_all()
87 menu.popup( None, None, None, event.button, event.time)
88 #self.view.tool.ungrab(self.view.tool)
89 return False
90
91 #def on_button_release(self, event):
92 #print event
93 #self.view.hovered_item = None
94 #return False
95
96 def on_double_click(self,event):
97 context = self.view.tool
98 if event.button != 1 or not context.view.hovered_item:
99 context.ungrab(self.view.tool)
100 return False
101
102 window = context.view.parent.parent.parent.parent.parent
103 self.blockproperties(None , window, context, context.view.hovered_item)
104
105 def deactivate(self,widget,context):
106 #print 'Hovered Item Set To None'
107 context.view.hovered_item = None
108
109 def rename(self,widget,item,window):
110 if hasattr(item,'blockinstance'):
111 bi = item.blockinstance
112 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))
113 dia.set_default_response(gtk.RESPONSE_OK)
114 ent = gtk.Entry()
115 ent.set_text(bi.name)
116 def key_press(ent,event):
117 key = gtk.gdk.keyval_name(event.keyval)
118 if key == 'Return':
119 dia.response(gtk.RESPONSE_OK)
120 return True
121 return False
122 ent.connect("key-press-event",key_press)
123 dia.vbox.add(ent)
124 dia.show_all()
125 res = dia.run()
126 if res == gtk.RESPONSE_OK:
127 bi.name = ent.get_text()
128 dia.destroy()
129
130 @undo.block_observed
131 def delete(self,widget,item,view):
132 view.canvas.remove(item)
133
134 def blockproperties(self, widget = None, window = None, context = None, item = None, tab = None):
135 blockproperties.BlockProperties(window, item, tab = tab).run()
136
137 def canvasproperties(self, widget, window, context):
138 canvasproperties.CanvasProperties(window).run()

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