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 "scatteritemmodelhandler_p.h"
31
32QT_BEGIN_NAMESPACE_DATAVISUALIZATION
33
34static const int noRoleIndex = -1;
35
36ScatterItemModelHandler::ScatterItemModelHandler(QItemModelScatterDataProxy *proxy, QObject *parent)
37 : AbstractItemModelHandler(parent),
38 m_proxy(proxy),
39 m_proxyArray(0),
40 m_xPosRole(noRoleIndex),
41 m_yPosRole(noRoleIndex),
42 m_zPosRole(noRoleIndex),
43 m_rotationRole(noRoleIndex),
44 m_haveXPosPattern(false),
45 m_haveYPosPattern(false),
46 m_haveZPosPattern(false),
47 m_haveRotationPattern(false)
48{
49}
50
51ScatterItemModelHandler::~ScatterItemModelHandler()
52{
53}
54
55void ScatterItemModelHandler::handleDataChanged(const QModelIndex &topLeft,
56 const QModelIndex &bottomRight,
57 const QVector<int> &roles)
58{
59 // Do nothing if full reset already pending
60 if (!m_fullReset) {
61 if (m_itemModel->columnCount() > 1) {
62 // If the data model is multi-column, do full asynchronous reset to simplify things
63 AbstractItemModelHandler::handleDataChanged(topLeft, bottomRight, roles);
64 } else {
65 int start = qMin(a: topLeft.row(), b: bottomRight.row());
66 int end = qMax(a: topLeft.row(), b: bottomRight.row());
67
68 QScatterDataArray array(end - start + 1);
69 int count = 0;
70 for (int i = start; i <= end; i++)
71 modelPosToScatterItem(modelRow: i, modelColumn: 0, item&: array[count++]);
72
73 m_proxy->setItems(index: start, items: array);
74 }
75 }
76}
77
78void ScatterItemModelHandler::handleRowsInserted(const QModelIndex &parent, int start, int end)
79{
80 // Do nothing if full reset already pending
81 if (!m_fullReset) {
82 if (!m_proxy->itemCount() || m_itemModel->columnCount() > 1) {
83 // If inserting into an empty array, do full asynchronous reset to avoid multiple
84 // separate inserts when initializing the model.
85 // If the data model is multi-column, do full asynchronous reset to simplify things
86 AbstractItemModelHandler::handleRowsInserted(parent, start, end);
87 } else {
88 QScatterDataArray array(end - start + 1);
89 int count = 0;
90 for (int i = start; i <= end; i++)
91 modelPosToScatterItem(modelRow: i, modelColumn: 0, item&: array[count++]);
92
93 m_proxy->insertItems(index: start, items: array);
94 }
95 }
96}
97
98void ScatterItemModelHandler::handleRowsRemoved(const QModelIndex &parent, int start, int end)
99{
100 Q_UNUSED(parent)
101
102 // Do nothing if full reset already pending
103 if (!m_fullReset) {
104 if (m_itemModel->columnCount() > 1) {
105 // If the data model is multi-column, do full asynchronous reset to simplify things
106 AbstractItemModelHandler::handleRowsRemoved(parent, start, end);
107 } else {
108 m_proxy->removeItems(index: start, removeCount: end - start + 1);
109 }
110 }
111}
112
113static inline QQuaternion toQuaternion(const QVariant &variant)
114{
115 if (variant.canConvert<QQuaternion>()) {
116 return variant.value<QQuaternion>();
117 } else if (variant.canConvert<QString>()) {
118 QString s = variant.toString();
119 if (!s.isEmpty()) {
120 bool angleAndAxis = false;
121 if (s.startsWith(c: QLatin1Char('@'))) {
122 angleAndAxis = true;
123 s = s.mid(position: 1);
124 }
125 if (s.count(c: QLatin1Char(',')) == 3) {
126 int index = s.indexOf(c: QLatin1Char(','));
127 int index2 = s.indexOf(c: QLatin1Char(','), from: index + 1);
128 int index3 = s.indexOf(c: QLatin1Char(','), from: index2 + 1);
129
130 bool sGood, xGood, yGood, zGood;
131 float sCoord = s.left(n: index).toFloat(ok: &sGood);
132 float xCoord = s.mid(position: index + 1, n: index2 - index - 1).toFloat(ok: &xGood);
133 float yCoord = s.mid(position: index2 + 1, n: index3 - index2 - 1).toFloat(ok: &yGood);
134 float zCoord = s.mid(position: index3 + 1).toFloat(ok: &zGood);
135
136 if (sGood && xGood && yGood && zGood) {
137 if (angleAndAxis)
138 return QQuaternion::fromAxisAndAngle(x: xCoord, y: yCoord, z: zCoord, angle: sCoord);
139 else
140 return QQuaternion(sCoord, xCoord, yCoord, zCoord);
141 }
142 }
143 }
144 }
145 return QQuaternion();
146}
147
148void ScatterItemModelHandler::modelPosToScatterItem(int modelRow, int modelColumn,
149 QScatterDataItem &item)
150{
151 QModelIndex index = m_itemModel->index(row: modelRow, column: modelColumn);
152 float xPos;
153 float yPos;
154 float zPos;
155 if (m_xPosRole != noRoleIndex) {
156 QVariant xValueVar = index.data(arole: m_xPosRole);
157 if (m_haveXPosPattern)
158 xPos = xValueVar.toString().replace(rx: m_xPosPattern, after: m_xPosReplace).toFloat();
159 else
160 xPos = xValueVar.toFloat();
161 } else {
162 xPos = 0.0f;
163 }
164 if (m_yPosRole != noRoleIndex) {
165 QVariant yValueVar = index.data(arole: m_yPosRole);
166 if (m_haveYPosPattern)
167 yPos = yValueVar.toString().replace(rx: m_yPosPattern, after: m_yPosReplace).toFloat();
168 else
169 yPos = yValueVar.toFloat();
170 } else {
171 yPos = 0.0f;
172 }
173 if (m_zPosRole != noRoleIndex) {
174 QVariant zValueVar = index.data(arole: m_zPosRole);
175 if (m_haveZPosPattern)
176 zPos = zValueVar.toString().replace(rx: m_zPosPattern, after: m_zPosReplace).toFloat();
177 else
178 zPos = zValueVar.toFloat();
179 } else {
180 zPos = 0.0f;
181 }
182 if (m_rotationRole != noRoleIndex) {
183 QVariant rotationVar = index.data(arole: m_rotationRole);
184 if (m_haveRotationPattern) {
185 item.setRotation(
186 toQuaternion(
187 variant: QVariant(rotationVar.toString().replace(rx: m_rotationPattern,
188 after: m_rotationReplace))));
189 } else {
190 item.setRotation(toQuaternion(variant: rotationVar));
191 }
192 }
193
194 item.setPosition(QVector3D(xPos, yPos, zPos));
195}
196
197// Resolve entire item model into QScatterDataArray.
198void ScatterItemModelHandler::resolveModel()
199{
200 if (m_itemModel.isNull()) {
201 m_proxy->resetArray(newArray: 0);
202 m_proxyArray = 0;
203 return;
204 }
205
206 m_xPosPattern = m_proxy->xPosRolePattern();
207 m_yPosPattern = m_proxy->yPosRolePattern();
208 m_zPosPattern = m_proxy->zPosRolePattern();
209 m_rotationPattern = m_proxy->rotationRolePattern();
210 m_xPosReplace = m_proxy->xPosRoleReplace();
211 m_yPosReplace = m_proxy->yPosRoleReplace();
212 m_zPosReplace = m_proxy->zPosRoleReplace();
213 m_rotationReplace = m_proxy->rotationRoleReplace();
214 m_haveXPosPattern = !m_xPosPattern.isEmpty() && m_xPosPattern.isValid();
215 m_haveYPosPattern = !m_yPosPattern.isEmpty() && m_yPosPattern.isValid();
216 m_haveZPosPattern = !m_zPosPattern.isEmpty() && m_zPosPattern.isValid();
217 m_haveRotationPattern = !m_rotationPattern.isEmpty() && m_rotationPattern.isValid();
218
219 QHash<int, QByteArray> roleHash = m_itemModel->roleNames();
220 m_xPosRole = roleHash.key(avalue: m_proxy->xPosRole().toLatin1(), defaultValue: noRoleIndex);
221 m_yPosRole = roleHash.key(avalue: m_proxy->yPosRole().toLatin1(), defaultValue: noRoleIndex);
222 m_zPosRole = roleHash.key(avalue: m_proxy->zPosRole().toLatin1(), defaultValue: noRoleIndex);
223 m_rotationRole = roleHash.key(avalue: m_proxy->rotationRole().toLatin1(), defaultValue: noRoleIndex);
224 const int columnCount = m_itemModel->columnCount();
225 const int rowCount = m_itemModel->rowCount();
226 const int totalCount = rowCount * columnCount;
227 int runningCount = 0;
228
229 // If dimensions have changed, recreate the array
230 if (m_proxyArray != m_proxy->array() || totalCount != m_proxyArray->size())
231 m_proxyArray = new QScatterDataArray(totalCount);
232
233 // Parse data into newProxyArray
234 for (int i = 0; i < rowCount; i++) {
235 for (int j = 0; j < columnCount; j++) {
236 modelPosToScatterItem(modelRow: i, modelColumn: j, item&: (*m_proxyArray)[runningCount]);
237 runningCount++;
238 }
239 }
240
241 m_proxy->resetArray(newArray: m_proxyArray);
242}
243
244QT_END_NAMESPACE_DATAVISUALIZATION
245

source code of qtdatavis3d/src/datavisualization/data/scatteritemmodelhandler.cpp