| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifndef QITEMSELECTIONMODEL_H | 
|---|
| 43 | #define QITEMSELECTIONMODEL_H | 
|---|
| 44 |  | 
|---|
| 45 | #include <QtCore/qset.h> | 
|---|
| 46 | #include <QtCore/qvector.h> | 
|---|
| 47 | #include <QtCore/qlist.h> | 
|---|
| 48 | #include <QtCore/qabstractitemmodel.h> | 
|---|
| 49 |  | 
|---|
| 50 | QT_BEGIN_HEADER | 
|---|
| 51 |  | 
|---|
| 52 | QT_BEGIN_NAMESPACE | 
|---|
| 53 |  | 
|---|
| 54 | QT_MODULE(Gui) | 
|---|
| 55 |  | 
|---|
| 56 | #ifndef QT_NO_ITEMVIEWS | 
|---|
| 57 |  | 
|---|
| 58 | class Q_GUI_EXPORT QItemSelectionRange | 
|---|
| 59 | { | 
|---|
| 60 |  | 
|---|
| 61 | public: | 
|---|
| 62 | inline QItemSelectionRange() {} | 
|---|
| 63 | inline QItemSelectionRange(const QItemSelectionRange &other) | 
|---|
| 64 | : tl(other.tl), br(other.br) {} | 
|---|
| 65 | inline QItemSelectionRange(const QModelIndex &topLeft, const QModelIndex &bottomRight); | 
|---|
| 66 | explicit inline QItemSelectionRange(const QModelIndex &index) | 
|---|
| 67 | { tl = index; br = tl; } | 
|---|
| 68 |  | 
|---|
| 69 | inline int top() const { return tl.row(); } | 
|---|
| 70 | inline int left() const { return tl.column(); } | 
|---|
| 71 | inline int bottom() const { return br.row(); } | 
|---|
| 72 | inline int right() const { return br.column(); } | 
|---|
| 73 | inline int width() const { return br.column() - tl.column() + 1; } | 
|---|
| 74 | inline int height() const { return br.row() - tl.row() + 1; } | 
|---|
| 75 |  | 
|---|
| 76 | inline QModelIndex topLeft() const { return QModelIndex(tl); } | 
|---|
| 77 | inline QModelIndex bottomRight() const { return QModelIndex(br); } | 
|---|
| 78 | inline QModelIndex parent() const { return tl.parent(); } | 
|---|
| 79 | inline const QAbstractItemModel *model() const { return tl.model(); } | 
|---|
| 80 |  | 
|---|
| 81 | inline bool contains(const QModelIndex &index) const | 
|---|
| 82 | { | 
|---|
| 83 | return (parent() == index.parent() | 
|---|
| 84 | && tl.row() <= index.row() && tl.column() <= index.column() | 
|---|
| 85 | && br.row() >= index.row() && br.column() >= index.column()); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | inline bool contains(int row, int column, const QModelIndex &parentIndex) const | 
|---|
| 89 | { | 
|---|
| 90 | return (parent() == parentIndex | 
|---|
| 91 | && tl.row() <= row && tl.column() <= column | 
|---|
| 92 | && br.row() >= row && br.column() >= column); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | bool intersects(const QItemSelectionRange &other) const; | 
|---|
| 96 | QItemSelectionRange intersect(const QItemSelectionRange &other) const; // ### Qt 5: make QT4_SUPPORT | 
|---|
| 97 | inline QItemSelectionRange intersected(const QItemSelectionRange &other) const | 
|---|
| 98 | { return intersect(other); } | 
|---|
| 99 |  | 
|---|
| 100 | inline bool operator==(const QItemSelectionRange &other) const | 
|---|
| 101 | { return (tl == other.tl && br == other.br); } | 
|---|
| 102 | inline bool operator!=(const QItemSelectionRange &other) const | 
|---|
| 103 | { return !operator==(other); } | 
|---|
| 104 |  | 
|---|
| 105 | inline bool isValid() const | 
|---|
| 106 | { | 
|---|
| 107 | return (tl.isValid() && br.isValid() && tl.parent() == br.parent() | 
|---|
| 108 | && top() <= bottom() && left() <= right()); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | bool isEmpty() const; | 
|---|
| 112 |  | 
|---|
| 113 | QModelIndexList indexes() const; | 
|---|
| 114 |  | 
|---|
| 115 | private: | 
|---|
| 116 | QPersistentModelIndex tl, br; | 
|---|
| 117 | }; | 
|---|
| 118 | Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE); | 
|---|
| 119 |  | 
|---|
| 120 | inline QItemSelectionRange::QItemSelectionRange(const QModelIndex &atopLeft, | 
|---|
| 121 | const QModelIndex &abottomRight) | 
|---|
| 122 | { tl = atopLeft; br = abottomRight; } | 
|---|
| 123 |  | 
|---|
| 124 | class QItemSelection; | 
|---|
| 125 | class QItemSelectionModelPrivate; | 
|---|
| 126 |  | 
|---|
| 127 | class Q_GUI_EXPORT QItemSelectionModel : public QObject | 
|---|
| 128 | { | 
|---|
| 129 | Q_OBJECT | 
|---|
| 130 | Q_DECLARE_PRIVATE(QItemSelectionModel) | 
|---|
| 131 | Q_FLAGS(SelectionFlags) | 
|---|
| 132 |  | 
|---|
| 133 | public: | 
|---|
| 134 |  | 
|---|
| 135 | enum SelectionFlag { | 
|---|
| 136 | NoUpdate       = 0x0000, | 
|---|
| 137 | Clear          = 0x0001, | 
|---|
| 138 | Select         = 0x0002, | 
|---|
| 139 | Deselect       = 0x0004, | 
|---|
| 140 | Toggle         = 0x0008, | 
|---|
| 141 | Current        = 0x0010, | 
|---|
| 142 | Rows           = 0x0020, | 
|---|
| 143 | Columns        = 0x0040, | 
|---|
| 144 | SelectCurrent  = Select | Current, | 
|---|
| 145 | ToggleCurrent  = Toggle | Current, | 
|---|
| 146 | ClearAndSelect = Clear | Select | 
|---|
| 147 | }; | 
|---|
| 148 |  | 
|---|
| 149 | Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag) | 
|---|
| 150 |  | 
|---|
| 151 | explicit QItemSelectionModel(QAbstractItemModel *model); | 
|---|
| 152 | explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent); | 
|---|
| 153 | virtual ~QItemSelectionModel(); | 
|---|
| 154 |  | 
|---|
| 155 | QModelIndex currentIndex() const; | 
|---|
| 156 |  | 
|---|
| 157 | bool isSelected(const QModelIndex &index) const; | 
|---|
| 158 | bool isRowSelected(int row, const QModelIndex &parent) const; | 
|---|
| 159 | bool isColumnSelected(int column, const QModelIndex &parent) const; | 
|---|
| 160 |  | 
|---|
| 161 | bool rowIntersectsSelection(int row, const QModelIndex &parent) const; | 
|---|
| 162 | bool columnIntersectsSelection(int column, const QModelIndex &parent) const; | 
|---|
| 163 |  | 
|---|
| 164 | bool hasSelection() const; | 
|---|
| 165 |  | 
|---|
| 166 | QModelIndexList selectedIndexes() const; | 
|---|
| 167 | QModelIndexList selectedRows(int column = 0) const; | 
|---|
| 168 | QModelIndexList selectedColumns(int row = 0) const; | 
|---|
| 169 | const QItemSelection selection() const; | 
|---|
| 170 |  | 
|---|
| 171 | const QAbstractItemModel *model() const; | 
|---|
| 172 |  | 
|---|
| 173 | public Q_SLOTS: | 
|---|
| 174 | void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command); | 
|---|
| 175 | virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command); | 
|---|
| 176 | virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command); | 
|---|
| 177 | virtual void clear(); | 
|---|
| 178 | virtual void reset(); | 
|---|
| 179 |  | 
|---|
| 180 | void clearSelection(); | 
|---|
| 181 |  | 
|---|
| 182 | Q_SIGNALS: | 
|---|
| 183 | void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); | 
|---|
| 184 | void currentChanged(const QModelIndex ¤t, const QModelIndex &previous); | 
|---|
| 185 | void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); | 
|---|
| 186 | void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous); | 
|---|
| 187 |  | 
|---|
| 188 | protected: | 
|---|
| 189 | QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model); | 
|---|
| 190 | void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection); | 
|---|
| 191 |  | 
|---|
| 192 | private: | 
|---|
| 193 | Q_DISABLE_COPY(QItemSelectionModel) | 
|---|
| 194 | Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int)) | 
|---|
| 195 | Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int)) | 
|---|
| 196 | Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int)) | 
|---|
| 197 | Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int)) | 
|---|
| 198 | Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged()) | 
|---|
| 199 | Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged()) | 
|---|
| 200 | }; | 
|---|
| 201 |  | 
|---|
| 202 | Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags) | 
|---|
| 203 |  | 
|---|
| 204 | // dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC | 
|---|
| 205 | inline uint qHash(const QItemSelectionRange &) { return 0; } | 
|---|
| 206 |  | 
|---|
| 207 | class Q_GUI_EXPORT QItemSelection : public QList<QItemSelectionRange> | 
|---|
| 208 | { | 
|---|
| 209 | public: | 
|---|
| 210 | QItemSelection() {} | 
|---|
| 211 | QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight); | 
|---|
| 212 | void select(const QModelIndex &topLeft, const QModelIndex &bottomRight); | 
|---|
| 213 | bool contains(const QModelIndex &index) const; | 
|---|
| 214 | QModelIndexList indexes() const; | 
|---|
| 215 | void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command); | 
|---|
| 216 | static void split(const QItemSelectionRange &range, | 
|---|
| 217 | const QItemSelectionRange &other, | 
|---|
| 218 | QItemSelection *result); | 
|---|
| 219 | }; | 
|---|
| 220 |  | 
|---|
| 221 | #ifndef QT_NO_DEBUG_STREAM | 
|---|
| 222 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &); | 
|---|
| 223 | #endif | 
|---|
| 224 |  | 
|---|
| 225 | #endif // QT_NO_ITEMVIEWS | 
|---|
| 226 |  | 
|---|
| 227 | QT_END_NAMESPACE | 
|---|
| 228 |  | 
|---|
| 229 | QT_END_HEADER | 
|---|
| 230 |  | 
|---|
| 231 | #endif // QITEMSELECTIONMODEL_H | 
|---|