1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "declarativebarseries_p.h"
5#include <QtCharts/QBarSet>
6#include <QtCharts/QVBarModelMapper>
7#include <QtCharts/QHBarModelMapper>
8
9QT_BEGIN_NAMESPACE
10
11DeclarativeBarSet::DeclarativeBarSet(QObject *parent) : QBarSet(QString(), parent)
12{
13 connect(sender: this, SIGNAL(valuesAdded(int,int)), receiver: this, SLOT(handleCountChanged(int,int)));
14 connect(sender: this, SIGNAL(valuesRemoved(int,int)), receiver: this, SLOT(handleCountChanged(int,int)));
15 connect(sender: this, SIGNAL(brushChanged()), receiver: this, SLOT(handleBrushChanged()));
16}
17
18void DeclarativeBarSet::handleCountChanged(int index, int count)
19{
20 Q_UNUSED(index);
21 Q_UNUSED(count);
22 emit countChanged(count: QBarSet::count());
23}
24
25qreal DeclarativeBarSet::borderWidth() const
26{
27 return pen().widthF();
28}
29
30void DeclarativeBarSet::setBorderWidth(qreal width)
31{
32 if (width != pen().widthF()) {
33 QPen p = pen();
34 p.setWidthF(width);
35 setPen(p);
36 emit borderWidthChanged(width);
37 }
38}
39
40QVariantList DeclarativeBarSet::values()
41{
42 QVariantList values;
43 for (int i(0); i < count(); i++)
44 values.append(t: QVariant(QBarSet::at(index: i)));
45 return values;
46}
47
48void DeclarativeBarSet::setValues(QVariantList values)
49{
50 while (count())
51 remove(index: count() - 1);
52
53 if (values.size() > 0 && values.at(i: 0).canConvert<QPoint>()) {
54 // Create list of values for appending if the first item is Qt.point
55 int maxValue = 0;
56 for (int i = 0; i < values.size(); i++) {
57 if (values.at(i).canConvert<QPoint>() &&
58 values.at(i).toPoint().x() > maxValue) {
59 maxValue = values.at(i).toPoint().x();
60 }
61 }
62
63 QList<qreal> indexValueList;
64 indexValueList.resize(size: maxValue + 1);
65
66 for (int i = 0; i < values.size(); i++) {
67 if (values.at(i).canConvert<QPoint>()) {
68 indexValueList.replace(i: values.at(i).toPoint().x(), t: values.at(i).toPointF().y());
69 }
70 }
71
72 for (int i = 0; i < indexValueList.size(); i++)
73 QBarSet::append(value: indexValueList.at(i));
74
75 } else {
76 for (int i(0); i < values.size(); i++) {
77 if (values.at(i).canConvert<double>())
78 QBarSet::append(value: values[i].toDouble());
79 }
80 }
81}
82
83QString DeclarativeBarSet::brushFilename() const
84{
85 return m_brushFilename;
86}
87
88void DeclarativeBarSet::setBrushFilename(const QString &brushFilename)
89{
90 QImage brushImage(brushFilename);
91 if (QBarSet::brush().textureImage() != brushImage) {
92 QBrush brush = QBarSet::brush();
93 brush.setTextureImage(brushImage);
94 QBarSet::setBrush(brush);
95 m_brushFilename = brushFilename;
96 m_brushImage = brushImage;
97 emit brushFilenameChanged(brushFilename);
98 }
99}
100
101void DeclarativeBarSet::handleBrushChanged()
102{
103 // If the texture image of the brush has changed along the brush
104 // the brush file name needs to be cleared.
105 if (!m_brushFilename.isEmpty() && QBarSet::brush().textureImage() != m_brushImage) {
106 m_brushFilename.clear();
107 emit brushFilenameChanged(brushFilename: QString());
108 }
109}
110
111// Declarative bar series ======================================================================================
112DeclarativeBarSeries::DeclarativeBarSeries(QQuickItem *parent) :
113 QBarSeries(parent),
114 m_axes(new DeclarativeAxes(this))
115{
116 connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXChanged(QAbstractAxis*)));
117 connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYChanged(QAbstractAxis*)));
118 connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
119 connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*)));
120}
121
122void DeclarativeBarSeries::classBegin()
123{
124}
125
126void DeclarativeBarSeries::componentComplete()
127{
128 foreach (QObject *child, children()) {
129 if (qobject_cast<DeclarativeBarSet *>(object: child)) {
130 QAbstractBarSeries::append(set: qobject_cast<DeclarativeBarSet *>(object: child));
131 } else if (qobject_cast<QVBarModelMapper *>(object: child)) {
132 QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(object: child);
133 mapper->setSeries(this);
134 } else if (qobject_cast<QHBarModelMapper *>(object: child)) {
135 QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(object: child);
136 mapper->setSeries(this);
137 }
138 }
139}
140
141QQmlListProperty<QObject> DeclarativeBarSeries::seriesChildren()
142{
143 return QQmlListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren ,0,0,0);
144}
145
146void DeclarativeBarSeries::appendSeriesChildren(QQmlListProperty<QObject> *list, QObject *element)
147{
148 // Empty implementation; the children are parsed in componentComplete instead
149 Q_UNUSED(list);
150 Q_UNUSED(element);
151}
152
153DeclarativeBarSet *DeclarativeBarSeries::at(int index)
154{
155 QList<QBarSet *> setList = barSets();
156 if (index >= 0 && index < setList.size())
157 return qobject_cast<DeclarativeBarSet *>(object: setList[index]);
158
159 return 0;
160}
161
162DeclarativeBarSet *DeclarativeBarSeries::insert(int index, QString label, QVariantList values)
163{
164 DeclarativeBarSet *barset = new DeclarativeBarSet(this);
165 barset->setLabel(label);
166 barset->setValues(values);
167 if (QBarSeries::insert(index, set: barset))
168 return barset;
169 delete barset;
170 return 0;
171}
172
173// Declarative stacked bar series ==============================================================================
174DeclarativeStackedBarSeries::DeclarativeStackedBarSeries(QQuickItem *parent) :
175 QStackedBarSeries(parent),
176 m_axes(0)
177{
178 m_axes = new DeclarativeAxes(this);
179 connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXChanged(QAbstractAxis*)));
180 connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYChanged(QAbstractAxis*)));
181 connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
182 connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*)));
183}
184
185void DeclarativeStackedBarSeries::classBegin()
186{
187}
188
189void DeclarativeStackedBarSeries::componentComplete()
190{
191 foreach (QObject *child, children()) {
192 if (qobject_cast<DeclarativeBarSet *>(object: child)) {
193 QAbstractBarSeries::append(set: qobject_cast<DeclarativeBarSet *>(object: child));
194 } else if (qobject_cast<QVBarModelMapper *>(object: child)) {
195 QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(object: child);
196 mapper->setSeries(this);
197 } else if (qobject_cast<QHBarModelMapper *>(object: child)) {
198 QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(object: child);
199 mapper->setSeries(this);
200 }
201 }
202}
203
204
205QQmlListProperty<QObject> DeclarativeStackedBarSeries::seriesChildren()
206{
207 return QQmlListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren ,0,0,0);
208}
209
210void DeclarativeStackedBarSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element)
211{
212 // Empty implementation; the children are parsed in componentComplete instead
213 Q_UNUSED(list);
214 Q_UNUSED(element);
215}
216
217DeclarativeBarSet *DeclarativeStackedBarSeries::at(int index)
218{
219 QList<QBarSet *> setList = barSets();
220 if (index >= 0 && index < setList.size())
221 return qobject_cast<DeclarativeBarSet *>(object: setList[index]);
222
223 return 0;
224}
225
226DeclarativeBarSet *DeclarativeStackedBarSeries::insert(int index, QString label, QVariantList values)
227{
228 DeclarativeBarSet *barset = new DeclarativeBarSet(this);
229 barset->setLabel(label);
230 barset->setValues(values);
231 if (QStackedBarSeries::insert(index, set: barset))
232 return barset;
233 delete barset;
234 return 0;
235}
236
237// Declarative percent bar series ==============================================================================
238DeclarativePercentBarSeries::DeclarativePercentBarSeries(QQuickItem *parent) :
239 QPercentBarSeries(parent),
240 m_axes(0)
241{
242 m_axes = new DeclarativeAxes(this);
243 connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXChanged(QAbstractAxis*)));
244 connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYChanged(QAbstractAxis*)));
245 connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
246 connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*)));
247}
248
249void DeclarativePercentBarSeries::classBegin()
250{
251}
252
253void DeclarativePercentBarSeries::componentComplete()
254{
255 foreach (QObject *child, children()) {
256 if (qobject_cast<DeclarativeBarSet *>(object: child)) {
257 QAbstractBarSeries::append(set: qobject_cast<DeclarativeBarSet *>(object: child));
258 } else if (qobject_cast<QVBarModelMapper *>(object: child)) {
259 QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(object: child);
260 mapper->setSeries(this);
261 } else if (qobject_cast<QHBarModelMapper *>(object: child)) {
262 QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(object: child);
263 mapper->setSeries(this);
264 }
265 }
266}
267
268QQmlListProperty<QObject> DeclarativePercentBarSeries::seriesChildren()
269{
270 return QQmlListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren ,0,0,0);
271}
272
273void DeclarativePercentBarSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element)
274{
275 // Empty implementation; the children are parsed in componentComplete instead
276 Q_UNUSED(list);
277 Q_UNUSED(element);
278}
279
280DeclarativeBarSet *DeclarativePercentBarSeries::at(int index)
281{
282 QList<QBarSet *> setList = barSets();
283 if (index >= 0 && index < setList.size())
284 return qobject_cast<DeclarativeBarSet *>(object: setList[index]);
285
286 return 0;
287}
288
289DeclarativeBarSet *DeclarativePercentBarSeries::insert(int index, QString label, QVariantList values)
290{
291 DeclarativeBarSet *barset = new DeclarativeBarSet(this);
292 barset->setLabel(label);
293 barset->setValues(values);
294 if (QPercentBarSeries::insert(index, set: barset))
295 return barset;
296 delete barset;
297 return 0;
298}
299
300// Declarative horizontal bar series ===========================================================================
301DeclarativeHorizontalBarSeries::DeclarativeHorizontalBarSeries(QQuickItem *parent) :
302 QHorizontalBarSeries(parent),
303 m_axes(0)
304{
305 m_axes = new DeclarativeAxes(this);
306 connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXChanged(QAbstractAxis*)));
307 connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYChanged(QAbstractAxis*)));
308 connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
309 connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*)));
310}
311
312void DeclarativeHorizontalBarSeries::classBegin()
313{
314}
315
316void DeclarativeHorizontalBarSeries::componentComplete()
317{
318 foreach (QObject *child, children()) {
319 if (qobject_cast<DeclarativeBarSet *>(object: child)) {
320 QAbstractBarSeries::append(set: qobject_cast<DeclarativeBarSet *>(object: child));
321 } else if (qobject_cast<QVBarModelMapper *>(object: child)) {
322 QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(object: child);
323 mapper->setSeries(this);
324 } else if (qobject_cast<QHBarModelMapper *>(object: child)) {
325 QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(object: child);
326 mapper->setSeries(this);
327 }
328 }
329}
330
331QQmlListProperty<QObject> DeclarativeHorizontalBarSeries::seriesChildren()
332{
333 return QQmlListProperty<QObject>(this, 0, &DeclarativeHorizontalBarSeries::appendSeriesChildren ,0,0,0);
334}
335
336void DeclarativeHorizontalBarSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element)
337{
338 // Empty implementation; the children are parsed in componentComplete instead
339 Q_UNUSED(list);
340 Q_UNUSED(element);
341}
342
343DeclarativeBarSet *DeclarativeHorizontalBarSeries::at(int index)
344{
345 QList<QBarSet *> setList = barSets();
346 if (index >= 0 && index < setList.size())
347 return qobject_cast<DeclarativeBarSet *>(object: setList[index]);
348
349 return 0;
350}
351
352DeclarativeBarSet *DeclarativeHorizontalBarSeries::insert(int index, QString label, QVariantList values)
353{
354 DeclarativeBarSet *barset = new DeclarativeBarSet(this);
355 barset->setLabel(label);
356 barset->setValues(values);
357 if (QHorizontalBarSeries::insert(index, set: barset))
358 return barset;
359 delete barset;
360 return 0;
361}
362
363// Declarative horizontal stacked bar series ===================================================================
364DeclarativeHorizontalStackedBarSeries::DeclarativeHorizontalStackedBarSeries(QQuickItem *parent) :
365 QHorizontalStackedBarSeries(parent),
366 m_axes(0)
367{
368 m_axes = new DeclarativeAxes(this);
369 connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXChanged(QAbstractAxis*)));
370 connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYChanged(QAbstractAxis*)));
371 connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
372 connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*)));
373}
374
375void DeclarativeHorizontalStackedBarSeries::classBegin()
376{
377}
378
379void DeclarativeHorizontalStackedBarSeries::componentComplete()
380{
381 foreach (QObject *child, children()) {
382 if (qobject_cast<DeclarativeBarSet *>(object: child)) {
383 QAbstractBarSeries::append(set: qobject_cast<DeclarativeBarSet *>(object: child));
384 } else if (qobject_cast<QVBarModelMapper *>(object: child)) {
385 QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(object: child);
386 mapper->setSeries(this);
387 } else if (qobject_cast<QHBarModelMapper *>(object: child)) {
388 QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(object: child);
389 mapper->setSeries(this);
390 }
391 }
392}
393
394QQmlListProperty<QObject> DeclarativeHorizontalStackedBarSeries::seriesChildren()
395{
396 return QQmlListProperty<QObject>(this, 0, &DeclarativeHorizontalStackedBarSeries::appendSeriesChildren ,0,0,0);
397}
398
399void DeclarativeHorizontalStackedBarSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element)
400{
401 // Empty implementation; the children are parsed in componentComplete instead
402 Q_UNUSED(list);
403 Q_UNUSED(element);
404}
405
406DeclarativeBarSet *DeclarativeHorizontalStackedBarSeries::at(int index)
407{
408 QList<QBarSet *> setList = barSets();
409 if (index >= 0 && index < setList.size())
410 return qobject_cast<DeclarativeBarSet *>(object: setList[index]);
411
412 return 0;
413}
414
415DeclarativeBarSet *DeclarativeHorizontalStackedBarSeries::insert(int index, QString label, QVariantList values)
416{
417 DeclarativeBarSet *barset = new DeclarativeBarSet(this);
418 barset->setLabel(label);
419 barset->setValues(values);
420 if (QHorizontalStackedBarSeries::insert(index, set: barset))
421 return barset;
422 delete barset;
423 return 0;
424}
425
426// Declarative horizontal percent bar series ===================================================================
427DeclarativeHorizontalPercentBarSeries::DeclarativeHorizontalPercentBarSeries(QQuickItem *parent) :
428 QHorizontalPercentBarSeries(parent),
429 m_axes(0)
430{
431 m_axes = new DeclarativeAxes(this);
432 connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXChanged(QAbstractAxis*)));
433 connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYChanged(QAbstractAxis*)));
434 connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
435 connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*)));
436}
437
438void DeclarativeHorizontalPercentBarSeries::classBegin()
439{
440}
441
442void DeclarativeHorizontalPercentBarSeries::componentComplete()
443{
444 foreach (QObject *child, children()) {
445 if (qobject_cast<DeclarativeBarSet *>(object: child)) {
446 QAbstractBarSeries::append(set: qobject_cast<DeclarativeBarSet *>(object: child));
447 } else if (qobject_cast<QVBarModelMapper *>(object: child)) {
448 QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(object: child);
449 mapper->setSeries(this);
450 } else if (qobject_cast<QHBarModelMapper *>(object: child)) {
451 QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(object: child);
452 mapper->setSeries(this);
453 }
454 }
455}
456
457QQmlListProperty<QObject> DeclarativeHorizontalPercentBarSeries::seriesChildren()
458{
459 return QQmlListProperty<QObject>(this, 0, &DeclarativeHorizontalPercentBarSeries::appendSeriesChildren ,0,0,0);
460}
461
462void DeclarativeHorizontalPercentBarSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element)
463{
464 // Empty implementation; the children are parsed in componentComplete instead
465 Q_UNUSED(list);
466 Q_UNUSED(element);
467}
468
469DeclarativeBarSet *DeclarativeHorizontalPercentBarSeries::at(int index)
470{
471 QList<QBarSet *> setList = barSets();
472 if (index >= 0 && index < setList.size())
473 return qobject_cast<DeclarativeBarSet *>(object: setList[index]);
474
475 return 0;
476}
477
478DeclarativeBarSet *DeclarativeHorizontalPercentBarSeries::insert(int index, QString label, QVariantList values)
479{
480 DeclarativeBarSet *barset = new DeclarativeBarSet(this);
481 barset->setLabel(label);
482 barset->setValues(values);
483 if (QHorizontalPercentBarSeries::insert(index, set: barset))
484 return barset;
485 delete barset;
486 return 0;
487}
488
489QT_END_NAMESPACE
490
491#include "moc_declarativebarseries_p.cpp"
492

source code of qtcharts/src/chartsqml2/declarativebarseries.cpp