| 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 Data Visualization 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 <QtTest/QtTest> |
| 31 | |
| 32 | #include <QtDataVisualization/QItemModelScatterDataProxy> |
| 33 | #include <QtDataVisualization/Q3DScatter> |
| 34 | #include <QtWidgets/QTableWidget> |
| 35 | |
| 36 | using namespace QtDataVisualization; |
| 37 | |
| 38 | class tst_proxy: public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | private slots: |
| 43 | void initTestCase(); |
| 44 | void cleanupTestCase(); |
| 45 | void init(); |
| 46 | void cleanup(); |
| 47 | |
| 48 | void construct(); |
| 49 | |
| 50 | void initialProperties(); |
| 51 | void initializeProperties(); |
| 52 | |
| 53 | void addModel(); |
| 54 | |
| 55 | private: |
| 56 | QItemModelScatterDataProxy *m_proxy; |
| 57 | }; |
| 58 | |
| 59 | void tst_proxy::initTestCase() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void tst_proxy::cleanupTestCase() |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | void tst_proxy::init() |
| 68 | { |
| 69 | m_proxy = new QItemModelScatterDataProxy(); |
| 70 | } |
| 71 | |
| 72 | void tst_proxy::cleanup() |
| 73 | { |
| 74 | delete m_proxy; |
| 75 | } |
| 76 | |
| 77 | void tst_proxy::construct() |
| 78 | { |
| 79 | QItemModelScatterDataProxy *proxy = new QItemModelScatterDataProxy(); |
| 80 | QVERIFY(proxy); |
| 81 | delete proxy; |
| 82 | |
| 83 | QTableWidget *table = new QTableWidget(); |
| 84 | |
| 85 | proxy = new QItemModelScatterDataProxy(table->model()); |
| 86 | QVERIFY(proxy); |
| 87 | delete proxy; |
| 88 | |
| 89 | proxy = new QItemModelScatterDataProxy(table->model(), "x" , "y" , "z" ); |
| 90 | QVERIFY(proxy); |
| 91 | QCOMPARE(proxy->xPosRole(), QString("x" )); |
| 92 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
| 93 | QCOMPARE(proxy->zPosRole(), QString("z" )); |
| 94 | QCOMPARE(proxy->rotationRole(), QString("" )); |
| 95 | delete proxy; |
| 96 | |
| 97 | proxy = new QItemModelScatterDataProxy(table->model(), "x" , "y" , "z" , "rot" ); |
| 98 | QVERIFY(proxy); |
| 99 | QCOMPARE(proxy->xPosRole(), QString("x" )); |
| 100 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
| 101 | QCOMPARE(proxy->zPosRole(), QString("z" )); |
| 102 | QCOMPARE(proxy->rotationRole(), QString("rot" )); |
| 103 | delete proxy; |
| 104 | } |
| 105 | |
| 106 | void tst_proxy::initialProperties() |
| 107 | { |
| 108 | QVERIFY(m_proxy); |
| 109 | |
| 110 | QVERIFY(!m_proxy->itemModel()); |
| 111 | QCOMPARE(m_proxy->rotationRole(), QString()); |
| 112 | QCOMPARE(m_proxy->rotationRolePattern(), QRegExp()); |
| 113 | QCOMPARE(m_proxy->rotationRoleReplace(), QString()); |
| 114 | QCOMPARE(m_proxy->xPosRole(), QString()); |
| 115 | QCOMPARE(m_proxy->xPosRolePattern(), QRegExp()); |
| 116 | QCOMPARE(m_proxy->xPosRoleReplace(), QString()); |
| 117 | QCOMPARE(m_proxy->yPosRole(), QString()); |
| 118 | QCOMPARE(m_proxy->yPosRolePattern(), QRegExp()); |
| 119 | QCOMPARE(m_proxy->yPosRoleReplace(), QString()); |
| 120 | QCOMPARE(m_proxy->zPosRole(), QString()); |
| 121 | QCOMPARE(m_proxy->zPosRolePattern(), QRegExp()); |
| 122 | QCOMPARE(m_proxy->zPosRoleReplace(), QString()); |
| 123 | |
| 124 | QCOMPARE(m_proxy->itemCount(), 0); |
| 125 | QVERIFY(!m_proxy->series()); |
| 126 | |
| 127 | QCOMPARE(m_proxy->type(), QAbstractDataProxy::DataTypeScatter); |
| 128 | } |
| 129 | |
| 130 | void tst_proxy::initializeProperties() |
| 131 | { |
| 132 | QVERIFY(m_proxy); |
| 133 | |
| 134 | QTableWidget table; |
| 135 | |
| 136 | m_proxy->setItemModel(table.model()); |
| 137 | m_proxy->setRotationRole("rotation" ); |
| 138 | m_proxy->setRotationRolePattern(QRegExp("/-/" )); |
| 139 | m_proxy->setRotationRoleReplace("\\\\1" ); |
| 140 | m_proxy->setXPosRole("X" ); |
| 141 | m_proxy->setXPosRolePattern(QRegExp("/-/" )); |
| 142 | m_proxy->setXPosRoleReplace("\\\\1" ); |
| 143 | m_proxy->setYPosRole("Y" ); |
| 144 | m_proxy->setYPosRolePattern(QRegExp("/-/" )); |
| 145 | m_proxy->setYPosRoleReplace("\\\\1" ); |
| 146 | m_proxy->setZPosRole("Z" ); |
| 147 | m_proxy->setZPosRolePattern(QRegExp("/-/" )); |
| 148 | m_proxy->setZPosRoleReplace("\\\\1" ); |
| 149 | |
| 150 | QVERIFY(m_proxy->itemModel()); |
| 151 | QCOMPARE(m_proxy->rotationRole(), QString("rotation" )); |
| 152 | QCOMPARE(m_proxy->rotationRolePattern(), QRegExp("/-/" )); |
| 153 | QCOMPARE(m_proxy->rotationRoleReplace(), QString("\\\\1" )); |
| 154 | QCOMPARE(m_proxy->xPosRole(), QString("X" )); |
| 155 | QCOMPARE(m_proxy->xPosRolePattern(), QRegExp("/-/" )); |
| 156 | QCOMPARE(m_proxy->xPosRoleReplace(), QString("\\\\1" )); |
| 157 | QCOMPARE(m_proxy->yPosRole(), QString("Y" )); |
| 158 | QCOMPARE(m_proxy->yPosRolePattern(), QRegExp("/-/" )); |
| 159 | QCOMPARE(m_proxy->yPosRoleReplace(), QString("\\\\1" )); |
| 160 | QCOMPARE(m_proxy->zPosRole(), QString("Z" )); |
| 161 | QCOMPARE(m_proxy->zPosRolePattern(), QRegExp("/-/" )); |
| 162 | QCOMPARE(m_proxy->zPosRoleReplace(), QString("\\\\1" )); |
| 163 | } |
| 164 | |
| 165 | void tst_proxy::addModel() |
| 166 | { |
| 167 | QTableWidget table; |
| 168 | QStringList rows; |
| 169 | rows << "row 1" ; |
| 170 | QStringList columns; |
| 171 | columns << "col 1" ; |
| 172 | const char *values[1][2] = {{"0/0/5.5/30" , "0/0/10.5/30" }}; |
| 173 | |
| 174 | table.setRowCount(2); |
| 175 | table.setColumnCount(1); |
| 176 | |
| 177 | for (int col = 0; col < columns.size(); col++) { |
| 178 | for (int row = 0; row < rows.size(); row++) { |
| 179 | QModelIndex index = table.model()->index(row: col, column: row); |
| 180 | table.model()->setData(index, value: values[col][row]); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | m_proxy->setItemModel(table.model()); |
| 185 | m_proxy->setXPosRole(table.model()->roleNames().value(akey: Qt::DisplayRole)); |
| 186 | m_proxy->setZPosRole(table.model()->roleNames().value(akey: Qt::DisplayRole)); |
| 187 | m_proxy->setXPosRolePattern(QRegExp(QStringLiteral("^(\\d*)\\/(\\d*)\\/\\d*[\\.\\,]?\\d*\\/\\d*[\\.\\,]?\\d*$" ))); |
| 188 | m_proxy->setXPosRoleReplace(QStringLiteral("\\2" )); |
| 189 | m_proxy->setYPosRolePattern(QRegExp(QStringLiteral("^\\d*(\\/)(\\d*)\\/(\\d*[\\.\\,]?\\d*)\\/\\d*[\\.\\,]?\\d*$" ))); |
| 190 | m_proxy->setYPosRoleReplace(QStringLiteral("\\3" )); |
| 191 | m_proxy->setZPosRolePattern(QRegExp(QStringLiteral("^(\\d*)(\\/)(\\d*)\\/\\d*[\\.\\,]?\\d*\\/\\d*[\\.\\,]?\\d*$" ))); |
| 192 | m_proxy->setZPosRoleReplace(QStringLiteral("\\1" )); |
| 193 | |
| 194 | QScatter3DSeries *series = new QScatter3DSeries(m_proxy); |
| 195 | |
| 196 | QSignalSpy spy(series, SIGNAL(dataProxyChanged(QScatterDataProxy *))); |
| 197 | spy.wait(timeout: 1000); |
| 198 | |
| 199 | QCOMPARE(m_proxy->itemCount(), 2); |
| 200 | QVERIFY(m_proxy->series()); |
| 201 | |
| 202 | delete series; |
| 203 | m_proxy = 0; // proxy gets deleted with series |
| 204 | } |
| 205 | |
| 206 | QTEST_MAIN(tst_proxy) |
| 207 | #include "tst_proxy.moc" |
| 208 | |