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

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