1 |
from gaphas.tool import Tool |
2 |
import pygtk |
3 |
pygtk.require('2.0') |
4 |
import gtk |
5 |
|
6 |
class ContextMenuTool(Tool): |
7 |
""" |
8 |
Context menu for blocks and connectors on the canvas, intended to be |
9 |
the main mouse-based way by which interaction with blocks occurs (blocks |
10 |
renamed, parameters specified, values examined, etc). |
11 |
Code for performing these tasks should not be here; this should just |
12 |
hook into the appropriate code in the Application layer. |
13 |
""" |
14 |
def __init__(self): |
15 |
pass |
16 |
|
17 |
def on_button_press(self, context, event): |
18 |
if event.button != 3: |
19 |
return False |
20 |
if context.view.hovered_item: |
21 |
menu = gtk.Menu() |
22 |
menurename = gtk.MenuItem("Re_name",True); |
23 |
window = context.view.parent.parent.parent.parent |
24 |
print window.__class__ |
25 |
menurename.connect("activate",self.rename,context.view.hovered_item,window) |
26 |
menu.add(menurename) |
27 |
menudelete = gtk.MenuItem("_Delete",True); |
28 |
menudelete.connect("activate",self.delete,context.view.hovered_item,context.view) |
29 |
menu.add(menudelete) |
30 |
menu.show_all() |
31 |
menu.popup( None, None, None, event.button, event.time) |
32 |
|
33 |
def rename(self,widget,item,window): |
34 |
if hasattr(item,'blockinstance'): |
35 |
bi = item.blockinstance |
36 |
dia = gtk.Dialog("Rename %s '%s'" |
37 |
% ( |
38 |
bi.blocktype.type.getName() |
39 |
,bi.name |
40 |
), window,gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT |
41 |
,(gtk.STOCK_CANCEL, 0, gtk.STOCK_OK, gtk.RESPONSE_OK) |
42 |
) |
43 |
dia.set_default_response(gtk.RESPONSE_OK) |
44 |
ent = gtk.Entry() |
45 |
ent.set_text(bi.name) |
46 |
dia.vbox.add(ent) |
47 |
dia.show_all() |
48 |
res = dia.run() |
49 |
if res == gtk.RESPONSE_OK: |
50 |
bi.name = ent.get_text() |
51 |
dia.destroy() |
52 |
|
53 |
def delete(self,widget,item,view): |
54 |
print "DELETING OBJECT" |
55 |
# TODO: add undo handler |
56 |
view.canvas.remove(item) |
57 |
|