summaryrefslogtreecommitdiffstats
path: root/src/corelib/itemmodels/qitemselectionmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/itemmodels/qitemselectionmodel.cpp')
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.cpp166
1 files changed, 133 insertions, 33 deletions
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp
index 5395fd5d09..47fcab9b24 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.cpp
+++ b/src/corelib/itemmodels/qitemselectionmodel.cpp
@@ -1,7 +1,7 @@
/****************************************************************************
**
-** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
-** Contact: http://www.qt-project.org/legal
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
@@ -10,9 +10,9 @@
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and Digia. For licensing terms and
-** conditions see http://qt.digia.com/licensing. For further information
-** use the contact form at http://qt.digia.com/contact-us.
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
@@ -23,8 +23,8 @@
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
-** In addition, as a special exception, Digia gives you certain additional
-** rights. These rights are described in the Digia Qt LGPL Exception
+** As a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
@@ -589,33 +589,51 @@ void QItemSelection::split(const QItemSelectionRange &range,
}
-void QItemSelectionModelPrivate::initModel(QAbstractItemModel *model)
+void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m)
{
- this->model = model;
+ struct Cx {
+ const char *signal;
+ const char *slot;
+ };
+ static const Cx connections[] = {
+ { SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
+ SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)) },
+ { SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
+ SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)) },
+ { SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
+ SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)) },
+ { SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
+ SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)) },
+ { SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
+ SLOT(_q_layoutAboutToBeChanged()) },
+ { SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
+ SLOT(_q_layoutAboutToBeChanged()) },
+ { SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
+ SLOT(_q_layoutChanged()) },
+ { SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
+ SLOT(_q_layoutChanged()) },
+ { SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
+ SLOT(_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) },
+ { SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
+ SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) },
+ { SIGNAL(modelReset()),
+ SLOT(reset()) },
+ { 0, 0 }
+ };
+
+ if (model == m)
+ return;
+
+ Q_Q(QItemSelectionModel);
if (model) {
- Q_Q(QItemSelectionModel);
- QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
- q, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
- q, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
- q, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
- q, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
- QObject::connect(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- q, SLOT(_q_layoutAboutToBeChanged()));
- QObject::connect(model, SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- q, SLOT(_q_layoutAboutToBeChanged()));
- QObject::connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
- q, SLOT(_q_layoutChanged()));
- QObject::connect(model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
- q, SLOT(_q_layoutChanged()));
- QObject::connect(model, SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- q, SLOT(_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
- QObject::connect(model, SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- q, SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
- QObject::connect(model, SIGNAL(modelReset()),
- q, SLOT(reset()));
+ for (const Cx *cx = &connections[0]; cx->signal; cx++)
+ QObject::disconnect(model, cx->signal, q, cx->slot);
+ q->reset();
+ }
+ model = m;
+ if (model) {
+ for (const Cx *cx = &connections[0]; cx->signal; cx++)
+ QObject::connect(model, cx->signal, q, cx->slot);
}
}
@@ -1078,6 +1096,10 @@ void QItemSelectionModelPrivate::_q_layoutChanged(const QList<QPersistentModelIn
selection. All functions operate on both layers; for example,
\l {QTableWidget::selectedItems()}{selecteditems()} will return items from both layers.
+ \note Since 5.5, \l{QItemSelectionModel::model()}{model},
+ \l{QItemSelectionModel::hasSelection()}{hasSelection}, and
+ \l{QItemSelectionModel::currentIndex()}{currentIndex} are meta-object properties.
+
\sa {Model/View Programming}, QAbstractItemModel, {Chart Example}
*/
@@ -1175,6 +1197,16 @@ void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::
*/
/*!
+ \fn void QItemSelectionModel::modelChanged(QAbstractItemModel *model)
+ \since 5.5
+
+ This signal is emitted when the \a model is successfully set with setModel().
+
+ \sa model(), setModel()
+*/
+
+
+/*!
\enum QItemSelectionModel::SelectionFlag
This enum describes the way the selection model will be updated.
@@ -1205,6 +1237,10 @@ void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::
void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
{
Q_D(QItemSelectionModel);
+ if (!d->model) {
+ qWarning("QItemSelectionModel: Selecting when no model has been set will result in a no-op.");
+ return;
+ }
if (command == NoUpdate)
return;
@@ -1312,6 +1348,10 @@ void QItemSelectionModel::clearSelection()
void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
{
Q_D(QItemSelectionModel);
+ if (!d->model) {
+ qWarning("QItemSelectionModel: Setting the current index when no model has been set will result in a no-op.");
+ return;
+ }
if (index == d->currentIndex) {
if (command != NoUpdate)
select(index, command); // select item
@@ -1387,6 +1427,8 @@ bool QItemSelectionModel::isSelected(const QModelIndex &index) const
bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) const
{
Q_D(const QItemSelectionModel);
+ if (!d->model)
+ return false;
if (parent.isValid() && d->model != parent.model())
return false;
@@ -1447,6 +1489,8 @@ bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) cons
bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent) const
{
Q_D(const QItemSelectionModel);
+ if (!d->model)
+ return false;
if (parent.isValid() && d->model != parent.model())
return false;
@@ -1503,6 +1547,8 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent
bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent) const
{
Q_D(const QItemSelectionModel);
+ if (!d->model)
+ return false;
if (parent.isValid() && d->model != parent.model())
return false;
@@ -1535,6 +1581,8 @@ bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &par
bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent) const
{
Q_D(const QItemSelectionModel);
+ if (!d->model)
+ return false;
if (parent.isValid() && d->model != parent.model())
return false;
@@ -1672,6 +1720,40 @@ const QItemSelection QItemSelectionModel::selection() const
}
/*!
+ \since 5.5
+
+ \property QItemSelectionModel::hasSelection
+ \internal
+*/
+/*!
+ \since 5.5
+
+ \property QItemSelectionModel::currentIndex
+ \internal
+*/
+/*!
+ \since 5.5
+
+ \property QItemSelectionModel::selectedIndexes
+*/
+/*!
+ \since 5.5
+
+ \property QItemSelectionModel::model
+ \internal
+*/
+
+/*!
+ \since 5.5
+
+ Returns the item model operated on by the selection model.
+*/
+QAbstractItemModel *QItemSelectionModel::model()
+{
+ return d_func()->model;
+}
+
+/*!
Returns the item model operated on by the selection model.
*/
const QAbstractItemModel *QItemSelectionModel::model() const
@@ -1680,6 +1762,23 @@ const QAbstractItemModel *QItemSelectionModel::model() const
}
/*!
+ \since 5.5
+
+ Sets the model. The modelChanged() signal will be emitted.
+
+ \sa model(), modelChanged()
+*/
+void QItemSelectionModel::setModel(QAbstractItemModel *model)
+{
+ Q_D(QItemSelectionModel);
+ if (d->model == model)
+ return;
+
+ d->initModel(model);
+ emit modelChanged(model);
+}
+
+/*!
Compares the two selections \a newSelection and \a oldSelection
and emits selectionChanged() with the deselected and selected items.
*/
@@ -1755,9 +1854,10 @@ void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelectio
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QItemSelectionRange &range)
{
+ QDebugStateSaver saver(dbg);
dbg.nospace() << "QItemSelectionRange(" << range.topLeft()
<< ',' << range.bottomRight() << ')';
- return dbg.space();
+ return dbg;
}
#endif