Back to index

gcompris  8.2.2
profile_list.py
Go to the documentation of this file.
00001 #  gcompris - profile_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 profile_group_list
00033 import profile_edit
00034 
00035 import constants
00036 
00037 # Profile Management
00038 (
00039   COLUMN_PROFILEID,
00040   COLUMN_NAME,
00041   COLUMN_DESCRIPTION
00042 ) = range(3)
00043 
00044 class Profile_list:
00045   """GCompris Profile 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 profile_id to work on
00055       self.current_profile_id = 0
00056 
00057       # ---------------
00058       # Profile Management
00059       # ---------------
00060 
00061       # create tree model
00062       self.profile_model = self.__create_model_profile()
00063 
00064       # Main box is vertical
00065       top_box = gtk.VBox(False, 8)
00066       top_box.show()
00067       frame.add(top_box)
00068 
00069       # Second line profiles and button
00070       profile_hbox = gtk.HBox(False, 8)
00071       profile_hbox.show()
00072       top_box.add(profile_hbox)
00073 
00074       profilelist_box = gtk.VBox(False, 8)
00075       profilelist_box.show()
00076       profile_hbox.add(profilelist_box)
00077 
00078       vbox_button = gtk.VBox(False, 8)
00079       vbox_button.show()
00080       profile_hbox.add(vbox_button)
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       treeview_profile = gtk.TreeView(self.profile_model)
00091       treeview_profile.show()
00092       treeview_profile.set_rules_hint(True)
00093       treeview_profile.set_search_column(COLUMN_NAME)
00094 
00095       sw.add(treeview_profile)
00096 
00097       profilelist_box.pack_start(sw, True, True, 0)
00098 
00099 
00100       # add columns to the tree view
00101       self.__add_columns_profile(treeview_profile)
00102 
00103       # Add buttons
00104       button = gtk.Button(stock='gtk-add')
00105       button.connect("clicked", self.on_add_profile_clicked, self.profile_model)
00106       vbox_button.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_profile_clicked, treeview_profile)
00111       vbox_button.pack_start(self.button_edit, False, False, 0)
00112       self.button_edit.show()
00113       self.button_edit.set_sensitive(False)
00114 
00115       self.button_remove = gtk.Button(stock='gtk-remove')
00116       self.button_remove.connect("clicked", self.on_remove_profile_clicked, treeview_profile)
00117       vbox_button.pack_start(self.button_remove, False, False, 0)
00118       self.button_remove.show()
00119       self.button_remove.set_sensitive(False)
00120 
00121       self.button_default = gtk.Button(_("Default"))
00122       self.button_default.connect("clicked", self.on_default_profile_clicked, treeview_profile)
00123       vbox_button.pack_start(self.button_default, False, False, 0)
00124       self.button_default.show()
00125       self.button_default.set_sensitive(False)
00126 
00127       # Group list for the profile
00128       group_hbox = gtk.HBox(False, 8)
00129       group_hbox.show()
00130       top_box.add(group_hbox)
00131 
00132       self.profile_group = profile_group_list.Profile_group_list(group_hbox,
00133                                                         self.con, self.cur,
00134                                                         self.current_profile_id)
00135 
00136       # Missing callbacks
00137       selection = treeview_profile.get_selection()
00138       selection.connect('changed', self.profile_changed_cb, self.profile_group)
00139 
00140       # Load lists
00141       self.reload_profile()
00142 
00143   # -------------------
00144   # Profile Management
00145   # -------------------
00146 
00147   # Update the profile list area
00148   def reload_profile(self):
00149 
00150     # Remove all entries in the list
00151     self.profile_model.clear()
00152 
00153     # Get the default profile
00154     self.cur.execute('select * from informations')
00155     self.default_profile_id = self.cur.fetchall()[0][2]
00156     self.default_profile_id_iter = None
00157     self.default_profile_id_description = None
00158 
00159     # Grab the profile data
00160     self.cur.execute('SELECT profile_id, name, description FROM profiles ORDER BY name')
00161     self.profile_data = self.cur.fetchall()
00162 
00163     for aprofile in self.profile_data:
00164       self.add_profile_in_model(self.profile_model, aprofile)
00165 
00166     self.profile_group.reload(self.current_profile_id)
00167 
00168 
00169 
00170   # Create the model for the profile list
00171   def __create_model_profile(self):
00172     model = gtk.ListStore(
00173       gobject.TYPE_INT,
00174       gobject.TYPE_STRING,
00175       gobject.TYPE_STRING,
00176       gobject.TYPE_BOOLEAN)
00177 
00178     return model
00179 
00180 
00181   def __add_columns_profile(self, treeview):
00182 
00183     model = treeview.get_model()
00184 
00185     # columns for name
00186     renderer = gtk.CellRendererText()
00187     renderer.set_data("column", COLUMN_NAME)
00188     column = gtk.TreeViewColumn(_('Profile'), renderer,
00189                                 text=COLUMN_NAME)
00190     column.set_sort_column_id(COLUMN_NAME)
00191     column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
00192     column.set_fixed_width(constants.COLUMN_WIDTH_PROFILENAME)
00193     treeview.append_column(column)
00194 
00195     # columns for description
00196     renderer = gtk.CellRendererText()
00197     renderer.set_data("column", COLUMN_DESCRIPTION)
00198     column = gtk.TreeViewColumn(_('Description'), renderer,
00199                                 text=COLUMN_DESCRIPTION)
00200     column.set_sort_column_id(COLUMN_DESCRIPTION)
00201     column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
00202     column.set_fixed_width(constants.COLUMN_WIDTH_PROFILEDESCRIPTION)
00203     treeview.append_column(column)
00204 
00205 
00206   # Add profile in the model
00207   def add_profile_in_model(self, model, aprofile):
00208     iter = model.append()
00209     model.set (iter,
00210                COLUMN_PROFILEID,          aprofile[COLUMN_PROFILEID],
00211                COLUMN_NAME,               aprofile[COLUMN_NAME],
00212                COLUMN_DESCRIPTION,        aprofile[COLUMN_DESCRIPTION]
00213                )
00214 
00215     if self.default_profile_id ==  aprofile[COLUMN_PROFILEID]:
00216       self.set_default_in_description(iter)
00217 
00218   #
00219   def on_add_profile_clicked(self, button, model):
00220 
00221     profile_id = constants.get_next_profile_id(self.con, self.cur)
00222 
00223     profile_edit.ProfileEdit(self.con, self.cur,
00224                              profile_id, None, None,
00225                              self)
00226 
00227 
00228   def on_remove_profile_clicked(self, button, treeview):
00229 
00230     selection = treeview.get_selection()
00231     model, iter = selection.get_selected()
00232 
00233     if iter:
00234       path = model.get_path(iter)[0]
00235       profile_id = model.get_value(iter, COLUMN_PROFILEID)
00236 
00237       model.remove(iter)
00238       # Remove it from the base
00239       self.cur.execute('DELETE FROM profiles WHERE profile_id=?', (profile_id,))
00240 
00241       # Remove it from list_groups_in_profiles
00242       self.cur.execute('DELETE FROM list_groups_in_profiles ' +
00243                        'WHERE profile_id=?',
00244                        (profile_id,))
00245 
00246       self.con.commit()
00247 
00248       self.profile_group.reload(-1)
00249 
00250 
00251   def on_cell_profile_edited(self, cell, path_string, new_text, model):
00252 
00253     iter = model.get_iter_from_string(path_string)
00254     path = model.get_path(iter)[0]
00255     column = cell.get_data("column")
00256 
00257     profile_id = model.get_value(iter, COLUMN_PROFILEID)
00258 
00259     if column == COLUMN_NAME:
00260       model.set(iter, column, new_text)
00261 
00262     elif column == COLUMN_DESCRIPTION:
00263       model.set(iter, column, new_text)
00264 
00265     profile_data = (profile_id,
00266                     model.get_value(iter, COLUMN_NAME),
00267                     model.get_value(iter, COLUMN_DESCRIPTION))
00268     # Save the changes in the base
00269     self.cur.execute('insert or replace into profiles (profile_id, name, description) values (?, ?, ?)',
00270                      profile_data)
00271     self.con.commit()
00272 
00273 
00274   def on_edit_profile_clicked(self, button, treeview):
00275 
00276     selection = treeview.get_selection()
00277     model, iter = selection.get_selected()
00278 
00279     if iter:
00280       path = model.get_path(iter)[0]
00281       profile_id          = model.get_value(iter, COLUMN_PROFILEID)
00282       profile_name        = model.get_value(iter, COLUMN_NAME)
00283       profile_description = model.get_value(iter, COLUMN_DESCRIPTION)
00284       profile_edit.ProfileEdit(self.con, self.cur,
00285                                profile_id, profile_name, profile_description,
00286                                self)
00287 
00288 
00289 
00290   # Set the default profile in the 'informations' table
00291   def on_default_profile_clicked(self, button, treeview):
00292     selection = treeview.get_selection()
00293     model, iter = selection.get_selected()
00294 
00295     if iter:
00296       path = model.get_path(iter)[0]
00297       profile_id   = model.get_value(iter, COLUMN_PROFILEID)
00298 
00299       self.default_profile_id = profile_id
00300       self.button_default.set_sensitive(False)
00301 
00302       # Save the changes in the base
00303       self.cur.execute('UPDATE informations SET profile_id=?',
00304                        (profile_id,))
00305       self.con.commit()
00306 
00307       self.set_default_in_description(iter)
00308 
00309   # Write an additional [Default] in the profile description
00310   def set_default_in_description(self, iter):
00311       # set default in description
00312       description  = self.profile_model.get_value(iter, COLUMN_DESCRIPTION)
00313 
00314       self.profile_model.set (iter,
00315                               COLUMN_DESCRIPTION,        description + " " + _("[Default]"),
00316                               )
00317 
00318       # There was a previous default TAG, erase it
00319       if self.default_profile_id_iter:
00320         self.profile_model.set (self.default_profile_id_iter,
00321                                 COLUMN_DESCRIPTION, self.default_profile_id_description,
00322                                 )
00323 
00324       # Save the default profile iter to erase it's "default" tag if needed
00325       self.default_profile_id_iter        = iter
00326       self.default_profile_id_description = description
00327 
00328 
00329   #
00330   # Called on profile change
00331   #
00332   def profile_changed_cb(self, selection, profile_group):
00333     model, iter = selection.get_selected()
00334 
00335     if iter:
00336       self.current_profile_id = model.get_value(iter, COLUMN_PROFILEID)
00337 
00338       profile_group.reload(self.current_profile_id)
00339 
00340       self.button_edit.set_sensitive(True)
00341       self.button_remove.set_sensitive(True)
00342 
00343       # Set the default button on if needed
00344       if(self.default_profile_id == self.current_profile_id):
00345         self.button_default.set_sensitive(False)
00346       else:
00347         self.button_default.set_sensitive(True)
00348 
00349       if(self.current_profile_id == 1):
00350          self.button_remove.set_sensitive(False)
00351       else:
00352          self.button_remove.set_sensitive(True)
00353