File indexing completed on 2025-09-18 09:26:31
0001
0002
0003
0004 #ifndef QITEMSELECTIONMODEL_H
0005 #define QITEMSELECTIONMODEL_H
0006
0007 #include <QtCore/qglobal.h>
0008
0009 #include <QtCore/qabstractitemmodel.h>
0010 #include <QtCore/qlist.h>
0011 #include <QtCore/qset.h>
0012
0013 QT_REQUIRE_CONFIG(itemmodel);
0014
0015 QT_BEGIN_NAMESPACE
0016
0017 class Q_CORE_EXPORT QItemSelectionRange
0018 {
0019
0020 public:
0021 QItemSelectionRange() = default;
0022 QItemSelectionRange(const QModelIndex &topL, const QModelIndex &bottomR) : tl(topL), br(bottomR) {}
0023 explicit QItemSelectionRange(const QModelIndex &index) : tl(index), br(tl) {}
0024
0025 void swap(QItemSelectionRange &other) noexcept
0026 {
0027 tl.swap(other.tl);
0028 br.swap(other.br);
0029 }
0030
0031 inline int top() const { return tl.row(); }
0032 inline int left() const { return tl.column(); }
0033 inline int bottom() const { return br.row(); }
0034 inline int right() const { return br.column(); }
0035 inline int width() const { return br.column() - tl.column() + 1; }
0036 inline int height() const { return br.row() - tl.row() + 1; }
0037
0038 inline const QPersistentModelIndex &topLeft() const { return tl; }
0039 inline const QPersistentModelIndex &bottomRight() const { return br; }
0040 inline QModelIndex parent() const { return tl.parent(); }
0041 inline const QAbstractItemModel *model() const { return tl.model(); }
0042
0043 inline bool contains(const QModelIndex &index) const
0044 {
0045 return contains(index.row(), index.column(), index.parent());
0046 }
0047
0048 inline bool contains(int row, int column, const QModelIndex &parentIndex) const
0049 {
0050 return (br.row() >= row && br.column() >= column &&
0051 tl.row() <= row && tl.column() <= column &&
0052 parent() == parentIndex);
0053 }
0054
0055 bool intersects(const QItemSelectionRange &other) const;
0056 QItemSelectionRange intersected(const QItemSelectionRange &other) const;
0057
0058 #if QT_CORE_REMOVED_SINCE(6, 8)
0059 inline bool operator==(const QItemSelectionRange &other) const
0060 { return comparesEqual(*this, other); }
0061 inline bool operator!=(const QItemSelectionRange &other) const
0062 { return !operator==(other); }
0063 #endif
0064 inline bool isValid() const
0065 {
0066 return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
0067 && top() <= bottom() && left() <= right());
0068 }
0069
0070 bool isEmpty() const;
0071
0072 QModelIndexList indexes() const;
0073
0074 private:
0075 friend bool comparesEqual(const QItemSelectionRange &lhs,
0076 const QItemSelectionRange &rhs) noexcept
0077 {
0078 return comparesEqual(lhs.tl, rhs.tl) && comparesEqual(lhs.br, rhs.br);
0079 }
0080 Q_DECLARE_EQUALITY_COMPARABLE(QItemSelectionRange)
0081 QPersistentModelIndex tl, br;
0082 };
0083 Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_RELOCATABLE_TYPE);
0084
0085 class QItemSelection;
0086 class QItemSelectionModelPrivate;
0087
0088 class Q_CORE_EXPORT QItemSelectionModel : public QObject
0089 {
0090 Q_OBJECT
0091 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged
0092 BINDABLE bindableModel)
0093 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false
0094 DESIGNABLE false)
0095 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false
0096 DESIGNABLE false)
0097 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false
0098 DESIGNABLE false)
0099 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged
0100 STORED false DESIGNABLE false)
0101
0102 Q_DECLARE_PRIVATE(QItemSelectionModel)
0103
0104 public:
0105
0106 enum SelectionFlag {
0107 NoUpdate = 0x0000,
0108 Clear = 0x0001,
0109 Select = 0x0002,
0110 Deselect = 0x0004,
0111 Toggle = 0x0008,
0112 Current = 0x0010,
0113 Rows = 0x0020,
0114 Columns = 0x0040,
0115 SelectCurrent = Select | Current,
0116 ToggleCurrent = Toggle | Current,
0117 ClearAndSelect = Clear | Select
0118 };
0119
0120 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
0121 Q_FLAG(SelectionFlags)
0122
0123 explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
0124 explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
0125 virtual ~QItemSelectionModel();
0126
0127 QModelIndex currentIndex() const;
0128
0129 Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
0130 Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const;
0131 Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const;
0132
0133 Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const;
0134 Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const;
0135
0136 bool hasSelection() const;
0137
0138 QModelIndexList selectedIndexes() const;
0139 Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const;
0140 Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const;
0141 const QItemSelection selection() const;
0142
0143 const QAbstractItemModel *model() const;
0144 QAbstractItemModel *model();
0145 QBindable<QAbstractItemModel *> bindableModel();
0146
0147 void setModel(QAbstractItemModel *model);
0148
0149 public Q_SLOTS:
0150 virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
0151 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
0152 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
0153 virtual void clear();
0154 virtual void reset();
0155
0156 void clearSelection();
0157 virtual void clearCurrentIndex();
0158
0159 Q_SIGNALS:
0160 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
0161 void currentChanged(const QModelIndex ¤t, const QModelIndex &previous);
0162 void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous);
0163 void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous);
0164 void modelChanged(QAbstractItemModel *model);
0165
0166 protected:
0167 QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
0168 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
0169
0170 private:
0171 Q_DISABLE_COPY(QItemSelectionModel)
0172 };
0173
0174 Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
0175
0176
0177
0178 class QItemSelection : public QList<QItemSelectionRange>
0179 {
0180 public:
0181 using QList<QItemSelectionRange>::QList;
0182 Q_CORE_EXPORT QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0183
0184
0185
0186 Q_CORE_EXPORT void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0187 Q_CORE_EXPORT bool contains(const QModelIndex &index) const;
0188 Q_CORE_EXPORT QModelIndexList indexes() const;
0189 Q_CORE_EXPORT void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
0190 Q_CORE_EXPORT static void split(const QItemSelectionRange &range,
0191 const QItemSelectionRange &other,
0192 QItemSelection *result);
0193 };
0194 Q_DECLARE_SHARED(QItemSelection)
0195
0196 #ifndef QT_NO_DEBUG_STREAM
0197 Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
0198 #endif
0199
0200 QT_END_NAMESPACE
0201
0202 QT_DECL_METATYPE_EXTERN(QItemSelectionRange, Q_CORE_EXPORT)
0203 QT_DECL_METATYPE_EXTERN(QItemSelection, Q_CORE_EXPORT)
0204
0205 #endif