tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    90     void merge();
    90     void merge();
    91     void task119433_isRowSelected();
    91     void task119433_isRowSelected();
    92     void task252069_rowIntersectsSelection();
    92     void task252069_rowIntersectsSelection();
    93     void task232634_childrenDeselectionSignal();
    93     void task232634_childrenDeselectionSignal();
    94     void task260134_layoutChangedWithAllSelected();
    94     void task260134_layoutChangedWithAllSelected();
       
    95     void QTBUG5671_layoutChangedWithAllSelected();
    95 
    96 
    96 private:
    97 private:
    97     QAbstractItemModel *model;
    98     QAbstractItemModel *model;
    98     QItemSelectionModel *selection;
    99     QItemSelectionModel *selection;
    99 };
   100 };
  2023 
  2024 
  2024 
  2025 
  2025 class QtTestTableModel: public QAbstractTableModel
  2026 class QtTestTableModel: public QAbstractTableModel
  2026 {
  2027 {
  2027     Q_OBJECT
  2028     Q_OBJECT
  2028     
  2029 
  2029     public:
  2030     public:
  2030         QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
  2031         QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
  2031         : QAbstractTableModel(parent),
  2032         : QAbstractTableModel(parent),
  2032         row_count(rows),
  2033         row_count(rows),
  2033         column_count(columns) {}
  2034         column_count(columns) {}
  2034         
  2035 
  2035         int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
  2036         int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
  2036         int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
  2037         int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
  2037         bool isEditable(const QModelIndex &) const { return true; }
  2038         bool isEditable(const QModelIndex &) const { return true; }
  2038         
  2039 
  2039         QVariant data(const QModelIndex &idx, int role) const
  2040         QVariant data(const QModelIndex &idx, int role) const
  2040         {
  2041         {
  2041             if (role == Qt::DisplayRole || role == Qt::EditRole)
  2042             if (role == Qt::DisplayRole || role == Qt::EditRole)
  2042                 return QString("[%1,%2]").arg(idx.row()).arg(idx.column());
  2043                 return QString("[%1,%2]").arg(idx.row()).arg(idx.column());
  2043             return QVariant();
  2044             return QVariant();
  2044         }
  2045         }
  2045         
  2046 
  2046         int row_count;
  2047         int row_count;
  2047         int column_count;
  2048         int column_count;
  2048         friend class tst_QItemSelectionModel;
  2049         friend class tst_QItemSelectionModel;
  2049 };
  2050 };
  2050 
  2051 
  2053 {
  2054 {
  2054     QtTestTableModel model(1,1);
  2055     QtTestTableModel model(1,1);
  2055     QItemSelectionModel selectionModel(&model);
  2056     QItemSelectionModel selectionModel(&model);
  2056     selectionModel.select(model.index(0,0), QItemSelectionModel::Select);
  2057     selectionModel.select(model.index(0,0), QItemSelectionModel::Select);
  2057     QCOMPARE(selectionModel.selectedIndexes().count() , 1);
  2058     QCOMPARE(selectionModel.selectedIndexes().count() , 1);
  2058     
  2059 
  2059     emit model.layoutAboutToBeChanged();
  2060     emit model.layoutAboutToBeChanged();
  2060     model.row_count = 5;
  2061     model.row_count = 5;
  2061     emit model.layoutChanged();
  2062     emit model.layoutChanged();
  2062 
  2063 
  2063     //The selection should not change.
  2064     //The selection should not change.
  2105     QTest::newRow("Deselect")
  2106     QTest::newRow("Deselect")
  2106         << QItemSelection(model->index(2, 1) , model->index(3, 4))
  2107         << QItemSelection(model->index(2, 1) , model->index(3, 4))
  2107         << QItemSelection(model->index(2, 2) , model->index(3, 4))
  2108         << QItemSelection(model->index(2, 2) , model->index(3, 4))
  2108         << int(QItemSelectionModel::Deselect)
  2109         << int(QItemSelectionModel::Deselect)
  2109         << QItemSelection(model->index(2, 1) , model->index(3, 1));
  2110         << QItemSelection(model->index(2, 1) , model->index(3, 1));
  2110   
  2111 
  2111     QItemSelection r1(model->index(2, 1) , model->index(3, 1));
  2112     QItemSelection r1(model->index(2, 1) , model->index(3, 1));
  2112     r1.select(model->index(2, 4) , model->index(3, 4));
  2113     r1.select(model->index(2, 4) , model->index(3, 4));
  2113     QTest::newRow("Toggle")
  2114     QTest::newRow("Toggle")
  2114         << QItemSelection(model->index(2, 1) , model->index(3, 3))
  2115         << QItemSelection(model->index(2, 1) , model->index(3, 3))
  2115         << QItemSelection(model->index(2, 2) , model->index(3, 4))
  2116         << QItemSelection(model->index(2, 2) , model->index(3, 4))
  2272     foreach(QPersistentModelIndex index, indexList)
  2273     foreach(QPersistentModelIndex index, indexList)
  2273         QVERIFY(selection.isSelected(index));
  2274         QVERIFY(selection.isSelected(index));
  2274 }
  2275 }
  2275 
  2276 
  2276 
  2277 
       
  2278 void tst_QItemSelectionModel::QTBUG5671_layoutChangedWithAllSelected()
       
  2279 {
       
  2280     struct MyFilterModel : public QSortFilterProxyModel
       
  2281     {     // Override sort filter proxy to remove even numbered rows.
       
  2282         bool filtering;
       
  2283         virtual bool filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const
       
  2284         {
       
  2285             return !filtering || !( source_row & 1 );
       
  2286         }
       
  2287     };
       
  2288 
       
  2289     //same as task260134_layoutChangedWithAllSelected but with a sightly bigger model
       
  2290 
       
  2291     enum { cNumRows=30, cNumCols=20 };
       
  2292 
       
  2293     QStandardItemModel model(cNumRows, cNumCols);
       
  2294     MyFilterModel proxy;
       
  2295     proxy.filtering = true;
       
  2296     proxy.setSourceModel(&model);
       
  2297     QItemSelectionModel selection(&proxy);
       
  2298 
       
  2299     // Populate the tree view.
       
  2300     for (unsigned int i = 0; i < cNumCols; i++)
       
  2301         model.setHeaderData( i, Qt::Horizontal, QString::fromLatin1("Column %1").arg(i));
       
  2302 
       
  2303     for (unsigned int r = 0; r < cNumRows; r++) {
       
  2304         for (unsigned int c = 0; c < cNumCols; c++) {
       
  2305             model.setData(model.index(r, c, QModelIndex()),
       
  2306                           QString::fromLatin1("r:%1/c:%2").arg(r, c));
       
  2307         }
       
  2308     }
       
  2309 
       
  2310 
       
  2311     QCOMPARE(model.rowCount(), int(cNumRows));
       
  2312     QCOMPARE(proxy.rowCount(), int(cNumRows/2));
       
  2313 
       
  2314     selection.select( QItemSelection(proxy.index(0,0), proxy.index(proxy.rowCount() - 1, proxy.columnCount() - 1)), QItemSelectionModel::Select);
       
  2315 
       
  2316     QList<QPersistentModelIndex> indexList;
       
  2317     foreach(const QModelIndex &id, selection.selectedIndexes())
       
  2318         indexList << id;
       
  2319 
       
  2320     proxy.filtering = false;
       
  2321     proxy.invalidate();
       
  2322     QCOMPARE(proxy.rowCount(), int(cNumRows));
       
  2323 
       
  2324     //let's check the selection hasn't changed
       
  2325     QCOMPARE(selection.selectedIndexes().count(), indexList.count());
       
  2326     foreach(QPersistentModelIndex index, indexList)
       
  2327         QVERIFY(selection.isSelected(index));
       
  2328 }
       
  2329 
  2277 QTEST_MAIN(tst_QItemSelectionModel)
  2330 QTEST_MAIN(tst_QItemSelectionModel)
  2278 #include "tst_qitemselectionmodel.moc"
  2331 #include "tst_qitemselectionmodel.moc"