Minor refactoring for GUI.
[zeroinstall.git] / iface_browser.py
blob2ac46aa020ab39c877d13b80e63b801b58112f14
1 import gtk
3 from model import Interface
4 import properties
5 import reader
6 from gui import policy
8 class InterfaceBrowser(gtk.ScrolledWindow):
9 model = None
10 root = None
11 edit_properties = None
13 INTERFACE = 0
14 INTERFACE_NAME = 1
15 VERSION = 2
16 SUMMARY = 3
18 def __init__(self):
19 self.edit_properties = gtk.Action('edit_properties',
20 'Interface Properties...',
21 'Set which implementation of this interface to use.',
22 gtk.STOCK_PROPERTIES)
23 self.edit_properties.set_property('sensitive', False)
25 gtk.ScrolledWindow.__init__(self)
26 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
27 self.set_shadow_type(gtk.SHADOW_IN)
29 self.model = gtk.TreeStore(object, str, str, str)
30 self.tree_view = tree_view = gtk.TreeView(self.model)
32 text = gtk.CellRendererText()
34 column = gtk.TreeViewColumn(_('Interface'), text,
35 text = InterfaceBrowser.INTERFACE_NAME)
36 tree_view.append_column(column)
38 column = gtk.TreeViewColumn(_('Version'), text,
39 text = InterfaceBrowser.VERSION)
40 tree_view.append_column(column)
42 column = gtk.TreeViewColumn(_('Description'), text,
43 text = InterfaceBrowser.SUMMARY)
44 tree_view.append_column(column)
46 self.add(tree_view)
47 tree_view.show()
49 tree_view.set_enable_search(True)
51 selection = tree_view.get_selection()
53 def sel_changed(sel):
54 store, iter = sel.get_selected()
55 self.edit_properties.set_property('sensitive', iter != None)
56 selection.connect('changed', sel_changed)
58 def button_press(tree_view, bev):
59 if bev.button != 1 or bev.type != gtk.gdk._2BUTTON_PRESS:
60 return False
61 pos = tree_view.get_path_at_pos(int(bev.x), int(bev.y))
62 if not pos:
63 return False
64 path, col, x, y = pos
65 properties.edit(self.model[path][InterfaceBrowser.INTERFACE])
66 tree_view.connect('button-press-event', button_press)
68 def edit_selected(action):
69 store, iter = selection.get_selected()
70 assert iter
71 properties.edit(self.model[iter][InterfaceBrowser.INTERFACE])
72 self.edit_properties.connect('activate', edit_selected)
74 self.connect('destroy', lambda s: policy.watchers.remove(self.build_tree))
75 policy.watchers.append(self.build_tree)
77 def set_root(self, root):
78 assert isinstance(root, Interface)
79 self.root = root
80 policy.recalculate() # Calls build_tree
82 def build_tree(self):
83 self.model.clear()
84 parent = None
85 def add_node(parent, iface):
86 iter = self.model.append(parent)
87 self.model[iter][InterfaceBrowser.INTERFACE] = iface
88 self.model[iter][InterfaceBrowser.INTERFACE_NAME] = iface.get_name()
89 self.model[iter][InterfaceBrowser.SUMMARY] = iface.summary
91 impl = policy.implementation.get(iface, None)
92 if impl:
93 self.model[iter][InterfaceBrowser.VERSION] = impl.get_version()
94 for child in impl.dependencies.values():
95 add_node(iter, policy.get_interface(child.interface))
96 else:
97 self.model[iter][InterfaceBrowser.VERSION] = '(choose)'
98 add_node(None, self.root)
99 self.tree_view.expand_all()