Back to index

gcompris  8.2.2
class_list.py
Go to the documentation of this file.
00001 #  gcompris - class_list.py
00002 #
00003 # Copyright (C) 2005 Bruno Coudoin and Yves Combe
00004 #
00005 #   This program is free software; you can redistribute it and/or modify
00006 #   it under the terms of the GNU General Public License as published by
00007 #   the Free Software Foundation; either version 2 of the License, or
00008 #   (at your option) any later version.
00009 #
00010 #   This program is distributed in the hope that it will be useful,
00011 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #   GNU General Public License for more details.
00014 #
00015 #   You should have received a copy of the GNU General Public License
00016 #   along with this program; if not, write to the Free Software
00017 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 
00020 import gnomecanvas
00021 import gcompris
00022 import gcompris.utils
00023 import gcompris.skin
00024 import gtk
00025 import gtk.gdk
00026 import gobject
00027 from gettext import gettext as _
00028 
00029 # Database
00030 from pysqlite2 import dbapi2 as sqlite
00031 
00032 import constants
00033 
00034 import class_edit
00035 import user_list
00036 
00037 # Class Management
00038 (
00039   COLUMN_CLASSID,
00040   COLUMN_NAME,
00041   COLUMN_TEACHER,
00042 ) = range(3)
00043 
00044 
00045 class Class_list:
00046   """GCompris Class List Table"""
00047 
00048 
00049   # area is the drawing area for the list
00050   def __init__(self, frame, db_connect, db_cursor):
00051 
00052       self.cur = db_cursor
00053       self.con = db_connect
00054 
00055       self.class_data = []
00056 
00057       # ---------------
00058       # Class Management
00059       # ---------------
00060 
00061       # create tree model
00062       model = self.__create_model_class()
00063 
00064       # Main box is vertical
00065       top_box = gtk.VBox(False, 8)
00066       top_box.show()
00067       frame.add(top_box)
00068 
00069       # First line
00070       group_hbox = gtk.HBox(False, 8)
00071       group_hbox.show()
00072       top_box.add(group_hbox)
00073 
00074       left_box = gtk.VBox(False, 8)
00075       left_box.show()
00076       group_hbox.add(left_box)
00077 
00078       right_box = gtk.VBox(False, 8)
00079       right_box.show()
00080       group_hbox.add(right_box)
00081 
00082 
00083       # Create the table
00084       sw = gtk.ScrolledWindow()
00085       sw.show()
00086       sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
00087       sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
00088 
00089       # create tree view
00090       self.treeview_class = gtk.TreeView(model)
00091       self.treeview_class.show()
00092       self.treeview_class.set_rules_hint(True)
00093       self.treeview_class.set_search_column(COLUMN_NAME)
00094 
00095       sw.add(self.treeview_class)
00096 
00097       left_box.pack_start(sw, True, True, 0)
00098 
00099       # add columns to the tree view
00100       self.__add_columns_class(self.treeview_class)
00101 
00102       # Add buttons
00103 
00104       button = gtk.Button(stock='gtk-add')
00105       button.connect("clicked", self.on_add_class_clicked, self.treeview_class)
00106       right_box.pack_start(button, False, False, 0)
00107       button.show()
00108 
00109       self.button_edit = gtk.Button(stock='gtk-edit')
00110       self.button_edit.connect("clicked", self.on_edit_class_clicked, self.treeview_class)
00111       right_box.pack_start(self.button_edit, False, False, 0)
00112       self.button_edit.show()
00113       # Not editable until one class is selected
00114       self.button_edit.set_sensitive(False)
00115 
00116       self.button_remove = gtk.Button(stock='gtk-remove')
00117       self.button_remove.connect("clicked", self.on_remove_class_clicked, self.treeview_class)
00118       right_box.pack_start(self.button_remove, False, False, 0)
00119       self.button_remove.show()
00120       # Not removable until one class is selected
00121       self.button_remove.set_sensitive(False)
00122 
00123       # User list for the group
00124       user_hbox = gtk.HBox(False, 8)
00125       user_hbox.show()
00126       top_box.add(user_hbox)
00127 
00128 
00129       self.list_user = user_list.User_list(user_hbox,
00130                                            self.con, self.cur)
00131 
00132       # Missing callbacks
00133       selection = self.treeview_class.get_selection()
00134       selection.connect('changed', self.class_changed_cb, self.list_user)
00135 
00136 
00137   # -------------------
00138   # Class Management
00139   # -------------------
00140 
00141   def __create_model_class(self):
00142     model = gtk.ListStore(
00143       gobject.TYPE_INT,
00144       gobject.TYPE_STRING,
00145       gobject.TYPE_STRING,
00146       gobject.TYPE_BOOLEAN)
00147 
00148     # Grab the class data
00149     self.cur.execute('select * from class')
00150     self.class_data = self.cur.fetchall()
00151 
00152     for item in self.class_data:
00153       iter = model.append()
00154       model.set(iter,
00155                  COLUMN_CLASSID,   item[COLUMN_CLASSID],
00156                  COLUMN_NAME,      item[COLUMN_NAME],
00157                  COLUMN_TEACHER,   item[COLUMN_TEACHER])
00158     return model
00159 
00160 
00161   def __add_columns_class(self, treeview):
00162     model = treeview.get_model()
00163 
00164     # Total column length must be 400
00165 
00166     # columns for name
00167     renderer = gtk.CellRendererText()
00168     renderer.set_data("column", COLUMN_NAME)
00169     column = gtk.TreeViewColumn(_('Class'), renderer,
00170                                 text=COLUMN_NAME)
00171     column.set_sort_column_id(COLUMN_NAME)
00172     column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
00173     column.set_fixed_width(constants.COLUMN_WIDTH_CLASSNAME)
00174     treeview.append_column(column)
00175 
00176     # columns for teacher
00177     renderer = gtk.CellRendererText()
00178     renderer.set_data("column", COLUMN_TEACHER)
00179     column = gtk.TreeViewColumn(_('Teacher'), renderer,
00180                                 text=COLUMN_TEACHER)
00181     column.set_sort_column_id(COLUMN_TEACHER)
00182     column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
00183     column.set_fixed_width(constants.COLUMN_WIDTH_TEACHER)
00184     treeview.append_column(column)
00185 
00186 
00187   # Add class in the model
00188   def add_class_in_model(self, model, aclass):
00189     iter = model.append()
00190     model.set (iter,
00191                COLUMN_CLASSID,    aclass[COLUMN_CLASSID],
00192                COLUMN_NAME,       aclass[COLUMN_NAME],
00193                COLUMN_TEACHER,    aclass[COLUMN_TEACHER]
00194                )
00195 
00196 
00197   def on_remove_class_clicked(self, button, treeview):
00198     selection = treeview.get_selection()
00199     model, iter = selection.get_selected()
00200 
00201     if iter:
00202       path = model.get_path(iter)[0]
00203       class_id = model.get_value(iter, COLUMN_CLASSID)
00204       model.remove(iter)
00205 
00206       #
00207       # Remove it from the base (Triggers maintains other tables)
00208       #
00209       print "Deleting class_id=" + str(class_id)
00210       self.cur.execute('DELETE FROM class WHERE class_id=?', (class_id,))
00211       self.con.commit()
00212 
00213       print "class_list:remove_class_clicked calling reload"
00214       self.list_user.reload(class_id)
00215 
00216 
00217   def on_edit_class_clicked(self, button, treeview):
00218     selection = treeview.get_selection()
00219     model, iter = selection.get_selected()
00220 
00221     if iter:
00222       path = model.get_path(iter)[0]
00223       class_id      = model.get_value(iter, COLUMN_CLASSID)
00224       class_name    = model.get_value(iter, COLUMN_NAME)
00225       teacher_name  = model.get_value(iter, COLUMN_TEACHER)
00226       class_edit.ClassEdit(self.con, self.cur,
00227                            class_id, class_name, teacher_name,
00228                            self)
00229 
00230 
00231   def on_add_class_clicked(self, button, treeview):
00232     model = treeview.get_model()
00233     class_id = constants.get_next_class_id(self.con, self.cur)
00234 
00235     class_edit.ClassEdit(self.con, self.cur,
00236                          class_id, "", "",
00237                          self)
00238 
00239 
00240   # The class is changed ...
00241   def class_changed_cb(self, selection, user_list):
00242     model, iter = selection.get_selected()
00243 
00244     if iter:
00245       path = model.get_path(iter)[0]
00246       class_id = model.get_value(iter, COLUMN_CLASSID)
00247       print "class_changed_cb %d" %class_id
00248       user_list.reload(class_id)
00249 
00250       # The Unaffected class is not editable.
00251       if class_id == 1:
00252         self.button_edit.set_sensitive(False)
00253         self.button_remove.set_sensitive(False)
00254       else:
00255         self.button_edit.set_sensitive(True)
00256         self.button_remove.set_sensitive(True)
00257 
00258 
00259   # Reload data (class data and users)
00260   def reload(self, class_id, class_name, class_teacher):
00261     print ">> class_list:reload %d" %class_id
00262     print "class_name %s" %class_name
00263     # We need to find the row matching this class_id.
00264     # If not found, it's a new class to create
00265     model = self.treeview_class.get_model()
00266     iter = model.get_iter_first()
00267 
00268     updated = False
00269     # Loop over each class raw
00270     while(iter):
00271       path = model.get_path(iter)[0]
00272       tmp_class_id = model.get_value(iter, COLUMN_CLASSID)
00273 
00274       if(tmp_class_id == class_id):
00275 
00276         # Now update the class_name and class_teacher if provided
00277         if class_name:
00278           print "updating %s" %class_name
00279           print dir(class_name)
00280           model.set(iter, COLUMN_NAME, class_name)
00281 
00282         if class_teacher:
00283           model.set(iter, COLUMN_TEACHER, class_teacher)
00284 
00285         updated = True
00286         # It's updated now
00287         break
00288 
00289       iter = model.iter_next(iter)
00290 
00291 
00292 
00293     # The job not done yet, it's a new class.
00294     if(not updated):
00295       new_class = [class_id, class_name, class_teacher]
00296       self.add_class_in_model(model, new_class)
00297 
00298     # Reload the selected class
00299     selection = self.treeview_class.get_selection()
00300     if(selection):
00301       print "got selection"
00302       model, iter = selection.get_selected()
00303       if iter:
00304         path = model.get_path(iter)[0]
00305         sel_class_id = model.get_value(iter, COLUMN_CLASSID)
00306         print "got selection %d" %sel_class_id
00307         self.list_user.reload(sel_class_id)
00308 
00309     print "class_list reload DONE"
00310