tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <QtGui/QtGui>
       
    46 
       
    47 //TESTED_CLASS=
       
    48 //TESTED_FILES=
       
    49 
       
    50 class tst_QItemSelectionModel : public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 
       
    54 public:
       
    55     tst_QItemSelectionModel();
       
    56     virtual ~tst_QItemSelectionModel();
       
    57 
       
    58 
       
    59 public slots:
       
    60     void initTestCase();
       
    61     void cleanupTestCase();
       
    62     void init();
       
    63 private slots:
       
    64     void clear_data();
       
    65     void clear();
       
    66     void clearAndSelect();
       
    67     void toggleSelection();
       
    68     void select_data();
       
    69     void select();
       
    70     void persistentselections_data();
       
    71     void persistentselections();
       
    72     void resetModel();
       
    73     void removeRows_data();
       
    74     void removeRows();
       
    75     void removeColumns_data();
       
    76     void removeColumns();
       
    77     void modelLayoutChanged_data();
       
    78     void modelLayoutChanged();
       
    79     void selectedRows_data();
       
    80     void selectedRows();
       
    81     void selectedColumns_data();
       
    82     void selectedColumns();
       
    83     void setCurrentIndex();
       
    84     void splitOnInsert();
       
    85     void task196285_rowIntersectsSelection();
       
    86     void unselectable();
       
    87     void task220420_selectedIndexes();
       
    88     void task240734_layoutChanged();
       
    89     void merge_data();
       
    90     void merge();
       
    91     void task119433_isRowSelected();
       
    92     void task252069_rowIntersectsSelection();
       
    93     void task232634_childrenDeselectionSignal();
       
    94     void task260134_layoutChangedWithAllSelected();
       
    95 
       
    96 private:
       
    97     QAbstractItemModel *model;
       
    98     QItemSelectionModel *selection;
       
    99 };
       
   100 
       
   101 QDataStream &operator<<(QDataStream &, const QModelIndex &);
       
   102 QDataStream &operator>>(QDataStream &, QModelIndex &);
       
   103 QDataStream &operator<<(QDataStream &, const QModelIndexList &);
       
   104 QDataStream &operator>>(QDataStream &, QModelIndexList &);
       
   105 
       
   106 typedef QList<int> IntList;
       
   107 typedef QPair<int, int> IntPair;
       
   108 typedef QList<IntPair> PairList;
       
   109 
       
   110 
       
   111 Q_DECLARE_METATYPE(PairList)
       
   112 Q_DECLARE_METATYPE(QModelIndex)
       
   113 Q_DECLARE_METATYPE(QModelIndexList)
       
   114 Q_DECLARE_METATYPE(IntList)
       
   115 Q_DECLARE_METATYPE(QItemSelection)
       
   116 
       
   117 class QStreamHelper: public QAbstractItemModel
       
   118 {
       
   119 public:
       
   120     QStreamHelper() {}
       
   121     static QModelIndex create(int row = -1, int column = -1, void *data = 0)
       
   122     {
       
   123         QStreamHelper helper;
       
   124         return helper.QAbstractItemModel::createIndex(row, column, data);
       
   125     }
       
   126 
       
   127     QModelIndex index(int, int, const QModelIndex&) const
       
   128         { return QModelIndex(); }
       
   129     QModelIndex parent(const QModelIndex&) const
       
   130         { return QModelIndex(); }
       
   131     int rowCount(const QModelIndex & = QModelIndex()) const
       
   132         { return 0; }
       
   133     int columnCount(const QModelIndex & = QModelIndex()) const
       
   134         { return 0; }
       
   135     QVariant data(const QModelIndex &, int = Qt::DisplayRole) const
       
   136         { return QVariant(); }
       
   137     bool hasChildren(const QModelIndex &) const
       
   138         { return false; }
       
   139 };
       
   140 
       
   141 QDataStream &operator<<(QDataStream &s, const QModelIndex &input)
       
   142 {
       
   143     s << input.row()
       
   144       << input.column()
       
   145       << reinterpret_cast<qlonglong>(input.internalPointer());
       
   146     return s;
       
   147 }
       
   148 
       
   149 QDataStream &operator>>(QDataStream &s, QModelIndex &output)
       
   150 {
       
   151     int r, c;
       
   152     qlonglong ptr;
       
   153     s >> r;
       
   154     s >> c;
       
   155     s >> ptr;
       
   156     output = QStreamHelper::create(r, c, reinterpret_cast<void *>(ptr));
       
   157     return s;
       
   158 }
       
   159 
       
   160 QDataStream &operator<<(QDataStream &s, const QModelIndexList &input)
       
   161 {
       
   162     s << input.count();
       
   163     for (int i=0; i<input.count(); ++i)
       
   164         s << input.at(i);
       
   165     return s;
       
   166 }
       
   167 
       
   168 QDataStream &operator>>(QDataStream &s, QModelIndexList &output)
       
   169 {
       
   170     QModelIndex tmpIndex;
       
   171     int count;
       
   172     s >> count;
       
   173     for (int i=0; i<count; ++i) {
       
   174         s >> tmpIndex;
       
   175         output << tmpIndex;
       
   176     }
       
   177     return s;
       
   178 }
       
   179 
       
   180 tst_QItemSelectionModel::tst_QItemSelectionModel() : model(0), selection(0)
       
   181 {
       
   182 }
       
   183 
       
   184 tst_QItemSelectionModel::~tst_QItemSelectionModel()
       
   185 {
       
   186 }
       
   187 
       
   188 /*
       
   189   This test usually uses a model with a 5x5 table
       
   190   -------------------------------------------
       
   191   |  0,0  |  0,1    |  0,2  |  0,3    |  0,4  |
       
   192   -------------------------------------------
       
   193   |  1,0  |  1,1    |  1,2  |  1,3    |  1,4  |
       
   194   -------------------------------------------
       
   195   |  2,0  |  2,1    |  2,2  |  2,3    |  2,4  |
       
   196   -------------------------------------------
       
   197   |  3,0  |  3,1    |  3,2  |  3,3    |  3,4  |
       
   198   -------------------------------------------
       
   199   |  4,0  |  4,1    |  4,2  |  4,3    |  4,4  |
       
   200   -------------------------------------------
       
   201 
       
   202   ...that for each row has a children in a new 5x5 table ad infinitum.
       
   203 
       
   204 */
       
   205 void tst_QItemSelectionModel::initTestCase()
       
   206 {
       
   207     qRegisterMetaType<QItemSelection>("QItemSelection");
       
   208 
       
   209     model = new QStandardItemModel(5, 5);
       
   210     QModelIndex parent = model->index(0, 0, QModelIndex());
       
   211     model->insertRows(0, 5, parent);
       
   212     model->insertColumns(0, 5, parent);
       
   213     selection  = new QItemSelectionModel(model);
       
   214 }
       
   215 
       
   216 void tst_QItemSelectionModel::cleanupTestCase()
       
   217 {
       
   218     delete selection;
       
   219     delete model;
       
   220 }
       
   221 
       
   222 void tst_QItemSelectionModel::init()
       
   223 {
       
   224     selection->clear();
       
   225     while (model->rowCount(QModelIndex()) > 5)
       
   226         model->removeRow(0, QModelIndex());
       
   227     while (model->rowCount(QModelIndex()) < 5)
       
   228         model->insertRow(0, QModelIndex());
       
   229 }
       
   230 
       
   231 void tst_QItemSelectionModel::clear_data()
       
   232 {
       
   233     QTest::addColumn<QModelIndexList>("indexList");
       
   234     QTest::addColumn<IntList>("commandList");
       
   235     {
       
   236         QModelIndexList index;
       
   237         IntList command;
       
   238         index << model->index(0, 0, QModelIndex());
       
   239         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   240         index << model->index(1, 0, QModelIndex());
       
   241         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   242         QTest::newRow("(0, 0) and (1, 0): Select|Rows")
       
   243             << index
       
   244             << command;
       
   245     }
       
   246     {
       
   247         QModelIndexList index;
       
   248         IntList command;
       
   249         index << model->index(0, 0, QModelIndex());
       
   250         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   251         index << model->index(0, 1, QModelIndex());
       
   252         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   253         QTest::newRow("(0, 0) and (1, 0): Select|Columns")
       
   254             << index
       
   255             << command;
       
   256     }
       
   257     {
       
   258         QModelIndexList index;
       
   259         IntList command;
       
   260         index << model->index(0, 0, QModelIndex());
       
   261         command << QItemSelectionModel::Select;
       
   262         index << model->index(1, 1, QModelIndex());
       
   263         command << QItemSelectionModel::Select;
       
   264         index << model->index(2, 2, QModelIndex());
       
   265         command << QItemSelectionModel::SelectCurrent;
       
   266         QTest::newRow("(0, 0), (1, 1) and (2, 2): Select, Select, SelectCurrent")
       
   267             << index
       
   268             << command;
       
   269     }
       
   270     {
       
   271         QModelIndexList index;
       
   272         IntList command;
       
   273         index << model->index(0, 0, QModelIndex());
       
   274         command << QItemSelectionModel::Select;
       
   275         index << model->index(1, 1, QModelIndex());
       
   276         command << QItemSelectionModel::Select;
       
   277         index << model->index(1, 1, QModelIndex());
       
   278         command << QItemSelectionModel::Toggle;
       
   279         QTest::newRow("(0, 0), (1, 1) and (1, 1): Select, Select, Toggle")
       
   280             << index
       
   281             << command;
       
   282     }
       
   283     {
       
   284         QModelIndexList index;
       
   285         IntList command;
       
   286         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   287         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   288         QTest::newRow("child (0, 0) of (0, 0): Select|Rows")
       
   289             << index
       
   290             << command;
       
   291     }
       
   292 }
       
   293 
       
   294 void tst_QItemSelectionModel::clear()
       
   295 {
       
   296     QFETCH(QModelIndexList, indexList);
       
   297     QFETCH(IntList, commandList);
       
   298 
       
   299     // do selections
       
   300     for (int i=0; i<indexList.count(); ++i) {
       
   301         selection->select(indexList.at(i), (QItemSelectionModel::SelectionFlags)commandList.at(i));
       
   302     }
       
   303     // test that we have selected items
       
   304     QVERIFY(!selection->selectedIndexes().isEmpty());
       
   305     selection->clear();
       
   306     // test that they were all cleared
       
   307     QVERIFY(selection->selectedIndexes().isEmpty());
       
   308 }
       
   309 
       
   310 void tst_QItemSelectionModel::clearAndSelect()
       
   311 {
       
   312     // populate selectionmodel
       
   313     selection->select(model->index(1, 1, QModelIndex()), QItemSelectionModel::Select);
       
   314     QCOMPARE(selection->selectedIndexes().count(), 1);
       
   315 	QVERIFY(selection->hasSelection());
       
   316 
       
   317     // ClearAndSelect with empty selection
       
   318     QItemSelection emptySelection;
       
   319     selection->select(emptySelection, QItemSelectionModel::ClearAndSelect);
       
   320 
       
   321     // verify the selectionmodel is empty
       
   322     QVERIFY(selection->selectedIndexes().isEmpty());
       
   323 	QVERIFY(selection->hasSelection()==false);
       
   324 }
       
   325 
       
   326 void tst_QItemSelectionModel::toggleSelection()
       
   327 {
       
   328 	//test the toggle selection and checks whether selectedIndex
       
   329 	//and hasSelection returns the correct value
       
   330 
       
   331 	selection->clearSelection();
       
   332     QCOMPARE(selection->selectedIndexes().count(), 0);
       
   333 	QVERIFY(selection->hasSelection()==false);
       
   334 
       
   335 	QModelIndex index=model->index(1, 1, QModelIndex());
       
   336     // populate selectionmodel
       
   337     selection->select(index, QItemSelectionModel::Toggle);
       
   338     QCOMPARE(selection->selectedIndexes().count(), 1);
       
   339 	QVERIFY(selection->hasSelection()==true);
       
   340 
       
   341     selection->select(index, QItemSelectionModel::Toggle);
       
   342     QCOMPARE(selection->selectedIndexes().count(), 0);
       
   343 	QVERIFY(selection->hasSelection()==false);
       
   344 
       
   345     // populate selectionmodel with rows
       
   346     selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
       
   347     QCOMPARE(selection->selectedIndexes().count(), model->columnCount());
       
   348 	QVERIFY(selection->hasSelection()==true);
       
   349 
       
   350     selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
       
   351     QCOMPARE(selection->selectedIndexes().count(), 0);
       
   352 	QVERIFY(selection->hasSelection()==false);
       
   353 
       
   354 }
       
   355 
       
   356 
       
   357 void tst_QItemSelectionModel::select_data()
       
   358 {
       
   359     QTest::addColumn<QModelIndexList>("indexList");
       
   360     QTest::addColumn<bool>("useRanges");
       
   361     QTest::addColumn<IntList>("commandList");
       
   362     QTest::addColumn<QModelIndexList>("expectedList");
       
   363 
       
   364     {
       
   365         QModelIndexList index;
       
   366         QModelIndexList expected;
       
   367         IntList command;
       
   368         index << model->index(0, 0, QModelIndex());
       
   369         command << QItemSelectionModel::Select;
       
   370         expected << model->index(0, 0, QModelIndex());
       
   371         QTest::newRow("(0, 0): Select")
       
   372             << index
       
   373             << false
       
   374             << command
       
   375             << expected;
       
   376     }
       
   377     {
       
   378         QModelIndexList index;
       
   379         QModelIndexList expected;
       
   380         IntList command;
       
   381         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   382         command << QItemSelectionModel::Select;
       
   383         expected << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   384         QTest::newRow("child (0, 0) of (0, 0): Select")
       
   385             << index
       
   386             << false
       
   387             << command
       
   388             << expected;
       
   389     }
       
   390     {
       
   391         QModelIndexList index;
       
   392         QModelIndexList expected;
       
   393         IntList command;
       
   394         index << model->index(0, 0, QModelIndex());
       
   395         command << QItemSelectionModel::Deselect;
       
   396         QTest::newRow("(0, 0): Deselect")
       
   397             << index
       
   398             << false
       
   399             << command
       
   400             << expected;
       
   401     }
       
   402     {
       
   403         QModelIndexList index;
       
   404         QModelIndexList expected;
       
   405         IntList command;
       
   406         index << model->index(0, 0, QModelIndex());
       
   407         command << QItemSelectionModel::Toggle;
       
   408         expected << model->index(0, 0, QModelIndex());
       
   409         QTest::newRow("(0, 0): Toggle")
       
   410             << index
       
   411             << false
       
   412             << command
       
   413             << expected;
       
   414     }
       
   415     {
       
   416         QModelIndexList index;
       
   417         QModelIndexList expected;
       
   418         IntList command;
       
   419         index << model->index(0, 0, QModelIndex());
       
   420         command << QItemSelectionModel::Select;
       
   421         index << model->index(0, 0, QModelIndex());
       
   422         command << QItemSelectionModel::Toggle;
       
   423         QTest::newRow("(0, 0) and (0, 0): Select and Toggle")
       
   424             << index
       
   425             << false
       
   426             << command
       
   427             << expected;
       
   428     }
       
   429     {
       
   430         QModelIndexList index;
       
   431         QModelIndexList expected;
       
   432         IntList command;
       
   433         index << model->index(0, 0, QModelIndex());
       
   434         command << QItemSelectionModel::Select;
       
   435         index << model->index(0, 0, QModelIndex());
       
   436         command << QItemSelectionModel::Deselect;
       
   437         QTest::newRow("(0, 0) and (0, 0): Select and Deselect")
       
   438             << index
       
   439             << false
       
   440             << command
       
   441             << expected;
       
   442     }
       
   443     {
       
   444         QModelIndexList index;
       
   445         QModelIndexList expected;
       
   446         IntList command;
       
   447         index << model->index(0, 0, QModelIndex());
       
   448         command << QItemSelectionModel::Select;
       
   449         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   450         command << QItemSelectionModel::ClearAndSelect;
       
   451         expected << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   452         QTest::newRow("(0, 0) and child (0, 0) of (0, 0): Select and ClearAndSelect")
       
   453             << index
       
   454             << false
       
   455             << command
       
   456             << expected;
       
   457     }
       
   458     {
       
   459         QModelIndexList index;
       
   460         QModelIndexList expected;
       
   461         IntList command;
       
   462         index << model->index(0, 0, QModelIndex());
       
   463         index << model->index(4, 0, QModelIndex());
       
   464         command << QItemSelectionModel::Select;
       
   465         index << model->index(0, 1, QModelIndex());
       
   466         index << model->index(4, 1, QModelIndex());
       
   467         command << QItemSelectionModel::Select;
       
   468         index << model->index(0, 0, QModelIndex());
       
   469         index << model->index(4, 1, QModelIndex());
       
   470         command << QItemSelectionModel::Deselect;
       
   471         QTest::newRow("(0, 0 to 4, 0) and (0, 1 to 4, 1) and (0, 0 to 4, 1): Select and Select and Deselect")
       
   472             << index
       
   473             << true
       
   474             << command
       
   475             << expected;
       
   476     }
       
   477     {
       
   478         QModelIndexList index;
       
   479         QModelIndexList expected;
       
   480         IntList command;
       
   481         index << model->index(0, 0, QModelIndex());
       
   482         command << QItemSelectionModel::Select;
       
   483         index << model->index(4, 4, QModelIndex());
       
   484         command << QItemSelectionModel::Select;
       
   485         expected << model->index(0, 0, QModelIndex()) << model->index(4, 4, QModelIndex());
       
   486         QTest::newRow("(0, 0) and (4, 4): Select")
       
   487             << index
       
   488             << false
       
   489             << command
       
   490             << expected;
       
   491     }
       
   492     {
       
   493         QModelIndexList index;
       
   494         QModelIndexList expected;
       
   495         IntList command;
       
   496         index << model->index(0, 0, QModelIndex());
       
   497         command << QItemSelectionModel::Select;
       
   498         index << model->index(4, 4, QModelIndex());
       
   499         command << QItemSelectionModel::ClearAndSelect;
       
   500         expected << model->index(4, 4, QModelIndex());
       
   501         QTest::newRow("(0, 0) and (4, 4): Select and ClearAndSelect")
       
   502             << index
       
   503             << false
       
   504             << command
       
   505             << expected;
       
   506     }
       
   507     {
       
   508         QModelIndexList index;
       
   509         QModelIndexList expected;
       
   510         IntList command;
       
   511         index << model->index(0, 0, QModelIndex());
       
   512         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   513         index << model->index(4, 4, QModelIndex());
       
   514         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   515         expected << model->index(0, 0, QModelIndex())
       
   516                  << model->index(0, 1, QModelIndex())
       
   517                  << model->index(0, 2, QModelIndex())
       
   518                  << model->index(0, 3, QModelIndex())
       
   519                  << model->index(0, 4, QModelIndex())
       
   520                  << model->index(4, 0, QModelIndex())
       
   521                  << model->index(4, 1, QModelIndex())
       
   522                  << model->index(4, 2, QModelIndex())
       
   523                  << model->index(4, 3, QModelIndex())
       
   524                  << model->index(4, 4, QModelIndex());
       
   525         QTest::newRow("(0, 0) and (4, 4): Select|Rows")
       
   526             << index
       
   527             << false
       
   528             << command
       
   529             << expected;
       
   530     }
       
   531     {
       
   532         QModelIndexList index;
       
   533         QModelIndexList expected;
       
   534         IntList command;
       
   535         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   536         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   537         index << model->index(4, 4, model->index(0, 0, QModelIndex()));
       
   538         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   539         QModelIndex parent = model->index(0, 0, QModelIndex());
       
   540         expected << model->index(0, 0, parent)
       
   541                  << model->index(0, 1, parent)
       
   542                  << model->index(0, 2, parent)
       
   543                  << model->index(0, 3, parent)
       
   544                  << model->index(0, 4, parent)
       
   545                  << model->index(4, 0, parent)
       
   546                  << model->index(4, 1, parent)
       
   547                  << model->index(4, 2, parent)
       
   548                  << model->index(4, 3, parent)
       
   549                  << model->index(4, 4, parent);
       
   550         QTest::newRow("child (0, 0) and (4, 4) of (0, 0): Select|Rows")
       
   551             << index
       
   552             << false
       
   553             << command
       
   554             << expected;
       
   555     }
       
   556     {
       
   557         QModelIndexList index;
       
   558         QModelIndexList expected;
       
   559         IntList command;
       
   560         index << model->index(0, 0, QModelIndex());
       
   561         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   562         index << model->index(4, 4, QModelIndex());
       
   563         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   564         expected << model->index(0, 0, QModelIndex())
       
   565                  << model->index(1, 0, QModelIndex())
       
   566                  << model->index(2, 0, QModelIndex())
       
   567                  << model->index(3, 0, QModelIndex())
       
   568                  << model->index(4, 0, QModelIndex())
       
   569                  << model->index(0, 4, QModelIndex())
       
   570                  << model->index(1, 4, QModelIndex())
       
   571                  << model->index(2, 4, QModelIndex())
       
   572                  << model->index(3, 4, QModelIndex())
       
   573                  << model->index(4, 4, QModelIndex());
       
   574         QTest::newRow("(0, 0) and (4, 4): Select|Columns")
       
   575             << index
       
   576             << false
       
   577             << command
       
   578             << expected;
       
   579     }
       
   580     {
       
   581         QModelIndexList index;
       
   582         QModelIndexList expected;
       
   583         IntList command;
       
   584         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   585         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   586         index << model->index(4, 4, model->index(0, 0, QModelIndex()));
       
   587         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   588         expected << model->index(0, 0, model->index(0, 0, QModelIndex()))
       
   589                  << model->index(1, 0, model->index(0, 0, QModelIndex()))
       
   590                  << model->index(2, 0, model->index(0, 0, QModelIndex()))
       
   591                  << model->index(3, 0, model->index(0, 0, QModelIndex()))
       
   592                  << model->index(4, 0, model->index(0, 0, QModelIndex()))
       
   593                  << model->index(0, 4, model->index(0, 0, QModelIndex()))
       
   594                  << model->index(1, 4, model->index(0, 0, QModelIndex()))
       
   595                  << model->index(2, 4, model->index(0, 0, QModelIndex()))
       
   596                  << model->index(3, 4, model->index(0, 0, QModelIndex()))
       
   597                  << model->index(4, 4, model->index(0, 0, QModelIndex()));
       
   598         QTest::newRow("child (0, 0) and (4, 4) of (0, 0): Select|Columns")
       
   599             << index
       
   600             << false
       
   601             << command
       
   602             << expected;
       
   603     }
       
   604     {
       
   605         QModelIndexList index;
       
   606         QModelIndexList expected;
       
   607         IntList command;
       
   608         index << model->index(0, 0, QModelIndex());
       
   609         index << model->index(4, 0, QModelIndex());
       
   610         command << QItemSelectionModel::Select;
       
   611         expected << model->index(0, 0, QModelIndex())
       
   612                  << model->index(1, 0, QModelIndex())
       
   613                  << model->index(2, 0, QModelIndex())
       
   614                  << model->index(3, 0, QModelIndex())
       
   615                  << model->index(4, 0, QModelIndex());
       
   616         QTest::newRow("(0, 0 to 4, 0): Select")
       
   617             << index
       
   618             << true
       
   619             << command
       
   620             << expected;
       
   621     }
       
   622     /* ### FAILS
       
   623     {
       
   624         QModelIndexList index;
       
   625         QModelIndexList expected;
       
   626         IntList command;
       
   627         index << model->index(0, 0, QModelIndex());
       
   628         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   629         command << QItemSelectionModel::Select;
       
   630         QTest::newRow("(0, 0 to child 0, 0): Select")
       
   631             << index
       
   632             << true
       
   633             << command
       
   634             << expected;
       
   635     }
       
   636     */
       
   637     {
       
   638         QModelIndexList index;
       
   639         QModelIndexList expected;
       
   640         IntList command;
       
   641         index << model->index(0, 0, model->index(0, 0, QModelIndex()));
       
   642         index << model->index(0, 0, model->index(1, 0, QModelIndex()));
       
   643         command << QItemSelectionModel::Select;
       
   644         QTest::newRow("child (0, 0) of (0, 0) to child (0, 0) of (1, 0): Select")
       
   645             << index
       
   646             << true
       
   647             << command
       
   648             << expected;
       
   649     }
       
   650     {
       
   651         QModelIndexList index;
       
   652         QModelIndexList expected;
       
   653         IntList command;
       
   654         index << model->index(0, 0, QModelIndex());
       
   655         index << model->index(4, 4, QModelIndex());
       
   656         command << QItemSelectionModel::Select;
       
   657         expected << model->index(0, 0, QModelIndex())
       
   658                  << model->index(0, 1, QModelIndex())
       
   659                  << model->index(0, 2, QModelIndex())
       
   660                  << model->index(0, 3, QModelIndex())
       
   661                  << model->index(0, 4, QModelIndex())
       
   662                  << model->index(1, 0, QModelIndex())
       
   663                  << model->index(1, 1, QModelIndex())
       
   664                  << model->index(1, 2, QModelIndex())
       
   665                  << model->index(1, 3, QModelIndex())
       
   666                  << model->index(1, 4, QModelIndex())
       
   667                  << model->index(2, 0, QModelIndex())
       
   668                  << model->index(2, 1, QModelIndex())
       
   669                  << model->index(2, 2, QModelIndex())
       
   670                  << model->index(2, 3, QModelIndex())
       
   671                  << model->index(2, 4, QModelIndex())
       
   672                  << model->index(3, 0, QModelIndex())
       
   673                  << model->index(3, 1, QModelIndex())
       
   674                  << model->index(3, 2, QModelIndex())
       
   675                  << model->index(3, 3, QModelIndex())
       
   676                  << model->index(3, 4, QModelIndex())
       
   677                  << model->index(4, 0, QModelIndex())
       
   678                  << model->index(4, 1, QModelIndex())
       
   679                  << model->index(4, 2, QModelIndex())
       
   680                  << model->index(4, 3, QModelIndex())
       
   681                  << model->index(4, 4, QModelIndex());
       
   682         QTest::newRow("(0, 0 to 4, 4): Select")
       
   683             << index
       
   684             << true
       
   685             << command
       
   686             << expected;
       
   687     }
       
   688     {
       
   689         QModelIndexList index;
       
   690         QModelIndexList expected;
       
   691         IntList command;
       
   692         index << model->index(0, 0, QModelIndex());
       
   693         index << model->index(4, 0, QModelIndex());
       
   694         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   695         expected << model->index(0, 0, QModelIndex())
       
   696                  << model->index(0, 1, QModelIndex())
       
   697                  << model->index(0, 2, QModelIndex())
       
   698                  << model->index(0, 3, QModelIndex())
       
   699                  << model->index(0, 4, QModelIndex())
       
   700                  << model->index(1, 0, QModelIndex())
       
   701                  << model->index(1, 1, QModelIndex())
       
   702                  << model->index(1, 2, QModelIndex())
       
   703                  << model->index(1, 3, QModelIndex())
       
   704                  << model->index(1, 4, QModelIndex())
       
   705                  << model->index(2, 0, QModelIndex())
       
   706                  << model->index(2, 1, QModelIndex())
       
   707                  << model->index(2, 2, QModelIndex())
       
   708                  << model->index(2, 3, QModelIndex())
       
   709                  << model->index(2, 4, QModelIndex())
       
   710                  << model->index(3, 0, QModelIndex())
       
   711                  << model->index(3, 1, QModelIndex())
       
   712                  << model->index(3, 2, QModelIndex())
       
   713                  << model->index(3, 3, QModelIndex())
       
   714                  << model->index(3, 4, QModelIndex())
       
   715                  << model->index(4, 0, QModelIndex())
       
   716                  << model->index(4, 1, QModelIndex())
       
   717                  << model->index(4, 2, QModelIndex())
       
   718                  << model->index(4, 3, QModelIndex())
       
   719                  << model->index(4, 4, QModelIndex());
       
   720         QTest::newRow("(0, 0 to 4, 0): Select|Rows")
       
   721             << index
       
   722             << true
       
   723             << command
       
   724             << expected;
       
   725     }
       
   726     {
       
   727         QModelIndexList index;
       
   728         QModelIndexList expected;
       
   729         IntList command;
       
   730         index << model->index(0, 0, QModelIndex());
       
   731         index << model->index(0, 4, QModelIndex());
       
   732         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   733         expected << model->index(0, 0, QModelIndex())
       
   734                  << model->index(0, 1, QModelIndex())
       
   735                  << model->index(0, 2, QModelIndex())
       
   736                  << model->index(0, 3, QModelIndex())
       
   737                  << model->index(0, 4, QModelIndex())
       
   738                  << model->index(1, 0, QModelIndex())
       
   739                  << model->index(1, 1, QModelIndex())
       
   740                  << model->index(1, 2, QModelIndex())
       
   741                  << model->index(1, 3, QModelIndex())
       
   742                  << model->index(1, 4, QModelIndex())
       
   743                  << model->index(2, 0, QModelIndex())
       
   744                  << model->index(2, 1, QModelIndex())
       
   745                  << model->index(2, 2, QModelIndex())
       
   746                  << model->index(2, 3, QModelIndex())
       
   747                  << model->index(2, 4, QModelIndex())
       
   748                  << model->index(3, 0, QModelIndex())
       
   749                  << model->index(3, 1, QModelIndex())
       
   750                  << model->index(3, 2, QModelIndex())
       
   751                  << model->index(3, 3, QModelIndex())
       
   752                  << model->index(3, 4, QModelIndex())
       
   753                  << model->index(4, 0, QModelIndex())
       
   754                  << model->index(4, 1, QModelIndex())
       
   755                  << model->index(4, 2, QModelIndex())
       
   756                  << model->index(4, 3, QModelIndex())
       
   757                  << model->index(4, 4, QModelIndex());
       
   758         QTest::newRow("(0, 0 to 0, 4): Select|Columns")
       
   759             << index
       
   760             << true
       
   761             << command
       
   762             << expected;
       
   763     }
       
   764     {
       
   765         QModelIndexList index;
       
   766         QModelIndexList expected;
       
   767         IntList command;
       
   768         index << model->index(0, 0, QModelIndex());
       
   769         index << model->index(4, 4, QModelIndex());
       
   770         command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   771         expected << model->index(0, 0, QModelIndex())
       
   772                  << model->index(0, 1, QModelIndex())
       
   773                  << model->index(0, 2, QModelIndex())
       
   774                  << model->index(0, 3, QModelIndex())
       
   775                  << model->index(0, 4, QModelIndex())
       
   776                  << model->index(1, 0, QModelIndex())
       
   777                  << model->index(1, 1, QModelIndex())
       
   778                  << model->index(1, 2, QModelIndex())
       
   779                  << model->index(1, 3, QModelIndex())
       
   780                  << model->index(1, 4, QModelIndex())
       
   781                  << model->index(2, 0, QModelIndex())
       
   782                  << model->index(2, 1, QModelIndex())
       
   783                  << model->index(2, 2, QModelIndex())
       
   784                  << model->index(2, 3, QModelIndex())
       
   785                  << model->index(2, 4, QModelIndex())
       
   786                  << model->index(3, 0, QModelIndex())
       
   787                  << model->index(3, 1, QModelIndex())
       
   788                  << model->index(3, 2, QModelIndex())
       
   789                  << model->index(3, 3, QModelIndex())
       
   790                  << model->index(3, 4, QModelIndex())
       
   791                  << model->index(4, 0, QModelIndex())
       
   792                  << model->index(4, 1, QModelIndex())
       
   793                  << model->index(4, 2, QModelIndex())
       
   794                  << model->index(4, 3, QModelIndex())
       
   795                  << model->index(4, 4, QModelIndex());
       
   796         QTest::newRow("(0, 0 to 4, 4): Select|Rows")
       
   797             << index
       
   798             << true
       
   799             << command
       
   800             << expected;
       
   801     }
       
   802     {
       
   803         QModelIndexList index;
       
   804         QModelIndexList expected;
       
   805         IntList command;
       
   806         index << model->index(0, 0, QModelIndex());
       
   807         index << model->index(4, 4, QModelIndex());
       
   808         command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
   809         expected << model->index(0, 0, QModelIndex())
       
   810                  << model->index(0, 1, QModelIndex())
       
   811                  << model->index(0, 2, QModelIndex())
       
   812                  << model->index(0, 3, QModelIndex())
       
   813                  << model->index(0, 4, QModelIndex())
       
   814                  << model->index(1, 0, QModelIndex())
       
   815                  << model->index(1, 1, QModelIndex())
       
   816                  << model->index(1, 2, QModelIndex())
       
   817                  << model->index(1, 3, QModelIndex())
       
   818                  << model->index(1, 4, QModelIndex())
       
   819                  << model->index(2, 0, QModelIndex())
       
   820                  << model->index(2, 1, QModelIndex())
       
   821                  << model->index(2, 2, QModelIndex())
       
   822                  << model->index(2, 3, QModelIndex())
       
   823                  << model->index(2, 4, QModelIndex())
       
   824                  << model->index(3, 0, QModelIndex())
       
   825                  << model->index(3, 1, QModelIndex())
       
   826                  << model->index(3, 2, QModelIndex())
       
   827                  << model->index(3, 3, QModelIndex())
       
   828                  << model->index(3, 4, QModelIndex())
       
   829                  << model->index(4, 0, QModelIndex())
       
   830                  << model->index(4, 1, QModelIndex())
       
   831                  << model->index(4, 2, QModelIndex())
       
   832                  << model->index(4, 3, QModelIndex())
       
   833                  << model->index(4, 4, QModelIndex());
       
   834         QTest::newRow("(0, 0 to 4, 4): Select|Columns")
       
   835             << index
       
   836             << true
       
   837             << command
       
   838             << expected;
       
   839     }
       
   840     {
       
   841         QModelIndexList index;
       
   842         QModelIndexList expected;
       
   843         IntList command;
       
   844         index << model->index(0, 2, QModelIndex());
       
   845         index << model->index(4, 2, QModelIndex());
       
   846         command << QItemSelectionModel::Select;
       
   847         index << model->index(2, 0, QModelIndex());
       
   848         index << model->index(2, 4, QModelIndex());
       
   849         command << QItemSelectionModel::Select;
       
   850         expected << model->index(0, 2, QModelIndex())
       
   851                  << model->index(1, 2, QModelIndex())
       
   852                  << model->index(2, 2, QModelIndex())
       
   853                  << model->index(3, 2, QModelIndex())
       
   854                  << model->index(4, 2, QModelIndex())
       
   855                  << model->index(2, 0, QModelIndex())
       
   856                  << model->index(2, 1, QModelIndex())
       
   857                  << model->index(2, 3, QModelIndex())
       
   858                  << model->index(2, 4, QModelIndex());
       
   859         QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select")
       
   860             << index
       
   861             << true
       
   862             << command
       
   863             << expected;
       
   864     }
       
   865     {
       
   866         QModelIndexList index;
       
   867         QModelIndexList expected;
       
   868         IntList command;
       
   869         index << model->index(0, 2, QModelIndex());
       
   870         index << model->index(4, 2, QModelIndex());
       
   871         command << QItemSelectionModel::Select;
       
   872         index << model->index(2, 0, QModelIndex());
       
   873         index << model->index(2, 4, QModelIndex());
       
   874         command << QItemSelectionModel::SelectCurrent;
       
   875         expected << model->index(2, 0, QModelIndex())
       
   876                  << model->index(2, 1, QModelIndex())
       
   877                  << model->index(2, 2, QModelIndex())
       
   878                  << model->index(2, 3, QModelIndex())
       
   879                  << model->index(2, 4, QModelIndex());
       
   880         QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and SelectCurrent")
       
   881             << index
       
   882             << true
       
   883             << command
       
   884             << expected;
       
   885     }
       
   886     {
       
   887         QModelIndexList index;
       
   888         QModelIndexList expected;
       
   889         IntList command;
       
   890         index << model->index(0, 2, QModelIndex());
       
   891         index << model->index(4, 2, QModelIndex());
       
   892         command << QItemSelectionModel::Select;
       
   893         index << model->index(2, 0, QModelIndex());
       
   894         index << model->index(2, 4, QModelIndex());
       
   895         command << QItemSelectionModel::Toggle;
       
   896         expected << model->index(0, 2, QModelIndex())
       
   897                  << model->index(1, 2, QModelIndex())
       
   898                  << model->index(3, 2, QModelIndex())
       
   899                  << model->index(4, 2, QModelIndex())
       
   900                  << model->index(2, 0, QModelIndex())
       
   901                  << model->index(2, 1, QModelIndex())
       
   902                  << model->index(2, 3, QModelIndex())
       
   903                  << model->index(2, 4, QModelIndex());
       
   904         QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and Toggle")
       
   905             << index
       
   906             << true
       
   907             << command
       
   908             << expected;
       
   909     }
       
   910     {
       
   911         QModelIndexList index;
       
   912         QModelIndexList expected;
       
   913         IntList command;
       
   914         index << model->index(0, 2, QModelIndex());
       
   915         index << model->index(4, 2, QModelIndex());
       
   916         command << QItemSelectionModel::Select;
       
   917         index << model->index(2, 0, QModelIndex());
       
   918         index << model->index(2, 4, QModelIndex());
       
   919         command << QItemSelectionModel::Deselect;
       
   920         expected << model->index(0, 2, QModelIndex())
       
   921                  << model->index(1, 2, QModelIndex())
       
   922                  << model->index(3, 2, QModelIndex())
       
   923                  << model->index(4, 2, QModelIndex());
       
   924         QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and Deselect")
       
   925             << index
       
   926             << true
       
   927             << command
       
   928             << expected;
       
   929     }
       
   930 
       
   931     {
       
   932         QModelIndexList index;
       
   933         QModelIndexList expected;
       
   934         IntList command;
       
   935         index << model->index(0, 0, QModelIndex());
       
   936         index << model->index(2, 2, QModelIndex());
       
   937         command << QItemSelectionModel::Select;
       
   938 
       
   939         index << model->index(0, 0, QModelIndex());
       
   940         index << model->index(0, 0, QModelIndex());
       
   941         command << QItemSelectionModel::Toggle;
       
   942 
       
   943         expected << model->index(0, 1, QModelIndex())
       
   944                  << model->index(0, 2, QModelIndex())
       
   945                  << model->index(1, 0, QModelIndex())
       
   946                  << model->index(1, 1, QModelIndex())
       
   947                  << model->index(1, 2, QModelIndex())
       
   948                  << model->index(2, 0, QModelIndex())
       
   949                  << model->index(2, 1, QModelIndex())
       
   950                  << model->index(2, 2, QModelIndex());
       
   951 
       
   952         QTest::newRow("(0, 0 to 2, 2) and (0, 0 to 0, 0): Select and Toggle at selection boundary")
       
   953             << index
       
   954             << true
       
   955             << command
       
   956             << expected;
       
   957     }
       
   958 
       
   959     {
       
   960         QModelIndexList index;
       
   961         QModelIndexList expected;
       
   962         IntList command;
       
   963         index << model->index(0, 0, QModelIndex());
       
   964         index << model->index(2, 2, QModelIndex());
       
   965         command << QItemSelectionModel::Select;
       
   966 
       
   967         index << model->index(0, 1, QModelIndex());
       
   968         index << model->index(0, 1, QModelIndex());
       
   969         command << QItemSelectionModel::Toggle;
       
   970 
       
   971         expected << model->index(0, 0, QModelIndex())
       
   972                  << model->index(0, 2, QModelIndex())
       
   973                  << model->index(1, 0, QModelIndex())
       
   974                  << model->index(1, 1, QModelIndex())
       
   975                  << model->index(1, 2, QModelIndex())
       
   976                  << model->index(2, 0, QModelIndex())
       
   977                  << model->index(2, 1, QModelIndex())
       
   978                  << model->index(2, 2, QModelIndex());
       
   979 
       
   980         QTest::newRow("(0, 0 to 2, 2) and (0, 1 to 0, 1): Select and Toggle at selection boundary")
       
   981             << index
       
   982             << true
       
   983             << command
       
   984             << expected;
       
   985     }
       
   986 
       
   987     {
       
   988         QModelIndexList index;
       
   989         QModelIndexList expected;
       
   990         IntList command;
       
   991         index << model->index(0, 0, QModelIndex());
       
   992         index << model->index(2, 2, QModelIndex());
       
   993         command << QItemSelectionModel::Select;
       
   994 
       
   995         index << model->index(0, 2, QModelIndex());
       
   996         index << model->index(0, 2, QModelIndex());
       
   997         command << QItemSelectionModel::Toggle;
       
   998 
       
   999         expected << model->index(0, 0, QModelIndex())
       
  1000                  << model->index(0, 1, QModelIndex())
       
  1001                  << model->index(1, 0, QModelIndex())
       
  1002                  << model->index(1, 1, QModelIndex())
       
  1003                  << model->index(1, 2, QModelIndex())
       
  1004                  << model->index(2, 0, QModelIndex())
       
  1005                  << model->index(2, 1, QModelIndex())
       
  1006                  << model->index(2, 2, QModelIndex());
       
  1007 
       
  1008         QTest::newRow("(0, 0 to 2, 2) and (0, 2 to 0, 2): Select and Toggle at selection boundary")
       
  1009             << index
       
  1010             << true
       
  1011             << command
       
  1012             << expected;
       
  1013     }
       
  1014 
       
  1015     {
       
  1016         QModelIndexList index;
       
  1017         QModelIndexList expected;
       
  1018         IntList command;
       
  1019         index << model->index(0, 0, QModelIndex());
       
  1020         index << model->index(2, 2, QModelIndex());
       
  1021         command << QItemSelectionModel::Select;
       
  1022 
       
  1023         index << model->index(1, 0, QModelIndex());
       
  1024         index << model->index(1, 0, QModelIndex());
       
  1025         command << QItemSelectionModel::Toggle;
       
  1026 
       
  1027         expected << model->index(0, 0, QModelIndex())
       
  1028                  << model->index(0, 1, QModelIndex())
       
  1029                  << model->index(0, 2, QModelIndex())
       
  1030                  << model->index(1, 1, QModelIndex())
       
  1031                  << model->index(1, 2, QModelIndex())
       
  1032                  << model->index(2, 0, QModelIndex())
       
  1033                  << model->index(2, 1, QModelIndex())
       
  1034                  << model->index(2, 2, QModelIndex());
       
  1035 
       
  1036         QTest::newRow("(0, 0 to 2, 2) and (1, 0 to 1, 0): Select and Toggle at selection boundary")
       
  1037             << index
       
  1038             << true
       
  1039             << command
       
  1040             << expected;
       
  1041     }
       
  1042 
       
  1043     {
       
  1044         QModelIndexList index;
       
  1045         QModelIndexList expected;
       
  1046         IntList command;
       
  1047         index << model->index(0, 0, QModelIndex());
       
  1048         index << model->index(2, 2, QModelIndex());
       
  1049         command << QItemSelectionModel::Select;
       
  1050 
       
  1051         index << model->index(1, 1, QModelIndex());
       
  1052         index << model->index(1, 1, QModelIndex());
       
  1053         command << QItemSelectionModel::Toggle;
       
  1054 
       
  1055         expected << model->index(0, 0, QModelIndex())
       
  1056                  << model->index(0, 1, QModelIndex())
       
  1057                  << model->index(0, 2, QModelIndex())
       
  1058                  << model->index(1, 0, QModelIndex())
       
  1059                  << model->index(1, 2, QModelIndex())
       
  1060                  << model->index(2, 0, QModelIndex())
       
  1061                  << model->index(2, 1, QModelIndex())
       
  1062                  << model->index(2, 2, QModelIndex());
       
  1063 
       
  1064         QTest::newRow("(0, 0 to 2, 2) and (1, 1 to 1, 1): Select and Toggle at selection boundary")
       
  1065             << index
       
  1066             << true
       
  1067             << command
       
  1068             << expected;
       
  1069     }
       
  1070     {
       
  1071         QModelIndexList index;
       
  1072         QModelIndexList expected;
       
  1073         IntList command;
       
  1074         index << model->index(0, 0, QModelIndex());
       
  1075         index << model->index(2, 2, QModelIndex());
       
  1076         command << QItemSelectionModel::Select;
       
  1077 
       
  1078         index << model->index(1, 2, QModelIndex());
       
  1079         index << model->index(1, 2, QModelIndex());
       
  1080         command << QItemSelectionModel::Toggle;
       
  1081 
       
  1082         expected << model->index(0, 0, QModelIndex())
       
  1083                  << model->index(0, 1, QModelIndex())
       
  1084                  << model->index(0, 2, QModelIndex())
       
  1085                  << model->index(1, 0, QModelIndex())
       
  1086                  << model->index(1, 1, QModelIndex())
       
  1087                  << model->index(2, 0, QModelIndex())
       
  1088                  << model->index(2, 1, QModelIndex())
       
  1089                  << model->index(2, 2, QModelIndex());
       
  1090 
       
  1091         QTest::newRow("(0, 0 to 2, 2) and (1, 2 to 1, 2): Select and Toggle at selection boundary")
       
  1092             << index
       
  1093             << true
       
  1094             << command
       
  1095             << expected;
       
  1096     }
       
  1097     {
       
  1098         QModelIndexList index;
       
  1099         QModelIndexList expected;
       
  1100         IntList command;
       
  1101         index << model->index(0, 0, QModelIndex());
       
  1102         index << model->index(2, 2, QModelIndex());
       
  1103         command << QItemSelectionModel::Select;
       
  1104 
       
  1105         index << model->index(2, 0, QModelIndex());
       
  1106         index << model->index(2, 0, QModelIndex());
       
  1107         command << QItemSelectionModel::Toggle;
       
  1108 
       
  1109         expected << model->index(0, 0, QModelIndex())
       
  1110                  << model->index(0, 1, QModelIndex())
       
  1111                  << model->index(0, 2, QModelIndex())
       
  1112                  << model->index(1, 0, QModelIndex())
       
  1113                  << model->index(1, 1, QModelIndex())
       
  1114                  << model->index(1, 2, QModelIndex())
       
  1115                  << model->index(2, 1, QModelIndex())
       
  1116                  << model->index(2, 2, QModelIndex());
       
  1117 
       
  1118         QTest::newRow("(0, 0 to 2, 2) and (2, 0 to 2, 0): Select and Toggle at selection boundary")
       
  1119             << index
       
  1120             << true
       
  1121             << command
       
  1122             << expected;
       
  1123     }
       
  1124     {
       
  1125         QModelIndexList index;
       
  1126         QModelIndexList expected;
       
  1127         IntList command;
       
  1128         index << model->index(0, 0, QModelIndex());
       
  1129         index << model->index(2, 2, QModelIndex());
       
  1130         command << QItemSelectionModel::Select;
       
  1131 
       
  1132         index << model->index(2, 1, QModelIndex());
       
  1133         index << model->index(2, 1, QModelIndex());
       
  1134         command << QItemSelectionModel::Toggle;
       
  1135 
       
  1136         expected << model->index(0, 0, QModelIndex())
       
  1137                  << model->index(0, 1, QModelIndex())
       
  1138                  << model->index(0, 2, QModelIndex())
       
  1139                  << model->index(1, 0, QModelIndex())
       
  1140                  << model->index(1, 1, QModelIndex())
       
  1141                  << model->index(1, 2, QModelIndex())
       
  1142                  << model->index(2, 0, QModelIndex())
       
  1143                  << model->index(2, 2, QModelIndex());
       
  1144 
       
  1145         QTest::newRow("(0, 0 to 2, 2) and (2, 1 to 2, 1): Select and Toggle at selection boundary")
       
  1146             << index
       
  1147             << true
       
  1148             << command
       
  1149             << expected;
       
  1150     }
       
  1151     {
       
  1152         QModelIndexList index;
       
  1153         QModelIndexList expected;
       
  1154         IntList command;
       
  1155 
       
  1156         index << model->index(0, 0, QModelIndex());
       
  1157         index << model->index(2, 2, QModelIndex());
       
  1158         command << QItemSelectionModel::Select;
       
  1159 
       
  1160         index << model->index(2, 2, QModelIndex());
       
  1161         index << model->index(2, 2, QModelIndex());
       
  1162         command << QItemSelectionModel::Toggle;
       
  1163 
       
  1164         expected << model->index(0, 0, QModelIndex())
       
  1165                  << model->index(0, 1, QModelIndex())
       
  1166                  << model->index(0, 2, QModelIndex())
       
  1167                  << model->index(1, 0, QModelIndex())
       
  1168                  << model->index(1, 1, QModelIndex())
       
  1169                  << model->index(1, 2, QModelIndex())
       
  1170                  << model->index(2, 0, QModelIndex())
       
  1171                  << model->index(2, 1, QModelIndex());
       
  1172 
       
  1173         QTest::newRow("(0, 0 to 2, 2) and (2, 2 to 2, 2): Select and Toggle at selection boundary")
       
  1174             << index
       
  1175             << true
       
  1176             << command
       
  1177             << expected;
       
  1178     }
       
  1179     {
       
  1180         QModelIndexList indexes;
       
  1181 	IntList commands;
       
  1182 	QModelIndexList expected;
       
  1183 
       
  1184 	indexes  << model->index(0, 0, QModelIndex()) << model->index(0, 0, QModelIndex()) // press 0
       
  1185 		 << model->index(0, 0, QModelIndex()) << model->index(0, 0, QModelIndex()) // release 0
       
  1186 		 << model->index(1, 0, QModelIndex()) << model->index(1, 0, QModelIndex()) // press 1
       
  1187 		 << model->index(1, 0, QModelIndex()) << model->index(1, 0, QModelIndex()) // release 1
       
  1188 		 << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // press 2
       
  1189 		 << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // release 2
       
  1190 		 << model->index(3, 0, QModelIndex()) << model->index(3, 0, QModelIndex()) // press 3
       
  1191 		 << model->index(3, 0, QModelIndex()) << model->index(3, 0, QModelIndex()) // release 3
       
  1192 		 << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // press 2 again
       
  1193 		 << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex());// move 2
       
  1194 
       
  1195 	commands << (QItemSelectionModel::NoUpdate)                                // press 0
       
  1196 		 << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows)        // release 0
       
  1197 		 << (QItemSelectionModel::NoUpdate)                                // press 1
       
  1198 		 << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows)        // release 1
       
  1199 		 << (QItemSelectionModel::NoUpdate)                                // press 2
       
  1200 		 << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows)        // release 2
       
  1201 		 << (QItemSelectionModel::NoUpdate)                                // press 3
       
  1202 		 << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows)        // release 3
       
  1203 	         << (QItemSelectionModel::NoUpdate)                                // press 2 again
       
  1204 		 << (QItemSelectionModel::Toggle/*Current*/|QItemSelectionModel::Rows);// move 2
       
  1205 
       
  1206 	expected << model->index(0, 0, QModelIndex())
       
  1207 		 << model->index(0, 1, QModelIndex())
       
  1208 		 << model->index(0, 2, QModelIndex())
       
  1209 		 << model->index(0, 3, QModelIndex())
       
  1210 		 << model->index(0, 4, QModelIndex())
       
  1211 
       
  1212 		 << model->index(1, 0, QModelIndex())
       
  1213 		 << model->index(1, 1, QModelIndex())
       
  1214 		 << model->index(1, 2, QModelIndex())
       
  1215 		 << model->index(1, 3, QModelIndex())
       
  1216 		 << model->index(1, 4, QModelIndex())
       
  1217 	  /*
       
  1218 		 << model->index(2, 0, QModelIndex())
       
  1219 		 << model->index(2, 1, QModelIndex())
       
  1220 		 << model->index(2, 2, QModelIndex())
       
  1221 		 << model->index(2, 3, QModelIndex())
       
  1222 		 << model->index(2, 4, QModelIndex())
       
  1223 	  */
       
  1224 		 << model->index(3, 0, QModelIndex())
       
  1225 		 << model->index(3, 1, QModelIndex())
       
  1226 		 << model->index(3, 2, QModelIndex())
       
  1227 		 << model->index(3, 3, QModelIndex())
       
  1228 		 << model->index(3, 4, QModelIndex());
       
  1229 
       
  1230         QTest::newRow("simulated treeview multiselection behavior")
       
  1231             << indexes
       
  1232             << true
       
  1233             << commands
       
  1234             << expected;
       
  1235     }
       
  1236 }
       
  1237 
       
  1238 void tst_QItemSelectionModel::select()
       
  1239 {
       
  1240     QFETCH(QModelIndexList, indexList);
       
  1241     QFETCH(bool, useRanges);
       
  1242     QFETCH(IntList, commandList);
       
  1243     QFETCH(QModelIndexList, expectedList);
       
  1244 
       
  1245     int lastCommand = 0;
       
  1246     // do selections
       
  1247     for (int i = 0; i<commandList.count(); ++i) {
       
  1248         if (useRanges) {
       
  1249             selection->select(QItemSelection(indexList.at(2*i), indexList.at(2*i+1)),
       
  1250                               (QItemSelectionModel::SelectionFlags)commandList.at(i));
       
  1251         } else {
       
  1252             selection->select(indexList.at(i),
       
  1253                               (QItemSelectionModel::SelectionFlags)commandList.at(i));
       
  1254         }
       
  1255         lastCommand = commandList.at(i);
       
  1256     }
       
  1257 
       
  1258 
       
  1259     QModelIndexList selectedList = selection->selectedIndexes();
       
  1260 
       
  1261     QVERIFY(selection->hasSelection()!=selectedList.isEmpty());
       
  1262 
       
  1263     // debug output
       
  1264 //     for (int i=0; i<selectedList.count(); ++i)
       
  1265 //         qDebug(QString("selected (%1, %2)")
       
  1266 //                .arg(selectedList.at(i).row())
       
  1267 //                .arg(selectedList.at(i).column()));
       
  1268 
       
  1269     // test that the number of indices are as expected
       
  1270     QVERIFY2(selectedList.count() == expectedList.count(),
       
  1271             QString("expected indices: %1 actual indices: %2")
       
  1272             .arg(expectedList.count())
       
  1273             .arg(selectedList.count()).toLatin1());
       
  1274 
       
  1275     // test existence of each index
       
  1276     for (int i=0; i<expectedList.count(); ++i) {
       
  1277         QVERIFY2(selectedList.contains(expectedList.at(i)),
       
  1278                 QString("expected index(%1, %2) not found in selectedIndexes()")
       
  1279                 .arg(expectedList.at(i).row())
       
  1280                 .arg(expectedList.at(i).column()).toLatin1());
       
  1281     }
       
  1282 
       
  1283     // test that isSelected agrees
       
  1284     for (int i=0; i<indexList.count(); ++i) {
       
  1285         QModelIndex idx = indexList.at(i);
       
  1286         QVERIFY2(selection->isSelected(idx) == selectedList.contains(idx),
       
  1287                  QString("isSelected(index: %1, %2) does not match selectedIndexes()")
       
  1288                  .arg(idx.row())
       
  1289                  .arg(idx.column()).toLatin1());
       
  1290     }
       
  1291 
       
  1292     //for now we assume Rows/Columns flag is the same for all commands, therefore we just check lastCommand
       
  1293     // test that isRowSelected agrees
       
  1294     if (lastCommand & QItemSelectionModel::Rows) {
       
  1295         for (int i=0; i<selectedList.count(); ++i)
       
  1296             QVERIFY2(selection->isRowSelected(selectedList.at(i).row(),
       
  1297                                              model->parent(selectedList.at(i))),
       
  1298                     QString("isRowSelected(row: %1) does not match selectedIndexes()")
       
  1299                     .arg(selectedList.at(i).row()).toLatin1());
       
  1300     }
       
  1301 
       
  1302     // test that isColumnSelected agrees
       
  1303     if (lastCommand & QItemSelectionModel::Columns) {
       
  1304         for (int i=0; i<selectedList.count(); ++i)
       
  1305             QVERIFY2(selection->isColumnSelected(selectedList.at(i).column(),
       
  1306                                                 model->parent(selectedList.at(i))),
       
  1307                     QString("isColumnSelected(column: %1) does not match selectedIndexes()")
       
  1308                     .arg(selectedList.at(i).column()).toLatin1());
       
  1309     }
       
  1310 }
       
  1311 
       
  1312 void tst_QItemSelectionModel::persistentselections_data()
       
  1313 {
       
  1314     QTest::addColumn<PairList>("indexList");
       
  1315     QTest::addColumn<IntList>("commandList");
       
  1316     QTest::addColumn<IntList>("insertRows"); // start, count
       
  1317     QTest::addColumn<IntList>("insertColumns"); // start, count
       
  1318     QTest::addColumn<IntList>("deleteRows"); // start, count
       
  1319     QTest::addColumn<IntList>("deleteColumns"); // start, count
       
  1320     QTest::addColumn<PairList>("expectedList");
       
  1321 
       
  1322     PairList index, expected;
       
  1323     IntList command, insertRows, insertColumns, deleteRows, deleteColumns;
       
  1324 
       
  1325 
       
  1326     index.clear(); expected.clear(); command.clear();
       
  1327     insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1328     index << IntPair(0, 0);
       
  1329     command << QItemSelectionModel::ClearAndSelect;
       
  1330     deleteRows << 4 << 1;
       
  1331     expected << IntPair(0, 0);
       
  1332     QTest::newRow("ClearAndSelect (0, 0). Delete last row.")
       
  1333         << index << command
       
  1334         << insertRows << insertColumns << deleteRows << deleteColumns
       
  1335         << expected;
       
  1336 
       
  1337      index.clear(); expected.clear(); command.clear();
       
  1338      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1339      index << IntPair(0, 0);
       
  1340      command << QItemSelectionModel::ClearAndSelect;
       
  1341      deleteRows << 0 << 1;
       
  1342      QTest::newRow("ClearAndSelect (0, 0). Delete first row.")
       
  1343          << index << command
       
  1344          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1345          << expected;
       
  1346 
       
  1347      index.clear(); expected.clear(); command.clear();
       
  1348      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1349      index << IntPair(1, 0);
       
  1350      command << QItemSelectionModel::ClearAndSelect;
       
  1351      deleteRows << 0 << 1;
       
  1352      expected << IntPair(0, 0);
       
  1353      QTest::newRow("ClearAndSelect (1, 0). Delete first row.")
       
  1354          << index << command
       
  1355          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1356          << expected;
       
  1357 
       
  1358      index.clear(); expected.clear(); command.clear();
       
  1359      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1360      index << IntPair(0, 0);
       
  1361      command << QItemSelectionModel::ClearAndSelect;
       
  1362      insertRows << 5 << 1;
       
  1363      expected << IntPair(0, 0);
       
  1364      QTest::newRow("ClearAndSelect (0, 0). Append row.")
       
  1365          << index << command
       
  1366          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1367          << expected;
       
  1368 
       
  1369      index.clear(); expected.clear(); command.clear();
       
  1370      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1371      index << IntPair(0, 0);
       
  1372      command << QItemSelectionModel::ClearAndSelect;
       
  1373      insertRows << 0 << 1;
       
  1374      expected << IntPair(1, 0);
       
  1375      QTest::newRow("ClearAndSelect (0, 0). Insert before first row.")
       
  1376          << index << command
       
  1377          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1378          << expected;
       
  1379 
       
  1380      index.clear(); expected.clear(); command.clear();
       
  1381      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1382      index << IntPair(0, 0)
       
  1383            << IntPair(4, 0);
       
  1384      command << QItemSelectionModel::ClearAndSelect;
       
  1385      insertRows << 5 << 1;
       
  1386      expected << IntPair(0, 0)
       
  1387               << IntPair(1, 0)
       
  1388               << IntPair(2, 0)
       
  1389               << IntPair(3, 0)
       
  1390               << IntPair(4, 0);
       
  1391      QTest::newRow("ClearAndSelect (0, 0) to (4, 0). Append row.")
       
  1392          << index << command
       
  1393          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1394          << expected;
       
  1395 
       
  1396      index.clear(); expected.clear(); command.clear();
       
  1397      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1398      index << IntPair(0, 0)
       
  1399            << IntPair(4, 0);
       
  1400      command << QItemSelectionModel::ClearAndSelect;
       
  1401      insertRows << 0  << 1;
       
  1402      expected << IntPair(1, 0)
       
  1403               << IntPair(2, 0)
       
  1404               << IntPair(3, 0)
       
  1405               << IntPair(4, 0)
       
  1406               << IntPair(5, 0);
       
  1407      QTest::newRow("ClearAndSelect (0, 0) to (4, 0). Insert before first row.")
       
  1408          << index << command
       
  1409          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1410          << expected;
       
  1411 
       
  1412      index.clear(); expected.clear(); command.clear();
       
  1413      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1414      index << IntPair(0, 0)
       
  1415            << IntPair(4, 0);
       
  1416      command << QItemSelectionModel::ClearAndSelect;
       
  1417      deleteRows << 0  << 1;
       
  1418      expected << IntPair(0, 0)
       
  1419               << IntPair(1, 0)
       
  1420               << IntPair(2, 0)
       
  1421               << IntPair(3, 0);
       
  1422      QTest::newRow("ClearAndSelect (0, 0) to (4, 0). Delete first row.")
       
  1423          << index << command
       
  1424          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1425          << expected;
       
  1426 
       
  1427      index.clear(); expected.clear(); command.clear();
       
  1428      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1429      index << IntPair(0, 0)
       
  1430            << IntPair(4, 0);
       
  1431      command << QItemSelectionModel::ClearAndSelect;
       
  1432      deleteRows << 4  << 1;
       
  1433      expected << IntPair(0, 0)
       
  1434               << IntPair(1, 0)
       
  1435               << IntPair(2, 0)
       
  1436               << IntPair(3, 0);
       
  1437      QTest::newRow("ClearAndSelect (0, 0) to (4, 0). Delete last row.")
       
  1438          << index << command
       
  1439          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1440          << expected;
       
  1441 
       
  1442      index.clear(); expected.clear(); command.clear();
       
  1443      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1444      index << IntPair(0, 0)
       
  1445            << IntPair(4, 0);
       
  1446      command << QItemSelectionModel::ClearAndSelect;
       
  1447      deleteRows << 1  << 3;
       
  1448      expected << IntPair(0, 0)
       
  1449               << IntPair(1, 0);
       
  1450      QTest::newRow("ClearAndSelect (0, 0) to (4, 0). Deleting all but first and last row.")
       
  1451          << index << command
       
  1452          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1453          << expected;
       
  1454 
       
  1455      index.clear(); expected.clear(); command.clear();
       
  1456      insertRows.clear(); insertColumns.clear(); deleteRows.clear(); deleteColumns.clear();
       
  1457      index << IntPair(0, 0)
       
  1458            << IntPair(4, 0);
       
  1459      command << QItemSelectionModel::ClearAndSelect;
       
  1460      insertRows << 1 << 1;
       
  1461      expected << IntPair(0, 0)
       
  1462          // the inserted row should not be selected
       
  1463               << IntPair(2, 0)
       
  1464               << IntPair(3, 0)
       
  1465               << IntPair(4, 0)
       
  1466               << IntPair(5, 0);
       
  1467      QTest::newRow("ClearAndSelect (0, 0) to (4, 0). Insert after first row.")
       
  1468          << index << command
       
  1469          << insertRows << insertColumns << deleteRows << deleteColumns
       
  1470          << expected;
       
  1471 }
       
  1472 
       
  1473 void tst_QItemSelectionModel::persistentselections()
       
  1474 {
       
  1475     QFETCH(PairList, indexList);
       
  1476     QFETCH(IntList, commandList);
       
  1477     QFETCH(IntList, insertRows);
       
  1478     QFETCH(IntList, insertColumns);
       
  1479     QFETCH(IntList, deleteRows);
       
  1480     QFETCH(IntList, deleteColumns);
       
  1481     QFETCH(PairList, expectedList);
       
  1482 
       
  1483     // make sure the model is sane (5x5)
       
  1484     QCOMPARE(model->rowCount(QModelIndex()), 5);
       
  1485     QCOMPARE(model->columnCount(QModelIndex()), 5);
       
  1486 
       
  1487     // do selections
       
  1488     for (int i=0; i<commandList.count(); ++i) {
       
  1489         if (indexList.count() == commandList.count()) {
       
  1490             QModelIndex index = model->index(indexList.at(i).first,
       
  1491                                              indexList.at(i).second,
       
  1492                                              QModelIndex());
       
  1493             selection->select(index, (QItemSelectionModel::SelectionFlags)commandList.at(i));
       
  1494         } else {
       
  1495             QModelIndex tl = model->index(indexList.at(2*i).first,
       
  1496                                           indexList.at(2*i).second,
       
  1497                                           QModelIndex());
       
  1498             QModelIndex br = model->index(indexList.at(2*i+1).first,
       
  1499                                           indexList.at(2*i+1).second,
       
  1500                                           QModelIndex());
       
  1501             selection->select(QItemSelection(tl, br),
       
  1502                               (QItemSelectionModel::SelectionFlags)commandList.at(i));
       
  1503         }
       
  1504     }
       
  1505     // test that we have selected items
       
  1506     QVERIFY(!selection->selectedIndexes().isEmpty());
       
  1507     QVERIFY(selection->hasSelection());
       
  1508 
       
  1509     // insert/delete row and/or columns
       
  1510     if (insertRows.count() > 1)
       
  1511         model->insertRows(insertRows.at(0), insertRows.at(1), QModelIndex());
       
  1512     if (insertColumns.count() > 1)
       
  1513         model->insertColumns(insertColumns.at(0), insertColumns.at(1), QModelIndex());
       
  1514     if (deleteRows.count() > 1)
       
  1515         model->removeRows(deleteRows.at(0), deleteRows.at(1), QModelIndex());
       
  1516     if (deleteColumns.count() > 1)
       
  1517         model->removeColumns(deleteColumns.at(0), deleteColumns.at(1), QModelIndex());
       
  1518 
       
  1519     // check that the selected items are the correct number and indexes
       
  1520     QModelIndexList selectedList = selection->selectedIndexes();
       
  1521     QCOMPARE(selectedList.count(), expectedList.count());
       
  1522     foreach(IntPair pair, expectedList) {
       
  1523         QModelIndex index = model->index(pair.first, pair.second, QModelIndex());
       
  1524         QVERIFY(selectedList.contains(index));
       
  1525     }
       
  1526 }
       
  1527 
       
  1528 // "make reset public"-model
       
  1529 class MyStandardItemModel: public QStandardItemModel
       
  1530 {
       
  1531     Q_OBJECT
       
  1532 public:
       
  1533     inline MyStandardItemModel(int i1, int i2): QStandardItemModel(i1, i2) {}
       
  1534     inline void reset() { QStandardItemModel::reset(); }
       
  1535 };
       
  1536 
       
  1537 void tst_QItemSelectionModel::resetModel()
       
  1538 {
       
  1539     MyStandardItemModel model(20, 20);
       
  1540     QTreeView view;
       
  1541     view.setModel(&model);
       
  1542 
       
  1543     QSignalSpy spy(view.selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)));
       
  1544 
       
  1545     view.selectionModel()->select(QItemSelection(model.index(0, 0), model.index(5, 5)), QItemSelectionModel::Select);
       
  1546 
       
  1547     QCOMPARE(spy.count(), 1);
       
  1548 
       
  1549     model.reset();
       
  1550 
       
  1551     QVERIFY(view.selectionModel()->selection().isEmpty());
       
  1552     QVERIFY(view.selectionModel()->hasSelection() == false);
       
  1553 
       
  1554     view.selectionModel()->select(QItemSelection(model.index(0, 0), model.index(5, 5)), QItemSelectionModel::Select);
       
  1555 
       
  1556     QCOMPARE(spy.count(), 2);
       
  1557     QCOMPARE(spy.at(1).count(), 2);
       
  1558     // make sure we don't get an "old selection"
       
  1559     QCOMPARE(spy.at(1).at(1).userType(), qMetaTypeId<QItemSelection>());
       
  1560     QVERIFY(qvariant_cast<QItemSelection>(spy.at(1).at(1)).isEmpty());
       
  1561 }
       
  1562 
       
  1563 void tst_QItemSelectionModel::removeRows_data()
       
  1564 {
       
  1565     QTest::addColumn<int>("rowCount");
       
  1566     QTest::addColumn<int>("columnCount");
       
  1567 
       
  1568     QTest::addColumn<int>("selectTop");
       
  1569     QTest::addColumn<int>("selectLeft");
       
  1570     QTest::addColumn<int>("selectBottom");
       
  1571     QTest::addColumn<int>("selectRight");
       
  1572 
       
  1573     QTest::addColumn<int>("removeTop");
       
  1574     QTest::addColumn<int>("removeBottom");
       
  1575 
       
  1576     QTest::addColumn<int>("expectedTop");
       
  1577     QTest::addColumn<int>("expectedLeft");
       
  1578     QTest::addColumn<int>("expectedBottom");
       
  1579     QTest::addColumn<int>("expectedRight");
       
  1580 
       
  1581     QTest::newRow("4x4 <0,1><1,1>")
       
  1582         << 4 << 4
       
  1583         << 0 << 1 << 1 << 1
       
  1584         << 0 << 0
       
  1585         << 0 << 1 << 0 << 1;
       
  1586 }
       
  1587 
       
  1588 void tst_QItemSelectionModel::removeRows()
       
  1589 {
       
  1590     QFETCH(int, rowCount);
       
  1591     QFETCH(int, columnCount);
       
  1592     QFETCH(int, selectTop);
       
  1593     QFETCH(int, selectLeft);
       
  1594     QFETCH(int, selectBottom);
       
  1595     QFETCH(int, selectRight);
       
  1596     QFETCH(int, removeTop);
       
  1597     QFETCH(int, removeBottom);
       
  1598     QFETCH(int, expectedTop);
       
  1599     QFETCH(int, expectedLeft);
       
  1600     QFETCH(int, expectedBottom);
       
  1601     QFETCH(int, expectedRight);
       
  1602 
       
  1603     MyStandardItemModel model(rowCount, columnCount);
       
  1604     QItemSelectionModel selections(&model);
       
  1605     QSignalSpy spy(&selections, SIGNAL(selectionChanged(QItemSelection,QItemSelection)));
       
  1606 
       
  1607     QModelIndex tl = model.index(selectTop, selectLeft);
       
  1608     QModelIndex br = model.index(selectBottom, selectRight);
       
  1609     selections.select(QItemSelection(tl, br), QItemSelectionModel::ClearAndSelect);
       
  1610 
       
  1611     QCOMPARE(spy.count(), 1);
       
  1612     QVERIFY(selections.isSelected(tl));
       
  1613     QVERIFY(selections.isSelected(br));
       
  1614     QVERIFY(selections.hasSelection());
       
  1615 
       
  1616     model.removeRows(removeTop, removeBottom - removeTop + 1);
       
  1617 
       
  1618     QCOMPARE(spy.count(), 2);
       
  1619     tl = model.index(expectedTop, expectedLeft);
       
  1620     br = model.index(expectedBottom, expectedRight);
       
  1621     QVERIFY(selections.isSelected(tl));
       
  1622     QVERIFY(selections.isSelected(br));
       
  1623 }
       
  1624 
       
  1625 void tst_QItemSelectionModel::removeColumns_data()
       
  1626 {
       
  1627     QTest::addColumn<int>("rowCount");
       
  1628     QTest::addColumn<int>("columnCount");
       
  1629 
       
  1630     QTest::addColumn<int>("selectTop");
       
  1631     QTest::addColumn<int>("selectLeft");
       
  1632     QTest::addColumn<int>("selectBottom");
       
  1633     QTest::addColumn<int>("selectRight");
       
  1634 
       
  1635     QTest::addColumn<int>("removeLeft");
       
  1636     QTest::addColumn<int>("removeRight");
       
  1637 
       
  1638     QTest::addColumn<int>("expectedTop");
       
  1639     QTest::addColumn<int>("expectedLeft");
       
  1640     QTest::addColumn<int>("expectedBottom");
       
  1641     QTest::addColumn<int>("expectedRight");
       
  1642 
       
  1643     QTest::newRow("4x4 <0,1><1,1>")
       
  1644         << 4 << 4
       
  1645         << 1 << 0 << 1 << 1
       
  1646         << 0 << 0
       
  1647         << 1 << 0 << 1 << 0;
       
  1648 }
       
  1649 
       
  1650 void tst_QItemSelectionModel::removeColumns()
       
  1651 {
       
  1652     QFETCH(int, rowCount);
       
  1653     QFETCH(int, columnCount);
       
  1654     QFETCH(int, selectTop);
       
  1655     QFETCH(int, selectLeft);
       
  1656     QFETCH(int, selectBottom);
       
  1657     QFETCH(int, selectRight);
       
  1658     QFETCH(int, removeLeft);
       
  1659     QFETCH(int, removeRight);
       
  1660     QFETCH(int, expectedTop);
       
  1661     QFETCH(int, expectedLeft);
       
  1662     QFETCH(int, expectedBottom);
       
  1663     QFETCH(int, expectedRight);
       
  1664 
       
  1665     MyStandardItemModel model(rowCount, columnCount);
       
  1666     QItemSelectionModel selections(&model);
       
  1667     QSignalSpy spy(&selections, SIGNAL(selectionChanged(QItemSelection,QItemSelection)));
       
  1668 
       
  1669     QModelIndex tl = model.index(selectTop, selectLeft);
       
  1670     QModelIndex br = model.index(selectBottom, selectRight);
       
  1671     selections.select(QItemSelection(tl, br), QItemSelectionModel::ClearAndSelect);
       
  1672 
       
  1673     QCOMPARE(spy.count(), 1);
       
  1674     QVERIFY(selections.isSelected(tl));
       
  1675     QVERIFY(selections.isSelected(br));
       
  1676     QVERIFY(selections.hasSelection());
       
  1677 
       
  1678     model.removeColumns(removeLeft, removeRight - removeLeft + 1);
       
  1679 
       
  1680     QCOMPARE(spy.count(), 2);
       
  1681     tl = model.index(expectedTop, expectedLeft);
       
  1682     br = model.index(expectedBottom, expectedRight);
       
  1683     QVERIFY(selections.isSelected(tl));
       
  1684     QVERIFY(selections.isSelected(br));
       
  1685 }
       
  1686 
       
  1687 typedef QList<IntList> IntListList;
       
  1688 typedef QPair<IntPair, IntPair> IntPairPair;
       
  1689 typedef QList<IntPairPair> IntPairPairList;
       
  1690 Q_DECLARE_METATYPE(IntListList)
       
  1691 Q_DECLARE_METATYPE(IntPairPair)
       
  1692 Q_DECLARE_METATYPE(IntPairPairList)
       
  1693 
       
  1694 void tst_QItemSelectionModel::modelLayoutChanged_data()
       
  1695 {
       
  1696     QTest::addColumn<IntListList>("items");
       
  1697     QTest::addColumn<IntPairPairList>("initialSelectedRanges");
       
  1698     QTest::addColumn<int>("sortOrder");
       
  1699     QTest::addColumn<int>("sortColumn");
       
  1700     QTest::addColumn<IntPairPairList>("expectedSelectedRanges");
       
  1701 
       
  1702     QTest::newRow("everything selected, then row order reversed")
       
  1703         << (IntListList()
       
  1704             << (IntList() << 0 << 1 << 2 << 3)
       
  1705             << (IntList() << 3 << 2 << 1 << 0))
       
  1706         << (IntPairPairList()
       
  1707             << IntPairPair(IntPair(0, 0), IntPair(3, 1)))
       
  1708         << int(Qt::DescendingOrder)
       
  1709         << 0
       
  1710         << (IntPairPairList()
       
  1711             << IntPairPair(IntPair(0, 0), IntPair(3, 1)));
       
  1712     QTest::newRow("first two rows selected, then row order reversed")
       
  1713         << (IntListList()
       
  1714             << (IntList() << 0 << 1 << 2 << 3)
       
  1715             << (IntList() << 3 << 2 << 1 << 0))
       
  1716         << (IntPairPairList()
       
  1717             << IntPairPair(IntPair(0, 0), IntPair(1, 1)))
       
  1718         << int(Qt::DescendingOrder)
       
  1719         << 0
       
  1720         << (IntPairPairList()
       
  1721             << IntPairPair(IntPair(2, 0), IntPair(3, 1)));
       
  1722     QTest::newRow("middle two rows selected, then row order reversed")
       
  1723         << (IntListList()
       
  1724             << (IntList() << 0 << 1 << 2 << 3)
       
  1725             << (IntList() << 3 << 2 << 1 << 0))
       
  1726         << (IntPairPairList()
       
  1727             << IntPairPair(IntPair(1, 0), IntPair(2, 1)))
       
  1728         << int(Qt::DescendingOrder)
       
  1729         << 0
       
  1730         << (IntPairPairList()
       
  1731             << IntPairPair(IntPair(1, 0), IntPair(2, 1)));
       
  1732     QTest::newRow("two ranges")
       
  1733         << (IntListList()
       
  1734             << (IntList() << 2 << 0 << 3 << 1)
       
  1735             << (IntList() << 2 << 0 << 3 << 1))
       
  1736         << (IntPairPairList()
       
  1737             << IntPairPair(IntPair(1, 0), IntPair(1, 1))
       
  1738             << IntPairPair(IntPair(3, 0), IntPair(3, 1)))
       
  1739         << int(Qt::AscendingOrder)
       
  1740         << 0
       
  1741         << (IntPairPairList()
       
  1742             << IntPairPair(IntPair(0, 0), IntPair(0, 1))
       
  1743             << IntPairPair(IntPair(1, 0), IntPair(1, 1)));
       
  1744 }
       
  1745 
       
  1746 void tst_QItemSelectionModel::modelLayoutChanged()
       
  1747 {
       
  1748     QFETCH(IntListList, items);
       
  1749     QFETCH(IntPairPairList, initialSelectedRanges);
       
  1750     QFETCH(int, sortOrder);
       
  1751     QFETCH(int, sortColumn);
       
  1752     QFETCH(IntPairPairList, expectedSelectedRanges);
       
  1753 
       
  1754     MyStandardItemModel model(items.at(0).count(), items.count());
       
  1755     // initialize model data
       
  1756     for (int i = 0; i < model.rowCount(); ++i) {
       
  1757         for (int j = 0; j < model.columnCount(); ++j) {
       
  1758             QModelIndex index = model.index(i, j);
       
  1759             model.setData(index, items.at(j).at(i), Qt::DisplayRole);
       
  1760         }
       
  1761     }
       
  1762 
       
  1763     // select initial ranges
       
  1764     QItemSelectionModel selectionModel(&model);
       
  1765     foreach (IntPairPair range, initialSelectedRanges) {
       
  1766         IntPair tl = range.first;
       
  1767         IntPair br = range.second;
       
  1768         QItemSelection selection(
       
  1769             model.index(tl.first, tl.second),
       
  1770             model.index(br.first, br.second));
       
  1771         selectionModel.select(selection, QItemSelectionModel::Select);
       
  1772     }
       
  1773 
       
  1774     // sort the model
       
  1775     model.sort(sortColumn, Qt::SortOrder(sortOrder));
       
  1776 
       
  1777     // verify that selection is as expected
       
  1778     QItemSelection selection = selectionModel.selection();
       
  1779     QCOMPARE(selection.count(), expectedSelectedRanges.count());
       
  1780 	QVERIFY(selectionModel.hasSelection() == !expectedSelectedRanges.isEmpty());
       
  1781 
       
  1782     for (int i = 0; i < expectedSelectedRanges.count(); ++i) {
       
  1783         IntPairPair expectedRange = expectedSelectedRanges.at(i);
       
  1784         IntPair expectedTl = expectedRange.first;
       
  1785         IntPair expectedBr = expectedRange.second;
       
  1786         QItemSelectionRange actualRange = selection.at(i);
       
  1787         QModelIndex actualTl = actualRange.topLeft();
       
  1788         QModelIndex actualBr = actualRange.bottomRight();
       
  1789         QCOMPARE(actualTl.row(), expectedTl.first);
       
  1790         QCOMPARE(actualTl.column(), expectedTl.second);
       
  1791         QCOMPARE(actualBr.row(), expectedBr.first);
       
  1792         QCOMPARE(actualBr.column(), expectedBr.second);
       
  1793     }
       
  1794 }
       
  1795 
       
  1796 void tst_QItemSelectionModel::selectedRows_data()
       
  1797 {
       
  1798     QTest::addColumn<int>("rowCount");
       
  1799     QTest::addColumn<int>("columnCount");
       
  1800     QTest::addColumn<int>("column");
       
  1801     QTest::addColumn<IntList>("selectRows");
       
  1802     QTest::addColumn<IntList>("expectedRows");
       
  1803     QTest::addColumn<IntList>("unexpectedRows");
       
  1804 
       
  1805     QTest::newRow("10x10, first row")
       
  1806         << 10 << 10 << 0
       
  1807         << (IntList() << 0)
       
  1808         << (IntList() << 0)
       
  1809         << (IntList() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9);
       
  1810 
       
  1811     QTest::newRow("10x10, first 4 rows")
       
  1812         << 10 << 10 << 0
       
  1813         << (IntList() << 0 << 1 << 2 << 3)
       
  1814         << (IntList() << 0 << 1 << 2 << 3)
       
  1815         << (IntList() << 4 << 5 << 6 << 7 << 8 << 9);
       
  1816 
       
  1817     QTest::newRow("10x10, last 4 rows")
       
  1818         << 10 << 10 << 0
       
  1819         << (IntList() << 6 << 7 << 8 << 9)
       
  1820         << (IntList() << 6 << 7 << 8 << 9)
       
  1821         << (IntList() << 0 << 1 << 2 << 3 << 4 << 6);
       
  1822 }
       
  1823 
       
  1824 void tst_QItemSelectionModel::selectedRows()
       
  1825 {
       
  1826     QFETCH(int, rowCount);
       
  1827     QFETCH(int, columnCount);
       
  1828     QFETCH(int, column);
       
  1829     QFETCH(IntList, selectRows);
       
  1830     QFETCH(IntList, expectedRows);
       
  1831     QFETCH(IntList, unexpectedRows);
       
  1832 
       
  1833     MyStandardItemModel model(rowCount, columnCount);
       
  1834     QItemSelectionModel selectionModel(&model);
       
  1835 
       
  1836     for (int i = 0; i < selectRows.count(); ++i)
       
  1837         selectionModel.select(model.index(selectRows.at(i), 0),
       
  1838                               QItemSelectionModel::Select
       
  1839                               |QItemSelectionModel::Rows);
       
  1840 
       
  1841     for (int j = 0; j < selectRows.count(); ++j)
       
  1842         QVERIFY(selectionModel.isRowSelected(expectedRows.at(j), QModelIndex()));
       
  1843 
       
  1844     for (int k = 0; k < selectRows.count(); ++k)
       
  1845         QVERIFY(!selectionModel.isRowSelected(unexpectedRows.at(k), QModelIndex()));
       
  1846 
       
  1847     QModelIndexList selectedRowIndexes = selectionModel.selectedRows(column);
       
  1848     QCOMPARE(selectedRowIndexes.count(), expectedRows.count());
       
  1849     qSort(selectedRowIndexes);
       
  1850     for (int l = 0; l < selectedRowIndexes.count(); ++l) {
       
  1851         QCOMPARE(selectedRowIndexes.at(l).row(), expectedRows.at(l));
       
  1852         QCOMPARE(selectedRowIndexes.at(l).column(), column);
       
  1853     }
       
  1854 }
       
  1855 
       
  1856 void tst_QItemSelectionModel::selectedColumns_data()
       
  1857 {
       
  1858     QTest::addColumn<int>("rowCount");
       
  1859     QTest::addColumn<int>("columnCount");
       
  1860     QTest::addColumn<int>("row");
       
  1861     QTest::addColumn<IntList>("selectColumns");
       
  1862     QTest::addColumn<IntList>("expectedColumns");
       
  1863     QTest::addColumn<IntList>("unexpectedColumns");
       
  1864 
       
  1865     QTest::newRow("10x10, first columns")
       
  1866         << 10 << 10 << 0
       
  1867         << (IntList() << 0)
       
  1868         << (IntList() << 0)
       
  1869         << (IntList() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9);
       
  1870 
       
  1871     QTest::newRow("10x10, first 4 columns")
       
  1872         << 10 << 10 << 0
       
  1873         << (IntList() << 0 << 1 << 2 << 3)
       
  1874         << (IntList() << 0 << 1 << 2 << 3)
       
  1875         << (IntList() << 4 << 5 << 6 << 7 << 8 << 9);
       
  1876 
       
  1877     QTest::newRow("10x10, last 4 columns")
       
  1878         << 10 << 10 << 0
       
  1879         << (IntList() << 6 << 7 << 8 << 9)
       
  1880         << (IntList() << 6 << 7 << 8 << 9)
       
  1881         << (IntList() << 0 << 1 << 2 << 3 << 4 << 6);
       
  1882 }
       
  1883 
       
  1884 void tst_QItemSelectionModel::selectedColumns()
       
  1885 {
       
  1886     QFETCH(int, rowCount);
       
  1887     QFETCH(int, columnCount);
       
  1888     QFETCH(int, row);
       
  1889     QFETCH(IntList, selectColumns);
       
  1890     QFETCH(IntList, expectedColumns);
       
  1891     QFETCH(IntList, unexpectedColumns);
       
  1892 
       
  1893     MyStandardItemModel model(rowCount, columnCount);
       
  1894     QItemSelectionModel selectionModel(&model);
       
  1895 
       
  1896     for (int i = 0; i < selectColumns.count(); ++i)
       
  1897         selectionModel.select(model.index(0, selectColumns.at(i)),
       
  1898                               QItemSelectionModel::Select
       
  1899                               |QItemSelectionModel::Columns);
       
  1900 
       
  1901     for (int j = 0; j < selectColumns.count(); ++j)
       
  1902         QVERIFY(selectionModel.isColumnSelected(expectedColumns.at(j), QModelIndex()));
       
  1903 
       
  1904     for (int k = 0; k < selectColumns.count(); ++k)
       
  1905         QVERIFY(!selectionModel.isColumnSelected(unexpectedColumns.at(k), QModelIndex()));
       
  1906 
       
  1907     QModelIndexList selectedColumnIndexes = selectionModel.selectedColumns(row);
       
  1908     QCOMPARE(selectedColumnIndexes.count(), expectedColumns.count());
       
  1909     qSort(selectedColumnIndexes);
       
  1910     for (int l = 0; l < selectedColumnIndexes.count(); ++l) {
       
  1911         QCOMPARE(selectedColumnIndexes.at(l).column(), expectedColumns.at(l));
       
  1912         QCOMPARE(selectedColumnIndexes.at(l).row(), row);
       
  1913     }
       
  1914 }
       
  1915 
       
  1916 void tst_QItemSelectionModel::setCurrentIndex()
       
  1917 {
       
  1918     // Build up a simple tree
       
  1919     QStandardItemModel *treemodel = new QStandardItemModel(0, 1);
       
  1920     treemodel->insertRow(0, new QStandardItem(1));
       
  1921     treemodel->insertRow(1, new QStandardItem(2));
       
  1922 
       
  1923     QTreeView treeView;
       
  1924     treeView.setModel(treemodel);
       
  1925     QItemSelectionModel *selectionModel = treeView.selectionModel();
       
  1926     selectionModel->setCurrentIndex(
       
  1927             treemodel->index(0, 0, treemodel->index(0, 0)),
       
  1928             QItemSelectionModel::SelectCurrent);
       
  1929 
       
  1930     QSignalSpy currentSpy(selectionModel,
       
  1931             SIGNAL(currentChanged(QModelIndex,QModelIndex)));
       
  1932     QSignalSpy rowSpy(selectionModel,
       
  1933             SIGNAL(currentRowChanged(QModelIndex,QModelIndex)));
       
  1934     QSignalSpy columnSpy(selectionModel,
       
  1935             SIGNAL(currentColumnChanged(QModelIndex,QModelIndex)));
       
  1936 
       
  1937     // Select the same row and column indexes, but with a different parent
       
  1938     selectionModel->setCurrentIndex(
       
  1939             treemodel->index(0, 0, treemodel->index(1, 0)),
       
  1940             QItemSelectionModel::SelectCurrent);
       
  1941 
       
  1942     QCOMPARE(currentSpy.count(), 1);
       
  1943     QCOMPARE(rowSpy.count(), 1);
       
  1944     QCOMPARE(columnSpy.count(), 1);
       
  1945 
       
  1946     // Select another row in the same parent
       
  1947     selectionModel->setCurrentIndex(
       
  1948             treemodel->index(1, 0, treemodel->index(1, 0)),
       
  1949             QItemSelectionModel::SelectCurrent);
       
  1950 
       
  1951     QCOMPARE(currentSpy.count(), 2);
       
  1952     QCOMPARE(rowSpy.count(), 2);
       
  1953     QCOMPARE(columnSpy.count(), 1);
       
  1954 
       
  1955     delete treemodel;
       
  1956 }
       
  1957 
       
  1958 void tst_QItemSelectionModel::splitOnInsert()
       
  1959 {
       
  1960     QStandardItemModel model(4, 1);
       
  1961     QItemSelectionModel selectionModel(&model);
       
  1962     selectionModel.select(model.index(2, 0), QItemSelectionModel::Select);
       
  1963     model.insertRow(2);
       
  1964     model.removeRow(3);
       
  1965     QVERIFY(!selectionModel.isSelected(model.index(1, 0)));
       
  1966 }
       
  1967 
       
  1968 void tst_QItemSelectionModel::task196285_rowIntersectsSelection()
       
  1969 {
       
  1970     QTableWidget table;
       
  1971     table.setColumnCount(1);
       
  1972     table.setRowCount(1);
       
  1973     table.setItem(0, 0, new QTableWidgetItem("foo"));
       
  1974     QAbstractItemModel *model = table.model();
       
  1975     QItemSelectionModel *selectionModel = table.selectionModel();
       
  1976     QModelIndex index = model->index(0, 0, QModelIndex());
       
  1977 
       
  1978     selectionModel->select(index, QItemSelectionModel::Select);
       
  1979     QVERIFY(selectionModel->rowIntersectsSelection(0, QModelIndex()));
       
  1980     QVERIFY(selectionModel->columnIntersectsSelection(0, QModelIndex()));
       
  1981 
       
  1982     selectionModel->select(index, QItemSelectionModel::Deselect);
       
  1983     QVERIFY(!selectionModel->rowIntersectsSelection(0, QModelIndex()));
       
  1984     QVERIFY(!selectionModel->columnIntersectsSelection(0, QModelIndex()));
       
  1985 
       
  1986     selectionModel->select(index, QItemSelectionModel::Toggle);
       
  1987     QVERIFY(selectionModel->rowIntersectsSelection(0, QModelIndex()));
       
  1988     QVERIFY(selectionModel->columnIntersectsSelection(0, QModelIndex()));
       
  1989 
       
  1990     selectionModel->select(index, QItemSelectionModel::Toggle);
       
  1991     QVERIFY(!selectionModel->rowIntersectsSelection(0, QModelIndex()));
       
  1992     QVERIFY(!selectionModel->columnIntersectsSelection(0, QModelIndex()));
       
  1993 }
       
  1994 
       
  1995 void tst_QItemSelectionModel::unselectable()
       
  1996 {
       
  1997     QTreeWidget w;
       
  1998     for (int i = 0; i < 10; ++i)
       
  1999         w.setItemSelected(new QTreeWidgetItem(&w), true);
       
  2000     QCOMPARE(w.topLevelItemCount(), 10);
       
  2001     QCOMPARE(w.selectionModel()->selectedIndexes().count(), 10);
       
  2002     QCOMPARE(w.selectionModel()->selectedRows().count(), 10);
       
  2003     for (int j = 0; j < 10; ++j)
       
  2004         w.topLevelItem(j)->setFlags(0);
       
  2005     QCOMPARE(w.selectionModel()->selectedIndexes().count(), 0);
       
  2006     QCOMPARE(w.selectionModel()->selectedRows().count(), 0);
       
  2007 }
       
  2008 
       
  2009 void tst_QItemSelectionModel::task220420_selectedIndexes()
       
  2010 {
       
  2011     QStandardItemModel model(2, 2);
       
  2012     QItemSelectionModel selectionModel(&model);
       
  2013     QItemSelection selection;
       
  2014     selection.append(QItemSelectionRange(model.index(0,0)));
       
  2015     selection.append(QItemSelectionRange(model.index(0,1)));
       
  2016 
       
  2017     //we select the 1st row
       
  2018     selectionModel.select(selection, QItemSelectionModel::Rows | QItemSelectionModel::Select);
       
  2019 
       
  2020     QCOMPARE(selectionModel.selectedRows().count(), 1);
       
  2021     QCOMPARE(selectionModel.selectedIndexes().count(), model.columnCount());
       
  2022 }
       
  2023 
       
  2024 
       
  2025 class QtTestTableModel: public QAbstractTableModel
       
  2026 {
       
  2027     Q_OBJECT
       
  2028     
       
  2029     public:
       
  2030         QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
       
  2031         : QAbstractTableModel(parent),
       
  2032         row_count(rows),
       
  2033         column_count(columns) {}
       
  2034         
       
  2035         int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
       
  2036         int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
       
  2037         bool isEditable(const QModelIndex &) const { return true; }
       
  2038         
       
  2039         QVariant data(const QModelIndex &idx, int role) const
       
  2040         {
       
  2041             if (role == Qt::DisplayRole || role == Qt::EditRole)
       
  2042                 return QString("[%1,%2]").arg(idx.row()).arg(idx.column());
       
  2043             return QVariant();
       
  2044         }
       
  2045         
       
  2046         int row_count;
       
  2047         int column_count;
       
  2048         friend class tst_QItemSelectionModel;
       
  2049 };
       
  2050 
       
  2051 
       
  2052 void tst_QItemSelectionModel::task240734_layoutChanged()
       
  2053 {
       
  2054     QtTestTableModel model(1,1);
       
  2055     QItemSelectionModel selectionModel(&model);
       
  2056     selectionModel.select(model.index(0,0), QItemSelectionModel::Select);
       
  2057     QCOMPARE(selectionModel.selectedIndexes().count() , 1);
       
  2058     
       
  2059     emit model.layoutAboutToBeChanged();
       
  2060     model.row_count = 5;
       
  2061     emit model.layoutChanged();
       
  2062 
       
  2063     //The selection should not change.
       
  2064     QCOMPARE(selectionModel.selectedIndexes().count() , 1);
       
  2065     QCOMPARE(selectionModel.selectedIndexes().first() , model.index(0,0));
       
  2066 }
       
  2067 
       
  2068 void tst_QItemSelectionModel::merge_data()
       
  2069 {
       
  2070     QTest::addColumn<QItemSelection>("init");
       
  2071     QTest::addColumn<QItemSelection>("other");
       
  2072     QTest::addColumn<int>("command");
       
  2073     QTest::addColumn<QItemSelection>("result");
       
  2074 
       
  2075     QTest::newRow("Simple select")
       
  2076         << QItemSelection()
       
  2077         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2078         << int(QItemSelectionModel::Select)
       
  2079         << QItemSelection(model->index(2, 1) , model->index(3, 4));
       
  2080 
       
  2081     QTest::newRow("Simple deselect")
       
  2082         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2083         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2084         << int(QItemSelectionModel::Deselect)
       
  2085         << QItemSelection();
       
  2086 
       
  2087     QTest::newRow("Simple Toggle deselect")
       
  2088         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2089         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2090         << int(QItemSelectionModel::Toggle)
       
  2091         << QItemSelection();
       
  2092 
       
  2093     QTest::newRow("Simple Toggle select")
       
  2094         << QItemSelection()
       
  2095         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2096         << int(QItemSelectionModel::Toggle)
       
  2097         << QItemSelection(model->index(2, 1) , model->index(3, 4));
       
  2098 
       
  2099     QTest::newRow("Add select")
       
  2100         << QItemSelection(model->index(2, 1) , model->index(3, 3))
       
  2101         << QItemSelection(model->index(2, 2) , model->index(3, 4))
       
  2102         << int(QItemSelectionModel::Select)
       
  2103         << QItemSelection(model->index(2, 1) , model->index(3, 4));
       
  2104 
       
  2105     QTest::newRow("Deselect")
       
  2106         << QItemSelection(model->index(2, 1) , model->index(3, 4))
       
  2107         << QItemSelection(model->index(2, 2) , model->index(3, 4))
       
  2108         << int(QItemSelectionModel::Deselect)
       
  2109         << QItemSelection(model->index(2, 1) , model->index(3, 1));
       
  2110   
       
  2111     QItemSelection r1(model->index(2, 1) , model->index(3, 1));
       
  2112     r1.select(model->index(2, 4) , model->index(3, 4));
       
  2113     QTest::newRow("Toggle")
       
  2114         << QItemSelection(model->index(2, 1) , model->index(3, 3))
       
  2115         << QItemSelection(model->index(2, 2) , model->index(3, 4))
       
  2116         << int(QItemSelectionModel::Toggle)
       
  2117         << r1;
       
  2118 }
       
  2119 
       
  2120 
       
  2121 void tst_QItemSelectionModel::merge()
       
  2122 {
       
  2123     QFETCH(QItemSelection, init);
       
  2124     QFETCH(QItemSelection, other);
       
  2125     QFETCH(int, command);
       
  2126     QFETCH(QItemSelection, result);
       
  2127 
       
  2128     init.merge(other, QItemSelectionModel::SelectionFlags(command));
       
  2129 
       
  2130     foreach(const QModelIndex &idx, init.indexes())
       
  2131         QVERIFY(result.contains(idx));
       
  2132     foreach(const QModelIndex &idx, result.indexes())
       
  2133         QVERIFY(init.contains(idx));
       
  2134 }
       
  2135 
       
  2136 void tst_QItemSelectionModel::task119433_isRowSelected()
       
  2137 {
       
  2138     QStandardItemModel model(2,2);
       
  2139     model.setData(model.index(0,0), 0, Qt::UserRole - 1);
       
  2140     QItemSelectionModel sel(&model);
       
  2141     sel.select( QItemSelection(model.index(0,0), model.index(0, 1)), QItemSelectionModel::Select);
       
  2142     QCOMPARE(sel.selectedIndexes().count(), 1);
       
  2143     QVERIFY(sel.isRowSelected(0, QModelIndex()));
       
  2144 }
       
  2145 
       
  2146 void tst_QItemSelectionModel::task252069_rowIntersectsSelection()
       
  2147 {
       
  2148     QStandardItemModel m;
       
  2149     for (int i=0; i<8; ++i) {
       
  2150         for (int j=0; j<8; ++j) {
       
  2151             QStandardItem *item = new QStandardItem(QString("Item number %1").arg(i));
       
  2152             if ((i % 2 == 0 && j == 0)  ||
       
  2153                 (j % 2 == 0 && i == 0)  ||
       
  2154                  j == 5 || i == 5 ) {
       
  2155                 item->setEnabled(false);
       
  2156                 //item->setSelectable(false);
       
  2157             }
       
  2158             m.setItem(i, j, item);
       
  2159         }
       
  2160     }
       
  2161 
       
  2162     QItemSelectionModel selected(&m);
       
  2163     //nothing is selected
       
  2164     QVERIFY(!selected.rowIntersectsSelection(0, QModelIndex()));
       
  2165     QVERIFY(!selected.rowIntersectsSelection(2, QModelIndex()));
       
  2166     QVERIFY(!selected.rowIntersectsSelection(3, QModelIndex()));
       
  2167     QVERIFY(!selected.rowIntersectsSelection(5, QModelIndex()));
       
  2168     QVERIFY(!selected.columnIntersectsSelection(0, QModelIndex()));
       
  2169     QVERIFY(!selected.columnIntersectsSelection(2, QModelIndex()));
       
  2170     QVERIFY(!selected.columnIntersectsSelection(3, QModelIndex()));
       
  2171     QVERIFY(!selected.columnIntersectsSelection(5, QModelIndex()));
       
  2172     selected.select(m.index(2, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
  2173     QVERIFY(!selected.rowIntersectsSelection(0, QModelIndex()));
       
  2174     QVERIFY( selected.rowIntersectsSelection(2, QModelIndex()));
       
  2175     QVERIFY(!selected.rowIntersectsSelection(3, QModelIndex()));
       
  2176     QVERIFY(!selected.rowIntersectsSelection(5, QModelIndex()));
       
  2177     QVERIFY(!selected.columnIntersectsSelection(0, QModelIndex()));
       
  2178     QVERIFY( selected.columnIntersectsSelection(2, QModelIndex()));
       
  2179     QVERIFY( selected.columnIntersectsSelection(3, QModelIndex()));
       
  2180     QVERIFY(!selected.columnIntersectsSelection(5, QModelIndex()));
       
  2181     selected.select(m.index(0, 5), QItemSelectionModel::Select | QItemSelectionModel::Columns);
       
  2182     QVERIFY(!selected.rowIntersectsSelection(0, QModelIndex()));
       
  2183     QVERIFY( selected.rowIntersectsSelection(2, QModelIndex()));
       
  2184     QVERIFY(!selected.rowIntersectsSelection(3, QModelIndex()));
       
  2185     QVERIFY(!selected.rowIntersectsSelection(5, QModelIndex()));
       
  2186     QVERIFY(!selected.columnIntersectsSelection(0, QModelIndex()));
       
  2187     QVERIFY( selected.columnIntersectsSelection(2, QModelIndex()));
       
  2188     QVERIFY( selected.columnIntersectsSelection(3, QModelIndex()));
       
  2189     QVERIFY(!selected.columnIntersectsSelection(5, QModelIndex()));
       
  2190 }
       
  2191 
       
  2192 void tst_QItemSelectionModel::task232634_childrenDeselectionSignal()
       
  2193 {
       
  2194     QStandardItemModel model;
       
  2195 
       
  2196     QStandardItem *parentItem = model.invisibleRootItem();
       
  2197     for (int i = 0; i < 4; ++i) {
       
  2198         QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
       
  2199         parentItem->appendRow(item);
       
  2200         parentItem = item;
       
  2201     }
       
  2202 
       
  2203     QModelIndex root = model.index(0,0);
       
  2204     QModelIndex par = root.child(0,0);
       
  2205     QModelIndex sel = par.child(0,0);
       
  2206 
       
  2207     QItemSelectionModel selectionModel(&model);
       
  2208     selectionModel.select(sel, QItemSelectionModel::SelectCurrent);
       
  2209 
       
  2210     QSignalSpy deselectSpy(&selectionModel, SIGNAL(selectionChanged(const QItemSelection& , const QItemSelection&)));
       
  2211     model.removeRows(0, 1, root);
       
  2212     QVERIFY(deselectSpy.count() == 1);
       
  2213 
       
  2214     // More testing stress for the patch.
       
  2215     model.clear();
       
  2216     selectionModel.clear();
       
  2217 
       
  2218     parentItem = model.invisibleRootItem();
       
  2219     for (int i = 0; i < 2; ++i) {
       
  2220         QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
       
  2221         parentItem->appendRow(item);
       
  2222     }
       
  2223     for (int i = 0; i < 2; ++i) {
       
  2224         parentItem = model.invisibleRootItem()->child(i, 0);
       
  2225         for (int j = 0; j < 2; ++j) {
       
  2226             QStandardItem *item = new QStandardItem(QString("item %0.%1").arg(i).arg(j));
       
  2227             parentItem->appendRow(item);
       
  2228         }
       
  2229     }
       
  2230 
       
  2231     sel = model.index(0, 0).child(0, 0);
       
  2232     selectionModel.select(sel, QItemSelectionModel::Select);
       
  2233     QModelIndex sel2 = model.index(1, 0).child(0, 0);
       
  2234     selectionModel.select(sel2, QItemSelectionModel::Select);
       
  2235 
       
  2236     QVERIFY(selectionModel.selection().contains(sel));
       
  2237     QVERIFY(selectionModel.selection().contains(sel2));
       
  2238     deselectSpy.clear();
       
  2239     model.removeRow(0, model.index(0, 0));
       
  2240     QVERIFY(deselectSpy.count() == 1);
       
  2241     QVERIFY(!selectionModel.selection().contains(sel));
       
  2242     QVERIFY(selectionModel.selection().contains(sel2));
       
  2243 }
       
  2244 
       
  2245 void tst_QItemSelectionModel::task260134_layoutChangedWithAllSelected()
       
  2246 {
       
  2247     QStringListModel model( QStringList() << "foo" << "bar" << "foo2");
       
  2248     QSortFilterProxyModel proxy;
       
  2249     proxy.setSourceModel(&model);
       
  2250     QItemSelectionModel selection(&proxy);
       
  2251 
       
  2252 
       
  2253     QCOMPARE(model.rowCount(), 3);
       
  2254     QCOMPARE(proxy.rowCount(), 3);
       
  2255     proxy.setFilterRegExp( QRegExp("f"));
       
  2256     QCOMPARE(proxy.rowCount(), 2);
       
  2257 
       
  2258     QList<QPersistentModelIndex> indexList;
       
  2259     indexList << proxy.index(0,0) << proxy.index(1,0);
       
  2260     selection.select( QItemSelection(indexList.first(), indexList.last()), QItemSelectionModel::Select);
       
  2261 
       
  2262     //let's check the selection hasn't changed
       
  2263     QCOMPARE(selection.selectedIndexes().count(), indexList.count());
       
  2264     foreach(QPersistentModelIndex index, indexList)
       
  2265         QVERIFY(selection.isSelected(index));
       
  2266 
       
  2267     proxy.setFilterRegExp(QRegExp());
       
  2268     QCOMPARE(proxy.rowCount(), 3);
       
  2269 
       
  2270     //let's check the selection hasn't changed
       
  2271     QCOMPARE(selection.selectedIndexes().count(), indexList.count());
       
  2272     foreach(QPersistentModelIndex index, indexList)
       
  2273         QVERIFY(selection.isSelected(index));
       
  2274 }
       
  2275 
       
  2276 
       
  2277 QTEST_MAIN(tst_QItemSelectionModel)
       
  2278 #include "tst_qitemselectionmodel.moc"