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