1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtCore/QString> |
31 | #include <QtTest/QtTest> |
32 | |
33 | #include <QtCharts/QChart> |
34 | #include <QtCharts/QChartView> |
35 | #include <QtCharts/QBarSeries> |
36 | #include <QtCharts/QBarSet> |
37 | #include <QtCharts/QVBarModelMapper> |
38 | #include <QtCharts/QHBarModelMapper> |
39 | #include <QtGui/QStandardItemModel> |
40 | |
41 | QT_CHARTS_USE_NAMESPACE |
42 | |
43 | class tst_qbarmodelmapper : public QObject |
44 | { |
45 | Q_OBJECT |
46 | |
47 | public: |
48 | tst_qbarmodelmapper(); |
49 | void createVerticalMapper(); |
50 | void createHorizontalMapper(); |
51 | |
52 | private Q_SLOTS: |
53 | void initTestCase(); |
54 | void cleanupTestCase(); |
55 | void init(); |
56 | void cleanup(); |
57 | void verticalMapper_data(); |
58 | void verticalMapper(); |
59 | void verticalMapperCustomMapping_data(); |
60 | void verticalMapperCustomMapping(); |
61 | void horizontalMapper_data(); |
62 | void horizontalMapper(); |
63 | void horizontalMapperCustomMapping_data(); |
64 | void horizontalMapperCustomMapping(); |
65 | void seriesUpdated(); |
66 | void verticalModelInsertRows(); |
67 | void verticalModelRemoveRows(); |
68 | void verticalModelInsertColumns(); |
69 | void verticalModelRemoveColumns(); |
70 | void horizontalModelInsertRows(); |
71 | void horizontalModelRemoveRows(); |
72 | void horizontalModelInsertColumns(); |
73 | void horizontalModelRemoveColumns(); |
74 | void modelUpdateCell(); |
75 | void verticalMapperSignals(); |
76 | void horizontalMapperSignals(); |
77 | |
78 | private: |
79 | QStandardItemModel *m_model; |
80 | int m_modelRowCount; |
81 | int m_modelColumnCount; |
82 | |
83 | QVBarModelMapper *m_vMapper; |
84 | QHBarModelMapper *m_hMapper; |
85 | |
86 | QBarSeries *m_series; |
87 | QChart *m_chart; |
88 | QChartView *m_chartView; |
89 | }; |
90 | |
91 | tst_qbarmodelmapper::tst_qbarmodelmapper(): |
92 | m_model(0), |
93 | m_modelRowCount(10), |
94 | m_modelColumnCount(8), |
95 | m_vMapper(0), |
96 | m_hMapper(0), |
97 | m_series(0), |
98 | m_chart(0), |
99 | m_chartView(0) |
100 | { |
101 | } |
102 | |
103 | void tst_qbarmodelmapper::createVerticalMapper() |
104 | { |
105 | m_vMapper = new QVBarModelMapper; |
106 | QVERIFY(m_vMapper->model() == 0); |
107 | m_vMapper->setFirstBarSetColumn(0); |
108 | m_vMapper->setLastBarSetColumn(4); |
109 | m_vMapper->setModel(m_model); |
110 | m_vMapper->setSeries(m_series); |
111 | } |
112 | |
113 | void tst_qbarmodelmapper::createHorizontalMapper() |
114 | { |
115 | m_hMapper = new QHBarModelMapper; |
116 | QVERIFY(m_hMapper->model() == 0); |
117 | m_hMapper->setFirstBarSetRow(0); |
118 | m_hMapper->setLastBarSetRow(4); |
119 | m_hMapper->setModel(m_model); |
120 | m_hMapper->setSeries(m_series); |
121 | } |
122 | |
123 | void tst_qbarmodelmapper::init() |
124 | { |
125 | m_series = new QBarSeries; |
126 | m_chart->addSeries(series: m_series); |
127 | |
128 | m_model = new QStandardItemModel(m_modelRowCount, m_modelColumnCount, this); |
129 | for (int row = 0; row < m_modelRowCount; ++row) { |
130 | for (int column = 0; column < m_modelColumnCount; column++) { |
131 | m_model->setData(index: m_model->index(row, column), value: row * column); |
132 | } |
133 | } |
134 | } |
135 | |
136 | void tst_qbarmodelmapper::cleanup() |
137 | { |
138 | m_chart->removeSeries(series: m_series); |
139 | delete m_series; |
140 | m_series = 0; |
141 | |
142 | m_model->clear(); |
143 | m_model->deleteLater(); |
144 | m_model = 0; |
145 | |
146 | if (m_vMapper) { |
147 | m_vMapper->deleteLater(); |
148 | m_vMapper = 0; |
149 | } |
150 | |
151 | if (m_hMapper) { |
152 | m_hMapper->deleteLater(); |
153 | m_hMapper = 0; |
154 | } |
155 | } |
156 | |
157 | void tst_qbarmodelmapper::initTestCase() |
158 | { |
159 | m_chart = new QChart; |
160 | m_chartView = new QChartView(m_chart); |
161 | m_chartView->resize(w: 200, h: 200); |
162 | m_chartView->show(); |
163 | } |
164 | |
165 | void tst_qbarmodelmapper::cleanupTestCase() |
166 | { |
167 | delete m_chartView; |
168 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
169 | } |
170 | |
171 | void tst_qbarmodelmapper::verticalMapper_data() |
172 | { |
173 | QTest::addColumn<int>(name: "firstBarSetColumn" ); |
174 | QTest::addColumn<int>(name: "lastBarSetColumn" ); |
175 | QTest::addColumn<int>(name: "expectedBarSetCount" ); |
176 | QTest::newRow(dataTag: "lastBarSetColumn greater than firstBarSetColumn" ) << 0 << 1 << 2; |
177 | QTest::newRow(dataTag: "lastBarSetColumn equal to firstBarSetColumn" ) << 1 << 1 << 1; |
178 | QTest::newRow(dataTag: "lastBarSetColumn lesser than firstBarSetColumn" ) << 1 << 0 << 0; |
179 | QTest::newRow(dataTag: "invalid firstBarSetColumn and correct lastBarSetColumn" ) << -3 << 1 << 0; |
180 | QTest::newRow(dataTag: "firstBarSetColumn beyond the size of model and correct lastBarSetColumn" ) << m_modelColumnCount << 1 << 0; |
181 | QTest::newRow(dataTag: "firstBarSetColumn beyond the size of model and invalid lastBarSetColumn" ) << m_modelColumnCount << -1 << 0; |
182 | } |
183 | |
184 | void tst_qbarmodelmapper::verticalMapper() |
185 | { |
186 | QFETCH(int, firstBarSetColumn); |
187 | QFETCH(int, lastBarSetColumn); |
188 | QFETCH(int, expectedBarSetCount); |
189 | |
190 | QBarSeries *series = new QBarSeries; |
191 | |
192 | QVBarModelMapper *mapper = new QVBarModelMapper; |
193 | mapper->setFirstBarSetColumn(firstBarSetColumn); |
194 | mapper->setLastBarSetColumn(lastBarSetColumn); |
195 | mapper->setModel(m_model); |
196 | mapper->setSeries(series); |
197 | |
198 | m_chart->addSeries(series); |
199 | |
200 | QCOMPARE(series->count(), expectedBarSetCount); |
201 | QCOMPARE(mapper->firstBarSetColumn(), qMax(-1, firstBarSetColumn)); |
202 | QCOMPARE(mapper->lastBarSetColumn(), qMax(-1, lastBarSetColumn)); |
203 | |
204 | delete mapper; |
205 | m_chart->removeSeries(series); |
206 | delete series; |
207 | } |
208 | |
209 | void tst_qbarmodelmapper::verticalMapperCustomMapping_data() |
210 | { |
211 | QTest::addColumn<int>(name: "first" ); |
212 | QTest::addColumn<int>(name: "countLimit" ); |
213 | QTest::addColumn<int>(name: "expectedBarSetCount" ); |
214 | QTest::addColumn<int>(name: "expectedCount" ); |
215 | QTest::newRow(dataTag: "first: 0, unlimited count" ) << 0 << -1 << 2 << m_modelRowCount; |
216 | QTest::newRow(dataTag: "first: 3, unlimited count" ) << 3 << -1 << 2 << m_modelRowCount - 3; |
217 | QTest::newRow(dataTag: "first: 0, count: 5" ) << 0 << 5 << 2 << qMin(a: 5, b: m_modelRowCount); |
218 | QTest::newRow(dataTag: "first: 3, count: 5" ) << 3 << 5 << 2 << qMin(a: 5, b: m_modelRowCount - 3); |
219 | QTest::newRow(dataTag: "first: +1 greater then the number of rows in the model, unlimited count" ) << m_modelRowCount + 1 << -1 << 0 << 0; |
220 | QTest::newRow(dataTag: "first: +1 greater then the number of rows in the model, count: 5" ) << m_modelRowCount + 1 << 5 << 0 << 0; |
221 | QTest::newRow(dataTag: "first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)" ) << 0 << m_modelRowCount + 3 << 2 << m_modelRowCount; |
222 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), unlimited count" ) << -3 << -1 << 2 << m_modelRowCount; |
223 | QTest::newRow(dataTag: "first: 0, count: -3 (invalid - shlould default to -1)" ) << 0 << -3 << 2 << m_modelRowCount; |
224 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)" ) << -3 << -3 << 2 << m_modelRowCount; |
225 | } |
226 | |
227 | void tst_qbarmodelmapper::verticalMapperCustomMapping() |
228 | { |
229 | QFETCH(int, first); |
230 | QFETCH(int, countLimit); |
231 | QFETCH(int, expectedBarSetCount); |
232 | QFETCH(int, expectedCount); |
233 | |
234 | QBarSeries *series = new QBarSeries; |
235 | |
236 | QCOMPARE(series->count(), 0); |
237 | |
238 | QVBarModelMapper *mapper = new QVBarModelMapper; |
239 | mapper->setFirstBarSetColumn(0); |
240 | mapper->setLastBarSetColumn(1); |
241 | mapper->setModel(m_model); |
242 | mapper->setSeries(series); |
243 | mapper->setFirstRow(first); |
244 | mapper->setRowCount(countLimit); |
245 | m_chart->addSeries(series); |
246 | |
247 | QCOMPARE(series->count(), expectedBarSetCount); |
248 | |
249 | if (expectedBarSetCount > 0) |
250 | QCOMPARE(series->barSets().first()->count(), expectedCount); |
251 | |
252 | // change values column mapping to invalid |
253 | mapper->setFirstBarSetColumn(-1); |
254 | mapper->setLastBarSetColumn(1); |
255 | |
256 | QCOMPARE(series->count(), 0); |
257 | |
258 | delete mapper; |
259 | m_chart->removeSeries(series); |
260 | delete series; |
261 | } |
262 | |
263 | void tst_qbarmodelmapper::horizontalMapper_data() |
264 | { |
265 | QTest::addColumn<int>(name: "firstBarSetRow" ); |
266 | QTest::addColumn<int>(name: "lastBarSetRow" ); |
267 | QTest::addColumn<int>(name: "expectedBarSetCount" ); |
268 | QTest::newRow(dataTag: "lastBarSetRow greater than firstBarSetRow" ) << 0 << 1 << 2; |
269 | QTest::newRow(dataTag: "lastBarSetRow equal to firstBarSetRow" ) << 1 << 1 << 1; |
270 | QTest::newRow(dataTag: "lastBarSetRow lesser than firstBarSetRow" ) << 1 << 0 << 0; |
271 | QTest::newRow(dataTag: "invalid firstBarSetRow and correct lastBarSetRow" ) << -3 << 1 << 0; |
272 | QTest::newRow(dataTag: "firstBarSetRow beyond the size of model and correct lastBarSetRow" ) << m_modelRowCount << 1 << 0; |
273 | QTest::newRow(dataTag: "firstBarSetRow beyond the size of model and invalid lastBarSetRow" ) << m_modelRowCount << -1 << 0; |
274 | } |
275 | |
276 | void tst_qbarmodelmapper::horizontalMapper() |
277 | { |
278 | QFETCH(int, firstBarSetRow); |
279 | QFETCH(int, lastBarSetRow); |
280 | QFETCH(int, expectedBarSetCount); |
281 | |
282 | QBarSeries *series = new QBarSeries; |
283 | |
284 | QHBarModelMapper *mapper = new QHBarModelMapper; |
285 | mapper->setFirstBarSetRow(firstBarSetRow); |
286 | mapper->setLastBarSetRow(lastBarSetRow); |
287 | mapper->setModel(m_model); |
288 | mapper->setSeries(series); |
289 | |
290 | m_chart->addSeries(series); |
291 | |
292 | QCOMPARE(series->count(), expectedBarSetCount); |
293 | QCOMPARE(mapper->firstBarSetRow(), qMax(-1, firstBarSetRow)); |
294 | QCOMPARE(mapper->lastBarSetRow(), qMax(-1, lastBarSetRow)); |
295 | |
296 | delete mapper; |
297 | m_chart->removeSeries(series); |
298 | delete series; |
299 | } |
300 | |
301 | void tst_qbarmodelmapper::horizontalMapperCustomMapping_data() |
302 | { |
303 | QTest::addColumn<int>(name: "first" ); |
304 | QTest::addColumn<int>(name: "countLimit" ); |
305 | QTest::addColumn<int>(name: "expectedBarSetCount" ); |
306 | QTest::addColumn<int>(name: "expectedCount" ); |
307 | QTest::newRow(dataTag: "first: 0, unlimited count" ) << 0 << -1 << 2 << m_modelColumnCount; |
308 | QTest::newRow(dataTag: "first: 3, unlimited count" ) << 3 << -1 << 2 << m_modelColumnCount - 3; |
309 | QTest::newRow(dataTag: "first: 0, count: 5" ) << 0 << 5 << 2 << qMin(a: 5, b: m_modelColumnCount); |
310 | QTest::newRow(dataTag: "first: 3, count: 5" ) << 3 << 5 << 2 << qMin(a: 5, b: m_modelColumnCount - 3); |
311 | QTest::newRow(dataTag: "first: +1 greater then the number of rows in the model, unlimited count" ) << m_modelColumnCount + 1 << -1 << 0 << 0; |
312 | QTest::newRow(dataTag: "first: +1 greater then the number of rows in the model, count: 5" ) << m_modelColumnCount + 1 << 5 << 0 << 0; |
313 | QTest::newRow(dataTag: "first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)" ) << 0 << m_modelColumnCount + 3 << 2 << m_modelColumnCount; |
314 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), unlimited count" ) << -3 << -1 << 2 << m_modelColumnCount; |
315 | QTest::newRow(dataTag: "first: 0, count: -3 (invalid - shlould default to -1)" ) << 0 << -3 << 2 << m_modelColumnCount; |
316 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)" ) << -3 << -3 << 2 << m_modelColumnCount; |
317 | } |
318 | |
319 | void tst_qbarmodelmapper::horizontalMapperCustomMapping() |
320 | { |
321 | QFETCH(int, first); |
322 | QFETCH(int, countLimit); |
323 | QFETCH(int, expectedBarSetCount); |
324 | QFETCH(int, expectedCount); |
325 | |
326 | QBarSeries *series = new QBarSeries; |
327 | |
328 | QCOMPARE(series->count(), 0); |
329 | |
330 | QHBarModelMapper *mapper = new QHBarModelMapper; |
331 | mapper->setFirstBarSetRow(0); |
332 | mapper->setLastBarSetRow(1); |
333 | mapper->setModel(m_model); |
334 | mapper->setSeries(series); |
335 | mapper->setFirstColumn(first); |
336 | mapper->setColumnCount(countLimit); |
337 | m_chart->addSeries(series); |
338 | |
339 | QCOMPARE(series->count(), expectedBarSetCount); |
340 | |
341 | if (expectedBarSetCount > 0) |
342 | QCOMPARE(series->barSets().first()->count(), expectedCount); |
343 | |
344 | // change values column mapping to invalid |
345 | mapper->setFirstBarSetRow(-1); |
346 | mapper->setLastBarSetRow(1); |
347 | |
348 | QCOMPARE(series->count(), 0); |
349 | |
350 | delete mapper; |
351 | m_chart->removeSeries(series); |
352 | delete series; |
353 | } |
354 | |
355 | void tst_qbarmodelmapper::seriesUpdated() |
356 | { |
357 | // setup the mapper |
358 | createVerticalMapper(); |
359 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
360 | QCOMPARE(m_vMapper->rowCount(), -1); |
361 | |
362 | m_series->barSets().first()->append(value: 123); |
363 | QCOMPARE(m_model->rowCount(), m_modelRowCount + 1); |
364 | QCOMPARE(m_vMapper->rowCount(), -1); // the value should not change as it indicates 'all' items there are in the model |
365 | |
366 | m_series->barSets().last()->remove(index: 0, count: m_modelRowCount); |
367 | QCOMPARE(m_model->rowCount(), 1); |
368 | QCOMPARE(m_vMapper->rowCount(), -1); // the value should not change as it indicates 'all' items there are in the model |
369 | |
370 | m_series->barSets().first()->replace(index: 0, value: 444.0); |
371 | QCOMPARE(m_model->data(m_model->index(0, 0)).toReal(), 444.0); |
372 | |
373 | m_series->barSets().first()->setLabel("Hello" ); |
374 | QCOMPARE(m_model->headerData(0, Qt::Horizontal).toString(), QString("Hello" )); |
375 | |
376 | QList<qreal> newValues; |
377 | newValues << 15 << 27 << 35 << 49; |
378 | m_series->barSets().first()->append(values: newValues); |
379 | QCOMPARE(m_model->rowCount(), 1 + newValues.count()); |
380 | |
381 | QList<QBarSet* > newBarSets; |
382 | QBarSet* newBarSet_1 = new QBarSet("New_1" ); |
383 | newBarSet_1->append(value: 101); |
384 | newBarSet_1->append(value: 102); |
385 | newBarSet_1->append(value: 103); |
386 | newBarSets.append(t: newBarSet_1); |
387 | |
388 | QBarSet* newBarSet_2 = new QBarSet("New_2" ); |
389 | newBarSet_2->append(value: 201); |
390 | newBarSet_2->append(value: 202); |
391 | newBarSet_2->append(value: 203); |
392 | newBarSets.append(t: newBarSet_2); |
393 | |
394 | m_series->append(sets: newBarSets); |
395 | QCOMPARE(m_model->columnCount(), m_modelColumnCount + newBarSets.count()); |
396 | } |
397 | |
398 | void tst_qbarmodelmapper::verticalModelInsertRows() |
399 | { |
400 | // setup the mapper |
401 | createVerticalMapper(); |
402 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
403 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
404 | QVERIFY(m_vMapper->model() != 0); |
405 | |
406 | int insertCount = 4; |
407 | m_model->insertRows(row: 3, count: insertCount); |
408 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
409 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount + insertCount); |
410 | |
411 | int first = 3; |
412 | m_vMapper->setFirstRow(3); |
413 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
414 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount + insertCount - first); |
415 | |
416 | m_model->insertRows(row: 3, count: insertCount); |
417 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
418 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount + 2 * insertCount - first); |
419 | |
420 | int countLimit = 6; |
421 | m_vMapper->setRowCount(countLimit); |
422 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
423 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelRowCount + 2 * insertCount - first)); |
424 | |
425 | m_model->insertRows(row: 3, count: insertCount); |
426 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
427 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelRowCount + 3 * insertCount - first)); |
428 | |
429 | m_vMapper->setFirstRow(0); |
430 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
431 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelRowCount + 3 * insertCount)); |
432 | |
433 | m_vMapper->setRowCount(-1); |
434 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
435 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount + 3 * insertCount); |
436 | } |
437 | |
438 | void tst_qbarmodelmapper::verticalModelRemoveRows() |
439 | { |
440 | // setup the mapper |
441 | createVerticalMapper(); |
442 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
443 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
444 | QVERIFY(m_vMapper->model() != 0); |
445 | |
446 | int removeCount = 2; |
447 | m_model->removeRows(row: 1, count: removeCount); |
448 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
449 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount - removeCount); |
450 | |
451 | int first = 1; |
452 | m_vMapper->setFirstRow(first); |
453 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
454 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount - removeCount - first); |
455 | |
456 | m_model->removeRows(row: 1, count: removeCount); |
457 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
458 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount - 2 * removeCount - first); |
459 | |
460 | int countLimit = 3; |
461 | m_vMapper->setRowCount(countLimit); |
462 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
463 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelRowCount - 2 * removeCount - first)); |
464 | |
465 | m_model->removeRows(row: 1, count: removeCount); |
466 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
467 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelRowCount - 3 * removeCount - first)); |
468 | |
469 | m_vMapper->setFirstRow(0); |
470 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
471 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelRowCount - 3 * removeCount)); |
472 | |
473 | m_vMapper->setRowCount(-1); |
474 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
475 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount - 3 * removeCount); |
476 | } |
477 | |
478 | void tst_qbarmodelmapper::verticalModelInsertColumns() |
479 | { |
480 | // setup the mapper |
481 | createVerticalMapper(); |
482 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
483 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
484 | QVERIFY(m_vMapper->model() != 0); |
485 | |
486 | int insertCount = 4; |
487 | m_model->insertColumns(column: 3, count: insertCount); |
488 | QCOMPARE(m_series->count(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1); |
489 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
490 | } |
491 | |
492 | void tst_qbarmodelmapper::verticalModelRemoveColumns() |
493 | { |
494 | // setup the mapper |
495 | createVerticalMapper(); |
496 | QCOMPARE(m_series->count(), qMin(m_model->columnCount(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1)); |
497 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
498 | QVERIFY(m_vMapper->model() != 0); |
499 | |
500 | int removeCount = m_modelColumnCount - 2; |
501 | m_model->removeColumns(column: 0, count: removeCount); |
502 | QCOMPARE(m_series->count(), qMin(m_model->columnCount(), m_vMapper->lastBarSetColumn() - m_vMapper->firstBarSetColumn() + 1)); |
503 | QCOMPARE(m_series->barSets().first()->count(), m_modelRowCount); |
504 | |
505 | // leave all the columns |
506 | m_model->removeColumns(column: 0, count: m_modelColumnCount - removeCount); |
507 | QCOMPARE(m_series->count(), 0); |
508 | } |
509 | |
510 | void tst_qbarmodelmapper::horizontalModelInsertRows() |
511 | { |
512 | // setup the mapper |
513 | createHorizontalMapper(); |
514 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
515 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount); |
516 | QVERIFY(m_hMapper->model() != 0); |
517 | |
518 | int insertCount = 4; |
519 | m_model->insertRows(row: 3, count: insertCount); |
520 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
521 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount); |
522 | } |
523 | |
524 | void tst_qbarmodelmapper::horizontalModelRemoveRows() |
525 | { |
526 | // setup the mapper |
527 | createHorizontalMapper(); |
528 | QCOMPARE(m_series->count(), qMin(m_model->rowCount(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1)); |
529 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount); |
530 | QVERIFY(m_hMapper->model() != 0); |
531 | |
532 | int removeCount = m_modelRowCount - 2; |
533 | m_model->removeRows(row: 0, count: removeCount); |
534 | QCOMPARE(m_series->count(), qMin(m_model->rowCount(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1)); |
535 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount); |
536 | |
537 | // leave all the columns |
538 | m_model->removeRows(row: 0, count: m_modelRowCount - removeCount); |
539 | QCOMPARE(m_series->count(), 0); |
540 | } |
541 | |
542 | void tst_qbarmodelmapper::horizontalModelInsertColumns() |
543 | { |
544 | // setup the mapper |
545 | createHorizontalMapper(); |
546 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
547 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount); |
548 | QVERIFY(m_hMapper->model() != 0); |
549 | |
550 | int insertCount = 4; |
551 | m_model->insertColumns(column: 3, count: insertCount); |
552 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
553 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount + insertCount); |
554 | |
555 | int first = 3; |
556 | m_hMapper->setFirstColumn(3); |
557 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
558 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount + insertCount - first); |
559 | |
560 | m_model->insertColumns(column: 3, count: insertCount); |
561 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
562 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount + 2 * insertCount - first); |
563 | |
564 | int countLimit = 6; |
565 | m_hMapper->setColumnCount(countLimit); |
566 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
567 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelColumnCount + 2 * insertCount - first)); |
568 | |
569 | m_model->insertColumns(column: 3, count: insertCount); |
570 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
571 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelColumnCount + 3 * insertCount - first)); |
572 | |
573 | m_hMapper->setFirstColumn(0); |
574 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
575 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelColumnCount + 3 * insertCount)); |
576 | |
577 | m_hMapper->setColumnCount(-1); |
578 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
579 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount + 3 * insertCount); |
580 | } |
581 | |
582 | void tst_qbarmodelmapper::horizontalModelRemoveColumns() |
583 | { |
584 | // setup the mapper |
585 | createHorizontalMapper(); |
586 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
587 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount); |
588 | QVERIFY(m_hMapper->model() != 0); |
589 | |
590 | int removeCount = 2; |
591 | m_model->removeColumns(column: 1, count: removeCount); |
592 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
593 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount - removeCount); |
594 | |
595 | int first = 1; |
596 | m_hMapper->setFirstColumn(first); |
597 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
598 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount - removeCount - first); |
599 | |
600 | m_model->removeColumns(column: 1, count: removeCount); |
601 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
602 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount - 2 * removeCount - first); |
603 | |
604 | int countLimit = 3; |
605 | m_hMapper->setColumnCount(countLimit); |
606 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
607 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelColumnCount - 2 * removeCount - first)); |
608 | |
609 | m_model->removeColumns(column: 1, count: removeCount); |
610 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
611 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelColumnCount - 3 * removeCount - first)); |
612 | |
613 | m_hMapper->setFirstColumn(0); |
614 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
615 | QCOMPARE(m_series->barSets().first()->count(), qMin(countLimit, m_modelColumnCount - 3 * removeCount)); |
616 | |
617 | m_hMapper->setColumnCount(-1); |
618 | QCOMPARE(m_series->count(), m_hMapper->lastBarSetRow() - m_hMapper->firstBarSetRow() + 1); |
619 | QCOMPARE(m_series->barSets().first()->count(), m_modelColumnCount - 3 * removeCount); |
620 | } |
621 | |
622 | void tst_qbarmodelmapper::modelUpdateCell() |
623 | { |
624 | // setup the mapper |
625 | createVerticalMapper(); |
626 | |
627 | QVERIFY(m_model->setData(m_model->index(1, 0), 44)); |
628 | QCOMPARE(m_series->barSets().at(0)->at(1), 44.0); |
629 | QCOMPARE(m_model->data(m_model->index(1, 0)).toReal(), 44.0); |
630 | } |
631 | |
632 | void tst_qbarmodelmapper::verticalMapperSignals() |
633 | { |
634 | QVBarModelMapper *mapper = new QVBarModelMapper; |
635 | |
636 | QSignalSpy spy0(mapper, SIGNAL(firstRowChanged())); |
637 | QSignalSpy spy1(mapper, SIGNAL(rowCountChanged())); |
638 | QSignalSpy spy2(mapper, SIGNAL(firstBarSetColumnChanged())); |
639 | QSignalSpy spy3(mapper, SIGNAL(lastBarSetColumnChanged())); |
640 | QSignalSpy spy4(mapper, SIGNAL(modelReplaced())); |
641 | QSignalSpy spy5(mapper, SIGNAL(seriesReplaced())); |
642 | |
643 | mapper->setFirstBarSetColumn(0); |
644 | mapper->setLastBarSetColumn(1); |
645 | mapper->setModel(m_model); |
646 | mapper->setSeries(m_series); |
647 | mapper->setFirstRow(1); |
648 | mapper->setRowCount(5); |
649 | |
650 | QCOMPARE(spy0.count(), 1); |
651 | QCOMPARE(spy1.count(), 1); |
652 | QCOMPARE(spy2.count(), 1); |
653 | QCOMPARE(spy3.count(), 1); |
654 | QCOMPARE(spy4.count(), 1); |
655 | QCOMPARE(spy5.count(), 1); |
656 | |
657 | delete mapper; |
658 | } |
659 | |
660 | void tst_qbarmodelmapper::horizontalMapperSignals() |
661 | { |
662 | QHBarModelMapper *mapper = new QHBarModelMapper; |
663 | |
664 | QSignalSpy spy0(mapper, SIGNAL(firstColumnChanged())); |
665 | QSignalSpy spy1(mapper, SIGNAL(columnCountChanged())); |
666 | QSignalSpy spy2(mapper, SIGNAL(firstBarSetRowChanged())); |
667 | QSignalSpy spy3(mapper, SIGNAL(lastBarSetRowChanged())); |
668 | QSignalSpy spy4(mapper, SIGNAL(modelReplaced())); |
669 | QSignalSpy spy5(mapper, SIGNAL(seriesReplaced())); |
670 | |
671 | mapper->setFirstBarSetRow(0); |
672 | mapper->setLastBarSetRow(1); |
673 | mapper->setModel(m_model); |
674 | mapper->setSeries(m_series); |
675 | mapper->setFirstColumn(1); |
676 | mapper->setColumnCount(5); |
677 | |
678 | QCOMPARE(spy0.count(), 1); |
679 | QCOMPARE(spy1.count(), 1); |
680 | QCOMPARE(spy2.count(), 1); |
681 | QCOMPARE(spy3.count(), 1); |
682 | QCOMPARE(spy4.count(), 1); |
683 | QCOMPARE(spy5.count(), 1); |
684 | |
685 | delete mapper; |
686 | } |
687 | |
688 | QTEST_MAIN(tst_qbarmodelmapper) |
689 | |
690 | #include "tst_qbarmodelmapper.moc" |
691 | |