📄 qitemselectionmodel.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qitemselectionmodel.h"#include <private/qitemselectionmodel_p.h>#include <qdebug.h>#ifndef QT_NO_ITEMVIEWS/*! \class QItemSelectionRange \brief The QItemSelectionRange class manages information about a range of selected items in a model. \ingroup model-view A QItemSelectionRange contains information about a range of selected items in a model. A range of items is a contiguous array of model items, extending to cover a number of adjacent rows and columns with a common parent item; this can be visualized as a two-dimensional block of cells in a table. A selection range has a top(), left() a bottom(), right() and a parent(). The QItemSelectionRange class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. The model items contained in the selection range can be obtained by using the items() function. Use QItemSelectionModel::selectedIndexes() to get a list of all selected items for a view. You can determine whether a given model item lies within a particular range by using the contains() function. Ranges can also be compared using the overloaded operators for equality and inequality, and the intersects() function allows you to determine whether two ranges overlap. \sa {Model/View Programming}, QAbstractItemModel, QItemSelection, QItemSelectionModel*//*! \fn QItemSelectionRange::QItemSelectionRange() Constructs an empty selection range.*//*! \fn QItemSelectionRange::QItemSelectionRange(const QItemSelectionRange &other) Copy constructor. Constructs a new selection range with the same contents as the \a other range given.*//*! \fn QItemSelectionRange::QItemSelectionRange(const QModelIndex &topLeft, const QModelIndex &bottomRight) Constructs a new selection range containing only the index specified by the \a topLeft and the index \a bottomRight.*//*! \fn QItemSelectionRange::QItemSelectionRange(const QModelIndex &index) Constructs a new selection range containing only the model item specified by the model index \a index.*//*! \fn int QItemSelectionRange::top() const Returns the row index corresponding to the uppermost selected row in the selection range.*//*! \fn int QItemSelectionRange::left() const Returns the column index corresponding to the leftmost selected column in the selection range.*//*! \fn int QItemSelectionRange::bottom() const Returns the row index corresponding to the lowermost selected row in the selection range.*//*! \fn int QItemSelectionRange::right() const Returns the column index corresponding to the rightmost selected column in the selection range.*//*! \fn int QItemSelectionRange::width() const Returns the number of selected columns in the selection range.*//*! \fn int QItemSelectionRange::height() const Returns the number of selected rows in the selection range.*//*! \fn const QAbstractItemModel *QItemSelectionRange::model() const Returns the model that the items in the selection range belong to.*//*! \fn QModelIndex QItemSelectionRange::topLeft() const Returns the index for the item located at the top-left corner of the selection range. \sa top(), left(), bottomRight()*//*! \fn QModelIndex QItemSelectionRange::bottomRight() const Returns the index for the item located at the bottom-right corner of the selection range. \sa bottom(), right(), topLeft()*//*! \fn QModelIndex QItemSelectionRange::parent() const Returns the parent model item index of the items in the selection range.*//*! \fn bool QItemSelectionRange::contains(const QModelIndex &index) const Returns true if the model item specified by the \a index lies within the range of selected items; otherwise returns false.*//*! \fn bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const Returns true if this selection range intersects (overlaps with) the \a other range given; otherwise returns false.*/bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const{ return (isValid() && other.isValid() && parent() == other.parent() && ((top() <= other.top() && bottom() >= other.top()) || (top() >= other.top() && top() <= other.bottom())) && ((left() <= other.left() && right() >= other.left()) || (left() >= other.left() && left() <= other.right())));}/*! \fn QItemSelectionRange QItemSelectionRange::intersect(const QItemSelectionRange &other) const Returns a new selection range containing only the items that are found in both the selection range and the \a other selection range.*/QItemSelectionRange QItemSelectionRange::intersect(const QItemSelectionRange &other) const{ if (model() == other.model() && parent() == other.parent()) { QModelIndex topLeft = model()->index(qMax(top(), other.top()), qMax(left(), other.left()), other.parent()); QModelIndex bottomRight = model()->index(qMin(bottom(), other.bottom()), qMin(right(), other.right()), other.parent()); return QItemSelectionRange(topLeft, bottomRight); } return QItemSelectionRange();}/*! \fn bool QItemSelectionRange::operator==(const QItemSelectionRange &other) const Returns true if the selection range is exactly the same as the \a other range given; otherwise returns false.*//*! \fn bool QItemSelectionRange::operator!=(const QItemSelectionRange &other) const Returns true if the selection range differs from the \a other range given; otherwise returns false.*//*! \fn bool QItemSelectionRange::isValid() const Returns true if the selection range is valid; otherwise returns false.*//*! Returns the list of model index items stored in the selection.*/QModelIndexList QItemSelectionRange::indexes() const{ QModelIndex index; QModelIndexList result; if (isValid()) { for (int column = left(); column <= right(); ++column) { for (int row = top(); row <= bottom(); ++row) { index = model()->index(row, column, parent()); if (model()->flags(index) & Qt::ItemIsSelectable) result.append(index); } } } return result;}/*! \class QItemSelection \brief The QItemSelection class manages information about selected items in a model. \ingroup model-view A QItemSelection describes the items in a model that have been selected by the user. A QItemSelection is basically a list of selection ranges, see QItemSelectionRange. It provides functions for creating and manipulating selections, and selecting a range of items from a model. The QItemSelection class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. An item selection can be constructed and initialized to contain a range of items from an existing model. The following example constructs a selection that contains a range of items from the given \c model, beginning at the \c topLeft, and ending at the \c bottomRight. \code QItemSelection *selection = new QItemSelection(topLeft, bottomRight); \endcode An empty item selection can be constructed, and later populated as required. So, if the model is going to be unavailable when we construct the item selection, we can rewrite the above code in the following way: \code QItemSelection *selection = new QItemSelection(); ... selection->select(topLeft, bottomRight); \endcode QItemSelection saves memory, and avoids unnecessary work, by working with selection ranges rather than recording the model item index for each item in the selection. Generally, an instance of this class will contain a list of non-overlapping selection ranges. Use merge() to merge one item selection into another without making overlapping ranges. Use split() to split one selection range into smaller ranges based on a another selection range. \sa {Model/View Programming}, QItemSelectionModel*//*! \fn QItemSelection::QItemSelection() Constructs an empty selection.*//*! Constructs an item selection that extends from the top-left model item, specified by the \a topLeft index, to the bottom-right item, specified by \a bottomRight.*/QItemSelection::QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight){ select(topLeft, bottomRight);}/*! Adds the items in the range that extends from the top-left model item, specified by the \a topLeft index, to the bottom-right item, specified by \a bottomRight to the list.*/void QItemSelection::select(const QModelIndex &topLeft, const QModelIndex &bottomRight){ if (!topLeft.isValid() || !bottomRight.isValid()) return; if ((topLeft.model() != bottomRight.model()) || topLeft.parent() != bottomRight.parent()) { qWarning("Can't select indexes from different model or with different parents"); return; } if (topLeft.row() > bottomRight.row() || topLeft.column() > bottomRight.column()) { int top = qMin(topLeft.row(), bottomRight.row()); int bottom = qMax(topLeft.row(), bottomRight.row()); int left = qMin(topLeft.column(), bottomRight.column()); int right = qMax(topLeft.column(), bottomRight.column()); QModelIndex tl = topLeft.sibling(top, left); QModelIndex br = bottomRight.sibling(bottom, right); append(QItemSelectionRange(tl, br)); return; } append(QItemSelectionRange(topLeft, bottomRight));}/*! Returns true if the selection contains the given \a index; otherwise returns false.*/bool QItemSelection::contains(const QModelIndex &index) const{ QList<QItemSelectionRange>::const_iterator it = begin(); for (; it != end(); ++it) if ((*it).contains(index)) return true; return false;}/*! Returns a list of model indexes that correspond to the selected items.*/QModelIndexList QItemSelection::indexes() const{ QModelIndexList result; QList<QItemSelectionRange>::const_iterator it = begin(); for (; it != end(); ++it) result += (*it).indexes(); return result;}/*! Merges the \a other selection with this QItemSelection using the \a command given. This method guarantees that no ranges are overlapping. Note that only QItemSelectionModel::Select, QItemSelectionModel::Deselect, and QItemSelectionModel::Toggle are supported. \sa split()*/void QItemSelection::merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command){ if (!(command & QItemSelectionModel::Select || command & QItemSelectionModel::Deselect || command & QItemSelectionModel::Toggle) || other.isEmpty())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -