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