qitemselectionmodel.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QITEMSELECTIONMODEL_H
00043 #define QITEMSELECTIONMODEL_H
00044 
00045 #include <QtCore/qset.h>
00046 #include <QtCore/qvector.h>
00047 #include <QtCore/qlist.h>
00048 #include <QtCore/qabstractitemmodel.h>
00049 
00050 QT_BEGIN_HEADER
00051 
00052 QT_BEGIN_NAMESPACE
00053 
00054 QT_MODULE(Gui)
00055 
00056 #ifndef QT_NO_ITEMVIEWS
00057 
00058 class Q_GUI_EXPORT QItemSelectionRange
00059 {
00060 
00061 public:
00062     inline QItemSelectionRange() {}
00063     inline QItemSelectionRange(const QItemSelectionRange &other)
00064         : tl(other.tl), br(other.br) {}
00065     inline QItemSelectionRange(const QModelIndex &topLeft, const QModelIndex &bottomRight);
00066     explicit inline QItemSelectionRange(const QModelIndex &index)
00067         { tl = index; br = tl; }
00068 
00069     inline int top() const { return tl.row(); }
00070     inline int left() const { return tl.column(); }
00071     inline int bottom() const { return br.row(); }
00072     inline int right() const { return br.column(); }
00073     inline int width() const { return br.column() - tl.column() + 1; }
00074     inline int height() const { return br.row() - tl.row() + 1; }
00075 
00076     inline QModelIndex topLeft() const { return QModelIndex(tl); }
00077     inline QModelIndex bottomRight() const { return QModelIndex(br); }
00078     inline QModelIndex parent() const { return tl.parent(); }
00079     inline const QAbstractItemModel *model() const { return tl.model(); }
00080 
00081     inline bool contains(const QModelIndex &index) const
00082     {
00083         return (parent() == index.parent()
00084                 && tl.row() <= index.row() && tl.column() <= index.column()
00085                 && br.row() >= index.row() && br.column() >= index.column());
00086     }
00087 
00088     inline bool contains(int row, int column, const QModelIndex &parentIndex) const
00089     {
00090         return (parent() == parentIndex
00091                 && tl.row() <= row && tl.column() <= column
00092                 && br.row() >= row && br.column() >= column);
00093     }
00094 
00095     bool intersects(const QItemSelectionRange &other) const;
00096     QItemSelectionRange intersect(const QItemSelectionRange &other) const; // ### Qt 5: make QT4_SUPPORT
00097     inline QItemSelectionRange intersected(const QItemSelectionRange &other) const
00098         { return intersect(other); }
00099 
00100     inline bool operator==(const QItemSelectionRange &other) const
00101         { return (tl == other.tl && br == other.br); }
00102     inline bool operator!=(const QItemSelectionRange &other) const
00103         { return !operator==(other); }
00104 
00105     inline bool isValid() const
00106     {
00107         return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
00108                 && top() <= bottom() && left() <= right());
00109     }
00110 
00111     bool isEmpty() const;
00112 
00113     QModelIndexList indexes() const;
00114 
00115 private:
00116     QPersistentModelIndex tl, br;
00117 };
00118 Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE);
00119 
00120 inline QItemSelectionRange::QItemSelectionRange(const QModelIndex &atopLeft,
00121                                                 const QModelIndex &abottomRight)
00122 { tl = atopLeft; br = abottomRight; }
00123 
00124 class QItemSelection;
00125 class QItemSelectionModelPrivate;
00126 
00127 class Q_GUI_EXPORT QItemSelectionModel : public QObject
00128 {
00129     Q_OBJECT
00130     Q_DECLARE_PRIVATE(QItemSelectionModel)
00131     Q_FLAGS(SelectionFlags)
00132 
00133 public:
00134 
00135     enum SelectionFlag {
00136         NoUpdate       = 0x0000,
00137         Clear          = 0x0001,
00138         Select         = 0x0002,
00139         Deselect       = 0x0004,
00140         Toggle         = 0x0008,
00141         Current        = 0x0010,
00142         Rows           = 0x0020,
00143         Columns        = 0x0040,
00144         SelectCurrent  = Select | Current,
00145         ToggleCurrent  = Toggle | Current,
00146         ClearAndSelect = Clear | Select
00147     };
00148 
00149     Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
00150 
00151     explicit QItemSelectionModel(QAbstractItemModel *model);
00152     explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
00153     virtual ~QItemSelectionModel();
00154 
00155     QModelIndex currentIndex() const;
00156 
00157     bool isSelected(const QModelIndex &index) const;
00158     bool isRowSelected(int row, const QModelIndex &parent) const;
00159     bool isColumnSelected(int column, const QModelIndex &parent) const;
00160 
00161     bool rowIntersectsSelection(int row, const QModelIndex &parent) const;
00162     bool columnIntersectsSelection(int column, const QModelIndex &parent) const;
00163 
00164     bool hasSelection() const;
00165 
00166     QModelIndexList selectedIndexes() const;
00167     QModelIndexList selectedRows(int column = 0) const;
00168     QModelIndexList selectedColumns(int row = 0) const;
00169     const QItemSelection selection() const;
00170 
00171     const QAbstractItemModel *model() const;
00172 
00173 public Q_SLOTS:
00174     void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
00175     virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
00176     virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
00177     virtual void clear();
00178     virtual void reset();
00179 
00180     void clearSelection();
00181 
00182 Q_SIGNALS:
00183     void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
00184     void currentChanged(const QModelIndex &current, const QModelIndex &previous);
00185     void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
00186     void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
00187 
00188 protected:
00189     QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
00190     void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
00191 
00192 private:
00193     Q_DISABLE_COPY(QItemSelectionModel)
00194     Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
00195     Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int))
00196     Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int))
00197     Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int))
00198     Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged())
00199     Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged())
00200 };
00201 
00202 Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
00203 
00204 // dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC
00205 inline uint qHash(const QItemSelectionRange &) { return 0; }
00206 
00207 class Q_GUI_EXPORT QItemSelection : public QList<QItemSelectionRange>
00208 {
00209 public:
00210     QItemSelection() {}
00211     QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
00212     void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
00213     bool contains(const QModelIndex &index) const;
00214     QModelIndexList indexes() const;
00215     void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
00216     static void split(const QItemSelectionRange &range,
00217                       const QItemSelectionRange &other,
00218                       QItemSelection *result);
00219 };
00220 
00221 #ifndef QT_NO_DEBUG_STREAM
00222 Q_GUI_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
00223 #endif
00224 
00225 #endif // QT_NO_ITEMVIEWS
00226 
00227 QT_END_NAMESPACE
00228 
00229 QT_END_HEADER
00230 
00231 #endif // QITEMSELECTIONMODEL_H