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/QItemModelBarDataProxy> |
33 | #include <QtDataVisualization/Q3DBars> |
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 | QItemModelBarDataProxy *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 QItemModelBarDataProxy(); |
72 | } |
73 | |
74 | void tst_proxy::cleanup() |
75 | { |
76 | delete m_proxy; |
77 | } |
78 | |
79 | void tst_proxy::construct() |
80 | { |
81 | QItemModelBarDataProxy *proxy = new QItemModelBarDataProxy(); |
82 | QVERIFY(proxy); |
83 | delete proxy; |
84 | |
85 | QTableWidget *table = new QTableWidget(); |
86 | |
87 | proxy = new QItemModelBarDataProxy(table->model()); |
88 | QVERIFY(proxy); |
89 | delete proxy; |
90 | |
91 | proxy = new QItemModelBarDataProxy(table->model(), "val" ); |
92 | QVERIFY(proxy); |
93 | QCOMPARE(proxy->rowRole(), QString("" )); |
94 | QCOMPARE(proxy->columnRole(), QString("" )); |
95 | QCOMPARE(proxy->valueRole(), QString("val" )); |
96 | QCOMPARE(proxy->rotationRole(), QString("" )); |
97 | QCOMPARE(proxy->rowCategories().length(), 0); |
98 | QCOMPARE(proxy->columnCategories().length(), 0); |
99 | delete proxy; |
100 | |
101 | proxy = new QItemModelBarDataProxy(table->model(), "row" , "col" , "val" ); |
102 | QVERIFY(proxy); |
103 | QCOMPARE(proxy->rowRole(), QString("row" )); |
104 | QCOMPARE(proxy->columnRole(), QString("col" )); |
105 | QCOMPARE(proxy->valueRole(), QString("val" )); |
106 | QCOMPARE(proxy->rotationRole(), QString("" )); |
107 | QCOMPARE(proxy->rowCategories().length(), 0); |
108 | QCOMPARE(proxy->columnCategories().length(), 0); |
109 | delete proxy; |
110 | |
111 | proxy = new QItemModelBarDataProxy(table->model(), "row" , "col" , "val" , "rot" ); |
112 | QVERIFY(proxy); |
113 | QCOMPARE(proxy->rowRole(), QString("row" )); |
114 | QCOMPARE(proxy->columnRole(), QString("col" )); |
115 | QCOMPARE(proxy->valueRole(), QString("val" )); |
116 | QCOMPARE(proxy->rotationRole(), QString("rot" )); |
117 | QCOMPARE(proxy->rowCategories().length(), 0); |
118 | QCOMPARE(proxy->columnCategories().length(), 0); |
119 | delete proxy; |
120 | |
121 | proxy = new QItemModelBarDataProxy(table->model(), "row" , "col" , "val" , |
122 | QStringList() << "rowCat" , QStringList() << "colCat" ); |
123 | QVERIFY(proxy); |
124 | QCOMPARE(proxy->rowRole(), QString("row" )); |
125 | QCOMPARE(proxy->columnRole(), QString("col" )); |
126 | QCOMPARE(proxy->valueRole(), QString("val" )); |
127 | QCOMPARE(proxy->rotationRole(), QString("" )); |
128 | QCOMPARE(proxy->rowCategories().length(), 1); |
129 | QCOMPARE(proxy->columnCategories().length(), 1); |
130 | delete proxy; |
131 | |
132 | proxy = new QItemModelBarDataProxy(table->model(), "row" , "col" , "val" , "rot" , |
133 | QStringList() << "rowCat" , QStringList() << "colCat" ); |
134 | QVERIFY(proxy); |
135 | QCOMPARE(proxy->rowRole(), QString("row" )); |
136 | QCOMPARE(proxy->columnRole(), QString("col" )); |
137 | QCOMPARE(proxy->valueRole(), QString("val" )); |
138 | QCOMPARE(proxy->rotationRole(), QString("rot" )); |
139 | QCOMPARE(proxy->rowCategories().length(), 1); |
140 | QCOMPARE(proxy->columnCategories().length(), 1); |
141 | delete proxy; |
142 | } |
143 | |
144 | void tst_proxy::initialProperties() |
145 | { |
146 | QVERIFY(m_proxy); |
147 | |
148 | QCOMPARE(m_proxy->autoColumnCategories(), true); |
149 | QCOMPARE(m_proxy->autoRowCategories(), true); |
150 | QCOMPARE(m_proxy->columnCategories(), QStringList()); |
151 | QCOMPARE(m_proxy->columnRole(), QString()); |
152 | QCOMPARE(m_proxy->columnRolePattern(), QRegExp()); |
153 | QCOMPARE(m_proxy->columnRoleReplace(), QString()); |
154 | QVERIFY(!m_proxy->itemModel()); |
155 | QCOMPARE(m_proxy->multiMatchBehavior(), QItemModelBarDataProxy::MMBLast); |
156 | QCOMPARE(m_proxy->rotationRole(), QString()); |
157 | QCOMPARE(m_proxy->rotationRolePattern(), QRegExp()); |
158 | QCOMPARE(m_proxy->rotationRoleReplace(), QString()); |
159 | QCOMPARE(m_proxy->rowCategories(), QStringList()); |
160 | QCOMPARE(m_proxy->rowRole(), QString()); |
161 | QCOMPARE(m_proxy->rowRolePattern(), QRegExp()); |
162 | QCOMPARE(m_proxy->rowRoleReplace(), QString()); |
163 | QCOMPARE(m_proxy->useModelCategories(), false); |
164 | QCOMPARE(m_proxy->valueRole(), QString()); |
165 | QCOMPARE(m_proxy->valueRolePattern(), QRegExp()); |
166 | QCOMPARE(m_proxy->valueRoleReplace(), QString()); |
167 | |
168 | QCOMPARE(m_proxy->columnLabels().count(), 0); |
169 | QCOMPARE(m_proxy->rowCount(), 0); |
170 | QCOMPARE(m_proxy->rowLabels().count(), 0); |
171 | QVERIFY(!m_proxy->series()); |
172 | |
173 | QCOMPARE(m_proxy->type(), QAbstractDataProxy::DataTypeBar); |
174 | } |
175 | |
176 | void tst_proxy::initializeProperties() |
177 | { |
178 | QVERIFY(m_proxy); |
179 | |
180 | QTableWidget table; |
181 | |
182 | m_proxy->setAutoColumnCategories(false); |
183 | m_proxy->setAutoRowCategories(false); |
184 | m_proxy->setColumnCategories(QStringList() << "col1" << "col2" ); |
185 | m_proxy->setColumnRole("column" ); |
186 | m_proxy->setColumnRolePattern(QRegExp("/^.*-(\\d\\d)$/" )); |
187 | m_proxy->setColumnRoleReplace("\\\\1" ); |
188 | m_proxy->setItemModel(table.model()); |
189 | m_proxy->setMultiMatchBehavior(QItemModelBarDataProxy::MMBAverage); |
190 | m_proxy->setRotationRole("rotation" ); |
191 | m_proxy->setRotationRolePattern(QRegExp("/-/" )); |
192 | m_proxy->setRotationRoleReplace("\\\\1" ); |
193 | m_proxy->setRowCategories(QStringList() << "row1" << "row2" ); |
194 | m_proxy->setRowRole("row" ); |
195 | m_proxy->setRowRolePattern(QRegExp("/^(\\d\\d\\d\\d).*$/" )); |
196 | m_proxy->setRowRoleReplace("\\\\1" ); |
197 | m_proxy->setUseModelCategories(true); |
198 | m_proxy->setValueRole("value" ); |
199 | m_proxy->setValueRolePattern(QRegExp("/-/" )); |
200 | m_proxy->setValueRoleReplace("\\\\1" ); |
201 | |
202 | QCOMPARE(m_proxy->autoColumnCategories(), false); |
203 | QCOMPARE(m_proxy->autoRowCategories(), false); |
204 | QCOMPARE(m_proxy->columnCategories().count(), 2); |
205 | QCOMPARE(m_proxy->columnRole(), QString("column" )); |
206 | QCOMPARE(m_proxy->columnRolePattern(), QRegExp("/^.*-(\\d\\d)$/" )); |
207 | QCOMPARE(m_proxy->columnRoleReplace(), QString("\\\\1" )); |
208 | QVERIFY(m_proxy->itemModel()); |
209 | QCOMPARE(m_proxy->multiMatchBehavior(), QItemModelBarDataProxy::MMBAverage); |
210 | QCOMPARE(m_proxy->rotationRole(), QString("rotation" )); |
211 | QCOMPARE(m_proxy->rotationRolePattern(), QRegExp("/-/" )); |
212 | QCOMPARE(m_proxy->rotationRoleReplace(), QString("\\\\1" )); |
213 | QCOMPARE(m_proxy->rowCategories().count(), 2); |
214 | QCOMPARE(m_proxy->rowRole(), QString("row" )); |
215 | QCOMPARE(m_proxy->rowRolePattern(), QRegExp("/^(\\d\\d\\d\\d).*$/" )); |
216 | QCOMPARE(m_proxy->rowRoleReplace(), QString("\\\\1" )); |
217 | QCOMPARE(m_proxy->useModelCategories(), true); |
218 | QCOMPARE(m_proxy->valueRole(), QString("value" )); |
219 | QCOMPARE(m_proxy->valueRolePattern(), QRegExp("/-/" )); |
220 | QCOMPARE(m_proxy->valueRoleReplace(), QString("\\\\1" )); |
221 | } |
222 | |
223 | void tst_proxy::multiMatch() |
224 | { |
225 | if (!CpptestUtil::isOpenGLSupported()) |
226 | QSKIP("OpenGL not supported on this platform" ); |
227 | |
228 | Q3DBars graph; |
229 | |
230 | QTableWidget table; |
231 | QStringList rows; |
232 | rows << "row 1" << "row 2" << "row 3" ; |
233 | QStringList columns; |
234 | columns << "col 1" ; |
235 | const char *values[1][3] = {{"0/0/3.5/30" , "0/0/5.0/30" , "0/0/6.5/30" }}; |
236 | |
237 | table.setRowCount(1); |
238 | table.setColumnCount(3); |
239 | |
240 | for (int col = 0; col < columns.size(); col++) { |
241 | for (int row = 0; row < rows.size(); row++) { |
242 | QModelIndex index = table.model()->index(row: col, column: row); |
243 | table.model()->setData(index, value: values[col][row]); |
244 | } |
245 | } |
246 | |
247 | m_proxy->setItemModel(table.model()); |
248 | m_proxy->setRowRole(table.model()->roleNames().value(akey: Qt::DisplayRole)); |
249 | m_proxy->setColumnRole(table.model()->roleNames().value(akey: Qt::DisplayRole)); |
250 | m_proxy->setRowRolePattern(QRegExp(QStringLiteral("^(\\d*)\\/(\\d*)\\/\\d*[\\.\\,]?\\d*\\/\\d*[\\.\\,]?\\d*$" ))); |
251 | m_proxy->setRowRoleReplace(QStringLiteral("\\2" )); |
252 | m_proxy->setValueRolePattern(QRegExp(QStringLiteral("^\\d*(\\/)(\\d*)\\/(\\d*[\\.\\,]?\\d*)\\/\\d*[\\.\\,]?\\d*$" ))); |
253 | m_proxy->setValueRoleReplace(QStringLiteral("\\3" )); |
254 | m_proxy->setColumnRolePattern(QRegExp(QStringLiteral("^(\\d*)(\\/)(\\d*)\\/\\d*[\\.\\,]?\\d*\\/\\d*[\\.\\,]?\\d*$" ))); |
255 | m_proxy->setColumnRoleReplace(QStringLiteral("\\1" )); |
256 | |
257 | QBar3DSeries *series = new QBar3DSeries(m_proxy); |
258 | |
259 | graph.addSeries(series); |
260 | |
261 | QSignalSpy spy(graph.valueAxis(), SIGNAL(maxChanged(float))); |
262 | spy.wait(timeout: 1000); |
263 | |
264 | QCOMPARE(graph.valueAxis()->max(), 6.5f); |
265 | m_proxy->setMultiMatchBehavior(QItemModelBarDataProxy::MMBFirst); |
266 | QCoreApplication::processEvents(); |
267 | QCOMPARE(graph.valueAxis()->max(), 3.5f); |
268 | m_proxy->setMultiMatchBehavior(QItemModelBarDataProxy::MMBLast); |
269 | QCoreApplication::processEvents(); |
270 | QCOMPARE(graph.valueAxis()->max(), 6.5f); |
271 | m_proxy->setMultiMatchBehavior(QItemModelBarDataProxy::MMBAverage); |
272 | QCoreApplication::processEvents(); |
273 | QCOMPARE(graph.valueAxis()->max(), 5.0f); |
274 | m_proxy->setMultiMatchBehavior(QItemModelBarDataProxy::MMBCumulative); |
275 | QCoreApplication::processEvents(); |
276 | QCOMPARE(graph.valueAxis()->max(), 15.0f); |
277 | |
278 | QCOMPARE(m_proxy->columnLabels().count(), 1); |
279 | QCOMPARE(m_proxy->rowCount(), 1); |
280 | QCOMPARE(m_proxy->rowLabels().count(), 1); |
281 | QVERIFY(m_proxy->series()); |
282 | |
283 | m_proxy = 0; // Proxy gets deleted as graph gets deleted |
284 | } |
285 | |
286 | QTEST_MAIN(tst_proxy) |
287 | #include "tst_proxy.moc" |
288 | |