File: tableview.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (253 lines) | stat: -rw-r--r-- 9,060 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import sys
from itertools import chain, starmap
from typing import Sequence, Tuple, cast, Optional

import numpy as np

from AnyQt.QtCore import (
    Qt, QObject, QEvent, QSize, QAbstractProxyModel, QItemSelection,
    QItemSelectionModel, QItemSelectionRange, QAbstractItemModel
)
from AnyQt.QtGui import QPainter
from AnyQt.QtWidgets import (
    QStyle, QWidget, QStyleOptionHeader, QAbstractButton
)

import Orange.data
import Orange.data.sql.table

from Orange.widgets.data.utils.models import RichTableModel
from Orange.widgets.utils.itemmodels import TableModel
from Orange.widgets.utils.itemselectionmodel import (
    BlockSelectionModel, selection_blocks, ranges
)
from Orange.widgets.utils.tableview import TableView


class DataTableView(TableView):
    """
    A TableView with settable corner text.
    """
    class __CornerPainter(QObject):
        def drawCorner(self, button: QWidget):
            opt = QStyleOptionHeader()
            view = self.parent()
            assert isinstance(view, DataTableView)
            header = view.horizontalHeader()
            opt.initFrom(header)
            state = QStyle.State_None
            if button.isEnabled():
                state |= QStyle.State_Enabled
            if button.isActiveWindow():
                state |= QStyle.State_Active
            if button.isDown():
                state |= QStyle.State_Sunken
            opt.state = state
            opt.rect = button.rect()
            opt.text = button.text()
            opt.position = QStyleOptionHeader.OnlyOneSection
            style = header.style()
            painter = QPainter(button)
            style.drawControl(QStyle.CE_Header, opt, painter, header)

        def eventFilter(self, receiver: QObject, event: QEvent) -> bool:
            if event.type() == QEvent.Paint:
                self.drawCorner(receiver)
                return True
            return super().eventFilter(receiver, event)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__cornerText = ""
        self.__cornerButton = btn = self.findChild(QAbstractButton)
        self.__cornerButtonFilter = DataTableView.__CornerPainter(self)
        btn.installEventFilter(self.__cornerButtonFilter)
        if sys.platform == "darwin":
            btn.setAttribute(Qt.WA_MacSmallSize)

    def setCornerText(self, text: str) -> None:
        """Set the corner text."""
        self.__cornerButton.setText(text)
        self.__cornerText = text
        self.__cornerButton.update()
        assert self.__cornerButton is self.findChild(QAbstractButton)
        opt = QStyleOptionHeader()
        opt.initFrom(self.__cornerButton)
        opt.text = text
        s = self.__cornerButton.style().sizeFromContents(
            QStyle.CT_HeaderSection, opt, QSize(), self.__cornerButton
        )
        if s.isValid():
            self.verticalHeader().setMinimumWidth(s.width())

    def cornerText(self):
        """Return the corner text."""
        return self.__cornerText


def source_model(model: QAbstractItemModel) -> Optional[QAbstractItemModel]:
    while isinstance(model, QAbstractProxyModel):
        model = model.sourceModel()
    return model


def is_table_sortable(table):
    if isinstance(table, Orange.data.sql.table.SqlTable):
        return False
    elif isinstance(table, Orange.data.Table):
        return True
    else:
        return False


class RichTableView(DataTableView):
    """
    The preferred table view for RichTableModel.

    Handles the display of variable's labels keys in top left corner.
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        header = self.horizontalHeader()
        header.setSortIndicator(-1, Qt.AscendingOrder)

    def setModel(self, model: QAbstractItemModel):
        current = self.model()
        if current is not None:
            current.headerDataChanged.disconnect(self.__headerDataChanged)
        super().setModel(model)
        if model is not None:
            model.headerDataChanged.connect(self.__headerDataChanged)
            self.__headerDataChanged(Qt.Horizontal)
            select_rows = self.selectionBehavior() == TableView.SelectRows
            sel_model = BlockSelectionModel(model, selectBlocks=not select_rows)
            self.setSelectionModel(sel_model)
            self.horizontalHeader().setSortIndicator(-1, Qt.AscendingOrder)

            sortable = self.isModelSortable(model)
            if sortable != self.isSortingEnabled():
                # setSortingEnabled disconnects/reconnects Qt's internal
                # connections to model.sort(), causing client
                # sortIndicatorChange connections to trigger before the model
                # is actually sorted. Avoid unnecessary calls.
                self.setSortingEnabled(sortable)
            header = self.horizontalHeader()
            header.setSectionsClickable(sortable)
            header.setSortIndicatorShown(sortable)

    def isModelSortable(self, model: QAbstractItemModel) -> bool:
        """
        Should the `model` be sortable via the view header click.

        This predicate is called when a model is set on the view and
        enables/disables the model sorting and header section sort indicators.
        """
        model = source_model(model)
        if isinstance(model, TableModel):
            table = model.source
            return is_table_sortable(table)
        return False

    def __headerDataChanged(
            self,
            orientation: Qt.Orientation,
    ) -> None:
        if orientation == Qt.Horizontal:
            model = self.model()
            model = source_model(model)
            if isinstance(model, RichTableModel) and \
                    model.richHeaderFlags() & RichTableModel.Labels and \
                    model.columnCount() > 0:
                items = model.headerData(
                    0, Qt.Horizontal, RichTableModel.LabelsItemsRole
                )
                text = "\n"
                text += "\n".join(key for key, _ in items)
            else:
                text = ""
            self.setCornerText(text)

    def setBlockSelection(
            self, rows: Sequence[int], columns: Sequence[int]
    ) -> None:
        """
        Set the block row and column selection.

        Note
        ----
        The `rows` indices refer to the underlying TableModel's rows.

        Parameters
        ----------
        rows: Sequence[int]
            The rows to select.
        columns: Sequence[int]
            The columns to select.

        See Also
        --------
        blockSelection()
        """
        model = self.model()
        if model is None:
            return
        sel_model = self.selectionModel()
        assert isinstance(sel_model, BlockSelectionModel)
        if not rows or not columns or model.rowCount() <= rows[-1] or \
                model.columnCount() <= columns[-1]:
            # selection out of range for the model
            rows = columns = []
        proxy_chain = []
        while isinstance(model, QAbstractProxyModel):
            proxy_chain.append(model)
            model = model.sourceModel()
        assert isinstance(model, TableModel)

        rows = model.mapFromSourceRows(rows)

        selection = QItemSelection()
        rowranges = list(ranges(rows))
        colranges = list(ranges(columns))

        for rowstart, rowend in rowranges:
            for colstart, colend in colranges:
                selection.append(
                    QItemSelectionRange(
                        model.index(rowstart, colstart),
                        model.index(rowend - 1, colend - 1)
                    )
                )
        for proxy in proxy_chain[::-1]:
            selection = proxy.mapSelectionFromSource(selection)
        sel_model.select(selection, QItemSelectionModel.ClearAndSelect)

    def blockSelection(self) -> Tuple[Sequence[int], Sequence[int]]:
        """
        Return the current selected rows and columns.

        Note
        ----
        The `rows` indices refer to the underlying TableModel's rows.
        """
        model = self.model()
        if model is None:
            return [], []
        sel_model = self.selectionModel()
        selection = sel_model.selection()

        # map through the proxies into input table.
        while isinstance(model, QAbstractProxyModel):
            selection = model.mapSelectionToSource(selection)
            model = model.sourceModel()

        assert isinstance(sel_model, BlockSelectionModel)
        assert isinstance(model, TableModel)

        row_spans, col_spans = selection_blocks(selection)
        rows = list(chain.from_iterable(starmap(range, row_spans)))
        cols = list(chain.from_iterable(starmap(range, col_spans)))
        rows = np.array(rows, dtype=np.intp)
        # map the rows through the applied sorting (if any)
        rows = model.mapToSourceRows(rows)
        rows = cast(Sequence[int], rows.tolist())
        return rows, cols