/[ascend]/trunk/pygtk/canvas/contextmenutool.py
ViewVC logotype

Annotation of /trunk/pygtk/canvas/contextmenutool.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2200 - (hide annotations) (download) (as text)
Thu May 20 05:17:35 2010 UTC (8 years, 11 months ago) by grivan
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 grivan 2200
2 jpye 1945 from gaphas.tool import Tool
3     import pygtk
4     pygtk.require('2.0')
5     import gtk
6 jpye 2005 import info
7     import blockinstance
8 arijit 2074 import blockproperties
9 jpye 1945
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 grivan 2200
21     def on_button_press(self, event):
22     context = self.view.tool
23 jpye 1945 if event.button != 3:
24 grivan 2200 context.ungrab(self.view.tool)
25 jpye 1945 return False
26     if context.view.hovered_item:
27     menu = gtk.Menu()
28 jpye 2005 menu.connect("deactivate",self.deactivate,context)
29     menurename = gtk.MenuItem("Re_name",True)
30 arijit 2074 window = context.view.parent.parent.parent.parent.parent
31 jpye 1945 menurename.connect("activate",self.rename,context.view.hovered_item,window)
32     menu.add(menurename)
33 jpye 2005 menudelete = gtk.MenuItem("_Delete",True)
34 jpye 1969 menudelete.connect("activate",self.delete,context.view.hovered_item,context.view)
35 jpye 1945 menu.add(menudelete)
36 jpye 2005 menu.add(gtk.SeparatorMenuItem())
37 arijit 2074
38     menublockproperties = gtk.MenuItem("_Properties")
39     menublockproperties.connect("activate",self.blockproperties, window, context, context.view.hovered_item)
40     menu.add(menublockproperties)
41    
42 jpye 2005 menuinfo = gtk.MenuItem("_Info",True)
43 arijit 2074 menuinfo.connect("activate",self.info,window,context,context.view.hovered_item)
44 jpye 2005 menu.add(menuinfo)
45 arijit 2074
46 jpye 2005 if not hasattr(context.view.hovered_item,'blockinstance'):
47     menurename.set_sensitive(False)
48     menuinfo.set_sensitive(False)
49     menu.show_all()
50 jpye 1945 menu.popup( None, None, None, event.button, event.time)
51 grivan 2200 self.view.tool.ungrab(self.view.tool)
52 jpye 2005 return True
53 jpye 1945
54 jpye 2005 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 arijit 2074 elif v.type == blockinstance.PORT_INOUT:
70     text += " (IN/OUT)\n"
71    
72 jpye 2005 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 arijit 2074
79 jpye 2005 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 jpye 1945 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 jpye 2004 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 jpye 1945 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 jpye 1969 def delete(self,widget,item,view):
119 jpye 1945 print "DELETING OBJECT"
120     # TODO: add undo handler
121 jpye 1969 view.canvas.remove(item)
122 jpye 1945
123 arijit 2074 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