1# Copyright (C) 2002-2009 Stephen Kennedy <stevek@gnome.org>
2# Copyright (C) 2010-2011, 2013 Kai Willadsen <kai.willadsen@gmail.com>
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 2 of the License, or (at
7# your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17from . import gnomeglade
18
19
20class ListWidget(gnomeglade.Component):
21
22    def __init__(self, ui_file, widget, store, treeview, new_row_data=None):
23        super().__init__(ui_file, widget, store)
24        self.new_row_data = new_row_data
25        self.list = getattr(self, treeview)
26        self.model = self.list.get_model()
27        selection = self.list.get_selection()
28        selection.connect("changed", self._update_sensitivity)
29
30    def _update_sensitivity(self, *args):
31        (model, it, path) = self._get_selected()
32        if not it:
33            self.remove.set_sensitive(False)
34            self.move_up.set_sensitive(False)
35            self.move_down.set_sensitive(False)
36        else:
37            self.remove.set_sensitive(True)
38            self.move_up.set_sensitive(path > 0)
39            self.move_down.set_sensitive(path < len(model) - 1)
40
41    def _get_selected(self):
42        (model, it) = self.list.get_selection().get_selected()
43        if it:
44            path = model.get_path(it)[0]
45        else:
46            path = None
47        return (model, it, path)
48
49    def on_add_clicked(self, button):
50        self.model.append(self.new_row_data)
51
52    def on_remove_clicked(self, button):
53        (model, it, path) = self._get_selected()
54        self.model.remove(it)
55
56    def on_move_up_clicked(self, button):
57        (model, it, path) = self._get_selected()
58        model.swap(it, model.get_iter(path - 1))
59
60    def on_move_down_clicked(self, button):
61        (model, it, path) = self._get_selected()
62        model.swap(it, model.get_iter(path + 1))
63