| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QITEMSELECTIONMODEL_P_H |
| 6 | #define QITEMSELECTIONMODEL_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include "qitemselectionmodel.h" |
| 20 | #include "private/qobject_p.h" |
| 21 | #include "private/qproperty_p.h" |
| 22 | #include <array> |
| 23 | |
| 24 | QT_REQUIRE_CONFIG(itemmodel); |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QItemSelectionModelPrivate: public QObjectPrivate |
| 29 | { |
| 30 | Q_DECLARE_PUBLIC(QItemSelectionModel) |
| 31 | public: |
| 32 | QItemSelectionModelPrivate() |
| 33 | : currentCommand(QItemSelectionModel::NoUpdate), |
| 34 | tableSelected(false), tableColCount(0), tableRowCount(0) {} |
| 35 | ~QItemSelectionModelPrivate() override; |
| 36 | |
| 37 | QItemSelection expandSelection(const QItemSelection &selection, |
| 38 | QItemSelectionModel::SelectionFlags command) const; |
| 39 | |
| 40 | void initModel(QAbstractItemModel *model); |
| 41 | |
| 42 | void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end); |
| 43 | void columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end); |
| 44 | void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end); |
| 45 | void columnsAboutToBeInserted(const QModelIndex &parent, int start, int end); |
| 46 | void layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint); |
| 47 | void triggerLayoutToBeChanged() |
| 48 | { |
| 49 | layoutAboutToBeChanged(parents: QList<QPersistentModelIndex>(), hint: QAbstractItemModel::NoLayoutChangeHint); |
| 50 | } |
| 51 | |
| 52 | void layoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint); |
| 53 | void triggerLayoutChanged() |
| 54 | { |
| 55 | layoutChanged(parents: QList<QPersistentModelIndex>(), hint: QAbstractItemModel::NoLayoutChangeHint); |
| 56 | } |
| 57 | |
| 58 | void modelDestroyed(); |
| 59 | |
| 60 | inline void remove(QList<QItemSelectionRange> &r) |
| 61 | { |
| 62 | QList<QItemSelectionRange>::const_iterator it = r.constBegin(); |
| 63 | for (; it != r.constEnd(); ++it) |
| 64 | ranges.removeAll(t: *it); |
| 65 | } |
| 66 | |
| 67 | inline void finalize() |
| 68 | { |
| 69 | ranges.merge(other: currentSelection, command: currentCommand); |
| 70 | if (!currentSelection.isEmpty()) // ### perhaps this should be in QList |
| 71 | currentSelection.clear(); |
| 72 | } |
| 73 | |
| 74 | void setModel(QAbstractItemModel *mod) { q_func()->setModel(mod); } |
| 75 | void disconnectModel(); |
| 76 | void modelChanged(QAbstractItemModel *mod) { Q_EMIT q_func()->modelChanged(model: mod); } |
| 77 | Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QItemSelectionModelPrivate, QAbstractItemModel *, model, |
| 78 | &QItemSelectionModelPrivate::setModel, |
| 79 | &QItemSelectionModelPrivate::modelChanged, nullptr) |
| 80 | |
| 81 | QItemSelection ranges; |
| 82 | QItemSelection currentSelection; |
| 83 | QPersistentModelIndex currentIndex; |
| 84 | QItemSelectionModel::SelectionFlags currentCommand; |
| 85 | QList<QPersistentModelIndex> savedPersistentIndexes; |
| 86 | QList<QPersistentModelIndex> savedPersistentCurrentIndexes; |
| 87 | QList<std::pair<QPersistentModelIndex, uint>> savedPersistentRowLengths; |
| 88 | QList<std::pair<QPersistentModelIndex, uint>> savedPersistentCurrentRowLengths; |
| 89 | // optimization when all indexes are selected |
| 90 | bool tableSelected; |
| 91 | QPersistentModelIndex tableParent; |
| 92 | int tableColCount, tableRowCount; |
| 93 | std::array<QMetaObject::Connection, 12> connections; |
| 94 | }; |
| 95 | |
| 96 | QT_END_NAMESPACE |
| 97 | |
| 98 | #endif // QITEMSELECTIONMODEL_P_H |
| 99 | |