1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "declarativepieseries_p.h" |
5 | #include <QtCharts/QPieSlice> |
6 | #include <QtCharts/QVPieModelMapper> |
7 | #include <QtCharts/QHPieModelMapper> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | DeclarativePieSlice::DeclarativePieSlice(QObject *parent) |
12 | : QPieSlice(parent) |
13 | { |
14 | connect(sender: this, SIGNAL(brushChanged()), receiver: this, SLOT(handleBrushChanged())); |
15 | } |
16 | |
17 | QString DeclarativePieSlice::brushFilename() const |
18 | { |
19 | return m_brushFilename; |
20 | } |
21 | |
22 | void DeclarativePieSlice::setBrushFilename(const QString &brushFilename) |
23 | { |
24 | QImage brushImage(brushFilename); |
25 | if (QPieSlice::brush().textureImage() != brushImage) { |
26 | QBrush brush = QPieSlice::brush(); |
27 | brush.setTextureImage(brushImage); |
28 | QPieSlice::setBrush(brush); |
29 | m_brushFilename = brushFilename; |
30 | m_brushImage = brushImage; |
31 | emit brushFilenameChanged(brushFilename); |
32 | } |
33 | } |
34 | |
35 | void DeclarativePieSlice::handleBrushChanged() |
36 | { |
37 | // If the texture image of the brush has changed along the brush |
38 | // the brush file name needs to be cleared. |
39 | if (!m_brushFilename.isEmpty() && QPieSlice::brush().textureImage() != m_brushImage) { |
40 | m_brushFilename.clear(); |
41 | emit brushFilenameChanged(brushFilename: QString()); |
42 | } |
43 | } |
44 | |
45 | // Declarative pie series ========================================================================= |
46 | DeclarativePieSeries::DeclarativePieSeries(QQuickItem *parent) : |
47 | QPieSeries(parent) |
48 | { |
49 | connect(sender: this, SIGNAL(added(QList<QPieSlice*>)), receiver: this, SLOT(handleAdded(QList<QPieSlice*>))); |
50 | connect(sender: this, SIGNAL(removed(QList<QPieSlice*>)), receiver: this, SLOT(handleRemoved(QList<QPieSlice*>))); |
51 | } |
52 | |
53 | void DeclarativePieSeries::classBegin() |
54 | { |
55 | } |
56 | |
57 | void DeclarativePieSeries::componentComplete() |
58 | { |
59 | foreach (QObject *child, children()) { |
60 | if (qobject_cast<QPieSlice *>(object: child)) { |
61 | QPieSeries::append(slice: qobject_cast<QPieSlice *>(object: child)); |
62 | } else if (qobject_cast<QVPieModelMapper *>(object: child)) { |
63 | QVPieModelMapper *mapper = qobject_cast<QVPieModelMapper *>(object: child); |
64 | mapper->setSeries(this); |
65 | } else if (qobject_cast<QHPieModelMapper *>(object: child)) { |
66 | QHPieModelMapper *mapper = qobject_cast<QHPieModelMapper *>(object: child); |
67 | mapper->setSeries(this); |
68 | } |
69 | } |
70 | } |
71 | |
72 | QQmlListProperty<QObject> DeclarativePieSeries::seriesChildren() |
73 | { |
74 | return QQmlListProperty<QObject>(this, 0, &DeclarativePieSeries::appendSeriesChildren ,0,0,0); |
75 | } |
76 | |
77 | void DeclarativePieSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element) |
78 | { |
79 | // Empty implementation; the children are parsed in componentComplete instead |
80 | Q_UNUSED(list); |
81 | Q_UNUSED(element); |
82 | } |
83 | |
84 | QPieSlice *DeclarativePieSeries::at(int index) |
85 | { |
86 | QList<QPieSlice *> sliceList = slices(); |
87 | if (index >= 0 && index < sliceList.size()) |
88 | return sliceList[index]; |
89 | |
90 | return 0; |
91 | } |
92 | |
93 | QPieSlice *DeclarativePieSeries::find(QString label) |
94 | { |
95 | foreach (QPieSlice *slice, slices()) { |
96 | if (slice->label() == label) |
97 | return slice; |
98 | } |
99 | return 0; |
100 | } |
101 | |
102 | DeclarativePieSlice *DeclarativePieSeries::append(QString label, qreal value) |
103 | { |
104 | DeclarativePieSlice *slice = new DeclarativePieSlice(this); |
105 | slice->setLabel(label); |
106 | slice->setValue(value); |
107 | if (QPieSeries::append(slice)) |
108 | return slice; |
109 | delete slice; |
110 | return 0; |
111 | } |
112 | |
113 | bool DeclarativePieSeries::remove(QPieSlice *slice) |
114 | { |
115 | return QPieSeries::remove(slice); |
116 | } |
117 | |
118 | void DeclarativePieSeries::clear() |
119 | { |
120 | QPieSeries::clear(); |
121 | } |
122 | |
123 | void DeclarativePieSeries::handleAdded(const QList<QPieSlice *> &slices) |
124 | { |
125 | for (auto *slice : slices) |
126 | emit sliceAdded(slice); |
127 | } |
128 | |
129 | void DeclarativePieSeries::handleRemoved(const QList<QPieSlice *> &slices) |
130 | { |
131 | for (auto *slice : slices) |
132 | emit sliceRemoved(slice); |
133 | } |
134 | |
135 | QT_END_NAMESPACE |
136 | |
137 | #include "moc_declarativepieseries_p.cpp" |
138 | |