1
# gcompris - group_list.py
3
# Copyright (C) 2005 Bruno Coudoin and Yves Combe
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
from gettext import gettext as _
30
from pysqlite2 import dbapi2 as sqlite
32
import group_user_list
45
"""GCompris Group List Table"""
48
# area is the drawing area for the list
49
def __init__(self, frame, db_connect, db_cursor):
54
# The class_id to work on
55
self.current_class_id = 0
58
# The group_id selected
59
self.current_group_id = 0
66
self.group_model = self.__create_model_group()
68
# Main box is vertical
69
top_box = gtk.VBox(False, 8)
73
# First line label and combo
74
label_box = gtk.HBox(False, 8)
76
top_box.pack_start(label_box, False, False, 0)
79
# Let the user select the class to work on
81
# Grab the class list and put it in a combo
82
class_box = gtk.HBox(False, 8)
84
label_box.pack_start(class_box, False, False, 0)
86
class_label = gtk.Label(_('Select a class:'))
88
label_box.pack_start(class_label, False, False, 0)
90
self.cur.execute('SELECT * FROM class WHERE class_id>1 ORDER BY name')
91
class_list = self.cur.fetchall()
93
self.combo_class = gtk.combo_box_new_text()
94
self.combo_class.show()
95
for aclass in class_list:
96
self.combo_class.append_text(aclass[1])
97
# Save in a list the combo index => the class_id
98
self.class_list.append(aclass[0])
100
self.combo_class.set_active(self.current_class_id)
101
label_box.pack_end(self.combo_class, True, True, 0)
103
# Second line groups and button
104
group_hbox = gtk.HBox(False, 8)
106
top_box.add(group_hbox)
108
grouplist_box = gtk.VBox(False, 8)
110
group_hbox.add(grouplist_box)
112
vbox_button = gtk.VBox(False, 8)
114
group_hbox.add(vbox_button)
118
sw = gtk.ScrolledWindow()
120
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
121
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
124
treeview_group = gtk.TreeView(self.group_model)
125
treeview_group.show()
126
treeview_group.set_rules_hint(True)
127
treeview_group.set_search_column(COLUMN_NAME)
129
sw.add(treeview_group)
131
grouplist_box.pack_start(sw, True, True, 0)
134
# add columns to the tree view
135
self.__add_columns_group(treeview_group)
138
self.button_add = gtk.Button(stock='gtk-add')
139
self.button_add.connect("clicked", self.on_add_group_clicked, self.group_model)
140
vbox_button.pack_start(self.button_add, False, False, 0)
141
self.button_add.show()
142
self.button_add.set_sensitive(False)
144
self.button_edit = gtk.Button(stock='gtk-edit')
145
self.button_edit.connect("clicked", self.on_edit_group_clicked, treeview_group)
146
vbox_button.pack_start(self.button_edit, False, False, 0)
147
self.button_edit.show()
148
self.button_edit.set_sensitive(False)
150
self.button_remove = gtk.Button(stock='gtk-remove')
151
self.button_remove.connect("clicked", self.on_remove_group_clicked, treeview_group)
152
vbox_button.pack_start(self.button_remove, False, False, 0)
153
self.button_remove.show()
154
self.button_remove.set_sensitive(False)
156
# User list for the group
157
user_hbox = gtk.HBox(False, 8)
159
top_box.add(user_hbox)
161
self.group_user = group_user_list.Group_user_list(user_hbox,
163
self.current_group_id)
166
self.combo_class.connect('changed', self.class_changed_cb)
167
selection = treeview_group.get_selection()
168
selection.connect('changed', self.group_changed_cb, self.group_user)
171
self.class_changed_cb(self.combo_class)
174
# -------------------
176
# -------------------
178
# Update the group list area
179
def reload_group(self):
181
# Remove all entries in the list
182
self.group_model.clear()
184
# Grab the group data
185
self.cur.execute('SELECT group_id, name, description FROM groups WHERE class_id=? ORDER BY name',
186
(self.current_class_id,))
187
self.group_data = self.cur.fetchall()
189
for agroup in self.group_data:
190
self.add_group_in_model(self.group_model, agroup)
192
self.group_user.reload(self.current_group_id)
195
# Create the model for the group list
196
def __create_model_group(self):
197
model = gtk.ListStore(
201
gobject.TYPE_BOOLEAN)
206
def __add_columns_group(self, treeview):
208
model = treeview.get_model()
211
renderer = gtk.CellRendererText()
212
renderer.connect("edited", self.on_cell_group_edited, model)
213
renderer.set_data("column", COLUMN_NAME)
214
column = gtk.TreeViewColumn(_('Group'), renderer,
216
column.set_sort_column_id(COLUMN_NAME)
217
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
218
column.set_fixed_width(constants.COLUMN_WIDTH_GROUPNAME)
219
treeview.append_column(column)
221
# columns for description
222
renderer = gtk.CellRendererText()
223
renderer.connect("edited", self.on_cell_group_edited, model)
224
renderer.set_data("column", COLUMN_DESCRIPTION)
225
column = gtk.TreeViewColumn(_('Description'), renderer,
226
text=COLUMN_DESCRIPTION)
227
column.set_sort_column_id(COLUMN_DESCRIPTION)
228
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
229
column.set_fixed_width(constants.COLUMN_WIDTH_GROUPDESCRIPTION)
230
treeview.append_column(column)
233
# Add group in the model
234
def add_group_in_model(self, model, agroup):
235
iter = model.append()
237
COLUMN_GROUPID, agroup[COLUMN_GROUPID],
238
COLUMN_NAME, agroup[COLUMN_NAME],
239
COLUMN_DESCRIPTION, agroup[COLUMN_DESCRIPTION]
244
def on_add_group_clicked(self, button, model):
245
group_id = constants.get_next_group_id(self.con, self.cur)
247
group_edit.GroupEdit(self.con, self.cur,
248
self.current_class_id, self.get_active_text(self.combo_class),
249
group_id, None, None,
253
def on_remove_group_clicked(self, button, treeview):
255
selection = treeview.get_selection()
256
model, iter = selection.get_selected()
259
path = model.get_path(iter)[0]
260
group_id = model.get_value(iter, COLUMN_GROUPID)
262
# Remove it from the base
263
self.cur.execute('delete from groups where group_id=?', (group_id,))
266
self.group_user.clear()
269
def on_cell_group_edited(self, cell, path_string, new_text, model):
271
iter = model.get_iter_from_string(path_string)
272
path = model.get_path(iter)[0]
273
column = cell.get_data("column")
275
group_id = model.get_value(iter, COLUMN_GROUPID)
277
if column == COLUMN_NAME:
278
model.set(iter, column, new_text)
280
elif column == COLUMN_DESCRIPTION:
281
model.set(iter, column, new_text)
283
group_data = (group_id,
284
self.current_class_id,
285
model.get_value(iter, COLUMN_NAME),
286
model.get_value(iter, COLUMN_DESCRIPTION))
287
# Save the changes in the base
288
self.cur.execute('insert or replace into groups (group_id, class_id, name, description) values (?, ?, ?, ?)',
293
# Return the selected text in the given combobox
294
def get_active_text(self, combobox):
295
model = combobox.get_model()
296
active = combobox.get_active()
299
return model[active][0]
301
def on_edit_group_clicked(self, button, treeview):
303
selection = treeview.get_selection()
304
model, iter = selection.get_selected()
307
path = model.get_path(iter)[0]
308
group_id = model.get_value(iter, COLUMN_GROUPID)
309
group_name = model.get_value(iter, COLUMN_NAME)
310
group_description = model.get_value(iter, COLUMN_DESCRIPTION)
311
group_edit.GroupEdit(self.con, self.cur,
312
self.current_class_id, self.get_active_text(self.combo_class),
313
group_id, group_name,
318
# Tell the user to select a group first
319
dialog = gtk.MessageDialog(None,
320
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
321
gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
322
_("You must first select a group in the list"))
327
def group_changed_cb(self, selection, group_user):
328
model, iter = selection.get_selected()
331
path = model.get_path(iter)[0]
332
self.current_group_id = model.get_value(iter, COLUMN_GROUPID)
334
group_user.reload(self.current_group_id)
336
# Set the default button on if needed
338
# The wholegroup is not editable
339
wholegroup_id = constants.get_wholegroup_id(self.con,
341
self.current_class_id)
343
if(wholegroup_id == self.current_group_id):
344
self.button_edit.set_sensitive(False)
345
self.button_remove.set_sensitive(False)
347
self.button_edit.set_sensitive(True)
348
self.button_remove.set_sensitive(True)
351
def class_changed_cb(self, combobox):
352
active = combobox.get_active()
354
self.button_edit.set_sensitive(False)
355
self.button_remove.set_sensitive(False)
356
self.button_add.set_sensitive(False)
359
self.button_add.set_sensitive(True)
360
self.current_class_id = self.class_list[active]