| 1 | // Copyright (C) 2025 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 QRANGEMODEL_H |
| 6 | #define QRANGEMODEL_H |
| 7 | |
| 8 | #include <QtCore/qrangemodel_impl.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | class QRangeModelPrivate; |
| 13 | |
| 14 | class Q_CORE_EXPORT QRangeModel : public QAbstractItemModel |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | Q_PROPERTY(QHash<int, QByteArray> roleNames READ roleNames WRITE setRoleNames RESET resetRoleNames |
| 18 | NOTIFY roleNamesChanged FINAL) |
| 19 | |
| 20 | public: |
| 21 | enum class RowCategory { |
| 22 | Default, |
| 23 | MultiRoleItem, |
| 24 | }; |
| 25 | |
| 26 | template <typename T> |
| 27 | struct RowOptions {}; |
| 28 | |
| 29 | template <typename Range, |
| 30 | QRangeModelDetails::if_table_range<Range> = true> |
| 31 | explicit QRangeModel(Range &&range, QObject *parent = nullptr) |
| 32 | : QRangeModel(new QGenericTableItemModelImpl<Range>(std::forward<Range>(range), this), parent) |
| 33 | {} |
| 34 | |
| 35 | template <typename Range, |
| 36 | QRangeModelDetails::if_tree_range<Range> = true> |
| 37 | explicit QRangeModel(Range &&range, QObject *parent = nullptr) |
| 38 | : QRangeModel(std::forward<Range>(range), QRangeModelDetails::DefaultTreeProtocol<Range>{}, |
| 39 | parent) |
| 40 | {} |
| 41 | |
| 42 | template <typename Range, typename Protocol, |
| 43 | QRangeModelDetails::if_tree_range<Range, Protocol> = true> |
| 44 | explicit QRangeModel(Range &&range, Protocol &&protocol, QObject *parent = nullptr) |
| 45 | : QRangeModel(new QGenericTreeItemModelImpl<Range, Protocol>(std::forward<Range>(range), |
| 46 | std::forward<Protocol>(protocol), |
| 47 | this), |
| 48 | parent) |
| 49 | {} |
| 50 | |
| 51 | ~QRangeModel() override; |
| 52 | |
| 53 | QModelIndex index(int row, int column, const QModelIndex &parent = {}) const final; |
| 54 | QModelIndex parent(const QModelIndex &child) const final; |
| 55 | QModelIndex sibling(int row, int column, const QModelIndex &index) const final; |
| 56 | int rowCount(const QModelIndex &parent = {}) const final; |
| 57 | int columnCount(const QModelIndex &parent = {}) const final; |
| 58 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 59 | QVariant (int section, Qt::Orientation orientation, |
| 60 | int role = Qt::DisplayRole) const override; |
| 61 | bool (int section, Qt::Orientation orientation, const QVariant &data, |
| 62 | int role = Qt::EditRole) override; |
| 63 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 64 | bool setData(const QModelIndex &index, const QVariant &data, int role = Qt::EditRole) override; |
| 65 | QMap<int, QVariant> itemData(const QModelIndex &index) const override; |
| 66 | bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &data) override; |
| 67 | bool clearItemData(const QModelIndex &index) override; |
| 68 | bool insertColumns(int column, int count, const QModelIndex &parent = {}) final; |
| 69 | bool removeColumns(int column, int count, const QModelIndex &parent = {}) final; |
| 70 | bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, |
| 71 | const QModelIndex &destParent, int destColumn) final; |
| 72 | bool insertRows(int row, int count, const QModelIndex &parent = {}) final; |
| 73 | bool removeRows(int row, int count, const QModelIndex &parent = {}) final; |
| 74 | bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, |
| 75 | const QModelIndex &destParent, int destRow) final; |
| 76 | |
| 77 | QHash<int, QByteArray> roleNames() const override; |
| 78 | void setRoleNames(const QHash<int, QByteArray> &names); |
| 79 | void resetRoleNames(); |
| 80 | |
| 81 | bool canFetchMore(const QModelIndex &parent) const override; |
| 82 | void fetchMore(const QModelIndex &parent) override; |
| 83 | |
| 84 | bool hasChildren(const QModelIndex &parent = QModelIndex()) const final; |
| 85 | QModelIndex buddy(const QModelIndex &index) const override; |
| 86 | bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, |
| 87 | const QModelIndex &parent) const override; |
| 88 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, |
| 89 | const QModelIndex &parent) override; |
| 90 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
| 91 | QStringList mimeTypes() const override; |
| 92 | QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, |
| 93 | Qt::MatchFlags flags) const override; |
| 94 | void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override; |
| 95 | void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; |
| 96 | QSize span(const QModelIndex &index) const override; |
| 97 | Qt::DropActions supportedDragActions() const override; |
| 98 | Qt::DropActions supportedDropActions() const override; |
| 99 | |
| 100 | Q_SIGNALS: |
| 101 | void roleNamesChanged(); |
| 102 | |
| 103 | protected Q_SLOTS: |
| 104 | void resetInternalData() override; |
| 105 | |
| 106 | protected: |
| 107 | bool event(QEvent *) override; |
| 108 | bool eventFilter(QObject *, QEvent *) override; |
| 109 | |
| 110 | private: |
| 111 | Q_DISABLE_COPY_MOVE(QRangeModel) |
| 112 | Q_DECLARE_PRIVATE(QRangeModel) |
| 113 | |
| 114 | explicit QRangeModel(QRangeModelImplBase *impl, QObject *parent); |
| 115 | friend class QRangeModelImplBase; |
| 116 | }; |
| 117 | |
| 118 | // implementation of forwarders |
| 119 | QModelIndex QRangeModelImplBase::createIndex(int row, int column, const void *ptr) const |
| 120 | { |
| 121 | return m_rangeModel->createIndex(arow: row, acolumn: column, adata: ptr); |
| 122 | } |
| 123 | void QRangeModelImplBase::changePersistentIndexList(const QModelIndexList &from, |
| 124 | const QModelIndexList &to) |
| 125 | { |
| 126 | m_rangeModel->changePersistentIndexList(from, to); |
| 127 | } |
| 128 | void QRangeModelImplBase::dataChanged(const QModelIndex &from, const QModelIndex &to, |
| 129 | const QList<int> &roles) |
| 130 | { |
| 131 | m_rangeModel->dataChanged(topLeft: from, bottomRight: to, roles); |
| 132 | } |
| 133 | void QRangeModelImplBase::beginInsertColumns(const QModelIndex &parent, int start, int count) |
| 134 | { |
| 135 | m_rangeModel->beginInsertColumns(parent, first: start, last: count); |
| 136 | } |
| 137 | void QRangeModelImplBase::endInsertColumns() |
| 138 | { |
| 139 | m_rangeModel->endInsertColumns(); |
| 140 | } |
| 141 | void QRangeModelImplBase::beginRemoveColumns(const QModelIndex &parent, int start, int count) |
| 142 | { |
| 143 | m_rangeModel->beginRemoveColumns(parent, first: start, last: count); |
| 144 | } |
| 145 | void QRangeModelImplBase::endRemoveColumns() |
| 146 | { |
| 147 | m_rangeModel->endRemoveColumns(); |
| 148 | } |
| 149 | bool QRangeModelImplBase::beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, |
| 150 | int sourceLast, const QModelIndex &destParent, |
| 151 | int destColumn) |
| 152 | { |
| 153 | return m_rangeModel->beginMoveColumns(sourceParent, sourceFirst, sourceLast, |
| 154 | destinationParent: destParent, destinationColumn: destColumn); |
| 155 | } |
| 156 | void QRangeModelImplBase::endMoveColumns() |
| 157 | { |
| 158 | m_rangeModel->endMoveColumns(); |
| 159 | } |
| 160 | |
| 161 | void QRangeModelImplBase::beginInsertRows(const QModelIndex &parent, int start, int count) |
| 162 | { |
| 163 | m_rangeModel->beginInsertRows(parent, first: start, last: count); |
| 164 | } |
| 165 | void QRangeModelImplBase::endInsertRows() |
| 166 | { |
| 167 | m_rangeModel->endInsertRows(); |
| 168 | } |
| 169 | void QRangeModelImplBase::beginRemoveRows(const QModelIndex &parent, int start, int count) |
| 170 | { |
| 171 | m_rangeModel->beginRemoveRows(parent, first: start, last: count); |
| 172 | } |
| 173 | void QRangeModelImplBase::endRemoveRows() |
| 174 | { |
| 175 | m_rangeModel->endRemoveRows(); |
| 176 | } |
| 177 | bool QRangeModelImplBase::beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, |
| 178 | int sourceLast, |
| 179 | const QModelIndex &destParent, int destRow) |
| 180 | { |
| 181 | return m_rangeModel->beginMoveRows(sourceParent, sourceFirst, sourceLast, destinationParent: destParent, destinationRow: destRow); |
| 182 | } |
| 183 | void QRangeModelImplBase::endMoveRows() |
| 184 | { |
| 185 | m_rangeModel->endMoveRows(); |
| 186 | } |
| 187 | QAbstractItemModel &QRangeModelImplBase::itemModel() |
| 188 | { |
| 189 | return *m_rangeModel; |
| 190 | } |
| 191 | const QAbstractItemModel &QRangeModelImplBase::itemModel() const |
| 192 | { |
| 193 | return *m_rangeModel; |
| 194 | } |
| 195 | |
| 196 | // Helper template that we can forward declare in the _impl header, |
| 197 | // where QRangeModel is not yet defined. |
| 198 | namespace QRangeModelDetails |
| 199 | { |
| 200 | template <typename T> |
| 201 | struct QRangeModelRowOptions : QRangeModel::RowOptions<T> {}; |
| 202 | } |
| 203 | |
| 204 | QT_END_NAMESPACE |
| 205 | |
| 206 | |
| 207 | #endif // QRANGEMODEL_H |
| 208 | |