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 "declarativeseries_p.h"
31 #include <QtCore/QMetaMethod>
32
33 QT_BEGIN_NAMESPACE_DATAVISUALIZATION
34
setSeriesGradient(QAbstract3DSeries * series,const ColorGradient & gradient,GradientType type)35 static void setSeriesGradient(QAbstract3DSeries *series, const ColorGradient &gradient,
36 GradientType type)
37 {
38 QLinearGradient newGradient;
39 QGradientStops stops;
40 QList<ColorGradientStop *> qmlstops = gradient.m_stops;
41
42 // Get sorted gradient stops
43 for (int i = 0; i < qmlstops.size(); i++) {
44 int j = 0;
45 while (j < stops.size() && stops.at(j).first < qmlstops[i]->position())
46 j++;
47 stops.insert(j, QGradientStop(qmlstops.at(i)->position(), qmlstops.at(i)->color()));
48 }
49
50 newGradient.setStops(stops);
51 switch (type) {
52 case GradientTypeBase:
53 series->setBaseGradient(newGradient);
54 break;
55 case GradientTypeSingle:
56 series->setSingleHighlightGradient(newGradient);
57 break;
58 case GradientTypeMulti:
59 series->setMultiHighlightGradient(newGradient);
60 break;
61 default: // Never goes here
62 break;
63 }
64 }
65
connectSeriesGradient(QAbstract3DSeries * series,ColorGradient * newGradient,GradientType type,ColorGradient ** memberGradient)66 static void connectSeriesGradient(QAbstract3DSeries *series, ColorGradient *newGradient,
67 GradientType type, ColorGradient **memberGradient)
68 {
69 // connect new / disconnect old
70 if (newGradient != *memberGradient) {
71 if (*memberGradient)
72 QObject::disconnect(*memberGradient, 0, series, 0);
73
74 *memberGradient = newGradient;
75
76 int updatedIndex = newGradient->metaObject()->indexOfSignal("updated()");
77 QMetaMethod updateFunction = newGradient->metaObject()->method(updatedIndex);
78 int handleIndex = -1;
79 switch (type) {
80 case GradientTypeBase:
81 handleIndex = series->metaObject()->indexOfSlot("handleBaseGradientUpdate()");
82 break;
83 case GradientTypeSingle:
84 handleIndex = series->metaObject()->indexOfSlot("handleSingleHighlightGradientUpdate()");
85 break;
86 case GradientTypeMulti:
87 handleIndex = series->metaObject()->indexOfSlot("handleMultiHighlightGradientUpdate()");
88 break;
89 default: // Never goes here
90 break;
91 }
92 QMetaMethod handleFunction = series->metaObject()->method(handleIndex);
93
94 if (*memberGradient)
95 QObject::connect(*memberGradient, updateFunction, series, handleFunction);
96 }
97
98 if (*memberGradient)
99 setSeriesGradient(series, **memberGradient, type);
100 }
101
DeclarativeBar3DSeries(QObject * parent)102 DeclarativeBar3DSeries::DeclarativeBar3DSeries(QObject *parent)
103 : QBar3DSeries(parent),
104 m_baseGradient(0),
105 m_singleHighlightGradient(0),
106 m_multiHighlightGradient(0)
107 {
108 QObject::connect(this, &QBar3DSeries::selectedBarChanged, this,
109 &DeclarativeBar3DSeries::selectedBarChanged);
110 }
111
~DeclarativeBar3DSeries()112 DeclarativeBar3DSeries::~DeclarativeBar3DSeries()
113 {
114 }
115
seriesChildren()116 QQmlListProperty<QObject> DeclarativeBar3DSeries::seriesChildren()
117 {
118 return QQmlListProperty<QObject>(this, this, &DeclarativeBar3DSeries::appendSeriesChildren
119 , 0, 0, 0);
120 }
121
appendSeriesChildren(QQmlListProperty<QObject> * list,QObject * element)122 void DeclarativeBar3DSeries::appendSeriesChildren(QQmlListProperty<QObject> *list, QObject *element)
123 {
124 QBarDataProxy *proxy = qobject_cast<QBarDataProxy *>(element);
125 if (proxy)
126 reinterpret_cast<DeclarativeBar3DSeries *>(list->data)->setDataProxy(proxy);
127 }
128
setSelectedBar(const QPointF & position)129 void DeclarativeBar3DSeries::setSelectedBar(const QPointF &position)
130 {
131 QBar3DSeries::setSelectedBar(position.toPoint());
132 }
133
selectedBar() const134 QPointF DeclarativeBar3DSeries::selectedBar() const
135 {
136 return QPointF(QBar3DSeries::selectedBar());
137 }
138
invalidSelectionPosition() const139 QPointF DeclarativeBar3DSeries::invalidSelectionPosition() const
140 {
141 return QPointF(QBar3DSeries::invalidSelectionPosition());
142 }
143
setBaseGradient(ColorGradient * gradient)144 void DeclarativeBar3DSeries::setBaseGradient(ColorGradient *gradient)
145 {
146 connectSeriesGradient(this, gradient, GradientTypeBase, &m_baseGradient);
147 }
148
baseGradient() const149 ColorGradient *DeclarativeBar3DSeries::baseGradient() const
150 {
151 return m_baseGradient;
152 }
153
setSingleHighlightGradient(ColorGradient * gradient)154 void DeclarativeBar3DSeries::setSingleHighlightGradient(ColorGradient *gradient)
155 {
156 connectSeriesGradient(this, gradient, GradientTypeSingle, &m_singleHighlightGradient);
157 }
158
singleHighlightGradient() const159 ColorGradient *DeclarativeBar3DSeries::singleHighlightGradient() const
160 {
161 return m_singleHighlightGradient;
162 }
163
setMultiHighlightGradient(ColorGradient * gradient)164 void DeclarativeBar3DSeries::setMultiHighlightGradient(ColorGradient *gradient)
165 {
166 connectSeriesGradient(this, gradient, GradientTypeMulti, &m_multiHighlightGradient);
167 }
168
multiHighlightGradient() const169 ColorGradient *DeclarativeBar3DSeries::multiHighlightGradient() const
170 {
171 return m_multiHighlightGradient;
172 }
173
handleBaseGradientUpdate()174 void DeclarativeBar3DSeries::handleBaseGradientUpdate()
175 {
176 if (m_baseGradient)
177 setSeriesGradient(this, *m_baseGradient, GradientTypeBase);
178 }
179
handleSingleHighlightGradientUpdate()180 void DeclarativeBar3DSeries::handleSingleHighlightGradientUpdate()
181 {
182 if (m_singleHighlightGradient)
183 setSeriesGradient(this, *m_singleHighlightGradient, GradientTypeSingle);
184 }
185
handleMultiHighlightGradientUpdate()186 void DeclarativeBar3DSeries::handleMultiHighlightGradientUpdate()
187 {
188 if (m_multiHighlightGradient)
189 setSeriesGradient(this, *m_multiHighlightGradient, GradientTypeMulti);
190 }
191
DeclarativeScatter3DSeries(QObject * parent)192 DeclarativeScatter3DSeries::DeclarativeScatter3DSeries(QObject *parent)
193 : QScatter3DSeries(parent),
194 m_baseGradient(0),
195 m_singleHighlightGradient(0),
196 m_multiHighlightGradient(0)
197 {
198 }
199
~DeclarativeScatter3DSeries()200 DeclarativeScatter3DSeries::~DeclarativeScatter3DSeries()
201 {
202 }
203
seriesChildren()204 QQmlListProperty<QObject> DeclarativeScatter3DSeries::seriesChildren()
205 {
206 return QQmlListProperty<QObject>(this, this, &DeclarativeScatter3DSeries::appendSeriesChildren
207 , 0, 0, 0);
208 }
209
appendSeriesChildren(QQmlListProperty<QObject> * list,QObject * element)210 void DeclarativeScatter3DSeries::appendSeriesChildren(QQmlListProperty<QObject> *list,
211 QObject *element)
212 {
213 QScatterDataProxy *proxy = qobject_cast<QScatterDataProxy *>(element);
214 if (proxy)
215 reinterpret_cast<DeclarativeScatter3DSeries *>(list->data)->setDataProxy(proxy);
216 }
217
setBaseGradient(ColorGradient * gradient)218 void DeclarativeScatter3DSeries::setBaseGradient(ColorGradient *gradient)
219 {
220 connectSeriesGradient(this, gradient, GradientTypeBase, &m_baseGradient);
221 }
222
baseGradient() const223 ColorGradient *DeclarativeScatter3DSeries::baseGradient() const
224 {
225 return m_baseGradient;
226 }
227
setSingleHighlightGradient(ColorGradient * gradient)228 void DeclarativeScatter3DSeries::setSingleHighlightGradient(ColorGradient *gradient)
229 {
230 connectSeriesGradient(this, gradient, GradientTypeSingle, &m_singleHighlightGradient);
231 }
232
singleHighlightGradient() const233 ColorGradient *DeclarativeScatter3DSeries::singleHighlightGradient() const
234 {
235 return m_singleHighlightGradient;
236 }
237
setMultiHighlightGradient(ColorGradient * gradient)238 void DeclarativeScatter3DSeries::setMultiHighlightGradient(ColorGradient *gradient)
239 {
240 connectSeriesGradient(this, gradient, GradientTypeMulti, &m_multiHighlightGradient);
241 }
242
multiHighlightGradient() const243 ColorGradient *DeclarativeScatter3DSeries::multiHighlightGradient() const
244 {
245 return m_multiHighlightGradient;
246 }
247
invalidSelectionIndex() const248 int DeclarativeScatter3DSeries::invalidSelectionIndex() const
249 {
250 return QScatter3DSeries::invalidSelectionIndex();
251 }
252
handleBaseGradientUpdate()253 void DeclarativeScatter3DSeries::handleBaseGradientUpdate()
254 {
255 if (m_baseGradient)
256 setSeriesGradient(this, *m_baseGradient, GradientTypeBase);
257 }
258
handleSingleHighlightGradientUpdate()259 void DeclarativeScatter3DSeries::handleSingleHighlightGradientUpdate()
260 {
261 if (m_singleHighlightGradient)
262 setSeriesGradient(this, *m_singleHighlightGradient, GradientTypeSingle);
263 }
264
handleMultiHighlightGradientUpdate()265 void DeclarativeScatter3DSeries::handleMultiHighlightGradientUpdate()
266 {
267 if (m_multiHighlightGradient)
268 setSeriesGradient(this, *m_multiHighlightGradient, GradientTypeMulti);
269 }
270
DeclarativeSurface3DSeries(QObject * parent)271 DeclarativeSurface3DSeries::DeclarativeSurface3DSeries(QObject *parent)
272 : QSurface3DSeries(parent),
273 m_baseGradient(0),
274 m_singleHighlightGradient(0),
275 m_multiHighlightGradient(0)
276 {
277 QObject::connect(this, &QSurface3DSeries::selectedPointChanged, this,
278 &DeclarativeSurface3DSeries::selectedPointChanged);
279 }
280
~DeclarativeSurface3DSeries()281 DeclarativeSurface3DSeries::~DeclarativeSurface3DSeries()
282 {
283 }
284
setSelectedPoint(const QPointF & position)285 void DeclarativeSurface3DSeries::setSelectedPoint(const QPointF &position)
286 {
287 QSurface3DSeries::setSelectedPoint(position.toPoint());
288 }
289
selectedPoint() const290 QPointF DeclarativeSurface3DSeries::selectedPoint() const
291 {
292 return QPointF(QSurface3DSeries::selectedPoint());
293 }
294
invalidSelectionPosition() const295 QPointF DeclarativeSurface3DSeries::invalidSelectionPosition() const
296 {
297 return QPointF(QSurface3DSeries::invalidSelectionPosition());
298 }
299
seriesChildren()300 QQmlListProperty<QObject> DeclarativeSurface3DSeries::seriesChildren()
301 {
302 return QQmlListProperty<QObject>(this, this, &DeclarativeSurface3DSeries::appendSeriesChildren
303 , 0, 0, 0);
304 }
305
appendSeriesChildren(QQmlListProperty<QObject> * list,QObject * element)306 void DeclarativeSurface3DSeries::appendSeriesChildren(QQmlListProperty<QObject> *list,
307 QObject *element)
308 {
309 QSurfaceDataProxy *proxy = qobject_cast<QSurfaceDataProxy *>(element);
310 if (proxy)
311 reinterpret_cast<DeclarativeSurface3DSeries *>(list->data)->setDataProxy(proxy);
312 }
313
setBaseGradient(ColorGradient * gradient)314 void DeclarativeSurface3DSeries::setBaseGradient(ColorGradient *gradient)
315 {
316 connectSeriesGradient(this, gradient, GradientTypeBase, &m_baseGradient);
317 }
318
baseGradient() const319 ColorGradient *DeclarativeSurface3DSeries::baseGradient() const
320 {
321 return m_baseGradient;
322 }
323
setSingleHighlightGradient(ColorGradient * gradient)324 void DeclarativeSurface3DSeries::setSingleHighlightGradient(ColorGradient *gradient)
325 {
326 connectSeriesGradient(this, gradient, GradientTypeSingle, &m_singleHighlightGradient);
327 }
328
singleHighlightGradient() const329 ColorGradient *DeclarativeSurface3DSeries::singleHighlightGradient() const
330 {
331 return m_singleHighlightGradient;
332 }
333
setMultiHighlightGradient(ColorGradient * gradient)334 void DeclarativeSurface3DSeries::setMultiHighlightGradient(ColorGradient *gradient)
335 {
336 connectSeriesGradient(this, gradient, GradientTypeMulti, &m_multiHighlightGradient);
337 }
338
multiHighlightGradient() const339 ColorGradient *DeclarativeSurface3DSeries::multiHighlightGradient() const
340 {
341 return m_multiHighlightGradient;
342 }
343
handleBaseGradientUpdate()344 void DeclarativeSurface3DSeries::handleBaseGradientUpdate()
345 {
346 if (m_baseGradient)
347 setSeriesGradient(this, *m_baseGradient, GradientTypeBase);
348 }
349
handleSingleHighlightGradientUpdate()350 void DeclarativeSurface3DSeries::handleSingleHighlightGradientUpdate()
351 {
352 if (m_singleHighlightGradient)
353 setSeriesGradient(this, *m_singleHighlightGradient, GradientTypeSingle);
354 }
355
handleMultiHighlightGradientUpdate()356 void DeclarativeSurface3DSeries::handleMultiHighlightGradientUpdate()
357 {
358 if (m_multiHighlightGradient)
359 setSeriesGradient(this, *m_multiHighlightGradient, GradientTypeMulti);
360 }
361
362 QT_END_NAMESPACE_DATAVISUALIZATION
363