2 # -*- coding: utf-8 -*-
5 This file is part of Multilist.
7 Multilist is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Multilist is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Multilist. If not, see <http://www.gnu.org/licenses/>.
20 Copyright (C) 2008 Christoph Würstle
38 _moduleLogger = logging.getLogger(__name__)
41 class Columns_dialog(gtk.VBox):
43 def __init__(self, db, liststorehandler):
44 gtk.VBox.__init__(self, homogeneous = False, spacing = 0)
47 self.liststorehandler = liststorehandler
49 #serverbutton = gtk.ToggleButton("SyncServer starten")
50 #serverbutton.connect("clicked", self.startServer, (None, ))
51 #self.pack_start(serverbutton, expand = False, fill = True, padding = 1)
54 frame = gtk.Frame(_("Columns"))
55 self.framebox = gtk.VBox(homogeneous = False, spacing = 0)
57 self.scrolled_window = gtk.ScrolledWindow()
58 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
60 self.scrolled_window.add_with_viewport(self.framebox)
62 i = 1 #uid can not be shown
63 while self.liststorehandler.get_colname(i) is not None:
64 name = str(self.liststorehandler.get_colname(i))
65 checkbutton = gtk.CheckButton(name)
66 if self.db.ladeDirekt("showcol_"+name) == "1":
67 checkbutton.set_active(True)
69 self.framebox.pack_start(checkbutton)
72 frame.add(self.scrolled_window)
73 self.pack_start(frame, expand = True, fill = True, padding = 1)
75 def is_col_selected(self, icol):
76 children = self.framebox.get_children()
77 if icol < len(children):
78 return children[icol].get_active()
82 def save_column_setting(self):
83 i = 1 #uid can not be shown
84 while self.liststorehandler.get_colname(i) is not None:
85 name = str(self.liststorehandler.get_colname(i))
86 if self.is_col_selected(i-1) == True:
87 self.db.speichereDirekt("showcol_"+name, "1")
89 self.db.speichereDirekt("showcol_"+name, "0")
93 class CellRendererTriple(gtk.GenericCellRenderer):
95 "status": (gobject.TYPE_STRING, "Status",
96 "Status", "", gobject.PARAM_READWRITE),
100 'status_changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_STRING)),
104 #self.__gobject_init__()
105 #gtk.GenericCellRenderer.__init__(self, *args, **kwargs)
106 gtk.GenericCellRenderer.__init__(self)
107 #self.__gobject_init__()
111 self.mode = gtk.CELL_RENDERER_MODE_ACTIVATABLE
118 self.last_cell = None
119 self.connect('editing-started', self.on_clicked)
121 def do_set_property(self, property, value):
122 setattr(self, property.name, value)
124 def do_get_property(self, property):
125 return getattr(self, property.name)
127 def get_layout(self, widget):
128 '''Gets the Pango layout used in the cell in a TreeView widget.'''
130 layout = pango.Layout(widget.get_pango_context())
131 layout.set_width(-1) # Do not wrap text.
137 def on_get_size(self, widget, cell_area = None):
144 layout = self.get_layout(widget)
145 width, height = layout.get_pixel_size()
152 x_offset = xalign * (cell_area.width - width)
153 x_offset = max(x_offset, xpad)
154 x_offset = int(round(x_offset, 0))
156 y_offset = yalign * (cell_area.height - height)
157 y_offset = max(y_offset, ypad)
158 y_offset = int(round(y_offset, 0))
160 width = width + (xpad * 2)
161 height = height + (ypad * 2)
163 return x_offset, y_offset, width, height
165 def on_clicked(self, widget, data):
168 def clicked(self, widget, data1 = None):
169 x, y = widget.get_pointer()
172 path = widget.get_path_at_pos(x, y)
174 #print "a", widget.get_cursor()
177 path = widget.get_cursor()[0]
180 irow = path[0] #path[0][0]-1
181 rect = widget.get_cell_area(irow, widget.get_column(0)) #FixME 0 is hardcoded
182 if x < rect.x+rect.width:
183 self.emit("status_changed", irow, self.status)
187 #workarround -1 means last item, because bug in treeview?!
189 rect = widget.get_visible_rect() #widget.get_cell_area(-1, widget.get_column(0))
190 #print rect.x, rect.y, rect.width, rect.height, x, y
192 rect = widget.get_cell_area(0, widget.get_column(0)) #FixME 0 is hardcoded
193 if x < rect.x+rect.width:
194 self.emit("status_changed", irow, "-1")
196 def on_render(self, window, widget, background_area, cell_area, expose_area, flags ):
197 if (self.widget == None):
200 self.widget.connect("cursor-changed", self.clicked) #button-press-event
202 self.last_cell = cell_area
204 x = int(cell_area.x+(cell_area.width-2)/2-(cell_area.height-2)/2)
205 y = int(cell_area.y+1)
206 height = int(cell_area.height-2)
209 if (self.status == "1"):
210 widget.style.paint_check(window, gtk.STATE_NORMAL, gtk.SHADOW_IN, cell_area, widget, "cellradio", x, y, width, height)
211 elif (self.status == "0"):
216 widget.style.paint_flat_box(window, gtk.STATE_NORMAL, gtk.SHADOW_NONE, cell_area, widget, "cellunselected", x, y, width, height)
218 widget.style.paint_hline(window, gtk.STATE_NORMAL, cell_area, widget, "cellunselected", x, x+width, y)
219 widget.style.paint_hline(window, gtk.STATE_NORMAL, cell_area, widget, "cellunselected", x, x+width, y+height)
220 widget.style.paint_vline(window, gtk.STATE_NORMAL, cell_area, widget, "cellunselected", y, y+height, x)
221 widget.style.paint_vline(window, gtk.STATE_NORMAL, cell_area, widget, "cellunselected", y, y+height, x+width)
224 widget.style.paint_diamond(window, gtk.STATE_NORMAL, gtk.SHADOW_IN, cell_area, widget, "cellunselected", x, y, width, height)
230 def on_start_editing(self, event, widget, path, background_area, cell_area, flags):
231 print "on_start_editing", path
234 def on_activate(self, event, widget, path, background_area, cell_area, flags):
235 print "activate", path
239 class CellRendererCombo2(gtk.GenericCellRenderer):
241 "text": (gobject.TYPE_STRING, "text",
242 "Text", "", gobject.PARAM_READWRITE),
246 'status_changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_STRING)),
250 #self.__gobject_init__()
251 #gtk.GenericCellRenderer.__init__(self, *args, **kwargs)
252 gtk.GenericCellRenderer.__init__(self)
253 #self.__gobject_init__()
257 self.mode = gtk.CELL_RENDERER_MODE_ACTIVATABLE
264 self.last_cell = None
266 self.connect('editing-started', self.on_clicked)
268 def do_set_property(self, property, value):
269 #print property, value
270 setattr(self, property.name, value)
272 def do_get_property(self, property):
273 return getattr(self, property.name)
275 def get_layout(self, widget):
276 '''Gets the Pango layout used in the cell in a TreeView widget.'''
278 layout = pango.Layout(widget.get_pango_context())
279 layout.set_width(-1) # Do not wrap text.
281 layout.set_text(self.text)
285 def on_get_size(self, widget, cell_area = None):
292 layout = self.get_layout(widget)
293 width, height = layout.get_pixel_size()
300 x_offset = xalign * (cell_area.width - width)
301 x_offset = max(x_offset, xpad)
302 x_offset = int(round(x_offset, 0))
304 y_offset = yalign * (cell_area.height - height)
305 y_offset = max(y_offset, ypad)
306 y_offset = int(round(y_offset, 0))
308 width = width + (xpad * 2)
309 height = height + (ypad * 2)
311 return x_offset, y_offset, width, height
313 @gtk_toolbox.log_exception(_moduleLogger)
314 def on_clicked(self, widget, data):
317 def clicked(self, widget, data1 = None):
319 x, y = widget.get_pointer()
322 #path = widget.get_path_at_pos(x, y)
324 path = widget.get_cursor()[0]
327 irow = path[0] #path[0][0]-1
328 rect = widget.get_cell_area(irow, widget.get_column(0)) #FixME 0 is hardcoded
329 if x < rect.x+rect.width:
330 self.emit("status_changed", irow, self.status)
334 #workarround -1 means last item, because bug in treeview?!
336 rect = widget.get_visible_rect() #widget.get_cell_area(-1, widget.get_column(0))
337 #print rect.x, rect.y, rect.width, rect.height, x, y
339 rect = widget.get_cell_area(0, widget.get_column(0)) #FixME 0 is hardcoded
340 if x < rect.x+rect.width:
341 self.emit("status_changed", irow, "-1")
343 def on_render(self, window, widget, background_area, cell_area, expose_area, flags ):
344 if (self.widget == None):
346 self.widget.connect("cursor-changed", self.clicked) #button-press-event
348 self.last_cell = cell_area
350 x = int(cell_area.x+(cell_area.width-2)/2-(cell_area.height-2)/2)
351 y = int(cell_area.y+1)
352 height = int(cell_area.height-2)
355 widget.style.paint_layout(window, gtk.STATE_NORMAL, True, cell_area, widget, "cellradio", x, y, self.get_layout(widget))
359 def on_start_editing(self, event, widget, path, background_area, cell_area, flags):
360 print "on_start_editing", path
363 def on_activate(self, event, widget, path, background_area, cell_area, flags):
364 print "activate", path
368 gobject.type_register(CellRendererCombo2)
369 gobject.type_register(CellRendererTriple)
372 class View(gtk.VBox):
374 def __init__(self, db, liststorehandler, parent_window):
376 self.parent_window = parent_window
377 self.liststorehandler = liststorehandler
379 gtk.VBox.__init__(self, homogeneous = False, spacing = 0)
381 logging.info("libview, init")
383 self.scrolled_window = None
387 bearbeitenFrame = gtk.Frame("Verteilung kopieren nach")
388 bearbeitenvBox = gtk.VBox(homogeneous = False, spacing = 0)
390 bearbeitenhBox = gtk.HBox(homogeneous = False, spacing = 0)
391 self.comboKlassen = gtk.combo_box_new_text()
392 bearbeitenhBox.pack_start(self.comboKlassen, expand = False, fill = True, padding = 0)
393 button = gtk.Button("Kopieren")
394 button.connect("clicked", self.kopiereStoffverteilung, None)
395 bearbeitenhBox.pack_start(button, expand = False, fill = True, padding = 0)
397 label = gtk.Label(" ")
398 bearbeitenhBox.pack_start(label, expand = False, fill = True, padding = 0)
400 button = gtk.Button("Export in CSV-Datei")
401 button.connect("clicked", self.exportStoffverteilung, None)
402 bearbeitenhBox.pack_start(button, expand = False, fill = True, padding = 0)
404 bearbeitenvBox.pack_start(bearbeitenhBox, expand = False, fill = True, padding = 0)
407 bearbeitenFrame.add(bearbeitenvBox)
408 self.pack_start(bearbeitenFrame, expand = False, fill = True, padding = 0)
411 #self.connect("unmap", self.speichere)
412 #self.connect("map", self.ladeWirklich)
416 #print "libstoffverteilung 9: ", time.clock()
419 ls = self.liststorehandler.get_liststore()
420 self.treeview.set_model(ls)
421 #self.tvcolumn[i].add_attribute( self.cell[i], "active", 1)
424 def col_edited(self, cell, irow, new_text, icol = None):
426 self.liststorehandler.update_row(irow, icol, new_text)
428 print cell, irow, new_text, icol
430 def col_toggled(self, widget, irow, status ):
431 #print irow, ls[irow][1], status
432 ls = self.treeview.get_model()
434 if self.liststorehandler.selection.get_status() == "0":
435 if ls[irow][1] == "0":
436 self.liststorehandler.update_row(irow, 1, "1")
438 self.liststorehandler.update_row(irow, 1, "0")
440 if ls[irow][1] == "1":
441 self.liststorehandler.update_row(irow, 1, "-1")
442 elif ls[irow][1] == "0":
443 self.liststorehandler.update_row(irow, 1, "1")
445 self.liststorehandler.update_row(irow, 1, "0")
447 #self.tvcolumn[i].set_attributes( self.cell[i], active = i)
449 def convert(self, s):
456 def del_active_row(self):
457 path, col = self.treeview.get_cursor()
461 row_iter = self.treeview.get_model().get_iter(path)
462 self.liststorehandler.del_row(irow, row_iter)
464 #treemodel.get_iter()
466 def sort_func_function(self, model, iter1, iter2, data = None):
469 def reload_view(self):
470 # create the TreeView using liststore
471 self.modelsort = gtk.TreeModelSort(self.liststorehandler.get_liststore())
472 self.modelsort.set_sort_column_id(2, gtk.SORT_ASCENDING)
474 self.treeview = gtk.TreeView(self.modelsort)
475 self.treeview.set_headers_visible(True)
477 self.cell = range(self.liststorehandler.get_colcount())
478 self.tvcolumn = range(self.liststorehandler.get_colcount())
480 m = self.liststorehandler.get_unitsstore()
482 for i in range(self.liststorehandler.get_colcount()):
487 if self.db.ladeDirekt("showcol_"+str(self.liststorehandler.get_colname(i)), default) == "1":
489 self.cell[i] = CellRendererTriple()
490 self.tvcolumn[i] = gtk.TreeViewColumn(self.liststorehandler.get_colname(i), self.cell[i])
491 self.cell[i].connect( 'status_changed', self.col_toggled)
492 self.tvcolumn[i].set_attributes( self.cell[i], status = i)
493 elif (i == 3)or(i == 4)or(i == 6):
494 self.cell[i] = gtk.CellRendererCombo()
495 self.tvcolumn[i] = gtk.TreeViewColumn(self.liststorehandler.get_colname(i), self.cell[i])
496 self.cell[i].set_property("model", m)
497 self.cell[i].set_property('text-column', i)
498 self.cell[i].set_property('editable', True)
499 self.cell[i].connect("edited", self.col_edited, i)
500 self.tvcolumn[i].set_attributes( self.cell[i], text = i)
502 self.cell[i] = gtk.CellRendererText()
503 self.tvcolumn[i] = gtk.TreeViewColumn(self.liststorehandler.get_colname(i), self.cell[i])
504 self.cell[i].set_property('editable', True)
505 self.cell[i].set_property('editable-set', True)
506 self.cell[i].connect("edited", self.col_edited, i)
507 #self.cell[i].connect("editing-canceled", self.col_edited2, i)
508 self.tvcolumn[i].set_attributes(self.cell[i], text = i)
510 self.cell[i].set_property('cell-background', 'lightgray')
511 self.tvcolumn[i].set_sort_column_id(i)
512 self.tvcolumn[i].set_resizable(True)
515 self.treeview.append_column(self.tvcolumn[i])
517 # Allow NOT drag and drop reordering of rows
518 self.treeview.set_reorderable(False)
520 if self.scrolled_window is not None:
521 self.scrolled_window.destroy()
523 self.scrolled_window = gtk.ScrolledWindow()
524 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
526 self.scrolled_window.add(self.treeview)
527 self.pack_start(self.scrolled_window, expand = True, fill = True, padding = 0)