forked from linuxdeepin/dde-launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortproxymodel.cpp
More file actions
630 lines (545 loc) · 19.2 KB
/
Copy pathsortproxymodel.cpp
File metadata and controls
630 lines (545 loc) · 19.2 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
This file was part of KDToolBox (52611ec, sortproxymodel.{h,cpp}).
Modified by deepin to fit their own needs.
Changes between the original and this version:
1. QML support.
2. Handle `modelReset` as well (still for QML support).
SPDX-FileCopyrightText: 2018 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: André Somers <andre.somers@kdab.com>
SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
Author: Wang Zichong <wangzichong@deepin.org>
SPDX-License-Identifier: MIT
*/
#include "sortproxymodel.h"
#include <QDebug>
#include <QVariant>
#include <QLoggingCategory>
#include <private/qabstractitemmodel_p.h>
Q_DECLARE_LOGGING_CATEGORY(logModels)
namespace
{
void buildReverseMap(const std::vector<int> &aToB, std::vector<int> &bToA)
{
const int size = int(aToB.size());
bToA.resize(size);
for (int i = 0; i < size; ++i)
{
bToA[aToB[i]] = i;
}
}
}
SortProxyModel::SortProxyModel(QObject *parent)
: QAbstractProxyModel(parent)
, m_invalidatedRows(make_pair(m_proxyToSourceMap.end(), m_proxyToSourceMap.end()))
{
qCDebug(logModels) << "Initializing SortProxyModel";
}
QModelIndex SortProxyModel::index(int row, int column, const QModelIndex &parent) const
{
Q_ASSERT(!parent.isValid()); // we do not support tree models
Q_UNUSED(parent)
if (!sourceModel()) {
qCWarning(logModels) << "No source model available";
return {};
}
if (row >= static_cast<int>(m_proxyToSourceMap.size())) {
qCWarning(logModels) << "Row out of range:" << row;
return {};
}
if (column < 0 || column >= sourceModel()->columnCount()) {
qCWarning(logModels) << "Column out of range:" << column;
return {};
}
return createIndex(row, column, static_cast<quintptr>(m_proxyToSourceMap[static_cast<ulong>(row)]));
}
QModelIndex SortProxyModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child)
return {};
}
int SortProxyModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if (sourceModel()) {
return static_cast<int>(m_proxyToSourceMap.size());
} else {
return 0;
}
}
int SortProxyModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
const auto source = sourceModel();
if (source)
{
const int count = source->columnCount();
qCDebug(logModels) << "Column count from source:" << count;
return count;
}
else
{
qCDebug(logModels) << "No source model, returning 0";
return 0;
}
}
/**
* @brief SortProxyModel::sort sorts the model
* @param column The column to sort on.
* @param order The order to use when sorting.
*
* The default @arg order is Qt::Ascending order. As per convention, if you
* pass -1 for @arg column the sorting is disabled. The valid range for
* @arg column is therefore -1 to columnCount() - 1.
*/
void SortProxyModel::sort(int column, Qt::SortOrder order)
{
Q_ASSERT(column >= -1 && column < columnCount());
if (m_sortColumn != column || m_order != order)
{
int oldColumn = m_sortColumn;
int oldOrder = m_order;
m_sortColumn = column;
m_order = order;
reorder();
if (oldOrder != m_order)
Q_EMIT sortOrderChanged();
if (oldColumn != m_sortColumn)
Q_EMIT sortColumnChanged();
}
}
QVariant SortProxyModel::data(const QModelIndex &proxyIndex, int role) const
{
if (proxyIndex.isValid() && !isInvalidedRow(proxyIndex.row()))
{
return QAbstractProxyModel::data(proxyIndex, role);
}
return {};
}
void SortProxyModel::setSourceModel(QAbstractItemModel *model)
{
if (model != sourceModel())
{
beginResetModel();
if (sourceModel())
sourceModel()->disconnect(this);
QAbstractProxyModel::setSourceModel(model);
if (model)
{
connect(model, &QAbstractItemModel::dataChanged, this, &SortProxyModel::handleDataChanged);
connect(model, &QAbstractItemModel::rowsInserted, this, &SortProxyModel::handleRowsInserted);
connect(model, &QAbstractItemModel::rowsRemoved, this, &SortProxyModel::handleRowsRemoved);
connect(model, &QAbstractItemModel::modelReset, this, &SortProxyModel::handleModelReset);
}
endResetModel();
}
}
QModelIndex SortProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
if (!proxyIndex.isValid())
return {};
Q_ASSERT(proxyIndex.model() == this);
// no further bounds checking, out of bounds indices are a breach of contract
return sourceModel()->index(static_cast<int>(proxyIndex.internalId()), proxyIndex.column());
}
QModelIndex SortProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
if (!sourceIndex.isValid()) {
qCDebug(logModels) << "Invalid source index, returning empty";
return {};
}
Q_ASSERT(sourceIndex.model() == sourceModel());
if (sourceIndex.parent().isValid()) {
qCDebug(logModels) << "Source index has parent, returning empty (flat model)";
return {};
}
// no further bounds checking, out of bounds indices are a breach of contract
const auto proxyRow = mapToProxyRow(sourceIndex.row());
return index(proxyRow, sourceIndex.column());
}
void SortProxyModel::setSortRole(int role)
{
if (m_sortRole != role)
{
m_sortRole = role;
qCInfo(logModels) << "Sort role changed to" << role << ", reordering";
Q_EMIT sortRoleChanged();
reorder();
}
}
void SortProxyModel::setSortColumn(int column)
{
if (m_sortColumn != column)
{
int oldColumn = m_sortColumn;
m_sortColumn = column;
Q_EMIT sortColumnChanged();
// If source model is already set, reorder to apply the new sort column
if (sourceModel() && !m_proxyToSourceMap.empty())
{
reorder();
}
Q_UNUSED(oldColumn)
}
}
int SortProxyModel::sortRole() const
{
return m_sortRole;
}
void SortProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity sensitivity)
{
if (m_caseSensitivity != sensitivity)
{
m_caseSensitivity = sensitivity;
Q_EMIT sortCaseSensitivityChanged();
reorder();
}
}
Qt::CaseSensitivity SortProxyModel::sortCaseSensitivity() const
{
return m_caseSensitivity;
}
int SortProxyModel::sortColumn() const
{
return m_sortColumn;
}
Qt::SortOrder SortProxyModel::sortOrder() const
{
return m_order;
}
bool SortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
const QVariant lhs = source_left.data(m_sortRole);
const QVariant rhs = source_right.data(m_sortRole);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (lhs.typeId() == QMetaType::QString && rhs.typeId() == QMetaType::QString)
#else
if (lhs.type() == QVariant::String && rhs.type() == QVariant::String)
#endif
{
return QString::compare(lhs.toString(), rhs.toString(), m_caseSensitivity) < 0;
}
else
{
return QAbstractItemModelPrivate::isVariantLessThan(lhs, rhs, m_caseSensitivity, false);
}
}
void SortProxyModel::resetInternalData()
{
rebuildRowMap();
}
void SortProxyModel::rebuildRowMap()
{
// simple initial sort. No emitting of row moves
m_proxyToSourceMap.clear();
if (sourceModel())
{
m_proxyToSourceMap.resize(static_cast<ulong>(sourceModel()->rowCount()));
std::iota(m_proxyToSourceMap.begin(), m_proxyToSourceMap.end(), 0);
sortMappingContainer(m_proxyToSourceMap);
}
buildReverseMap(m_proxyToSourceMap, m_sourceToProxyMap);
}
template<class Iterator>
inline Iterator predecessor(Iterator it)
{
--it;
return it;
}
template<class Iterator>
inline Iterator successor(Iterator it)
{
++it;
return it;
}
template<class Iterator, class Predicate>
inline Iterator find_if_from_back(const Iterator &begin, const Iterator &end, Predicate predicate)
{
auto it = end;
do
{
auto pred = predecessor(it);
if (predicate(*pred))
{
return pred;
}
it = pred;
} while (it != begin);
return end;
}
template<class Iterator>
inline Iterator find_from_back(const Iterator &begin, const Iterator &end, const typename Iterator::value_type &value)
{
const auto predicate = [value](const typename Iterator::value_type &itemValue) { return itemValue == value; };
return find_if_from_back(begin, end, predicate);
}
void SortProxyModel::reorder()
{
// update the sort order. Emits row moves
if (m_proxyToSourceMap.empty()) // checks emptiness, doesn't empty by itself
return;
auto newOrder = m_proxyToSourceMap; // deep copy
if (m_sortColumn == -1)
{
qCDebug(logModels) << "No sort column set, using natural order";
std::iota(newOrder.begin(), newOrder.end(), 0);
}
else
{
qCDebug(logModels) << "Sorting container for column" << m_sortColumn;
sortMappingContainer(newOrder);
}
// during a reorder, we don't try to keep the reverse map in order. We clear and rebuild later.
m_sourceToProxyMap.clear();
auto orderedIt = predecessor(newOrder.end());
auto unorderedIt = predecessor(m_proxyToSourceMap.end());
while (orderedIt != newOrder.begin())
{
if (*orderedIt == *unorderedIt)
{
--orderedIt;
--unorderedIt;
}
else
{
auto it = find_from_back(m_proxyToSourceMap.begin(), unorderedIt, *orderedIt);
// we know it is valid, as newOrder is just a permutation of m_rowMap
int movedRow = static_cast<int>(it - m_proxyToSourceMap.begin());
int destinationRow = static_cast<int>(unorderedIt - m_proxyToSourceMap.begin()) + 1;
int moveCount = 1;
while (it != m_proxyToSourceMap.begin() && orderedIt != newOrder.begin() &&
*predecessor(it) == *predecessor(orderedIt))
{
++moveCount;
--movedRow;
--it;
--orderedIt;
}
bool ok = beginMoveRows(QModelIndex(), movedRow, movedRow + moveCount - 1, QModelIndex(), destinationRow);
if (!ok)
{
qWarning() << "moveRows from" << movedRow << "up to" << movedRow + moveCount - 1 << "to"
<< destinationRow;
QStringList contents;
contents.reserve(rowCount());
for (int row = 0; row < rowCount(); ++row)
{
contents << index(row).data(m_sortRole).toString();
}
qWarning() << "moving failed. Current contents:" << contents.join(QLatin1String(", "));
// if beginMoveRows fails, we cannot continue, skip this move
// do not call endMoveRows() and do not modify internal data structure
--orderedIt;
continue;
}
auto rotateEnd = successor(unorderedIt);
std::rotate(it, it + moveCount, rotateEnd);
endMoveRows();
--orderedIt;
unorderedIt = rotateEnd - (moveCount + 1);
}
}
buildReverseMap(m_proxyToSourceMap, m_sourceToProxyMap);
}
void SortProxyModel::sortMappingContainer(std::vector<int> &container)
{
if (m_sortColumn == -1)
return;
std::sort(container.begin(), container.end(),
[this](int lhs, int rhs) { return lessThan(lhs, rhs) != (m_order == Qt::DescendingOrder); });
}
bool SortProxyModel::lessThan(int source_left_row, int source_right_row) const
{
if (m_sortColumn == -1)
return false;
return lessThan(sourceModel()->index(source_left_row, m_sortColumn),
sourceModel()->index(source_right_row, m_sortColumn));
}
int SortProxyModel::mapToProxyRow(int sourceRow) const
{
if (!m_sourceToProxyMap.empty())
{
// we have an up-to-date reverse mapping, so use that
return m_sourceToProxyMap[sourceRow];
}
// reverse mapping is not up to date, use slower linear search instead
auto it = std::find(m_proxyToSourceMap.cbegin(), m_proxyToSourceMap.cend(), sourceRow);
return static_cast<int>(it - m_proxyToSourceMap.cbegin());
}
void SortProxyModel::handleDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QVector<int> &roles)
{
// Map the row-range
const int firstSrcRow = topLeft.row();
const std::vector<int>::size_type rowCnt = bottomRight.row() - firstSrcRow + 1;
std::vector<int> rows(rowCnt);
for (int r = 0; r < (int)rowCnt; ++r)
{
rows[r] = mapToProxyRow(r + firstSrcRow);
}
std::sort(rows.begin(), rows.end());
// convert the vector of ints indicating changed columns into a vector of pairs of ints indicating ranges.
// for example, the vector {1, 2, 3, 5, 6, 9} would be converted to {{1, 3}, {5, 6}, {9, 9}}
struct ColumnRange
{
int from;
int to;
};
using ColumnRanges = std::vector<ColumnRange>;
auto accumulator = [](ColumnRanges ranges, int column) {
if (ranges.empty() || (ranges.back().to < column - 1))
{
ranges.push_back({column, column});
}
else
{
ranges.back().to = column;
}
return ranges;
};
const ColumnRanges ranges = std::accumulate(rows.begin(), rows.end(), ColumnRanges(), accumulator);
// re-emit the dataChanged signals
for (const auto &range : ranges)
{
QModelIndex pTopLeft = index(range.from, topLeft.column());
QModelIndex pBottomRight = index(range.to, bottomRight.column());
Q_EMIT dataChanged(pTopLeft, pBottomRight, roles);
}
// re-order if needed
if (roles.isEmpty() || roles.contains(m_sortRole))
{
reorder();
}
}
void SortProxyModel::handleRowsInserted(const QModelIndex &parent, int firstNewRow, int lastNewRow)
{
if (parent.isValid())
return;
// reverse mapping is now invalid
m_sourceToProxyMap.clear();
// create a mapping for the new rows
std::vector<int> newRowsMap;
newRowsMap.resize(static_cast<ulong>(lastNewRow - firstNewRow + 1));
std::iota(newRowsMap.begin(), newRowsMap.end(), firstNewRow);
sortMappingContainer(newRowsMap);
// update the row indices in the mapping pointing to rows that shifted backwards
const int shift = lastNewRow - firstNewRow + 1;
for (auto &oldPos : m_proxyToSourceMap)
{
if (oldPos >= firstNewRow)
{
oldPos += shift;
}
}
// assure we have enough space for the new items (and avoid reallocations that can break iterators)
m_proxyToSourceMap.reserve(m_proxyToSourceMap.size() + newRowsMap.size());
// now merge the new rows into the mapping we already have
auto newIt = newRowsMap.begin();
auto curIt = m_proxyToSourceMap.begin();
while (curIt != m_proxyToSourceMap.end() && newIt != newRowsMap.end())
{
if (lessThan(*newIt, *curIt))
{
auto firstInsert = newIt;
// see how many more items we can insert in one go
while (successor(newIt) != newRowsMap.end() && !lessThan(*curIt, *successor(newIt)))
{
++newIt;
}
const auto insertStartPos = static_cast<int>(curIt - m_proxyToSourceMap.begin());
const auto insertLength = static_cast<int>(newIt - firstInsert) + 1;
beginInsertRows({}, insertStartPos, insertStartPos + insertLength - 1);
curIt = m_proxyToSourceMap.insert(curIt, firstInsert, successor(newIt));
++curIt;
endInsertRows();
++newIt;
}
else
{
++curIt;
}
}
// handle case of insert at the end of the container: insert the remaining items in newRowsMap
if (curIt == m_proxyToSourceMap.end() && newIt != newRowsMap.end())
{
const auto insertStartPos = static_cast<int>(curIt - m_proxyToSourceMap.begin());
const auto insertLength = static_cast<int>(newRowsMap.end() - newIt);
beginInsertRows({}, insertStartPos, insertStartPos + insertLength - 1);
m_proxyToSourceMap.insert(m_proxyToSourceMap.end(), newIt, newRowsMap.end());
endInsertRows();
}
buildReverseMap(m_proxyToSourceMap, m_sourceToProxyMap);
}
void SortProxyModel::handleRowsRemoved(const QModelIndex &parent, int firstRemovedRow, int lastRemovedRow)
{
if (parent.isValid())
return;
// reverse mapping is now invalid
m_sourceToProxyMap.clear();
// update the row indices in the mapping pointing to rows that shifted forwards and build
// up list of rows to remove
const int shift = lastRemovedRow - firstRemovedRow + 1;
std::vector<int> removedRows;
removedRows.reserve(static_cast<ulong>(shift));
int row = 0;
for (auto &oldPos : m_proxyToSourceMap)
{
if (oldPos > lastRemovedRow)
{
oldPos -= shift;
}
else if (oldPos >= firstRemovedRow)
{
removedRows.push_back(row);
}
++row;
}
std::sort(removedRows.begin(), removedRows.end());
m_invalidatedRows = make_pair(removedRows.begin(), removedRows.end());
// iterates backwards through the list of rows to remove so the indices in removedRows stay
// correct during the iteration
auto it = predecessor(removedRows.end());
for (;;)
{
auto lastRowToRemove = *it;
// see if we have consecutive rows we can remove in one go
while (it != removedRows.begin() && *predecessor(it) == *it - 1)
--it;
auto firstRowToRemove = *it;
beginRemoveRows({}, firstRowToRemove, lastRowToRemove);
m_proxyToSourceMap.erase(m_proxyToSourceMap.begin() + firstRowToRemove,
m_proxyToSourceMap.begin() + lastRowToRemove + 1);
m_invalidatedRows.second = it;
endRemoveRows();
if (it == removedRows.begin())
break;
--it;
}
m_invalidatedRows = make_pair(m_proxyToSourceMap.end(), m_proxyToSourceMap.end());
buildReverseMap(m_proxyToSourceMap, m_sourceToProxyMap);
}
void SortProxyModel::handleModelReset()
{
if (!sourceModel())
return;
beginResetModel();
resetInternalData();
endResetModel();
}
/**
* @brief SortProxyModel::isInvalidedRow
* @param row
* @returns true if the indicated row has already been removed from the source model
*
* During a remove operation, we cannot always send a single rowsRemoved signal. That means
* we will be signalling to the outside world during while the source model has already
* removed some rows that we still have in the model. However, we cannot access these rows
* any more, as the indexes either point to rows outside the range of the model or point to
* rows that have shifted into the position that the row was in.) If such a row is accessed
* via the proxy, we will return an invalid QVariant.
*/
bool SortProxyModel::isInvalidedRow(const int row) const
{
// m_invalidatedRows only contains a valid range during a remove operation that involves multiple rows
return std::any_of(m_invalidatedRows.first, m_invalidatedRows.second,
[row](int invalidated) { return invalidated == row; });
}