src/gui/itemviews/qitemselectionmodel.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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     QModelIndexList indexes() const;
       
   112 
       
   113 private:
       
   114     QPersistentModelIndex tl, br;
       
   115 };
       
   116 Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE);
       
   117 
       
   118 inline QItemSelectionRange::QItemSelectionRange(const QModelIndex &atopLeft,
       
   119                                                 const QModelIndex &abottomRight)
       
   120 { tl = atopLeft; br = abottomRight; }
       
   121 
       
   122 class QItemSelection;
       
   123 class QItemSelectionModelPrivate;
       
   124 
       
   125 class Q_GUI_EXPORT QItemSelectionModel : public QObject
       
   126 {
       
   127     Q_OBJECT
       
   128     Q_DECLARE_PRIVATE(QItemSelectionModel)
       
   129     Q_FLAGS(SelectionFlags)
       
   130 
       
   131 public:
       
   132 
       
   133     enum SelectionFlag {
       
   134         NoUpdate       = 0x0000,
       
   135         Clear          = 0x0001,
       
   136         Select         = 0x0002,
       
   137         Deselect       = 0x0004,
       
   138         Toggle         = 0x0008,
       
   139         Current        = 0x0010,
       
   140         Rows           = 0x0020,
       
   141         Columns        = 0x0040,
       
   142         SelectCurrent  = Select | Current,
       
   143         ToggleCurrent  = Toggle | Current,
       
   144         ClearAndSelect = Clear | Select
       
   145     };
       
   146 
       
   147     Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
       
   148 
       
   149     explicit QItemSelectionModel(QAbstractItemModel *model);
       
   150     explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
       
   151     virtual ~QItemSelectionModel();
       
   152 
       
   153     QModelIndex currentIndex() const;
       
   154 
       
   155     bool isSelected(const QModelIndex &index) const;
       
   156     bool isRowSelected(int row, const QModelIndex &parent) const;
       
   157     bool isColumnSelected(int column, const QModelIndex &parent) const;
       
   158 
       
   159     bool rowIntersectsSelection(int row, const QModelIndex &parent) const;
       
   160     bool columnIntersectsSelection(int column, const QModelIndex &parent) const;
       
   161 
       
   162     bool hasSelection() const;
       
   163 
       
   164     QModelIndexList selectedIndexes() const;
       
   165     QModelIndexList selectedRows(int column = 0) const;
       
   166     QModelIndexList selectedColumns(int row = 0) const;
       
   167     const QItemSelection selection() const;
       
   168 
       
   169     const QAbstractItemModel *model() const;
       
   170 
       
   171 public Q_SLOTS:
       
   172     void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
       
   173     virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
       
   174     virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
       
   175     virtual void clear();
       
   176     virtual void reset();
       
   177 
       
   178     void clearSelection();
       
   179 
       
   180 Q_SIGNALS:
       
   181     void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
       
   182     void currentChanged(const QModelIndex &current, const QModelIndex &previous);
       
   183     void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
       
   184     void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
       
   185 
       
   186 protected:
       
   187     QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
       
   188     void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
       
   189 
       
   190 private:
       
   191     Q_DISABLE_COPY(QItemSelectionModel)
       
   192     Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
       
   193     Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int))
       
   194     Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int))
       
   195     Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int))
       
   196     Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged())
       
   197     Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged())
       
   198 };
       
   199 
       
   200 Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
       
   201 
       
   202 // dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC
       
   203 inline uint qHash(const QItemSelectionRange &) { return 0; }
       
   204 
       
   205 class Q_GUI_EXPORT QItemSelection : public QList<QItemSelectionRange>
       
   206 {
       
   207 public:
       
   208     QItemSelection() {}
       
   209     QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
       
   210     void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
       
   211     bool contains(const QModelIndex &index) const;
       
   212     QModelIndexList indexes() const;
       
   213     void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
       
   214     static void split(const QItemSelectionRange &range,
       
   215                       const QItemSelectionRange &other,
       
   216                       QItemSelection *result);
       
   217 };
       
   218 
       
   219 #ifndef QT_NO_DEBUG_STREAM
       
   220 Q_GUI_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
       
   221 #endif
       
   222 
       
   223 #endif // QT_NO_ITEMVIEWS
       
   224 
       
   225 QT_END_NAMESPACE
       
   226 
       
   227 QT_END_HEADER
       
   228 
       
   229 #endif // QITEMSELECTIONMODEL_H