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 "declarativepieseries_p.h" |
31 | #include <QtCharts/QPieSlice> |
32 | #include <QtCharts/QVPieModelMapper> |
33 | #include <QtCharts/QHPieModelMapper> |
34 | |
35 | QT_CHARTS_BEGIN_NAMESPACE |
36 | |
37 | DeclarativePieSlice::DeclarativePieSlice(QObject *parent) |
38 | : QPieSlice(parent) |
39 | { |
40 | connect(sender: this, SIGNAL(brushChanged()), receiver: this, SLOT(handleBrushChanged())); |
41 | } |
42 | |
43 | QString DeclarativePieSlice::brushFilename() const |
44 | { |
45 | return m_brushFilename; |
46 | } |
47 | |
48 | void DeclarativePieSlice::setBrushFilename(const QString &brushFilename) |
49 | { |
50 | QImage brushImage(brushFilename); |
51 | if (QPieSlice::brush().textureImage() != brushImage) { |
52 | QBrush brush = QPieSlice::brush(); |
53 | brush.setTextureImage(brushImage); |
54 | QPieSlice::setBrush(brush); |
55 | m_brushFilename = brushFilename; |
56 | m_brushImage = brushImage; |
57 | emit brushFilenameChanged(brushFilename); |
58 | } |
59 | } |
60 | |
61 | void DeclarativePieSlice::handleBrushChanged() |
62 | { |
63 | // If the texture image of the brush has changed along the brush |
64 | // the brush file name needs to be cleared. |
65 | if (!m_brushFilename.isEmpty() && QPieSlice::brush().textureImage() != m_brushImage) { |
66 | m_brushFilename.clear(); |
67 | emit brushFilenameChanged(brushFilename: QString("" )); |
68 | } |
69 | } |
70 | |
71 | // Declarative pie series ========================================================================= |
72 | DeclarativePieSeries::DeclarativePieSeries(QQuickItem *parent) : |
73 | QPieSeries(parent) |
74 | { |
75 | connect(sender: this, SIGNAL(added(QList<QPieSlice*>)), receiver: this, SLOT(handleAdded(QList<QPieSlice*>))); |
76 | connect(sender: this, SIGNAL(removed(QList<QPieSlice*>)), receiver: this, SLOT(handleRemoved(QList<QPieSlice*>))); |
77 | } |
78 | |
79 | void DeclarativePieSeries::classBegin() |
80 | { |
81 | } |
82 | |
83 | void DeclarativePieSeries::componentComplete() |
84 | { |
85 | foreach (QObject *child, children()) { |
86 | if (qobject_cast<QPieSlice *>(object: child)) { |
87 | QPieSeries::append(slice: qobject_cast<QPieSlice *>(object: child)); |
88 | } else if (qobject_cast<QVPieModelMapper *>(object: child)) { |
89 | QVPieModelMapper *mapper = qobject_cast<QVPieModelMapper *>(object: child); |
90 | mapper->setSeries(this); |
91 | } else if (qobject_cast<QHPieModelMapper *>(object: child)) { |
92 | QHPieModelMapper *mapper = qobject_cast<QHPieModelMapper *>(object: child); |
93 | mapper->setSeries(this); |
94 | } |
95 | } |
96 | } |
97 | |
98 | QQmlListProperty<QObject> DeclarativePieSeries::seriesChildren() |
99 | { |
100 | return QQmlListProperty<QObject>(this, 0, &DeclarativePieSeries::appendSeriesChildren ,0,0,0); |
101 | } |
102 | |
103 | void DeclarativePieSeries::appendSeriesChildren(QQmlListProperty<QObject> * list, QObject *element) |
104 | { |
105 | // Empty implementation; the children are parsed in componentComplete instead |
106 | Q_UNUSED(list); |
107 | Q_UNUSED(element); |
108 | } |
109 | |
110 | QPieSlice *DeclarativePieSeries::at(int index) |
111 | { |
112 | QList<QPieSlice *> sliceList = slices(); |
113 | if (index >= 0 && index < sliceList.count()) |
114 | return sliceList[index]; |
115 | |
116 | return 0; |
117 | } |
118 | |
119 | QPieSlice *DeclarativePieSeries::find(QString label) |
120 | { |
121 | foreach (QPieSlice *slice, slices()) { |
122 | if (slice->label() == label) |
123 | return slice; |
124 | } |
125 | return 0; |
126 | } |
127 | |
128 | DeclarativePieSlice *DeclarativePieSeries::append(QString label, qreal value) |
129 | { |
130 | DeclarativePieSlice *slice = new DeclarativePieSlice(this); |
131 | slice->setLabel(label); |
132 | slice->setValue(value); |
133 | if (QPieSeries::append(slice)) |
134 | return slice; |
135 | delete slice; |
136 | return 0; |
137 | } |
138 | |
139 | bool DeclarativePieSeries::remove(QPieSlice *slice) |
140 | { |
141 | return QPieSeries::remove(slice); |
142 | } |
143 | |
144 | void DeclarativePieSeries::clear() |
145 | { |
146 | QPieSeries::clear(); |
147 | } |
148 | |
149 | void DeclarativePieSeries::handleAdded(QList<QPieSlice *> slices) |
150 | { |
151 | foreach (QPieSlice *slice, slices) |
152 | emit sliceAdded(slice); |
153 | } |
154 | |
155 | void DeclarativePieSeries::handleRemoved(QList<QPieSlice *> slices) |
156 | { |
157 | foreach (QPieSlice *slice, slices) |
158 | emit sliceRemoved(slice); |
159 | } |
160 | |
161 | QT_CHARTS_END_NAMESPACE |
162 | |
163 | #include "moc_declarativepieseries_p.cpp" |
164 | |