|
| 1 | +# |
| 2 | +# Gramps - a GTK+/GNOME based genealogy program |
| 3 | +# |
| 4 | +# Copyright (C) 2007-2009 Douglas S. Blank <doug.blank@gmail.com> |
| 5 | +# Copyright (C) 2026 Douglas S. Blank <doug.blank@gmail.com> |
| 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 along |
| 18 | +# with this program; if not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +# ------------------------------------------------------------------------ |
| 21 | +# |
| 22 | +# Python modules |
| 23 | +# |
| 24 | +# ------------------------------------------------------------------------ |
| 25 | +from abc import abstractmethod |
| 26 | + |
| 27 | +# ------------------------------------------------------------------------ |
| 28 | +# |
| 29 | +# Gramps modules |
| 30 | +# |
| 31 | +# ------------------------------------------------------------------------ |
| 32 | +from gramps.gen.plug import Gramplet, BasePluginManager |
| 33 | +from gramps.gen.config import config |
| 34 | +from gramps.gen.const import GRAMPS_LOCALE as glocale |
| 35 | + |
| 36 | +try: |
| 37 | + _trans = glocale.get_addon_translator(__file__) |
| 38 | +except ValueError: |
| 39 | + _trans = glocale.translation |
| 40 | +_ = _trans.sgettext |
| 41 | + |
| 42 | +from wordcloudwidget import WordCloudWidget |
| 43 | +from slideroption import SliderOption, GuiSliderOption |
| 44 | + |
| 45 | +# ------------------------------------------------------------------------ |
| 46 | +# |
| 47 | +# Constants |
| 48 | +# |
| 49 | +# ------------------------------------------------------------------------ |
| 50 | + |
| 51 | +_YIELD_INTERVAL = 350 |
| 52 | + |
| 53 | +_DEFAULT_COLOR_LOW = "#99ccff" # (0.6, 0.8, 1.0) |
| 54 | +_DEFAULT_COLOR_HIGH = "#003399" # (0.0, 0.2, 0.6) |
| 55 | +_DEFAULT_COLOR_HOVER = "#cc0000" # (0.8, 0.0, 0.0) |
| 56 | +_DEFAULT_QUALITY = 0.0 |
| 57 | + |
| 58 | + |
| 59 | +def _hex_to_rgb(hex_color): |
| 60 | + h = hex_color.lstrip("#") |
| 61 | + return tuple(int(h[i : i + 2], 16) / 255.0 for i in (0, 2, 4)) |
| 62 | + |
| 63 | + |
| 64 | +# ------------------------------------------------------------------------ |
| 65 | +# |
| 66 | +# CloudGramplet class |
| 67 | +# |
| 68 | +# ------------------------------------------------------------------------ |
| 69 | +class CloudGramplet(Gramplet): |
| 70 | + """A gramplet that displays a word cloud where word size reflects frequency.""" |
| 71 | + |
| 72 | + def init(self): |
| 73 | + pmgr = BasePluginManager.get_instance() |
| 74 | + pmgr.register_option(SliderOption, GuiSliderOption) |
| 75 | + |
| 76 | + self.top_size = 150 |
| 77 | + self.color_low = _DEFAULT_COLOR_LOW |
| 78 | + self.color_high = _DEFAULT_COLOR_HIGH |
| 79 | + self.color_hover = _DEFAULT_COLOR_HOVER |
| 80 | + self.quality = _DEFAULT_QUALITY |
| 81 | + self.filter_missing = True |
| 82 | + self.value_name = "default_value_name" |
| 83 | + self.preference_no_value = "" |
| 84 | + self._values_linked_data = {} |
| 85 | + |
| 86 | + self.word_cloud = WordCloudWidget( |
| 87 | + [], |
| 88 | + on_click=self._on_word_clicked, |
| 89 | + color_low=_hex_to_rgb(self.color_low), |
| 90 | + color_high=_hex_to_rgb(self.color_high), |
| 91 | + color_hover=_hex_to_rgb(self.color_hover), |
| 92 | + quality=self.quality, |
| 93 | + ) |
| 94 | + self.gui.get_container_widget().remove(self.gui.textview) |
| 95 | + self.gui.get_container_widget().add(self.word_cloud) |
| 96 | + self.word_cloud.show() |
| 97 | + |
| 98 | + def set_value_name(self, value_name): |
| 99 | + """What the cloud displays. For a name cloud, `value_name` is "name".""" |
| 100 | + self.value_name = _(value_name) |
| 101 | + |
| 102 | + def set_preference_no_value(self, preference_no_value): |
| 103 | + """Config key holding the default text to show when there are no values.""" |
| 104 | + self.preference_no_value = preference_no_value |
| 105 | + |
| 106 | + def _on_word_clicked(self, word): |
| 107 | + linked_data = self._values_linked_data.get(word) |
| 108 | + if linked_data is not None: |
| 109 | + self.on_item_clicked(word, linked_data) |
| 110 | + |
| 111 | + def on_item_clicked(self, word, linked_data): |
| 112 | + """Called when the user clicks a word. Subclasses override to navigate.""" |
| 113 | + |
| 114 | + @abstractmethod |
| 115 | + def db_changed(self): |
| 116 | + """Connect the cloud with the database. |
| 117 | + See the example in surnamewordcloudgramplet.py. |
| 118 | + """ |
| 119 | + |
| 120 | + @abstractmethod |
| 121 | + def get_items(self) -> list: |
| 122 | + """How to access data in the cloud. Must return an iterator of |
| 123 | + (value, linked_data, count) triples. |
| 124 | + See the example in surnamewordcloudgramplet.py. |
| 125 | + """ |
| 126 | + |
| 127 | + def on_load(self): |
| 128 | + data = self.gui.data |
| 129 | + if len(data) >= 1: |
| 130 | + self.top_size = int(data[0]) |
| 131 | + if len(data) >= 5: |
| 132 | + self.color_low = data[1] |
| 133 | + self.color_high = data[2] |
| 134 | + self.color_hover = data[3] |
| 135 | + self.quality = float(data[4]) |
| 136 | + if len(data) >= 6: |
| 137 | + self.filter_missing = bool(int(data[5])) |
| 138 | + self.word_cloud.set_colors( |
| 139 | + _hex_to_rgb(self.color_low), |
| 140 | + _hex_to_rgb(self.color_high), |
| 141 | + _hex_to_rgb(self.color_hover), |
| 142 | + ) |
| 143 | + self.word_cloud.set_quality(self.quality) |
| 144 | + |
| 145 | + def _read_options(self): |
| 146 | + self.top_size = int( |
| 147 | + self.get_option(_("Number of %s") % self.value_name).get_value() |
| 148 | + ) |
| 149 | + self.color_low = self.get_option(_("Color (low)")).get_value() |
| 150 | + self.color_high = self.get_option(_("Color (high)")).get_value() |
| 151 | + self.color_hover = self.get_option(_("Hover color")).get_value() |
| 152 | + self.quality = float(self.get_option(_("Layout quality")).get_value()) |
| 153 | + self.filter_missing = self.get_option( |
| 154 | + _("Filter missing/unknown words") |
| 155 | + ).get_value() |
| 156 | + |
| 157 | + def save_update_options(self, widget=None): |
| 158 | + self._read_options() |
| 159 | + self.gui.data = [ |
| 160 | + self.top_size, |
| 161 | + self.color_low, |
| 162 | + self.color_high, |
| 163 | + self.color_hover, |
| 164 | + self.quality, |
| 165 | + int(self.filter_missing), |
| 166 | + ] |
| 167 | + self.update() |
| 168 | + |
| 169 | + def save_options(self): |
| 170 | + self._read_options() |
| 171 | + |
| 172 | + def main(self): |
| 173 | + yield True |
| 174 | + |
| 175 | + yield_counter = 0 |
| 176 | + |
| 177 | + values_counts = {} |
| 178 | + values_linked_data = {} |
| 179 | + |
| 180 | + for value, linked_data, count in self.get_items(): |
| 181 | + if value not in values_counts: |
| 182 | + values_linked_data[value] = linked_data |
| 183 | + values_counts[value] = count |
| 184 | + else: |
| 185 | + values_counts[value] += count |
| 186 | + |
| 187 | + yield_counter += 1 |
| 188 | + if not yield_counter % _YIELD_INTERVAL: |
| 189 | + yield True |
| 190 | + |
| 191 | + # count order: [(value, count), ...] |
| 192 | + sorted_values = sorted( |
| 193 | + list(values_counts.items()), key=(lambda k: k[1]), reverse=True |
| 194 | + ) |
| 195 | + |
| 196 | + # limit to top_size distinct values |
| 197 | + selected_values = sorted_values[: self.top_size] |
| 198 | + |
| 199 | + # Build words list for the widget, resolving empty-value display text |
| 200 | + self._values_linked_data = {} |
| 201 | + words = [] |
| 202 | + for value, count in selected_values: |
| 203 | + if len(value) == 0: |
| 204 | + if self.preference_no_value != "": |
| 205 | + display = config.get(self.preference_no_value) |
| 206 | + else: |
| 207 | + display = _("[Missing %s]") % self.value_name |
| 208 | + else: |
| 209 | + display = value |
| 210 | + self._values_linked_data[display] = values_linked_data.get(value) |
| 211 | + words.append((display, count)) |
| 212 | + |
| 213 | + self.word_cloud.configure( |
| 214 | + quality=self.quality, |
| 215 | + color_low=_hex_to_rgb(self.color_low), |
| 216 | + color_high=_hex_to_rgb(self.color_high), |
| 217 | + color_hover=_hex_to_rgb(self.color_hover), |
| 218 | + ) |
| 219 | + self.word_cloud.set_words(words) |
| 220 | + |
| 221 | + def build_options(self): |
| 222 | + from gramps.gen.plug.menu import BooleanOption, ColorOption |
| 223 | + |
| 224 | + self.add_option( |
| 225 | + SliderOption(_("Number of %s") % self.value_name, self.top_size, 1, 150) |
| 226 | + ) |
| 227 | + self.add_option(ColorOption(_("Color (low)"), self.color_low)) |
| 228 | + self.add_option(ColorOption(_("Color (high)"), self.color_high)) |
| 229 | + self.add_option(ColorOption(_("Hover color"), self.color_hover)) |
| 230 | + self.add_option(SliderOption(_("Layout quality"), self.quality, 0.0, 1.0, 0.1)) |
| 231 | + self.add_option( |
| 232 | + BooleanOption(_("Filter missing/unknown words"), self.filter_missing) |
| 233 | + ) |
0 commit comments