summaryrefslogtreecommitdiff
path: root/silx/gui/qt/_pyside_missing.py
blob: a7e27815145c056dd7f5c3334e1266931c8d16de (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""
Python implementation of classes which are not provided by default by PySide.
"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "17/01/2017"


from PySide.QtGui import QAbstractProxyModel
from PySide.QtCore import QModelIndex
from PySide.QtCore import Qt
from PySide.QtGui import QItemSelection
from PySide.QtGui import QItemSelectionRange


class QIdentityProxyModel(QAbstractProxyModel):
    """Python translation of the source code of Qt c++ file"""

    def __init__(self, parent=None):
        super(QIdentityProxyModel, self).__init__(parent)
        self.__ignoreNextLayoutAboutToBeChanged = False
        self.__ignoreNextLayoutChanged = False
        self.__persistentIndexes = []

    def columnCount(self, parent):
        parent = self.mapToSource(parent)
        return self.sourceModel().columnCount(parent)

    def dropMimeData(self, data, action, row, column, parent):
        parent = self.mapToSource(parent)
        return self.sourceModel().dropMimeData(data, action, row, column, parent)

    def index(self, row, column, parent=QModelIndex()):
        parent = self.mapToSource(parent)
        i = self.sourceModel().index(row, column, parent)
        return self.mapFromSource(i)

    def insertColumns(self, column, count, parent=QModelIndex()):
        parent = self.mapToSource(parent)
        return self.sourceModel().insertColumns(column, count, parent)

    def insertRows(self, row, count, parent=QModelIndex()):
        parent = self.mapToSource(parent)
        return self.sourceModel().insertRows(row, count, parent)

    def mapFromSource(self, sourceIndex):
        if self.sourceModel() is None or not sourceIndex.isValid():
            return QModelIndex()
        index = self.createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer())
        return index

    def mapSelectionFromSource(self, sourceSelection):
        proxySelection = QItemSelection()
        if self.sourceModel() is None:
            return proxySelection

        cursor = sourceSelection.constBegin()
        end = sourceSelection.constEnd()
        while cursor != end:
            topLeft = self.mapFromSource(cursor.topLeft())
            bottomRight = self.mapFromSource(cursor.bottomRight())
            proxyRange = QItemSelectionRange(topLeft, bottomRight)
            proxySelection.append(proxyRange)
            cursor += 1
        return proxySelection

    def mapSelectionToSource(self, proxySelection):
        sourceSelection = QItemSelection()
        if self.sourceModel() is None:
            return sourceSelection

        cursor = proxySelection.constBegin()
        end = proxySelection.constEnd()
        while cursor != end:
            topLeft = self.mapToSource(cursor.topLeft())
            bottomRight = self.mapToSource(cursor.bottomRight())
            sourceRange = QItemSelectionRange(topLeft, bottomRight)
            sourceSelection.append(sourceRange)
            cursor += 1
        return sourceSelection

    def mapToSource(self, proxyIndex):
        if self.sourceModel() is None or not proxyIndex.isValid():
            return QModelIndex()
        return self.sourceModel().createIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer())

    def match(self, start, role, value, hits=1, flags=Qt.MatchFlags(Qt.MatchStartsWith | Qt.MatchWrap)):
        if self.sourceModel() is None:
            return []

        start = self.mapToSource(start)
        sourceList = self.sourceModel().match(start, role, value, hits, flags)
        proxyList = []
        for cursor in sourceList:
            proxyList.append(self.mapFromSource(cursor))
        return proxyList

    def parent(self, child):
        sourceIndex = self.mapToSource(child)
        sourceParent = sourceIndex.parent()
        index = self.mapFromSource(sourceParent)
        return index

    def removeColumns(self, column, count, parent=QModelIndex()):
        parent = self.mapToSource(parent)
        return self.sourceModel().removeColumns(column, count, parent)

    def removeRows(self, row, count, parent=QModelIndex()):
        parent = self.mapToSource(parent)
        return self.sourceModel().removeRows(row, count, parent)

    def rowCount(self, parent=QModelIndex()):
        parent = self.mapToSource(parent)
        return self.sourceModel().rowCount(parent)

    def setSourceModel(self, newSourceModel):
        """Bind and unbind the source model events"""
        self.beginResetModel()

        sourceModel = self.sourceModel()
        if sourceModel is not None:
            sourceModel.rowsAboutToBeInserted.disconnect(self.__rowsAboutToBeInserted)
            sourceModel.rowsInserted.disconnect(self.__rowsInserted)
            sourceModel.rowsAboutToBeRemoved.disconnect(self.__rowsAboutToBeRemoved)
            sourceModel.rowsRemoved.disconnect(self.__rowsRemoved)
            sourceModel.rowsAboutToBeMoved.disconnect(self.__rowsAboutToBeMoved)
            sourceModel.rowsMoved.disconnect(self.__rowsMoved)
            sourceModel.columnsAboutToBeInserted.disconnect(self.__columnsAboutToBeInserted)
            sourceModel.columnsInserted.disconnect(self.__columnsInserted)
            sourceModel.columnsAboutToBeRemoved.disconnect(self.__columnsAboutToBeRemoved)
            sourceModel.columnsRemoved.disconnect(self.__columnsRemoved)
            sourceModel.columnsAboutToBeMoved.disconnect(self.__columnsAboutToBeMoved)
            sourceModel.columnsMoved.disconnect(self.__columnsMoved)
            sourceModel.modelAboutToBeReset.disconnect(self.__modelAboutToBeReset)
            sourceModel.modelReset.disconnect(self.__modelReset)
            sourceModel.dataChanged.disconnect(self.__dataChanged)
            sourceModel.headerDataChanged.disconnect(self.__headerDataChanged)
            sourceModel.layoutAboutToBeChanged.disconnect(self.__layoutAboutToBeChanged)
            sourceModel.layoutChanged.disconnect(self.__layoutChanged)

        super(QIdentityProxyModel, self).setSourceModel(newSourceModel)

        sourceModel = self.sourceModel()
        if sourceModel is not None:
            sourceModel.rowsAboutToBeInserted.connect(self.__rowsAboutToBeInserted)
            sourceModel.rowsInserted.connect(self.__rowsInserted)
            sourceModel.rowsAboutToBeRemoved.connect(self.__rowsAboutToBeRemoved)
            sourceModel.rowsRemoved.connect(self.__rowsRemoved)
            sourceModel.rowsAboutToBeMoved.connect(self.__rowsAboutToBeMoved)
            sourceModel.rowsMoved.connect(self.__rowsMoved)
            sourceModel.columnsAboutToBeInserted.connect(self.__columnsAboutToBeInserted)
            sourceModel.columnsInserted.connect(self.__columnsInserted)
            sourceModel.columnsAboutToBeRemoved.connect(self.__columnsAboutToBeRemoved)
            sourceModel.columnsRemoved.connect(self.__columnsRemoved)
            sourceModel.columnsAboutToBeMoved.connect(self.__columnsAboutToBeMoved)
            sourceModel.columnsMoved.connect(self.__columnsMoved)
            sourceModel.modelAboutToBeReset.connect(self.__modelAboutToBeReset)
            sourceModel.modelReset.connect(self.__modelReset)
            sourceModel.dataChanged.connect(self.__dataChanged)
            sourceModel.headerDataChanged.connect(self.__headerDataChanged)
            sourceModel.layoutAboutToBeChanged.connect(self.__layoutAboutToBeChanged)
            sourceModel.layoutChanged.connect(self.__layoutChanged)

        self.endResetModel()

    def __columnsAboutToBeInserted(self, parent, start, end):
        parent = self.mapFromSource(parent)
        self.beginInsertColumns(parent, start, end)

    def __columnsAboutToBeMoved(self, sourceParent, sourceStart, sourceEnd, destParent, dest):
        sourceParent = self.mapFromSource(sourceParent)
        destParent = self.mapFromSource(destParent)
        self.beginMoveColumns(sourceParent, sourceStart, sourceEnd, destParent, dest)

    def __columnsAboutToBeRemoved(self, parent, start, end):
        parent = self.mapFromSource(parent)
        self.beginRemoveColumns(parent, start, end)

    def __columnsInserted(self, parent, start, end):
        self.endInsertColumns()

    def __columnsMoved(self, sourceParent, sourceStart, sourceEnd, destParent, dest):
        self.endMoveColumns()

    def __columnsRemoved(self, parent, start, end):
        self.endRemoveColumns()

    def __dataChanged(self, topLeft, bottomRight):
        topLeft = self.mapFromSource(topLeft)
        bottomRight = self.mapFromSource(bottomRight)
        self.dataChanged(topLeft, bottomRight)

    def __headerDataChanged(self, orientation, first, last):
        self.headerDataChanged(orientation, first, last)

    def __layoutAboutToBeChanged(self):
        """Store persistent indexes"""
        if self.__ignoreNextLayoutAboutToBeChanged:
            return

        for proxyPersistentIndex in self.persistentIndexList():
            self.__proxyIndexes.append()
            sourcePersistentIndex = self.mapToSource(proxyPersistentIndex)
            mapping = proxyPersistentIndex, sourcePersistentIndex
            self.__persistentIndexes.append(mapping)

        self.layoutAboutToBeChanged()

    def __layoutChanged(self):
        """Restore persistent indexes"""
        if self.__ignoreNextLayoutChanged:
            return

        for mapping in self.__persistentIndexes:
            proxyIndex, sourcePersistentIndex = mapping
            sourcePersistentIndex = self.mapFromSource(sourcePersistentIndex)
            self.changePersistentIndex(proxyIndex, sourcePersistentIndex)

        self.__persistentIndexes = []

        self.layoutChanged()

    def __modelAboutToBeReset(self):
        self.beginResetModel()

    def __modelReset(self):
        self.endResetModel()

    def __rowsAboutToBeInserted(self, parent, start, end):
        parent = self.mapFromSource(parent)
        self.beginInsertRows(parent, start, end)

    def __rowsAboutToBeMoved(self, sourceParent, sourceStart, sourceEnd, destParent, dest):
        sourceParent = self.mapFromSource(sourceParent)
        destParent = self.mapFromSource(destParent)
        self.beginMoveRows(sourceParent, sourceStart, sourceEnd, destParent, dest)

    def __rowsAboutToBeRemoved(self, parent, start, end):
        parent = self.mapFromSource(parent)
        self.beginRemoveRows(parent, start, end)

    def __rowsInserted(self, parent, start, end):
        self.endInsertRows()

    def __rowsMoved(self, sourceParent, sourceStart, sourceEnd, destParent, dest):
        self.endMoveRows()

    def __rowsRemoved(self, parent, start, end):
        self.endRemoveRows()