Fix Qt4 to Qt5 porting issues with debug build
Also update modeltest to Qt5
This commit is contained in:
parent
90fe2080c1
commit
db231054da
@ -1,542 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
**
|
|
||||||
** Copyright (C) 2007 Trolltech ASA. All rights reserved.
|
|
||||||
**
|
|
||||||
** This file is part of the Qt Concurrent project on Trolltech Labs.
|
|
||||||
**
|
|
||||||
** 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 <QtGui/QtGui>
|
|
||||||
|
|
||||||
#include "modeltest.h"
|
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QModelIndex)
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Connect to all of the models signals. Whenever anything happens recheck everything.
|
|
||||||
*/
|
|
||||||
ModelTest::ModelTest(QAbstractItemModel *_model, QObject *parent) : QObject(parent), model(_model), fetchingMore(false)
|
|
||||||
{
|
|
||||||
Q_ASSERT(model);
|
|
||||||
|
|
||||||
connect(model, SIGNAL(columnsAboutToBeInserted(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(columnsRemoved(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(layoutAboutToBeChanged ()), this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(layoutChanged ()), this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(modelReset ()), this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(runAllTests()));
|
|
||||||
|
|
||||||
// Special checks for inserting/removing
|
|
||||||
connect(model, SIGNAL(layoutAboutToBeChanged()),
|
|
||||||
this, SLOT(layoutAboutToBeChanged()));
|
|
||||||
connect(model, SIGNAL(layoutChanged()),
|
|
||||||
this, SLOT(layoutChanged()));
|
|
||||||
|
|
||||||
connect(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(rowsAboutToBeInserted(const QModelIndex &, int, int)));
|
|
||||||
connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
|
|
||||||
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(rowsInserted(const QModelIndex &, int, int)));
|
|
||||||
connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
|
|
||||||
this, SLOT(rowsRemoved(const QModelIndex &, int, int)));
|
|
||||||
|
|
||||||
runAllTests();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ModelTest::runAllTests()
|
|
||||||
{
|
|
||||||
if (fetchingMore)
|
|
||||||
return;
|
|
||||||
nonDestructiveBasicTest();
|
|
||||||
rowCount();
|
|
||||||
columnCount();
|
|
||||||
hasIndex();
|
|
||||||
index();
|
|
||||||
parent();
|
|
||||||
data();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
nonDestructiveBasicTest tries to call a number of the basic functions (not all)
|
|
||||||
to make sure the model doesn't outright segfault, testing the functions that makes sense.
|
|
||||||
*/
|
|
||||||
void ModelTest::nonDestructiveBasicTest()
|
|
||||||
{
|
|
||||||
Q_ASSERT(model->buddy(QModelIndex()) == QModelIndex());
|
|
||||||
model->canFetchMore(QModelIndex());
|
|
||||||
Q_ASSERT(model->columnCount(QModelIndex()) >= 0);
|
|
||||||
Q_ASSERT(model->data(QModelIndex()) == QVariant());
|
|
||||||
fetchingMore = true;
|
|
||||||
model->fetchMore(QModelIndex());
|
|
||||||
fetchingMore = false;
|
|
||||||
Qt::ItemFlags flags = model->flags(QModelIndex());
|
|
||||||
Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);
|
|
||||||
model->hasChildren(QModelIndex());
|
|
||||||
model->hasIndex(0, 0);
|
|
||||||
model->headerData(0, Qt::Horizontal);
|
|
||||||
model->index(0, 0);
|
|
||||||
Q_ASSERT(model->index(-1, -1) == QModelIndex());
|
|
||||||
model->itemData(QModelIndex());
|
|
||||||
QVariant cache;
|
|
||||||
model->match(QModelIndex(), -1, cache);
|
|
||||||
model->mimeTypes();
|
|
||||||
Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
|
|
||||||
Q_ASSERT(model->rowCount() >= 0);
|
|
||||||
QVariant variant;
|
|
||||||
model->setData(QModelIndex(), variant, -1);
|
|
||||||
model->setHeaderData(-1, Qt::Horizontal, QVariant());
|
|
||||||
model->setHeaderData(0, Qt::Horizontal, QVariant());
|
|
||||||
model->setHeaderData(999999, Qt::Horizontal, QVariant());
|
|
||||||
QMap<int, QVariant> roles;
|
|
||||||
model->sibling(0, 0, QModelIndex());
|
|
||||||
model->span(QModelIndex());
|
|
||||||
model->supportedDropActions();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Tests model's implementation of QAbstractItemModel::rowCount() and hasChildren()
|
|
||||||
|
|
||||||
Models that are dynamically populated are not as fully tested here.
|
|
||||||
*/
|
|
||||||
void ModelTest::rowCount()
|
|
||||||
{
|
|
||||||
// check top row
|
|
||||||
QModelIndex topIndex = model->index(0, 0, QModelIndex());
|
|
||||||
int rows = model->rowCount(topIndex);
|
|
||||||
Q_ASSERT(rows >= 0);
|
|
||||||
if (rows > 0)
|
|
||||||
Q_ASSERT(model->hasChildren(topIndex) == true);
|
|
||||||
|
|
||||||
QModelIndex secondLevelIndex = model->index(0, 0, topIndex);
|
|
||||||
if (secondLevelIndex.isValid()) { // not the top level
|
|
||||||
// check a row count where parent is valid
|
|
||||||
rows = model->rowCount(secondLevelIndex);
|
|
||||||
Q_ASSERT(rows >= 0);
|
|
||||||
if (rows > 0)
|
|
||||||
Q_ASSERT(model->hasChildren(secondLevelIndex) == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// The models rowCount() is tested more extensively in checkChildren(),
|
|
||||||
// but this catches the big mistakes
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Tests model's implementation of QAbstractItemModel::columnCount() and hasChildren()
|
|
||||||
*/
|
|
||||||
void ModelTest::columnCount()
|
|
||||||
{
|
|
||||||
// check top row
|
|
||||||
QModelIndex topIndex = model->index(0, 0, QModelIndex());
|
|
||||||
Q_ASSERT(model->columnCount(topIndex) >= 0);
|
|
||||||
|
|
||||||
// check a column count where parent is valid
|
|
||||||
QModelIndex childIndex = model->index(0, 0, topIndex);
|
|
||||||
if (childIndex.isValid())
|
|
||||||
Q_ASSERT(model->columnCount(childIndex) >= 0);
|
|
||||||
|
|
||||||
// columnCount() is tested more extensively in checkChildren(),
|
|
||||||
// but this catches the big mistakes
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Tests model's implementation of QAbstractItemModel::hasIndex()
|
|
||||||
*/
|
|
||||||
void ModelTest::hasIndex()
|
|
||||||
{
|
|
||||||
// Make sure that invalid values returns an invalid index
|
|
||||||
Q_ASSERT(model->hasIndex(-2, -2) == false);
|
|
||||||
Q_ASSERT(model->hasIndex(-2, 0) == false);
|
|
||||||
Q_ASSERT(model->hasIndex(0, -2) == false);
|
|
||||||
|
|
||||||
int rows = model->rowCount();
|
|
||||||
int columns = model->columnCount();
|
|
||||||
|
|
||||||
// check out of bounds
|
|
||||||
Q_ASSERT(model->hasIndex(rows, columns) == false);
|
|
||||||
Q_ASSERT(model->hasIndex(rows + 1, columns + 1) == false);
|
|
||||||
|
|
||||||
if (rows > 0)
|
|
||||||
Q_ASSERT(model->hasIndex(0, 0) == true);
|
|
||||||
|
|
||||||
// hasIndex() is tested more extensively in checkChildren(),
|
|
||||||
// but this catches the big mistakes
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Tests model's implementation of QAbstractItemModel::index()
|
|
||||||
*/
|
|
||||||
void ModelTest::index()
|
|
||||||
{
|
|
||||||
// Make sure that invalid values returns an invalid index
|
|
||||||
Q_ASSERT(model->index(-2, -2) == QModelIndex());
|
|
||||||
Q_ASSERT(model->index(-2, 0) == QModelIndex());
|
|
||||||
Q_ASSERT(model->index(0, -2) == QModelIndex());
|
|
||||||
|
|
||||||
int rows = model->rowCount();
|
|
||||||
int columns = model->columnCount();
|
|
||||||
|
|
||||||
if (rows == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Catch off by one errors
|
|
||||||
Q_ASSERT(model->index(rows, columns) == QModelIndex());
|
|
||||||
Q_ASSERT(model->index(0, 0).isValid() == true);
|
|
||||||
|
|
||||||
// Make sure that the same index is *always* returned
|
|
||||||
QModelIndex a = model->index(0, 0);
|
|
||||||
QModelIndex b = model->index(0, 0);
|
|
||||||
Q_ASSERT(a == b);
|
|
||||||
|
|
||||||
// index() is tested more extensively in checkChildren(),
|
|
||||||
// but this catches the big mistakes
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Tests model's implementation of QAbstractItemModel::parent()
|
|
||||||
*/
|
|
||||||
void ModelTest::parent()
|
|
||||||
{
|
|
||||||
// Make sure the model wont crash and will return an invalid QModelIndex
|
|
||||||
// when asked for the parent of an invalid index.
|
|
||||||
Q_ASSERT(model->parent(QModelIndex()) == QModelIndex());
|
|
||||||
|
|
||||||
if (model->rowCount() == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Column 0 | Column 1 |
|
|
||||||
// QModelIndex() | |
|
|
||||||
// \- topIndex | topIndex1 |
|
|
||||||
// \- childIndex | childIndex1 |
|
|
||||||
|
|
||||||
// Common error test #1, make sure that a top level index has a parent
|
|
||||||
// that is a invalid QModelIndex.
|
|
||||||
QModelIndex topIndex = model->index(0, 0, QModelIndex());
|
|
||||||
Q_ASSERT(model->parent(topIndex) == QModelIndex());
|
|
||||||
|
|
||||||
// Common error test #2, make sure that a second level index has a parent
|
|
||||||
// that is the first level index.
|
|
||||||
if (model->rowCount(topIndex) > 0) {
|
|
||||||
QModelIndex childIndex = model->index(0, 0, topIndex);
|
|
||||||
qDebug("topIndex RCI %x %x %llx", topIndex.row(), topIndex.column(), topIndex.internalId());
|
|
||||||
qDebug("topIndex I %llx", topIndex.internalId());
|
|
||||||
qDebug("childIndex RCI %x %x %llx", childIndex.row(), childIndex.column(), childIndex.internalId());
|
|
||||||
Q_ASSERT(model->parent(childIndex) == topIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Common error test #3, the second column should NOT have the same children
|
|
||||||
// as the first column in a row.
|
|
||||||
// Usually the second column shouldn't have children.
|
|
||||||
QModelIndex topIndex1 = model->index(0, 1, QModelIndex());
|
|
||||||
if (model->rowCount(topIndex1) > 0) {
|
|
||||||
QModelIndex childIndex = model->index(0, 0, topIndex);
|
|
||||||
QModelIndex childIndex1 = model->index(0, 0, topIndex1);
|
|
||||||
Q_ASSERT(childIndex != childIndex1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Full test, walk n levels deep through the model making sure that all
|
|
||||||
// parent's children correctly specify their parent.
|
|
||||||
checkChildren(QModelIndex());
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Called from the parent() test.
|
|
||||||
|
|
||||||
A model that returns an index of parent X should also return X when asking
|
|
||||||
for the parent of the index.
|
|
||||||
|
|
||||||
This recursive function does pretty extensive testing on the whole model in an
|
|
||||||
effort to catch edge cases.
|
|
||||||
|
|
||||||
This function assumes that rowCount(), columnCount() and index() already work.
|
|
||||||
If they have a bug it will point it out, but the above tests should have already
|
|
||||||
found the basic bugs because it is easier to figure out the problem in
|
|
||||||
those tests then this one.
|
|
||||||
*/
|
|
||||||
void ModelTest::checkChildren(const QModelIndex &parent, int currentDepth)
|
|
||||||
{
|
|
||||||
// First just try walking back up the tree.
|
|
||||||
QModelIndex p = parent;
|
|
||||||
while (p.isValid())
|
|
||||||
p = p.parent();
|
|
||||||
|
|
||||||
// For models that are dynamically populated
|
|
||||||
if (model->canFetchMore(parent)) {
|
|
||||||
fetchingMore = true;
|
|
||||||
model->fetchMore(parent);
|
|
||||||
fetchingMore = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int rows = model->rowCount(parent);
|
|
||||||
int columns = model->columnCount(parent);
|
|
||||||
|
|
||||||
if (rows > 0)
|
|
||||||
Q_ASSERT(model->hasChildren(parent));
|
|
||||||
|
|
||||||
// Some further testing against rows(), columns(), and hasChildren()
|
|
||||||
Q_ASSERT(rows >= 0);
|
|
||||||
Q_ASSERT(columns >= 0);
|
|
||||||
if (rows > 0)
|
|
||||||
Q_ASSERT(model->hasChildren(parent) == true);
|
|
||||||
|
|
||||||
//qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows
|
|
||||||
// << "columns:" << columns << "parent column:" << parent.column();
|
|
||||||
|
|
||||||
Q_ASSERT(model->hasIndex(rows + 1, 0, parent) == false);
|
|
||||||
for (int r = 0; r < rows; ++r) {
|
|
||||||
if (model->canFetchMore(parent)) {
|
|
||||||
fetchingMore = true;
|
|
||||||
model->fetchMore(parent);
|
|
||||||
fetchingMore = false;
|
|
||||||
}
|
|
||||||
Q_ASSERT(model->hasIndex(r, columns + 1, parent) == false);
|
|
||||||
for (int c = 0; c < columns; ++c) {
|
|
||||||
Q_ASSERT(model->hasIndex(r, c, parent) == true);
|
|
||||||
QModelIndex index = model->index(r, c, parent);
|
|
||||||
// rowCount() and columnCount() said that it existed...
|
|
||||||
Q_ASSERT(index.isValid() == true);
|
|
||||||
|
|
||||||
// index() should always return the same index when called twice in a row
|
|
||||||
QModelIndex modifiedIndex = model->index(r, c, parent);
|
|
||||||
Q_ASSERT(index == modifiedIndex);
|
|
||||||
|
|
||||||
// Make sure we get the same index if we request it twice in a row
|
|
||||||
QModelIndex a = model->index(r, c, parent);
|
|
||||||
QModelIndex b = model->index(r, c, parent);
|
|
||||||
Q_ASSERT(a == b);
|
|
||||||
|
|
||||||
// Some basic checking on the index that is returned
|
|
||||||
Q_ASSERT(index.model() == model);
|
|
||||||
Q_ASSERT(index.row() == r);
|
|
||||||
Q_ASSERT(index.column() == c);
|
|
||||||
// While you can technically return a QVariant usually this is a sign
|
|
||||||
// of an bug in data() Disable if this really is ok in your model.
|
|
||||||
//Q_ASSERT(model->data(index, Qt::DisplayRole).isValid() == true);
|
|
||||||
|
|
||||||
// If the next test fails here is some somewhat useful debug you play with.
|
|
||||||
/*
|
|
||||||
if (model->parent(index) != parent) {
|
|
||||||
qDebug() << r << c << currentDepth << model->data(index).toString()
|
|
||||||
<< model->data(parent).toString();
|
|
||||||
qDebug() << index << parent << model->parent(index);
|
|
||||||
// And a view that you can even use to show the model.
|
|
||||||
//QTreeView view;
|
|
||||||
//view.setModel(model);
|
|
||||||
//view.show();
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Check that we can get back our real parent.
|
|
||||||
QModelIndex p = model->parent(index);
|
|
||||||
//qDebug() << "child:" << index;
|
|
||||||
//qDebug() << p;
|
|
||||||
//qDebug() << parent;
|
|
||||||
Q_ASSERT(model->parent(index) == parent);
|
|
||||||
|
|
||||||
// recursively go down the children
|
|
||||||
if (model->hasChildren(index) && currentDepth < 10 ) {
|
|
||||||
//qDebug() << r << c << "has children" << model->rowCount(index);
|
|
||||||
checkChildren(index, ++currentDepth);
|
|
||||||
}/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/
|
|
||||||
|
|
||||||
// make sure that after testing the children that the index doesn't change.
|
|
||||||
QModelIndex newerIndex = model->index(r, c, parent);
|
|
||||||
Q_ASSERT(index == newerIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Tests model's implementation of QAbstractItemModel::data()
|
|
||||||
*/
|
|
||||||
void ModelTest::data()
|
|
||||||
{
|
|
||||||
// Invalid index should return an invalid qvariant
|
|
||||||
Q_ASSERT(!model->data(QModelIndex()).isValid());
|
|
||||||
|
|
||||||
if (model->rowCount() == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// A valid index should have a valid QVariant data
|
|
||||||
Q_ASSERT(model->index(0, 0).isValid());
|
|
||||||
|
|
||||||
// shouldn't be able to set data on an invalid index
|
|
||||||
Q_ASSERT(model->setData(QModelIndex(), QLatin1String("foo"), Qt::DisplayRole) == false);
|
|
||||||
|
|
||||||
// General Purpose roles that should return a QString
|
|
||||||
QVariant variant = model->data(model->index(0, 0), Qt::ToolTipRole);
|
|
||||||
if (variant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QString>(variant));
|
|
||||||
}
|
|
||||||
variant = model->data(model->index(0, 0), Qt::StatusTipRole);
|
|
||||||
if (variant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QString>(variant));
|
|
||||||
}
|
|
||||||
variant = model->data(model->index(0, 0), Qt::WhatsThisRole);
|
|
||||||
if (variant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QString>(variant));
|
|
||||||
}
|
|
||||||
|
|
||||||
// General Purpose roles that should return a QSize
|
|
||||||
variant = model->data(model->index(0, 0), Qt::SizeHintRole);
|
|
||||||
if (variant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QSize>(variant));
|
|
||||||
}
|
|
||||||
|
|
||||||
// General Purpose roles that should return a QFont
|
|
||||||
QVariant fontVariant = model->data(model->index(0, 0), Qt::FontRole);
|
|
||||||
if (fontVariant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QFont>(fontVariant));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that the alignment is one we know about
|
|
||||||
QVariant textAlignmentVariant = model->data(model->index(0, 0), Qt::TextAlignmentRole);
|
|
||||||
if (textAlignmentVariant.isValid()) {
|
|
||||||
int alignment = textAlignmentVariant.toInt();
|
|
||||||
Q_ASSERT(alignment == Qt::AlignLeft ||
|
|
||||||
alignment == Qt::AlignRight ||
|
|
||||||
alignment == Qt::AlignHCenter ||
|
|
||||||
alignment == Qt::AlignJustify ||
|
|
||||||
alignment == Qt::AlignTop ||
|
|
||||||
alignment == Qt::AlignBottom ||
|
|
||||||
alignment == Qt::AlignVCenter ||
|
|
||||||
alignment == Qt::AlignCenter ||
|
|
||||||
alignment == Qt::AlignAbsolute ||
|
|
||||||
alignment == Qt::AlignLeading ||
|
|
||||||
alignment == Qt::AlignTrailing);
|
|
||||||
}
|
|
||||||
|
|
||||||
// General Purpose roles that should return a QColor
|
|
||||||
QVariant colorVariant = model->data(model->index(0, 0), Qt::BackgroundColorRole);
|
|
||||||
if (colorVariant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
|
|
||||||
}
|
|
||||||
|
|
||||||
colorVariant = model->data(model->index(0, 0), Qt::TextColorRole);
|
|
||||||
if (colorVariant.isValid()) {
|
|
||||||
Q_ASSERT(qVariantCanConvert<QColor>(colorVariant));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that the "check state" is one we know about.
|
|
||||||
QVariant checkStateVariant = model->data(model->index(0, 0), Qt::CheckStateRole);
|
|
||||||
if (checkStateVariant.isValid()) {
|
|
||||||
int state = checkStateVariant.toInt();
|
|
||||||
Q_ASSERT(state == Qt::Unchecked ||
|
|
||||||
state == Qt::PartiallyChecked ||
|
|
||||||
state == Qt::Checked);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Store what is about to be inserted to make sure it actually happens
|
|
||||||
|
|
||||||
\sa rowsInserted()
|
|
||||||
*/
|
|
||||||
void ModelTest::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
|
|
||||||
{
|
|
||||||
Q_UNUSED(end);
|
|
||||||
Changing c;
|
|
||||||
c.parent = parent;
|
|
||||||
c.oldSize = model->rowCount(parent);
|
|
||||||
c.last = model->data(model->index(start - 1, 0, parent));
|
|
||||||
c.next = model->data(model->index(start, 0, parent));
|
|
||||||
insert.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Confirm that what was said was going to happen actually did
|
|
||||||
|
|
||||||
\sa rowsAboutToBeInserted()
|
|
||||||
*/
|
|
||||||
void ModelTest::rowsInserted(const QModelIndex & parent, int start, int end)
|
|
||||||
{
|
|
||||||
Changing c = insert.pop();
|
|
||||||
Q_ASSERT(c.parent == parent);
|
|
||||||
Q_ASSERT(c.oldSize + (end - start + 1) == model->rowCount(parent));
|
|
||||||
Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
|
|
||||||
/*
|
|
||||||
if (c.next != model->data(model->index(end + 1, 0, c.parent))) {
|
|
||||||
qDebug() << start << end;
|
|
||||||
for (int i=0; i < model->rowCount(); ++i)
|
|
||||||
qDebug() << model->index(i, 0).data().toString();
|
|
||||||
qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
Q_ASSERT(c.next == model->data(model->index(end + 1, 0, c.parent)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ModelTest::layoutAboutToBeChanged()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < qBound(0, model->rowCount(), 100); ++i)
|
|
||||||
changing.append(QPersistentModelIndex(model->index(i, 0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ModelTest::layoutChanged()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < changing.count(); ++i) {
|
|
||||||
QPersistentModelIndex p = changing[i];
|
|
||||||
Q_ASSERT(p == model->index(p.row(), p.column(), p.parent()));
|
|
||||||
}
|
|
||||||
changing.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Store what is about to be inserted to make sure it actually happens
|
|
||||||
|
|
||||||
\sa rowsRemoved()
|
|
||||||
*/
|
|
||||||
void ModelTest::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
|
|
||||||
{
|
|
||||||
Changing c;
|
|
||||||
c.parent = parent;
|
|
||||||
c.oldSize = model->rowCount(parent);
|
|
||||||
c.last = model->data(model->index(start - 1, 0, parent));
|
|
||||||
c.next = model->data(model->index(end + 1, 0, parent));
|
|
||||||
remove.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Confirm that what was said was going to happen actually did
|
|
||||||
|
|
||||||
\sa rowsAboutToBeRemoved()
|
|
||||||
*/
|
|
||||||
void ModelTest::rowsRemoved(const QModelIndex & parent, int start, int end)
|
|
||||||
{
|
|
||||||
Changing c = remove.pop();
|
|
||||||
Q_ASSERT(c.parent == parent);
|
|
||||||
Q_ASSERT(c.oldSize - (end - start + 1) == model->rowCount(parent));
|
|
||||||
Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent)));
|
|
||||||
Q_ASSERT(c.next == model->data(model->index(start, 0, c.parent)));
|
|
||||||
}
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
**
|
|
||||||
** Copyright (C) 2007 Trolltech ASA. All rights reserved.
|
|
||||||
**
|
|
||||||
** This file is part of the Qt Concurrent project on Trolltech Labs.
|
|
||||||
**
|
|
||||||
** 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.
|
|
||||||
**
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#ifndef MODELTEST_H
|
|
||||||
#define MODELTEST_H
|
|
||||||
|
|
||||||
#include <QtCore/QObject>
|
|
||||||
#include <QtCore/QAbstractItemModel>
|
|
||||||
#include <QtCore/QStack>
|
|
||||||
|
|
||||||
class ModelTest : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
ModelTest(QAbstractItemModel *model, QObject *parent = 0);
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void nonDestructiveBasicTest();
|
|
||||||
void rowCount();
|
|
||||||
void columnCount();
|
|
||||||
void hasIndex();
|
|
||||||
void index();
|
|
||||||
void parent();
|
|
||||||
void data();
|
|
||||||
|
|
||||||
protected Q_SLOTS:
|
|
||||||
void runAllTests();
|
|
||||||
void layoutAboutToBeChanged();
|
|
||||||
void layoutChanged();
|
|
||||||
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
|
|
||||||
void rowsInserted(const QModelIndex & parent, int start, int end);
|
|
||||||
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
|
|
||||||
void rowsRemoved(const QModelIndex & parent, int start, int end);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void checkChildren(const QModelIndex &parent, int currentDepth = 0);
|
|
||||||
|
|
||||||
QAbstractItemModel *model;
|
|
||||||
|
|
||||||
struct Changing
|
|
||||||
{
|
|
||||||
QModelIndex parent;
|
|
||||||
int oldSize;
|
|
||||||
QVariant last;
|
|
||||||
QVariant next;
|
|
||||||
};
|
|
||||||
QStack<Changing> insert;
|
|
||||||
QStack<Changing> remove;
|
|
||||||
|
|
||||||
bool fetchingMore;
|
|
||||||
|
|
||||||
QList<QPersistentModelIndex> changing;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,4 +0,0 @@
|
|||||||
INCLUDEPATH += $$PWD
|
|
||||||
DEPENDPATH += $$PWD
|
|
||||||
SOURCES += $$PWD/modeltest.cpp
|
|
||||||
HEADERS += $$PWD/modeltest.h
|
|
@ -116,4 +116,6 @@ include(../install.pri)
|
|||||||
include(../version.pri)
|
include(../version.pri)
|
||||||
|
|
||||||
# TODO(LOW): Test only
|
# TODO(LOW): Test only
|
||||||
CONFIG(debug, debug|release):include(modeltest.pri)
|
INCLUDEPATH += "../extra/modeltest"
|
||||||
|
CONFIG(debug, debug|release): LIBS += -L"../extra/modeltest/$(OBJECTS_DIR)/" -lmodeltest
|
||||||
|
CONFIG(debug, debug|release): QT += testlib
|
||||||
|
@ -21,8 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
|||||||
|
|
||||||
#include "params.h"
|
#include "params.h"
|
||||||
|
|
||||||
// TODO(LOW): Remove
|
#include "modeltest.h"
|
||||||
#include <modeltest.h>
|
|
||||||
|
|
||||||
PortGroupList::PortGroupList()
|
PortGroupList::PortGroupList()
|
||||||
: mPortGroupListModel(this),
|
: mPortGroupListModel(this),
|
||||||
|
@ -25,7 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
|||||||
#include "port.h"
|
#include "port.h"
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
#include "packetmodel.h"
|
#include "packetmodel.h"
|
||||||
#include "modeltest.h"
|
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QProgressDialog>
|
#include <QProgressDialog>
|
||||||
@ -42,6 +41,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class AbstractProtocolConfigForm;
|
class AbstractProtocolConfigForm;
|
||||||
|
class ModelTest;
|
||||||
|
|
||||||
class StreamConfigDialog : public QDialog, public Ui::StreamConfigDialog
|
class StreamConfigDialog : public QDialog, public Ui::StreamConfigDialog
|
||||||
{
|
{
|
||||||
|
@ -69,10 +69,10 @@ public:
|
|||||||
dynamic_cast<ComboProtocol<protoNumber, ProtoA, ProtoB>*>(proto);
|
dynamic_cast<ComboProtocol<protoNumber, ProtoA, ProtoB>*>(proto);
|
||||||
|
|
||||||
Q_ASSERT_X(comboProto != NULL,
|
Q_ASSERT_X(comboProto != NULL,
|
||||||
QString("ComboProtocolConfigForm{%1}::loadWidget()")
|
qPrintable(tr("ComboProtocolConfigForm{%1}::loadWidget()")
|
||||||
.arg(protoNumber).toAscii().constData(),
|
.arg(protoNumber)),
|
||||||
QString("Protocol{%1} is not a instance of ComboProtocol")
|
qPrintable(tr("Protocol{%1} is not a instance of ComboProtocol")
|
||||||
.arg(proto->protocolNumber()).toAscii().constData());
|
.arg(proto->protocolNumber())));
|
||||||
|
|
||||||
formA->loadWidget(comboProto->protoA);
|
formA->loadWidget(comboProto->protoA);
|
||||||
formB->loadWidget(comboProto->protoB);
|
formB->loadWidget(comboProto->protoB);
|
||||||
@ -83,10 +83,10 @@ public:
|
|||||||
dynamic_cast<ComboProtocol<protoNumber, ProtoA, ProtoB>*>(proto);
|
dynamic_cast<ComboProtocol<protoNumber, ProtoA, ProtoB>*>(proto);
|
||||||
|
|
||||||
Q_ASSERT_X(comboProto != NULL,
|
Q_ASSERT_X(comboProto != NULL,
|
||||||
QString("ComboProtocolConfigForm{%1}::loadWidget()")
|
qPrintable(tr("ComboProtocolConfigForm{%1}::loadWidget()")
|
||||||
.arg(protoNumber).toAscii().constData(),
|
.arg(protoNumber)),
|
||||||
QString("Protocol{%1} is not a instance of ComboProtocol")
|
qPrintable(tr("Protocol{%1} is not a instance of ComboProtocol")
|
||||||
.arg(proto->protocolNumber()).toAscii().constData());
|
.arg(proto->protocolNumber())));
|
||||||
|
|
||||||
formA->storeWidget(comboProto->protoA);
|
formA->storeWidget(comboProto->protoA);
|
||||||
formB->storeWidget(comboProto->protoB);
|
formB->storeWidget(comboProto->protoB);
|
||||||
|
@ -38,7 +38,7 @@ public:
|
|||||||
QValidator::State state;
|
QValidator::State state;
|
||||||
QHostAddress addr(input);
|
QHostAddress addr(input);
|
||||||
|
|
||||||
//qDebug("%s: %s (%d)", __FUNCTION__, input.toAscii().constData(), pos);
|
//qDebug("%s: %s (%d)", __FUNCTION__, qPrintable(input), pos);
|
||||||
|
|
||||||
if (addr.protocol() == QAbstractSocket::IPv6Protocol)
|
if (addr.protocol() == QAbstractSocket::IPv6Protocol)
|
||||||
state = Acceptable;
|
state = Acceptable;
|
||||||
@ -48,7 +48,7 @@ public:
|
|||||||
else
|
else
|
||||||
state = Invalid;
|
state = Invalid;
|
||||||
//qDebug("%s(%d): %s (%d), ", __FUNCTION__, state,
|
//qDebug("%s(%d): %s (%d), ", __FUNCTION__, state,
|
||||||
//input.toAscii().constData(), pos);
|
//qPrintable(input), pos);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
virtual void fixup(QString &input) const
|
virtual void fixup(QString &input) const
|
||||||
@ -57,7 +57,7 @@ public:
|
|||||||
QHostAddress addr(input);
|
QHostAddress addr(input);
|
||||||
int len = input.size();
|
int len = input.size();
|
||||||
|
|
||||||
//qDebug("%s: %s", __FUNCTION__, input.toAscii().constData());
|
//qDebug("%s: %s", __FUNCTION__, qPrintable(input));
|
||||||
|
|
||||||
while (addr.protocol() != QAbstractSocket::IPv6Protocol)
|
while (addr.protocol() != QAbstractSocket::IPv6Protocol)
|
||||||
{
|
{
|
||||||
|
@ -203,8 +203,8 @@ AbstractProtocol* ProtocolManager::createProtocol(int protoNumber,
|
|||||||
|
|
||||||
Q_ASSERT_X(pc != NULL,
|
Q_ASSERT_X(pc != NULL,
|
||||||
__FUNCTION__,
|
__FUNCTION__,
|
||||||
QString("No Protocol Creator registered for protocol %1")
|
qPrintable(QString("No Protocol Creator registered for protocol %1")
|
||||||
.arg(protoNumber).toAscii().constData());
|
.arg(protoNumber)));
|
||||||
|
|
||||||
p = (*pc)(stream, parent);
|
p = (*pc)(stream, parent);
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ AbstractProtocolConfigForm* ProtocolWidgetFactory::createConfigWidget(
|
|||||||
|
|
||||||
Q_ASSERT_X(pc != NULL,
|
Q_ASSERT_X(pc != NULL,
|
||||||
__FUNCTION__,
|
__FUNCTION__,
|
||||||
QString(protoNumber).toAscii().constData());
|
qPrintable(QString(protoNumber)));
|
||||||
|
|
||||||
p = (*pc)();
|
p = (*pc)();
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
TEMPLATE = subdirs
|
TEMPLATE = subdirs
|
||||||
SUBDIRS = \
|
SUBDIRS = \
|
||||||
|
modeltest \
|
||||||
qhexedit2
|
qhexedit2
|
||||||
|
390
extra/modeltest/dynamictreemodel.cpp
Normal file
390
extra/modeltest/dynamictreemodel.cpp
Normal file
@ -0,0 +1,390 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** 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 The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "dynamictreemodel.h"
|
||||||
|
|
||||||
|
#include <QtCore/QHash>
|
||||||
|
#include <QtCore/QList>
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
#include <QtCore/QDebug>
|
||||||
|
|
||||||
|
|
||||||
|
DynamicTreeModel::DynamicTreeModel(QObject *parent)
|
||||||
|
: QAbstractItemModel(parent),
|
||||||
|
nextId(1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
// if (column != 0)
|
||||||
|
// return QModelIndex();
|
||||||
|
|
||||||
|
|
||||||
|
if ( column < 0 || row < 0 )
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId());
|
||||||
|
|
||||||
|
const qint64 grandParent = findParentId(parent.internalId());
|
||||||
|
if (grandParent >= 0) {
|
||||||
|
QList<QList<qint64> > parentTable = m_childItems.value(grandParent);
|
||||||
|
if (parent.column() >= parentTable.size())
|
||||||
|
qFatal("%s: parent.column() must be less than parentTable.size()", Q_FUNC_INFO);
|
||||||
|
QList<qint64> parentSiblings = parentTable.at(parent.column());
|
||||||
|
if (parent.row() >= parentSiblings.size())
|
||||||
|
qFatal("%s: parent.row() must be less than parentSiblings.size()", Q_FUNC_INFO);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (childIdColumns.size() == 0)
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
if (column >= childIdColumns.size())
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
QList<qint64> rowIds = childIdColumns.at(column);
|
||||||
|
|
||||||
|
if ( row >= rowIds.size())
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
qint64 id = rowIds.at(row);
|
||||||
|
|
||||||
|
return createIndex(row, column, reinterpret_cast<void *>(id));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
qint64 DynamicTreeModel::findParentId(qint64 searchId) const
|
||||||
|
{
|
||||||
|
if (searchId <= 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
QHashIterator<qint64, QList<QList<qint64> > > i(m_childItems);
|
||||||
|
while (i.hasNext())
|
||||||
|
{
|
||||||
|
i.next();
|
||||||
|
QListIterator<QList<qint64> > j(i.value());
|
||||||
|
while (j.hasNext())
|
||||||
|
{
|
||||||
|
QList<qint64> l = j.next();
|
||||||
|
if (l.contains(searchId))
|
||||||
|
{
|
||||||
|
return i.key();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
if (!index.isValid())
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
qint64 searchId = index.internalId();
|
||||||
|
qint64 parentId = findParentId(searchId);
|
||||||
|
// Will never happen for valid index, but what the hey...
|
||||||
|
if (parentId <= 0)
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
qint64 grandParentId = findParentId(parentId);
|
||||||
|
if (grandParentId < 0)
|
||||||
|
grandParentId = 0;
|
||||||
|
|
||||||
|
int column = 0;
|
||||||
|
QList<qint64> childList = m_childItems.value(grandParentId).at(column);
|
||||||
|
|
||||||
|
int row = childList.indexOf(parentId);
|
||||||
|
|
||||||
|
return createIndex(row, column, reinterpret_cast<void *>(parentId));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int DynamicTreeModel::rowCount(const QModelIndex &index ) const
|
||||||
|
{
|
||||||
|
QList<QList<qint64> > cols = m_childItems.value(index.internalId());
|
||||||
|
|
||||||
|
if (cols.size() == 0 )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (index.column() > 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return cols.at(0).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
int DynamicTreeModel::columnCount(const QModelIndex &index ) const
|
||||||
|
{
|
||||||
|
// Q_UNUSED(index);
|
||||||
|
return m_childItems.value(index.internalId()).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
if (!index.isValid())
|
||||||
|
return QVariant();
|
||||||
|
|
||||||
|
if (Qt::DisplayRole == role)
|
||||||
|
{
|
||||||
|
return m_items.value(index.internalId());
|
||||||
|
}
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicTreeModel::clear()
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
m_items.clear();
|
||||||
|
m_childItems.clear();
|
||||||
|
nextId = 1;
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent )
|
||||||
|
: QObject(parent), m_model(model), m_numCols(1), m_startRow(-1), m_endRow(-1)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex ModelChangeCommand::findIndex(QList<int> rows)
|
||||||
|
{
|
||||||
|
const int col = 0;
|
||||||
|
QModelIndex parent = QModelIndex();
|
||||||
|
QListIterator<int> i(rows);
|
||||||
|
while (i.hasNext())
|
||||||
|
{
|
||||||
|
parent = m_model->index(i.next(), col, parent);
|
||||||
|
if (!parent.isValid())
|
||||||
|
qFatal("%s: parent must be valid", Q_FUNC_INFO);
|
||||||
|
}
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent )
|
||||||
|
: ModelChangeCommand(model, parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelInsertCommand::doCommand()
|
||||||
|
{
|
||||||
|
QModelIndex parent = findIndex(m_rowNumbers);
|
||||||
|
m_model->beginInsertRows(parent, m_startRow, m_endRow);
|
||||||
|
qint64 parentId = parent.internalId();
|
||||||
|
for (int row = m_startRow; row <= m_endRow; row++)
|
||||||
|
{
|
||||||
|
for(int col = 0; col < m_numCols; col++ )
|
||||||
|
{
|
||||||
|
if (m_model->m_childItems[parentId].size() <= col)
|
||||||
|
{
|
||||||
|
m_model->m_childItems[parentId].append(QList<qint64>());
|
||||||
|
}
|
||||||
|
// QString name = QUuid::createUuid().toString();
|
||||||
|
qint64 id = m_model->newId();
|
||||||
|
QString name = QString::number(id);
|
||||||
|
|
||||||
|
m_model->m_items.insert(id, name);
|
||||||
|
m_model->m_childItems[parentId][col].insert(row, id);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_model->endInsertRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent)
|
||||||
|
: ModelChangeCommand(model, parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
bool ModelMoveCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
|
||||||
|
{
|
||||||
|
return m_model->beginMoveRows(srcParent, srcStart, srcEnd, destParent, destRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelMoveCommand::doCommand()
|
||||||
|
{
|
||||||
|
QModelIndex srcParent = findIndex(m_rowNumbers);
|
||||||
|
QModelIndex destParent = findIndex(m_destRowNumbers);
|
||||||
|
|
||||||
|
if (!emitPreSignal(srcParent, m_startRow, m_endRow, destParent, m_destRow))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int column = 0; column < m_numCols; ++column)
|
||||||
|
{
|
||||||
|
QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[column].mid(m_startRow, m_endRow - m_startRow + 1 );
|
||||||
|
|
||||||
|
for (int i = m_startRow; i <= m_endRow ; i++)
|
||||||
|
{
|
||||||
|
m_model->m_childItems[srcParent.internalId()][column].removeAt(m_startRow);
|
||||||
|
}
|
||||||
|
int d;
|
||||||
|
if (m_destRow < m_startRow)
|
||||||
|
d = m_destRow;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (srcParent == destParent)
|
||||||
|
d = m_destRow - (m_endRow - m_startRow + 1);
|
||||||
|
else
|
||||||
|
d = m_destRow - (m_endRow - m_startRow) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(const qint64 id, l)
|
||||||
|
{
|
||||||
|
m_model->m_childItems[destParent.internalId()][column].insert(d++, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emitPostSignal();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelMoveCommand::emitPostSignal()
|
||||||
|
{
|
||||||
|
m_model->endMoveRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelResetCommand::ModelResetCommand(DynamicTreeModel* model, QObject* parent)
|
||||||
|
: ModelMoveCommand(model, parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelResetCommand::~ModelResetCommand()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ModelResetCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
|
||||||
|
{
|
||||||
|
Q_UNUSED(srcParent);
|
||||||
|
Q_UNUSED(srcStart);
|
||||||
|
Q_UNUSED(srcEnd);
|
||||||
|
Q_UNUSED(destParent);
|
||||||
|
Q_UNUSED(destRow);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelResetCommand::emitPostSignal()
|
||||||
|
{
|
||||||
|
m_model->beginResetModel();
|
||||||
|
m_model->endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelResetCommandFixed::ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent)
|
||||||
|
: ModelMoveCommand(model, parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelResetCommandFixed::~ModelResetCommandFixed()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ModelResetCommandFixed::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
|
||||||
|
{
|
||||||
|
Q_UNUSED(srcParent);
|
||||||
|
Q_UNUSED(srcStart);
|
||||||
|
Q_UNUSED(srcEnd);
|
||||||
|
Q_UNUSED(destParent);
|
||||||
|
Q_UNUSED(destRow);
|
||||||
|
|
||||||
|
m_model->beginResetModel();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelResetCommandFixed::emitPostSignal()
|
||||||
|
{
|
||||||
|
m_model->endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelChangeChildrenLayoutsCommand::ModelChangeChildrenLayoutsCommand(DynamicTreeModel* model, QObject* parent)
|
||||||
|
: ModelChangeCommand(model, parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelChangeChildrenLayoutsCommand::doCommand()
|
||||||
|
{
|
||||||
|
const QPersistentModelIndex parent1 = findIndex(m_rowNumbers);
|
||||||
|
const QPersistentModelIndex parent2 = findIndex(m_secondRowNumbers);
|
||||||
|
|
||||||
|
QList<QPersistentModelIndex> parents;
|
||||||
|
parents << parent1;
|
||||||
|
parents << parent2;
|
||||||
|
|
||||||
|
emit m_model->layoutAboutToBeChanged(parents);
|
||||||
|
|
||||||
|
int rowSize1 = -1;
|
||||||
|
int rowSize2 = -1;
|
||||||
|
|
||||||
|
for (int column = 0; column < m_numCols; ++column)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
QList<qint64> &l = m_model->m_childItems[parent1.internalId()][column];
|
||||||
|
rowSize1 = l.size();
|
||||||
|
l.prepend(l.takeLast());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
QList<qint64> &l = m_model->m_childItems[parent2.internalId()][column];
|
||||||
|
rowSize2 = l.size();
|
||||||
|
l.append(l.takeFirst());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're changing one of the parent indexes, we need to ensure that we do that before
|
||||||
|
// changing any children of that parent. The reason is that we're keeping parent1 and parent2
|
||||||
|
// around as QPersistentModelIndex instances, and we query idx.parent() in the loop.
|
||||||
|
QModelIndexList persistent = m_model->persistentIndexList();
|
||||||
|
foreach (const QModelIndex &parent, parents) {
|
||||||
|
int idx = persistent.indexOf(parent);
|
||||||
|
if (idx != -1)
|
||||||
|
persistent.move(idx, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (const QModelIndex &idx, persistent) {
|
||||||
|
if (idx.parent() == parent1) {
|
||||||
|
if (idx.row() == rowSize1 - 1) {
|
||||||
|
m_model->changePersistentIndex(idx, m_model->createIndex(0, idx.column(), idx.internalPointer()));
|
||||||
|
} else {
|
||||||
|
m_model->changePersistentIndex(idx, m_model->createIndex(idx.row() + 1, idx.column(), idx.internalPointer()));
|
||||||
|
}
|
||||||
|
} else if (idx.parent() == parent2) {
|
||||||
|
if (idx.row() == 0) {
|
||||||
|
m_model->changePersistentIndex(idx, m_model->createIndex(rowSize2 - 1, idx.column(), idx.internalPointer()));
|
||||||
|
} else {
|
||||||
|
m_model->changePersistentIndex(idx, m_model->createIndex(idx.row() - 1, idx.column(), idx.internalPointer()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emit m_model->layoutChanged(parents);
|
||||||
|
}
|
201
extra/modeltest/dynamictreemodel.h
Normal file
201
extra/modeltest/dynamictreemodel.h
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** 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 The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef DYNAMICTREEMODEL_H
|
||||||
|
#define DYNAMICTREEMODEL_H
|
||||||
|
|
||||||
|
#include <QtCore/QAbstractItemModel>
|
||||||
|
|
||||||
|
#include <QtCore/QHash>
|
||||||
|
#include <QtCore/QList>
|
||||||
|
|
||||||
|
|
||||||
|
class DynamicTreeModel : public QAbstractItemModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
DynamicTreeModel(QObject *parent = 0);
|
||||||
|
|
||||||
|
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
QModelIndex parent(const QModelIndex &index) const;
|
||||||
|
int rowCount(const QModelIndex &index = QModelIndex()) const;
|
||||||
|
int columnCount(const QModelIndex &index = QModelIndex()) const;
|
||||||
|
|
||||||
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
|
||||||
|
/**
|
||||||
|
Finds the parent id of the string with id @p searchId.
|
||||||
|
|
||||||
|
Returns -1 if not found.
|
||||||
|
*/
|
||||||
|
qint64 findParentId(qint64 searchId) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QHash<qint64, QString> m_items;
|
||||||
|
QHash<qint64, QList<QList<qint64> > > m_childItems;
|
||||||
|
qint64 nextId;
|
||||||
|
qint64 newId() { return nextId++; };
|
||||||
|
|
||||||
|
QModelIndex m_nextParentIndex;
|
||||||
|
int m_nextRow;
|
||||||
|
|
||||||
|
int m_depth;
|
||||||
|
int maxDepth;
|
||||||
|
|
||||||
|
friend class ModelInsertCommand;
|
||||||
|
friend class ModelMoveCommand;
|
||||||
|
friend class ModelResetCommand;
|
||||||
|
friend class ModelResetCommandFixed;
|
||||||
|
friend class ModelChangeChildrenLayoutsCommand;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class ModelChangeCommand : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
|
||||||
|
ModelChangeCommand( DynamicTreeModel *model, QObject *parent = 0 );
|
||||||
|
|
||||||
|
virtual ~ModelChangeCommand() {}
|
||||||
|
|
||||||
|
void setAncestorRowNumbers(QList<int> rowNumbers) { m_rowNumbers = rowNumbers; }
|
||||||
|
|
||||||
|
QModelIndex findIndex(QList<int> rows);
|
||||||
|
|
||||||
|
void setStartRow(int row) { m_startRow = row; }
|
||||||
|
|
||||||
|
void setEndRow(int row) { m_endRow = row; }
|
||||||
|
|
||||||
|
void setNumCols(int cols) { m_numCols = cols; }
|
||||||
|
|
||||||
|
virtual void doCommand() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
DynamicTreeModel* m_model;
|
||||||
|
QList<int> m_rowNumbers;
|
||||||
|
int m_numCols;
|
||||||
|
int m_startRow;
|
||||||
|
int m_endRow;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef QList<ModelChangeCommand*> ModelChangeCommandList;
|
||||||
|
|
||||||
|
class ModelInsertCommand : public ModelChangeCommand
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
ModelInsertCommand(DynamicTreeModel *model, QObject *parent = 0 );
|
||||||
|
virtual ~ModelInsertCommand() {}
|
||||||
|
|
||||||
|
virtual void doCommand();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class ModelMoveCommand : public ModelChangeCommand
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ModelMoveCommand(DynamicTreeModel *model, QObject *parent);
|
||||||
|
|
||||||
|
virtual ~ModelMoveCommand() {}
|
||||||
|
|
||||||
|
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
|
||||||
|
|
||||||
|
virtual void doCommand();
|
||||||
|
|
||||||
|
virtual void emitPostSignal();
|
||||||
|
|
||||||
|
void setDestAncestors( QList<int> rows ) { m_destRowNumbers = rows; }
|
||||||
|
|
||||||
|
void setDestRow(int row) { m_destRow = row; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QList<int> m_destRowNumbers;
|
||||||
|
int m_destRow;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
A command which does a move and emits a reset signal.
|
||||||
|
*/
|
||||||
|
class ModelResetCommand : public ModelMoveCommand
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ModelResetCommand(DynamicTreeModel* model, QObject* parent = 0);
|
||||||
|
|
||||||
|
virtual ~ModelResetCommand();
|
||||||
|
|
||||||
|
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
|
||||||
|
virtual void emitPostSignal();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
A command which does a move and emits a beginResetModel and endResetModel signals.
|
||||||
|
*/
|
||||||
|
class ModelResetCommandFixed : public ModelMoveCommand
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent = 0);
|
||||||
|
|
||||||
|
virtual ~ModelResetCommandFixed();
|
||||||
|
|
||||||
|
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
|
||||||
|
virtual void emitPostSignal();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class ModelChangeChildrenLayoutsCommand : public ModelChangeCommand
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ModelChangeChildrenLayoutsCommand(DynamicTreeModel *model, QObject *parent);
|
||||||
|
|
||||||
|
virtual ~ModelChangeChildrenLayoutsCommand() {}
|
||||||
|
|
||||||
|
virtual void doCommand();
|
||||||
|
|
||||||
|
void setSecondAncestorRowNumbers( QList<int> rows ) { m_secondRowNumbers = rows; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QList<int> m_secondRowNumbers;
|
||||||
|
int m_destRow;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
585
extra/modeltest/modeltest.cpp
Normal file
585
extra/modeltest/modeltest.cpp
Normal file
@ -0,0 +1,585 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** 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 The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "modeltest.h"
|
||||||
|
|
||||||
|
#include <QtCore/QtCore>
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Connect to all of the models signals. Whenever anything happens recheck everything.
|
||||||
|
*/
|
||||||
|
ModelTest::ModelTest ( QAbstractItemModel *_model, QObject *parent ) : QObject ( parent ), model ( _model ), fetchingMore ( false )
|
||||||
|
{
|
||||||
|
if (!model)
|
||||||
|
qFatal("%s: model must not be null", Q_FUNC_INFO);
|
||||||
|
|
||||||
|
connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(layoutChanged()), this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(modelReset()), this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
||||||
|
this, SLOT(runAllTests()) );
|
||||||
|
|
||||||
|
// Special checks for changes
|
||||||
|
connect(model, SIGNAL(layoutAboutToBeChanged()),
|
||||||
|
this, SLOT(layoutAboutToBeChanged()) );
|
||||||
|
connect(model, SIGNAL(layoutChanged()),
|
||||||
|
this, SLOT(layoutChanged()) );
|
||||||
|
|
||||||
|
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
|
||||||
|
this, SLOT(rowsAboutToBeInserted(QModelIndex,int,int)) );
|
||||||
|
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
|
||||||
|
this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)) );
|
||||||
|
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
||||||
|
this, SLOT(rowsInserted(QModelIndex,int,int)) );
|
||||||
|
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
||||||
|
this, SLOT(rowsRemoved(QModelIndex,int,int)) );
|
||||||
|
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
||||||
|
this, SLOT(dataChanged(QModelIndex,QModelIndex)) );
|
||||||
|
connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
|
||||||
|
this, SLOT(headerDataChanged(Qt::Orientation,int,int)) );
|
||||||
|
|
||||||
|
runAllTests();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelTest::runAllTests()
|
||||||
|
{
|
||||||
|
if ( fetchingMore )
|
||||||
|
return;
|
||||||
|
nonDestructiveBasicTest();
|
||||||
|
rowCount();
|
||||||
|
columnCount();
|
||||||
|
hasIndex();
|
||||||
|
index();
|
||||||
|
parent();
|
||||||
|
data();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
nonDestructiveBasicTest tries to call a number of the basic functions (not all)
|
||||||
|
to make sure the model doesn't outright segfault, testing the functions that makes sense.
|
||||||
|
*/
|
||||||
|
void ModelTest::nonDestructiveBasicTest()
|
||||||
|
{
|
||||||
|
QVERIFY(!model->buddy(QModelIndex()).isValid());
|
||||||
|
model->canFetchMore ( QModelIndex() );
|
||||||
|
QVERIFY( model->columnCount ( QModelIndex() ) >= 0 );
|
||||||
|
QCOMPARE(model->data(QModelIndex()), QVariant());
|
||||||
|
fetchingMore = true;
|
||||||
|
model->fetchMore ( QModelIndex() );
|
||||||
|
fetchingMore = false;
|
||||||
|
Qt::ItemFlags flags = model->flags ( QModelIndex() );
|
||||||
|
QVERIFY( flags == Qt::ItemIsDropEnabled || flags == 0 );
|
||||||
|
model->hasChildren ( QModelIndex() );
|
||||||
|
model->hasIndex ( 0, 0 );
|
||||||
|
model->headerData ( 0, Qt::Horizontal );
|
||||||
|
model->index ( 0, 0 );
|
||||||
|
model->itemData ( QModelIndex() );
|
||||||
|
QVariant cache;
|
||||||
|
model->match ( QModelIndex(), -1, cache );
|
||||||
|
model->mimeTypes();
|
||||||
|
QVERIFY(!model->parent(QModelIndex()).isValid());
|
||||||
|
QVERIFY( model->rowCount() >= 0 );
|
||||||
|
QVariant variant;
|
||||||
|
model->setData ( QModelIndex(), variant, -1 );
|
||||||
|
model->setHeaderData ( -1, Qt::Horizontal, QVariant() );
|
||||||
|
model->setHeaderData ( 999999, Qt::Horizontal, QVariant() );
|
||||||
|
QMap<int, QVariant> roles;
|
||||||
|
model->sibling ( 0, 0, QModelIndex() );
|
||||||
|
model->span ( QModelIndex() );
|
||||||
|
model->supportedDropActions();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Tests model's implementation of QAbstractItemModel::rowCount() and hasChildren()
|
||||||
|
|
||||||
|
Models that are dynamically populated are not as fully tested here.
|
||||||
|
*/
|
||||||
|
void ModelTest::rowCount()
|
||||||
|
{
|
||||||
|
// qDebug() << "rc";
|
||||||
|
// check top row
|
||||||
|
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
|
||||||
|
int rows = model->rowCount ( topIndex );
|
||||||
|
QVERIFY( rows >= 0 );
|
||||||
|
if ( rows > 0 )
|
||||||
|
QVERIFY( model->hasChildren ( topIndex ) );
|
||||||
|
|
||||||
|
QModelIndex secondLevelIndex = model->index ( 0, 0, topIndex );
|
||||||
|
if ( secondLevelIndex.isValid() ) { // not the top level
|
||||||
|
// check a row count where parent is valid
|
||||||
|
rows = model->rowCount ( secondLevelIndex );
|
||||||
|
QVERIFY( rows >= 0 );
|
||||||
|
if ( rows > 0 )
|
||||||
|
QVERIFY( model->hasChildren ( secondLevelIndex ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// The models rowCount() is tested more extensively in checkChildren(),
|
||||||
|
// but this catches the big mistakes
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Tests model's implementation of QAbstractItemModel::columnCount() and hasChildren()
|
||||||
|
*/
|
||||||
|
void ModelTest::columnCount()
|
||||||
|
{
|
||||||
|
// check top row
|
||||||
|
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
|
||||||
|
QVERIFY( model->columnCount ( topIndex ) >= 0 );
|
||||||
|
|
||||||
|
// check a column count where parent is valid
|
||||||
|
QModelIndex childIndex = model->index ( 0, 0, topIndex );
|
||||||
|
if ( childIndex.isValid() )
|
||||||
|
QVERIFY( model->columnCount ( childIndex ) >= 0 );
|
||||||
|
|
||||||
|
// columnCount() is tested more extensively in checkChildren(),
|
||||||
|
// but this catches the big mistakes
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Tests model's implementation of QAbstractItemModel::hasIndex()
|
||||||
|
*/
|
||||||
|
void ModelTest::hasIndex()
|
||||||
|
{
|
||||||
|
// qDebug() << "hi";
|
||||||
|
// Make sure that invalid values returns an invalid index
|
||||||
|
QVERIFY( !model->hasIndex ( -2, -2 ) );
|
||||||
|
QVERIFY( !model->hasIndex ( -2, 0 ) );
|
||||||
|
QVERIFY( !model->hasIndex ( 0, -2 ) );
|
||||||
|
|
||||||
|
int rows = model->rowCount();
|
||||||
|
int columns = model->columnCount();
|
||||||
|
|
||||||
|
// check out of bounds
|
||||||
|
QVERIFY( !model->hasIndex ( rows, columns ) );
|
||||||
|
QVERIFY( !model->hasIndex ( rows + 1, columns + 1 ) );
|
||||||
|
|
||||||
|
if ( rows > 0 )
|
||||||
|
QVERIFY( model->hasIndex ( 0, 0 ) );
|
||||||
|
|
||||||
|
// hasIndex() is tested more extensively in checkChildren(),
|
||||||
|
// but this catches the big mistakes
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Tests model's implementation of QAbstractItemModel::index()
|
||||||
|
*/
|
||||||
|
void ModelTest::index()
|
||||||
|
{
|
||||||
|
// qDebug() << "i";
|
||||||
|
// Make sure that invalid values returns an invalid index
|
||||||
|
QVERIFY(!model->index(-2, -2).isValid());
|
||||||
|
QVERIFY(!model->index(-2, 0).isValid());
|
||||||
|
QVERIFY(!model->index(0, -2).isValid());
|
||||||
|
|
||||||
|
int rows = model->rowCount();
|
||||||
|
int columns = model->columnCount();
|
||||||
|
|
||||||
|
if ( rows == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Catch off by one errors
|
||||||
|
QVERIFY(!model->index(rows, columns).isValid());
|
||||||
|
QVERIFY(model->index(0, 0).isValid());
|
||||||
|
|
||||||
|
// Make sure that the same index is *always* returned
|
||||||
|
QModelIndex a = model->index ( 0, 0 );
|
||||||
|
QModelIndex b = model->index ( 0, 0 );
|
||||||
|
QCOMPARE(a, b);
|
||||||
|
|
||||||
|
// index() is tested more extensively in checkChildren(),
|
||||||
|
// but this catches the big mistakes
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Tests model's implementation of QAbstractItemModel::parent()
|
||||||
|
*/
|
||||||
|
void ModelTest::parent()
|
||||||
|
{
|
||||||
|
// qDebug() << "p";
|
||||||
|
// Make sure the model won't crash and will return an invalid QModelIndex
|
||||||
|
// when asked for the parent of an invalid index.
|
||||||
|
QVERIFY(!model->parent(QModelIndex()).isValid());
|
||||||
|
|
||||||
|
if ( model->rowCount() == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Column 0 | Column 1 |
|
||||||
|
// QModelIndex() | |
|
||||||
|
// \- topIndex | topIndex1 |
|
||||||
|
// \- childIndex | childIndex1 |
|
||||||
|
|
||||||
|
// Common error test #1, make sure that a top level index has a parent
|
||||||
|
// that is a invalid QModelIndex.
|
||||||
|
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
|
||||||
|
QVERIFY(!model->parent(topIndex).isValid());
|
||||||
|
|
||||||
|
// Common error test #2, make sure that a second level index has a parent
|
||||||
|
// that is the first level index.
|
||||||
|
if ( model->rowCount ( topIndex ) > 0 ) {
|
||||||
|
QModelIndex childIndex = model->index ( 0, 0, topIndex );
|
||||||
|
QCOMPARE(model->parent(childIndex), topIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Common error test #3, the second column should NOT have the same children
|
||||||
|
// as the first column in a row.
|
||||||
|
// Usually the second column shouldn't have children.
|
||||||
|
QModelIndex topIndex1 = model->index ( 0, 1, QModelIndex() );
|
||||||
|
if ( model->rowCount ( topIndex1 ) > 0 ) {
|
||||||
|
QModelIndex childIndex = model->index ( 0, 0, topIndex );
|
||||||
|
QModelIndex childIndex1 = model->index ( 0, 0, topIndex1 );
|
||||||
|
QVERIFY( childIndex != childIndex1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full test, walk n levels deep through the model making sure that all
|
||||||
|
// parent's children correctly specify their parent.
|
||||||
|
checkChildren ( QModelIndex() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Called from the parent() test.
|
||||||
|
|
||||||
|
A model that returns an index of parent X should also return X when asking
|
||||||
|
for the parent of the index.
|
||||||
|
|
||||||
|
This recursive function does pretty extensive testing on the whole model in an
|
||||||
|
effort to catch edge cases.
|
||||||
|
|
||||||
|
This function assumes that rowCount(), columnCount() and index() already work.
|
||||||
|
If they have a bug it will point it out, but the above tests should have already
|
||||||
|
found the basic bugs because it is easier to figure out the problem in
|
||||||
|
those tests then this one.
|
||||||
|
*/
|
||||||
|
void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
|
||||||
|
{
|
||||||
|
// First just try walking back up the tree.
|
||||||
|
QModelIndex p = parent;
|
||||||
|
while ( p.isValid() )
|
||||||
|
p = p.parent();
|
||||||
|
|
||||||
|
// For models that are dynamically populated
|
||||||
|
if ( model->canFetchMore ( parent ) ) {
|
||||||
|
fetchingMore = true;
|
||||||
|
model->fetchMore ( parent );
|
||||||
|
fetchingMore = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rows = model->rowCount ( parent );
|
||||||
|
int columns = model->columnCount ( parent );
|
||||||
|
|
||||||
|
if ( rows > 0 )
|
||||||
|
QVERIFY( model->hasChildren ( parent ) );
|
||||||
|
|
||||||
|
// Some further testing against rows(), columns(), and hasChildren()
|
||||||
|
QVERIFY( rows >= 0 );
|
||||||
|
QVERIFY( columns >= 0 );
|
||||||
|
if ( rows > 0 )
|
||||||
|
QVERIFY( model->hasChildren ( parent ) );
|
||||||
|
|
||||||
|
//qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows
|
||||||
|
// << "columns:" << columns << "parent column:" << parent.column();
|
||||||
|
|
||||||
|
const QModelIndex topLeftChild = model->index( 0, 0, parent );
|
||||||
|
|
||||||
|
QVERIFY( !model->hasIndex ( rows + 1, 0, parent ) );
|
||||||
|
for ( int r = 0; r < rows; ++r ) {
|
||||||
|
if ( model->canFetchMore ( parent ) ) {
|
||||||
|
fetchingMore = true;
|
||||||
|
model->fetchMore ( parent );
|
||||||
|
fetchingMore = false;
|
||||||
|
}
|
||||||
|
QVERIFY( !model->hasIndex ( r, columns + 1, parent ) );
|
||||||
|
for ( int c = 0; c < columns; ++c ) {
|
||||||
|
QVERIFY( model->hasIndex ( r, c, parent ) );
|
||||||
|
QModelIndex index = model->index ( r, c, parent );
|
||||||
|
// rowCount() and columnCount() said that it existed...
|
||||||
|
QVERIFY(index.isValid());
|
||||||
|
|
||||||
|
// index() should always return the same index when called twice in a row
|
||||||
|
QModelIndex modifiedIndex = model->index ( r, c, parent );
|
||||||
|
QCOMPARE(index, modifiedIndex);
|
||||||
|
|
||||||
|
// Make sure we get the same index if we request it twice in a row
|
||||||
|
QModelIndex a = model->index ( r, c, parent );
|
||||||
|
QModelIndex b = model->index ( r, c, parent );
|
||||||
|
QCOMPARE(a, b);
|
||||||
|
|
||||||
|
{
|
||||||
|
const QModelIndex sibling = model->sibling( r, c, topLeftChild );
|
||||||
|
QCOMPARE(index, sibling);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const QModelIndex sibling = topLeftChild.sibling( r, c );
|
||||||
|
QCOMPARE(index, sibling);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Some basic checking on the index that is returned
|
||||||
|
QCOMPARE(index.model(), model);
|
||||||
|
QCOMPARE( index.row(), r );
|
||||||
|
QCOMPARE( index.column(), c );
|
||||||
|
// While you can technically return a QVariant usually this is a sign
|
||||||
|
// of a bug in data(). Disable if this really is ok in your model.
|
||||||
|
// QVERIFY( model->data ( index, Qt::DisplayRole ).isValid() );
|
||||||
|
|
||||||
|
// If the next test fails here is some somewhat useful debug you play with.
|
||||||
|
|
||||||
|
if (model->parent(index) != parent) {
|
||||||
|
qDebug() << r << c << currentDepth << model->data(index).toString()
|
||||||
|
<< model->data(parent).toString();
|
||||||
|
qDebug() << index << parent << model->parent(index);
|
||||||
|
// And a view that you can even use to show the model.
|
||||||
|
// QTreeView view;
|
||||||
|
// view.setModel(model);
|
||||||
|
// view.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that we can get back our real parent.
|
||||||
|
QCOMPARE( model->parent ( index ), parent );
|
||||||
|
|
||||||
|
// recursively go down the children
|
||||||
|
if ( model->hasChildren ( index ) && currentDepth < 10 ) {
|
||||||
|
//qDebug() << r << c << "has children" << model->rowCount(index);
|
||||||
|
checkChildren ( index, ++currentDepth );
|
||||||
|
}/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/
|
||||||
|
|
||||||
|
// make sure that after testing the children that the index doesn't change.
|
||||||
|
QModelIndex newerIndex = model->index ( r, c, parent );
|
||||||
|
QCOMPARE(index, newerIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Tests model's implementation of QAbstractItemModel::data()
|
||||||
|
*/
|
||||||
|
void ModelTest::data()
|
||||||
|
{
|
||||||
|
// Invalid index should return an invalid qvariant
|
||||||
|
QVERIFY( !model->data ( QModelIndex() ).isValid() );
|
||||||
|
|
||||||
|
if ( model->rowCount() == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// A valid index should have a valid QVariant data
|
||||||
|
QVERIFY( model->index ( 0, 0 ).isValid() );
|
||||||
|
|
||||||
|
// shouldn't be able to set data on an invalid index
|
||||||
|
QVERIFY( !model->setData ( QModelIndex(), QLatin1String ( "foo" ), Qt::DisplayRole ) );
|
||||||
|
|
||||||
|
// General Purpose roles that should return a QString
|
||||||
|
QVariant variant = model->data ( model->index ( 0, 0 ), Qt::ToolTipRole );
|
||||||
|
if ( variant.isValid() ) {
|
||||||
|
QVERIFY( variant.canConvert<QString>() );
|
||||||
|
}
|
||||||
|
variant = model->data ( model->index ( 0, 0 ), Qt::StatusTipRole );
|
||||||
|
if ( variant.isValid() ) {
|
||||||
|
QVERIFY( variant.canConvert<QString>() );
|
||||||
|
}
|
||||||
|
variant = model->data ( model->index ( 0, 0 ), Qt::WhatsThisRole );
|
||||||
|
if ( variant.isValid() ) {
|
||||||
|
QVERIFY( variant.canConvert<QString>() );
|
||||||
|
}
|
||||||
|
|
||||||
|
// General Purpose roles that should return a QSize
|
||||||
|
variant = model->data ( model->index ( 0, 0 ), Qt::SizeHintRole );
|
||||||
|
if ( variant.isValid() ) {
|
||||||
|
QVERIFY( variant.canConvert<QSize>() );
|
||||||
|
}
|
||||||
|
|
||||||
|
// General Purpose roles that should return a QFont
|
||||||
|
QVariant fontVariant = model->data ( model->index ( 0, 0 ), Qt::FontRole );
|
||||||
|
if ( fontVariant.isValid() ) {
|
||||||
|
QVERIFY( fontVariant.canConvert<QFont>() );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the alignment is one we know about
|
||||||
|
QVariant textAlignmentVariant = model->data ( model->index ( 0, 0 ), Qt::TextAlignmentRole );
|
||||||
|
if ( textAlignmentVariant.isValid() ) {
|
||||||
|
Qt::Alignment alignment = textAlignmentVariant.value<Qt::Alignment>();
|
||||||
|
QCOMPARE( alignment, ( alignment & ( Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask ) ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// General Purpose roles that should return a QColor
|
||||||
|
QVariant colorVariant = model->data ( model->index ( 0, 0 ), Qt::BackgroundColorRole );
|
||||||
|
if ( colorVariant.isValid() ) {
|
||||||
|
QVERIFY( colorVariant.canConvert<QColor>() );
|
||||||
|
}
|
||||||
|
|
||||||
|
colorVariant = model->data ( model->index ( 0, 0 ), Qt::TextColorRole );
|
||||||
|
if ( colorVariant.isValid() ) {
|
||||||
|
QVERIFY( colorVariant.canConvert<QColor>() );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the "check state" is one we know about.
|
||||||
|
QVariant checkStateVariant = model->data ( model->index ( 0, 0 ), Qt::CheckStateRole );
|
||||||
|
if ( checkStateVariant.isValid() ) {
|
||||||
|
int state = checkStateVariant.toInt();
|
||||||
|
QVERIFY( state == Qt::Unchecked ||
|
||||||
|
state == Qt::PartiallyChecked ||
|
||||||
|
state == Qt::Checked );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Store what is about to be inserted to make sure it actually happens
|
||||||
|
|
||||||
|
\sa rowsInserted()
|
||||||
|
*/
|
||||||
|
void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int /* end */)
|
||||||
|
{
|
||||||
|
// Q_UNUSED(end);
|
||||||
|
// qDebug() << "rowsAboutToBeInserted" << "start=" << start << "end=" << end << "parent=" << model->data ( parent ).toString()
|
||||||
|
// << "current count of parent=" << model->rowCount ( parent ); // << "display of last=" << model->data( model->index(start-1, 0, parent) );
|
||||||
|
// qDebug() << model->index(start-1, 0, parent) << model->data( model->index(start-1, 0, parent) );
|
||||||
|
Changing c;
|
||||||
|
c.parent = parent;
|
||||||
|
c.oldSize = model->rowCount ( parent );
|
||||||
|
c.last = model->data ( model->index ( start - 1, 0, parent ) );
|
||||||
|
c.next = model->data ( model->index ( start, 0, parent ) );
|
||||||
|
insert.push ( c );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Confirm that what was said was going to happen actually did
|
||||||
|
|
||||||
|
\sa rowsAboutToBeInserted()
|
||||||
|
*/
|
||||||
|
void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end )
|
||||||
|
{
|
||||||
|
Changing c = insert.pop();
|
||||||
|
QCOMPARE(c.parent, parent);
|
||||||
|
// qDebug() << "rowsInserted" << "start=" << start << "end=" << end << "oldsize=" << c.oldSize
|
||||||
|
// << "parent=" << model->data ( parent ).toString() << "current rowcount of parent=" << model->rowCount ( parent );
|
||||||
|
|
||||||
|
// for (int ii=start; ii <= end; ii++)
|
||||||
|
// {
|
||||||
|
// qDebug() << "itemWasInserted:" << ii << model->data ( model->index ( ii, 0, parent ));
|
||||||
|
// }
|
||||||
|
// qDebug();
|
||||||
|
|
||||||
|
QCOMPARE(c.oldSize + (end - start + 1), model->rowCount(parent));
|
||||||
|
QCOMPARE(c.last, model->data(model->index(start - 1, 0, c.parent)));
|
||||||
|
|
||||||
|
if (c.next != model->data(model->index(end + 1, 0, c.parent))) {
|
||||||
|
qDebug() << start << end;
|
||||||
|
for (int i=0; i < model->rowCount(); ++i)
|
||||||
|
qDebug() << model->index(i, 0).data().toString();
|
||||||
|
qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent));
|
||||||
|
}
|
||||||
|
|
||||||
|
QCOMPARE(c.next, model->data(model->index(end + 1, 0, c.parent)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelTest::layoutAboutToBeChanged()
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < qBound ( 0, model->rowCount(), 100 ); ++i )
|
||||||
|
changing.append ( QPersistentModelIndex ( model->index ( i, 0 ) ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelTest::layoutChanged()
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < changing.count(); ++i ) {
|
||||||
|
QPersistentModelIndex p = changing[i];
|
||||||
|
QCOMPARE(QModelIndex(p), model->index(p.row(), p.column(), p.parent()));
|
||||||
|
}
|
||||||
|
changing.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Store what is about to be inserted to make sure it actually happens
|
||||||
|
|
||||||
|
\sa rowsRemoved()
|
||||||
|
*/
|
||||||
|
void ModelTest::rowsAboutToBeRemoved ( const QModelIndex &parent, int start, int end )
|
||||||
|
{
|
||||||
|
qDebug() << "ratbr" << parent << start << end;
|
||||||
|
Changing c;
|
||||||
|
c.parent = parent;
|
||||||
|
c.oldSize = model->rowCount ( parent );
|
||||||
|
c.last = model->data ( model->index ( start - 1, 0, parent ) );
|
||||||
|
c.next = model->data ( model->index ( end + 1, 0, parent ) );
|
||||||
|
remove.push ( c );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Confirm that what was said was going to happen actually did
|
||||||
|
|
||||||
|
\sa rowsAboutToBeRemoved()
|
||||||
|
*/
|
||||||
|
void ModelTest::rowsRemoved ( const QModelIndex & parent, int start, int end )
|
||||||
|
{
|
||||||
|
qDebug() << "rr" << parent << start << end;
|
||||||
|
Changing c = remove.pop();
|
||||||
|
QCOMPARE(c.parent, parent);
|
||||||
|
QCOMPARE(c.oldSize - (end - start + 1), model->rowCount(parent));
|
||||||
|
QCOMPARE(c.last, model->data(model->index(start - 1, 0, c.parent)));
|
||||||
|
QCOMPARE(c.next, model->data(model->index(start, 0, c.parent)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelTest::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
||||||
|
{
|
||||||
|
QVERIFY(topLeft.isValid());
|
||||||
|
QVERIFY(bottomRight.isValid());
|
||||||
|
QModelIndex commonParent = bottomRight.parent();
|
||||||
|
QCOMPARE(topLeft.parent(), commonParent);
|
||||||
|
QVERIFY(topLeft.row() <= bottomRight.row());
|
||||||
|
QVERIFY(topLeft.column() <= bottomRight.column());
|
||||||
|
int rowCount = model->rowCount(commonParent);
|
||||||
|
int columnCount = model->columnCount(commonParent);
|
||||||
|
QVERIFY(bottomRight.row() < rowCount);
|
||||||
|
QVERIFY(bottomRight.column() < columnCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ModelTest::headerDataChanged(Qt::Orientation orientation, int start, int end)
|
||||||
|
{
|
||||||
|
QVERIFY(start >= 0);
|
||||||
|
QVERIFY(end >= 0);
|
||||||
|
QVERIFY(start <= end);
|
||||||
|
int itemCount = orientation == Qt::Vertical ? model->rowCount() : model->columnCount();
|
||||||
|
QVERIFY(start < itemCount);
|
||||||
|
QVERIFY(end < itemCount);
|
||||||
|
}
|
||||||
|
|
83
extra/modeltest/modeltest.h
Normal file
83
extra/modeltest/modeltest.h
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** 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 The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef MODELTEST_H
|
||||||
|
#define MODELTEST_H
|
||||||
|
|
||||||
|
#include <QtCore/QObject>
|
||||||
|
#include <QtCore/QAbstractItemModel>
|
||||||
|
#include <QtCore/QStack>
|
||||||
|
|
||||||
|
class ModelTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
ModelTest( QAbstractItemModel *model, QObject *parent = 0 );
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void nonDestructiveBasicTest();
|
||||||
|
void rowCount();
|
||||||
|
void columnCount();
|
||||||
|
void hasIndex();
|
||||||
|
void index();
|
||||||
|
void parent();
|
||||||
|
void data();
|
||||||
|
|
||||||
|
protected Q_SLOTS:
|
||||||
|
void runAllTests();
|
||||||
|
void layoutAboutToBeChanged();
|
||||||
|
void layoutChanged();
|
||||||
|
void rowsAboutToBeInserted( const QModelIndex &parent, int start, int end );
|
||||||
|
void rowsInserted( const QModelIndex & parent, int start, int end );
|
||||||
|
void rowsAboutToBeRemoved( const QModelIndex &parent, int start, int end );
|
||||||
|
void rowsRemoved( const QModelIndex & parent, int start, int end );
|
||||||
|
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
|
||||||
|
void headerDataChanged(Qt::Orientation orientation, int start, int end);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void checkChildren( const QModelIndex &parent, int currentDepth = 0 );
|
||||||
|
|
||||||
|
QAbstractItemModel *model;
|
||||||
|
|
||||||
|
struct Changing {
|
||||||
|
QModelIndex parent;
|
||||||
|
int oldSize;
|
||||||
|
QVariant last;
|
||||||
|
QVariant next;
|
||||||
|
};
|
||||||
|
QStack<Changing> insert;
|
||||||
|
QStack<Changing> remove;
|
||||||
|
|
||||||
|
bool fetchingMore;
|
||||||
|
|
||||||
|
QList<QPersistentModelIndex> changing;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
5
extra/modeltest/modeltest.pro
Normal file
5
extra/modeltest/modeltest.pro
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
TEMPLATE = lib
|
||||||
|
CONFIG += testcase static
|
||||||
|
QT += widgets testlib
|
||||||
|
SOURCES += modeltest.cpp dynamictreemodel.cpp
|
||||||
|
HEADERS += modeltest.h dynamictreemodel.h
|
301
extra/modeltest/tst_modeltest.cpp
Normal file
301
extra/modeltest/tst_modeltest.cpp
Normal file
@ -0,0 +1,301 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** 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 The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
#include <QtGui/QtGui>
|
||||||
|
#include <QtWidgets/QtWidgets>
|
||||||
|
|
||||||
|
#include "modeltest.h"
|
||||||
|
#include "dynamictreemodel.h"
|
||||||
|
|
||||||
|
|
||||||
|
class tst_ModelTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void stringListModel();
|
||||||
|
void treeWidgetModel();
|
||||||
|
void standardItemModel();
|
||||||
|
void testInsertThroughProxy();
|
||||||
|
void moveSourceItems();
|
||||||
|
void testResetThroughProxy();
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
void tst_ModelTest::stringListModel()
|
||||||
|
{
|
||||||
|
QStringListModel model;
|
||||||
|
QSortFilterProxyModel proxy;
|
||||||
|
|
||||||
|
ModelTest t1(&model);
|
||||||
|
ModelTest t2(&proxy);
|
||||||
|
|
||||||
|
proxy.setSourceModel(&model);
|
||||||
|
|
||||||
|
model.setStringList(QStringList() << "2" << "3" << "1");
|
||||||
|
model.setStringList(QStringList() << "a" << "e" << "plop" << "b" << "c" );
|
||||||
|
|
||||||
|
proxy.setDynamicSortFilter(true);
|
||||||
|
proxy.setFilterRegExp(QRegExp("[^b]"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_ModelTest::treeWidgetModel()
|
||||||
|
{
|
||||||
|
QTreeWidget widget;
|
||||||
|
|
||||||
|
ModelTest t1(widget.model());
|
||||||
|
|
||||||
|
QTreeWidgetItem *root = new QTreeWidgetItem(&widget, QStringList("root"));
|
||||||
|
for (int i = 0; i < 20; ++i) {
|
||||||
|
new QTreeWidgetItem(root, QStringList(QString::number(i)));
|
||||||
|
}
|
||||||
|
QTreeWidgetItem *remove = root->child(2);
|
||||||
|
root->removeChild(remove);
|
||||||
|
QTreeWidgetItem *parent = new QTreeWidgetItem(&widget, QStringList("parent"));
|
||||||
|
new QTreeWidgetItem(parent, QStringList("child"));
|
||||||
|
widget.setItemHidden(parent, true);
|
||||||
|
|
||||||
|
widget.sortByColumn(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_ModelTest::standardItemModel()
|
||||||
|
{
|
||||||
|
QStandardItemModel model(10,10);
|
||||||
|
QSortFilterProxyModel proxy;
|
||||||
|
|
||||||
|
|
||||||
|
ModelTest t1(&model);
|
||||||
|
ModelTest t2(&proxy);
|
||||||
|
|
||||||
|
proxy.setSourceModel(&model);
|
||||||
|
|
||||||
|
model.insertRows(2, 5);
|
||||||
|
model.removeRows(4, 5);
|
||||||
|
|
||||||
|
model.insertColumns(2, 5);
|
||||||
|
model.removeColumns(4, 5);
|
||||||
|
|
||||||
|
model.insertRows(0,5, model.index(1,1));
|
||||||
|
model.insertColumns(0,5, model.index(1,3));
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_ModelTest::testInsertThroughProxy()
|
||||||
|
{
|
||||||
|
DynamicTreeModel *model = new DynamicTreeModel(this);
|
||||||
|
|
||||||
|
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
|
||||||
|
proxy->setSourceModel(model);
|
||||||
|
|
||||||
|
new ModelTest(proxy, this);
|
||||||
|
|
||||||
|
ModelInsertCommand *insertCommand = new ModelInsertCommand(model, this);
|
||||||
|
insertCommand->setNumCols(4);
|
||||||
|
insertCommand->setStartRow(0);
|
||||||
|
insertCommand->setEndRow(9);
|
||||||
|
// Parent is QModelIndex()
|
||||||
|
insertCommand->doCommand();
|
||||||
|
|
||||||
|
insertCommand = new ModelInsertCommand(model, this);
|
||||||
|
insertCommand->setNumCols(4);
|
||||||
|
insertCommand->setAncestorRowNumbers(QList<int>() << 5);
|
||||||
|
insertCommand->setStartRow(0);
|
||||||
|
insertCommand->setEndRow(9);
|
||||||
|
insertCommand->doCommand();
|
||||||
|
|
||||||
|
ModelMoveCommand *moveCommand = new ModelMoveCommand(model, this);
|
||||||
|
moveCommand->setNumCols(4);
|
||||||
|
moveCommand->setStartRow(0);
|
||||||
|
moveCommand->setEndRow(0);
|
||||||
|
moveCommand->setDestRow(9);
|
||||||
|
moveCommand->setDestAncestors(QList<int>() << 5);
|
||||||
|
moveCommand->doCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Makes the persistent index list publicly accessible
|
||||||
|
*/
|
||||||
|
class AccessibleProxyModel : public QSortFilterProxyModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
AccessibleProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
|
||||||
|
|
||||||
|
QModelIndexList persistent()
|
||||||
|
{
|
||||||
|
return persistentIndexList();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ObservingObject : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ObservingObject(AccessibleProxyModel *proxy, QObject *parent = 0)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_proxy(proxy)
|
||||||
|
, storePersistentFailureCount(0)
|
||||||
|
, checkPersistentFailureCount(0)
|
||||||
|
{
|
||||||
|
connect(m_proxy, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), SLOT(storePersistent()));
|
||||||
|
connect(m_proxy, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), SLOT(checkPersistent()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
void storePersistent(const QModelIndex &parent)
|
||||||
|
{
|
||||||
|
for (int row = 0; row < m_proxy->rowCount(parent); ++row) {
|
||||||
|
QModelIndex proxyIndex = m_proxy->index(row, 0, parent);
|
||||||
|
QModelIndex sourceIndex = m_proxy->mapToSource(proxyIndex);
|
||||||
|
if (!proxyIndex.isValid()) {
|
||||||
|
qWarning("%s: Invalid proxy index", Q_FUNC_INFO);
|
||||||
|
++storePersistentFailureCount;
|
||||||
|
}
|
||||||
|
if (!sourceIndex.isValid()) {
|
||||||
|
qWarning("%s: invalid source index", Q_FUNC_INFO);
|
||||||
|
++storePersistentFailureCount;
|
||||||
|
}
|
||||||
|
m_persistentSourceIndexes.append(sourceIndex);
|
||||||
|
m_persistentProxyIndexes.append(proxyIndex);
|
||||||
|
if (m_proxy->hasChildren(proxyIndex))
|
||||||
|
storePersistent(proxyIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void storePersistent()
|
||||||
|
{
|
||||||
|
// This method is called from rowsAboutToBeMoved. Persistent indexes should be valid
|
||||||
|
foreach(const QModelIndex &idx, m_persistentProxyIndexes)
|
||||||
|
if (!idx.isValid()) {
|
||||||
|
qWarning("%s: persistentProxyIndexes contains invalid index", Q_FUNC_INFO);
|
||||||
|
++storePersistentFailureCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_proxy->persistent().isEmpty()) {
|
||||||
|
qWarning("%s: proxy should have no persistent indexes when storePersistent called",
|
||||||
|
Q_FUNC_INFO);
|
||||||
|
++storePersistentFailureCount;
|
||||||
|
}
|
||||||
|
storePersistent(QModelIndex());
|
||||||
|
if (m_proxy->persistent().isEmpty()) {
|
||||||
|
qWarning("%s: proxy should have persistent index after storePersistent called",
|
||||||
|
Q_FUNC_INFO);
|
||||||
|
++storePersistentFailureCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkPersistent()
|
||||||
|
{
|
||||||
|
for (int row = 0; row < m_persistentProxyIndexes.size(); ++row) {
|
||||||
|
m_persistentProxyIndexes.at(row);
|
||||||
|
m_persistentSourceIndexes.at(row);
|
||||||
|
}
|
||||||
|
for (int row = 0; row < m_persistentProxyIndexes.size(); ++row) {
|
||||||
|
QModelIndex updatedProxy = m_persistentProxyIndexes.at(row);
|
||||||
|
QModelIndex updatedSource = m_persistentSourceIndexes.at(row);
|
||||||
|
if (m_proxy->mapToSource(updatedProxy) != updatedSource) {
|
||||||
|
qWarning("%s: check failed at row %d", Q_FUNC_INFO, row);
|
||||||
|
++checkPersistentFailureCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_persistentSourceIndexes.clear();
|
||||||
|
m_persistentProxyIndexes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
AccessibleProxyModel *m_proxy;
|
||||||
|
QList<QPersistentModelIndex> m_persistentSourceIndexes;
|
||||||
|
QList<QPersistentModelIndex> m_persistentProxyIndexes;
|
||||||
|
public:
|
||||||
|
int storePersistentFailureCount;
|
||||||
|
int checkPersistentFailureCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
void tst_ModelTest::moveSourceItems()
|
||||||
|
{
|
||||||
|
DynamicTreeModel *model = new DynamicTreeModel(this);
|
||||||
|
AccessibleProxyModel *proxy = new AccessibleProxyModel(this);
|
||||||
|
proxy->setSourceModel(model);
|
||||||
|
|
||||||
|
ModelInsertCommand *insertCommand = new ModelInsertCommand(model, this);
|
||||||
|
insertCommand->setStartRow(0);
|
||||||
|
insertCommand->setEndRow(2);
|
||||||
|
insertCommand->doCommand();
|
||||||
|
|
||||||
|
insertCommand = new ModelInsertCommand(model, this);
|
||||||
|
insertCommand->setAncestorRowNumbers(QList<int>() << 1);
|
||||||
|
insertCommand->setStartRow(0);
|
||||||
|
insertCommand->setEndRow(2);
|
||||||
|
insertCommand->doCommand();
|
||||||
|
|
||||||
|
ObservingObject observer(proxy);
|
||||||
|
|
||||||
|
ModelMoveCommand *moveCommand = new ModelMoveCommand(model, this);
|
||||||
|
moveCommand->setStartRow(0);
|
||||||
|
moveCommand->setEndRow(0);
|
||||||
|
moveCommand->setDestAncestors(QList<int>() << 1);
|
||||||
|
moveCommand->setDestRow(0);
|
||||||
|
moveCommand->doCommand();
|
||||||
|
|
||||||
|
QCOMPARE(observer.storePersistentFailureCount, 0);
|
||||||
|
QCOMPARE(observer.checkPersistentFailureCount, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_ModelTest::testResetThroughProxy()
|
||||||
|
{
|
||||||
|
DynamicTreeModel *model = new DynamicTreeModel(this);
|
||||||
|
|
||||||
|
ModelInsertCommand *insertCommand = new ModelInsertCommand(model, this);
|
||||||
|
insertCommand->setStartRow(0);
|
||||||
|
insertCommand->setEndRow(2);
|
||||||
|
insertCommand->doCommand();
|
||||||
|
|
||||||
|
QPersistentModelIndex persistent = model->index(0, 0);
|
||||||
|
|
||||||
|
AccessibleProxyModel *proxy = new AccessibleProxyModel(this);
|
||||||
|
proxy->setSourceModel(model);
|
||||||
|
|
||||||
|
ObservingObject observer(proxy);
|
||||||
|
observer.storePersistent();
|
||||||
|
|
||||||
|
ModelResetCommand *resetCommand = new ModelResetCommand(model, this);
|
||||||
|
resetCommand->setNumCols(0);
|
||||||
|
resetCommand->doCommand();
|
||||||
|
|
||||||
|
QCOMPARE(observer.storePersistentFailureCount, 0);
|
||||||
|
QCOMPARE(observer.checkPersistentFailureCount, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QTEST_MAIN(tst_ModelTest)
|
||||||
|
#include "tst_modeltest.moc"
|
@ -70,7 +70,7 @@ int testImportPcap(int argc, char* argv[])
|
|||||||
if (!error.isEmpty())
|
if (!error.isEmpty())
|
||||||
{
|
{
|
||||||
printf("%s: %s\n",
|
printf("%s: %s\n",
|
||||||
inFile.toAscii().constData(), error.toAscii().constData());
|
qPrintable(inFile), qPrintable(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isOk)
|
if (!isOk)
|
||||||
|
Loading…
Reference in New Issue
Block a user