Make it possible to use new syntax to connect to model signals.

The private signals can not be used as function pointers, as
required by the new syntax, so we introduce a parameter which
can only be created privately.

Change-Id: I3d7bb8a163e764d685e8007cba831fb77e3c6855
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Stephen Kelly 2012-07-09 09:56:56 +02:00 committed by Qt by Nokia
parent 0efa445141
commit 536ec793b6
3 changed files with 190 additions and 31 deletions

View File

@ -2483,7 +2483,7 @@ void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, i
Q_ASSERT(last >= first);
Q_D(QAbstractItemModel);
d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
emit rowsAboutToBeInserted(parent, first, last);
emit rowsAboutToBeInserted(parent, first, last, QPrivateSignal());
d->rowsAboutToBeInserted(parent, first, last);
}
@ -2500,7 +2500,7 @@ void QAbstractItemModel::endInsertRows()
Q_D(QAbstractItemModel);
QAbstractItemModelPrivate::Change change = d->changes.pop();
d->rowsInserted(change.parent, change.first, change.last);
emit rowsInserted(change.parent, change.first, change.last);
emit rowsInserted(change.parent, change.first, change.last, QPrivateSignal());
}
/*!
@ -2537,7 +2537,7 @@ void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, i
Q_ASSERT(last >= first);
Q_D(QAbstractItemModel);
d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
emit rowsAboutToBeRemoved(parent, first, last);
emit rowsAboutToBeRemoved(parent, first, last, QPrivateSignal());
d->rowsAboutToBeRemoved(parent, first, last);
}
@ -2554,7 +2554,7 @@ void QAbstractItemModel::endRemoveRows()
Q_D(QAbstractItemModel);
QAbstractItemModelPrivate::Change change = d->changes.pop();
d->rowsRemoved(change.parent, change.first, change.last);
emit rowsRemoved(change.parent, change.first, change.last);
emit rowsRemoved(change.parent, change.first, change.last, QPrivateSignal());
}
/*!
@ -2699,7 +2699,7 @@ bool QAbstractItemModel::beginMoveRows(const QModelIndex &sourceParent, int sour
destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent;
d->changes.push(destinationChange);
emit rowsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild);
emit rowsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, QPrivateSignal());
d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical);
return true;
}
@ -2734,7 +2734,7 @@ void QAbstractItemModel::endMoveRows()
d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Vertical);
emit rowsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first);
emit rowsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, QPrivateSignal());
}
/*!
@ -2784,7 +2784,7 @@ void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first
Q_ASSERT(last >= first);
Q_D(QAbstractItemModel);
d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
emit columnsAboutToBeInserted(parent, first, last);
emit columnsAboutToBeInserted(parent, first, last, QPrivateSignal());
d->columnsAboutToBeInserted(parent, first, last);
}
@ -2802,7 +2802,7 @@ void QAbstractItemModel::endInsertColumns()
Q_D(QAbstractItemModel);
QAbstractItemModelPrivate::Change change = d->changes.pop();
d->columnsInserted(change.parent, change.first, change.last);
emit columnsInserted(change.parent, change.first, change.last);
emit columnsInserted(change.parent, change.first, change.last, QPrivateSignal());
}
/*!
@ -2839,7 +2839,7 @@ void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first
Q_ASSERT(last >= first);
Q_D(QAbstractItemModel);
d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
emit columnsAboutToBeRemoved(parent, first, last);
emit columnsAboutToBeRemoved(parent, first, last, QPrivateSignal());
d->columnsAboutToBeRemoved(parent, first, last);
}
@ -2856,7 +2856,7 @@ void QAbstractItemModel::endRemoveColumns()
Q_D(QAbstractItemModel);
QAbstractItemModelPrivate::Change change = d->changes.pop();
d->columnsRemoved(change.parent, change.first, change.last);
emit columnsRemoved(change.parent, change.first, change.last);
emit columnsRemoved(change.parent, change.first, change.last, QPrivateSignal());
}
/*!
@ -2918,7 +2918,7 @@ bool QAbstractItemModel::beginMoveColumns(const QModelIndex &sourceParent, int s
d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal);
emit columnsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild);
emit columnsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, QPrivateSignal());
return true;
}
@ -2952,7 +2952,7 @@ void QAbstractItemModel::endMoveColumns()
d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Horizontal);
emit columnsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first);
emit columnsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, QPrivateSignal());
}
/*!
@ -3002,7 +3002,7 @@ void QAbstractItemModel::endMoveColumns()
*/
void QAbstractItemModel::beginResetModel()
{
emit modelAboutToBeReset();
emit modelAboutToBeReset(QPrivateSignal());
}
/*!
@ -3020,7 +3020,7 @@ void QAbstractItemModel::endResetModel()
{
Q_D(QAbstractItemModel);
d->invalidatePersistentIndexes();
emit modelReset();
emit modelReset(QPrivateSignal());
}
/*!

View File

@ -249,30 +249,82 @@ Q_SIGNALS:
void layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>());
void layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>());
#if !defined(Q_MOC_RUN) && !defined(qdoc)
private: // can only be emitted by QAbstractItemModel
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
void rowsInserted(const QModelIndex &parent, int first, int last);
);
void rowsInserted(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
void rowsRemoved(const QModelIndex &parent, int first, int last);
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void rowsRemoved(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
void columnsInserted(const QModelIndex &parent, int first, int last);
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void columnsInserted(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
void columnsRemoved(const QModelIndex &parent, int first, int last);
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void columnsRemoved(const QModelIndex &parent, int first, int last
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void modelAboutToBeReset();
void modelReset();
void modelAboutToBeReset(
#if !defined(qdoc)
QPrivateSignal
#endif
);
void modelReset(
#if !defined(qdoc)
QPrivateSignal
#endif
);
void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow );
void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row );
void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn );
void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column );
void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn
#if !defined(qdoc)
, QPrivateSignal
#endif
);
void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column
#if !defined(qdoc)
, QPrivateSignal
#endif
);
public Q_SLOTS:
virtual bool submit();

View File

@ -118,6 +118,8 @@ private slots:
void testRoleNames();
void testDragActions();
void testFunctionPointerSignalConnection();
private:
DynamicTreeModel *m_model;
};
@ -2177,5 +2179,110 @@ void tst_QAbstractItemModel::testDragActions()
QVERIFY(actions & Qt::MoveAction);
}
class SignalConnectionTester : public QObject
{
Q_OBJECT
public:
SignalConnectionTester(QObject *parent = 0)
: QObject(parent), testPassed(false)
{
}
public Q_SLOTS:
void testSlot()
{
testPassed = true;
}
void testSlotWithParam_1(const QModelIndex &idx)
{
testPassed = !idx.isValid();
}
void testSlotWithParam_2(const QModelIndex &idx, int start)
{
testPassed = !idx.isValid() && start == 0;
}
void testSlotWithParam_3(const QModelIndex &idx, int start, int end)
{
testPassed = !idx.isValid() && start == 0 && end == 1;
}
public:
bool testPassed;
};
void tst_QAbstractItemModel::testFunctionPointerSignalConnection()
{
QStringListModel model;
{
SignalConnectionTester tester;
QObject::connect(&model, &QAbstractItemModel::rowsInserted, &tester, &SignalConnectionTester::testSlot);
QVERIFY(!tester.testPassed);
model.insertRows(0, 2);
QVERIFY(tester.testPassed);
tester.testPassed = false;
QMetaObject::invokeMethod(&model, "rowsInserted", Q_ARG(QModelIndex, QModelIndex()), Q_ARG(int, 0), Q_ARG(int, 1));
QVERIFY(tester.testPassed);
}
{
SignalConnectionTester tester;
QObject::connect(&model, &QAbstractItemModel::rowsInserted, &tester, &SignalConnectionTester::testSlotWithParam_1);
QVERIFY(!tester.testPassed);
model.insertRows(0, 2);
QVERIFY(tester.testPassed);
tester.testPassed = false;
QMetaObject::invokeMethod(&model, "rowsInserted", Q_ARG(QModelIndex, QModelIndex()), Q_ARG(int, 0), Q_ARG(int, 1));
QVERIFY(tester.testPassed);
}
{
SignalConnectionTester tester;
QObject::connect(&model, &QAbstractItemModel::rowsInserted, &tester, &SignalConnectionTester::testSlotWithParam_2);
QVERIFY(!tester.testPassed);
model.insertRows(0, 2);
QVERIFY(tester.testPassed);
tester.testPassed = false;
QMetaObject::invokeMethod(&model, "rowsInserted", Q_ARG(QModelIndex, QModelIndex()), Q_ARG(int, 0), Q_ARG(int, 1));
QVERIFY(tester.testPassed);
}
{
SignalConnectionTester tester;
QObject::connect(&model, &QAbstractItemModel::rowsInserted, &tester, &SignalConnectionTester::testSlotWithParam_3);
QVERIFY(!tester.testPassed);
model.insertRows(0, 2);
QVERIFY(tester.testPassed);
tester.testPassed = false;
QMetaObject::invokeMethod(&model, "rowsInserted", Q_ARG(QModelIndex, QModelIndex()), Q_ARG(int, 0), Q_ARG(int, 1));
QVERIFY(tester.testPassed);
}
{
SignalConnectionTester tester;
QObject::connect(&model, SIGNAL(rowsInserted(QModelIndex,int,int)), &tester, SLOT(testSlot()));
QVERIFY(!tester.testPassed);
model.insertRows(0, 2);
QVERIFY(tester.testPassed);
tester.testPassed = false;
QMetaObject::invokeMethod(&model, "rowsInserted", Q_ARG(QModelIndex, QModelIndex()), Q_ARG(int, 0), Q_ARG(int, 1));
QVERIFY(tester.testPassed);
}
// Intentionally does not compile.
// model.rowsInserted(QModelIndex(), 0, 0);
}
QTEST_MAIN(tst_QAbstractItemModel)
#include "tst_qabstractitemmodel.moc"