1
/* This file is part of the KDE project
2
Copyright (C) 2010 Dag Andersen <danders@get2net.dk>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
19
#include "FlatProxyModelTester.h"
22
#include <QModelIndex>
23
#include <QStandardItemModel>
24
#include <QStandardItem>
26
#include <qtest_kde.h>
32
void FlatProxyModelTester::emptyModel()
34
QCOMPARE( m_flatmodel.columnCount(), 0 );
35
QCOMPARE( m_flatmodel.rowCount(), 0 );
38
void FlatProxyModelTester::test()
40
qDebug()<<"set 1 row, 1 column";
41
m_standardmodel.setColumnCount( 1 );
42
m_standardmodel.setRowCount( 1 );
44
m_standardmodel.setHorizontalHeaderLabels( QStringList() << "Column 1" );
45
m_flatmodel.setSourceModel( &m_standardmodel );
47
QCOMPARE( m_flatmodel.columnCount(), 2 ); // it adds an extra column
48
QCOMPARE( m_flatmodel.rowCount(), 1 );
49
QCOMPARE( m_flatmodel.headerData( 0, Qt::Horizontal ), QVariant( "Column 1" ) );
51
m_standardmodel.setData( m_standardmodel.index( 0, 0 ), "Index 0,0" );
53
QModelIndex idx = m_flatmodel.index( 0, 0 );
54
QVERIFY( idx.isValid() );
55
qDebug()<<"Index 0,0:"<<idx.data();
57
QCOMPARE( idx.data(), QVariant( "Index 0,0" ) );
59
qDebug()<<"1 row, set 2 columns";
60
m_standardmodel.setColumnCount( 2 );
61
QCOMPARE( m_flatmodel.columnCount(), 3 ); // it adds an extra column
62
m_flatmodel.setHeaderData( 1, Qt::Horizontal, "Column 2" );
63
QCOMPARE( m_flatmodel.headerData( 1, Qt::Horizontal ), QVariant( "Column 2" ) );
64
m_standardmodel.setData( m_standardmodel.index( 0, 1 ), "Index 0,1" );
65
idx = m_flatmodel.index( 0, 1 );
66
QVERIFY( idx.isValid() );
67
qDebug()<<"Index 0,1:"<<idx.data();
68
QCOMPARE( idx.data(), QVariant( "Index 0,1" ) );
70
qDebug()<<"Set 2 rows, 2 columns";
71
m_standardmodel.setRowCount( 2 );
72
QCOMPARE( m_flatmodel.rowCount(), 2 );
73
m_standardmodel.setData( m_standardmodel.index( 1, 0 ), "Index 1,0" );
74
idx = m_flatmodel.index( 1, 0 );
75
QVERIFY( idx.isValid() );
76
qDebug()<<"Index 1,0:"<<idx.data();
77
QCOMPARE( idx.data(), QVariant( "Index 1,0" ) );
79
m_standardmodel.setData( m_standardmodel.index( 1, 1 ), "Index 1,1" );
80
idx = m_flatmodel.index( 1, 1 );
81
QVERIFY( idx.isValid() );
82
qDebug()<<"Index 1,1:"<<idx.data();
83
QCOMPARE( idx.data(), QVariant( "Index 1,1" ) );
85
qDebug()<<"Add child on last index, adds a new row 1 in the flat model";
87
// NOTE: m_standardmodel.insertRow( 0, m_standardmodel.index( 1, 0 ) );
88
// does not work as there will be no columns and thus no valid indexes for this row
89
QStandardItem *item = m_standardmodel.itemFromIndex( m_standardmodel.index( 1, 0 ) );
90
QList<QStandardItem*> items;
91
items << new QStandardItem( "Child last column 1" ) << new QStandardItem( "Child last column 2" );
92
item->appendRow( items );
93
QCOMPARE( m_flatmodel.rowCount(), 3 );
94
idx = m_flatmodel.index( 2, 0 );
95
QCOMPARE( idx.data(), QVariant( "Child last column 1" ) );
96
idx = m_flatmodel.index( 2, 1 );
97
QCOMPARE( idx.data(), QVariant( "Child last column 2" ) );
99
qDebug()<<"add child on first index";
101
item = m_standardmodel.itemFromIndex( m_standardmodel.index( 0, 0 ) );
103
items << new QStandardItem( "Child first column 1" ) << new QStandardItem( "Child first column 2" );
104
item->appendRow( items );
105
QCOMPARE( m_flatmodel.rowCount(), 4 );
106
idx = m_flatmodel.index( 1, 0 );
107
QCOMPARE( idx.data(), QVariant( "Child first column 1" ) );
108
idx = m_flatmodel.index( 1, 1 );
109
QCOMPARE( idx.data(), QVariant( "Child first column 2" ) );
111
qDebug()<<"add row (2) on top level between first and last";
113
item = m_standardmodel.invisibleRootItem();
115
items << new QStandardItem( "New index 1,0" ) << new QStandardItem( "New index 1,1" );
116
item->insertRow( 1, items );
117
QCOMPARE( m_flatmodel.rowCount(), 5 );
118
idx = m_flatmodel.index( 2, 0 );
119
QCOMPARE( idx.data(), QVariant( "New index 1,0" ) );
120
idx = m_flatmodel.index( 2, 1 );
121
QCOMPARE( idx.data(), QVariant( "New index 1,1" ) );
123
qDebug()<<"Add child on middle index, adds row 3 to flat model";
125
item = m_standardmodel.itemFromIndex( m_standardmodel.index( 1, 0 ) );
127
items << new QStandardItem( "Child middle column 1" ) << new QStandardItem( "Child middle column 2" );
128
item->appendRow( items );
129
QCOMPARE( m_flatmodel.rowCount(), 6 );
130
idx = m_flatmodel.index( 3, 0 );
131
QCOMPARE( idx.data().toString(), QVariant( "Child middle column 1" ).toString() );
132
idx = m_flatmodel.index( 3, 1 );
133
QCOMPARE( idx.data(), QVariant( "Child middle column 2" ) );
135
qDebug()<<"Add child on middle index's child, adds row 4 to flat model";
137
QModelIndex parent = m_standardmodel.index( 1, 0 );
138
QCOMPARE( parent.data().toString(), QString( "New index 1,0" ) );
139
idx = m_standardmodel.index( 0, 0, parent );
140
QCOMPARE( idx.data().toString(), QString( "Child middle column 1" ) );
141
item = m_standardmodel.itemFromIndex( idx );
143
items << new QStandardItem( "Child of middle child column 1" ) << new QStandardItem( "Child of middle child column 2" );
144
item->appendRow( items );
145
QCOMPARE( m_flatmodel.rowCount(), 7 );
146
idx = m_flatmodel.index( 4, 0 );
147
QCOMPARE( idx.data().toString(), QVariant( "Child of middle child column 1" ).toString() );
148
idx = m_flatmodel.index( 4, 1 );
149
QCOMPARE( idx.data(), QVariant( "Child of middle child column 2" ) );
151
qDebug()<<"Add child on middle index at row 0, adds row 3 to flat model";
153
idx = m_standardmodel.index( 1, 0 );
154
QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
155
item = m_standardmodel.itemFromIndex( idx );
157
items << new QStandardItem( "Index 1,0 -> Index 0,0" ) << new QStandardItem( "Index 1,0 -> Index 0,1" );
158
item->insertRow( 0, items );
159
QCOMPARE( m_flatmodel.rowCount(), 8 );
160
idx = m_flatmodel.index( 3, 0 );
161
QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 0,0" ).toString() );
162
idx = m_flatmodel.index( 3, 1 );
163
QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 0,1" ) );
165
qDebug()<<"Add child on middle index at row 1, adds row 4 to flat model";
167
idx = m_standardmodel.index( 1, 0 );
168
QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
169
item = m_standardmodel.itemFromIndex( idx );
171
items << new QStandardItem( "Index 1,0 -> Index 1,0" ) << new QStandardItem( "Index 1,0 -> Index 1,1" );
172
item->insertRow( 1, items );
173
QCOMPARE( m_flatmodel.rowCount(), 9 );
174
idx = m_flatmodel.index( 4, 0 );
175
QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 1,0" ).toString() );
176
idx = m_flatmodel.index( 4, 1 );
177
QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 1,1" ) );
179
qDebug()<<"Add child on middle index at last row (4), adds row 8 to flat model";
181
idx = m_standardmodel.index( 1, 0 );
182
QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
183
item = m_standardmodel.itemFromIndex( idx );
185
items << new QStandardItem( "Index 1,0 -> Index 4,0" ) << new QStandardItem( "Index 1,0 -> Index 4,1" );
186
item->insertRow( 3, items );
187
QCOMPARE( m_standardmodel.rowCount( m_standardmodel.index( 1, 0 ) ), 4 );
188
QCOMPARE( m_flatmodel.rowCount(), 10 );
189
idx = m_flatmodel.index( 7, 0 );
190
QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 4,0" ).toString() );
191
idx = m_flatmodel.index( 7, 1 );
192
QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 4,1" ) );
197
QTEST_KDEMAIN_CORE( KPlato::FlatProxyModelTester )
199
#include "FlatProxyModelTester.moc"