/usr/share/pyshared/gquilt_pkg/table.py is in gquilt 0.25-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | ### Copyright (C) 2010 Peter Williams <peter_ono@users.sourceforge.net>
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; version 2 of the License only.
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Provide generic enhancements to Textview widgets primarily to create
them from templates and allow easier access to named contents.
"""
import gtk, collections
from gquilt_pkg import gutils, tlview, icons
class Model(tlview.ListStore):
def __init__(self, descr):
tlview.ListStore.__init__(self, descr)
ALWAYS_ON = 'table_always_on'
MODIFIED = 'table_modified'
NOT_MODIFIED = 'table_not_modified'
SELECTION = 'table_selection'
NO_SELECTION = 'table_no_selection'
UNIQUE_SELECTION = 'table_unique_selection'
TABLE_STATES = \
[ALWAYS_ON, MODIFIED, NOT_MODIFIED, SELECTION, NO_SELECTION,
UNIQUE_SELECTION]
class Table(gtk.VBox):
def __init__(self, model_descr, table_descr, size_req=None):
gtk.VBox.__init__(self)
self.model = Model(model_descr)
self.view = tlview.View(table_descr, self.model)
self.seln = self.view.get_selection()
if size_req:
self.view.set_size_request(*size_req)
self.pack_start(gutils.wrap_in_scrolled_window(self.view))
self.action_groups = {}
for key in TABLE_STATES:
self.action_groups[key] = gtk.ActionGroup(key)
self.action_groups[ALWAYS_ON].add_actions(
[
('table_add_row', gtk.STOCK_ADD, '_Add', None,
'Add a new entry to the table', self._add_row_acb),
])
self.action_groups[MODIFIED].add_actions(
[
('table_undo_changes', gtk.STOCK_UNDO, '_Undo', None,
'Undo unapplied changes', self._undo_changes_acb),
('table_apply_changes', gtk.STOCK_APPLY, '_Apply', None,
'Apply outstanding changes', self._apply_changes_acb),
])
self.action_groups[SELECTION].add_actions(
[
('table_delete_selection', gtk.STOCK_DELETE, '_Delete', None,
'Delete selected row(s)', self._delete_selection_acb),
('table_insert_row', icons.STOCK_INSERT, '_Insert', None,
'Insert a new entry before the selected row(s)', self._insert_row_acb),
])
self._modified = False
self.model.connect('row-inserted', self._row_inserted_cb)
self.seln.connect('changed', self._selection_changed_cb)
self.view.register_modification_callback(self._set_modified, True)
self.seln.unselect_all()
self._selection_changed_cb(self.seln)
def _set_modified(self, val):
self._modified = val
self.action_groups[MODIFIED].set_sensitive(val)
self.action_groups[NOT_MODIFIED].set_sensitive(not val)
def _fetch_contents(self):
pass # define in child
def set_contents(self):
self.model.set_contents(self._fetch_contents())
self._set_modified(False)
def get_contents(self):
return self.model.get_contents()
def apply_changes(self):
pass # define in child
def _row_inserted_cb(self, model, path, model_iter):
self._set_modified(True)
def _selection_changed_cb(self, selection):
rows = selection.count_selected_rows()
self.action_groups[SELECTION].set_sensitive(rows > 0)
self.action_groups[NO_SELECTION].set_sensitive(rows == 0)
self.action_groups[UNIQUE_SELECTION].set_sensitive(rows == 1)
def _undo_changes_acb(self, _action=None):
self.set_contents()
def _apply_changes_acb(self, _action=None):
self.apply_changes()
def _add_row_acb(self, _action=None):
model_iter = self.model.append(None)
self.view.get_selection().select_iter(model_iter)
return
def _delete_selection_acb(self, _action=None):
model, paths = self.seln.get_selected_rows()
iters = []
for path in paths:
iters.append(model.get_iter(path))
for model_iter in iters:
model.remove(model_iter)
def _insert_row_acb(self, _action=None):
model, paths = self.seln.get_selected_rows()
if not paths:
return
model_iter = self.model.insert_before(model.get_iter(paths[0]), None)
self.view.get_selection().select_iter(model_iter)
return
def get_selected_data(self, columns=None):
store, selected_rows = self.seln.get_selected_rows()
if not columns:
columns = list(range(store.get_n_columns()))
result = []
for row in selected_rows:
model_iter = store.get_iter(row)
assert model_iter is not None
result.append(store.get_values(model_iter, columns))
return result
def get_selected_data_by_label(self, labels):
columns = self.model.get_cols(labels)
return self.get_selected_data(columns)
|