~ubuntu-branches/ubuntu/utopic/gramps/utopic

« back to all changes in this revision

Viewing changes to src/plugins/gramplet/Citations.py

  • Committer: Package Import Robot
  • Author(s): James A. Treacy
  • Date: 2012-05-22 17:18:36 UTC
  • mfrom: (39.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20120522171836-35fi62lp4w7jnrd7
Tags: 3.4.0-1
* New upstream version
* Updated desktop file. Closes: #667472

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Gramps - a GTK+/GNOME based genealogy program
 
2
#
 
3
# Copyright (C) 2011 Nick Hall
 
4
# Copyright (C) 2011 Tim G L Lyons
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
# $Id: Citations.py 18679 2012-01-01 17:58:33Z nick-h $
 
21
#
 
22
 
 
23
from gui.editors import EditSource, EditCitation
 
24
from ListModel import ListModel, NOSORT
 
25
from gen.plug import Gramplet
 
26
from gen.ggettext import gettext as _
 
27
import Errors
 
28
import gtk
 
29
 
 
30
class Citations(Gramplet):
 
31
    """
 
32
    Displays the citations for an object.
 
33
    """
 
34
    def init(self):
 
35
        self.gui.WIDGET = self.build_gui()
 
36
        self.gui.get_container_widget().remove(self.gui.textview)
 
37
        self.gui.get_container_widget().add_with_viewport(self.gui.WIDGET)
 
38
        self.gui.WIDGET.show()
 
39
 
 
40
    def build_gui(self):
 
41
        """
 
42
        Build the GUI interface.
 
43
        """
 
44
        tip = _('Double-click on a row to edit the selected source/citation.')
 
45
        self.set_tooltip(tip)
 
46
        top = gtk.TreeView()
 
47
        titles = [('', NOSORT, 50,),
 
48
                  (_('Source/Citation'), 1, 350),
 
49
                  (_('Author'), 2, 200),
 
50
                  (_('Publisher'), 3, 150)]
 
51
        self.model = ListModel(top, titles, list_mode="tree", 
 
52
                               event_func=self.invoke_editor)
 
53
        return top
 
54
        
 
55
    def add_citations(self, obj):
 
56
        for citation_handle in obj.get_citation_list():
 
57
            self.add_citation_ref(citation_handle)
 
58
        
 
59
    def add_name_citations(self, obj):
 
60
        names = [obj.get_primary_name()] + obj.get_alternate_names()
 
61
        for name in names:
 
62
            self.add_citations(name)
 
63
 
 
64
    def add_attribute_citations(self, obj):
 
65
        for attr in obj.get_attribute_list():
 
66
            self.add_citations(attr)
 
67
 
 
68
    def add_mediaref_citations(self, obj):
 
69
        for media_ref in obj.get_media_list():
 
70
            self.add_citations(media_ref)
 
71
            self.add_attribute_citations(media_ref)
 
72
            media = self.dbstate.db.get_object_from_handle(media_ref.ref)
 
73
            self.add_media_citations(media)
 
74
 
 
75
    def add_media_citations(self, media):
 
76
        self.add_citations(media)
 
77
        self.add_attribute_citations(media)
 
78
 
 
79
    def add_eventref_citations(self, obj):
 
80
        for event_ref in obj.get_event_ref_list():
 
81
            self.add_attribute_citations(event_ref)
 
82
            event = self.dbstate.db.get_event_from_handle(event_ref.ref)
 
83
            self.add_event_citations(event)
 
84
 
 
85
    def add_event_citations(self, event):
 
86
        self.add_citations(event)
 
87
        self.add_attribute_citations(event)
 
88
        self.add_mediaref_citations(event)
 
89
        place_handle = event.get_place_handle()
 
90
        place = self.dbstate.db.get_place_from_handle(place_handle)
 
91
        if place:
 
92
            self.add_place_citations(place)
 
93
 
 
94
    def add_place_citations(self, place):
 
95
        self.add_citations(place)
 
96
        self.add_mediaref_citations(place)
 
97
 
 
98
    def add_address_citations(self, obj):
 
99
        for address in obj.get_address_list():
 
100
            self.add_citations(address)
 
101
 
 
102
    def add_lds_citations(self, obj):
 
103
        for lds in obj.get_lds_ord_list():
 
104
            self.add_citations(lds)
 
105
            place_handle = lds.get_place_handle()
 
106
            place = self.dbstate.db.get_place_from_handle(place_handle)
 
107
            if place:
 
108
                self.add_place_citations(place)
 
109
 
 
110
    def add_association_citations(self, obj):
 
111
        for assoc in obj.get_person_ref_list():
 
112
            self.add_citations(assoc)
 
113
 
 
114
    def add_citation_ref(self, citation_handle):
 
115
        """
 
116
        Add a citation to the model.
 
117
        """
 
118
        citation = self.dbstate.db.get_citation_from_handle(citation_handle)
 
119
        page = citation.get_page()
 
120
        if not page:
 
121
            page = _('<No Citation>')
 
122
        source_handle = citation.get_reference_handle()
 
123
        source = self.dbstate.db.get_source_from_handle(source_handle)
 
124
        title = source.get_title()
 
125
        author = source.get_author()
 
126
        publisher = source.get_publication_info()
 
127
 
 
128
        if source_handle not in self.source_nodes:
 
129
            node = self.model.add([source_handle, title, author, publisher])
 
130
            self.source_nodes[source_handle] = node
 
131
            
 
132
        self.model.add([citation_handle, page, '', ''], 
 
133
                       node=self.source_nodes[source_handle])
 
134
 
 
135
    def check_citations(self, obj):
 
136
        return True if obj.get_citation_list() else False
 
137
        
 
138
    def check_name_citations(self, obj):
 
139
        names = [obj.get_primary_name()] + obj.get_alternate_names()
 
140
        for name in names:
 
141
            if self.check_citations(name):
 
142
                return True
 
143
        return False
 
144
 
 
145
    def check_attribute_citations(self, obj):
 
146
        for attr in obj.get_attribute_list():
 
147
            if self.check_citations(attr):
 
148
                return True
 
149
        return False
 
150
 
 
151
    def check_mediaref_citations(self, obj):
 
152
        for media_ref in obj.get_media_list():
 
153
            if self.check_citations(media_ref):
 
154
                return True
 
155
            if self.check_attribute_citations(media_ref):
 
156
                return True
 
157
            media = self.dbstate.db.get_object_from_handle(media_ref.ref)
 
158
            if self.check_media_citations(media):
 
159
                return True
 
160
        return False
 
161
 
 
162
    def check_media_citations(self, media):
 
163
        if self.check_citations(media):
 
164
            return True
 
165
        if self.check_attribute_citations(media):
 
166
            return True
 
167
        return False
 
168
 
 
169
    def check_eventref_citations(self, obj):
 
170
        for event_ref in obj.get_event_ref_list():
 
171
            if self.check_attribute_citations(event_ref):
 
172
                return True
 
173
            event = self.dbstate.db.get_event_from_handle(event_ref.ref)
 
174
            if self.check_event_citations(event):
 
175
                return True
 
176
        return False
 
177
 
 
178
    def check_event_citations(self, event):
 
179
        if self.check_citations(event):
 
180
            return True
 
181
        if self.check_attribute_citations(event):
 
182
            return True
 
183
        if self.check_mediaref_citations(event):
 
184
            return True
 
185
        place_handle = event.get_place_handle()
 
186
        place = self.dbstate.db.get_place_from_handle(place_handle)
 
187
        if place and self.check_place_citations(place):
 
188
            return True
 
189
        return False
 
190
 
 
191
    def check_place_citations(self, place):
 
192
        if self.check_citations(place):
 
193
            return True
 
194
        if self.check_mediaref_citations(place):
 
195
            return True
 
196
        return False
 
197
 
 
198
    def check_address_citations(self, obj):
 
199
        for address in obj.get_address_list():
 
200
            if self.check_citations(address):
 
201
                return True
 
202
        return False
 
203
 
 
204
    def check_lds_citations(self, obj):
 
205
        for lds in obj.get_lds_ord_list():
 
206
            if self.check_citations(lds):
 
207
                return True
 
208
            place_handle = lds.get_place_handle()
 
209
            place = self.dbstate.db.get_place_from_handle(place_handle)
 
210
            if place and self.check_place_citations(place):
 
211
                return True
 
212
        return False
 
213
 
 
214
    def check_association_citations(self, obj):
 
215
        for assoc in obj.get_person_ref_list():
 
216
            if self.check_citations(assoc):
 
217
                return True
 
218
        return False
 
219
 
 
220
    def invoke_editor(self, treeview):
 
221
        """
 
222
        Edit the selected source or citation.
 
223
        """
 
224
        model, iter_ = treeview.get_selection().get_selected()
 
225
        if iter_:
 
226
            handle = model.get_value(iter_, 0)
 
227
            if len(model.get_path(iter_)) == 1:
 
228
                self.edit_source(handle)
 
229
            else:
 
230
                self.edit_citation(handle)
 
231
 
 
232
    def edit_source(self, handle):
 
233
        """
 
234
        Edit the selected source.
 
235
        """
 
236
        try:
 
237
            source = self.dbstate.db.get_source_from_handle(handle)
 
238
            EditSource(self.dbstate, self.uistate, [], source)
 
239
        except Errors.WindowActiveError:
 
240
            pass
 
241
 
 
242
    def edit_citation(self, handle):
 
243
        """
 
244
        Edit the selected citation.
 
245
        """
 
246
        try:
 
247
            citation = self.dbstate.db.get_citation_from_handle(handle)
 
248
            source_handle = citation.get_reference_handle()
 
249
            source = self.dbstate.db.get_source_from_handle(source_handle)
 
250
            EditCitation(self.dbstate, self.uistate, [], citation, source)
 
251
        except Errors.WindowActiveError:
 
252
            pass
 
253
 
 
254
class PersonCitations(Citations):
 
255
    """
 
256
    Displays the citations for a person.
 
257
    """
 
258
    def db_changed(self):
 
259
        self.dbstate.db.connect('person-update', self.update)
 
260
 
 
261
    def active_changed(self, handle):
 
262
        self.update()
 
263
 
 
264
    def update_has_data(self):
 
265
        active_handle = self.get_active('Person')
 
266
        active = self.dbstate.db.get_person_from_handle(active_handle)
 
267
        self.set_has_data(self.get_has_data(active))
 
268
    
 
269
    def main(self):
 
270
        active_handle = self.get_active('Person')
 
271
        active = self.dbstate.db.get_person_from_handle(active_handle)
 
272
            
 
273
        self.model.clear()
 
274
        if active:
 
275
            self.display_citations(active)
 
276
        else:
 
277
            self.set_has_data(False)
 
278
 
 
279
    def display_citations(self, person):
 
280
        """
 
281
        Display the citations for the active person.
 
282
        """
 
283
        self.source_nodes = {}
 
284
        self.add_citations(person)
 
285
        self.add_eventref_citations(person)
 
286
        for handle in person.get_family_handle_list():
 
287
            family = self.dbstate.db.get_family_from_handle(handle)
 
288
            self.add_eventref_citations(family)
 
289
        self.add_name_citations(person)
 
290
        self.add_attribute_citations(person)
 
291
        self.add_address_citations(person)
 
292
        self.add_mediaref_citations(person)
 
293
        self.add_association_citations(person)
 
294
        self.add_lds_citations(person)
 
295
 
 
296
        self.set_has_data(self.model.count > 0)
 
297
        self.model.tree.expand_all()
 
298
 
 
299
    def get_has_data(self, person):
 
300
        """
 
301
        Return True if the gramplet has data, else return False.
 
302
        """
 
303
        if person is None:
 
304
            return False
 
305
        if self.check_citations(person):
 
306
            return True
 
307
        if self.check_eventref_citations(person):
 
308
            return True
 
309
        for handle in person.get_family_handle_list():
 
310
            family = self.dbstate.db.get_family_from_handle(handle)
 
311
            if self.check_eventref_citations(family):
 
312
                return True
 
313
        if self.check_name_citations(person):
 
314
            return True
 
315
        if self.check_attribute_citations(person):
 
316
            return True
 
317
        if self.check_address_citations(person):
 
318
            return True
 
319
        if self.check_mediaref_citations(person):
 
320
            return True
 
321
        if self.check_association_citations(person):
 
322
            return True
 
323
        if self.check_lds_citations(person):
 
324
            return True
 
325
        return False
 
326
 
 
327
class EventCitations(Citations):
 
328
    """
 
329
    Displays the citations for an event.
 
330
    """
 
331
    def db_changed(self):
 
332
        self.dbstate.db.connect('event-update', self.update)
 
333
        self.connect_signal('Event', self.update)
 
334
 
 
335
    def update_has_data(self):
 
336
        active_handle = self.get_active('Event')
 
337
        active = self.dbstate.db.get_event_from_handle(active_handle)
 
338
        self.set_has_data(self.get_has_data(active))
 
339
    
 
340
    def main(self):
 
341
        active_handle = self.get_active('Event')
 
342
        active = self.dbstate.db.get_event_from_handle(active_handle)
 
343
            
 
344
        self.model.clear()
 
345
        if active:
 
346
            self.display_citations(active)
 
347
        else:
 
348
            self.set_has_data(False)
 
349
 
 
350
    def display_citations(self, event):
 
351
        """
 
352
        Display the citations for the active event.
 
353
        """
 
354
        self.source_nodes = {}
 
355
        self.add_event_citations(event)
 
356
        self.set_has_data(self.model.count > 0)
 
357
        self.model.tree.expand_all()
 
358
 
 
359
    def get_has_data(self, event):
 
360
        """
 
361
        Return True if the gramplet has data, else return False.
 
362
        """
 
363
        if event is None:
 
364
            return False
 
365
        if self.check_event_citations(event):
 
366
            return True
 
367
        return False
 
368
 
 
369
class FamilyCitations(Citations):
 
370
    """
 
371
    Displays the citations for a family.
 
372
    """
 
373
    def db_changed(self):
 
374
        self.dbstate.db.connect('family-update', self.update)
 
375
        self.connect_signal('Family', self.update)
 
376
 
 
377
    def update_has_data(self):
 
378
        active_handle = self.get_active('Family')
 
379
        active = self.dbstate.db.get_family_from_handle(active_handle)
 
380
        self.set_has_data(self.get_has_data(active))
 
381
    
 
382
    def main(self):
 
383
        active_handle = self.get_active('Family')
 
384
        active = self.dbstate.db.get_family_from_handle(active_handle)
 
385
            
 
386
        self.model.clear()
 
387
        if active:
 
388
            self.display_citations(active)
 
389
        else:
 
390
            self.set_has_data(False)
 
391
 
 
392
    def display_citations(self, family):
 
393
        """
 
394
        Display the citations for the active family.
 
395
        """
 
396
        self.source_nodes = {}
 
397
        self.add_citations(family)
 
398
        self.add_eventref_citations(family)
 
399
        self.add_attribute_citations(family)
 
400
        self.add_mediaref_citations(family)
 
401
        self.add_lds_citations(family)
 
402
 
 
403
        self.set_has_data(self.model.count > 0)
 
404
        self.model.tree.expand_all()
 
405
 
 
406
    def get_has_data(self, family):
 
407
        """
 
408
        Return True if the gramplet has data, else return False.
 
409
        """
 
410
        if family is None:
 
411
            return False
 
412
        if self.check_citations(family):
 
413
            return True
 
414
        if self.check_eventref_citations(family):
 
415
            return True
 
416
        if self.check_attribute_citations(family):
 
417
            return True
 
418
        if self.check_mediaref_citations(family):
 
419
            return True
 
420
        if self.check_lds_citations(family):
 
421
            return True
 
422
        return False
 
423
 
 
424
class PlaceCitations(Citations):
 
425
    """
 
426
    Displays the citations for a place.
 
427
    """
 
428
    def db_changed(self):
 
429
        self.dbstate.db.connect('place-update', self.update)
 
430
        self.connect_signal('Place', self.update)
 
431
 
 
432
    def update_has_data(self):
 
433
        active_handle = self.get_active('Place')
 
434
        active = self.dbstate.db.get_place_from_handle(active_handle)
 
435
        self.set_has_data(self.get_has_data(active))
 
436
    
 
437
    def main(self):
 
438
        active_handle = self.get_active('Place')
 
439
        active = self.dbstate.db.get_place_from_handle(active_handle)
 
440
            
 
441
        self.model.clear()
 
442
        if active:
 
443
            self.display_citations(active)
 
444
        else:
 
445
            self.set_has_data(False)
 
446
 
 
447
    def display_citations(self, place):
 
448
        """
 
449
        Display the citations for the active place.
 
450
        """
 
451
        self.source_nodes = {}
 
452
        self.add_place_citations(place)
 
453
        self.set_has_data(self.model.count > 0)
 
454
        self.model.tree.expand_all()
 
455
 
 
456
    def get_has_data(self, place):
 
457
        """
 
458
        Return True if the gramplet has data, else return False.
 
459
        """
 
460
        if place is None:
 
461
            return False
 
462
        if self.check_place_citations(place):
 
463
            return True
 
464
        return False
 
465
 
 
466
class MediaCitations(Citations):
 
467
    """
 
468
    Displays the citations for a media object.
 
469
    """
 
470
    def db_changed(self):
 
471
        self.dbstate.db.connect('media-update', self.update)
 
472
        self.connect_signal('Media', self.update)
 
473
 
 
474
    def update_has_data(self):
 
475
        active_handle = self.get_active('Media')
 
476
        active = self.dbstate.db.get_object_from_handle(active_handle)
 
477
        self.set_has_data(self.get_has_data(active))
 
478
    
 
479
    def main(self):
 
480
        active_handle = self.get_active('Media')
 
481
        active = self.dbstate.db.get_object_from_handle(active_handle)
 
482
            
 
483
        self.model.clear()
 
484
        if active:
 
485
            self.display_citations(active)
 
486
        else:
 
487
            self.set_has_data(False)
 
488
 
 
489
    def display_citations(self, media):
 
490
        """
 
491
        Display the citations for the active media object.
 
492
        """
 
493
        self.source_nodes = {}
 
494
        self.add_media_citations(media)
 
495
        self.set_has_data(self.model.count > 0)
 
496
        self.model.tree.expand_all()
 
497
 
 
498
    def get_has_data(self, media):
 
499
        """
 
500
        Return True if the gramplet has data, else return False.
 
501
        """
 
502
        if media is None:
 
503
            return False
 
504
        if self.check_media_citations(media):
 
505
            return True
 
506
        return False