~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/boards/python/admin/group_list.py

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2006-12-15 23:08:17 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215230817-exr5ks1hd73s3tlk
Tags: 8.2.2-1
* New upstream bugfix release, fixes among other things the support for
  the version of gnucap shipped in etch.
* Add missing dependency on python-gtk2 (Closes: #396523).
* Removed reference to non-existent sound file from memory.c (upstream
  fix - impacts 8.2 as well).  
* Now suggests gnuchess, gnucap, and tuxpaint.
* Updated extended description for the main package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  gcompris - group_list.py
 
2
#
 
3
# Copyright (C) 2005 Bruno Coudoin and Yves Combe
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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
 
18
#
 
19
 
 
20
import gnomecanvas
 
21
import gcompris
 
22
import gcompris.utils
 
23
import gcompris.skin
 
24
import gtk
 
25
import gtk.gdk
 
26
import gobject
 
27
from gettext import gettext as _
 
28
 
 
29
# Database
 
30
from pysqlite2 import dbapi2 as sqlite
 
31
 
 
32
import group_user_list
 
33
import group_edit
 
34
 
 
35
import constants
 
36
 
 
37
# Group Management
 
38
(
 
39
  COLUMN_GROUPID,
 
40
  COLUMN_NAME,
 
41
  COLUMN_DESCRIPTION
 
42
) = range(3)
 
43
 
 
44
class Group_list:
 
45
  """GCompris Group List Table"""
 
46
 
 
47
 
 
48
  # area is the drawing area for the list
 
49
  def __init__(self, frame, db_connect, db_cursor):
 
50
 
 
51
      self.cur = db_cursor
 
52
      self.con = db_connect
 
53
 
 
54
      # The class_id to work on
 
55
      self.current_class_id = 0
 
56
      self.class_list = []
 
57
 
 
58
      # The group_id selected
 
59
      self.current_group_id = 0
 
60
 
 
61
      # ---------------
 
62
      # Group Management
 
63
      # ---------------
 
64
 
 
65
      # create tree model
 
66
      self.group_model = self.__create_model_group()
 
67
 
 
68
      # Main box is vertical
 
69
      top_box = gtk.VBox(False, 8)
 
70
      top_box.show()
 
71
      frame.add(top_box)
 
72
 
 
73
      # First line label and combo
 
74
      label_box = gtk.HBox(False, 8)
 
75
      label_box.show()
 
76
      top_box.pack_start(label_box, False, False, 0)
 
77
 
 
78
 
 
79
      # Let the user select the class to work on
 
80
      #
 
81
      # Grab the class list and put it in a combo
 
82
      class_box = gtk.HBox(False, 8)
 
83
      class_box.show()
 
84
      label_box.pack_start(class_box, False, False, 0)
 
85
 
 
86
      class_label = gtk.Label(_('Select a class:'))
 
87
      class_label.show()
 
88
      label_box.pack_start(class_label, False, False, 0)
 
89
 
 
90
      self.cur.execute('SELECT * FROM class WHERE class_id>1 ORDER BY name')
 
91
      class_list = self.cur.fetchall()
 
92
 
 
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])
 
99
 
 
100
      self.combo_class.set_active(self.current_class_id)
 
101
      label_box.pack_end(self.combo_class, True, True, 0)
 
102
 
 
103
      # Second line groups and button
 
104
      group_hbox = gtk.HBox(False, 8)
 
105
      group_hbox.show()
 
106
      top_box.add(group_hbox)
 
107
 
 
108
      grouplist_box = gtk.VBox(False, 8)
 
109
      grouplist_box.show()
 
110
      group_hbox.add(grouplist_box)
 
111
 
 
112
      vbox_button = gtk.VBox(False, 8)
 
113
      vbox_button.show()
 
114
      group_hbox.add(vbox_button)
 
115
 
 
116
 
 
117
      # Create the table
 
118
      sw = gtk.ScrolledWindow()
 
119
      sw.show()
 
120
      sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
 
121
      sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
122
 
 
123
      # create tree view
 
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)
 
128
 
 
129
      sw.add(treeview_group)
 
130
 
 
131
      grouplist_box.pack_start(sw, True, True, 0)
 
132
 
 
133
 
 
134
      # add columns to the tree view
 
135
      self.__add_columns_group(treeview_group)
 
136
 
 
137
      # Add buttons
 
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)
 
143
 
 
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)
 
149
 
 
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)
 
155
 
 
156
      # User list for the group
 
157
      user_hbox = gtk.HBox(False, 8)
 
158
      user_hbox.show()
 
159
      top_box.add(user_hbox)
 
160
 
 
161
      self.group_user = group_user_list.Group_user_list(user_hbox,
 
162
                                                        self.con, self.cur,
 
163
                                                        self.current_group_id)
 
164
 
 
165
      # Missing callbacks
 
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)
 
169
 
 
170
      # Load lists
 
171
      self.class_changed_cb(self.combo_class)
 
172
      self.reload_group()
 
173
 
 
174
  # -------------------
 
175
  # Group Management
 
176
  # -------------------
 
177
 
 
178
  # Update the group list area
 
179
  def reload_group(self):
 
180
 
 
181
    # Remove all entries in the list
 
182
    self.group_model.clear()
 
183
 
 
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()
 
188
 
 
189
    for agroup in self.group_data:
 
190
      self.add_group_in_model(self.group_model, agroup)
 
191
 
 
192
    self.group_user.reload(self.current_group_id)
 
193
 
 
194
 
 
195
  # Create the model for the group list
 
196
  def __create_model_group(self):
 
197
    model = gtk.ListStore(
 
198
      gobject.TYPE_INT,
 
199
      gobject.TYPE_STRING,
 
200
      gobject.TYPE_STRING,
 
201
      gobject.TYPE_BOOLEAN)
 
202
 
 
203
    return model
 
204
 
 
205
 
 
206
  def __add_columns_group(self, treeview):
 
207
 
 
208
    model = treeview.get_model()
 
209
 
 
210
    # columns for name
 
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,
 
215
                                text=COLUMN_NAME)
 
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)
 
220
 
 
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)
 
231
 
 
232
 
 
233
  # Add group in the model
 
234
  def add_group_in_model(self, model, agroup):
 
235
    iter = model.append()
 
236
    model.set (iter,
 
237
               COLUMN_GROUPID,          agroup[COLUMN_GROUPID],
 
238
               COLUMN_NAME,             agroup[COLUMN_NAME],
 
239
               COLUMN_DESCRIPTION,      agroup[COLUMN_DESCRIPTION]
 
240
               )
 
241
 
 
242
 
 
243
  #
 
244
  def on_add_group_clicked(self, button, model):
 
245
    group_id = constants.get_next_group_id(self.con, self.cur)
 
246
 
 
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,
 
250
                         self)
 
251
 
 
252
 
 
253
  def on_remove_group_clicked(self, button, treeview):
 
254
 
 
255
    selection = treeview.get_selection()
 
256
    model, iter = selection.get_selected()
 
257
 
 
258
    if iter:
 
259
      path = model.get_path(iter)[0]
 
260
      group_id = model.get_value(iter, COLUMN_GROUPID)
 
261
      model.remove(iter)
 
262
      # Remove it from the base
 
263
      self.cur.execute('delete from groups where group_id=?', (group_id,))
 
264
      self.con.commit()
 
265
 
 
266
    self.group_user.clear()
 
267
 
 
268
 
 
269
  def on_cell_group_edited(self, cell, path_string, new_text, model):
 
270
 
 
271
    iter = model.get_iter_from_string(path_string)
 
272
    path = model.get_path(iter)[0]
 
273
    column = cell.get_data("column")
 
274
 
 
275
    group_id = model.get_value(iter, COLUMN_GROUPID)
 
276
 
 
277
    if column == COLUMN_NAME:
 
278
      model.set(iter, column, new_text)
 
279
 
 
280
    elif column == COLUMN_DESCRIPTION:
 
281
      model.set(iter, column, new_text)
 
282
 
 
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 (?, ?, ?, ?)',
 
289
                     group_data)
 
290
    self.con.commit()
 
291
 
 
292
 
 
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()
 
297
      if active < 0:
 
298
        return None
 
299
      return model[active][0]
 
300
 
 
301
  def on_edit_group_clicked(self, button, treeview):
 
302
 
 
303
    selection = treeview.get_selection()
 
304
    model, iter = selection.get_selected()
 
305
 
 
306
    if iter:
 
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,
 
314
                           group_description,
 
315
                           self)
 
316
 
 
317
    else:
 
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"))
 
323
      dialog.run()
 
324
      dialog.destroy()
 
325
 
 
326
 
 
327
  def group_changed_cb(self, selection, group_user):
 
328
    model, iter = selection.get_selected()
 
329
 
 
330
    if iter:
 
331
      path = model.get_path(iter)[0]
 
332
      self.current_group_id = model.get_value(iter, COLUMN_GROUPID)
 
333
 
 
334
      group_user.reload(self.current_group_id)
 
335
 
 
336
      # Set the default button on if needed
 
337
 
 
338
      # The wholegroup is not editable
 
339
      wholegroup_id = constants.get_wholegroup_id(self.con,
 
340
                                                  self.cur,
 
341
                                                  self.current_class_id)
 
342
 
 
343
      if(wholegroup_id == self.current_group_id):
 
344
        self.button_edit.set_sensitive(False)
 
345
        self.button_remove.set_sensitive(False)
 
346
      else:
 
347
        self.button_edit.set_sensitive(True)
 
348
        self.button_remove.set_sensitive(True)
 
349
 
 
350
 
 
351
  def class_changed_cb(self, combobox):
 
352
    active = combobox.get_active()
 
353
    if active < 0:
 
354
      self.button_edit.set_sensitive(False)
 
355
      self.button_remove.set_sensitive(False)
 
356
      self.button_add.set_sensitive(False)
 
357
      return
 
358
 
 
359
    self.button_add.set_sensitive(True)
 
360
    self.current_class_id = self.class_list[active]
 
361
    self.reload_group()
 
362