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

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