GUI: Clean up creation of GUIs, hopefully fixing the crash-on-destroy this time.
[calf.git] / bigbull / mainwin.py
blob63e4d0dee8c3b767ad438e4dbf1795c90cf6f153
1 #!/usr/bin/python
2 import pygtk
3 pygtk.require('2.0')
4 import gtk
5 import lv2
6 import fakeserv
7 import client
8 import calfpytools
9 import conndiagram
10 from calfgtkutils import *
11 Colors = conndiagram.Colors
13 class LV2GraphParser():
14 def get_port_color(self, portData):
15 color = Colors.defPort
16 if portData.isAudio:
17 color = Colors.audioPort
18 if portData.isControl:
19 color = Colors.controlPort
20 if portData.isEvent:
21 color = Colors.eventPort
22 return color
23 def is_port_input(self, portData):
24 return portData.isInput
25 def get_port_name(self, portData):
26 return portData.name
27 def get_port_id(self, portData):
28 return portData.uri
29 def get_module_name(self, moduleData):
30 return moduleData.name
31 def get_module_port_list(self, moduleData):
32 return [(port.uri, port) for port in moduleData.ports if lv2.epi + "notAutomatic" not in port.properties]
33 def can_connect(self, first, second):
34 return first.connectableTo(second)
35 def connect(self, first, second):
36 print "Connection not implemented yet"
37 pass
39 class App:
40 def __init__(self):
41 fakeserv.start()
42 self.lv2db = lv2.LV2DB()
43 self.parser = LV2GraphParser()
44 self.cgraph = conndiagram.ConnectionGraphEditor(self, self.parser)
46 def canvas_popup_menu_handler(self, widget):
47 self.canvas_popup_menu(0, 0, 0)
48 return True
50 def canvas_button_press_handler(self, widget, event):
51 if event.button == 3:
52 self.canvas_popup_menu(event.x, event.y, event.time)
53 return True
55 def add_module_cb(self, params):
56 self.cgraph.add_module(*params)
58 def canvas_popup_menu(self, x, y, time):
59 menu = gtk.Menu()
60 items = self.cgraph.get_data_items_at(x, y)
61 types = set([di[0] for di in items])
62 if 'port' in types:
63 pass
64 #add_option(menu, "-Port-", self.cgraph.delete_module_cb, (None, x, y))
65 elif 'module' in types:
66 for mod in [di for di in items if di[0] == "module"]:
67 add_option(menu, "Delete "+mod[1].plugin.name, self.cgraph.delete_module, mod[1])
68 else:
69 for uri in self.lv2db.getPluginList():
70 plugin = self.lv2db.getPluginInfo(uri)
71 add_option(menu, plugin.name, self.add_module_cb, (plugin, x, y))
72 menu.show_all()
73 menu.popup(None, None, None, 3, time)
75 def create(self):
76 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
77 self.window.connect("delete_event", self.delete_event)
78 self.window.connect("destroy", self.destroy)
79 self.main_vbox = gtk.VBox()
80 self.create_main_menu()
81 self.scroll = gtk.ScrolledWindow()
82 self.scroll.add(self.cgraph.create())
83 self.main_vbox.pack_start(self.menu_bar, expand = False, fill = False)
84 self.main_vbox.add(self.scroll)
85 self.window.add(self.main_vbox)
86 self.window.show_all()
87 self.main_vbox.connect("popup-menu", self.canvas_popup_menu_handler)
88 self.cgraph.canvas.connect("button-press-event", self.canvas_button_press_handler)
90 def create_main_menu(self):
91 self.menu_bar = gtk.MenuBar()
93 self.file_menu = add_submenu(self.menu_bar, "_File")
94 add_option(self.file_menu, "_Exit", self.exit)
95 self.plugin_menu = add_submenu(self.menu_bar, "_Plugins")
96 self.submenus = dict()
97 self.category_to_path = dict()
98 for (path, uri) in self.lv2db.get_categories():
99 if path == []:
100 continue;
101 parent_menu = self.plugin_menu
102 parent_path = "/".join(path[:-1])
103 if parent_path in self.submenus:
104 parent_menu = self.submenus[parent_path]
105 self.submenus["/".join(path)] = add_submenu(parent_menu, path[-1])
106 self.category_to_path[uri] = path
107 for uri in self.lv2db.getPluginList():
108 plugin = self.lv2db.getPluginInfo(uri)
109 parent_menu = self.plugin_menu
110 best_path = self.get_best_category(plugin)
111 if best_path in self.submenus:
112 parent_menu = self.submenus[best_path]
113 add_option(parent_menu, plugin.name, self.add_module_cb, (plugin, None, None))
115 def get_best_category(self, plugin):
116 max_len = -1
117 best_path = []
118 for path in [self.category_to_path[c] for c in plugin.classes if c in self.category_to_path]:
119 if len(path) > max_len:
120 best_path = path
121 max_len = len(path)
122 return "/".join(best_path)
124 def exit(self, data):
125 self.window.destroy()
127 def delete_event(self, widget, data = None):
128 gtk.main_quit()
129 def destroy(self, widget, data = None):
130 gtk.main_quit()
132 app = App()
133 app.create()
134 gtk.main()