Back to index

gcompris  8.2.2
group_list.py
Go to the documentation of this file.
00001 #  gcompris - group_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 group_user_list
00033 import group_edit
00034 
00035 import constants
00036 
00037 # Group Management
00038 (
00039   COLUMN_GROUPID,
00040   COLUMN_NAME,
00041   COLUMN_DESCRIPTION
00042 ) = range(3)
00043 
00044 class Group_list:
00045   """GCompris Group List Table"""
00046 
00047 
00048   # area is the drawing area for the list
00049   def __init__(self, frame, db_connect, db_cursor):
00050 
00051       self.cur = db_cursor
00052       self.con = db_connect
00053 
00054       # The class_id to work on
00055       self.current_class_id = 0
00056       self.class_list = []
00057 
00058       # The group_id selected
00059       self.current_group_id = 0
00060 
00061       # ---------------
00062       # Group Management
00063       # ---------------
00064 
00065       # create tree model
00066       self.group_model = self.__create_model_group()
00067 
00068       # Main box is vertical
00069       top_box = gtk.VBox(False, 8)
00070       top_box.show()
00071       frame.add(top_box)
00072 
00073       # First line label and combo
00074       label_box = gtk.HBox(False, 8)
00075       label_box.show()
00076       top_box.pack_start(label_box, False, False, 0)
00077 
00078 
00079       # Let the user select the class to work on
00080       #
00081       # Grab the class list and put it in a combo
00082       class_box = gtk.HBox(False, 8)
00083       class_box.show()
00084       label_box.pack_start(class_box, False, False, 0)
00085 
00086       class_label = gtk.Label(_('Select a class:'))
00087       class_label.show()
00088       label_box.pack_start(class_label, False, False, 0)
00089 
00090       self.cur.execute('SELECT * FROM class WHERE class_id>1 ORDER BY name')
00091       class_list = self.cur.fetchall()
00092 
00093       self.combo_class = gtk.combo_box_new_text()
00094       self.combo_class.show()
00095       for aclass in class_list:
00096         self.combo_class.append_text(aclass[1])
00097         # Save in a list the combo index => the class_id
00098         self.class_list.append(aclass[0])
00099 
00100       self.combo_class.set_active(self.current_class_id)
00101       label_box.pack_end(self.combo_class, True, True, 0)
00102 
00103       # Second line groups and button
00104       group_hbox = gtk.HBox(False, 8)
00105       group_hbox.show()
00106       top_box.add(group_hbox)
00107 
00108       grouplist_box = gtk.VBox(False, 8)
00109       grouplist_box.show()
00110       group_hbox.add(grouplist_box)
00111 
00112       vbox_button = gtk.VBox(False, 8)
00113       vbox_button.show()
00114       group_hbox.add(vbox_button)
00115 
00116 
00117       # Create the table
00118       sw = gtk.ScrolledWindow()
00119       sw.show()
00120       sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
00121       sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
00122 
00123       # create tree view
00124       treeview_group = gtk.TreeView(self.group_model)
00125       treeview_group.show()
00126       treeview_group.set_rules_hint(True)
00127       treeview_group.set_search_column(COLUMN_NAME)
00128 
00129       sw.add(treeview_group)
00130 
00131       grouplist_box.pack_start(sw, True, True, 0)
00132 
00133 
00134       # add columns to the tree view
00135       self.__add_columns_group(treeview_group)
00136 
00137       # Add buttons
00138       self.button_add = gtk.Button(stock='gtk-add')
00139       self.button_add.connect("clicked", self.on_add_group_clicked, self.group_model)
00140       vbox_button.pack_start(self.button_add, False, False, 0)
00141       self.button_add.show()
00142       self.button_add.set_sensitive(False)
00143 
00144       self.button_edit = gtk.Button(stock='gtk-edit')
00145       self.button_edit.connect("clicked", self.on_edit_group_clicked, treeview_group)
00146       vbox_button.pack_start(self.button_edit, False, False, 0)
00147       self.button_edit.show()
00148       self.button_edit.set_sensitive(False)
00149 
00150       self.button_remove = gtk.Button(stock='gtk-remove')
00151       self.button_remove.connect("clicked", self.on_remove_group_clicked, treeview_group)
00152       vbox_button.pack_start(self.button_remove, False, False, 0)
00153       self.button_remove.show()
00154       self.button_remove.set_sensitive(False)
00155 
00156       # User list for the group
00157       user_hbox = gtk.HBox(False, 8)
00158       user_hbox.show()
00159       top_box.add(user_hbox)
00160 
00161       self.group_user = group_user_list.Group_user_list(user_hbox,
00162                                                         self.con, self.cur,
00163                                                         self.current_group_id)
00164 
00165       # Missing callbacks
00166       self.combo_class.connect('changed', self.class_changed_cb)
00167       selection = treeview_group.get_selection()
00168       selection.connect('changed', self.group_changed_cb, self.group_user)
00169 
00170       # Load lists
00171       self.class_changed_cb(self.combo_class)
00172       self.reload_group()
00173 
00174   # -------------------
00175   # Group Management
00176   # -------------------
00177 
00178   # Update the group list area
00179   def reload_group(self):
00180 
00181     # Remove all entries in the list
00182     self.group_model.clear()
00183 
00184     # Grab the group data
00185     self.cur.execute('SELECT group_id, name, description FROM groups WHERE class_id=? ORDER BY name',
00186                      (self.current_class_id,))
00187     self.group_data = self.cur.fetchall()
00188 
00189     for agroup in self.group_data:
00190       self.add_group_in_model(self.group_model, agroup)
00191 
00192     self.group_user.reload(self.current_group_id)
00193 
00194 
00195   # Create the model for the group list
00196   def __create_model_group(self):
00197     model = gtk.ListStore(
00198       gobject.TYPE_INT,
00199       gobject.TYPE_STRING,
00200       gobject.TYPE_STRING,
00201       gobject.TYPE_BOOLEAN)
00202 
00203     return model
00204 
00205 
00206   def __add_columns_group(self, treeview):
00207 
00208     model = treeview.get_model()
00209 
00210     # columns for name
00211     renderer = gtk.CellRendererText()
00212     renderer.connect("edited", self.on_cell_group_edited, model)
00213     renderer.set_data("column", COLUMN_NAME)
00214     column = gtk.TreeViewColumn(_('Group'), renderer,
00215                                 text=COLUMN_NAME)
00216     column.set_sort_column_id(COLUMN_NAME)
00217     column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
00218     column.set_fixed_width(constants.COLUMN_WIDTH_GROUPNAME)
00219     treeview.append_column(column)
00220 
00221     # columns for description
00222     renderer = gtk.CellRendererText()
00223     renderer.connect("edited", self.on_cell_group_edited, model)
00224     renderer.set_data("column", COLUMN_DESCRIPTION)
00225     column = gtk.TreeViewColumn(_('Description'), renderer,
00226                                 text=COLUMN_DESCRIPTION)
00227     column.set_sort_column_id(COLUMN_DESCRIPTION)
00228     column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
00229     column.set_fixed_width(constants.COLUMN_WIDTH_GROUPDESCRIPTION)
00230     treeview.append_column(column)
00231 
00232 
00233   # Add group in the model
00234   def add_group_in_model(self, model, agroup):
00235     iter = model.append()
00236     model.set (iter,
00237                COLUMN_GROUPID,          agroup[COLUMN_GROUPID],
00238                COLUMN_NAME,             agroup[COLUMN_NAME],
00239                COLUMN_DESCRIPTION,      agroup[COLUMN_DESCRIPTION]
00240                )
00241 
00242 
00243   #
00244   def on_add_group_clicked(self, button, model):
00245     group_id = constants.get_next_group_id(self.con, self.cur)
00246 
00247     group_edit.GroupEdit(self.con, self.cur,
00248                          self.current_class_id, self.get_active_text(self.combo_class),
00249                          group_id, None, None,
00250                          self)
00251 
00252 
00253   def on_remove_group_clicked(self, button, treeview):
00254 
00255     selection = treeview.get_selection()
00256     model, iter = selection.get_selected()
00257 
00258     if iter:
00259       path = model.get_path(iter)[0]
00260       group_id = model.get_value(iter, COLUMN_GROUPID)
00261       model.remove(iter)
00262       # Remove it from the base
00263       self.cur.execute('delete from groups where group_id=?', (group_id,))
00264       self.con.commit()
00265 
00266     self.group_user.clear()
00267 
00268 
00269   def on_cell_group_edited(self, cell, path_string, new_text, model):
00270 
00271     iter = model.get_iter_from_string(path_string)
00272     path = model.get_path(iter)[0]
00273     column = cell.get_data("column")
00274 
00275     group_id = model.get_value(iter, COLUMN_GROUPID)
00276 
00277     if column == COLUMN_NAME:
00278       model.set(iter, column, new_text)
00279 
00280     elif column == COLUMN_DESCRIPTION:
00281       model.set(iter, column, new_text)
00282 
00283     group_data = (group_id,
00284                   self.current_class_id,
00285                   model.get_value(iter, COLUMN_NAME),
00286                   model.get_value(iter, COLUMN_DESCRIPTION))
00287     # Save the changes in the base
00288     self.cur.execute('insert or replace into groups (group_id, class_id, name, description) values (?, ?, ?, ?)',
00289                      group_data)
00290     self.con.commit()
00291 
00292 
00293   # Return the selected text in the given combobox
00294   def get_active_text(self, combobox):
00295       model = combobox.get_model()
00296       active = combobox.get_active()
00297       if active < 0:
00298         return None
00299       return model[active][0]
00300 
00301   def on_edit_group_clicked(self, button, treeview):
00302 
00303     selection = treeview.get_selection()
00304     model, iter = selection.get_selected()
00305 
00306     if iter:
00307       path = model.get_path(iter)[0]
00308       group_id          = model.get_value(iter, COLUMN_GROUPID)
00309       group_name        = model.get_value(iter, COLUMN_NAME)
00310       group_description = model.get_value(iter, COLUMN_DESCRIPTION)
00311       group_edit.GroupEdit(self.con, self.cur,
00312                            self.current_class_id, self.get_active_text(self.combo_class),
00313                            group_id, group_name,
00314                            group_description,
00315                            self)
00316 
00317     else:
00318       # Tell the user to select a group first
00319       dialog = gtk.MessageDialog(None,
00320                                  gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
00321                                  gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
00322                                  _("You must first select a group in the list"))
00323       dialog.run()
00324       dialog.destroy()
00325 
00326 
00327   def group_changed_cb(self, selection, group_user):
00328     model, iter = selection.get_selected()
00329 
00330     if iter:
00331       path = model.get_path(iter)[0]
00332       self.current_group_id = model.get_value(iter, COLUMN_GROUPID)
00333 
00334       group_user.reload(self.current_group_id)
00335 
00336       # Set the default button on if needed
00337 
00338       # The wholegroup is not editable
00339       wholegroup_id = constants.get_wholegroup_id(self.con,
00340                                                   self.cur,
00341                                                   self.current_class_id)
00342 
00343       if(wholegroup_id == self.current_group_id):
00344         self.button_edit.set_sensitive(False)
00345         self.button_remove.set_sensitive(False)
00346       else:
00347         self.button_edit.set_sensitive(True)
00348         self.button_remove.set_sensitive(True)
00349 
00350 
00351   def class_changed_cb(self, combobox):
00352     active = combobox.get_active()
00353     if active < 0:
00354       self.button_edit.set_sensitive(False)
00355       self.button_remove.set_sensitive(False)
00356       self.button_add.set_sensitive(False)
00357       return
00358 
00359     self.button_add.set_sensitive(True)
00360     self.current_class_id = self.class_list[active]
00361     self.reload_group()
00362