2009-02-01 16:42:57 +00:00
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007-2009 Douglas S. Blank <doug.blank@gmail.com>
|
|
|
|
#
|
|
|
|
# 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; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
2010-01-25 17:45:21 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2009-02-01 16:42:57 +00:00
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2009-10-25 20:29:45 +00:00
|
|
|
from gen.plug import Gramplet
|
2010-01-18 04:42:17 +00:00
|
|
|
from gen.ggettext import sgettext as _
|
2009-10-08 01:12:51 +00:00
|
|
|
import config
|
2009-02-01 16:42:57 +00:00
|
|
|
|
2009-07-03 20:23:41 +00:00
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
|
|
|
_YIELD_INTERVAL = 350
|
|
|
|
|
2009-02-01 16:42:57 +00:00
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Local functions
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
def make_tag_size(n, counts, mins=8, maxs=20):
|
|
|
|
# return font sizes mins to maxs
|
|
|
|
diff = maxs - mins
|
|
|
|
# based on counts (biggest to smallest)
|
|
|
|
if len(counts) > 1:
|
|
|
|
position = diff - (diff * (float(counts.index(n)) / (len(counts) - 1)))
|
|
|
|
else:
|
|
|
|
position = 0
|
|
|
|
return int(position) + mins
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramplet class
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
class SurnameCloudGramplet(Gramplet):
|
|
|
|
def init(self):
|
|
|
|
self.set_tooltip(_("Double-click surname for details"))
|
2009-12-12 04:18:04 +00:00
|
|
|
self.top_size = 150 # will be overwritten in load
|
2010-02-13 03:21:38 +00:00
|
|
|
self.min_font = 8
|
|
|
|
self.max_font = 20
|
2009-02-01 16:42:57 +00:00
|
|
|
self.set_text(_("No Family Tree loaded."))
|
|
|
|
|
|
|
|
def db_changed(self):
|
|
|
|
self.dbstate.db.connect('person-add', self.update)
|
|
|
|
self.dbstate.db.connect('person-delete', self.update)
|
|
|
|
self.dbstate.db.connect('person-update', self.update)
|
|
|
|
self.dbstate.db.connect('person-rebuild', self.update)
|
|
|
|
self.dbstate.db.connect('family-rebuild', self.update)
|
|
|
|
|
|
|
|
def on_load(self):
|
2010-02-13 03:21:38 +00:00
|
|
|
if len(self.gui.data) == 3:
|
2009-02-01 16:42:57 +00:00
|
|
|
self.top_size = int(self.gui.data[0])
|
2010-02-13 03:21:38 +00:00
|
|
|
self.min_font = int(self.gui.data[1])
|
|
|
|
self.max_font = int(self.gui.data[2])
|
2009-02-01 16:42:57 +00:00
|
|
|
|
2010-04-18 13:42:46 +00:00
|
|
|
def save_update_options(self, widget=None):
|
|
|
|
self.top_size = int(self.get_option(_("Number of surnames")).get_value())
|
|
|
|
self.min_font = int(self.get_option(_("Min font size")).get_value())
|
|
|
|
self.max_font = int(self.get_option(_("Max font size")).get_value())
|
2010-02-13 03:21:38 +00:00
|
|
|
self.gui.data = [self.top_size, self.min_font, self.max_font]
|
2010-04-18 13:42:46 +00:00
|
|
|
self.update()
|
2009-02-01 16:42:57 +00:00
|
|
|
|
|
|
|
def main(self):
|
|
|
|
self.set_text(_("Processing...") + "\n")
|
|
|
|
yield True
|
2010-01-25 17:45:21 +00:00
|
|
|
surnames = defaultdict(int)
|
2009-02-01 16:42:57 +00:00
|
|
|
representative_handle = {}
|
2009-07-14 14:16:03 +00:00
|
|
|
|
|
|
|
cnt = 0
|
2009-09-14 20:50:25 +00:00
|
|
|
for person in self.dbstate.db.iter_people():
|
|
|
|
allnames = [person.get_primary_name()] + person.get_alternate_names()
|
|
|
|
allnames = set([name.get_group_name().strip() for name in allnames])
|
|
|
|
for surname in allnames:
|
2010-01-25 17:45:21 +00:00
|
|
|
surnames[surname] += 1
|
2009-09-14 20:50:25 +00:00
|
|
|
representative_handle[surname] = person.handle
|
|
|
|
cnt += 1
|
2009-07-03 20:23:41 +00:00
|
|
|
if not cnt % _YIELD_INTERVAL:
|
2009-02-01 16:42:57 +00:00
|
|
|
yield True
|
2009-07-03 20:23:41 +00:00
|
|
|
|
2009-02-01 16:42:57 +00:00
|
|
|
total_people = cnt
|
|
|
|
surname_sort = []
|
2009-07-14 14:16:03 +00:00
|
|
|
total = cnt = 0
|
|
|
|
for surname in surnames:
|
2009-02-01 16:42:57 +00:00
|
|
|
surname_sort.append( (surnames[surname], surname) )
|
|
|
|
total += surnames[surname]
|
2009-09-14 20:50:25 +00:00
|
|
|
cnt += 1
|
2009-07-03 20:23:41 +00:00
|
|
|
if not cnt % _YIELD_INTERVAL:
|
2009-02-01 16:42:57 +00:00
|
|
|
yield True
|
2009-07-03 20:23:41 +00:00
|
|
|
|
2009-02-01 16:42:57 +00:00
|
|
|
total_surnames = cnt
|
2009-07-03 20:23:41 +00:00
|
|
|
surname_sort.sort(reverse=True)
|
2009-02-01 16:42:57 +00:00
|
|
|
cloud_names = []
|
|
|
|
cloud_values = []
|
2009-07-14 14:16:03 +00:00
|
|
|
for (count, surname) in surname_sort:
|
2009-02-01 16:42:57 +00:00
|
|
|
cloud_names.append( (count, surname) )
|
|
|
|
cloud_values.append( count )
|
2009-07-03 20:23:41 +00:00
|
|
|
|
|
|
|
cloud_names.sort(key=lambda k:k[1])
|
2009-02-01 16:42:57 +00:00
|
|
|
line = 0
|
|
|
|
### All done!
|
|
|
|
# Now, find out how many we can display without going over top_size:
|
2010-01-25 17:45:21 +00:00
|
|
|
totals = defaultdict(int)
|
2009-02-01 16:42:57 +00:00
|
|
|
for (count, givensubname) in cloud_names: # givensubname_sort:
|
2010-01-25 17:45:21 +00:00
|
|
|
totals[count] += 1
|
2009-05-27 17:33:45 +00:00
|
|
|
sums = sorted(totals, reverse=True)
|
2009-02-01 16:42:57 +00:00
|
|
|
total = 0
|
|
|
|
include_greater_than = 0
|
|
|
|
for s in sums:
|
2010-04-18 13:42:46 +00:00
|
|
|
if total + totals[s] <= self.top_size:
|
2009-02-01 16:42:57 +00:00
|
|
|
total += totals[s]
|
|
|
|
else:
|
|
|
|
include_greater_than = s
|
|
|
|
break
|
2010-04-18 13:42:46 +00:00
|
|
|
# now, limit counts to only include those that we can display:
|
|
|
|
|
|
|
|
mins = self.min_font
|
|
|
|
maxs = self.max_font
|
2009-02-01 16:42:57 +00:00
|
|
|
# Ok, now we can show those counts > include_greater_than:
|
2010-04-18 13:42:46 +00:00
|
|
|
good_counts = []
|
|
|
|
for (count, surname) in cloud_names: # surname_sort:
|
|
|
|
if count > include_greater_than:
|
|
|
|
good_counts.append(count)
|
|
|
|
counts = list(set(good_counts))
|
|
|
|
counts.sort(reverse=True)
|
2009-02-01 16:42:57 +00:00
|
|
|
showing = 0
|
|
|
|
self.set_text("")
|
|
|
|
for (count, surname) in cloud_names: # surname_sort:
|
|
|
|
if count > include_greater_than:
|
|
|
|
if len(surname) == 0:
|
2009-10-08 01:12:51 +00:00
|
|
|
text = config.get('preferences.no-surname-text')
|
2009-02-01 16:42:57 +00:00
|
|
|
else:
|
|
|
|
text = surname
|
2009-12-12 04:18:04 +00:00
|
|
|
size = make_tag_size(count, counts, mins=mins, maxs=maxs)
|
2009-02-01 16:42:57 +00:00
|
|
|
self.link(text, 'Surname', representative_handle[surname], size,
|
|
|
|
"%s, %d%% (%d)" % (text,
|
|
|
|
int((float(count)/total_people) * 100),
|
|
|
|
count))
|
|
|
|
self.append_text(" ")
|
|
|
|
showing += 1
|
|
|
|
self.append_text(("\n\n" + _("Total unique surnames") + ": %d\n") %
|
|
|
|
total_surnames)
|
|
|
|
self.append_text((_("Total surnames showing") + ": %d\n") % showing)
|
|
|
|
self.append_text((_("Total people") + ": %d") % total_people, "begin")
|
2009-12-12 04:18:04 +00:00
|
|
|
|
|
|
|
def build_options(self):
|
|
|
|
from gen.plug.menu import NumberOption
|
|
|
|
self.top_size_option = NumberOption(_("Number of surnames"), self.top_size, 1, 150)
|
|
|
|
self.add_option(self.top_size_option)
|
2010-02-13 03:21:38 +00:00
|
|
|
self.min_option = NumberOption(_("Min font size"), self.min_font, 1, 50)
|
2009-12-12 04:18:04 +00:00
|
|
|
self.add_option(self.min_option)
|
2010-02-13 03:21:38 +00:00
|
|
|
self.max_option = NumberOption(_("Max font size"), self.max_font, 1, 50)
|
2009-12-12 04:18:04 +00:00
|
|
|
self.add_option(self.max_option)
|
|
|
|
|
2010-02-13 03:21:38 +00:00
|
|
|
def save_options(self):
|
|
|
|
self.top_size = int(self.get_option(_("Number of surnames")).get_value())
|
|
|
|
self.min_font = int(self.get_option(_("Min font size")).get_value())
|
|
|
|
self.max_font = int(self.get_option(_("Max font size")).get_value())
|
|
|
|
|