##// END OF EJS Templates
Hide slice label by default
Jani Honkonen -
r294:bf1f84426ed8
parent child
Show More
@@ -1,20 +1,22
1 1 #include "customslice.h"
2 2
3 3 CustomSlice::CustomSlice(qreal value, QObject* parent)
4 4 :QPieSlice(parent)
5 5 {
6 6 setValue(value);
7 setLabelVisible(true);
8 setExploded(true);
7 9 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
8 10 connect(this, SIGNAL(hoverEnter()), this, SLOT(toggleExploded()));
9 11 connect(this, SIGNAL(hoverLeave()), this, SLOT(toggleExploded()));
10 12 }
11 13
12 14 void CustomSlice::updateLabel()
13 15 {
14 setLabel(QString::number(this->percentage()));
16 setLabel(QString::number(this->percentage()*100) + "%");
15 17 }
16 18
17 19 void CustomSlice::toggleExploded()
18 20 {
19 21 setExploded(!isExploded());
20 22 }
@@ -1,306 +1,308
1 1 #include "qpieseries.h"
2 2 #include "qpieslice.h"
3 3 #include "piepresenter.h"
4 4 #include "pieslice.h"
5 5 #include <QFontMetrics>
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice)
11 11 {
12 12 if (!m_added.contains(slice))
13 13 m_added << slice;
14 14 }
15 15
16 16 void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices)
17 17 {
18 18 foreach (QPieSlice* s, slices)
19 19 appendAdded(s);
20 20 }
21 21
22 22 void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice)
23 23 {
24 24 if (!m_changed.contains(slice))
25 25 m_changed << slice;
26 26 }
27 27
28 28 void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice)
29 29 {
30 30 if (!m_removed.contains(slice))
31 31 m_removed << slice;
32 32 }
33 33
34 34 QList<QPieSlice*> QPieSeries::ChangeSet::added() const
35 35 {
36 36 return m_added;
37 37 }
38 38
39 39 QList<QPieSlice*> QPieSeries::ChangeSet::changed() const
40 40 {
41 41 return m_changed;
42 42 }
43 43
44 44 QList<QPieSlice*> QPieSeries::ChangeSet::removed() const
45 45 {
46 46 return m_removed;
47 47 }
48 48
49 49 bool QPieSeries::ChangeSet::isEmpty() const
50 50 {
51 51 if (m_added.count() || m_changed.count() || m_removed.count())
52 52 return false;
53 53 return true;
54 54 }
55 55
56 56
57 57 QPieSeries::QPieSeries(QObject *parent) :
58 58 QChartSeries(parent),
59 59 m_sizeFactor(1.0),
60 60 m_position(PiePositionMaximized),
61 61 m_pieStartAngle(0),
62 62 m_pieSpan(360)
63 63 {
64 64
65 65 }
66 66
67 67 QPieSeries::~QPieSeries()
68 68 {
69 69
70 70 }
71 71
72 72 bool QPieSeries::setData(QList<qreal> data)
73 73 {
74 74 // TODO: remove this function
75 75 QList<QPieSlice*> slices;
76 76 foreach (qreal value, data)
77 77 slices << new QPieSlice(value, QString::number(value));
78 78 set(slices);
79 79 return true;
80 80 }
81 81
82 82 void QPieSeries::set(QList<QPieSlice*> slices)
83 83 {
84 84 clear();
85 85 add(slices);
86 86 }
87 87
88 88 void QPieSeries::add(QList<QPieSlice*> slices)
89 89 {
90 90 ChangeSet changeSet;
91 91 foreach (QPieSlice* s, slices) {
92 92 s->setParent(this);
93 93 m_slices << s;
94 94 changeSet.appendAdded(s);
95 95 }
96 96
97 97 updateDerivativeData();
98 98
99 99 foreach (QPieSlice* s, slices) {
100 100 connect(s, SIGNAL(changed()), this, SLOT(sliceChanged()));
101 101 connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked()));
102 102 connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter()));
103 103 connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave()));
104 104 }
105 105
106 106 emit changed(changeSet);
107 107 }
108 108
109 109 void QPieSeries::add(QPieSlice* slice)
110 110 {
111 111 add(QList<QPieSlice*>() << slice);
112 112 }
113 113
114 114 QPieSlice* QPieSeries::add(qreal value, QString name)
115 115 {
116 116 QPieSlice* slice = new QPieSlice(value, name);
117 117 add(slice);
118 118 return slice;
119 119 }
120 120
121 121 void QPieSeries::remove(QPieSlice* slice)
122 122 {
123 123 if (!m_slices.removeOne(slice)) {
124 124 Q_ASSERT(0); // TODO: how should this be reported?
125 125 return;
126 126 }
127 127
128 128 ChangeSet changeSet;
129 129 changeSet.appendRemoved(slice);
130 130 emit changed(changeSet);
131 131
132 132 delete slice;
133 133 slice = NULL;
134 134
135 135 updateDerivativeData();
136 136 }
137 137
138 138 void QPieSeries::clear()
139 139 {
140 140 if (m_slices.count() == 0)
141 141 return;
142 142
143 143 ChangeSet changeSet;
144 144 foreach (QPieSlice* s, m_slices) {
145 145 changeSet.appendRemoved(s);
146 146 m_slices.removeOne(s);
147 147 delete s;
148 148 }
149 149 emit changed(changeSet);
150 150 updateDerivativeData();
151 151 }
152 152
153 153 void QPieSeries::setSizeFactor(qreal factor)
154 154 {
155 155 if (factor < 0.0)
156 156 return;
157 157
158 158 if (m_sizeFactor != factor) {
159 159 m_sizeFactor = factor;
160 160 emit sizeFactorChanged();
161 161 }
162 162 }
163 163
164 164 void QPieSeries::setPosition(PiePosition position)
165 165 {
166 166 if (m_position != position) {
167 167 m_position = position;
168 168 emit positionChanged();
169 169 }
170 170 }
171 171
172 172 void QPieSeries::setSpan(qreal startAngle, qreal span)
173 173 {
174 174 if (startAngle >= 0 && startAngle < 360 &&
175 175 span > 0 && span <= 360) {
176 176 m_pieStartAngle = startAngle;
177 177 m_pieSpan = span;
178 178 updateDerivativeData();
179 179 }
180 180 }
181 181
182 182 void QPieSeries::setLabelsVisible(bool visible)
183 183 {
184 184 foreach (QPieSlice* s, m_slices)
185 185 s->setLabelVisible(visible);
186 186 }
187 187
188 188 void QPieSeries::enableClickExplodes(bool enable)
189 189 {
190 190 if (enable)
191 191 connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*)));
192 192 else
193 193 disconnect(this, SLOT(toggleExploded(QPieSlice*)));
194 194 }
195 195
196 196 void QPieSeries::enableHoverHighlight(bool enable)
197 197 {
198 198 if (enable) {
199 199 connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*)));
200 200 connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*)));
201 201 } else {
202 202 disconnect(this, SLOT(hoverEnter(QPieSlice*)));
203 203 disconnect(this, SLOT(hoverLeave(QPieSlice*)));
204 204 }
205 205 }
206 206
207 207 void QPieSeries::sliceChanged()
208 208 {
209 209 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
210 210 Q_ASSERT(m_slices.contains(slice));
211 211
212 212 ChangeSet changeSet;
213 213 changeSet.appendChanged(slice);
214 214 emit changed(changeSet);
215 215
216 216 updateDerivativeData();
217 217 }
218 218
219 219 void QPieSeries::sliceClicked()
220 220 {
221 221 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
222 222 Q_ASSERT(m_slices.contains(slice));
223 223 emit clicked(slice);
224 224 }
225 225
226 226 void QPieSeries::sliceHoverEnter()
227 227 {
228 228 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
229 229 Q_ASSERT(m_slices.contains(slice));
230 230 emit hoverEnter(slice);
231 231 }
232 232
233 233 void QPieSeries::sliceHoverLeave()
234 234 {
235 235 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
236 236 Q_ASSERT(m_slices.contains(slice));
237 237 emit hoverLeave(slice);
238 238 }
239 239
240 240 void QPieSeries::toggleExploded(QPieSlice* slice)
241 241 {
242 242 Q_ASSERT(slice);
243 243 slice->setExploded(!slice->isExploded());
244 244 }
245 245
246 246 void QPieSeries::highlightOn(QPieSlice* slice)
247 247 {
248 248 Q_ASSERT(slice);
249 249 QColor c = slice->brush().color().lighter();
250 250 slice->setBrush(c);
251 slice->setLabelVisible(true);
251 252 }
252 253
253 254 void QPieSeries::highlightOff(QPieSlice* slice)
254 255 {
255 256 Q_ASSERT(slice);
256 257 QColor c = slice->brush().color().darker(150);
257 258 slice->setBrush(c);
259 slice->setLabelVisible(false);
258 260 }
259 261
260 262 void QPieSeries::updateDerivativeData()
261 263 {
262 264 m_total = 0;
263 265
264 266 // nothing to do?
265 267 if (m_slices.count() == 0)
266 268 return;
267 269
268 270 // calculate total
269 271 foreach (QPieSlice* s, m_slices)
270 272 m_total += s->value();
271 273
272 274 // we must have some values
273 275 Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this?
274 276
275 277 // update slice attributes
276 278 qreal sliceAngle = m_pieStartAngle;
277 279 foreach (QPieSlice* s, m_slices) {
278 280
279 281 bool changed = false;
280 282
281 283 qreal percentage = s->value() / m_total;
282 284 if (s->m_percentage != percentage) {
283 285 s->m_percentage = percentage;
284 286 changed = true;
285 287 }
286 288
287 289 qreal sliceSpan = m_pieSpan * percentage;
288 290 if (s->m_angleSpan != sliceSpan) {
289 291 s->m_angleSpan = sliceSpan;
290 292 changed = true;
291 293 }
292 294
293 295 if (s->m_angle != sliceAngle) {
294 296 s->m_angle = sliceAngle;
295 297 changed = true;
296 298 }
297 299 sliceAngle += sliceSpan;
298 300
299 301 if (changed)
300 302 emit s->changed();
301 303 }
302 304 }
303 305
304 306 #include "moc_qpieseries.cpp"
305 307
306 308 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,197 +1,197
1 1 #include "qpieslice.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 #define DEFAULT_PEN_COLOR Qt::black
6 6 #define DEFAULT_BRUSH_COLOR Qt::white
7 7 #define DEFAULT_LABEL_ARM_LENGTH 50
8 8 #define DEFAULT_EXPOLODE_DISTANCE 20
9 9
10 10 QPieSlice::QPieSlice(QObject *parent)
11 11 :QObject(parent),
12 12 m_value(0),
13 m_isLabelVisible(true),
13 m_isLabelVisible(false),
14 14 m_isExploded(false),
15 15 m_explodeDistance(DEFAULT_EXPOLODE_DISTANCE),
16 16 m_percentage(0),
17 17 m_angle(0),
18 18 m_angleSpan(0),
19 19 m_pen(DEFAULT_PEN_COLOR),
20 20 m_brush(DEFAULT_BRUSH_COLOR),
21 21 m_labelPen(DEFAULT_PEN_COLOR),
22 22 m_labelArmLength(DEFAULT_LABEL_ARM_LENGTH)
23 23 {
24 24
25 25 }
26 26
27 27 QPieSlice::QPieSlice(qreal value, QString label, bool labelVisible, QObject *parent)
28 28 :QObject(parent),
29 29 m_value(value),
30 30 m_label(label),
31 31 m_isLabelVisible(labelVisible),
32 32 m_isExploded(false),
33 33 m_explodeDistance(DEFAULT_EXPOLODE_DISTANCE),
34 34 m_percentage(0),
35 35 m_angle(0),
36 36 m_angleSpan(0),
37 37 m_pen(DEFAULT_PEN_COLOR),
38 38 m_brush(DEFAULT_BRUSH_COLOR),
39 39 m_labelPen(DEFAULT_PEN_COLOR),
40 40 m_labelArmLength(DEFAULT_LABEL_ARM_LENGTH)
41 41 {
42 42
43 43 }
44 44
45 45 QPieSlice::~QPieSlice()
46 46 {
47 47
48 48 }
49 49
50 50 qreal QPieSlice::value() const
51 51 {
52 52 return m_value;
53 53 }
54 54
55 55 QString QPieSlice::label() const
56 56 {
57 57 return m_label;
58 58 }
59 59
60 60 bool QPieSlice::isLabelVisible() const
61 61 {
62 62 return m_isLabelVisible;
63 63 }
64 64
65 65 bool QPieSlice::isExploded() const
66 66 {
67 67 return m_isExploded;
68 68 }
69 69
70 70 qreal QPieSlice::explodeDistance() const
71 71 {
72 72 return m_explodeDistance;
73 73 }
74 74
75 75 qreal QPieSlice::percentage() const
76 76 {
77 77 return m_percentage;
78 78 }
79 79
80 80 qreal QPieSlice::angle() const
81 81 {
82 82 return m_angle;
83 83 }
84 84
85 85 qreal QPieSlice::angleSpan() const
86 86 {
87 87 return m_angleSpan;
88 88 }
89 89
90 90 QPen QPieSlice::pen() const
91 91 {
92 92 return m_pen;
93 93 }
94 94
95 95 QBrush QPieSlice::brush() const
96 96 {
97 97 return m_brush;
98 98 }
99 99
100 100 QPen QPieSlice::labelPen() const
101 101 {
102 102 return m_labelPen;
103 103 }
104 104
105 105 QFont QPieSlice::labelFont() const
106 106 {
107 107 return m_labelFont;
108 108 }
109 109
110 110 qreal QPieSlice::labelArmLength() const
111 111 {
112 112 return m_labelArmLength;
113 113 }
114 114
115 115 void QPieSlice::setValue(qreal value)
116 116 {
117 117 if (m_value != value) {
118 118 m_value = value;
119 119 emit changed();
120 120 }
121 121 }
122 122
123 123 void QPieSlice::setLabel(QString label)
124 124 {
125 125 if (m_label != label) {
126 126 m_label = label;
127 127 emit changed();
128 128 }
129 129 }
130 130
131 131 void QPieSlice::setLabelVisible(bool visible)
132 132 {
133 133 if (m_isLabelVisible != visible) {
134 134 m_isLabelVisible = visible;
135 135 emit changed();
136 136 }
137 137 }
138 138
139 139 void QPieSlice::setExploded(bool exploded)
140 140 {
141 141 if (m_isExploded != exploded) {
142 142 m_isExploded = exploded;
143 143 emit changed();
144 144 }
145 145 }
146 146
147 147 void QPieSlice::setExplodeDistance(qreal distance)
148 148 {
149 149 if (m_explodeDistance != distance) {
150 150 m_explodeDistance = distance;
151 151 emit changed();
152 152 }
153 153 }
154 154
155 155 void QPieSlice::setPen(QPen pen)
156 156 {
157 157 if (m_pen != pen) {
158 158 m_pen = pen;
159 159 emit changed();
160 160 }
161 161 }
162 162
163 163 void QPieSlice::setBrush(QBrush brush)
164 164 {
165 165 if (m_brush != brush) {
166 166 m_brush = brush;
167 167 emit changed();
168 168 }
169 169 }
170 170
171 171 void QPieSlice::setLabelFont(QFont font)
172 172 {
173 173 if (m_labelFont != font) {
174 174 m_labelFont = font;
175 175 emit changed();
176 176 }
177 177 }
178 178
179 179 void QPieSlice::setLabelPen(QPen pen)
180 180 {
181 181 if (m_labelPen != pen) {
182 182 m_labelPen = pen;
183 183 emit changed();
184 184 }
185 185 }
186 186
187 187 void QPieSlice::setLabelArmLength(qreal len)
188 188 {
189 189 if (m_labelArmLength != len) {
190 190 m_labelArmLength = len;
191 191 emit changed();
192 192 }
193 193 }
194 194
195 195 #include "moc_qpieslice.cpp"
196 196
197 197 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,96 +1,96
1 1 #ifndef QPIESLICE_H
2 2 #define QPIESLICE_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <QObject>
6 6 #include <QPen>
7 7 #include <QBrush>
8 8 #include <QFont>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
13 13 {
14 14 Q_OBJECT
15 15 Q_PROPERTY(QString label READ label WRITE setLabel /*NOTIFY dataYChanged*/)
16 16 Q_PROPERTY(qreal value READ value WRITE setValue /*NOTIFY dataXChanged*/)
17 17
18 18 public:
19 19 QPieSlice(QObject *parent = 0);
20 QPieSlice(qreal value, QString label, bool labelVisible = true, QObject *parent = 0);
20 QPieSlice(qreal value, QString label, bool labelVisible = false, QObject *parent = 0);
21 21 virtual ~QPieSlice();
22 22
23 23 // data
24 24 qreal value() const;
25 25 QString label() const;
26 26 bool isLabelVisible() const;
27 27 bool isExploded() const;
28 28 qreal explodeDistance() const;
29 29
30 30 // generated data
31 31 qreal percentage() const;
32 32 qreal angle() const;
33 33 qreal angleSpan() const;
34 34
35 35 // customization
36 36 QPen pen() const;
37 37 QBrush brush() const;
38 38 QPen labelPen() const;
39 39 QFont labelFont() const;
40 40 qreal labelArmLength() const;
41 41
42 42 Q_SIGNALS:
43 43 void clicked();
44 44 void hoverEnter();
45 45 void hoverLeave();
46 46 void changed();
47 47
48 48 public Q_SLOTS:
49 49
50 50 // data
51 51 void setLabel(QString label);
52 52 void setLabelVisible(bool visible);
53 53 void setValue(qreal value);
54 54 void setExploded(bool exploded);
55 55 void setExplodeDistance(qreal distance);
56 56
57 57 // customization
58 58 void setPen(QPen pen);
59 59 void setBrush(QBrush brush);
60 60 void setLabelFont(QFont font);
61 61 void setLabelPen(QPen pen);
62 62 void setLabelArmLength(qreal len);
63 63
64 64 // TODO: label position in general
65 65 // setLabelFlags(inside|outside|labelArmOn|labelArmOff|???)
66 66 // setLabelOrientation(horizontal|vertical|same as slice center angle|???)
67 67
68 68 private:
69 69
70 70 // TODO: use private class
71 71 friend class QPieSeries;
72 72 friend class PiePresenter;
73 73
74 74 // data
75 75 qreal m_value;
76 76 QString m_label;
77 77 bool m_isLabelVisible;
78 78 bool m_isExploded;
79 79 qreal m_explodeDistance;
80 80
81 81 // generated data
82 82 qreal m_percentage;
83 83 qreal m_angle;
84 84 qreal m_angleSpan;
85 85
86 86 // customization
87 87 QPen m_pen;
88 88 QBrush m_brush;
89 89 QPen m_labelPen;
90 90 QFont m_labelFont;
91 91 qreal m_labelArmLength;
92 92 };
93 93
94 94 QTCOMMERCIALCHART_END_NAMESPACE
95 95
96 96 #endif // QPIESLICE_H
General Comments 0
You need to be logged in to leave comments. Login now