| 1 |
# COPYRIGHT (C) 2020-2025 Nicotine+ Contributors |
| 2 |
# COPYRIGHT (C) 2016-2017 Michael Labouebe <[email protected]> |
| 3 |
# COPYRIGHT (C) 2008-2009 quinox <[email protected]> |
| 4 |
# COPYRIGHT (C) 2006-2009 daelstorm <[email protected]> |
| 5 |
# COPYRIGHT (C) 2003-2004 Hyriand <[email protected]> |
| 6 |
# |
| 7 |
# GNU GENERAL PUBLIC LICENSE |
| 8 |
# Version 3, 29 June 2007 |
| 9 |
# |
| 10 |
# This program is free software: you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation, either version 3 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
import time |
| 24 |
|
| 25 |
import gi.module |
| 26 |
|
| 27 |
from gi.repository import Gio |
| 28 |
from gi.repository import GLib |
| 29 |
from gi.repository import GObject |
| 30 |
from gi.repository import Gtk |
| 31 |
|
| 32 |
from pynicotine.config import config |
| 33 |
from pynicotine.core import core |
| 34 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 35 |
from pynicotine.gtkgui.widgets import clipboard |
| 36 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 37 |
from pynicotine.gtkgui.widgets.popupmenu import PopupMenu |
| 38 |
from pynicotine.gtkgui.widgets.theme import FILE_TYPE_ICON_LABELS |
| 39 |
from pynicotine.gtkgui.widgets.theme import USER_STATUS_ICON_LABELS |
| 40 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 41 |
|
| 42 |
|
| 43 |
class TreeView: |
| 44 |
|
| 45 |
def __init__(self, window, parent, columns, has_tree=False, multi_select=False, |
| 46 |
persistent_sort=False, name=None, secondary_name=None, activate_row_callback=None, |
| 47 |
focus_in_callback=None, select_row_callback=None, delete_accelerator_callback=None, |
| 48 |
search_entry=None): |
| 49 |
|
| 50 |
self.window = window |
| 51 |
self.widget = Gtk.TreeView(fixed_height_mode=True, has_tooltip=True, visible=True) |
| 52 |
self.model = None |
| 53 |
self.multi_select = multi_select |
| 54 |
self.iterators = {} |
| 55 |
self.has_tree = has_tree |
| 56 |
self._widget_name = name |
| 57 |
self._secondary_name = secondary_name |
| 58 |
self._columns = columns |
| 59 |
self._data_types = [] |
| 60 |
self._iterator_keys = {} |
| 61 |
self._iterator_key_column = 0 |
| 62 |
self._column_ids = {} |
| 63 |
self._column_offsets = {} |
| 64 |
self._column_gvalues = {} |
| 65 |
self._column_gesture_controllers = [] |
| 66 |
self._column_numbers = None |
| 67 |
self._default_sort_column = None |
| 68 |
self._default_sort_type = Gtk.SortType.ASCENDING |
| 69 |
self._sort_column = None |
| 70 |
self._sort_type = None |
| 71 |
self._persistent_sort = persistent_sort |
| 72 |
self._columns_changed_handler = None |
| 73 |
self._last_redraw_time = 0 |
| 74 |
self._selection = self.widget.get_selection() |
| 75 |
self._h_adjustment = parent.get_hadjustment() |
| 76 |
self._v_adjustment = parent.get_vadjustment() |
| 77 |
self._v_adjustment_upper = 0 |
| 78 |
self._v_adjustment_value = 0 |
| 79 |
self._is_scrolling_to_row = False |
| 80 |
self.notify_value_handler = self._v_adjustment.connect("notify::value", self.on_v_adjustment_value) |
| 81 |
|
| 82 |
if GTK_API_VERSION >= 4: |
| 83 |
parent.set_child(self.widget) # pylint: disable=no-member |
| 84 |
else: |
| 85 |
parent.add(self.widget) # pylint: disable=no-member |
| 86 |
|
| 87 |
self._initialise_columns(columns) |
| 88 |
|
| 89 |
Accelerator("<Primary>c", self.widget, self.on_copy_cell_data_accelerator) |
| 90 |
Accelerator("<Primary>f", self.widget, self.on_start_search) |
| 91 |
|
| 92 |
Accelerator("Left", self.widget, self.on_collapse_row_accelerator) |
| 93 |
Accelerator("minus", self.widget, self.on_collapse_row_blocked_accelerator) |
| 94 |
Accelerator("Right", self.widget, self.on_expand_row_accelerator) |
| 95 |
Accelerator("plus", self.widget, self.on_expand_row_blocked_accelerator) |
| 96 |
Accelerator("equal", self.widget, self.on_expand_row_blocked_accelerator) |
| 97 |
Accelerator("backslash", self.widget, self.on_expand_row_level_accelerator) |
| 98 |
|
| 99 |
self._column_menu = self.widget.column_menu = PopupMenu( |
| 100 |
self.window.application, self.widget, callback=self.on_column_header_menu, connect_events=False) |
| 101 |
|
| 102 |
if multi_select: |
| 103 |
self.widget.set_rubber_banding(True) |
| 104 |
self._selection.set_mode(Gtk.SelectionMode.MULTIPLE) |
| 105 |
|
| 106 |
if activate_row_callback: |
| 107 |
self.widget.connect("row-activated", self.on_activate_row, activate_row_callback) |
| 108 |
|
| 109 |
if focus_in_callback: |
| 110 |
if GTK_API_VERSION >= 4: |
| 111 |
focus_controller = Gtk.EventControllerFocus() |
| 112 |
focus_controller.connect("enter", self.on_focus_in, focus_in_callback) |
| 113 |
self.widget.add_controller(focus_controller) # pylint: disable=no-member |
| 114 |
else: |
| 115 |
self.widget.connect("focus-in-event", self.on_focus_in, focus_in_callback) |
| 116 |
|
| 117 |
if select_row_callback: |
| 118 |
self._selection.connect("changed", self.on_select_row, select_row_callback) |
| 119 |
|
| 120 |
if delete_accelerator_callback: |
| 121 |
Accelerator("Delete", self.widget, self.on_delete_accelerator, delete_accelerator_callback) |
| 122 |
|
| 123 |
if search_entry: |
| 124 |
self.widget.set_search_entry(search_entry) |
| 125 |
|
| 126 |
self._query_tooltip_handler = self.widget.connect("query-tooltip", self.on_tooltip) |
| 127 |
self.widget.connect("move-cursor", self.on_key_move_cursor) |
| 128 |
self.widget.set_search_equal_func(self.on_search_match) |
| 129 |
|
| 130 |
add_css_class(self.widget, "treeview-spacing") |
| 131 |
|
| 132 |
def destroy(self): |
| 133 |
|
| 134 |
# Prevent updates while destroying widget |
| 135 |
self.widget.disconnect(self._columns_changed_handler) |
| 136 |
self.widget.disconnect(self._query_tooltip_handler) |
| 137 |
self._v_adjustment.disconnect(self.notify_value_handler) |
| 138 |
|
| 139 |
self._column_menu.destroy() |
| 140 |
self.__dict__.clear() |
| 141 |
|
| 142 |
def create_model(self): |
| 143 |
|
| 144 |
# Bypass Tree/ListStore overrides for improved performance in set_value() |
| 145 |
gtk_module = gi.module.get_introspection_module("Gtk") |
| 146 |
model_class = gtk_module.TreeStore if self.has_tree else gtk_module.ListStore |
| 147 |
|
| 148 |
if hasattr(gtk_module.ListStore, "insert_with_valuesv"): |
| 149 |
gtk_module.ListStore.insert_with_values = gtk_module.ListStore.insert_with_valuesv |
| 150 |
|
| 151 |
self.model = model_class() |
| 152 |
self.model.set_column_types(self._data_types) |
| 153 |
|
| 154 |
if self._sort_column is not None and self._sort_type is not None: |
| 155 |
self.model.set_sort_column_id(self._sort_column, self._sort_type) |
| 156 |
|
| 157 |
self.widget.set_model(self.model) |
| 158 |
return self.model |
| 159 |
|
| 160 |
def redraw(self): |
| 161 |
"""Workaround for GTK 3 issue where GtkTreeView doesn't refresh changed |
| 162 |
values if horizontal scrolling is present while fixed-height mode is |
| 163 |
enabled.""" |
| 164 |
|
| 165 |
if GTK_API_VERSION != 3 or self._h_adjustment.get_value() <= 0: |
| 166 |
return |
| 167 |
|
| 168 |
current_time = time.monotonic() |
| 169 |
|
| 170 |
if (current_time - self._last_redraw_time) < 1: |
| 171 |
return |
| 172 |
|
| 173 |
self._last_redraw_time = current_time |
| 174 |
self.widget.queue_draw() |
| 175 |
|
| 176 |
def _append_columns(self, cols, column_config): |
| 177 |
|
| 178 |
# Restore column order from config |
| 179 |
for column_id in column_config: |
| 180 |
column = cols.get(column_id) |
| 181 |
|
| 182 |
if column is not None: |
| 183 |
self.widget.append_column(column) |
| 184 |
|
| 185 |
added_columns = self.widget.get_columns() |
| 186 |
|
| 187 |
# If any columns were missing in the config, append them |
| 188 |
for index, column in enumerate(cols.values()): |
| 189 |
if column not in added_columns: |
| 190 |
self.widget.insert_column(column, index) |
| 191 |
|
| 192 |
# Read Show / Hide column settings from last session |
| 193 |
for column_id, column in cols.items(): |
| 194 |
column.set_visible(bool(column_config.get(column_id, {}).get("visible", True))) |
| 195 |
|
| 196 |
def _update_column_properties(self, *_args): |
| 197 |
|
| 198 |
columns = self.widget.get_columns() |
| 199 |
resizable_set = False |
| 200 |
|
| 201 |
for column in reversed(columns): |
| 202 |
if not column.get_visible(): |
| 203 |
continue |
| 204 |
|
| 205 |
if not resizable_set: |
| 206 |
# Make sure the last visible column isn't resizable |
| 207 |
column.set_resizable(False) |
| 208 |
column.set_fixed_width(-1) |
| 209 |
|
| 210 |
resizable_set = True |
| 211 |
continue |
| 212 |
|
| 213 |
# Make the previously last column resizable again |
| 214 |
column.set_resizable(True) |
| 215 |
break |
| 216 |
|
| 217 |
# Set first non-icon column as the expander column |
| 218 |
for column in columns: |
| 219 |
if column.type != "icon" and column.get_visible(): |
| 220 |
self.widget.set_expander_column(column) |
| 221 |
break |
| 222 |
|
| 223 |
def _initialise_column_ids(self, columns): |
| 224 |
|
| 225 |
self._data_types = [] |
| 226 |
int_types = {GObject.TYPE_UINT, GObject.TYPE_UINT64} |
| 227 |
|
| 228 |
for column_index, (column_id, column_data) in enumerate(columns.items()): |
| 229 |
data_type = column_data.get("data_type") |
| 230 |
|
| 231 |
if not data_type: |
| 232 |
column_type = column_data.get("column_type") |
| 233 |
|
| 234 |
if column_type == "progress": |
| 235 |
data_type = GObject.TYPE_INT |
| 236 |
|
| 237 |
elif column_type == "toggle": |
| 238 |
data_type = GObject.TYPE_BOOLEAN |
| 239 |
|
| 240 |
else: |
| 241 |
data_type = GObject.TYPE_STRING |
| 242 |
|
| 243 |
self._column_ids[column_id] = column_index |
| 244 |
self._data_types.append(data_type) |
| 245 |
|
| 246 |
if data_type not in int_types: |
| 247 |
continue |
| 248 |
|
| 249 |
self._column_gvalues[column_index] = value = GObject.Value(data_type) |
| 250 |
|
| 251 |
# Optimization: bypass PyGObject's set_value override |
| 252 |
value.set_value = value.set_uint if data_type == GObject.TYPE_UINT else value.set_uint64 |
| 253 |
|
| 254 |
self._column_numbers = list(self._column_ids.values()) |
| 255 |
|
| 256 |
def _initialise_columns(self, columns): |
| 257 |
|
| 258 |
self._initialise_column_ids(columns) |
| 259 |
self.model = self.create_model() |
| 260 |
|
| 261 |
progress_padding = 1 |
| 262 |
height_padding = 4 |
| 263 |
width_padding = 10 if GTK_API_VERSION >= 4 else 12 |
| 264 |
|
| 265 |
column_widgets = {} |
| 266 |
column_config = {} |
| 267 |
has_visible_column_header = False |
| 268 |
|
| 269 |
for column_index, (column_id, column_data) in enumerate(columns.items()): |
| 270 |
title = column_data.get("title") |
| 271 |
iterator_key = column_data.get("iterator_key") |
| 272 |
sort_data_column = column_data.get("sort_column", column_id) |
| 273 |
sort_column_id = self._column_ids[sort_data_column] |
| 274 |
default_sort_type = column_data.get("default_sort_type") |
| 275 |
|
| 276 |
if iterator_key: |
| 277 |
# Use values from this column as keys for iterator mapping |
| 278 |
self._iterator_key_column = column_index |
| 279 |
|
| 280 |
if default_sort_type: |
| 281 |
# Sort treeview by values in this column by default |
| 282 |
self._default_sort_column = sort_column_id |
| 283 |
self._default_sort_type = (Gtk.SortType.DESCENDING if default_sort_type == "descending" |
| 284 |
else Gtk.SortType.ASCENDING) |
| 285 |
|
| 286 |
if self._sort_column is None and self._sort_type is None: |
| 287 |
self._sort_column = self._default_sort_column |
| 288 |
self._sort_type = self._default_sort_type |
| 289 |
|
| 290 |
self.model.set_sort_column_id(self._default_sort_column, self._default_sort_type) |
| 291 |
|
| 292 |
if title is None: |
| 293 |
# Hidden data column |
| 294 |
continue |
| 295 |
|
| 296 |
column_type = column_data["column_type"] |
| 297 |
width = column_data.get("width") |
| 298 |
should_expand_column = column_data.get("expand_column") |
| 299 |
sensitive_column = column_data.get("sensitive_column") |
| 300 |
|
| 301 |
if self._widget_name: |
| 302 |
try: |
| 303 |
column_config = config.sections["columns"][self._widget_name][self._secondary_name] |
| 304 |
except KeyError: |
| 305 |
column_config = config.sections["columns"][self._widget_name] |
| 306 |
|
| 307 |
column_properties = column_config.get(column_id, {}) |
| 308 |
column_sort_type = column_properties.get("sort") |
| 309 |
|
| 310 |
# Restore saved column width |
| 311 |
if column_type != "icon": |
| 312 |
width = column_properties.get("width", width) |
| 313 |
|
| 314 |
if column_sort_type and self._persistent_sort: |
| 315 |
# Sort treeview by values in this column by default |
| 316 |
self._sort_column = sort_column_id |
| 317 |
self._sort_type = (Gtk.SortType.DESCENDING if column_sort_type == "descending" |
| 318 |
else Gtk.SortType.ASCENDING) |
| 319 |
self.model.set_sort_column_id(self._sort_column, self._sort_type) |
| 320 |
|
| 321 |
# Allow individual cells to receive visual focus |
| 322 |
mode = Gtk.CellRendererMode.ACTIVATABLE if len(columns) > 1 else Gtk.CellRendererMode.INERT |
| 323 |
xalign = 0.0 |
| 324 |
|
| 325 |
if column_type == "text": |
| 326 |
renderer = Gtk.CellRendererText( |
| 327 |
mode=mode, single_paragraph_mode=True, xpad=width_padding, ypad=height_padding |
| 328 |
) |
| 329 |
column = Gtk.TreeViewColumn(title=title, cell_renderer=renderer, text=column_index) |
| 330 |
text_underline_column = column_data.get("text_underline_column") |
| 331 |
text_weight_column = column_data.get("text_weight_column") |
| 332 |
|
| 333 |
if text_underline_column: |
| 334 |
column.add_attribute(renderer, "underline", self._column_ids[text_underline_column]) |
| 335 |
|
| 336 |
if text_weight_column: |
| 337 |
column.add_attribute(renderer, "weight", self._column_ids[text_weight_column]) |
| 338 |
|
| 339 |
elif column_type == "number": |
| 340 |
xalign = 1 |
| 341 |
renderer = Gtk.CellRendererText(mode=mode, xalign=xalign, xpad=width_padding, ypad=height_padding) |
| 342 |
column = Gtk.TreeViewColumn(title=title, cell_renderer=renderer, text=column_index) |
| 343 |
column.set_alignment(xalign) |
| 344 |
|
| 345 |
elif column_type == "progress": |
| 346 |
xalign = 1 |
| 347 |
renderer = Gtk.CellRendererProgress(mode=mode, ypad=progress_padding) |
| 348 |
column = Gtk.TreeViewColumn(title=title, cell_renderer=renderer, value=column_index) |
| 349 |
column.set_alignment(xalign) |
| 350 |
|
| 351 |
elif column_type == "toggle": |
| 352 |
xalign = 0.5 |
| 353 |
renderer = Gtk.CellRendererToggle(mode=mode, xalign=xalign, xpad=13) |
| 354 |
renderer.connect("toggled", self.on_toggle, column_data["toggle_callback"]) |
| 355 |
|
| 356 |
column = Gtk.TreeViewColumn(title=title, cell_renderer=renderer, active=column_index) |
| 357 |
|
| 358 |
elif column_type == "icon": |
| 359 |
icon_args = {} |
| 360 |
|
| 361 |
if column_id == "country": |
| 362 |
if GTK_API_VERSION >= 4: |
| 363 |
# Custom icon size defined in theme.py |
| 364 |
icon_args["icon_size"] = Gtk.IconSize.NORMAL # pylint: disable=no-member |
| 365 |
else: |
| 366 |
# Use the same size as the original icon |
| 367 |
icon_args["stock_size"] = 0 |
| 368 |
|
| 369 |
renderer = Gtk.CellRendererPixbuf(mode=mode, xalign=1.0, **icon_args) |
| 370 |
column = Gtk.TreeViewColumn(title=title, cell_renderer=renderer, icon_name=column_index) |
| 371 |
|
| 372 |
column_header = column.get_button() |
| 373 |
|
| 374 |
if GTK_API_VERSION >= 4: |
| 375 |
gesture_click = Gtk.GestureClick() |
| 376 |
column_header.add_controller(gesture_click) # pylint: disable=no-member |
| 377 |
else: |
| 378 |
gesture_click = Gtk.GestureMultiPress(widget=column_header) |
| 379 |
|
| 380 |
gesture_click.set_propagation_phase(Gtk.PropagationPhase.CAPTURE) |
| 381 |
gesture_click.connect("released", self.on_column_header_pressed, column_id, sort_column_id) |
| 382 |
self._column_gesture_controllers.append(gesture_click) |
| 383 |
|
| 384 |
title_container = next(iter(column_header)) |
| 385 |
title_widget = next(iter(title_container)) if xalign < 1 else list(title_container)[-1] |
| 386 |
|
| 387 |
if column_data.get("hide_header"): |
| 388 |
title_widget.set_visible(False) |
| 389 |
else: |
| 390 |
has_visible_column_header = True |
| 391 |
|
| 392 |
# Required for fixed height mode |
| 393 |
column.set_sizing(Gtk.TreeViewColumnSizing.FIXED) |
| 394 |
|
| 395 |
if width is not None: |
| 396 |
column.set_resizable(column_type != "icon") |
| 397 |
|
| 398 |
if isinstance(width, int) and width > 0: |
| 399 |
column.set_fixed_width(width) |
| 400 |
|
| 401 |
column.set_reorderable(True) |
| 402 |
column.set_min_width(24) |
| 403 |
|
| 404 |
if xalign == 1 and GTK_API_VERSION >= 4: |
| 405 |
# Gtk.TreeViewColumn.set_alignment() only changes the sort arrow position in GTK 4 |
| 406 |
# Actually align the label to the right here instead |
| 407 |
title_widget.set_halign(Gtk.Align.END) |
| 408 |
|
| 409 |
if sensitive_column: |
| 410 |
column.add_attribute(renderer, "sensitive", self._column_ids[sensitive_column]) |
| 411 |
|
| 412 |
if should_expand_column: |
| 413 |
column.set_expand(True) |
| 414 |
|
| 415 |
if self._widget_name: |
| 416 |
column.connect("notify::x-offset", self.on_column_position_changed) |
| 417 |
|
| 418 |
column.id = column_id |
| 419 |
column.type = column_type |
| 420 |
column.tooltip_callback = column_data.get("tooltip_callback") |
| 421 |
|
| 422 |
column.set_sort_column_id(sort_column_id) |
| 423 |
column_widgets[column_id] = column |
| 424 |
|
| 425 |
self.widget.set_headers_visible(has_visible_column_header) |
| 426 |
|
| 427 |
self._append_columns(column_widgets, column_config) |
| 428 |
|
| 429 |
self._columns_changed_handler = self.widget.connect("columns-changed", self._update_column_properties) |
| 430 |
self.widget.emit("columns-changed") |
| 431 |
|
| 432 |
def save_columns(self): |
| 433 |
"""Save a treeview's column widths and visibilities for the next |
| 434 |
session.""" |
| 435 |
|
| 436 |
if not self._widget_name: |
| 437 |
return |
| 438 |
|
| 439 |
saved_columns = {} |
| 440 |
column_config = config.sections["columns"] |
| 441 |
|
| 442 |
for column in self.widget.get_columns(): |
| 443 |
title = column.id |
| 444 |
width = column.get_width() |
| 445 |
visible = column.get_visible() |
| 446 |
sort_column_id = column.get_sort_column_id() |
| 447 |
|
| 448 |
# A column width of zero should not be saved to the config. |
| 449 |
# When a column is hidden, the correct width will be remembered during the |
| 450 |
# run it was hidden. Subsequent runs will yield a zero width, so we |
| 451 |
# attempt to re-use a previously saved non-zero column width instead. |
| 452 |
try: |
| 453 |
if width <= 0: |
| 454 |
if not visible: |
| 455 |
saved_columns[title] = { |
| 456 |
"visible": visible, |
| 457 |
"width": column_config[self._widget_name][title]["width"] |
| 458 |
} |
| 459 |
|
| 460 |
continue |
| 461 |
|
| 462 |
except KeyError: |
| 463 |
# No previously saved width, going with zero |
| 464 |
pass |
| 465 |
|
| 466 |
saved_columns[title] = columns = {"visible": visible, "width": width} |
| 467 |
|
| 468 |
if not self._persistent_sort: |
| 469 |
continue |
| 470 |
|
| 471 |
if sort_column_id == self._sort_column and sort_column_id != self._default_sort_column: |
| 472 |
columns["sort"] = "descending" if self._sort_type == Gtk.SortType.DESCENDING else "ascending" |
| 473 |
|
| 474 |
if self._secondary_name is not None: |
| 475 |
if self._widget_name not in column_config: |
| 476 |
column_config[self._widget_name] = {} |
| 477 |
|
| 478 |
column_config[self._widget_name][self._secondary_name] = saved_columns |
| 479 |
else: |
| 480 |
column_config[self._widget_name] = saved_columns |
| 481 |
|
| 482 |
def freeze(self): |
| 483 |
self.model.set_sort_column_id(Gtk.TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, Gtk.SortType.ASCENDING) |
| 484 |
|
| 485 |
def unfreeze(self): |
| 486 |
if self._sort_column is not None and self._sort_type is not None: |
| 487 |
self.model.set_sort_column_id(self._sort_column, self._sort_type) |
| 488 |
|
| 489 |
def set_show_expanders(self, show): |
| 490 |
self.widget.set_show_expanders(show) |
| 491 |
|
| 492 |
def add_row(self, values, select_row=True, parent_iterator=None): |
| 493 |
|
| 494 |
key = values[self._iterator_key_column] |
| 495 |
|
| 496 |
if key in self.iterators: |
| 497 |
return None |
| 498 |
|
| 499 |
position = 0 # Insert at the beginning for large performance improvement |
| 500 |
value_columns = [] |
| 501 |
included_values = [] |
| 502 |
|
| 503 |
for index, value in enumerate(values): |
| 504 |
if not value and index is not self._sort_column: |
| 505 |
# Skip empty values if not active sort column to avoid unnecessary work |
| 506 |
continue |
| 507 |
|
| 508 |
if index in self._column_gvalues: |
| 509 |
# Need gvalue conversion for large integers |
| 510 |
gvalue = self._column_gvalues[index] |
| 511 |
gvalue.set_value(value or 0) |
| 512 |
value = gvalue |
| 513 |
|
| 514 |
value_columns.append(index) |
| 515 |
included_values.append(value) |
| 516 |
|
| 517 |
if self.has_tree: |
| 518 |
self.iterators[key] = iterator = self.model.insert_with_values( |
| 519 |
parent_iterator, position, value_columns, included_values |
| 520 |
) |
| 521 |
else: |
| 522 |
self.iterators[key] = iterator = self.model.insert_with_values( |
| 523 |
position, value_columns, included_values |
| 524 |
) |
| 525 |
|
| 526 |
self._iterator_keys[iterator] = key |
| 527 |
|
| 528 |
if select_row: |
| 529 |
self.select_row(iterator) |
| 530 |
|
| 531 |
return iterator |
| 532 |
|
| 533 |
def get_selected_rows(self): |
| 534 |
|
| 535 |
_model, paths = self._selection.get_selected_rows() |
| 536 |
|
| 537 |
for path in paths: |
| 538 |
yield self.model.get_iter(path) |
| 539 |
|
| 540 |
def get_num_selected_rows(self): |
| 541 |
return self._selection.count_selected_rows() |
| 542 |
|
| 543 |
def get_focused_row(self): |
| 544 |
|
| 545 |
path, _column = self.widget.get_cursor() |
| 546 |
|
| 547 |
if path is None: |
| 548 |
return None |
| 549 |
|
| 550 |
return self.model.get_iter(path) |
| 551 |
|
| 552 |
def get_row_value(self, iterator, column_id): |
| 553 |
return self.model.get_value(iterator, self._column_ids[column_id]) |
| 554 |
|
| 555 |
def set_row_value(self, iterator, column_id, value): |
| 556 |
|
| 557 |
column_index = self._column_ids[column_id] |
| 558 |
|
| 559 |
if column_index in self._column_gvalues: |
| 560 |
# Need gvalue conversion for large integers |
| 561 |
gvalue = self._column_gvalues[column_index] |
| 562 |
gvalue.set_value(value) |
| 563 |
value = gvalue |
| 564 |
|
| 565 |
return self.model.set_value(iterator, column_index, value) |
| 566 |
|
| 567 |
def set_row_values(self, iterator, column_ids, values): |
| 568 |
|
| 569 |
value_columns = [] |
| 570 |
|
| 571 |
for index, column_id in enumerate(column_ids): |
| 572 |
column_index = self._column_ids[column_id] |
| 573 |
|
| 574 |
if column_index in self._column_gvalues: |
| 575 |
# Need gvalue conversion for large integers |
| 576 |
gvalue = self._column_gvalues[column_index] |
| 577 |
gvalue.set_value(values[index]) |
| 578 |
values[index] = gvalue |
| 579 |
|
| 580 |
value_columns.append(column_index) |
| 581 |
|
| 582 |
return self.model.set(iterator, value_columns, values) |
| 583 |
|
| 584 |
def remove_row(self, iterator): |
| 585 |
del self.iterators[self._iterator_keys[iterator]] |
| 586 |
self.model.remove(iterator) |
| 587 |
|
| 588 |
def select_row(self, iterator=None, expand_rows=True, should_scroll=True): |
| 589 |
|
| 590 |
if iterator is None: |
| 591 |
# Select first row if available |
| 592 |
iterator = self.model.get_iter_first() |
| 593 |
|
| 594 |
if iterator is None: |
| 595 |
return |
| 596 |
|
| 597 |
if should_scroll: |
| 598 |
path = self.model.get_path(iterator) |
| 599 |
|
| 600 |
if expand_rows: |
| 601 |
self.widget.expand_to_path(path) |
| 602 |
|
| 603 |
self._is_scrolling_to_row = True |
| 604 |
self.widget.set_cursor(path) |
| 605 |
self.widget.scroll_to_cell(path, column=None, use_align=True, row_align=0.5, col_align=0.5) |
| 606 |
return |
| 607 |
|
| 608 |
self._selection.select_iter(iterator) |
| 609 |
|
| 610 |
def select_all_rows(self): |
| 611 |
self._selection.select_all() |
| 612 |
|
| 613 |
def unselect_all_rows(self): |
| 614 |
self._selection.unselect_all() |
| 615 |
|
| 616 |
def expand_row(self, iterator): |
| 617 |
path = self.model.get_path(iterator) |
| 618 |
return self.widget.expand_row(path, open_all=False) |
| 619 |
|
| 620 |
def collapse_row(self, iterator): |
| 621 |
path = self.model.get_path(iterator) |
| 622 |
return self.widget.collapse_row(path) |
| 623 |
|
| 624 |
def expand_all_rows(self): |
| 625 |
self.widget.expand_all() |
| 626 |
|
| 627 |
def collapse_all_rows(self): |
| 628 |
self.widget.collapse_all() |
| 629 |
|
| 630 |
def expand_root_rows(self): |
| 631 |
|
| 632 |
model = self.model |
| 633 |
iterator = model.get_iter_first() |
| 634 |
|
| 635 |
while iterator: |
| 636 |
path = model.get_path(iterator) |
| 637 |
self.widget.expand_row(path, open_all=False) |
| 638 |
iterator = model.iter_next(iterator) |
| 639 |
|
| 640 |
def get_focused_column(self): |
| 641 |
_path, column = self.widget.get_cursor() |
| 642 |
return column.id |
| 643 |
|
| 644 |
def get_visible_columns(self): |
| 645 |
|
| 646 |
for column in self.widget.get_columns(): |
| 647 |
if column.get_visible(): |
| 648 |
yield column.id |
| 649 |
|
| 650 |
def is_empty(self): |
| 651 |
return not self.iterators |
| 652 |
|
| 653 |
def is_selection_empty(self): |
| 654 |
return self._selection.count_selected_rows() <= 0 |
| 655 |
|
| 656 |
def is_row_expanded(self, iterator): |
| 657 |
path = self.model.get_path(iterator) |
| 658 |
return self.widget.row_expanded(path) |
| 659 |
|
| 660 |
def is_row_selected(self, iterator): |
| 661 |
return self._selection.iter_is_selected(iterator) |
| 662 |
|
| 663 |
def grab_focus(self): |
| 664 |
self.widget.grab_focus() |
| 665 |
|
| 666 |
def clear(self): |
| 667 |
|
| 668 |
self.widget.set_model(None) |
| 669 |
self.freeze() |
| 670 |
|
| 671 |
self.model.clear() |
| 672 |
self.iterators.clear() |
| 673 |
self._iterator_keys.clear() |
| 674 |
|
| 675 |
self.unfreeze() |
| 676 |
self.widget.set_model(self.model) |
| 677 |
|
| 678 |
@staticmethod |
| 679 |
def get_icon_label(column, icon_name, is_short_country_label=False): |
| 680 |
|
| 681 |
if column.id == "country": |
| 682 |
country_code = icon_name[-2:].upper() |
| 683 |
|
| 684 |
if is_short_country_label: |
| 685 |
return country_code |
| 686 |
|
| 687 |
country_name = core.network_filter.COUNTRIES.get(country_code, _("Unknown")) |
| 688 |
return f"{country_name} ({country_code})" |
| 689 |
|
| 690 |
if column.id == "status": |
| 691 |
return USER_STATUS_ICON_LABELS[icon_name] |
| 692 |
|
| 693 |
if column.id == "file_type": |
| 694 |
return FILE_TYPE_ICON_LABELS[icon_name] |
| 695 |
|
| 696 |
return icon_name |
| 697 |
|
| 698 |
def on_toggle(self, _widget, path, callback): |
| 699 |
callback(self, self.model.get_iter(path)) |
| 700 |
|
| 701 |
def on_activate_row(self, _widget, path, column, callback): |
| 702 |
callback(self, self.model.get_iter(path), column.id) |
| 703 |
|
| 704 |
def on_focus_in(self, *args): |
| 705 |
|
| 706 |
if GTK_API_VERSION >= 4: |
| 707 |
_widget, callback = args |
| 708 |
else: |
| 709 |
_widget, _controller, callback = args |
| 710 |
|
| 711 |
callback(self) |
| 712 |
|
| 713 |
def on_select_row(self, selection, callback): |
| 714 |
|
| 715 |
iterator = None |
| 716 |
|
| 717 |
if self.multi_select: |
| 718 |
iterator = next(self.get_selected_rows(), None) |
| 719 |
else: |
| 720 |
_model, iterator = selection.get_selected() |
| 721 |
|
| 722 |
callback(self, iterator) |
| 723 |
|
| 724 |
def on_delete_accelerator(self, _treeview, _state, callback): |
| 725 |
callback(self) |
| 726 |
|
| 727 |
def on_column_header_pressed(self, controller, _num_p, _pos_x, _pos_y, column_id, sort_column_id): |
| 728 |
"""Reset sorting when column header has been pressed three times.""" |
| 729 |
|
| 730 |
self._sort_column, self._sort_type = self.model.get_sort_column_id() |
| 731 |
|
| 732 |
if self._default_sort_column is None: |
| 733 |
# No default sort column for treeview, keep standard GTK behavior |
| 734 |
self.save_columns() |
| 735 |
return False |
| 736 |
|
| 737 |
if self._data_types[sort_column_id] == GObject.TYPE_STRING or column_id in {"in_queue", "queue_position"}: |
| 738 |
# String value (or queue position column): ascending sort by default |
| 739 |
first_sort_type = Gtk.SortType.ASCENDING |
| 740 |
second_sort_type = Gtk.SortType.DESCENDING |
| 741 |
else: |
| 742 |
# Numerical value: descending sort by default |
| 743 |
first_sort_type = Gtk.SortType.DESCENDING |
| 744 |
second_sort_type = Gtk.SortType.ASCENDING |
| 745 |
|
| 746 |
if self._sort_column != sort_column_id: |
| 747 |
self._sort_column = sort_column_id |
| 748 |
self._sort_type = first_sort_type |
| 749 |
|
| 750 |
elif self._sort_type == first_sort_type: |
| 751 |
self._sort_type = second_sort_type |
| 752 |
|
| 753 |
elif self._sort_type == second_sort_type: |
| 754 |
# Reset treeview to default state |
| 755 |
self._sort_column = self._default_sort_column |
| 756 |
self._sort_type = self._default_sort_type |
| 757 |
|
| 758 |
self.model.set_sort_column_id(self._sort_column, self._sort_type) |
| 759 |
self.save_columns() |
| 760 |
|
| 761 |
controller.set_state(Gtk.EventSequenceState.CLAIMED) |
| 762 |
return True |
| 763 |
|
| 764 |
def on_column_header_toggled(self, _action, _state, column): |
| 765 |
column.set_visible(not column.get_visible()) |
| 766 |
self._update_column_properties() |
| 767 |
|
| 768 |
def on_column_header_menu(self, menu, _treeview): |
| 769 |
|
| 770 |
columns = self.widget.get_columns() |
| 771 |
visible_columns = [column for column in columns if column.get_visible()] |
| 772 |
menu.clear() |
| 773 |
|
| 774 |
for column_num, column in enumerate(columns, start=1): |
| 775 |
title = column.get_title() |
| 776 |
|
| 777 |
if not title: |
| 778 |
title = _("Column #%i") % column_num |
| 779 |
|
| 780 |
menu.add_items( |
| 781 |
("$" + title, None) |
| 782 |
) |
| 783 |
menu.update_model() |
| 784 |
menu.actions[title].set_state(GLib.Variant.new_boolean(column in visible_columns)) |
| 785 |
|
| 786 |
if column in visible_columns: |
| 787 |
menu.actions[title].set_enabled(len(visible_columns) > 1) |
| 788 |
|
| 789 |
menu.actions[title].connect("activate", self.on_column_header_toggled, column) |
| 790 |
|
| 791 |
def on_column_position_changed(self, column, _param): |
| 792 |
"""Save column position and width to config.""" |
| 793 |
|
| 794 |
column_id = column.id |
| 795 |
offset = column.get_x_offset() |
| 796 |
|
| 797 |
if self._column_offsets.get(column_id) == offset: |
| 798 |
return |
| 799 |
|
| 800 |
self._column_offsets[column_id] = offset |
| 801 |
self.save_columns() |
| 802 |
|
| 803 |
def on_key_move_cursor(self, _widget, step, *_args): |
| 804 |
|
| 805 |
if step != Gtk.MovementStep.BUFFER_ENDS: |
| 806 |
return |
| 807 |
|
| 808 |
# We are scrolling to the end using the End key. Disable the |
| 809 |
# auto-scroll workaround to actually change the scroll adjustment value. |
| 810 |
self._is_scrolling_to_row = True |
| 811 |
|
| 812 |
def on_v_adjustment_value(self, *_args): |
| 813 |
|
| 814 |
upper = self._v_adjustment.get_upper() |
| 815 |
|
| 816 |
if not self._is_scrolling_to_row and upper != self._v_adjustment_upper and self._v_adjustment_value <= 0: |
| 817 |
# When new rows are added while sorting is enabled, treeviews |
| 818 |
# auto-scroll to the new position of the currently visible row. |
| 819 |
# Disable this behavior while we're at the top to prevent jumping |
| 820 |
# to random positions as rows are populated. |
| 821 |
self._v_adjustment.set_value(0) |
| 822 |
else: |
| 823 |
self._v_adjustment_value = self._v_adjustment.get_value() |
| 824 |
|
| 825 |
self._v_adjustment_upper = upper |
| 826 |
self._is_scrolling_to_row = False |
| 827 |
|
| 828 |
def on_search_match(self, model, _column, search_term, iterator): |
| 829 |
|
| 830 |
if not search_term: |
| 831 |
return True |
| 832 |
|
| 833 |
accepted_column_types = {"text", "number"} |
| 834 |
|
| 835 |
for column_index, column_data in enumerate(self._columns.values()): |
| 836 |
if "column_type" not in column_data: |
| 837 |
continue |
| 838 |
|
| 839 |
if column_data["column_type"] not in accepted_column_types: |
| 840 |
continue |
| 841 |
|
| 842 |
column_value = model.get_value(iterator, column_index) |
| 843 |
|
| 844 |
if column_value and search_term.lower() in column_value.lower(): |
| 845 |
return False |
| 846 |
|
| 847 |
return True |
| 848 |
|
| 849 |
def on_tooltip(self, _widget, pos_x, pos_y, _keyboard_mode, tooltip): |
| 850 |
|
| 851 |
bin_x, bin_y = self.widget.convert_widget_to_bin_window_coords(pos_x, pos_y) |
| 852 |
is_blank, path, column, _cell_x, _cell_y = self.widget.is_blank_at_pos(bin_x, bin_y) |
| 853 |
|
| 854 |
if is_blank: |
| 855 |
return False |
| 856 |
|
| 857 |
iterator = self.model.get_iter(path) |
| 858 |
|
| 859 |
if column.tooltip_callback: |
| 860 |
value = column.tooltip_callback(self, iterator) |
| 861 |
else: |
| 862 |
value = self.get_row_value(iterator, column.id) |
| 863 |
|
| 864 |
if not value: |
| 865 |
return False |
| 866 |
|
| 867 |
if not isinstance(value, str): |
| 868 |
return False |
| 869 |
|
| 870 |
if column.type == "icon": |
| 871 |
value = self.get_icon_label(column, value) |
| 872 |
|
| 873 |
# Update tooltip position |
| 874 |
self.widget.set_tooltip_cell(tooltip, path, column) |
| 875 |
|
| 876 |
tooltip.set_text(value) |
| 877 |
return True |
| 878 |
|
| 879 |
def on_copy_cell_data_accelerator(self, *_args): |
| 880 |
"""Ctrl+C: copy cell data.""" |
| 881 |
|
| 882 |
path, column = self.widget.get_cursor() |
| 883 |
|
| 884 |
if path is None: |
| 885 |
return False |
| 886 |
|
| 887 |
iterator = self.model.get_iter(path) |
| 888 |
value = str(self.model.get_value(iterator, column.get_sort_column_id())) |
| 889 |
|
| 890 |
if not value: |
| 891 |
return False |
| 892 |
|
| 893 |
if column.type == "icon": |
| 894 |
value = self.get_icon_label(column, value, is_short_country_label=True) |
| 895 |
|
| 896 |
clipboard.copy_text(value) |
| 897 |
return True |
| 898 |
|
| 899 |
def on_start_search(self, *_args): |
| 900 |
"""Ctrl+F: start search.""" |
| 901 |
|
| 902 |
self.widget.emit("start-interactive-search") |
| 903 |
|
| 904 |
def on_collapse_row_accelerator(self, *_args): |
| 905 |
"""Left: collapse row.""" |
| 906 |
|
| 907 |
iterator = self.get_focused_row() |
| 908 |
|
| 909 |
if iterator is None: |
| 910 |
return False |
| 911 |
|
| 912 |
return self.collapse_row(iterator) |
| 913 |
|
| 914 |
def on_collapse_row_blocked_accelerator(self, *_args): |
| 915 |
"""minus: collapse row (block search).""" |
| 916 |
|
| 917 |
self.on_collapse_row_accelerator() |
| 918 |
return True |
| 919 |
|
| 920 |
def on_expand_row_accelerator(self, *_args): |
| 921 |
"""Right: expand row.""" |
| 922 |
|
| 923 |
iterator = self.get_focused_row() |
| 924 |
|
| 925 |
if iterator is None: |
| 926 |
return False |
| 927 |
|
| 928 |
return self.expand_row(iterator) |
| 929 |
|
| 930 |
def on_expand_row_blocked_accelerator(self, *_args): |
| 931 |
"""plus, equal: expand row (block search).""" |
| 932 |
|
| 933 |
self.on_expand_row_accelerator() |
| 934 |
return True |
| 935 |
|
| 936 |
def on_expand_row_level_accelerator(self, *_args): |
| 937 |
"""\backslash: collapse or expand to show subs.""" |
| 938 |
|
| 939 |
iterator = self.get_focused_row() |
| 940 |
|
| 941 |
if iterator is None: |
| 942 |
return False |
| 943 |
|
| 944 |
self.collapse_row(iterator) # show 2nd level |
| 945 |
self.expand_row(iterator) |
| 946 |
return True |
| 947 |
|
| 948 |
|
| 949 |
# Legacy Functions (to be removed) # |
| 950 |
|
| 951 |
|
| 952 |
def create_grouping_menu(window, active_mode, callback): |
| 953 |
|
| 954 |
action_id = f"grouping-{GLib.uuid_string_random()}" |
| 955 |
menu = Gio.Menu() |
| 956 |
|
| 957 |
menuitem = Gio.MenuItem.new(_("Ungrouped"), f"win.{action_id}::ungrouped") |
| 958 |
menu.append_item(menuitem) |
| 959 |
|
| 960 |
menuitem = Gio.MenuItem.new(_("Group by Folder"), f"win.{action_id}::folder_grouping") |
| 961 |
menu.append_item(menuitem) |
| 962 |
|
| 963 |
menuitem = Gio.MenuItem.new(_("Group by User"), f"win.{action_id}::user_grouping") |
| 964 |
menu.append_item(menuitem) |
| 965 |
|
| 966 |
state = GLib.Variant.new_string(active_mode) |
| 967 |
action = Gio.SimpleAction(name=action_id, parameter_type=state.get_type(), state=state) |
| 968 |
action.connect("change-state", callback) |
| 969 |
|
| 970 |
window.add_action(action) |
| 971 |
action.change_state(state) |
| 972 |
|
| 973 |
return menu |
| 974 |
|
| 975 |
|
| 976 |
def set_treeview_selected_row(treeview, bin_x, bin_y): |
| 977 |
"""Handles row selection when right-clicking in a treeview.""" |
| 978 |
|
| 979 |
pathinfo = treeview.get_path_at_pos(bin_x, bin_y) |
| 980 |
selection = treeview.get_selection() |
| 981 |
|
| 982 |
if pathinfo is not None: |
| 983 |
path, column, _cell_x, _cell_y = pathinfo |
| 984 |
|
| 985 |
# Make sure we don't attempt to select a single row if the row is already |
| 986 |
# in a selection of multiple rows, otherwise the other rows will be unselected |
| 987 |
if selection.count_selected_rows() <= 1 or not selection.path_is_selected(path): |
| 988 |
treeview.grab_focus() |
| 989 |
treeview.set_cursor(path, column, False) |
| 990 |
else: |
| 991 |
selection.unselect_all() |