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/QItemModelSurfaceDataProxy> |
33 | #include <QtDataVisualization/Q3DSurface> |
34 | #include <QtWidgets/QTableWidget> |
35 | |
36 | #include "cpptestutil.h" |
37 | |
38 | using namespace QtDataVisualization; |
39 | |
40 | class tst_proxy: public QObject |
41 | { |
42 | Q_OBJECT |
43 | |
44 | private slots: |
45 | void initTestCase(); |
46 | void cleanupTestCase(); |
47 | void init(); |
48 | void cleanup(); |
49 | |
50 | void construct(); |
51 | |
52 | void initialProperties(); |
53 | void initializeProperties(); |
54 | |
55 | void multiMatch(); |
56 | |
57 | private: |
58 | QItemModelSurfaceDataProxy *m_proxy; |
59 | }; |
60 | |
61 | void tst_proxy::initTestCase() |
62 | { |
63 | } |
64 | |
65 | void tst_proxy::cleanupTestCase() |
66 | { |
67 | } |
68 | |
69 | void tst_proxy::init() |
70 | { |
71 | m_proxy = new QItemModelSurfaceDataProxy(); |
72 | } |
73 | |
74 | void tst_proxy::cleanup() |
75 | { |
76 | delete m_proxy; |
77 | } |
78 | |
79 | |
80 | void tst_proxy::construct() |
81 | { |
82 | QItemModelSurfaceDataProxy *proxy = new QItemModelSurfaceDataProxy(); |
83 | QVERIFY(proxy); |
84 | delete proxy; |
85 | |
86 | QTableWidget table; |
87 | |
88 | proxy = new QItemModelSurfaceDataProxy(table.model()); |
89 | QVERIFY(proxy); |
90 | delete proxy; |
91 | |
92 | proxy = new QItemModelSurfaceDataProxy(table.model(), "y" ); |
93 | QVERIFY(proxy); |
94 | QCOMPARE(proxy->rowRole(), QString("" )); |
95 | QCOMPARE(proxy->columnRole(), QString("" )); |
96 | QCOMPARE(proxy->xPosRole(), QString("" )); |
97 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
98 | QCOMPARE(proxy->zPosRole(), QString("" )); |
99 | QCOMPARE(proxy->rowCategories().length(), 0); |
100 | QCOMPARE(proxy->columnCategories().length(), 0); |
101 | delete proxy; |
102 | |
103 | proxy = new QItemModelSurfaceDataProxy(table.model(), "row" , "column" , "y" ); |
104 | QVERIFY(proxy); |
105 | QCOMPARE(proxy->rowRole(), QString("row" )); |
106 | QCOMPARE(proxy->columnRole(), QString("column" )); |
107 | QCOMPARE(proxy->xPosRole(), QString("column" )); |
108 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
109 | QCOMPARE(proxy->zPosRole(), QString("row" )); |
110 | QCOMPARE(proxy->rowCategories().length(), 0); |
111 | QCOMPARE(proxy->columnCategories().length(), 0); |
112 | delete proxy; |
113 | |
114 | proxy = new QItemModelSurfaceDataProxy(table.model(), "row" , "column" , "x" , "y" , "z" ); |
115 | QVERIFY(proxy); |
116 | QCOMPARE(proxy->rowRole(), QString("row" )); |
117 | QCOMPARE(proxy->columnRole(), QString("column" )); |
118 | QCOMPARE(proxy->xPosRole(), QString("x" )); |
119 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
120 | QCOMPARE(proxy->zPosRole(), QString("z" )); |
121 | QCOMPARE(proxy->rowCategories().length(), 0); |
122 | QCOMPARE(proxy->columnCategories().length(), 0); |
123 | delete proxy; |
124 | |
125 | proxy = new QItemModelSurfaceDataProxy(table.model(), "row" , "column" , "y" , |
126 | QStringList() << "rowCat" , QStringList() << "colCat" ); |
127 | QVERIFY(proxy); |
128 | QCOMPARE(proxy->rowRole(), QString("row" )); |
129 | QCOMPARE(proxy->columnRole(), QString("column" )); |
130 | QCOMPARE(proxy->xPosRole(), QString("column" )); |
131 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
132 | QCOMPARE(proxy->zPosRole(), QString("row" )); |
133 | QCOMPARE(proxy->rowCategories().length(), 1); |
134 | QCOMPARE(proxy->columnCategories().length(), 1); |
135 | delete proxy; |
136 | |
137 | proxy = new QItemModelSurfaceDataProxy(table.model(), "row" , "column" , "x" , "y" , "z" , |
138 | QStringList() << "rowCat" , QStringList() << "colCat" ); |
139 | QVERIFY(proxy); |
140 | QCOMPARE(proxy->rowRole(), QString("row" )); |
141 | QCOMPARE(proxy->columnRole(), QString("column" )); |
142 | QCOMPARE(proxy->xPosRole(), QString("x" )); |
143 | QCOMPARE(proxy->yPosRole(), QString("y" )); |
144 | QCOMPARE(proxy->zPosRole(), QString("z" )); |
145 | QCOMPARE(proxy->rowCategories().length(), 1); |
146 | QCOMPARE(proxy->columnCategories().length(), 1); |
147 | delete proxy; |
148 | } |
149 | |
150 | void tst_proxy::initialProperties() |
151 | { |
152 | QVERIFY(m_proxy); |
153 | |
154 | QCOMPARE(m_proxy->autoColumnCategories(), true); |
155 | QCOMPARE(m_proxy->autoRowCategories(), true); |
156 | QCOMPARE(m_proxy->columnCategories(), QStringList()); |
157 | QCOMPARE(m_proxy->columnRole(), QString()); |
158 | QCOMPARE(m_proxy->columnRolePattern(), QRegExp()); |
159 | QCOMPARE(m_proxy->columnRoleReplace(), QString()); |
160 | QVERIFY(!m_proxy->itemModel()); |
161 | QCOMPARE(m_proxy->multiMatchBehavior(), QItemModelSurfaceDataProxy::MMBLast); |
162 | QCOMPARE(m_proxy->rowCategories(), QStringList()); |
163 | QCOMPARE(m_proxy->rowRole(), QString()); |
164 | QCOMPARE(m_proxy->rowRolePattern(), QRegExp()); |
165 | QCOMPARE(m_proxy->rowRoleReplace(), QString()); |
166 | QCOMPARE(m_proxy->useModelCategories(), false); |
167 | QCOMPARE(m_proxy->xPosRole(), QString()); |
168 | QCOMPARE(m_proxy->xPosRolePattern(), QRegExp()); |
169 | QCOMPARE(m_proxy->xPosRoleReplace(), QString()); |
170 | QCOMPARE(m_proxy->yPosRole(), QString()); |
171 | QCOMPARE(m_proxy->yPosRolePattern(), QRegExp()); |
172 | QCOMPARE(m_proxy->yPosRoleReplace(), QString()); |
173 | QCOMPARE(m_proxy->zPosRole(), QString()); |
174 | QCOMPARE(m_proxy->zPosRolePattern(), QRegExp()); |
175 | QCOMPARE(m_proxy->zPosRoleReplace(), QString()); |
176 | |
177 | QCOMPARE(m_proxy->columnCount(), 0); |
178 | QCOMPARE(m_proxy->rowCount(), 0); |
179 | QVERIFY(!m_proxy->series()); |
180 | |
181 | QCOMPARE(m_proxy->type(), QAbstractDataProxy::DataTypeSurface); |
182 | } |
183 | |
184 | void tst_proxy::initializeProperties() |
185 | { |
186 | QVERIFY(m_proxy); |
187 | |
188 | QTableWidget table; |
189 | |
190 | m_proxy->setAutoColumnCategories(false); |
191 | m_proxy->setAutoRowCategories(false); |
192 | m_proxy->setColumnCategories(QStringList() << "col1" << "col2" ); |
193 | m_proxy->setColumnRole("column" ); |
194 | m_proxy->setColumnRolePattern(QRegExp("/^.*-(\\d\\d)$/" )); |
195 | m_proxy->setColumnRoleReplace("\\\\1" ); |
196 | m_proxy->setItemModel(table.model()); |
197 | m_proxy->setMultiMatchBehavior(QItemModelSurfaceDataProxy::MMBAverage); |
198 | m_proxy->setRowCategories(QStringList() << "row1" << "row2" ); |
199 | m_proxy->setRowRole("row" ); |
200 | m_proxy->setRowRolePattern(QRegExp("/^(\\d\\d\\d\\d).*$/" )); |
201 | m_proxy->setRowRoleReplace("\\\\1" ); |
202 | m_proxy->setUseModelCategories(true); |
203 | m_proxy->setXPosRole("X" ); |
204 | m_proxy->setXPosRolePattern(QRegExp("/-/" )); |
205 | m_proxy->setXPosRoleReplace("\\\\1" ); |
206 | m_proxy->setYPosRole("Y" ); |
207 | m_proxy->setYPosRolePattern(QRegExp("/-/" )); |
208 | m_proxy->setYPosRoleReplace("\\\\1" ); |
209 | m_proxy->setZPosRole("Z" ); |
210 | m_proxy->setZPosRolePattern(QRegExp("/-/" )); |
211 | m_proxy->setZPosRoleReplace("\\\\1" ); |
212 | |
213 | QCOMPARE(m_proxy->autoColumnCategories(), false); |
214 | QCOMPARE(m_proxy->autoRowCategories(), false); |
215 | QCOMPARE(m_proxy->columnCategories().count(), 2); |
216 | QCOMPARE(m_proxy->columnRole(), QString("column" )); |
217 | QCOMPARE(m_proxy->columnRolePattern(), QRegExp("/^.*-(\\d\\d)$/" )); |
218 | QCOMPARE(m_proxy->columnRoleReplace(), QString("\\\\1" )); |
219 | QVERIFY(m_proxy->itemModel()); |
220 | QCOMPARE(m_proxy->multiMatchBehavior(), QItemModelSurfaceDataProxy::MMBAverage); |
221 | QCOMPARE(m_proxy->rowCategories().count(), 2); |
222 | QCOMPARE(m_proxy->rowRole(), QString("row" )); |
223 | QCOMPARE(m_proxy->rowRolePattern(), QRegExp("/^(\\d\\d\\d\\d).*$/" )); |
224 | QCOMPARE(m_proxy->rowRoleReplace(), QString("\\\\1" )); |
225 | QCOMPARE(m_proxy->useModelCategories(), true); |
226 | QCOMPARE(m_proxy->xPosRole(), QString("X" )); |
227 | QCOMPARE(m_proxy->xPosRolePattern(), QRegExp("/-/" )); |
228 | QCOMPARE(m_proxy->xPosRoleReplace(), QString("\\\\1" )); |
229 | QCOMPARE(m_proxy->yPosRole(), QString("Y" )); |
230 | QCOMPARE(m_proxy->yPosRolePattern(), QRegExp("/-/" )); |
231 | QCOMPARE(m_proxy->yPosRoleReplace(), QString("\\\\1" )); |
232 | QCOMPARE(m_proxy->zPosRole(), QString("Z" )); |
233 | QCOMPARE(m_proxy->zPosRolePattern(), QRegExp("/-/" )); |
234 | QCOMPARE(m_proxy->zPosRoleReplace(), QString("\\\\1" )); |
235 | } |
236 | |
237 | void tst_proxy::multiMatch() |
238 | { |
239 | if (!CpptestUtil::isOpenGLSupported()) |
240 | QSKIP("OpenGL not supported on this platform" ); |
241 | |
242 | Q3DSurface graph; |
243 | |
244 | QTableWidget table; |
245 | QStringList rows; |
246 | rows << "row 1" << "row 2" ; |
247 | QStringList columns; |
248 | columns << "col 1" << "col 2" << "col 3" << "col 4" ; |
249 | const char *values[4][2] = {{"0/0/5.5/30" , "0/0/10.5/30" }, |
250 | {"0/1/5.5/30" , "0/1/0.5/30" }, |
251 | {"1/0/5.5/30" , "1/0/0.5/30" }, |
252 | {"1/1/0.0/30" , "1/1/0.0/30" }}; |
253 | |
254 | table.setRowCount(2); |
255 | table.setColumnCount(4); |
256 | |
257 | for (int col = 0; col < columns.size(); col++) { |
258 | for (int row = 0; row < rows.size(); row++) { |
259 | QModelIndex index = table.model()->index(row: col, column: row); |
260 | table.model()->setData(index, value: values[col][row]); |
261 | } |
262 | } |
263 | |
264 | m_proxy->setItemModel(table.model()); |
265 | m_proxy->setRowRole(table.model()->roleNames().value(akey: Qt::DisplayRole)); |
266 | m_proxy->setColumnRole(table.model()->roleNames().value(akey: Qt::DisplayRole)); |
267 | m_proxy->setRowRolePattern(QRegExp(QStringLiteral("^(\\d*)\\/(\\d*)\\/\\d*[\\.\\,]?\\d*\\/\\d*[\\.\\,]?\\d*$" ))); |
268 | m_proxy->setRowRoleReplace(QStringLiteral("\\2" )); |
269 | m_proxy->setYPosRolePattern(QRegExp(QStringLiteral("^\\d*(\\/)(\\d*)\\/(\\d*[\\.\\,]?\\d*)\\/\\d*[\\.\\,]?\\d*$" ))); |
270 | m_proxy->setYPosRoleReplace(QStringLiteral("\\3" )); |
271 | m_proxy->setColumnRolePattern(QRegExp(QStringLiteral("^(\\d*)(\\/)(\\d*)\\/\\d*[\\.\\,]?\\d*\\/\\d*[\\.\\,]?\\d*$" ))); |
272 | m_proxy->setColumnRoleReplace(QStringLiteral("\\1" )); |
273 | |
274 | QSurface3DSeries *series = new QSurface3DSeries(m_proxy); |
275 | |
276 | graph.addSeries(series); |
277 | QSignalSpy spy(graph.axisY(), SIGNAL(maxChanged(float))); |
278 | spy.wait(timeout: 1000); |
279 | |
280 | QCOMPARE(graph.axisY()->max(), 10.5f); |
281 | m_proxy->setMultiMatchBehavior(QItemModelSurfaceDataProxy::MMBFirst); |
282 | QCoreApplication::processEvents(); |
283 | QCOMPARE(graph.axisY()->max(), 5.5f); |
284 | m_proxy->setMultiMatchBehavior(QItemModelSurfaceDataProxy::MMBLast); |
285 | QCoreApplication::processEvents(); |
286 | QCOMPARE(graph.axisY()->max(), 10.5f); |
287 | m_proxy->setMultiMatchBehavior(QItemModelSurfaceDataProxy::MMBAverage); |
288 | QCoreApplication::processEvents(); |
289 | QCOMPARE(graph.axisY()->max(), 8.0f); |
290 | m_proxy->setMultiMatchBehavior(QItemModelSurfaceDataProxy::MMBCumulativeY); |
291 | QCoreApplication::processEvents(); |
292 | QCOMPARE(graph.axisY()->max(), 16.0f); |
293 | |
294 | QCOMPARE(m_proxy->columnCount(), 2); |
295 | QCOMPARE(m_proxy->rowCount(), 3); |
296 | QVERIFY(m_proxy->series()); |
297 | |
298 | m_proxy = 0; // Graph deletes proxy |
299 | } |
300 | |
301 | QTEST_MAIN(tst_proxy) |
302 | #include "tst_proxy.moc" |
303 | |