@@ -1,538 +1,537 | |||||
1 | #include "qpieseries.h" |
|
1 | #include "qpieseries.h" | |
2 | #include "qpieslice.h" |
|
2 | #include "qpieslice.h" | |
3 | #include "piepresenter.h" |
|
3 | #include "piepresenter.h" | |
4 | #include "pieslice.h" |
|
4 | #include "pieslice.h" | |
5 | #include <QFontMetrics> |
|
5 | #include <QFontMetrics> | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 |
|
10 | |||
11 | /*! |
|
11 | /*! | |
12 | \class QPieSeries::ChangeSet |
|
12 | \class QPieSeries::ChangeSet | |
13 | \brief Defines the changes in the series. |
|
13 | \brief Defines the changes in the series. | |
14 |
|
14 | |||
15 | Contains the changes that have occurred in the series. Lists of added, changed and removed slices. |
|
15 | Contains the changes that have occurred in the series. Lists of added, changed and removed slices. | |
16 |
|
16 | |||
17 | \sa QPieSeries::changed() |
|
17 | \sa QPieSeries::changed() | |
18 | */ |
|
18 | */ | |
19 |
|
19 | |||
20 | /*! |
|
20 | /*! | |
21 | \internal |
|
21 | \internal | |
22 | */ |
|
22 | */ | |
23 | void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) |
|
23 | void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) | |
24 | { |
|
24 | { | |
25 | if (!m_added.contains(slice)) |
|
25 | if (!m_added.contains(slice)) | |
26 | m_added << slice; |
|
26 | m_added << slice; | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | /*! |
|
29 | /*! | |
30 | \internal |
|
30 | \internal | |
31 | */ |
|
31 | */ | |
32 | void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices) |
|
32 | void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices) | |
33 | { |
|
33 | { | |
34 | foreach (QPieSlice* s, slices) |
|
34 | foreach (QPieSlice* s, slices) | |
35 | appendAdded(s); |
|
35 | appendAdded(s); | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | /*! |
|
38 | /*! | |
39 | \internal |
|
39 | \internal | |
40 | */ |
|
40 | */ | |
41 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) |
|
41 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) | |
42 | { |
|
42 | { | |
43 | if (!m_changed.contains(slice)) |
|
43 | if (!m_changed.contains(slice)) | |
44 | m_changed << slice; |
|
44 | m_changed << slice; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | /*! |
|
47 | /*! | |
48 | \internal |
|
48 | \internal | |
49 | */ |
|
49 | */ | |
50 | void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice) |
|
50 | void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice) | |
51 | { |
|
51 | { | |
52 | if (!m_removed.contains(slice)) |
|
52 | if (!m_removed.contains(slice)) | |
53 | m_removed << slice; |
|
53 | m_removed << slice; | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | /*! |
|
56 | /*! | |
57 | Returns a list of slices that have been added to the series. |
|
57 | Returns a list of slices that have been added to the series. | |
58 | \sa QPieSeries::changed() |
|
58 | \sa QPieSeries::changed() | |
59 | */ |
|
59 | */ | |
60 | QList<QPieSlice*> QPieSeries::ChangeSet::added() const |
|
60 | QList<QPieSlice*> QPieSeries::ChangeSet::added() const | |
61 | { |
|
61 | { | |
62 | return m_added; |
|
62 | return m_added; | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | /*! |
|
65 | /*! | |
66 | Returns a list of slices that have been changed in the series. |
|
66 | Returns a list of slices that have been changed in the series. | |
67 | \sa QPieSeries::changed() |
|
67 | \sa QPieSeries::changed() | |
68 | */ |
|
68 | */ | |
69 | QList<QPieSlice*> QPieSeries::ChangeSet::changed() const |
|
69 | QList<QPieSlice*> QPieSeries::ChangeSet::changed() const | |
70 | { |
|
70 | { | |
71 | return m_changed; |
|
71 | return m_changed; | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | /*! |
|
74 | /*! | |
75 | Returns a list of slices that have been removed from the series. |
|
75 | Returns a list of slices that have been removed from the series. | |
76 | \sa QPieSeries::changed() |
|
76 | \sa QPieSeries::changed() | |
77 | */ |
|
77 | */ | |
78 | QList<QPieSlice*> QPieSeries::ChangeSet::removed() const |
|
78 | QList<QPieSlice*> QPieSeries::ChangeSet::removed() const | |
79 | { |
|
79 | { | |
80 | return m_removed; |
|
80 | return m_removed; | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 |
|
83 | |||
84 | /*! |
|
84 | /*! | |
85 | Returns true if there are no added/changed or removed slices in the change set. |
|
85 | Returns true if there are no added/changed or removed slices in the change set. | |
86 | */ |
|
86 | */ | |
87 | bool QPieSeries::ChangeSet::isEmpty() const |
|
87 | bool QPieSeries::ChangeSet::isEmpty() const | |
88 | { |
|
88 | { | |
89 | if (m_added.count() || m_changed.count() || m_removed.count()) |
|
89 | if (m_added.count() || m_changed.count() || m_removed.count()) | |
90 | return false; |
|
90 | return false; | |
91 | return true; |
|
91 | return true; | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | /*! |
|
94 | /*! | |
95 | \enum QPieSeries::PiePosition |
|
95 | \enum QPieSeries::PiePosition | |
96 |
|
96 | |||
97 | This enum describes pie position within its bounding rectangle |
|
97 | This enum describes pie position within its bounding rectangle | |
98 |
|
98 | |||
99 | \value PiePositionMaximized |
|
99 | \value PiePositionMaximized | |
100 | \value PiePositionTopLeft |
|
100 | \value PiePositionTopLeft | |
101 | \value PiePositionTopRight |
|
101 | \value PiePositionTopRight | |
102 | \value PiePositionBottomLeft |
|
102 | \value PiePositionBottomLeft | |
103 | \value PiePositionBottomRight |
|
103 | \value PiePositionBottomRight | |
104 | */ |
|
104 | */ | |
105 |
|
105 | |||
106 | /*! |
|
106 | /*! | |
107 | \class QPieSeries |
|
107 | \class QPieSeries | |
108 | \brief Pie series API for QtCommercial Charts |
|
108 | \brief Pie series API for QtCommercial Charts | |
109 |
|
109 | |||
110 | The pie series defines a pie chart which consists of pie slices which are QPieSlice objects. |
|
110 | The pie series defines a pie chart which consists of pie slices which are QPieSlice objects. | |
111 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. |
|
111 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. | |
112 | The actual slice size (span) is determined by that relative value. |
|
112 | The actual slice size (span) is determined by that relative value. | |
113 |
|
113 | |||
114 | By default the pie is defined as full but it can be a partial pie. |
|
114 | By default the pie is defined as full but it can be a partial pie. | |
115 | This can be done by setting a starting angle and angle span to the series. |
|
115 | This can be done by setting a starting angle and angle span to the series. | |
116 |
|
116 | |||
117 | Example on how to create a chart with pie series: |
|
117 | Example on how to create a chart with pie series: | |
118 | \snippet ../example/piechart/main.cpp 1 |
|
118 | \snippet ../example/piechart/main.cpp 1 | |
119 |
|
119 | |||
120 | To help with the most common user intercation scenarions there some convenience functions. Specifically |
|
120 | To help with the most common user intercation scenarions there some convenience functions. Specifically | |
121 | exploding and higlighting: |
|
121 | exploding and higlighting: | |
122 | \snippet ../example/piechart/main.cpp 2 |
|
122 | \snippet ../example/piechart/main.cpp 2 | |
123 |
|
123 | |||
124 | */ |
|
124 | */ | |
125 |
|
125 | |||
126 | /*! |
|
126 | /*! | |
127 | Constructs a series object which is a child of \a parent. |
|
127 | Constructs a series object which is a child of \a parent. | |
128 | */ |
|
128 | */ | |
129 | QPieSeries::QPieSeries(QObject *parent) : |
|
129 | QPieSeries::QPieSeries(QObject *parent) : | |
130 | QChartSeries(parent), |
|
130 | QChartSeries(parent), | |
131 | m_sizeFactor(1.0), |
|
131 | m_sizeFactor(1.0), | |
132 | m_position(PiePositionMaximized), |
|
132 | m_position(PiePositionMaximized), | |
133 | m_pieStartAngle(0), |
|
133 | m_pieStartAngle(0), | |
134 | m_pieAngleSpan(360) |
|
134 | m_pieAngleSpan(360) | |
135 | { |
|
135 | { | |
136 |
|
136 | |||
137 | } |
|
137 | } | |
138 |
|
138 | |||
139 | /*! |
|
139 | /*! | |
140 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. |
|
140 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. | |
141 | */ |
|
141 | */ | |
142 | QPieSeries::~QPieSeries() |
|
142 | QPieSeries::~QPieSeries() | |
143 | { |
|
143 | { | |
144 |
|
144 | |||
145 | } |
|
145 | } | |
146 |
|
146 | |||
147 | /*! |
|
147 | /*! | |
148 | Returns QChartSeries::SeriesTypePie. |
|
148 | Returns QChartSeries::SeriesTypePie. | |
149 | */ |
|
149 | */ | |
150 | QChartSeries::QChartSeriesType QPieSeries::type() const |
|
150 | QChartSeries::QChartSeriesType QPieSeries::type() const | |
151 | { |
|
151 | { | |
152 | return QChartSeries::SeriesTypePie; |
|
152 | return QChartSeries::SeriesTypePie; | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | /*! |
|
155 | /*! | |
156 | Sets an array of values to the series. |
|
156 | \internal \a data | |
157 | TO BE REMOVED |
|
|||
158 | */ |
|
157 | */ | |
159 | bool QPieSeries::setData(QList<qreal> data) |
|
158 | bool QPieSeries::setData(QList<qreal> data) | |
160 | { |
|
159 | { | |
161 | // TODO: remove this function |
|
160 | // TODO: remove this function | |
162 | QList<QPieSlice*> slices; |
|
161 | QList<QPieSlice*> slices; | |
163 | foreach (qreal value, data) |
|
162 | foreach (qreal value, data) | |
164 | slices << new QPieSlice(value, QString::number(value)); |
|
163 | slices << new QPieSlice(value, QString::number(value)); | |
165 | set(slices); |
|
164 | set(slices); | |
166 | return true; |
|
165 | return true; | |
167 | } |
|
166 | } | |
168 |
|
167 | |||
169 | /*! |
|
168 | /*! | |
170 | Sets an array of \a slices to the series. |
|
169 | Sets an array of \a slices to the series. | |
171 | Slice ownership is passed to the series. |
|
170 | Slice ownership is passed to the series. | |
172 | */ |
|
171 | */ | |
173 | void QPieSeries::set(QList<QPieSlice*> slices) |
|
172 | void QPieSeries::set(QList<QPieSlice*> slices) | |
174 | { |
|
173 | { | |
175 | clear(); |
|
174 | clear(); | |
176 | add(slices); |
|
175 | add(slices); | |
177 | } |
|
176 | } | |
178 |
|
177 | |||
179 | /*! |
|
178 | /*! | |
180 | Adds an array of \a slices to the series. |
|
179 | Adds an array of \a slices to the series. | |
181 | Slice ownership is passed to the series. |
|
180 | Slice ownership is passed to the series. | |
182 | */ |
|
181 | */ | |
183 | void QPieSeries::add(QList<QPieSlice*> slices) |
|
182 | void QPieSeries::add(QList<QPieSlice*> slices) | |
184 | { |
|
183 | { | |
185 | ChangeSet changeSet; |
|
184 | ChangeSet changeSet; | |
186 | foreach (QPieSlice* s, slices) { |
|
185 | foreach (QPieSlice* s, slices) { | |
187 | s->setParent(this); |
|
186 | s->setParent(this); | |
188 | m_slices << s; |
|
187 | m_slices << s; | |
189 | changeSet.appendAdded(s); |
|
188 | changeSet.appendAdded(s); | |
190 | } |
|
189 | } | |
191 |
|
190 | |||
192 | updateDerivativeData(); |
|
191 | updateDerivativeData(); | |
193 |
|
192 | |||
194 | foreach (QPieSlice* s, slices) { |
|
193 | foreach (QPieSlice* s, slices) { | |
195 | connect(s, SIGNAL(changed()), this, SLOT(sliceChanged())); |
|
194 | connect(s, SIGNAL(changed()), this, SLOT(sliceChanged())); | |
196 | connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked())); |
|
195 | connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked())); | |
197 | connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); |
|
196 | connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); | |
198 | connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); |
|
197 | connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); | |
199 | } |
|
198 | } | |
200 |
|
199 | |||
201 | emit changed(changeSet); |
|
200 | emit changed(changeSet); | |
202 | } |
|
201 | } | |
203 |
|
202 | |||
204 | /*! |
|
203 | /*! | |
205 | Adds a single \a slice to the series. |
|
204 | Adds a single \a slice to the series. | |
206 | Slice ownership is passed to the series. |
|
205 | Slice ownership is passed to the series. | |
207 | */ |
|
206 | */ | |
208 | void QPieSeries::add(QPieSlice* slice) |
|
207 | void QPieSeries::add(QPieSlice* slice) | |
209 | { |
|
208 | { | |
210 | add(QList<QPieSlice*>() << slice); |
|
209 | add(QList<QPieSlice*>() << slice); | |
211 | } |
|
210 | } | |
212 |
|
211 | |||
213 |
|
212 | |||
214 | /*! |
|
213 | /*! | |
215 | Adds a single slice to the series with give \a value and \a name. |
|
214 | Adds a single slice to the series with give \a value and \a name. | |
216 | Slice ownership is passed to the series. |
|
215 | Slice ownership is passed to the series. | |
217 | */ |
|
216 | */ | |
218 | QPieSlice* QPieSeries::add(qreal value, QString name) |
|
217 | QPieSlice* QPieSeries::add(qreal value, QString name) | |
219 | { |
|
218 | { | |
220 | QPieSlice* slice = new QPieSlice(value, name); |
|
219 | QPieSlice* slice = new QPieSlice(value, name); | |
221 | add(slice); |
|
220 | add(slice); | |
222 | return slice; |
|
221 | return slice; | |
223 | } |
|
222 | } | |
224 |
|
223 | |||
225 | /*! |
|
224 | /*! | |
226 | Removes a single \a slice from the series and deletes the slice. |
|
225 | Removes a single \a slice from the series and deletes the slice. | |
227 |
|
226 | |||
228 | Do not reference this pointer after this call. |
|
227 | Do not reference this pointer after this call. | |
229 | */ |
|
228 | */ | |
230 | void QPieSeries::remove(QPieSlice* slice) |
|
229 | void QPieSeries::remove(QPieSlice* slice) | |
231 | { |
|
230 | { | |
232 | if (!m_slices.removeOne(slice)) { |
|
231 | if (!m_slices.removeOne(slice)) { | |
233 | Q_ASSERT(0); // TODO: how should this be reported? |
|
232 | Q_ASSERT(0); // TODO: how should this be reported? | |
234 | return; |
|
233 | return; | |
235 | } |
|
234 | } | |
236 |
|
235 | |||
237 | ChangeSet changeSet; |
|
236 | ChangeSet changeSet; | |
238 | changeSet.appendRemoved(slice); |
|
237 | changeSet.appendRemoved(slice); | |
239 | emit changed(changeSet); |
|
238 | emit changed(changeSet); | |
240 |
|
239 | |||
241 | delete slice; |
|
240 | delete slice; | |
242 | slice = NULL; |
|
241 | slice = NULL; | |
243 |
|
242 | |||
244 | updateDerivativeData(); |
|
243 | updateDerivativeData(); | |
245 | } |
|
244 | } | |
246 |
|
245 | |||
247 | /*! |
|
246 | /*! | |
248 | Clears all slices from the series. |
|
247 | Clears all slices from the series. | |
249 | */ |
|
248 | */ | |
250 | void QPieSeries::clear() |
|
249 | void QPieSeries::clear() | |
251 | { |
|
250 | { | |
252 | if (m_slices.count() == 0) |
|
251 | if (m_slices.count() == 0) | |
253 | return; |
|
252 | return; | |
254 |
|
253 | |||
255 | ChangeSet changeSet; |
|
254 | ChangeSet changeSet; | |
256 | foreach (QPieSlice* s, m_slices) { |
|
255 | foreach (QPieSlice* s, m_slices) { | |
257 | changeSet.appendRemoved(s); |
|
256 | changeSet.appendRemoved(s); | |
258 | m_slices.removeOne(s); |
|
257 | m_slices.removeOne(s); | |
259 | delete s; |
|
258 | delete s; | |
260 | } |
|
259 | } | |
261 | emit changed(changeSet); |
|
260 | emit changed(changeSet); | |
262 | updateDerivativeData(); |
|
261 | updateDerivativeData(); | |
263 | } |
|
262 | } | |
264 |
|
263 | |||
265 | /*! |
|
264 | /*! | |
266 | Counts the number of the slices in this series. |
|
265 | Counts the number of the slices in this series. | |
267 | */ |
|
266 | */ | |
268 | int QPieSeries::count() const |
|
267 | int QPieSeries::count() const | |
269 | { |
|
268 | { | |
270 | return m_slices.count(); |
|
269 | return m_slices.count(); | |
271 | } |
|
270 | } | |
272 |
|
271 | |||
273 | /*! |
|
272 | /*! | |
274 | Returns a list of slices that belong to this series. |
|
273 | Returns a list of slices that belong to this series. | |
275 | */ |
|
274 | */ | |
276 | QList<QPieSlice*> QPieSeries::slices() const |
|
275 | QList<QPieSlice*> QPieSeries::slices() const | |
277 | { |
|
276 | { | |
278 | return m_slices; |
|
277 | return m_slices; | |
279 | } |
|
278 | } | |
280 |
|
279 | |||
281 | /*! |
|
280 | /*! | |
282 | Sets the size \a factor of the pie. 1.0 is the default value. |
|
281 | Sets the size \a factor of the pie. 1.0 is the default value. | |
283 | Note that the pie will not grow beyond its absolute maximum size. |
|
282 | Note that the pie will not grow beyond its absolute maximum size. | |
284 | In practice its use is to make the pie appear smaller. |
|
283 | In practice its use is to make the pie appear smaller. | |
285 | \sa sizeFactor() |
|
284 | \sa sizeFactor() | |
286 | */ |
|
285 | */ | |
287 | void QPieSeries::setSizeFactor(qreal factor) |
|
286 | void QPieSeries::setSizeFactor(qreal factor) | |
288 | { |
|
287 | { | |
289 | if (factor < 0.0) |
|
288 | if (factor < 0.0) | |
290 | return; |
|
289 | return; | |
291 |
|
290 | |||
292 | if (m_sizeFactor != factor) { |
|
291 | if (m_sizeFactor != factor) { | |
293 | m_sizeFactor = factor; |
|
292 | m_sizeFactor = factor; | |
294 | emit sizeFactorChanged(); |
|
293 | emit sizeFactorChanged(); | |
295 | } |
|
294 | } | |
296 | } |
|
295 | } | |
297 |
|
296 | |||
298 | /*! |
|
297 | /*! | |
299 | Gets the size factor of the pie. |
|
298 | Gets the size factor of the pie. | |
300 | \sa setSizeFactor() |
|
299 | \sa setSizeFactor() | |
301 | */ |
|
300 | */ | |
302 | qreal QPieSeries::sizeFactor() const |
|
301 | qreal QPieSeries::sizeFactor() const | |
303 | { |
|
302 | { | |
304 | return m_sizeFactor; |
|
303 | return m_sizeFactor; | |
305 | } |
|
304 | } | |
306 |
|
305 | |||
307 | /*! |
|
306 | /*! | |
308 | Sets the \a position of the pie within its bounding rectangle. |
|
307 | Sets the \a position of the pie within its bounding rectangle. | |
309 | \sa PiePosition, position() |
|
308 | \sa PiePosition, position() | |
310 | */ |
|
309 | */ | |
311 | void QPieSeries::setPosition(PiePosition position) |
|
310 | void QPieSeries::setPosition(PiePosition position) | |
312 | { |
|
311 | { | |
313 | if (m_position != position) { |
|
312 | if (m_position != position) { | |
314 | m_position = position; |
|
313 | m_position = position; | |
315 | emit positionChanged(); |
|
314 | emit positionChanged(); | |
316 | } |
|
315 | } | |
317 | } |
|
316 | } | |
318 |
|
317 | |||
319 | /*! |
|
318 | /*! | |
320 | Gets the position of the pie within its bounding rectangle. |
|
319 | Gets the position of the pie within its bounding rectangle. | |
321 | \sa PiePosition, setPosition() |
|
320 | \sa PiePosition, setPosition() | |
322 | */ |
|
321 | */ | |
323 | QPieSeries::PiePosition QPieSeries::position() const |
|
322 | QPieSeries::PiePosition QPieSeries::position() const | |
324 | { |
|
323 | { | |
325 | return m_position; |
|
324 | return m_position; | |
326 | } |
|
325 | } | |
327 |
|
326 | |||
328 |
|
327 | |||
329 | /*! |
|
328 | /*! | |
330 | Sets the \a startAngle and \a angleSpan of this series. |
|
329 | Sets the \a startAngle and \a angleSpan of this series. | |
331 |
|
330 | |||
332 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
331 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | |
333 | */ |
|
332 | */ | |
334 | void QPieSeries::setSpan(qreal startAngle, qreal angleSpan) |
|
333 | void QPieSeries::setSpan(qreal startAngle, qreal angleSpan) | |
335 | { |
|
334 | { | |
336 | if (startAngle >= 0 && startAngle < 360 && |
|
335 | if (startAngle >= 0 && startAngle < 360 && | |
337 | angleSpan > 0 && angleSpan <= 360) { |
|
336 | angleSpan > 0 && angleSpan <= 360) { | |
338 | m_pieStartAngle = startAngle; |
|
337 | m_pieStartAngle = startAngle; | |
339 | m_pieAngleSpan = angleSpan; |
|
338 | m_pieAngleSpan = angleSpan; | |
340 | updateDerivativeData(); |
|
339 | updateDerivativeData(); | |
341 | } |
|
340 | } | |
342 | } |
|
341 | } | |
343 |
|
342 | |||
344 | /*! |
|
343 | /*! | |
345 | Sets the all the slice labels \a visible or invisible. |
|
344 | Sets the all the slice labels \a visible or invisible. | |
346 |
|
345 | |||
347 | \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() |
|
346 | \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() | |
348 | */ |
|
347 | */ | |
349 | void QPieSeries::setLabelsVisible(bool visible) |
|
348 | void QPieSeries::setLabelsVisible(bool visible) | |
350 | { |
|
349 | { | |
351 | foreach (QPieSlice* s, m_slices) |
|
350 | foreach (QPieSlice* s, m_slices) | |
352 | s->setLabelVisible(visible); |
|
351 | s->setLabelVisible(visible); | |
353 | } |
|
352 | } | |
354 |
|
353 | |||
355 | /*! |
|
354 | /*! | |
356 | Convenience method for exploding a slice when user clicks the pie. Set \a enable to true to |
|
355 | Convenience method for exploding a slice when user clicks the pie. Set \a enable to true to | |
357 | explode slices by clicking. |
|
356 | explode slices by clicking. | |
358 |
|
357 | |||
359 | \sa QPieSlice::isExploded(), QPieSlice::setExploded(), QPieSlice::setExplodeDistance() |
|
358 | \sa QPieSlice::isExploded(), QPieSlice::setExploded(), QPieSlice::setExplodeDistance() | |
360 | */ |
|
359 | */ | |
361 | void QPieSeries::enableClickExplodes(bool enable) |
|
360 | void QPieSeries::enableClickExplodes(bool enable) | |
362 | { |
|
361 | { | |
363 | if (enable) |
|
362 | if (enable) | |
364 | connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*))); |
|
363 | connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*))); | |
365 | else |
|
364 | else | |
366 | disconnect(this, SLOT(toggleExploded(QPieSlice*))); |
|
365 | disconnect(this, SLOT(toggleExploded(QPieSlice*))); | |
367 | } |
|
366 | } | |
368 |
|
367 | |||
369 | /*! |
|
368 | /*! | |
370 | Convenience method for highlighting a slice when user hovers over the slice. |
|
369 | Convenience method for highlighting a slice when user hovers over the slice. | |
371 | It changes the slice color to be lighter and shows the label of the slice. |
|
370 | It changes the slice color to be lighter and shows the label of the slice. | |
372 | Set \a enable to true to highlight a slice when user hovers on top of it. |
|
371 | Set \a enable to true to highlight a slice when user hovers on top of it. | |
373 |
|
372 | |||
374 | \sa QPieSlice::isExploded(), QPieSlice::setExploded() |
|
373 | \sa QPieSlice::isExploded(), QPieSlice::setExploded() | |
375 | */ |
|
374 | */ | |
376 |
|
375 | |||
377 | void QPieSeries::enableHoverHighlight(bool enable) |
|
376 | void QPieSeries::enableHoverHighlight(bool enable) | |
378 | { |
|
377 | { | |
379 | if (enable) { |
|
378 | if (enable) { | |
380 | connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*))); |
|
379 | connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*))); | |
381 | connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*))); |
|
380 | connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*))); | |
382 | } else { |
|
381 | } else { | |
383 | disconnect(this, SLOT(hoverEnter(QPieSlice*))); |
|
382 | disconnect(this, SLOT(hoverEnter(QPieSlice*))); | |
384 | disconnect(this, SLOT(hoverLeave(QPieSlice*))); |
|
383 | disconnect(this, SLOT(hoverLeave(QPieSlice*))); | |
385 | } |
|
384 | } | |
386 | } |
|
385 | } | |
387 |
|
386 | |||
388 | /*! |
|
387 | /*! | |
389 | \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) |
|
388 | \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) | |
390 |
|
389 | |||
391 | This signal emitted when something has changed in the series. |
|
390 | This signal emitted when something has changed in the series. | |
392 | The \a changeSet contains the details of which slices have been added, changed or removed. |
|
391 | The \a changeSet contains the details of which slices have been added, changed or removed. | |
393 |
|
392 | |||
394 | \sa QPieSeries::ChangeSet, QPieSlice::changed() |
|
393 | \sa QPieSeries::ChangeSet, QPieSlice::changed() | |
395 | */ |
|
394 | */ | |
396 |
|
395 | |||
397 | /*! |
|
396 | /*! | |
398 | \fn void QPieSeries::clicked(QPieSlice* slice) |
|
397 | \fn void QPieSeries::clicked(QPieSlice* slice) | |
399 |
|
398 | |||
400 | This signal is emitted when a \a slice has been clicked. |
|
399 | This signal is emitted when a \a slice has been clicked. | |
401 |
|
400 | |||
402 | \sa QPieSlice::clicked() |
|
401 | \sa QPieSlice::clicked() | |
403 | */ |
|
402 | */ | |
404 |
|
403 | |||
405 | /*! |
|
404 | /*! | |
406 | \fn void QPieSeries::hoverEnter(QPieSlice* slice) |
|
405 | \fn void QPieSeries::hoverEnter(QPieSlice* slice) | |
407 |
|
406 | |||
408 | This signal is emitted when user has hovered over a \a slice. |
|
407 | This signal is emitted when user has hovered over a \a slice. | |
409 |
|
408 | |||
410 | \sa QPieSlice::hoverEnter() |
|
409 | \sa QPieSlice::hoverEnter() | |
411 | */ |
|
410 | */ | |
412 |
|
411 | |||
413 | /*! |
|
412 | /*! | |
414 | \fn void QPieSeries::hoverLeave(QPieSlice* slice) |
|
413 | \fn void QPieSeries::hoverLeave(QPieSlice* slice) | |
415 |
|
414 | |||
416 | This signal is emitted when user has hovered away from a \a slice. |
|
415 | This signal is emitted when user has hovered away from a \a slice. | |
417 |
|
416 | |||
418 | \sa QPieSlice::hoverLeave() |
|
417 | \sa QPieSlice::hoverLeave() | |
419 | */ |
|
418 | */ | |
420 |
|
419 | |||
421 | /*! |
|
420 | /*! | |
422 | \fn void QPieSeries::sizeFactorChanged() |
|
421 | \fn void QPieSeries::sizeFactorChanged() | |
423 |
|
422 | |||
424 | This signal is emitted when size factor has been changed. |
|
423 | This signal is emitted when size factor has been changed. | |
425 |
|
424 | |||
426 | \sa sizeFactor(), setSizeFactor() |
|
425 | \sa sizeFactor(), setSizeFactor() | |
427 | */ |
|
426 | */ | |
428 |
|
427 | |||
429 | /*! |
|
428 | /*! | |
430 | \fn void QPieSeries::positionChanged() |
|
429 | \fn void QPieSeries::positionChanged() | |
431 |
|
430 | |||
432 | This signal is emitted when position of the pie has been changed. |
|
431 | This signal is emitted when position of the pie has been changed. | |
433 |
|
432 | |||
434 | \sa position(), setPosition() |
|
433 | \sa position(), setPosition() | |
435 | */ |
|
434 | */ | |
436 |
|
435 | |||
437 | void QPieSeries::sliceChanged() |
|
436 | void QPieSeries::sliceChanged() | |
438 | { |
|
437 | { | |
439 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
438 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
440 | Q_ASSERT(m_slices.contains(slice)); |
|
439 | Q_ASSERT(m_slices.contains(slice)); | |
441 |
|
440 | |||
442 | ChangeSet changeSet; |
|
441 | ChangeSet changeSet; | |
443 | changeSet.appendChanged(slice); |
|
442 | changeSet.appendChanged(slice); | |
444 | emit changed(changeSet); |
|
443 | emit changed(changeSet); | |
445 |
|
444 | |||
446 | updateDerivativeData(); |
|
445 | updateDerivativeData(); | |
447 | } |
|
446 | } | |
448 |
|
447 | |||
449 | void QPieSeries::sliceClicked() |
|
448 | void QPieSeries::sliceClicked() | |
450 | { |
|
449 | { | |
451 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
450 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
452 | Q_ASSERT(m_slices.contains(slice)); |
|
451 | Q_ASSERT(m_slices.contains(slice)); | |
453 | emit clicked(slice); |
|
452 | emit clicked(slice); | |
454 | } |
|
453 | } | |
455 |
|
454 | |||
456 | void QPieSeries::sliceHoverEnter() |
|
455 | void QPieSeries::sliceHoverEnter() | |
457 | { |
|
456 | { | |
458 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
457 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
459 | Q_ASSERT(m_slices.contains(slice)); |
|
458 | Q_ASSERT(m_slices.contains(slice)); | |
460 | emit hoverEnter(slice); |
|
459 | emit hoverEnter(slice); | |
461 | } |
|
460 | } | |
462 |
|
461 | |||
463 | void QPieSeries::sliceHoverLeave() |
|
462 | void QPieSeries::sliceHoverLeave() | |
464 | { |
|
463 | { | |
465 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
464 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
466 | Q_ASSERT(m_slices.contains(slice)); |
|
465 | Q_ASSERT(m_slices.contains(slice)); | |
467 | emit hoverLeave(slice); |
|
466 | emit hoverLeave(slice); | |
468 | } |
|
467 | } | |
469 |
|
468 | |||
470 | void QPieSeries::toggleExploded(QPieSlice* slice) |
|
469 | void QPieSeries::toggleExploded(QPieSlice* slice) | |
471 | { |
|
470 | { | |
472 | Q_ASSERT(slice); |
|
471 | Q_ASSERT(slice); | |
473 | slice->setExploded(!slice->isExploded()); |
|
472 | slice->setExploded(!slice->isExploded()); | |
474 | } |
|
473 | } | |
475 |
|
474 | |||
476 | void QPieSeries::highlightOn(QPieSlice* slice) |
|
475 | void QPieSeries::highlightOn(QPieSlice* slice) | |
477 | { |
|
476 | { | |
478 | Q_ASSERT(slice); |
|
477 | Q_ASSERT(slice); | |
479 | QColor c = slice->brush().color().lighter(); |
|
478 | QColor c = slice->brush().color().lighter(); | |
480 | slice->setBrush(c); |
|
479 | slice->setBrush(c); | |
481 | slice->setLabelVisible(true); |
|
480 | slice->setLabelVisible(true); | |
482 | } |
|
481 | } | |
483 |
|
482 | |||
484 | void QPieSeries::highlightOff(QPieSlice* slice) |
|
483 | void QPieSeries::highlightOff(QPieSlice* slice) | |
485 | { |
|
484 | { | |
486 | Q_ASSERT(slice); |
|
485 | Q_ASSERT(slice); | |
487 | QColor c = slice->brush().color().darker(150); |
|
486 | QColor c = slice->brush().color().darker(150); | |
488 | slice->setBrush(c); |
|
487 | slice->setBrush(c); | |
489 | slice->setLabelVisible(false); |
|
488 | slice->setLabelVisible(false); | |
490 | } |
|
489 | } | |
491 |
|
490 | |||
492 | void QPieSeries::updateDerivativeData() |
|
491 | void QPieSeries::updateDerivativeData() | |
493 | { |
|
492 | { | |
494 | m_total = 0; |
|
493 | m_total = 0; | |
495 |
|
494 | |||
496 | // nothing to do? |
|
495 | // nothing to do? | |
497 | if (m_slices.count() == 0) |
|
496 | if (m_slices.count() == 0) | |
498 | return; |
|
497 | return; | |
499 |
|
498 | |||
500 | // calculate total |
|
499 | // calculate total | |
501 | foreach (QPieSlice* s, m_slices) |
|
500 | foreach (QPieSlice* s, m_slices) | |
502 | m_total += s->value(); |
|
501 | m_total += s->value(); | |
503 |
|
502 | |||
504 | // we must have some values |
|
503 | // we must have some values | |
505 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? |
|
504 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? | |
506 |
|
505 | |||
507 | // update slice attributes |
|
506 | // update slice attributes | |
508 | qreal sliceAngle = m_pieStartAngle; |
|
507 | qreal sliceAngle = m_pieStartAngle; | |
509 | foreach (QPieSlice* s, m_slices) { |
|
508 | foreach (QPieSlice* s, m_slices) { | |
510 |
|
509 | |||
511 | bool changed = false; |
|
510 | bool changed = false; | |
512 |
|
511 | |||
513 | qreal percentage = s->value() / m_total; |
|
512 | qreal percentage = s->value() / m_total; | |
514 | if (s->m_percentage != percentage) { |
|
513 | if (s->m_percentage != percentage) { | |
515 | s->m_percentage = percentage; |
|
514 | s->m_percentage = percentage; | |
516 | changed = true; |
|
515 | changed = true; | |
517 | } |
|
516 | } | |
518 |
|
517 | |||
519 | qreal sliceSpan = m_pieAngleSpan * percentage; |
|
518 | qreal sliceSpan = m_pieAngleSpan * percentage; | |
520 | if (s->m_angleSpan != sliceSpan) { |
|
519 | if (s->m_angleSpan != sliceSpan) { | |
521 | s->m_angleSpan = sliceSpan; |
|
520 | s->m_angleSpan = sliceSpan; | |
522 | changed = true; |
|
521 | changed = true; | |
523 | } |
|
522 | } | |
524 |
|
523 | |||
525 | if (s->m_angle != sliceAngle) { |
|
524 | if (s->m_angle != sliceAngle) { | |
526 | s->m_angle = sliceAngle; |
|
525 | s->m_angle = sliceAngle; | |
527 | changed = true; |
|
526 | changed = true; | |
528 | } |
|
527 | } | |
529 | sliceAngle += sliceSpan; |
|
528 | sliceAngle += sliceSpan; | |
530 |
|
529 | |||
531 | if (changed) |
|
530 | if (changed) | |
532 | emit s->changed(); |
|
531 | emit s->changed(); | |
533 | } |
|
532 | } | |
534 | } |
|
533 | } | |
535 |
|
534 | |||
536 | #include "moc_qpieseries.cpp" |
|
535 | #include "moc_qpieseries.cpp" | |
537 |
|
536 | |||
538 | QTCOMMERCIALCHART_END_NAMESPACE |
|
537 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now