##// END OF EJS Templates
pie: added everything as a property in QPieSlice...
Jani Honkonen -
r1274:9c11726517fa
parent child
Show More
@@ -0,0 +1,43
1 #ifndef QPIESLICE_P_H
2 #define QPIESLICE_P_H
3
4 #include <QObject>
5 #include "qpieslice.h"
6 #include "pieslicedata_p.h"
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class QPieSlicePrivate : public QObject
11 {
12 Q_OBJECT
13
14 public:
15 QPieSlicePrivate(QPieSlice *parent);
16 ~QPieSlicePrivate();
17
18 static QPieSlicePrivate* fromSlice(QPieSlice *slice);
19
20 void setPen(const QPen &pen, bool themed);
21 void setBrush(const QBrush &brush, bool themed);
22 void setLabelPen(const QPen &pen, bool themed);
23 void setLabelFont(const QFont &font, bool themed);
24
25 void setPercentage(qreal percentage);
26 void setStartAngle(qreal angle);
27 void setAngleSpan(qreal span);
28
29 private:
30 PieSliceData m_data;
31
32 private:
33 friend class QPieSeriesPrivate;
34 friend class ChartTheme;
35 friend class PieChartItem;
36
37 QPieSlice * const q_ptr;
38 Q_DECLARE_PUBLIC(QPieSlice)
39 };
40
41 QTCOMMERCIALCHART_END_NAMESPACE
42
43 #endif // QPIESLICE_P_H
@@ -29,7 +29,7 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QAbstractSeries* dri
29 setValue(value);
29 setValue(value);
30 updateLabel();
30 updateLabel();
31 setLabelFont(QFont("Arial", 8));
31 setLabelFont(QFont("Arial", 8));
32 connect(this, SIGNAL(calculatedDataChanged()), this, SLOT(updateLabel()));
32 connect(this, SIGNAL(percentageChanged()), this, SLOT(updateLabel()));
33 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
33 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
34 }
34 }
35
35
@@ -36,6 +36,7
36 #include "qscatterseries.h"
36 #include "qscatterseries.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieslice.h"
38 #include "qpieslice.h"
39 #include "qpieslice_p.h"
39 #include "qsplineseries.h"
40 #include "qsplineseries.h"
40
41
41 //items
42 //items
@@ -225,32 +226,19 void ChartTheme::decorate(QPieSeries *series, int index)
225 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
226 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
226
227
227 QPieSlice *s = series->slices().at(i);
228 QPieSlice *s = series->slices().at(i);
228 PieSliceData data = PieSliceData::fromSlice(s);
229 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
229
230
230 if (data.m_slicePen.isThemed() || m_force) {
231 if (d->m_data.m_slicePen.isThemed() || m_force)
231 data.m_slicePen = penColor;
232 d->setPen(penColor, true);
232 data.m_slicePen.setThemed(true);
233 }
234
235 if (data.m_sliceBrush.isThemed() || m_force) {
236 data.m_sliceBrush = brushColor;
237 data.m_sliceBrush.setThemed(true);
238 }
239
233
240 if (data.m_labelPen.isThemed() || m_force) {
234 if (d->m_data.m_sliceBrush.isThemed() || m_force)
241 data.m_labelPen = QPen(m_titleBrush.color());
235 d->setBrush(brushColor, true);
242 data.m_labelPen.setThemed(true);
243 }
244
236
245 if (data.m_labelFont.isThemed() || m_force) {
237 if (d->m_data.m_labelPen.isThemed() || m_force)
246 data.m_labelFont = m_labelFont;
238 d->setLabelPen(QPen(m_titleBrush.color()), true);
247 data.m_labelFont.setThemed(true);
248 }
249
239
250 if (PieSliceData::fromSlice(s) != data) {
240 if (d->m_data.m_labelFont.isThemed() || m_force)
251 PieSliceData::fromSlice(s) = data;
241 d->setLabelFont(m_labelFont, true);
252 emit PieSliceData::emitAppearanceChanged(s);
253 }
254 }
242 }
255 }
243 }
256
244
@@ -156,7 +156,7 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend
156 m_pieslice(pieslice)
156 m_pieslice(pieslice)
157 {
157 {
158 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
158 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
159 QObject::connect(pieslice, SIGNAL(appearanceChanged()), this, SLOT(updated()));
159 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
160 updated();
160 updated();
161 }
161 }
162
162
@@ -14,6 +14,7 PRIVATE_HEADERS += \
14 $$PWD/pieslicedata_p.h \
14 $$PWD/pieslicedata_p.h \
15 $$PWD/piechartitem_p.h \
15 $$PWD/piechartitem_p.h \
16 $$PWD/piesliceitem_p.h \
16 $$PWD/piesliceitem_p.h \
17 $$PWD/qpieslice_p.h \
17 $$PWD/qpieseries_p.h \
18 $$PWD/qpieseries_p.h \
18 $$PWD/qpiemodelmapper_p.h
19 $$PWD/qpiemodelmapper_p.h
19
20
@@ -23,3 +24,4 PUBLIC_HEADERS += \
23 $$PWD/qpiemodelmapper.h \
24 $$PWD/qpiemodelmapper.h \
24 $$PWD/qvpiemodelmapper.h \
25 $$PWD/qvpiemodelmapper.h \
25 $$PWD/qhpiemodelmapper.h
26 $$PWD/qhpiemodelmapper.h
27
@@ -21,6 +21,7
21 #include "piechartitem_p.h"
21 #include "piechartitem_p.h"
22 #include "piesliceitem_p.h"
22 #include "piesliceitem_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieslice_p.h"
24 #include "qpieseries.h"
25 #include "qpieseries.h"
25 #include "qpieseries_p.h"
26 #include "qpieseries_p.h"
26 #include "chartpresenter_p.h"
27 #include "chartpresenter_p.h"
@@ -42,6 +43,7 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
42 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
43 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
43 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
44 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
44 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
45 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
46 connect(QPieSeriesPrivate::fromSeries(series), SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout()));
45
47
46 // Note: the following does not affect as long as the item does not have anything to paint
48 // Note: the following does not affect as long as the item does not have anything to paint
47 setZValue(ChartPresenter::PieSeriesZValue);
49 setZValue(ChartPresenter::PieSeriesZValue);
@@ -134,11 +136,18 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
134 PieSliceItem* sliceItem = new PieSliceItem(this);
136 PieSliceItem* sliceItem = new PieSliceItem(this);
135 m_sliceItems.insert(slice, sliceItem);
137 m_sliceItems.insert(slice, sliceItem);
136
138
137 // note: do need to connect to slice valueChanged(). calculatedDataChanged() is enough.
139 // Note: do need to connect to slice valueChanged() etc.
138 // to update the slice.
140 // This is handled through calculatedDataChanged signal.
139 connect(slice, SIGNAL(calculatedDataChanged()), this, SLOT(handleSliceChanged()));
140 connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged()));
141 connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged()));
141 connect(slice, SIGNAL(appearanceChanged()), this, SLOT(handleSliceChanged()));
142 connect(slice, SIGNAL(labelVisibleChanged()), this, SLOT(handleSliceChanged()));
143 connect(slice, SIGNAL(explodedChanged()), this, SLOT(handleSliceChanged()));
144 connect(slice, SIGNAL(penChanged()), this, SLOT(handleSliceChanged()));
145 connect(slice, SIGNAL(brushChanged()), this, SLOT(handleSliceChanged()));
146 connect(slice, SIGNAL(labelPenChanged()), this, SLOT(handleSliceChanged()));
147 connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged()));
148 connect(slice, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged()));
149 connect(slice, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged()));
150
142 connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked()));
151 connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked()));
143 connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool)));
152 connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool)));
144
153
@@ -183,7 +192,7 void PieChartItem::handleSliceChanged()
183
192
184 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
193 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
185 {
194 {
186 PieSliceData &sliceData = PieSliceData::fromSlice(slice);
195 PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data;
187 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
196 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
188 sliceData.m_radius = m_pieRadius;
197 sliceData.m_radius = m_pieRadius;
189 return sliceData;
198 return sliceData;
@@ -104,24 +104,6 public:
104 return false;
104 return false;
105 }
105 }
106
106
107 static void emitAppearanceChanged(QPieSlice *slice)
108 {
109 Q_ASSERT(slice);
110 emit slice->appearanceChanged();
111 }
112
113 static void emitCalculatedDataChanged(QPieSlice *slice)
114 {
115 Q_ASSERT(slice);
116 emit slice->calculatedDataChanged();
117 }
118
119 static PieSliceData &fromSlice(QPieSlice *slice)
120 {
121 Q_ASSERT(slice);
122 return *slice->d;
123 }
124
125 qreal m_value;
107 qreal m_value;
126
108
127 Themed<QPen> m_slicePen;
109 Themed<QPen> m_slicePen;
@@ -143,7 +143,7 void PieSliceItem::updateGeometry()
143 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
143 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
144 {
144 {
145 if (slice->isExploded()) {
145 if (slice->isExploded()) {
146 qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2);
146 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
147 qreal len = radius * slice->explodeDistanceFactor();
147 qreal len = radius * slice->explodeDistanceFactor();
148 qreal dx = qSin(centerAngle*(PI/180)) * len;
148 qreal dx = qSin(centerAngle*(PI/180)) * len;
149 qreal dy = -qCos(centerAngle*(PI/180)) * len;
149 qreal dy = -qCos(centerAngle*(PI/180)) * len;
@@ -21,6 +21,7
21 #include "qpieseries.h"
21 #include "qpieseries.h"
22 #include "qpieseries_p.h"
22 #include "qpieseries_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieslice_p.h"
24 #include "pieslicedata_p.h"
25 #include "pieslicedata_p.h"
25 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
27 #include "charttheme_p.h"
@@ -173,7 +174,7 bool QPieSeries::append(QList<QPieSlice*> slices)
173 d->updateDerivativeData();
174 d->updateDerivativeData();
174
175
175 foreach (QPieSlice* s, slices) {
176 foreach (QPieSlice* s, slices) {
176 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceChanged()));
177 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
177 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
178 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
178 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
179 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
179 }
180 }
@@ -238,7 +239,7 bool QPieSeries::insert(int index, QPieSlice* slice)
238
239
239 d->updateDerivativeData();
240 d->updateDerivativeData();
240
241
241 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceChanged()));
242 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
242 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
243 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
243 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
244 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
244
245
@@ -531,30 +532,23 void QPieSeriesPrivate::updateDerivativeData()
531 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
532 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
532 QVector<QPieSlice*> changed;
533 QVector<QPieSlice*> changed;
533 foreach (QPieSlice* s, m_slices) {
534 foreach (QPieSlice* s, m_slices) {
534
535 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
535 PieSliceData data = PieSliceData::fromSlice(s);
536 d->setPercentage(s->value() / m_sum);
536 data.m_percentage = s->value() / m_sum;
537 d->setStartAngle(sliceAngle);
537 data.m_angleSpan = pieSpan * data.m_percentage;
538 d->setAngleSpan(pieSpan * s->percentage());
538 data.m_startAngle = sliceAngle;
539 sliceAngle += s->angleSpan();
539 sliceAngle += data.m_angleSpan;
540
541 if (PieSliceData::fromSlice(s) != data) {
542 PieSliceData::fromSlice(s) = data;
543 changed << s;
544 }
545 }
540 }
546
541
547 // emit signals
542
548 foreach (QPieSlice* s, changed)
543 emit calculatedDataChanged();
549 PieSliceData::emitCalculatedDataChanged(s);
550 }
544 }
551
545
552 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
546 QPieSeriesPrivate* QPieSeriesPrivate::fromSeries(QPieSeries *series)
553 {
547 {
554 return series.d_func();
548 return series->d_func();
555 }
549 }
556
550
557 void QPieSeriesPrivate::sliceChanged()
551 void QPieSeriesPrivate::sliceValueChanged()
558 {
552 {
559 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
553 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
560 updateDerivativeData();
554 updateDerivativeData();
@@ -41,14 +41,17 public:
41
41
42 void updateDerivativeData();
42 void updateDerivativeData();
43
43
44 static QPieSeriesPrivate* seriesData(QPieSeries &series);
44 static QPieSeriesPrivate* fromSeries(QPieSeries *series);
45
46 signals:
47 void calculatedDataChanged();
45
48
46 public Q_SLOTS:
49 public Q_SLOTS:
47 void sliceChanged();
50 void sliceValueChanged();
48 void sliceClicked();
51 void sliceClicked();
49 void sliceHovered(bool state);
52 void sliceHovered(bool state);
50
53
51 public:
54 private:
52 QList<QPieSlice*> m_slices;
55 QList<QPieSlice*> m_slices;
53 qreal m_pieRelativeHorPos;
56 qreal m_pieRelativeHorPos;
54 qreal m_pieRelativeVerPos;
57 qreal m_pieRelativeVerPos;
@@ -19,7 +19,7
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qpieslice.h"
21 #include "qpieslice.h"
22 #include "pieslicedata_p.h"
22 #include "qpieslice_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
@@ -59,7 +59,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
59 */
59 */
60 QPieSlice::QPieSlice(QObject *parent)
60 QPieSlice::QPieSlice(QObject *parent)
61 :QObject(parent),
61 :QObject(parent),
62 d(new PieSliceData())
62 d_ptr(new QPieSlicePrivate(this))
63 {
63 {
64
64
65 }
65 }
@@ -70,10 +70,10 QPieSlice::QPieSlice(QObject *parent)
70 */
70 */
71 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
71 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
72 :QObject(parent),
72 :QObject(parent),
73 d(new PieSliceData())
73 d_ptr(new QPieSlicePrivate(this))
74 {
74 {
75 d->m_value = value;
75 d_ptr->m_data.m_value = value;
76 d->m_labelText = label;
76 d_ptr->m_data.m_labelText = label;
77 }
77 }
78
78
79 /*!
79 /*!
@@ -82,7 +82,7 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
82 */
82 */
83 QPieSlice::~QPieSlice()
83 QPieSlice::~QPieSlice()
84 {
84 {
85 delete d;
85
86 }
86 }
87
87
88 /*!
88 /*!
@@ -92,7 +92,7 QPieSlice::~QPieSlice()
92 */
92 */
93 qreal QPieSlice::value() const
93 qreal QPieSlice::value() const
94 {
94 {
95 return d->m_value;
95 return d_ptr->m_data.m_value;
96 }
96 }
97
97
98 /*!
98 /*!
@@ -101,7 +101,7 qreal QPieSlice::value() const
101 */
101 */
102 QString QPieSlice::label() const
102 QString QPieSlice::label() const
103 {
103 {
104 return d->m_labelText;
104 return d_ptr->m_data.m_labelText;
105 }
105 }
106
106
107 /*!
107 /*!
@@ -110,7 +110,7 QString QPieSlice::label() const
110 */
110 */
111 bool QPieSlice::isLabelVisible() const
111 bool QPieSlice::isLabelVisible() const
112 {
112 {
113 return d->m_isLabelVisible;
113 return d_ptr->m_data.m_isLabelVisible;
114 }
114 }
115
115
116 /*!
116 /*!
@@ -119,7 +119,7 bool QPieSlice::isLabelVisible() const
119 */
119 */
120 bool QPieSlice::isExploded() const
120 bool QPieSlice::isExploded() const
121 {
121 {
122 return d->m_isExploded;
122 return d_ptr->m_data.m_isExploded;
123 }
123 }
124
124
125 /*!
125 /*!
@@ -135,7 +135,7 bool QPieSlice::isExploded() const
135 */
135 */
136 qreal QPieSlice::explodeDistanceFactor() const
136 qreal QPieSlice::explodeDistanceFactor() const
137 {
137 {
138 return d->m_explodeDistanceFactor;
138 return d_ptr->m_data.m_explodeDistanceFactor;
139 }
139 }
140
140
141 /*!
141 /*!
@@ -148,7 +148,7 qreal QPieSlice::explodeDistanceFactor() const
148 */
148 */
149 qreal QPieSlice::percentage() const
149 qreal QPieSlice::percentage() const
150 {
150 {
151 return d->m_percentage;
151 return d_ptr->m_data.m_percentage;
152 }
152 }
153
153
154 /*!
154 /*!
@@ -160,7 +160,7 qreal QPieSlice::percentage() const
160 */
160 */
161 qreal QPieSlice::startAngle() const
161 qreal QPieSlice::startAngle() const
162 {
162 {
163 return d->m_startAngle;
163 return d_ptr->m_data.m_startAngle;
164 }
164 }
165
165
166 /*!
166 /*!
@@ -170,9 +170,9 qreal QPieSlice::startAngle() const
170
170
171 Updated internally after the slice is added to the series.
171 Updated internally after the slice is added to the series.
172 */
172 */
173 qreal QPieSlice::endAngle() const
173 qreal QPieSlice::angleSpan() const
174 {
174 {
175 return d->m_startAngle + d->m_angleSpan;
175 return d_ptr->m_data.m_angleSpan;
176 }
176 }
177
177
178 /*!
178 /*!
@@ -181,7 +181,7 qreal QPieSlice::endAngle() const
181 */
181 */
182 QPen QPieSlice::pen() const
182 QPen QPieSlice::pen() const
183 {
183 {
184 return d->m_slicePen;
184 return d_ptr->m_data.m_slicePen;
185 }
185 }
186
186
187 /*!
187 /*!
@@ -190,7 +190,7 QPen QPieSlice::pen() const
190 */
190 */
191 QBrush QPieSlice::brush() const
191 QBrush QPieSlice::brush() const
192 {
192 {
193 return d->m_sliceBrush;
193 return d_ptr->m_data.m_sliceBrush;
194 }
194 }
195
195
196 /*!
196 /*!
@@ -199,7 +199,7 QBrush QPieSlice::brush() const
199 */
199 */
200 QPen QPieSlice::labelPen() const
200 QPen QPieSlice::labelPen() const
201 {
201 {
202 return d->m_labelPen;
202 return d_ptr->m_data.m_labelPen;
203 }
203 }
204
204
205 /*!
205 /*!
@@ -208,7 +208,7 QPen QPieSlice::labelPen() const
208 */
208 */
209 QFont QPieSlice::labelFont() const
209 QFont QPieSlice::labelFont() const
210 {
210 {
211 return d->m_labelFont;
211 return d_ptr->m_data.m_labelFont;
212 }
212 }
213
213
214 /*!
214 /*!
@@ -224,7 +224,7 QFont QPieSlice::labelFont() const
224 */
224 */
225 qreal QPieSlice::labelArmLengthFactor() const
225 qreal QPieSlice::labelArmLengthFactor() const
226 {
226 {
227 return d->m_labelArmLengthFactor;
227 return d_ptr->m_data.m_labelArmLengthFactor;
228 }
228 }
229
229
230 /*!
230 /*!
@@ -283,8 +283,8 qreal QPieSlice::labelArmLengthFactor() const
283 */
283 */
284 void QPieSlice::setValue(qreal value)
284 void QPieSlice::setValue(qreal value)
285 {
285 {
286 if (!qFuzzyIsNull(d->m_value - value)) {
286 if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) {
287 d->m_value = value;
287 d_ptr->m_data.m_value = value;
288 emit valueChanged();
288 emit valueChanged();
289 }
289 }
290 }
290 }
@@ -295,8 +295,8 void QPieSlice::setValue(qreal value)
295 */
295 */
296 void QPieSlice::setLabel(QString label)
296 void QPieSlice::setLabel(QString label)
297 {
297 {
298 if (d->m_labelText != label) {
298 if (d_ptr->m_data.m_labelText != label) {
299 d->m_labelText = label;
299 d_ptr->m_data.m_labelText = label;
300 emit labelChanged();
300 emit labelChanged();
301 }
301 }
302 }
302 }
@@ -307,9 +307,9 void QPieSlice::setLabel(QString label)
307 */
307 */
308 void QPieSlice::setLabelVisible(bool visible)
308 void QPieSlice::setLabelVisible(bool visible)
309 {
309 {
310 if (d->m_isLabelVisible != visible) {
310 if (d_ptr->m_data.m_isLabelVisible != visible) {
311 d->m_isLabelVisible = visible;
311 d_ptr->m_data.m_isLabelVisible = visible;
312 emit appearanceChanged();
312 emit labelVisibleChanged();
313 }
313 }
314 }
314 }
315
315
@@ -322,9 +322,9 void QPieSlice::setLabelVisible(bool visible)
322 */
322 */
323 void QPieSlice::setExploded(bool exploded)
323 void QPieSlice::setExploded(bool exploded)
324 {
324 {
325 if (d->m_isExploded != exploded) {
325 if (d_ptr->m_data.m_isExploded != exploded) {
326 d->m_isExploded = exploded;
326 d_ptr->m_data.m_isExploded = exploded;
327 emit appearanceChanged();
327 emit explodedChanged();
328 }
328 }
329 }
329 }
330
330
@@ -341,9 +341,9 void QPieSlice::setExploded(bool exploded)
341 */
341 */
342 void QPieSlice::setExplodeDistanceFactor(qreal factor)
342 void QPieSlice::setExplodeDistanceFactor(qreal factor)
343 {
343 {
344 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
344 if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) {
345 d->m_explodeDistanceFactor = factor;
345 d_ptr->m_data.m_explodeDistanceFactor = factor;
346 emit appearanceChanged();
346 emit explodeDistanceFactorChanged();
347 }
347 }
348 }
348 }
349
349
@@ -356,11 +356,7 void QPieSlice::setExplodeDistanceFactor(qreal factor)
356 */
356 */
357 void QPieSlice::setPen(const QPen &pen)
357 void QPieSlice::setPen(const QPen &pen)
358 {
358 {
359 if (d->m_slicePen != pen) {
359 d_ptr->setPen(pen, false);
360 d->m_slicePen = pen;
361 d->m_slicePen.setThemed(false);
362 emit appearanceChanged();
363 }
364 }
360 }
365
361
366 /*!
362 /*!
@@ -372,11 +368,7 void QPieSlice::setPen(const QPen &pen)
372 */
368 */
373 void QPieSlice::setBrush(const QBrush &brush)
369 void QPieSlice::setBrush(const QBrush &brush)
374 {
370 {
375 if (d->m_sliceBrush != brush) {
371 d_ptr->setBrush(brush, false);
376 d->m_sliceBrush = brush;
377 d->m_sliceBrush.setThemed(false);
378 emit appearanceChanged();
379 }
380 }
372 }
381
373
382 /*!
374 /*!
@@ -388,11 +380,7 void QPieSlice::setBrush(const QBrush &brush)
388 */
380 */
389 void QPieSlice::setLabelPen(const QPen &pen)
381 void QPieSlice::setLabelPen(const QPen &pen)
390 {
382 {
391 if (d->m_labelPen != pen) {
383 d_ptr->setLabelPen(pen, false);
392 d->m_labelPen = pen;
393 d->m_labelPen.setThemed(false);
394 emit appearanceChanged();
395 }
396 }
384 }
397
385
398 /*!
386 /*!
@@ -404,11 +392,7 void QPieSlice::setLabelPen(const QPen &pen)
404 */
392 */
405 void QPieSlice::setLabelFont(const QFont &font)
393 void QPieSlice::setLabelFont(const QFont &font)
406 {
394 {
407 if (d->m_labelFont != font) {
395 d_ptr->setLabelFont(font, false);
408 d->m_labelFont = font;
409 d->m_labelFont.setThemed(false);
410 emit appearanceChanged();
411 }
412 }
396 }
413
397
414 /*!
398 /*!
@@ -424,9 +408,86 void QPieSlice::setLabelFont(const QFont &font)
424 */
408 */
425 void QPieSlice::setLabelArmLengthFactor(qreal factor)
409 void QPieSlice::setLabelArmLengthFactor(qreal factor)
426 {
410 {
427 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
411 if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) {
428 d->m_labelArmLengthFactor = factor;
412 d_ptr->m_data.m_labelArmLengthFactor = factor;
429 emit appearanceChanged();
413 emit labelArmLengthFactorChanged();
414 }
415 }
416
417 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
418 :QObject(parent),
419 q_ptr(parent)
420 {
421
422 }
423
424 QPieSlicePrivate::~QPieSlicePrivate()
425 {
426
427 }
428
429 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice)
430 {
431 return slice->d_func();
432 }
433
434 void QPieSlicePrivate::setPen(const QPen &pen, bool themed)
435 {
436 if (m_data.m_slicePen != pen) {
437 m_data.m_slicePen = pen;
438 m_data.m_slicePen.setThemed(themed);
439 emit q_ptr->penChanged();
440 }
441 }
442
443 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed)
444 {
445 if (m_data.m_sliceBrush != brush) {
446 m_data.m_sliceBrush = brush;
447 m_data.m_sliceBrush.setThemed(themed);
448 emit q_ptr->brushChanged();
449 }
450 }
451
452 void QPieSlicePrivate::setLabelPen(const QPen &pen, bool themed)
453 {
454 if (m_data.m_labelPen != pen) {
455 m_data.m_labelPen = pen;
456 m_data.m_labelPen.setThemed(themed);
457 emit q_ptr->labelPenChanged();
458 }
459 }
460
461 void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed)
462 {
463 if (m_data.m_labelFont != font) {
464 m_data.m_labelFont = font;
465 m_data.m_labelFont.setThemed(themed);
466 emit q_ptr->labelFontChanged();
467 }
468 }
469
470 void QPieSlicePrivate::setPercentage(qreal percentage)
471 {
472 if (!qFuzzyIsNull(m_data.m_percentage - percentage)) {
473 m_data.m_percentage = percentage;
474 emit q_ptr->percentageChanged();
475 }
476 }
477
478 void QPieSlicePrivate::setStartAngle(qreal angle)
479 {
480 if (!qFuzzyIsNull(m_data.m_startAngle - angle)) {
481 m_data.m_startAngle = angle;
482 emit q_ptr->startAngleChanged();
483 }
484 }
485
486 void QPieSlicePrivate::setAngleSpan(qreal span)
487 {
488 if (!qFuzzyIsNull(m_data.m_angleSpan - span)) {
489 m_data.m_angleSpan = span;
490 emit q_ptr->angleSpanChanged();
430 }
491 }
431 }
492 }
432
493
@@ -434,3 +495,4 QTCOMMERCIALCHART_END_NAMESPACE
434
495
435 QTCOMMERCIALCHART_USE_NAMESPACE
496 QTCOMMERCIALCHART_USE_NAMESPACE
436 #include "moc_qpieslice.cpp"
497 #include "moc_qpieslice.cpp"
498 #include "moc_qpieslice_p.cpp"
@@ -28,20 +28,24
28 #include <QFont>
28 #include <QFont>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 class PieSliceData;
31 class QPieSlicePrivate;
32
32
33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
38 Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible)
38 Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible NOTIFY labelVisibleChanged)
39 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded)
39 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded NOTIFY explodedChanged)
40 Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
41 Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
42 Q_PROPERTY(QPen labelPen READ labelPen WRITE setLabelPen NOTIFY labelPenChanged)
43 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged)
40 Q_PROPERTY(qreal LabelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor)
44 Q_PROPERTY(qreal LabelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor)
41 Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor)
45 Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor)
42 Q_PROPERTY(qreal percentage READ percentage NOTIFY calculatedDataChanged)
46 Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged)
43 Q_PROPERTY(qreal startAngle READ startAngle NOTIFY calculatedDataChanged)
47 Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged)
44 Q_PROPERTY(qreal endAngle READ endAngle NOTIFY calculatedDataChanged)
48 Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged)
45
49
46 public:
50 public:
47 explicit QPieSlice(QObject *parent = 0);
51 explicit QPieSlice(QObject *parent = 0);
@@ -54,45 +58,54 public:
54 void setValue(qreal value);
58 void setValue(qreal value);
55 qreal value() const;
59 qreal value() const;
56
60
61 void setLabelVisible(bool visible = true);
62 bool isLabelVisible() const;
63
64 void setExploded(bool exploded = true);
65 bool isExploded() const;
66
57 void setPen(const QPen &pen);
67 void setPen(const QPen &pen);
58 QPen pen() const;
68 QPen pen() const;
59
69
60 void setBrush(const QBrush &brush);
70 void setBrush(const QBrush &brush);
61 QBrush brush() const;
71 QBrush brush() const;
62
72
63 void setLabelVisible(bool visible = true);
64 bool isLabelVisible() const;
65
66 void setLabelArmLengthFactor(qreal factor);
67 qreal labelArmLengthFactor() const;
68
69 void setLabelPen(const QPen &pen);
73 void setLabelPen(const QPen &pen);
70 QPen labelPen() const;
74 QPen labelPen() const;
71
75
72 void setLabelFont(const QFont &font);
76 void setLabelFont(const QFont &font);
73 QFont labelFont() const;
77 QFont labelFont() const;
74
78
75 void setExploded(bool exploded = true);
79 void setLabelArmLengthFactor(qreal factor);
76 bool isExploded() const;
80 qreal labelArmLengthFactor() const;
77
81
78 void setExplodeDistanceFactor(qreal factor);
82 void setExplodeDistanceFactor(qreal factor);
79 qreal explodeDistanceFactor() const;
83 qreal explodeDistanceFactor() const;
80
84
81 qreal percentage() const;
85 qreal percentage() const;
82 qreal startAngle() const;
86 qreal startAngle() const;
83 qreal endAngle() const;
87 qreal angleSpan() const;
84
88
85 Q_SIGNALS:
89 Q_SIGNALS:
86 void labelChanged();
90 void labelChanged();
87 void valueChanged();
91 void valueChanged();
88 void appearanceChanged();
92 void labelVisibleChanged();
89 void calculatedDataChanged();
93 void explodedChanged();
94 void penChanged();
95 void brushChanged();
96 void labelPenChanged();
97 void labelFontChanged();
98 void labelArmLengthFactorChanged();
99 void explodeDistanceFactorChanged();
100 void percentageChanged();
101 void startAngleChanged();
102 void angleSpanChanged();
90 void clicked();
103 void clicked();
91 void hovered(bool state);
104 void hovered(bool state);
92
105
93 private:
106 private:
94 friend class PieSliceData;
107 QPieSlicePrivate * const d_ptr;
95 PieSliceData * const d;
108 Q_DECLARE_PRIVATE(QPieSlice)
96 Q_DISABLE_COPY(QPieSlice)
109 Q_DISABLE_COPY(QPieSlice)
97 };
110 };
98
111
@@ -260,14 +260,18 void tst_qpieseries::calculatedValues()
260 QPieSeries s;
260 QPieSeries s;
261
261
262 QPieSlice *slice1 = new QPieSlice("slice 1", 1);
262 QPieSlice *slice1 = new QPieSlice("slice 1", 1);
263 QSignalSpy calculatedDataSpy(slice1, SIGNAL(calculatedDataChanged()));
263 QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged()));
264 QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged()));
265 QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged()));
264
266
265 // add a slice
267 // add a slice
266 s.append(slice1);
268 s.append(slice1);
267 verifyCalculatedData(s, &ok);
269 verifyCalculatedData(s, &ok);
268 if (!ok)
270 if (!ok)
269 return;
271 return;
270 QCOMPARE(calculatedDataSpy.count(), 1);
272 QCOMPARE(percentageSpy.count(), 1);
273 QCOMPARE(startAngleSpy.count(), 0);
274 QCOMPARE(angleSpanSpy.count(), 1);
271
275
272 // add some more slices
276 // add some more slices
273 QList<QPieSlice *> list;
277 QList<QPieSlice *> list;
@@ -277,21 +281,27 void tst_qpieseries::calculatedValues()
277 verifyCalculatedData(s, &ok);
281 verifyCalculatedData(s, &ok);
278 if (!ok)
282 if (!ok)
279 return;
283 return;
280 QCOMPARE(calculatedDataSpy.count(), 2);
284 QCOMPARE(percentageSpy.count(), 2);
285 QCOMPARE(startAngleSpy.count(), 0);
286 QCOMPARE(angleSpanSpy.count(), 2);
281
287
282 // remove a slice
288 // remove a slice
283 s.remove(list.first()); // remove slice 2
289 s.remove(list.first()); // remove slice 2
284 verifyCalculatedData(s, &ok);
290 verifyCalculatedData(s, &ok);
285 if (!ok)
291 if (!ok)
286 return;
292 return;
287 QCOMPARE(calculatedDataSpy.count(), 3);
293 QCOMPARE(percentageSpy.count(), 3);
294 QCOMPARE(startAngleSpy.count(), 0);
295 QCOMPARE(angleSpanSpy.count(), 3);
288
296
289 // insert a slice
297 // insert a slice
290 s.insert(0, new QPieSlice("Slice 4", 4));
298 s.insert(0, new QPieSlice("Slice 4", 4));
291 verifyCalculatedData(s, &ok);
299 verifyCalculatedData(s, &ok);
292 if (!ok)
300 if (!ok)
293 return;
301 return;
294 QCOMPARE(calculatedDataSpy.count(), 4);
302 QCOMPARE(percentageSpy.count(), 4);
303 QCOMPARE(startAngleSpy.count(), 1);
304 QCOMPARE(angleSpanSpy.count(), 4);
295
305
296 // modify pie angles
306 // modify pie angles
297 s.setPieStartAngle(-90);
307 s.setPieStartAngle(-90);
@@ -299,11 +309,16 void tst_qpieseries::calculatedValues()
299 verifyCalculatedData(s, &ok);
309 verifyCalculatedData(s, &ok);
300 if (!ok)
310 if (!ok)
301 return;
311 return;
302 QCOMPARE(calculatedDataSpy.count(), 6);
312 QCOMPARE(percentageSpy.count(), 4);
313 QCOMPARE(startAngleSpy.count(), 3);
314 QCOMPARE(angleSpanSpy.count(), 6);
303
315
304 // clear all
316 // clear all
305 s.clear();
317 s.clear();
306 verifyCalculatedData(s, &ok);
318 verifyCalculatedData(s, &ok);
319 QCOMPARE(percentageSpy.count(), 4);
320 QCOMPARE(startAngleSpy.count(), 3);
321 QCOMPARE(angleSpanSpy.count(), 6);
307 }
322 }
308
323
309 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
324 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
@@ -321,13 +336,13 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
321 qreal ratio = slice->value() / sum;
336 qreal ratio = slice->value() / sum;
322 qreal sliceSpan = pieAngleSpan * ratio;
337 qreal sliceSpan = pieAngleSpan * ratio;
323 QCOMPARE(slice->startAngle(), startAngle);
338 QCOMPARE(slice->startAngle(), startAngle);
324 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
339 QCOMPARE(slice->angleSpan(), sliceSpan);
325 QCOMPARE(slice->percentage(), ratio);
340 QCOMPARE(slice->percentage(), ratio);
326 startAngle += sliceSpan;
341 startAngle += sliceSpan;
327 }
342 }
328
343
329 if (!series.isEmpty())
344 if (!series.isEmpty())
330 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
345 QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle());
331
346
332 *ok = true;
347 *ok = true;
333 }
348 }
@@ -85,7 +85,7 void tst_qpieslice::construction()
85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
86 QCOMPARE(slice1.percentage(), 0.0);
86 QCOMPARE(slice1.percentage(), 0.0);
87 QCOMPARE(slice1.startAngle(), 0.0);
87 QCOMPARE(slice1.startAngle(), 0.0);
88 QCOMPARE(slice1.endAngle(), 0.0);
88 QCOMPARE(slice1.angleSpan(), 0.0);
89
89
90 // value and label params
90 // value and label params
91 QPieSlice slice2("foobar", 1.0);
91 QPieSlice slice2("foobar", 1.0);
@@ -101,7 +101,7 void tst_qpieslice::construction()
101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
102 QCOMPARE(slice2.percentage(), 0.0);
102 QCOMPARE(slice2.percentage(), 0.0);
103 QCOMPARE(slice2.startAngle(), 0.0);
103 QCOMPARE(slice2.startAngle(), 0.0);
104 QCOMPARE(slice2.endAngle(), 0.0);
104 QCOMPARE(slice2.angleSpan(), 0.0);
105 }
105 }
106
106
107 void tst_qpieslice::changedSignals()
107 void tst_qpieslice::changedSignals()
@@ -110,8 +110,15 void tst_qpieslice::changedSignals()
110
110
111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
113 QSignalSpy appearanceSpy(&slice, SIGNAL(appearanceChanged()));
113 QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged()));
114 // calculatedDataChanged signal is tested at tst_qpieseries::calculatedValues()
114 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
115 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
116 QSignalSpy labelPenSpy(&slice, SIGNAL(labelPenChanged()));
117 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
118 QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged()));
119 QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged()));
120
121 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
115
122
116 // set everything twice to see we do not get unnecessary signals
123 // set everything twice to see we do not get unnecessary signals
117 slice.setValue(1);
124 slice.setValue(1);
@@ -137,7 +144,13 void tst_qpieslice::changedSignals()
137
144
138 TRY_COMPARE(valueSpy.count(), 1);
145 TRY_COMPARE(valueSpy.count(), 1);
139 TRY_COMPARE(labelSpy.count(), 1);
146 TRY_COMPARE(labelSpy.count(), 1);
140 TRY_COMPARE(appearanceSpy.count(), 8);
147 TRY_COMPARE(explodedSpy.count(), 1);
148 TRY_COMPARE(penSpy.count(), 1);
149 TRY_COMPARE(brushSpy.count(), 1);
150 TRY_COMPARE(labelPenSpy.count(), 1);
151 TRY_COMPARE(labelFontSpy.count(), 1);
152 TRY_COMPARE(labelArmLengthFactorSpy.count(), 1);
153 TRY_COMPARE(explodeDistanceFactorSpy.count(), 1);
141 }
154 }
142
155
143 void tst_qpieslice::customize()
156 void tst_qpieslice::customize()
General Comments 0
You need to be logged in to leave comments. Login now