1# Gramps - a GTK+/GNOME based genealogy program
2#
3# Copyright (C) 2011 Nick Hall
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18#
19
20#-------------------------------------------------------------------------
21#
22# Gtk modules
23#
24#-------------------------------------------------------------------------
25from gi.repository import Gtk
26
27#-------------------------------------------------------------------------
28#
29# Gramps modules
30#
31#-------------------------------------------------------------------------
32from gramps.gui.listmodel import ListModel
33from gramps.gui.plug.quick import run_quick_report_by_name
34from gramps.gen.plug import Gramplet
35from gramps.gen.const import GRAMPS_LOCALE as glocale
36_ = glocale.translation.gettext
37
38class Attributes(Gramplet):
39    """
40    Displays the attributes of an object.
41    """
42    def init(self):
43        self.gui.WIDGET = self.build_gui()
44        self.gui.get_container_widget().remove(self.gui.textview)
45        self.gui.get_container_widget().add(self.gui.WIDGET)
46        self.gui.WIDGET.show()
47
48    def build_gui(self):
49        """
50        Build the GUI interface.
51        """
52        tip = _('Double-click on a row to view a quick report showing '
53                'all people with the selected attribute.')
54        self.set_tooltip(tip)
55        top = Gtk.TreeView()
56        titles = [(_('Key'), 1, 100),
57                  (_('Value'), 2, 100)]
58        self.model = ListModel(top, titles, event_func=self.display_report)
59        return top
60
61    def display_attributes(self, obj):
62        """
63        Display the attributes of an object.
64        """
65        for attr in obj.get_attribute_list():
66            self.model.add((str(attr.get_type()), attr.get_value()))
67        self.set_has_data(self.model.count > 0)
68
69    def display_report(self, treeview):
70        """
71        Display the quick report for matching attribute key.
72        """
73        model, iter_ = treeview.get_selection().get_selected()
74        if iter_:
75            key = model.get_value(iter_, 0)
76            run_quick_report_by_name(self.dbstate,
77                                     self.uistate,
78                                     'attribute_match',
79                                     key)
80
81    def get_has_data(self, obj):
82        """
83        Return True if the gramplet has data, else return False.
84        """
85        if obj is None:
86            return False
87        if obj.get_attribute_list():
88            return True
89        return False
90
91class PersonAttributes(Attributes):
92    """
93    Displays the attributes of a person.
94    """
95    def db_changed(self):
96        self.connect(self.dbstate.db, 'person-update', self.update)
97
98    def active_changed(self, handle):
99        self.update()
100
101    def update_has_data(self):
102        active_handle = self.get_active('Person')
103        if active_handle:
104            active = self.dbstate.db.get_person_from_handle(active_handle)
105            self.set_has_data(self.get_has_data(active))
106        else:
107            self.set_has_data(False)
108
109    def main(self):
110        self.model.clear()
111        active_handle = self.get_active('Person')
112        if active_handle:
113            active = self.dbstate.db.get_person_from_handle(active_handle)
114            if active:
115                self.display_attributes(active)
116            else:
117                self.set_has_data(False)
118        else:
119            self.set_has_data(False)
120
121class EventAttributes(Attributes):
122    """
123    Displays the attributes of an event.
124    """
125    def db_changed(self):
126        self.connect(self.dbstate.db, 'event-update', self.update)
127        self.connect_signal('Event', self.update)
128
129    def update_has_data(self):
130        active_handle = self.get_active('Event')
131        if active_handle:
132            active = self.dbstate.db.get_event_from_handle(active_handle)
133            self.set_has_data(self.get_has_data(active))
134        else:
135            self.set_has_data(False)
136
137    def main(self):
138        self.model.clear()
139        active_handle = self.get_active('Event')
140        if active_handle:
141            active = self.dbstate.db.get_event_from_handle(active_handle)
142            if active:
143                self.display_attributes(active)
144            else:
145                self.set_has_data(False)
146        else:
147            self.set_has_data(False)
148
149class FamilyAttributes(Attributes):
150    """
151    Displays the attributes of an event.
152    """
153    def db_changed(self):
154        self.connect(self.dbstate.db, 'family-update', self.update)
155        self.connect_signal('Family', self.update)
156
157    def update_has_data(self):
158        active_handle = self.get_active('Family')
159        if active_handle:
160            active = self.dbstate.db.get_family_from_handle(active_handle)
161            self.set_has_data(self.get_has_data(active))
162        else:
163            self.set_has_data(False)
164
165    def main(self):
166        self.model.clear()
167        active_handle = self.get_active('Family')
168        if active_handle:
169            active = self.dbstate.db.get_family_from_handle(active_handle)
170            if active:
171                self.display_attributes(active)
172            else:
173                self.set_has_data(False)
174        else:
175            self.set_has_data(False)
176
177class MediaAttributes(Attributes):
178    """
179    Displays the attributes of a media object.
180    """
181    def db_changed(self):
182        self.connect(self.dbstate.db, 'media-update', self.update)
183        self.connect_signal('Media', self.update)
184
185    def update_has_data(self):
186        active_handle = self.get_active('Media')
187        if active_handle:
188            active = self.dbstate.db.get_media_from_handle(active_handle)
189            self.set_has_data(self.get_has_data(active))
190        else:
191            self.set_has_data(False)
192
193    def main(self):
194        self.model.clear()
195        active_handle = self.get_active('Media')
196        if active_handle:
197            active = self.dbstate.db.get_media_from_handle(active_handle)
198            if active:
199                self.display_attributes(active)
200            else:
201                self.set_has_data(False)
202        else:
203            self.set_has_data(False)
204
205class SourceAttributes(Attributes):
206    """
207    Displays the attributes of a source object.
208    """
209    def db_changed(self):
210        self.connect(self.dbstate.db, 'source-update', self.update)
211        self.connect_signal('Source', self.update)
212
213    def update_has_data(self):
214        active_handle = self.get_active('Source')
215        if active_handle:
216            active = self.dbstate.db.get_source_from_handle(active_handle)
217            self.set_has_data(self.get_has_data(active))
218        else:
219            self.set_has_data(False)
220
221    def main(self):
222        self.model.clear()
223        active_handle = self.get_active('Source')
224        if active_handle:
225            active = self.dbstate.db.get_source_from_handle(active_handle)
226            if active:
227                self.display_attributes(active)
228            else:
229                self.set_has_data(False)
230        else:
231            self.set_has_data(False)
232
233class CitationAttributes(Attributes):
234    """
235    Displays the attributes of a citation object.
236    """
237    def db_changed(self):
238        self.connect(self.dbstate.db, 'citation-update', self.update)
239        self.connect_signal('Citation', self.update)
240
241    def update_has_data(self):
242        active_handle = self.get_active('Citation')
243        if active_handle:
244            active = self.dbstate.db.get_citation_from_handle(active_handle)
245            self.set_has_data(self.get_has_data(active))
246        else:
247            self.set_has_data(False)
248
249    def main(self):
250        self.model.clear()
251        active_handle = self.get_active('Citation')
252        if active_handle:
253            active = self.dbstate.db.get_citation_from_handle(active_handle)
254            if active:
255                self.display_attributes(active)
256            else:
257                self.set_has_data(False)
258        else:
259            self.set_has_data(False)
260