del net-oscar
[learning-git.git] / pgworksheet_yvesf / plugins / sidebar / FunctionBrowser.py
blob7a8e16b240e74611ad3adab0a31126c33907a3f7
1 # (c) Yves Fischer yvesf - xapek org, 2007, License: GPL-2
3 import pygtk;
4 import gtk;
5 from pgw.Plugin import SidebarPlugin;
6 import pgw.Syntax;
8 class ListBox(gtk.ScrolledWindow):
9 def __init__(self,ui):
10 gtk.ScrolledWindow.__init__(self,ui)
11 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
12 self.set_shadow_type(gtk.SHADOW_NONE)
14 self.store = gtk.ListStore(str)
15 self.view = gtk.TreeView(self.store)
17 cell = gtk.CellRendererText()
18 column = gtk.TreeViewColumn("Functions")
19 column.pack_start(cell, True)
20 column.add_attribute(cell, 'text', 0)
21 column.set_sort_column_id(0)
22 self.view.append_column(column)
24 self.add(self.view)
26 def set_array(self, array):
27 self.store.clear()
28 for item in array:
29 self.store.append(item)
31 def get(self):
32 tuple, treeview = self.view.get_cursor()
33 if tuple == None:
34 return None
35 return self.store[tuple[0]]
37 class TextWidget(gtk.ScrolledWindow):
38 def __init__(self):
39 gtk.ScrolledWindow.__init__(self)
40 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
41 self.set_shadow_type(gtk.SHADOW_NONE)
43 self.buffer = gtk.TextBuffer()
44 self.syntax = pgw.Syntax.Syntax(self.buffer)
46 self.buffer.connect('delete-range', self.syntax.text_deleted)
47 self.buffer.connect('insert-text', self.syntax.text_inserted)
48 self.buffer.connect('changed', self.syntax.text_changed)
50 self.view = gtk.TextView(self.buffer)
52 self.add(self.view)
55 def set_text(self, text):
56 self.buffer.set_text(text)
57 def get_text(self):
58 return self.buffer.get_text( \
59 self.buffer.get_start_iter(), self.buffer.get_end_iter() )
61 class FunctionBrowserPlugin(SidebarPlugin):
62 name = "Function Browser"
63 def __init__(self):
64 SidebarPlugin.__init__(self)
65 self.vpaned = gtk.VPaned()
67 self.hcontainer = gtk.HBox()
69 self.button = gtk.Button("Reload")
70 self.button.set_property("height-request", 30)
71 self.button.set_property("width-request", 50)
72 self.button.connect("clicked", self.refresh)
73 self.hcontainer.add(self.button)
75 self.liststore = gtk.ListStore(str);
76 self.combobox = gtk.ComboBox(self.liststore)
77 cell = gtk.CellRendererText()
78 self.combobox.pack_start(cell, True)
79 self.combobox.add_attribute(cell, 'text', 0)
81 self.hcontainer.add(self.combobox)
82 self.liststore.append(["asdf"]);
83 self.liststore.append(["asdf"]);
84 self.liststore.append(["asdf"]);
85 self.liststore.append(["asdf"]);
89 self.pack_start(self.hcontainer, False, True)
90 self.listbox = ListBox()
91 self.listbox.view.set_property("height-request", 200)
92 self.vpaned.pack1(self.listbox)
94 self.text = TextWidget()
95 self.vpaned.pack2(self.text)
97 self.add(self.vpaned)
99 self.listbox.view.connect("cursor-changed", self.cursor)
101 def on_init(self):
102 pass
104 def on_connection(self):
105 self.refresh()
107 def cursor(self, widget, data=None):
108 name = self.listbox.get()[0]
109 if not vars(self.app).has_key("db") or not self.app.db.is_connected:
110 print "FunctionBrowser: No Connection!"
111 return
112 query = self.app.db.query("SELECT prosrc AS sprache FROM pg_catalog.pg_proc WHERE proname = '%s'" %(name))
113 result = query['cursor'].fetchall()
114 self.text.set_text(result[0][0])
116 def refresh(self, widget=None):
117 if not vars(self.app).has_key("db") or not self.app.db.is_connected:
118 return
119 self.button.set_relief( gtk.RELIEF_NONE )
121 #dropdown
122 sql = """SELECT DISTINCT(lanname)
123 FROM pg_catalog.pg_language AS L, pg_catalog.pg_proc AS P
124 WHERE int4(L.lanplcallfoid)+1 = int4(P.prolang);"""
125 query = self.app.db.query(sql);
127 results = query['cursor'].fetchall()
128 active = self.combobox.get_active_text()
129 self.liststore.clear()
130 self.liststore.append([_("All")])
133 for result in results:
134 if len(result) == 1:
135 self.liststore.append([result[0]])
136 if result[0] == active: #take-over old selection
137 j = n
138 n+=1
139 else:
140 print "FunctionBrowser: Error, unexpected result" + str(result)
141 self.combobox.set_active(j)
142 #list
144 filter = self.combobox.get_active_text()
145 if filter == _("All") or filter == None:
146 filter = '%';
147 sql = """SELECT proname
148 FROM pg_catalog.pg_language AS L, pg_catalog.pg_proc AS P
149 WHERE
150 int4(L.lanplcallfoid)+1 = int4(P.prolang)
151 AND L.lanname LIKE '%s';""" % filter
152 got = self.app.db.query(sql)
153 cursor = got['cursor']
154 results = cursor.fetchall()
156 store = []
157 for line in results:
158 store.append([line[0]])
160 self.listbox.set_array(store)
162 self.button.set_relief( gtk.RELIEF_NORMAL )
163 return
165 EXPORTS=[FunctionBrowserPlugin]