del net-oscar
[learning-git.git] / pgworksheet_yvesf / pgw / ResultView.py
blob487a96d9e9dfdc028c040ecc4c403a38c788cd9f
1 import string
2 import pygtk;
3 import gtk;
4 import gobject;
5 import sys
7 import pgw;
8 import pgw.Plugin;
9 import gettext
10 _ = gettext.gettext
11 class ListBox(gtk.ScrolledWindow):
12 def __init__(self, store=None):
13 gtk.ScrolledWindow.__init__(self)
14 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
15 self.set_shadow_type(gtk.SHADOW_NONE)
17 if store:
18 self.store = store
19 else:
20 self.store = gtk.ListStore(str)
21 self.view = gtk.TreeView(self.store)
23 cell = gtk.CellRendererText()
24 column = gtk.TreeViewColumn(_('Ansicht'))
25 column.pack_start(cell, True)
26 column.add_attribute(cell, 'text', 0)
27 column.set_sort_column_id(0)
28 self.view.append_column(column)
30 self.add(self.view)
32 def set_array(self, array):
33 self.store.clear()
34 for item in array:
35 self.store.append(item)
37 def get(self):
38 tuple, treeview = self.view.get_cursor()
39 if tuple == None:
40 return None
41 return self.store[tuple[0]]
45 class ResultViewTable(pgw.Plugin.ResultViewPlugin):
46 name = _('Tabellenansicht')
47 def __init__(self, ui, result):
48 pgw.Plugin.ResultViewPlugin.__init__(self, ui, result)
50 sql = result.sql.strip()
51 scroll = gtk.ScrolledWindow()
52 tvw = gtk.TreeView()
53 scroll.add(tvw)
54 self.add(scroll)
56 type_str = ""
57 cell = gtk.CellRendererText()
58 ncol = 0
59 for col in result.description:
60 column = gtk.TreeViewColumn(string.replace(col.name, "_", "__"))
61 column.pack_start(cell, True)
62 column.add_attribute(cell, 'text', ncol)
63 column.set_sort_column_id(ncol)
64 tvw.append_column(column)
65 ncol = ncol + 1
66 if (type_str == ""):
67 type_str = "str"
68 else:
69 type_str = type_str + ",str"
70 pass
71 # dynamically create the liststore, crazy
72 code = "liststore = gtk.ListStore(" + type_str + ")"
73 print code
74 exec compile(code, "<string>", 'exec')
76 for row in result.rows:
77 c = 0
78 while c < len(row):
79 if row[c] is None:
80 row[c] = '(null)'
81 c += 1
82 liststore.append(row)
84 tvw.set_model(liststore)
85 tvw.set_reorderable(True)
86 tvw.columns_autosize()
88 self.show_all()
90 def update(self):
91 type_str = ""
92 cell = gtk.CellRendererText()
93 ncol = 0
94 for col in self.result.description:
95 column = gtk.TreeViewColumn(string.replace(col.name, "_", "__"))
96 column.pack_start(cell, True)
97 column.add_attribute(cell, 'text', ncol)
98 column.set_sort_column_id(ncol)
99 tvw.append_column(column)
100 ncol = ncol + 1
101 if (type_str == ""):
102 type_str = "str"
103 else:
104 type_str = type_str + ",str"
105 pass
106 # dynamically create the liststore, crazy
107 code = "liststore = gtk.ListStore(" + type_str + ")"
108 print code
109 exec compile(code, "<string>", 'exec')
111 for row in self.result.rows:
112 c = 0
113 while c < len(row):
114 if row[c] is None:
115 row[c] = '(null)'
116 c += 1
117 liststore.append(row)
119 tvw.set_model(liststore)
120 tvw.set_reorderable(True)
121 tvw.columns_autosize()
125 class GObjectContainer(gobject.GObject):
126 def __init__(self, data):
127 gobject.GObject.__init__(self)
128 self.set_data("key", data)
130 def get(self):
131 return self.get_data("key")
133 def set(self, data):
134 self.set_data("key", data)
135 return data
137 class ResultViewPluginContainer(gtk.HPaned):
138 def __init__(self,app, result):
139 gtk.HPaned.__init__(self)
140 self.app = app
141 self.result = result
142 self.is_persistent = False
143 self.plugins = [ResultViewTable]
144 for plugin in pgw.Plugin.fromPath(sys.path[0] + "/plugins/", pgw.Plugin.ResultViewPlugin):
145 self.plugins.append(plugin)
147 self.listbox = ListBox(gtk.ListStore(str,GObjectContainer))
148 data = []
149 for plugin in self.plugins:
150 data.append([plugin.name, GObjectContainer(plugin)])
151 self.listbox.set_array(data)
152 self.listbox.view.set_property("width-request", 170)
153 self.listbox.view.connect("cursor-changed", self.set_plugin)
155 self.persistent = gtk.CheckButton(_('Persistent'))
156 self.persistent.connect("clicked", self.sig_set_persistense)
157 self.persistent.set_property("height-request", 30)
159 self.suicide = gtk.Button(_('Close'))
160 self.suicide.connect("clicked", self.remove_myself)
161 self.suicide.set_property("height-request", 30)
163 self.vbox_hbox = gtk.HBox()
164 self.vbox_hbox.add(self.persistent)
165 self.vbox_hbox.add(self.suicide)
167 self.vbox = gtk.VBox()
168 self.vbox.pack_start(self.vbox_hbox, False, False)
169 self.vbox.pack_start(self.listbox, True, True)
171 self.pack2(self.vbox, False, True)
172 self.vbox.show_all()
174 self.show()
176 self.set_plugin() #set default plugin
178 def remove_myself(self, widget=None):
179 self.parent.remove(self)
181 def sig_set_persistense(self, widget=None):
182 self.is_persistent = self.persistent.get_property("active")
184 def set_plugin(self, widget=None):
185 print str(self) + "set_plugin"
186 tuple, tvw = self.listbox.view.get_cursor()
187 if tuple == None: #nothin selected
188 tuple = (0,) #select 0
189 plugin_class = self.listbox.store[tuple[0]][1].get()
191 if plugin_class == None: #Plugin is in ListBox but not registered, should never happen
192 print "plugin_class == None"
193 raise Exception("plugin_class should contain a plugin and not None")
195 oldchild = self.get_child1()
196 if oldchild:
197 self.remove(oldchild)
199 self.plugin = plugin_class(self.app, self.result)
200 self.pack1(self.plugin, resize=True, shrink=True)
202 class CloseLabel(gtk.HBox):
203 def __init__(self, widget, caption):
204 gtk.HBox.__init__(self)
205 self.page = widget
207 self.label = gtk.Label(_('No Title'))
208 if type(caption) == str:
209 self.label.set_text(caption)
210 elif type(caption) == gtk.Label:
211 self.label.set_text(caption.get_text())
213 self.label.set_property("height-request", 20)
214 self.add(self.label)
216 self.button = gtk.Button("x")
217 self.button.connect("clicked", self.on_button_close)
218 self.button.set_property("height-request", 25)
220 self.add(self.button)
221 self.show_all()
223 def on_button_close(self, widget=None):
224 self.parent.remove(self.page)
227 class ResultView(gtk.Notebook):
228 def __init__(self, app):
229 gtk.Notebook.__init__(self)
230 self.app = app
231 # self.connect("page-added", self.check_hiding)
232 # self.connect("page-removed", self.check_hiding)
235 def check_hiding(self, notebook=None, child=None, page_num=None):
236 print self.get_n_pages()
237 if self.get_n_pages() == 0:
238 self.hide_all()
239 else:
240 self.show_all()
242 def add_results(self, result):
243 new_page = ResultViewPluginContainer(self.app, result)
244 self.append_page(new_page, None)
245 n=self.page_num(new_page) + 1
246 sql = result.sql.strip()
247 title = "%d: %s"%(n, sql)
248 self.set_tab_label(new_page, CloseLabel(new_page, title))
251 def add_widget(self, widget, label=None):
252 if label == None:
253 label = _('No Title')
254 self.append_page(widget, CloseLabel(widget,label))
256 def add_error(self, msg, title=None):
257 box = gtk.HBox()
259 scroll = gtk.ScrolledWindow()
260 txt = gtk.TextView()
261 txt.get_buffer().set_text(unicode(msg, pgw.get_user_encoding()))
262 pgw.set_proportional(txt.get_buffer())
263 txt.set_editable(False)
264 scroll.add(txt)
265 scroll.show_all()
266 if title:
267 title = title
268 else:
269 title = _('Error')
270 scroll.is_persistent = False
272 def anon_closure(widget=None):
273 self.remove(box)
274 button_close = gtk.Button("Close")
275 button_close.connect("clicked", anon_closure)
277 box.add(scroll)
278 box.add(button_close)
279 self.append_page(box, CloseLabel(box, title))
281 def append_page(self, widget, label):
282 gtk.Notebook.append_page(self, widget, label)
283 n = self.page_num(widget)
284 self.set_current_page(n)
286 def clear(self):
287 for child in self.get_children():
288 if not child.is_persistent:
289 self.remove(child)