1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2000-2006  Donald N. Allingham
5# Copyright (C) 2011       Tim G L Lyons, Nick Hall
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21
22"""
23CitationTreeModel classes for Gramps.
24"""
25
26#-------------------------------------------------------------------------
27#
28# python modules
29#
30#-------------------------------------------------------------------------
31import logging
32log = logging.getLogger(".")
33LOG = logging.getLogger(".citation")
34
35#-------------------------------------------------------------------------
36#
37# internationalization
38#
39#-------------------------------------------------------------------------
40from gramps.gen.const import GRAMPS_LOCALE as glocale
41_ = glocale.translation.gettext
42
43#-------------------------------------------------------------------------
44#
45# GNOME/GTK modules
46#
47#-------------------------------------------------------------------------
48from gi.repository import Gtk
49
50#-------------------------------------------------------------------------
51#
52# Gramps modules
53#
54#-------------------------------------------------------------------------
55from gramps.gen.utils.db import get_source_referents
56from .treebasemodel import TreeBaseModel
57from .citationbasemodel import CitationBaseModel
58
59#-------------------------------------------------------------------------
60#
61# CitationModel
62#
63#-------------------------------------------------------------------------
64class CitationTreeModel(CitationBaseModel, TreeBaseModel):
65    """
66    Hierarchical citation model.
67    """
68    def __init__(self, db, uistate, scol=0, order=Gtk.SortType.ASCENDING,
69                 search=None, skip=set(), sort_map=None):
70        self.db = db
71        self.number_items = self.db.get_number_of_sources
72        self.map = self.db.get_raw_source_data
73        self.gen_cursor = self.db.get_source_cursor
74        # The items here must correspond, in order, with data in
75        # CitationTreeView, and with the items in the secondary fmap, fmap2
76        self.fmap = [
77            self.source_src_title,   # COL_TITLE_PAGE (both Source & Citation)
78            self.source_src_id,      # COL_ID         (both Source & Citation)
79            None,                    # COL_DATE       (not for Source)
80            None,                    # COL_CONFIDENCE (not for Source)
81            self.source_src_private, # COL_PRIV       (both Source & Citation)
82            self.source_src_tags,    # COL_TAGS       (both Source & Citation)
83            self.source_src_chan,    # COL_CHAN       (both Source & Citation)
84            self.source_src_auth,    # COL_SRC_AUTH   (Source only)
85            self.source_src_abbr,    # COL_SRC_ABBR   (Source only)
86            self.source_src_pinfo,   # COL_SRC_PINFO  (Source only)
87            self.source_src_tag_color
88            ]
89        self.smap = [
90            self.source_src_title,
91            self.source_src_id,
92            self.dummy_sort_key,
93            self.dummy_sort_key,
94            self.source_src_private,
95            self.source_src_tags,
96            self.source_sort2_change,
97            self.source_src_auth,
98            self.source_src_abbr,
99            self.source_src_pinfo,
100            self.source_src_tag_color
101            ]
102
103        TreeBaseModel.__init__(self, self.db, uistate, scol=scol, order=order,
104                               search=search, skip=skip, sort_map=sort_map,
105                               nrgroups=1,
106                               group_can_have_handle=True,
107                               has_secondary=True)
108
109    def destroy(self):
110        """
111        Unset all elements that can prevent garbage collection
112        """
113        self.db = None
114        self.gen_cursor = None
115        self.map = None
116        self.fmap = None
117        self.smap = None
118        self.number_items = None
119        self.gen_cursor2 = None
120        self.map2 = None
121        self.fmap2 = None
122        self.smap2 = None
123        self.number_items2 = None
124        TreeBaseModel.destroy(self)
125
126    def _set_base_data(self):
127        """See TreeBaseModel, for citations, most have been set in init of
128        CitationBaseModel
129        """
130        self.number_items2 = self.db.get_number_of_citations
131        self.map2 = self.db.get_raw_citation_data
132        self.gen_cursor2 = self.db.get_citation_cursor
133        self.fmap2 = [
134            self.citation_page,
135            self.citation_id,
136            self.citation_date,
137            self.citation_confidence,
138            self.citation_private,
139            self.citation_tags,
140            self.citation_change,
141            None,
142            None,
143            None,
144            self.citation_tag_color
145            ]
146        self.smap2 = [
147            self.citation_page,
148            self.citation_id,
149            self.citation_sort_date,
150            self.citation_sort_confidence,
151            self.citation_private,
152            self.citation_tags,
153            self.citation_sort_change,
154            self.dummy_sort_key,
155            self.dummy_sort_key,
156            self.dummy_sort_key,
157            self.citation_tag_color
158            ]
159
160    def color_column(self):
161        """
162        Return the color column.
163        """
164        return 10
165
166    def get_tree_levels(self):
167        """
168        Return the headings of the levels in the hierarchy.
169        """
170        return [_('Source'), _('Citation')]
171
172    def add_row(self, handle, data):
173        """
174        Add source nodes to the node map.
175
176        handle      The handle of the gramps object.
177        data        The object data.
178        """
179        sort_key = self.sort_func(data)
180        self.add_node(None, handle, sort_key, handle)
181
182    def add_row2(self, handle, data):
183        """
184        Add citation nodes to the node map.
185
186        handle      The handle of the gramps object.
187        data        The object data.
188        """
189        sort_key = self.sort_func2(data)
190        # If the source for this citation already exists (in the tree model) we
191        # add the citation as a child of the source. Otherwise we add the source
192        # first (because citations don't have any meaning without the associated
193        # source)
194        if self._get_node(data[5]):
195            #             parent   child   sortkey   handle
196            self.add_node(data[5], handle, sort_key, handle, secondary=True)
197        else:
198            # add the source node first
199            source_sort_key = self.sort_func(self.map(data[5]))
200            #            parent child    sortkey          handle
201            self.add_node(None, data[5], source_sort_key, data[5])
202
203            #            parent    child   sortkey   handle
204            self.add_node(data[5], handle, sort_key, handle, secondary=True)
205
206    def on_get_n_columns(self):
207        return len(self.fmap)+1
208
209    def column_header(self, node):
210        """
211        Return a column heading.  This is called for nodes with no associated
212        Gramps handle.
213        """
214        return node.name
215