/tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp
https://bitbucket.org/ultra_iter/qt-vtl · C++ · 2713 lines · 2254 code · 329 blank · 130 comment · 91 complexity · 65f361859ee45d048fee8866e318933b MD5 · raw file
Large files are truncated click here to view the full file
- /****************************************************************************
- **
- ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
- ** All rights reserved.
- ** Contact: Nokia Corporation (qt-info@nokia.com)
- **
- ** This file is part of the test suite of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:LGPL$
- ** GNU Lesser General Public License Usage
- ** This file may be used under the terms of the GNU Lesser General Public
- ** License version 2.1 as published by the Free Software Foundation and
- ** appearing in the file LICENSE.LGPL included in the packaging of this
- ** file. Please review the following information to ensure the GNU Lesser
- ** General Public License version 2.1 requirements will be met:
- ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- **
- ** In addition, as a special exception, Nokia gives you certain additional
- ** rights. These rights are described in the Nokia Qt LGPL Exception
- ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU General
- ** Public License version 3.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 the GNU General
- ** Public License version 3.0 requirements will be met:
- ** http://www.gnu.org/copyleft/gpl.html.
- **
- ** Other Usage
- ** Alternatively, this file may be used in accordance with the terms and
- ** conditions contained in a signed written agreement between you and Nokia.
- **
- **
- **
- **
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #include <QtTest/QtTest>
- #include <QtGui/QtGui>
- //TESTED_CLASS=
- //TESTED_FILES=
- class tst_QItemSelectionModel : public QObject
- {
- Q_OBJECT
- public:
- tst_QItemSelectionModel();
- virtual ~tst_QItemSelectionModel();
- public slots:
- void initTestCase();
- void cleanupTestCase();
- void init();
- private slots:
- void clear_data();
- void clear();
- void clearAndSelect();
- void toggleSelection();
- void select_data();
- void select();
- void persistentselections_data();
- void persistentselections();
- void resetModel();
- void removeRows_data();
- void removeRows();
- void removeColumns_data();
- void removeColumns();
- void modelLayoutChanged_data();
- void modelLayoutChanged();
- void selectedRows_data();
- void selectedRows();
- void selectedColumns_data();
- void selectedColumns();
- void setCurrentIndex();
- void splitOnInsert();
- void task196285_rowIntersectsSelection();
- void unselectable();
- void task220420_selectedIndexes();
- void task240734_layoutChanged();
- void merge_data();
- void merge();
- void task119433_isRowSelected();
- void task252069_rowIntersectsSelection();
- void task232634_childrenDeselectionSignal();
- void task260134_layoutChangedWithAllSelected();
- void QTBUG5671_layoutChangedWithAllSelected();
- void QTBUG2804_layoutChangedTreeSelection();
- void deselectRemovedMiddleRange();
- void rangeOperatorLessThan_data();
- void rangeOperatorLessThan();
- void testDifferentModels();
- void testValidRangesInSelectionsAfterReset();
- void testChainedSelectionClear();
- private:
- QAbstractItemModel *model;
- QItemSelectionModel *selection;
- };
- QDataStream &operator<<(QDataStream &, const QModelIndex &);
- QDataStream &operator>>(QDataStream &, QModelIndex &);
- QDataStream &operator<<(QDataStream &, const QModelIndexList &);
- QDataStream &operator>>(QDataStream &, QModelIndexList &);
- typedef QList<int> IntList;
- typedef QPair<int, int> IntPair;
- typedef QList<IntPair> PairList;
- Q_DECLARE_METATYPE(PairList)
- Q_DECLARE_METATYPE(QModelIndex)
- Q_DECLARE_METATYPE(QModelIndexList)
- Q_DECLARE_METATYPE(IntList)
- Q_DECLARE_METATYPE(QItemSelection)
- class QStreamHelper: public QAbstractItemModel
- {
- public:
- QStreamHelper() {}
- static QModelIndex create(int row = -1, int column = -1, void *data = 0)
- {
- QStreamHelper helper;
- return helper.QAbstractItemModel::createIndex(row, column, data);
- }
- QModelIndex index(int, int, const QModelIndex&) const
- { return QModelIndex(); }
- QModelIndex parent(const QModelIndex&) const
- { return QModelIndex(); }
- int rowCount(const QModelIndex & = QModelIndex()) const
- { return 0; }
- int columnCount(const QModelIndex & = QModelIndex()) const
- { return 0; }
- QVariant data(const QModelIndex &, int = Qt::DisplayRole) const
- { return QVariant(); }
- bool hasChildren(const QModelIndex &) const
- { return false; }
- };
- QDataStream &operator<<(QDataStream &s, const QModelIndex &input)
- {
- s << input.row()
- << input.column()
- << reinterpret_cast<qlonglong>(input.internalPointer());
- return s;
- }
- QDataStream &operator>>(QDataStream &s, QModelIndex &output)
- {
- int r, c;
- qlonglong ptr;
- s >> r;
- s >> c;
- s >> ptr;
- output = QStreamHelper::create(r, c, reinterpret_cast<void *>(ptr));
- return s;
- }
- QDataStream &operator<<(QDataStream &s, const QModelIndexList &input)
- {
- s << input.count();
- for (int i=0; i<input.count(); ++i)
- s << input.at(i);
- return s;
- }
- QDataStream &operator>>(QDataStream &s, QModelIndexList &output)
- {
- QModelIndex tmpIndex;
- int count;
- s >> count;
- for (int i=0; i<count; ++i) {
- s >> tmpIndex;
- output << tmpIndex;
- }
- return s;
- }
- tst_QItemSelectionModel::tst_QItemSelectionModel() : model(0), selection(0)
- {
- }
- tst_QItemSelectionModel::~tst_QItemSelectionModel()
- {
- }
- /*
- This test usually uses a model with a 5x5 table
- -------------------------------------------
- | 0,0 | 0,1 | 0,2 | 0,3 | 0,4 |
- -------------------------------------------
- | 1,0 | 1,1 | 1,2 | 1,3 | 1,4 |
- -------------------------------------------
- | 2,0 | 2,1 | 2,2 | 2,3 | 2,4 |
- -------------------------------------------
- | 3,0 | 3,1 | 3,2 | 3,3 | 3,4 |
- -------------------------------------------
- | 4,0 | 4,1 | 4,2 | 4,3 | 4,4 |
- -------------------------------------------
- ...that for each row has a children in a new 5x5 table ad infinitum.
- */
- void tst_QItemSelectionModel::initTestCase()
- {
- qRegisterMetaType<QItemSelection>("QItemSelection");
- model = new QStandardItemModel(5, 5);
- QModelIndex parent = model->index(0, 0, QModelIndex());
- model->insertRows(0, 5, parent);
- model->insertColumns(0, 5, parent);
- selection = new QItemSelectionModel(model);
- }
- void tst_QItemSelectionModel::cleanupTestCase()
- {
- delete selection;
- delete model;
- }
- void tst_QItemSelectionModel::init()
- {
- selection->clear();
- while (model->rowCount(QModelIndex()) > 5)
- model->removeRow(0, QModelIndex());
- while (model->rowCount(QModelIndex()) < 5)
- model->insertRow(0, QModelIndex());
- }
- void tst_QItemSelectionModel::clear_data()
- {
- QTest::addColumn<QModelIndexList>("indexList");
- QTest::addColumn<IntList>("commandList");
- {
- QModelIndexList index;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- index << model->index(1, 0, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- QTest::newRow("(0, 0) and (1, 0): Select|Rows")
- << index
- << command;
- }
- {
- QModelIndexList index;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- index << model->index(0, 1, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- QTest::newRow("(0, 0) and (1, 0): Select|Columns")
- << index
- << command;
- }
- {
- QModelIndexList index;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(1, 1, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::SelectCurrent;
- QTest::newRow("(0, 0), (1, 1) and (2, 2): Select, Select, SelectCurrent")
- << index
- << command;
- }
- {
- QModelIndexList index;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(1, 1, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(1, 1, QModelIndex());
- command << QItemSelectionModel::Toggle;
- QTest::newRow("(0, 0), (1, 1) and (1, 1): Select, Select, Toggle")
- << index
- << command;
- }
- {
- QModelIndexList index;
- IntList command;
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- QTest::newRow("child (0, 0) of (0, 0): Select|Rows")
- << index
- << command;
- }
- }
- void tst_QItemSelectionModel::clear()
- {
- QFETCH(QModelIndexList, indexList);
- QFETCH(IntList, commandList);
- // do selections
- for (int i=0; i<indexList.count(); ++i) {
- selection->select(indexList.at(i), (QItemSelectionModel::SelectionFlags)commandList.at(i));
- }
- // test that we have selected items
- QVERIFY(!selection->selectedIndexes().isEmpty());
- selection->clear();
- // test that they were all cleared
- QVERIFY(selection->selectedIndexes().isEmpty());
- }
- void tst_QItemSelectionModel::clearAndSelect()
- {
- // populate selectionmodel
- selection->select(model->index(1, 1, QModelIndex()), QItemSelectionModel::Select);
- QCOMPARE(selection->selectedIndexes().count(), 1);
- QVERIFY(selection->hasSelection());
- // ClearAndSelect with empty selection
- QItemSelection emptySelection;
- selection->select(emptySelection, QItemSelectionModel::ClearAndSelect);
- // verify the selectionmodel is empty
- QVERIFY(selection->selectedIndexes().isEmpty());
- QVERIFY(selection->hasSelection()==false);
- }
- void tst_QItemSelectionModel::toggleSelection()
- {
- //test the toggle selection and checks whether selectedIndex
- //and hasSelection returns the correct value
- selection->clearSelection();
- QCOMPARE(selection->selectedIndexes().count(), 0);
- QVERIFY(selection->hasSelection()==false);
- QModelIndex index=model->index(1, 1, QModelIndex());
- // populate selectionmodel
- selection->select(index, QItemSelectionModel::Toggle);
- QCOMPARE(selection->selectedIndexes().count(), 1);
- QVERIFY(selection->hasSelection()==true);
- selection->select(index, QItemSelectionModel::Toggle);
- QCOMPARE(selection->selectedIndexes().count(), 0);
- QVERIFY(selection->hasSelection()==false);
- // populate selectionmodel with rows
- selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
- QCOMPARE(selection->selectedIndexes().count(), model->columnCount());
- QVERIFY(selection->hasSelection()==true);
- selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
- QCOMPARE(selection->selectedIndexes().count(), 0);
- QVERIFY(selection->hasSelection()==false);
- }
- void tst_QItemSelectionModel::select_data()
- {
- QTest::addColumn<QModelIndexList>("indexList");
- QTest::addColumn<bool>("useRanges");
- QTest::addColumn<IntList>("commandList");
- QTest::addColumn<QModelIndexList>("expectedList");
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- expected << model->index(0, 0, QModelIndex());
- QTest::newRow("(0, 0): Select")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- command << QItemSelectionModel::Select;
- expected << model->index(0, 0, model->index(0, 0, QModelIndex()));
- QTest::newRow("child (0, 0) of (0, 0): Select")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Deselect;
- QTest::newRow("(0, 0): Deselect")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex());
- QTest::newRow("(0, 0): Toggle")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Toggle;
- QTest::newRow("(0, 0) and (0, 0): Select and Toggle")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Deselect;
- QTest::newRow("(0, 0) and (0, 0): Select and Deselect")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- command << QItemSelectionModel::ClearAndSelect;
- expected << model->index(0, 0, model->index(0, 0, QModelIndex()));
- QTest::newRow("(0, 0) and child (0, 0) of (0, 0): Select and ClearAndSelect")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 1, QModelIndex());
- index << model->index(4, 1, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 1, QModelIndex());
- command << QItemSelectionModel::Deselect;
- QTest::newRow("(0, 0 to 4, 0) and (0, 1 to 4, 1) and (0, 0 to 4, 1): Select and Select and Deselect")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(4, 4, QModelIndex());
- command << QItemSelectionModel::Select;
- expected << model->index(0, 0, QModelIndex()) << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0) and (4, 4): Select")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(4, 4, QModelIndex());
- command << QItemSelectionModel::ClearAndSelect;
- expected << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0) and (4, 4): Select and ClearAndSelect")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- index << model->index(4, 4, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(4, 1, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(4, 3, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0) and (4, 4): Select|Rows")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- index << model->index(4, 4, model->index(0, 0, QModelIndex()));
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- QModelIndex parent = model->index(0, 0, QModelIndex());
- expected << model->index(0, 0, parent)
- << model->index(0, 1, parent)
- << model->index(0, 2, parent)
- << model->index(0, 3, parent)
- << model->index(0, 4, parent)
- << model->index(4, 0, parent)
- << model->index(4, 1, parent)
- << model->index(4, 2, parent)
- << model->index(4, 3, parent)
- << model->index(4, 4, parent);
- QTest::newRow("child (0, 0) and (4, 4) of (0, 0): Select|Rows")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- index << model->index(4, 4, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- expected << model->index(0, 0, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 4, QModelIndex())
- << model->index(2, 4, QModelIndex())
- << model->index(3, 4, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0) and (4, 4): Select|Columns")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- index << model->index(4, 4, model->index(0, 0, QModelIndex()));
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- expected << model->index(0, 0, model->index(0, 0, QModelIndex()))
- << model->index(1, 0, model->index(0, 0, QModelIndex()))
- << model->index(2, 0, model->index(0, 0, QModelIndex()))
- << model->index(3, 0, model->index(0, 0, QModelIndex()))
- << model->index(4, 0, model->index(0, 0, QModelIndex()))
- << model->index(0, 4, model->index(0, 0, QModelIndex()))
- << model->index(1, 4, model->index(0, 0, QModelIndex()))
- << model->index(2, 4, model->index(0, 0, QModelIndex()))
- << model->index(3, 4, model->index(0, 0, QModelIndex()))
- << model->index(4, 4, model->index(0, 0, QModelIndex()));
- QTest::newRow("child (0, 0) and (4, 4) of (0, 0): Select|Columns")
- << index
- << false
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 0, QModelIndex());
- command << QItemSelectionModel::Select;
- expected << model->index(0, 0, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(4, 0, QModelIndex());
- QTest::newRow("(0, 0 to 4, 0): Select")
- << index
- << true
- << command
- << expected;
- }
- /* ### FAILS
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- command << QItemSelectionModel::Select;
- QTest::newRow("(0, 0 to child 0, 0): Select")
- << index
- << true
- << command
- << expected;
- }
- */
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, model->index(0, 0, QModelIndex()));
- index << model->index(0, 0, model->index(1, 0, QModelIndex()));
- command << QItemSelectionModel::Select;
- QTest::newRow("child (0, 0) of (0, 0) to child (0, 0) of (1, 0): Select")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 4, QModelIndex());
- command << QItemSelectionModel::Select;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(1, 3, QModelIndex())
- << model->index(1, 4, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(3, 1, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(3, 3, QModelIndex())
- << model->index(3, 4, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(4, 1, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(4, 3, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0 to 4, 4): Select")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 0, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(1, 3, QModelIndex())
- << model->index(1, 4, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(3, 1, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(3, 3, QModelIndex())
- << model->index(3, 4, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(4, 1, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(4, 3, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0 to 4, 0): Select|Rows")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(0, 4, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(1, 3, QModelIndex())
- << model->index(1, 4, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(3, 1, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(3, 3, QModelIndex())
- << model->index(3, 4, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(4, 1, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(4, 3, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0 to 0, 4): Select|Columns")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 4, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(1, 3, QModelIndex())
- << model->index(1, 4, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(3, 1, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(3, 3, QModelIndex())
- << model->index(3, 4, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(4, 1, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(4, 3, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0 to 4, 4): Select|Rows")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(4, 4, QModelIndex());
- command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(1, 3, QModelIndex())
- << model->index(1, 4, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex())
- << model->index(3, 0, QModelIndex())
- << model->index(3, 1, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(3, 3, QModelIndex())
- << model->index(3, 4, QModelIndex())
- << model->index(4, 0, QModelIndex())
- << model->index(4, 1, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(4, 3, QModelIndex())
- << model->index(4, 4, QModelIndex());
- QTest::newRow("(0, 0 to 4, 4): Select|Columns")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 2, QModelIndex());
- index << model->index(4, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 0, QModelIndex());
- index << model->index(2, 4, QModelIndex());
- command << QItemSelectionModel::Select;
- expected << model->index(0, 2, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex());
- QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 2, QModelIndex());
- index << model->index(4, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 0, QModelIndex());
- index << model->index(2, 4, QModelIndex());
- command << QItemSelectionModel::SelectCurrent;
- expected << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex());
- QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and SelectCurrent")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 2, QModelIndex());
- index << model->index(4, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 0, QModelIndex());
- index << model->index(2, 4, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 2, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(4, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex());
- QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and Toggle")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 2, QModelIndex());
- index << model->index(4, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 0, QModelIndex());
- index << model->index(2, 4, QModelIndex());
- command << QItemSelectionModel::Deselect;
- expected << model->index(0, 2, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(4, 2, QModelIndex());
- QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and Deselect")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 0, QModelIndex());
- index << model->index(0, 0, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (0, 0 to 0, 0): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 1, QModelIndex());
- index << model->index(0, 1, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (0, 1 to 0, 1): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(0, 2, QModelIndex());
- index << model->index(0, 2, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (0, 2 to 0, 2): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(1, 0, QModelIndex());
- index << model->index(1, 0, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (1, 0 to 1, 0): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(1, 1, QModelIndex());
- index << model->index(1, 1, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (1, 1 to 1, 1): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(1, 2, QModelIndex());
- index << model->index(1, 2, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (1, 2 to 1, 2): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 0, QModelIndex());
- index << model->index(2, 0, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (2, 0 to 2, 0): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 1, QModelIndex());
- index << model->index(2, 1, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 2, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (2, 1 to 2, 1): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList index;
- QModelIndexList expected;
- IntList command;
- index << model->index(0, 0, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Select;
- index << model->index(2, 2, QModelIndex());
- index << model->index(2, 2, QModelIndex());
- command << QItemSelectionModel::Toggle;
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex());
- QTest::newRow("(0, 0 to 2, 2) and (2, 2 to 2, 2): Select and Toggle at selection boundary")
- << index
- << true
- << command
- << expected;
- }
- {
- QModelIndexList indexes;
- IntList commands;
- QModelIndexList expected;
- indexes << model->index(0, 0, QModelIndex()) << model->index(0, 0, QModelIndex()) // press 0
- << model->index(0, 0, QModelIndex()) << model->index(0, 0, QModelIndex()) // release 0
- << model->index(1, 0, QModelIndex()) << model->index(1, 0, QModelIndex()) // press 1
- << model->index(1, 0, QModelIndex()) << model->index(1, 0, QModelIndex()) // release 1
- << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // press 2
- << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // release 2
- << model->index(3, 0, QModelIndex()) << model->index(3, 0, QModelIndex()) // press 3
- << model->index(3, 0, QModelIndex()) << model->index(3, 0, QModelIndex()) // release 3
- << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // press 2 again
- << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex());// move 2
- commands << (QItemSelectionModel::NoUpdate) // press 0
- << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 0
- << (QItemSelectionModel::NoUpdate) // press 1
- << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 1
- << (QItemSelectionModel::NoUpdate) // press 2
- << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 2
- << (QItemSelectionModel::NoUpdate) // press 3
- << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 3
- << (QItemSelectionModel::NoUpdate) // press 2 again
- << (QItemSelectionModel::Toggle/*Current*/|QItemSelectionModel::Rows);// move 2
- expected << model->index(0, 0, QModelIndex())
- << model->index(0, 1, QModelIndex())
- << model->index(0, 2, QModelIndex())
- << model->index(0, 3, QModelIndex())
- << model->index(0, 4, QModelIndex())
- << model->index(1, 0, QModelIndex())
- << model->index(1, 1, QModelIndex())
- << model->index(1, 2, QModelIndex())
- << model->index(1, 3, QModelIndex())
- << model->index(1, 4, QModelIndex())
- /*
- << model->index(2, 0, QModelIndex())
- << model->index(2, 1, QModelIndex())
- << model->index(2, 2, QModelIndex())
- << model->index(2, 3, QModelIndex())
- << model->index(2, 4, QModelIndex())
- */
- << model->index(3, 0, QModelIndex())
- << model->index(3, 1, QModelIndex())
- << model->index(3, 2, QModelIndex())
- << model->index(3, 3, QModelIndex())
- << model->index(3, 4, QModelIndex());
- QTest::newRow("simulated treeview multiselection behavior")
- << indexes
- << true
- << commands
- << expected;
- }
- }
- void tst_QItemSelectionModel::select()
- {
- QFETCH(QModelIndexList, indexList);
- QFETCH(bool, useRanges);
- QFETCH(IntList, commandList);
- QFETCH(QModelIndexList, expectedList);
- int lastCommand = 0;
- // do selections
- for (int i = 0; i<commandList.count(); ++i) {
- if (useRanges) {
- selection->select(QItemSelection(indexList.at(2*i), indexList.at(2*i+1)),
- (QItemSelectionModel::SelectionFlags)commandList.at(i));
- } else {
- selection->select(indexList.at(i),
- (QItemSelectionModel::SelectionFlags)commandList.at(i));
- }
- lastCommand = commandList.at(i);
- }
- QModelIndexList selectedList = selection->selectedIndexes();
- QVERIFY(selection->hasSelection()!=selectedList.isEmpty());
- // debug output
- // for (int i=0; i<selectedList.count(); ++i)
- // qDebug(QString("selected (%1, %2)")
- // .arg(selectedList.at(i).row())
- // .arg(selectedList.at(i).column()));
- // test that the number of indices are as expected
- QVERIFY2(selectedList.count() == expectedList.count(),
- QString("expected indices: %1 actual indices: %2")
- .arg(expectedList.count())
- .arg(selectedList.count()).toLatin1());
- // test existence of each index
- for (int i=0; i<expectedList.count(); ++i) {
- QVERIFY2(selectedList.contains(expectedList.at(i)),
- QString("expected index(%1, %2) not found in selectedIndexes()")
- .arg(expectedList.at(i).row())
- .arg(expectedList.at(i).column()).toLatin1());
- }
- // test that isSelected agrees
- for (int i=0; i<indexList.count(); ++i) {
- QModelIndex idx = indexList.at(i);
- QVERIFY2(selection->isSelected(idx) == selectedList.contains(idx),
- QString("isSelected(index: %1, %2) does not match selectedIndexes()")
- .arg(idx.row())
- .arg(idx.column()).toLatin1());
- }
- //for now we assume Rows/Columns flag is the same for all commands, therefore we just check lastCommand
- // test that isRowSelected agrees
- if (lastCommand & QItemSelectionModel::Rows) {
- for (int i=0; i<selectedList.count(); ++i)
- QVERIFY2(selection->isRowSelected(selectedList.at(i).row(),
- model->parent(selectedList.at(i))),
- QString("isRowSelected(row: %1) does not match selectedIndexes()")
- .arg(selectedList.at(i).row()).toLatin1());
- }
- // test that isColumnSelected agrees
- if (lastC…