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

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