/[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 2200 - (show annotations) (download) (as text)
Thu May 20 05:17:35 2010 UTC (9 years, 1 month ago) by grivan
Original Path: trunk/pygtk/canvas/contextmenutool.py
File MIME type: text/x-python
File size: 3834 byte(s)
Adding patch from Grivan, attempt to get code running with latest git master version of Gaphas. Submitted by John.
1
2 from gaphas.tool import Tool
3 import pygtk
4 pygtk.require('2.0')
5 import gtk
6 import info
7 import blockinstance
8 import blockproperties
9
10 class ContextMenuTool(Tool):
11 """
12 Context menu for blocks and connectors on the canvas, intended to be
13 the main mouse-based way by which interaction with blocks occurs (blocks
14 renamed, parameters specified, values examined, etc).
15 Code for performing these tasks should not be here; this should just
16 hook into the appropriate code in the Application layer.
17 """
18 def __init__(self):
19 pass
20
21 def on_button_press(self, event):
22 context = self.view.tool
23 if event.button != 3:
24 context.ungrab(self.view.tool)
25 return False
26 if context.view.hovered_item:
27 menu = gtk.Menu()
28 menu.connect("deactivate",self.deactivate,context)
29 menurename = gtk.MenuItem("Re_name",True)
30 window = context.view.parent.parent.parent.parent.parent
31 menurename.connect("activate",self.rename,context.view.hovered_item,window)
32 menu.add(menurename)
33 menudelete = gtk.MenuItem("_Delete",True)
34 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
35 menu.add(menudelete)
36 menu.add(gtk.SeparatorMenuItem())
37
38 menublockproperties = gtk.MenuItem("_Properties")
39 menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item)
40 menu.add(menublockproperties)
41
42 menuinfo = gtk.MenuItem("_Info",True)
43 menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
44 menu.add(menuinfo)
45
46 if not hasattr(context.view.hovered_item,'blockinstance'):
47 menurename.set_sensitive(False)
48 menuinfo.set_sensitive(False)
49 menu.show_all()
50 menu.popup( None, None, None, event.button, event.time)
51 self.view.tool.ungrab(self.view.tool)
52 return True
53
54 def info(self,widget,window,context,item):
55 print window.__class__
56 bi = item.blockinstance
57 title = "Info for block '%s'" % bi.name
58 text = "Block info\n\n"
59 text += "Name:\t%s\n" % bi.name
60 text += "Type:\t%s\n" % bi.blocktype.type.getName()
61
62 text += "\nPorts:\n"
63 for k,v in bi.ports.iteritems():
64 text += "\t%s" % v.name
65 if v.type == blockinstance.PORT_IN:
66 text += " (IN)\n"
67 elif v.type == blockinstance.PORT_OUT:
68 text += " (OUT)\n"
69 elif v.type == blockinstance.PORT_INOUT:
70 text += " (IN/OUT)\n"
71
72 if hasattr(bi,"params"):
73 text += "\nParameters:\n"
74 for k,v in bi.params.iteritems():
75 text += "\t%s = %f\n" % (v.name, v.value)
76
77 if bi.instance:
78
79 text += "\nInstance exists\n"
80 else:
81 text += "\nNo instance exists\n"
82 info.Info(window,text,title).run()
83
84
85 def deactivate(self,widget,context):
86 #print "DEACTIVATING MENU"
87 # TODO pass a signal to the HoverTool to update the hovered item
88 # according to where the mouse is currently located.
89 context.view.hovered_item = None
90
91 def rename(self,widget,item,window):
92 if hasattr(item,'blockinstance'):
93 bi = item.blockinstance
94 dia = gtk.Dialog("Rename %s '%s'"
95 % (
96 bi.blocktype.type.getName()
97 ,bi.name
98 ), window,gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
99 ,(gtk.STOCK_CANCEL, 0, gtk.STOCK_OK, gtk.RESPONSE_OK)
100 )
101 dia.set_default_response(gtk.RESPONSE_OK)
102 ent = gtk.Entry()
103 ent.set_text(bi.name)
104 def key_press(ent,event):
105 key = gtk.gdk.keyval_name(event.keyval)
106 if key == 'Return':
107 dia.response(gtk.RESPONSE_OK)
108 return True
109 return False
110 ent.connect("key-press-event",key_press)
111 dia.vbox.add(ent)
112 dia.show_all()
113 res = dia.run()
114 if res == gtk.RESPONSE_OK:
115 bi.name = ent.get_text()
116 dia.destroy()
117
118 def delete(self,widget,item,view):
119 print "DELETING OBJECT"
120 # TODO: add undo handler
121 view.canvas.remove(item)
122
123 def blockproperties(self, widget, window, context, item):
124 blockproperties.BlockProperties(window, item).run()
125
126

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