File: listmodel.py

package info (click to toggle)
frescobaldi 3.3.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,212 kB
  • sloc: python: 39,014; javascript: 263; sh: 238; makefile: 80
file content (109 lines) | stat: -rw-r--r-- 3,588 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
Simple readonly subclass of QAbstractListModel around a Python sequence.

Functions are used to present the data from an item for a role.
There are some predefined functions to use in this module.
"""

from PyQt5.QtCore import QAbstractListModel, Qt


def display(item):
    """Displays an item unchanged."""
    return item

def translate(item):
    """Calls an item (normally returning a translation of the item)."""
    return item()

def display_index(index):
    """Returns a function that displays the given index of an item."""
    return lambda item: item[index]

def translate_index(index):
    """Returns a function that translates the given index of an item."""
    return lambda item: item[index]()


class ListModel(QAbstractListModel):
    """A simple QAbstractListModel around a Python list."""
    def __init__(self, data, parent=None, display=display, edit=None, tooltip=None, icon=None):
        """Initializes the list.

        parent may be a parent QObject.
        display, tooltip and icon may be functions that extract the data from an item
        for the respective role (DisplayRole, ToolTipRole or DecorationRole).

        The original data can be found in the _data attribute.

        """
        super(ListModel, self).__init__(parent)
        self._data = data
        self._roles = {}
        if edit is None:
            edit = display
        if display:
            self._roles[Qt.DisplayRole] = display
        if edit:
            self._roles[Qt.EditRole] = edit
        if tooltip:
            self._roles[Qt.ToolTipRole] = tooltip
        if icon:
            self._roles[Qt.DecorationRole] = icon

    def setRoleFunction(self, role, function):
        """Sets a function that returns a value for a Qt.ItemDataRole.

        The function accepts an item in the data list given on construction
        as argument. If function is None, deletes a previously set function.

        """
        if function:
            self._roles[role] = function
        elif role in self._roles:
            del self._roles[role]

    def rowCount(self, parent):
        return 0 if parent.isValid() else len(self._data)

    def data(self, index, role):
        try:
            data = self._data[index.row()]
        except IndexError:
            return
        try:
            f = self._roles[role]
        except KeyError:
            return
        return f(data)

    def update(self):
        """Emits the dataChanged signal for all entries.

        This can e.g. be used to request that translated strings are redisplayed.

        """
        self.dataChanged.emit(
            self.createIndex(0, 0),
            self.createIndex(len(self._data) - 1, 0))