| 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/QXYSeries> |
| 36 | #include <QtCharts/QLineSeries> |
| 37 | #include <QtCharts/QVXYModelMapper> |
| 38 | #include <QtCharts/QHXYModelMapper> |
| 39 | #include <QtGui/QStandardItemModel> |
| 40 | #include "tst_definitions.h" |
| 41 | |
| 42 | QT_CHARTS_USE_NAMESPACE |
| 43 | |
| 44 | class tst_qxymodelmapper : public QObject |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | |
| 48 | public: |
| 49 | tst_qxymodelmapper(); |
| 50 | void createVerticalMapper(); |
| 51 | void createHorizontalMapper(); |
| 52 | |
| 53 | private Q_SLOTS: |
| 54 | void initTestCase(); |
| 55 | void cleanupTestCase(); |
| 56 | void init(); |
| 57 | void cleanup(); |
| 58 | void verticalMapper_data(); |
| 59 | void verticalMapper(); |
| 60 | void verticalMapperCustomMapping_data(); |
| 61 | void verticalMapperCustomMapping(); |
| 62 | void horizontalMapper_data(); |
| 63 | void horizontalMapper(); |
| 64 | void horizontalMapperCustomMapping_data(); |
| 65 | void horizontalMapperCustomMapping(); |
| 66 | void seriesUpdated(); |
| 67 | void verticalModelInsertRows(); |
| 68 | void verticalModelRemoveRows(); |
| 69 | void verticalModelInsertColumns(); |
| 70 | void verticalModelRemoveColumns(); |
| 71 | void horizontalModelInsertRows(); |
| 72 | void horizontalModelRemoveRows(); |
| 73 | void horizontalModelInsertColumns(); |
| 74 | void horizontalModelRemoveColumns(); |
| 75 | void modelUpdateCell(); |
| 76 | void verticalMapperSignals(); |
| 77 | void horizontalMapperSignals(); |
| 78 | |
| 79 | private: |
| 80 | QStandardItemModel *m_model; |
| 81 | int m_modelRowCount; |
| 82 | int m_modelColumnCount; |
| 83 | |
| 84 | QHXYModelMapper *m_hMapper; |
| 85 | QVXYModelMapper *m_vMapper; |
| 86 | |
| 87 | QXYSeries *m_series; |
| 88 | QChart *m_chart; |
| 89 | QChartView *m_chartView; |
| 90 | }; |
| 91 | |
| 92 | tst_qxymodelmapper::tst_qxymodelmapper(): |
| 93 | m_model(0), |
| 94 | m_modelRowCount(10), |
| 95 | m_modelColumnCount(8), |
| 96 | m_hMapper(0), |
| 97 | m_vMapper(0), |
| 98 | m_series(0), |
| 99 | m_chart(0), |
| 100 | m_chartView(0) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | void tst_qxymodelmapper::createVerticalMapper() |
| 105 | { |
| 106 | m_vMapper = new QVXYModelMapper; |
| 107 | QVERIFY(m_vMapper->model() == 0); |
| 108 | m_vMapper->setXColumn(0); |
| 109 | m_vMapper->setYColumn(1); |
| 110 | m_vMapper->setModel(m_model); |
| 111 | m_vMapper->setSeries(m_series); |
| 112 | } |
| 113 | |
| 114 | void tst_qxymodelmapper::createHorizontalMapper() |
| 115 | { |
| 116 | m_hMapper = new QHXYModelMapper; |
| 117 | QVERIFY(m_hMapper->model() == 0); |
| 118 | m_hMapper->setXRow(0); |
| 119 | m_hMapper->setYRow(1); |
| 120 | m_hMapper->setModel(m_model); |
| 121 | m_hMapper->setSeries(m_series); |
| 122 | } |
| 123 | |
| 124 | void tst_qxymodelmapper::init() |
| 125 | { |
| 126 | m_series = new QLineSeries; |
| 127 | m_chart->addSeries(series: m_series); |
| 128 | |
| 129 | m_model = new QStandardItemModel(m_modelRowCount, m_modelColumnCount, this); |
| 130 | for (int row = 0; row < m_modelRowCount; ++row) { |
| 131 | for (int column = 0; column < m_modelColumnCount; column++) { |
| 132 | m_model->setData(index: m_model->index(row, column), value: row * column); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void tst_qxymodelmapper::cleanup() |
| 138 | { |
| 139 | m_chart->removeSeries(series: m_series); |
| 140 | m_series->deleteLater(); |
| 141 | m_series = 0; |
| 142 | |
| 143 | m_model->clear(); |
| 144 | m_model->deleteLater(); |
| 145 | m_model = 0; |
| 146 | |
| 147 | if (m_vMapper) { |
| 148 | m_vMapper->deleteLater(); |
| 149 | m_vMapper = 0; |
| 150 | } |
| 151 | |
| 152 | if (m_hMapper) { |
| 153 | m_hMapper->deleteLater(); |
| 154 | m_hMapper = 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void tst_qxymodelmapper::initTestCase() |
| 159 | { |
| 160 | m_chart = newQChartOrQPolarChart(); |
| 161 | m_chartView = new QChartView(m_chart); |
| 162 | m_chartView->resize(w: 200, h: 200); |
| 163 | m_chartView->show(); |
| 164 | } |
| 165 | |
| 166 | void tst_qxymodelmapper::cleanupTestCase() |
| 167 | { |
| 168 | delete m_chartView; |
| 169 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
| 170 | } |
| 171 | |
| 172 | void tst_qxymodelmapper::verticalMapper_data() |
| 173 | { |
| 174 | QTest::addColumn<int>(name: "xColumn" ); |
| 175 | QTest::addColumn<int>(name: "yColumn" ); |
| 176 | QTest::addColumn<int>(name: "expectedCount" ); |
| 177 | QTest::newRow(dataTag: "different x and y columns" ) << 0 << 1 << m_modelRowCount; |
| 178 | QTest::newRow(dataTag: "same x and y columns" ) << 1 << 1 << m_modelRowCount; |
| 179 | QTest::newRow(dataTag: "invalid x column and correct y column" ) << -3 << 1 << 0; |
| 180 | QTest::newRow(dataTag: "x column beyond the size of model and correct y column" ) << m_modelColumnCount << 1 << 0; |
| 181 | QTest::newRow(dataTag: "x column beyond the size of model and invalid y column" ) << m_modelColumnCount << -1 << 0; |
| 182 | } |
| 183 | |
| 184 | void tst_qxymodelmapper::verticalMapper() |
| 185 | { |
| 186 | QFETCH(int, xColumn); |
| 187 | QFETCH(int, yColumn); |
| 188 | QFETCH(int, expectedCount); |
| 189 | |
| 190 | QVXYModelMapper *mapper = new QVXYModelMapper; |
| 191 | QVERIFY(mapper->model() == 0); |
| 192 | |
| 193 | mapper->setXColumn(xColumn); |
| 194 | mapper->setYColumn(yColumn); |
| 195 | mapper->setModel(m_model); |
| 196 | mapper->setSeries(m_series); |
| 197 | |
| 198 | QCOMPARE(m_series->count(), expectedCount); |
| 199 | QCOMPARE(mapper->xColumn(), qMax(-1, xColumn)); |
| 200 | QCOMPARE(mapper->yColumn(), qMax(-1, yColumn)); |
| 201 | |
| 202 | delete mapper; |
| 203 | mapper = 0; |
| 204 | } |
| 205 | |
| 206 | void tst_qxymodelmapper::verticalMapperCustomMapping_data() |
| 207 | { |
| 208 | QTest::addColumn<int>(name: "first" ); |
| 209 | QTest::addColumn<int>(name: "countLimit" ); |
| 210 | QTest::addColumn<int>(name: "expectedCount" ); |
| 211 | QTest::newRow(dataTag: "first: 0, unlimited count" ) << 0 << -1 << m_modelRowCount; |
| 212 | QTest::newRow(dataTag: "first: 3, unlimited count" ) << 3 << -1 << m_modelRowCount - 3; |
| 213 | QTest::newRow(dataTag: "first: 0, count: 5" ) << 0 << 5 << qMin(a: 5, b: m_modelRowCount); |
| 214 | QTest::newRow(dataTag: "first: 3, count: 5" ) << 3 << 5 << qMin(a: 5, b: m_modelRowCount - 3); |
| 215 | QTest::newRow(dataTag: "first: +1 greater then the number of rows in the model, unlimited count" ) << m_modelRowCount + 1 << -1 << 0; |
| 216 | QTest::newRow(dataTag: "first: +1 greater then the number of rows in the model, count: 5" ) << m_modelRowCount + 1 << 5 << 0; |
| 217 | 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 << m_modelRowCount; |
| 218 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), unlimited count" ) << -3 << -1 << m_modelRowCount; |
| 219 | QTest::newRow(dataTag: "first: 0, count: -3 (invalid - shlould default to -1)" ) << 0 << -3 << m_modelRowCount; |
| 220 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)" ) << -3 << -3 << m_modelRowCount; |
| 221 | |
| 222 | } |
| 223 | |
| 224 | void tst_qxymodelmapper::verticalMapperCustomMapping() |
| 225 | { |
| 226 | QFETCH(int, first); |
| 227 | QFETCH(int, countLimit); |
| 228 | QFETCH(int, expectedCount); |
| 229 | |
| 230 | QCOMPARE(m_series->count(), 0); |
| 231 | |
| 232 | QVXYModelMapper *mapper = new QVXYModelMapper; |
| 233 | mapper->setXColumn(0); |
| 234 | mapper->setYColumn(1); |
| 235 | mapper->setModel(m_model); |
| 236 | mapper->setSeries(m_series); |
| 237 | mapper->setFirstRow(first); |
| 238 | mapper->setRowCount(countLimit); |
| 239 | |
| 240 | QCOMPARE(m_series->count(), expectedCount); |
| 241 | |
| 242 | // change values column mapping to invalid |
| 243 | mapper->setXColumn(-1); |
| 244 | mapper->setYColumn(1); |
| 245 | |
| 246 | QCOMPARE(m_series->count(), 0); |
| 247 | |
| 248 | delete mapper; |
| 249 | mapper = 0; |
| 250 | } |
| 251 | |
| 252 | void tst_qxymodelmapper::horizontalMapper_data() |
| 253 | { |
| 254 | QTest::addColumn<int>(name: "xRow" ); |
| 255 | QTest::addColumn<int>(name: "yRow" ); |
| 256 | QTest::addColumn<int>(name: "expectedCount" ); |
| 257 | QTest::newRow(dataTag: "different x and y rows" ) << 0 << 1 << m_modelColumnCount; |
| 258 | QTest::newRow(dataTag: "same x and y rows" ) << 1 << 1 << m_modelColumnCount; |
| 259 | QTest::newRow(dataTag: "invalid x row and correct y row" ) << -3 << 1 << 0; |
| 260 | QTest::newRow(dataTag: "x row beyond the size of model and correct y row" ) << m_modelRowCount << 1 << 0; |
| 261 | QTest::newRow(dataTag: "x row beyond the size of model and invalid y row" ) << m_modelRowCount << -1 << 0; |
| 262 | } |
| 263 | |
| 264 | void tst_qxymodelmapper::horizontalMapper() |
| 265 | { |
| 266 | QFETCH(int, xRow); |
| 267 | QFETCH(int, yRow); |
| 268 | QFETCH(int, expectedCount); |
| 269 | |
| 270 | QHXYModelMapper *mapper = new QHXYModelMapper; |
| 271 | mapper->setXRow(xRow); |
| 272 | mapper->setYRow(yRow); |
| 273 | mapper->setModel(m_model); |
| 274 | mapper->setSeries(m_series); |
| 275 | |
| 276 | QCOMPARE(m_series->count(), expectedCount); |
| 277 | QCOMPARE(mapper->xRow(), qMax(-1, xRow)); |
| 278 | QCOMPARE(mapper->yRow(), qMax(-1, yRow)); |
| 279 | |
| 280 | delete mapper; |
| 281 | mapper = 0; |
| 282 | } |
| 283 | |
| 284 | void tst_qxymodelmapper::horizontalMapperCustomMapping_data() |
| 285 | { |
| 286 | QTest::addColumn<int>(name: "first" ); |
| 287 | QTest::addColumn<int>(name: "countLimit" ); |
| 288 | QTest::addColumn<int>(name: "expectedCount" ); |
| 289 | QTest::newRow(dataTag: "first: 0, unlimited count" ) << 0 << -1 << m_modelColumnCount; |
| 290 | QTest::newRow(dataTag: "first: 3, unlimited count" ) << 3 << -1 << m_modelColumnCount - 3; |
| 291 | QTest::newRow(dataTag: "first: 0, count: 5" ) << 0 << 5 << qMin(a: 5, b: m_modelColumnCount); |
| 292 | QTest::newRow(dataTag: "first: 3, count: 5" ) << 3 << 5 << qMin(a: 5, b: m_modelColumnCount - 3); |
| 293 | QTest::newRow(dataTag: "first: +1 greater then the number of columns in the model, unlimited count" ) << m_modelColumnCount + 1 << -1 << 0; |
| 294 | QTest::newRow(dataTag: "first: +1 greater then the number of columns in the model, count: 5" ) << m_modelColumnCount + 1 << 5 << 0; |
| 295 | QTest::newRow(dataTag: "first: 0, count: +3 greater than the number of columns in the model (should limit to the size of model)" ) << 0 << m_modelColumnCount + 3 << m_modelColumnCount; |
| 296 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), unlimited count" ) << -3 << -1 << m_modelColumnCount; |
| 297 | QTest::newRow(dataTag: "first: 0, count: -3 (invalid - shlould default to -1)" ) << 0 << -3 << m_modelColumnCount; |
| 298 | QTest::newRow(dataTag: "first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)" ) << -3 << -3 << m_modelColumnCount; |
| 299 | } |
| 300 | |
| 301 | void tst_qxymodelmapper::horizontalMapperCustomMapping() |
| 302 | { |
| 303 | QFETCH(int, first); |
| 304 | QFETCH(int, countLimit); |
| 305 | QFETCH(int, expectedCount); |
| 306 | |
| 307 | QCOMPARE(m_series->count(), 0); |
| 308 | |
| 309 | QHXYModelMapper *mapper = new QHXYModelMapper; |
| 310 | mapper->setXRow(0); |
| 311 | mapper->setYRow(1); |
| 312 | mapper->setModel(m_model); |
| 313 | mapper->setSeries(m_series); |
| 314 | mapper->setFirstColumn(first); |
| 315 | mapper->setColumnCount(countLimit); |
| 316 | |
| 317 | QCOMPARE(m_series->count(), expectedCount); |
| 318 | |
| 319 | // change values row mapping to invalid |
| 320 | mapper->setXRow(-1); |
| 321 | mapper->setYRow(1); |
| 322 | |
| 323 | QCOMPARE(m_series->count(), 0); |
| 324 | |
| 325 | delete mapper; |
| 326 | mapper = 0; |
| 327 | } |
| 328 | |
| 329 | void tst_qxymodelmapper::seriesUpdated() |
| 330 | { |
| 331 | // setup the mapper |
| 332 | createVerticalMapper(); |
| 333 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 334 | QCOMPARE(m_vMapper->rowCount(), -1); |
| 335 | |
| 336 | m_series->append(point: QPointF(100, 100)); |
| 337 | QCOMPARE(m_series->count(), m_modelRowCount + 1); |
| 338 | QCOMPARE(m_vMapper->rowCount(), -1); // the value should not change as it indicates 'all' items there are in the model |
| 339 | |
| 340 | m_series->remove(point: m_series->points().last()); |
| 341 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 342 | QCOMPARE(m_vMapper->rowCount(), -1); // the value should not change as it indicates 'all' items there are in the model |
| 343 | |
| 344 | m_series->removePoints(index: 1, count: m_modelRowCount - 4); |
| 345 | QCOMPARE(m_series->count(), 4); |
| 346 | QCOMPARE(m_vMapper->rowCount(), -1); // the value should not change as it indicates 'all' items there are in the model |
| 347 | |
| 348 | m_series->replace(oldPoint: m_series->points().first(), newPoint: QPointF(25.0, 75.0)); |
| 349 | QCOMPARE(m_model->data(m_model->index(0, 0)).toReal(), 25.0); |
| 350 | QCOMPARE(m_model->data(m_model->index(0, 1)).toReal(), 75.0); |
| 351 | } |
| 352 | |
| 353 | void tst_qxymodelmapper::verticalModelInsertRows() |
| 354 | { |
| 355 | // setup the mapper |
| 356 | createVerticalMapper(); |
| 357 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 358 | QVERIFY(m_vMapper->model() != 0); |
| 359 | |
| 360 | int insertCount = 4; |
| 361 | m_model->insertRows(row: 3, count: insertCount); |
| 362 | QCOMPARE(m_series->count(), m_modelRowCount + insertCount); |
| 363 | |
| 364 | int first = 3; |
| 365 | m_vMapper->setFirstRow(3); |
| 366 | QCOMPARE(m_series->count(), m_modelRowCount + insertCount - first); |
| 367 | |
| 368 | m_model->insertRows(row: 3, count: insertCount); |
| 369 | QCOMPARE(m_series->count(), m_modelRowCount + 2 * insertCount - first); |
| 370 | |
| 371 | int countLimit = 6; |
| 372 | m_vMapper->setRowCount(countLimit); |
| 373 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount + 2 * insertCount - first)); |
| 374 | |
| 375 | m_model->insertRows(row: 3, count: insertCount); |
| 376 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount + 3 * insertCount - first)); |
| 377 | |
| 378 | m_vMapper->setFirstRow(0); |
| 379 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount + 3 * insertCount)); |
| 380 | |
| 381 | m_vMapper->setRowCount(-1); |
| 382 | QCOMPARE(m_series->count(), m_modelRowCount + 3 * insertCount); |
| 383 | } |
| 384 | |
| 385 | void tst_qxymodelmapper::verticalModelRemoveRows() |
| 386 | { |
| 387 | // setup the mapper |
| 388 | createVerticalMapper(); |
| 389 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 390 | QVERIFY(m_vMapper->model() != 0); |
| 391 | |
| 392 | int removeCount = 2; |
| 393 | m_model->removeRows(row: 1, count: removeCount); |
| 394 | QCOMPARE(m_series->count(), m_modelRowCount - removeCount); |
| 395 | |
| 396 | int first = 1; |
| 397 | m_vMapper->setFirstRow(first); |
| 398 | QCOMPARE(m_series->count(), m_modelRowCount - removeCount - first); |
| 399 | |
| 400 | m_model->removeRows(row: 1, count: removeCount); |
| 401 | QCOMPARE(m_series->count(), m_modelRowCount - 2 * removeCount - first); |
| 402 | |
| 403 | int countLimit = 3; |
| 404 | m_vMapper->setRowCount(countLimit); |
| 405 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount - 2 * removeCount - first)); |
| 406 | |
| 407 | m_model->removeRows(row: 1, count: removeCount); |
| 408 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount - 3 * removeCount - first)); |
| 409 | |
| 410 | m_vMapper->setFirstRow(0); |
| 411 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount - 3 * removeCount)); |
| 412 | |
| 413 | m_vMapper->setRowCount(-1); |
| 414 | QCOMPARE(m_series->count(), m_modelRowCount - 3 * removeCount); |
| 415 | } |
| 416 | |
| 417 | void tst_qxymodelmapper::verticalModelInsertColumns() |
| 418 | { |
| 419 | // setup the mapper |
| 420 | createVerticalMapper(); |
| 421 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 422 | QVERIFY(m_vMapper->model() != 0); |
| 423 | |
| 424 | int insertCount = 4; |
| 425 | m_model->insertColumns(column: 3, count: insertCount); |
| 426 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 427 | } |
| 428 | |
| 429 | void tst_qxymodelmapper::verticalModelRemoveColumns() |
| 430 | { |
| 431 | // setup the mapper |
| 432 | createVerticalMapper(); |
| 433 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 434 | QVERIFY(m_vMapper->model() != 0); |
| 435 | |
| 436 | int removeCount = m_modelColumnCount - 2; |
| 437 | m_model->removeColumns(column: 0, count: removeCount); |
| 438 | QCOMPARE(m_series->count(), m_modelRowCount); |
| 439 | |
| 440 | // leave only one column |
| 441 | m_model->removeColumns(column: 0, count: m_modelColumnCount - removeCount - 1); |
| 442 | QCOMPARE(m_series->count(), 0); |
| 443 | } |
| 444 | |
| 445 | void tst_qxymodelmapper::horizontalModelInsertRows() |
| 446 | { |
| 447 | // setup the mapper |
| 448 | createHorizontalMapper(); |
| 449 | QCOMPARE(m_series->count(), m_modelColumnCount); |
| 450 | QVERIFY(m_hMapper->model() != 0); |
| 451 | |
| 452 | int insertCount = 4; |
| 453 | m_model->insertRows(row: 3, count: insertCount); |
| 454 | QCOMPARE(m_series->count(), m_modelColumnCount); |
| 455 | } |
| 456 | |
| 457 | void tst_qxymodelmapper::horizontalModelRemoveRows() |
| 458 | { |
| 459 | // setup the mapper |
| 460 | createHorizontalMapper(); |
| 461 | QCOMPARE(m_series->count(), m_modelColumnCount); |
| 462 | QVERIFY(m_hMapper->model() != 0); |
| 463 | |
| 464 | int removeCount = m_modelRowCount - 2; |
| 465 | m_model->removeRows(row: 0, count: removeCount); |
| 466 | QCOMPARE(m_series->count(), m_modelColumnCount); |
| 467 | |
| 468 | // leave only one column |
| 469 | m_model->removeRows(row: 0, count: m_modelRowCount - removeCount - 1); |
| 470 | QCOMPARE(m_series->count(), 0); |
| 471 | } |
| 472 | |
| 473 | void tst_qxymodelmapper::horizontalModelInsertColumns() |
| 474 | { |
| 475 | // setup the mapper |
| 476 | createHorizontalMapper(); |
| 477 | QCOMPARE(m_series->count(), m_modelColumnCount); |
| 478 | QVERIFY(m_hMapper->model() != 0); |
| 479 | |
| 480 | int insertCount = 4; |
| 481 | m_model->insertColumns(column: 3, count: insertCount); |
| 482 | QCOMPARE(m_series->count(), m_modelColumnCount + insertCount); |
| 483 | |
| 484 | int first = 3; |
| 485 | m_hMapper->setFirstColumn(3); |
| 486 | QCOMPARE(m_series->count(), m_modelColumnCount + insertCount - first); |
| 487 | |
| 488 | m_model->insertColumns(column: 3, count: insertCount); |
| 489 | QCOMPARE(m_series->count(), m_modelColumnCount + 2 * insertCount - first); |
| 490 | |
| 491 | int countLimit = 6; |
| 492 | m_hMapper->setColumnCount(countLimit); |
| 493 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount + 2 * insertCount - first)); |
| 494 | |
| 495 | m_model->insertColumns(column: 3, count: insertCount); |
| 496 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount + 3 * insertCount - first)); |
| 497 | |
| 498 | m_hMapper->setFirstColumn(0); |
| 499 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount + 3 * insertCount)); |
| 500 | |
| 501 | m_hMapper->setColumnCount(-1); |
| 502 | QCOMPARE(m_series->count(), m_modelColumnCount + 3 * insertCount); |
| 503 | } |
| 504 | |
| 505 | void tst_qxymodelmapper::horizontalModelRemoveColumns() |
| 506 | { |
| 507 | // setup the mapper |
| 508 | createHorizontalMapper(); |
| 509 | QCOMPARE(m_series->count(), m_modelColumnCount); |
| 510 | QVERIFY(m_hMapper->model() != 0); |
| 511 | |
| 512 | int removeCount = 2; |
| 513 | m_model->removeColumns(column: 1, count: removeCount); |
| 514 | QCOMPARE(m_series->count(), m_modelColumnCount - removeCount); |
| 515 | |
| 516 | int first = 1; |
| 517 | m_hMapper->setFirstColumn(first); |
| 518 | QCOMPARE(m_series->count(), m_modelColumnCount - removeCount - first); |
| 519 | |
| 520 | m_model->removeColumns(column: 1, count: removeCount); |
| 521 | QCOMPARE(m_series->count(), m_modelColumnCount - 2 * removeCount - first); |
| 522 | |
| 523 | int countLimit = 3; |
| 524 | m_hMapper->setColumnCount(countLimit); |
| 525 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount - 2 * removeCount - first)); |
| 526 | |
| 527 | m_model->removeColumns(column: 1, count: removeCount); |
| 528 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount - 3 * removeCount - first)); |
| 529 | |
| 530 | m_hMapper->setFirstColumn(0); |
| 531 | QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount - 3 * removeCount)); |
| 532 | |
| 533 | m_hMapper->setColumnCount(-1); |
| 534 | QCOMPARE(m_series->count(), m_modelColumnCount - 3 * removeCount); |
| 535 | } |
| 536 | |
| 537 | void tst_qxymodelmapper::modelUpdateCell() |
| 538 | { |
| 539 | // setup the mapper |
| 540 | createVerticalMapper(); |
| 541 | |
| 542 | QVERIFY(m_model->setData(m_model->index(1, 0), 44)); |
| 543 | QCOMPARE(m_series->points().at(1).x(), 44.0); |
| 544 | QCOMPARE(m_model->data(m_model->index(1, 0)).toReal(), 44.0); |
| 545 | } |
| 546 | |
| 547 | void tst_qxymodelmapper::verticalMapperSignals() |
| 548 | { |
| 549 | QVXYModelMapper *mapper = new QVXYModelMapper; |
| 550 | |
| 551 | QSignalSpy spy0(mapper, SIGNAL(firstRowChanged())); |
| 552 | QSignalSpy spy1(mapper, SIGNAL(rowCountChanged())); |
| 553 | QSignalSpy spy2(mapper, SIGNAL(xColumnChanged())); |
| 554 | QSignalSpy spy3(mapper, SIGNAL(yColumnChanged())); |
| 555 | QSignalSpy spy4(mapper, SIGNAL(modelReplaced())); |
| 556 | QSignalSpy spy5(mapper, SIGNAL(seriesReplaced())); |
| 557 | |
| 558 | mapper->setXColumn(0); |
| 559 | mapper->setYColumn(1); |
| 560 | mapper->setModel(m_model); |
| 561 | mapper->setSeries(m_series); |
| 562 | mapper->setFirstRow(1); |
| 563 | mapper->setRowCount(5); |
| 564 | |
| 565 | QCOMPARE(spy0.count(), 1); |
| 566 | QCOMPARE(spy1.count(), 1); |
| 567 | QCOMPARE(spy2.count(), 1); |
| 568 | QCOMPARE(spy3.count(), 1); |
| 569 | QCOMPARE(spy4.count(), 1); |
| 570 | QCOMPARE(spy5.count(), 1); |
| 571 | |
| 572 | delete mapper; |
| 573 | } |
| 574 | |
| 575 | void tst_qxymodelmapper::horizontalMapperSignals() |
| 576 | { |
| 577 | QHXYModelMapper *mapper = new QHXYModelMapper; |
| 578 | |
| 579 | QSignalSpy spy0(mapper, SIGNAL(firstColumnChanged())); |
| 580 | QSignalSpy spy1(mapper, SIGNAL(columnCountChanged())); |
| 581 | QSignalSpy spy2(mapper, SIGNAL(xRowChanged())); |
| 582 | QSignalSpy spy3(mapper, SIGNAL(yRowChanged())); |
| 583 | QSignalSpy spy4(mapper, SIGNAL(modelReplaced())); |
| 584 | QSignalSpy spy5(mapper, SIGNAL(seriesReplaced())); |
| 585 | |
| 586 | mapper->setXRow(0); |
| 587 | mapper->setYRow(1); |
| 588 | mapper->setModel(m_model); |
| 589 | mapper->setSeries(m_series); |
| 590 | mapper->setFirstColumn(1); |
| 591 | mapper->setColumnCount(5); |
| 592 | |
| 593 | QCOMPARE(spy0.count(), 1); |
| 594 | QCOMPARE(spy1.count(), 1); |
| 595 | QCOMPARE(spy2.count(), 1); |
| 596 | QCOMPARE(spy3.count(), 1); |
| 597 | QCOMPARE(spy4.count(), 1); |
| 598 | QCOMPARE(spy5.count(), 1); |
| 599 | |
| 600 | delete mapper; |
| 601 | } |
| 602 | |
| 603 | QTEST_MAIN(tst_qxymodelmapper) |
| 604 | |
| 605 | #include "tst_qxymodelmapper.moc" |
| 606 | |