2019-09-10 00:54:28 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2010-07-25
|
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2020-01-01 02:16:27 +00:00
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
2019-09-10 00:54:28 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
|
|
|
import copy
|
|
|
|
|
|
|
|
from .base import GUIObject
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
class Column:
|
|
|
|
"""Holds column attributes such as its name, width, visibility, etc.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
These attributes are then used to correctly configure the column on the "view" side.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
def __init__(self, name, display="", visible=True, optional=False):
|
2019-09-10 00:54:28 +00:00
|
|
|
#: "programmatical" (not for display) name. Used as a reference in a couple of place, such
|
|
|
|
#: as :meth:`Columns.column_by_name`.
|
|
|
|
self.name = name
|
|
|
|
#: Immutable index of the column. Doesn't change even when columns are re-ordered. Used in
|
|
|
|
#: :meth:`Columns.column_by_index`.
|
|
|
|
self.logical_index = 0
|
|
|
|
#: Index of the column in the ordered set of columns.
|
|
|
|
self.ordered_index = 0
|
|
|
|
#: Width of the column.
|
|
|
|
self.width = 0
|
|
|
|
#: Default width of the column. This value usually depends on the platform and is set on
|
|
|
|
#: columns initialisation. It will be used if column restoration doesn't contain any
|
|
|
|
#: "remembered" widths.
|
|
|
|
self.default_width = 0
|
|
|
|
#: Display name (title) of the column.
|
|
|
|
self.display = display
|
|
|
|
#: Whether the column is visible.
|
|
|
|
self.visible = visible
|
|
|
|
#: Whether the column is visible by default. It will be used if column restoration doesn't
|
|
|
|
#: contain any "remembered" widths.
|
|
|
|
self.default_visible = visible
|
|
|
|
#: Whether the column can have :attr:`visible` set to false.
|
|
|
|
self.optional = optional
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
class ColumnsView:
|
|
|
|
"""Expected interface for :class:`Columns`'s view.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
*Not actually used in the code. For documentation purposes only.*
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
Our view, the columns controller of a table or outline, is expected to properly respond to
|
|
|
|
callbacks.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def restore_columns(self):
|
|
|
|
"""Update all columns according to the model.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
When this is called, our view has to update the columns title, order and visibility of all
|
|
|
|
columns.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def set_column_visible(self, colname, visible):
|
|
|
|
"""Update visibility of column ``colname``.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
Called when the user toggles the visibility of a column, we must update the column
|
|
|
|
``colname``'s visibility status to ``visible``.
|
|
|
|
"""
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
class PrefAccessInterface:
|
|
|
|
"""Expected interface for :class:`Columns`'s prefaccess.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
*Not actually used in the code. For documentation purposes only.*
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def get_default(self, key, fallback_value):
|
|
|
|
"""Retrieve the value for ``key`` in the currently running app's preference store.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
If the key doesn't exist, return ``fallback_value``.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def set_default(self, key, value):
|
|
|
|
"""Set the value ``value`` for ``key`` in the currently running app's preference store.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
class Columns(GUIObject):
|
|
|
|
"""Cross-toolkit GUI-enabled column set for tables or outlines.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
Manages a column set's order, visibility and width. We also manage the persistence of these
|
|
|
|
attributes so that we can restore them on the next run.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
Subclasses :class:`.GUIObject`. Expected view: :class:`ColumnsView`.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
:param table: The table the columns belong to. It's from there that we retrieve our column
|
|
|
|
configuration and it must have a ``COLUMNS`` attribute which is a list of
|
|
|
|
:class:`Column`. We also call :meth:`~.GUITable.save_edits` on it from time to
|
|
|
|
time. Technically, this argument can also be a tree, but there's probably some
|
|
|
|
sorting in the code to do to support this option cleanly.
|
|
|
|
:param prefaccess: An object giving access to user preferences for the currently running app.
|
|
|
|
We use this to make column attributes persistent. Must follow
|
|
|
|
:class:`PrefAccessInterface`.
|
|
|
|
:param str savename: The name under which column preferences will be saved. This name is in fact
|
|
|
|
a prefix. Preferences are saved under more than one name, but they will all
|
|
|
|
have that same prefix.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def __init__(self, table, prefaccess=None, savename=None):
|
|
|
|
GUIObject.__init__(self)
|
|
|
|
self.table = table
|
|
|
|
self.prefaccess = prefaccess
|
|
|
|
self.savename = savename
|
|
|
|
# We use copy here for test isolation. If we don't, changing a column affects all tests.
|
|
|
|
self.column_list = list(map(copy.copy, table.COLUMNS))
|
|
|
|
for i, column in enumerate(self.column_list):
|
|
|
|
column.logical_index = i
|
|
|
|
column.ordered_index = i
|
|
|
|
self.coldata = {col.name: col for col in self.column_list}
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
# --- Private
|
2019-09-10 00:54:28 +00:00
|
|
|
def _get_colname_attr(self, colname, attrname, default):
|
|
|
|
try:
|
|
|
|
return getattr(self.coldata[colname], attrname)
|
|
|
|
except KeyError:
|
|
|
|
return default
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def _set_colname_attr(self, colname, attrname, value):
|
|
|
|
try:
|
|
|
|
col = self.coldata[colname]
|
|
|
|
setattr(col, attrname, value)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def _optional_columns(self):
|
|
|
|
return [c for c in self.column_list if c.optional]
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
# --- Override
|
2019-09-10 00:54:28 +00:00
|
|
|
def _view_updated(self):
|
|
|
|
self.restore_columns()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
# --- Public
|
2019-09-10 00:54:28 +00:00
|
|
|
def column_by_index(self, index):
|
|
|
|
"""Return the :class:`Column` having the :attr:`~Column.logical_index` ``index``.
|
|
|
|
"""
|
|
|
|
return self.column_list[index]
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def column_by_name(self, name):
|
|
|
|
"""Return the :class:`Column` having the :attr:`~Column.name` ``name``.
|
|
|
|
"""
|
|
|
|
return self.coldata[name]
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def columns_count(self):
|
|
|
|
"""Returns the number of columns in our set.
|
|
|
|
"""
|
|
|
|
return len(self.column_list)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def column_display(self, colname):
|
|
|
|
"""Returns display name for column named ``colname``, or ``''`` if there's none.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
return self._get_colname_attr(colname, "display", "")
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def column_is_visible(self, colname):
|
|
|
|
"""Returns visibility for column named ``colname``, or ``True`` if there's none.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
return self._get_colname_attr(colname, "visible", True)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def column_width(self, colname):
|
|
|
|
"""Returns width for column named ``colname``, or ``0`` if there's none.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
return self._get_colname_attr(colname, "width", 0)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def columns_to_right(self, colname):
|
|
|
|
"""Returns the list of all columns to the right of ``colname``.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
"right" meaning "having a higher :attr:`Column.ordered_index`" in our left-to-right
|
|
|
|
civilization.
|
|
|
|
"""
|
|
|
|
column = self.coldata[colname]
|
|
|
|
index = column.ordered_index
|
2020-01-01 02:16:27 +00:00
|
|
|
return [
|
|
|
|
col.name
|
|
|
|
for col in self.column_list
|
|
|
|
if (col.visible and col.ordered_index > index)
|
|
|
|
]
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def menu_items(self):
|
|
|
|
"""Returns a list of items convenient for quick visibility menu generation.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
Returns a list of ``(display_name, is_marked)`` items for each optional column in the
|
|
|
|
current view (``is_marked`` means that it's visible).
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
You can use this to generate a menu to let the user toggle the visibility of an optional
|
|
|
|
column. That is why we only show optional column, because the visibility of mandatory
|
|
|
|
columns can't be toggled.
|
|
|
|
"""
|
|
|
|
return [(c.display, c.visible) for c in self._optional_columns()]
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def move_column(self, colname, index):
|
|
|
|
"""Moves column ``colname`` to ``index``.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
The column will be placed just in front of the column currently having that index, or to the
|
|
|
|
end of the list if there's none.
|
|
|
|
"""
|
|
|
|
colnames = self.colnames
|
|
|
|
colnames.remove(colname)
|
|
|
|
colnames.insert(index, colname)
|
|
|
|
self.set_column_order(colnames)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def reset_to_defaults(self):
|
|
|
|
"""Reset all columns' width and visibility to their default values.
|
|
|
|
"""
|
|
|
|
self.set_column_order([col.name for col in self.column_list])
|
|
|
|
for col in self._optional_columns():
|
|
|
|
col.visible = col.default_visible
|
|
|
|
col.width = col.default_width
|
|
|
|
self.view.restore_columns()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def resize_column(self, colname, newwidth):
|
|
|
|
"""Set column ``colname``'s width to ``newwidth``.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
self._set_colname_attr(colname, "width", newwidth)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def restore_columns(self):
|
|
|
|
"""Restore's column persistent attributes from the last :meth:`save_columns`.
|
|
|
|
"""
|
|
|
|
if not (self.prefaccess and self.savename and self.coldata):
|
|
|
|
if (not self.savename) and (self.coldata):
|
|
|
|
# This is a table that will not have its coldata saved/restored. we should
|
|
|
|
# "restore" its default column attributes.
|
|
|
|
self.view.restore_columns()
|
|
|
|
return
|
|
|
|
for col in self.column_list:
|
2020-01-01 02:16:27 +00:00
|
|
|
pref_name = "{}.Columns.{}".format(self.savename, col.name)
|
2019-09-10 00:54:28 +00:00
|
|
|
coldata = self.prefaccess.get_default(pref_name, fallback_value={})
|
2020-01-01 02:16:27 +00:00
|
|
|
if "index" in coldata:
|
|
|
|
col.ordered_index = coldata["index"]
|
|
|
|
if "width" in coldata:
|
|
|
|
col.width = coldata["width"]
|
|
|
|
if col.optional and "visible" in coldata:
|
|
|
|
col.visible = coldata["visible"]
|
2019-09-10 00:54:28 +00:00
|
|
|
self.view.restore_columns()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def save_columns(self):
|
|
|
|
"""Save column attributes in persistent storage for restoration in :meth:`restore_columns`.
|
|
|
|
"""
|
|
|
|
if not (self.prefaccess and self.savename and self.coldata):
|
|
|
|
return
|
|
|
|
for col in self.column_list:
|
2020-01-01 02:16:27 +00:00
|
|
|
pref_name = "{}.Columns.{}".format(self.savename, col.name)
|
|
|
|
coldata = {"index": col.ordered_index, "width": col.width}
|
2019-09-10 00:54:28 +00:00
|
|
|
if col.optional:
|
2020-01-01 02:16:27 +00:00
|
|
|
coldata["visible"] = col.visible
|
2019-09-10 00:54:28 +00:00
|
|
|
self.prefaccess.set_default(pref_name, coldata)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def set_column_order(self, colnames):
|
|
|
|
"""Change the columns order so it matches the order in ``colnames``.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
:param colnames: A list of column names in the desired order.
|
|
|
|
"""
|
|
|
|
colnames = (name for name in colnames if name in self.coldata)
|
|
|
|
for i, colname in enumerate(colnames):
|
|
|
|
col = self.coldata[colname]
|
|
|
|
col.ordered_index = i
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def set_column_visible(self, colname, visible):
|
|
|
|
"""Set the visibility of column ``colname``.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
self.table.save_edits() # the table on the GUI side will stop editing when the columns change
|
|
|
|
self._set_colname_attr(colname, "visible", visible)
|
2019-09-10 00:54:28 +00:00
|
|
|
self.view.set_column_visible(colname, visible)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def set_default_width(self, colname, width):
|
|
|
|
"""Set the default width or column ``colname``.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
self._set_colname_attr(colname, "default_width", width)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def toggle_menu_item(self, index):
|
|
|
|
"""Toggles the visibility of an optional column.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
You know, that optional column menu you've generated in :meth:`menu_items`? Well, ``index``
|
|
|
|
is the index of them menu item in *that* menu that the user has clicked on to toggle it.
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
Returns whether the column in question ends up being visible or not.
|
|
|
|
"""
|
|
|
|
col = self._optional_columns()[index]
|
|
|
|
self.set_column_visible(col.name, not col.visible)
|
|
|
|
return col.visible
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
# --- Properties
|
2019-09-10 00:54:28 +00:00
|
|
|
@property
|
|
|
|
def ordered_columns(self):
|
|
|
|
"""List of :class:`Column` in visible order.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
return [
|
|
|
|
col for col in sorted(self.column_list, key=lambda col: col.ordered_index)
|
|
|
|
]
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
@property
|
|
|
|
def colnames(self):
|
|
|
|
"""List of column names in visible order.
|
|
|
|
"""
|
|
|
|
return [col.name for col in self.ordered_columns]
|