File indexing completed on 2025-01-18 10:07:26
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 (parent() == index.parent()
0046 && tl.row() <= index.row() && tl.column() <= index.column()
0047 && br.row() >= index.row() && br.column() >= index.column());
0048 }
0049
0050 inline bool contains(int row, int column, const QModelIndex &parentIndex) const
0051 {
0052 return (parent() == parentIndex
0053 && tl.row() <= row && tl.column() <= column
0054 && br.row() >= row && br.column() >= column);
0055 }
0056
0057 bool intersects(const QItemSelectionRange &other) const;
0058 QItemSelectionRange intersected(const QItemSelectionRange &other) const;
0059
0060
0061 inline bool operator==(const QItemSelectionRange &other) const
0062 { return (tl == other.tl && br == other.br); }
0063 inline bool operator!=(const QItemSelectionRange &other) const
0064 { return !operator==(other); }
0065
0066 inline bool isValid() const
0067 {
0068 return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
0069 && top() <= bottom() && left() <= right());
0070 }
0071
0072 bool isEmpty() const;
0073
0074 QModelIndexList indexes() const;
0075
0076 private:
0077 QPersistentModelIndex tl, br;
0078 };
0079 Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_RELOCATABLE_TYPE);
0080
0081 class QItemSelection;
0082 class QItemSelectionModelPrivate;
0083
0084 class Q_CORE_EXPORT QItemSelectionModel : public QObject
0085 {
0086 Q_OBJECT
0087 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged
0088 BINDABLE bindableModel)
0089 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false
0090 DESIGNABLE false)
0091 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false
0092 DESIGNABLE false)
0093 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false
0094 DESIGNABLE false)
0095 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged
0096 STORED false DESIGNABLE false)
0097
0098 Q_DECLARE_PRIVATE(QItemSelectionModel)
0099
0100 public:
0101
0102 enum SelectionFlag {
0103 NoUpdate = 0x0000,
0104 Clear = 0x0001,
0105 Select = 0x0002,
0106 Deselect = 0x0004,
0107 Toggle = 0x0008,
0108 Current = 0x0010,
0109 Rows = 0x0020,
0110 Columns = 0x0040,
0111 SelectCurrent = Select | Current,
0112 ToggleCurrent = Toggle | Current,
0113 ClearAndSelect = Clear | Select
0114 };
0115
0116 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
0117 Q_FLAG(SelectionFlags)
0118
0119 explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
0120 explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
0121 virtual ~QItemSelectionModel();
0122
0123 QModelIndex currentIndex() const;
0124
0125 Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
0126 Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const;
0127 Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const;
0128
0129 Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const;
0130 Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const;
0131
0132 bool hasSelection() const;
0133
0134 QModelIndexList selectedIndexes() const;
0135 Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const;
0136 Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const;
0137 const QItemSelection selection() const;
0138
0139 const QAbstractItemModel *model() const;
0140 QAbstractItemModel *model();
0141 QBindable<QAbstractItemModel *> bindableModel();
0142
0143 void setModel(QAbstractItemModel *model);
0144
0145 public Q_SLOTS:
0146 virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
0147 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
0148 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
0149 virtual void clear();
0150 virtual void reset();
0151
0152 void clearSelection();
0153 virtual void clearCurrentIndex();
0154
0155 Q_SIGNALS:
0156 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
0157 void currentChanged(const QModelIndex ¤t, const QModelIndex &previous);
0158 void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous);
0159 void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous);
0160 void modelChanged(QAbstractItemModel *model);
0161
0162 protected:
0163 QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
0164 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
0165
0166 private:
0167 Q_DISABLE_COPY(QItemSelectionModel)
0168 };
0169
0170 Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
0171
0172
0173
0174 class QItemSelection : public QList<QItemSelectionRange>
0175 {
0176 public:
0177 using QList<QItemSelectionRange>::QList;
0178 Q_CORE_EXPORT QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0179
0180
0181
0182 Q_CORE_EXPORT void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0183 Q_CORE_EXPORT bool contains(const QModelIndex &index) const;
0184 Q_CORE_EXPORT QModelIndexList indexes() const;
0185 Q_CORE_EXPORT void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
0186 Q_CORE_EXPORT static void split(const QItemSelectionRange &range,
0187 const QItemSelectionRange &other,
0188 QItemSelection *result);
0189 };
0190 Q_DECLARE_SHARED(QItemSelection)
0191
0192 #ifndef QT_NO_DEBUG_STREAM
0193 Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
0194 #endif
0195
0196 QT_END_NAMESPACE
0197
0198 QT_DECL_METATYPE_EXTERN(QItemSelectionRange, Q_CORE_EXPORT)
0199 QT_DECL_METATYPE_EXTERN(QItemSelection, Q_CORE_EXPORT)
0200
0201 #endif