@@ -26,9 +26,6 int main(int argc, char *argv[]) | |||||
26 | series->enableClickExplodes(true); |
|
26 | series->enableClickExplodes(true); | |
27 | series->enableHoverHighlight(true); |
|
27 | series->enableHoverHighlight(true); | |
28 |
|
28 | |||
29 | foreach (QPieSlice*s, series->slices()) |
|
|||
30 | qDebug() << s->angle() << s->span() << s->percentage(); |
|
|||
31 |
|
||||
32 | QChartView* chartView = new QChartView(&window); |
|
29 | QChartView* chartView = new QChartView(&window); | |
33 | chartView->setRenderHint(QPainter::Antialiasing); |
|
30 | chartView->setRenderHint(QPainter::Antialiasing); | |
34 | chartView->addSeries(series); |
|
31 | chartView->addSeries(series); |
@@ -2,8 +2,12 | |||||
2 | #include "piepresenter.h" |
|
2 | #include "piepresenter.h" | |
3 | #include "pieslice.h" |
|
3 | #include "pieslice.h" | |
4 | #include "qpieslice.h" |
|
4 | #include "qpieslice.h" | |
|
5 | #include "pieslicelabel.h" | |||
|
6 | #include "qpieseries.h" | |||
|
7 | #include <qmath.h> | |||
5 | #include <QDebug> |
|
8 | #include <QDebug> | |
6 |
#include <Q |
|
9 | #include <QFontMetrics> | |
|
10 | ||||
7 |
|
11 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
13 | |||
@@ -15,6 +19,12 PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) | |||||
15 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); |
|
19 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); | |
16 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); |
|
20 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); | |
17 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); |
|
21 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); | |
|
22 | ||||
|
23 | if (m_series->count()) { | |||
|
24 | QPieSeries::ChangeSet changeSet; | |||
|
25 | changeSet.appendAdded(m_series->m_slices); | |||
|
26 | handleSeriesChanged(changeSet); | |||
|
27 | } | |||
18 | } |
|
28 | } | |
19 |
|
29 | |||
20 | PiePresenter::~PiePresenter() |
|
30 | PiePresenter::~PiePresenter() | |
@@ -22,7 +32,7 PiePresenter::~PiePresenter() | |||||
22 | // slices deleted automatically through QGraphicsItem |
|
32 | // slices deleted automatically through QGraphicsItem | |
23 | } |
|
33 | } | |
24 |
|
34 | |||
25 | void PiePresenter::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) |
|
35 | void PiePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | |
26 | { |
|
36 | { | |
27 | // TODO: paint shadows for all components |
|
37 | // TODO: paint shadows for all components | |
28 | // - get paths from items & merge & offset and draw with shadow color? |
|
38 | // - get paths from items & merge & offset and draw with shadow color? | |
@@ -35,19 +45,17 void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) | |||||
35 | //qDebug() << " changed: " << changeSet.changed(); |
|
45 | //qDebug() << " changed: " << changeSet.changed(); | |
36 | //qDebug() << " removed: " << changeSet.removed(); |
|
46 | //qDebug() << " removed: " << changeSet.removed(); | |
37 |
|
47 | |||
38 | // ignore changeset when there are no visual slices |
|
48 | foreach (QPieSlice* s, changeSet.added()) | |
39 | // changeset might not be valid about the added slices |
|
49 | addSlice(s); | |
40 | if (m_slices.count() == 0) { |
|
50 | ||
41 |
|
|
51 | foreach (QPieSlice* s, changeSet.changed()) | |
42 |
|
|
52 | updateSlice(s); | |
43 | return; |
|
|||
44 | } |
|
|||
45 |
|
53 | |||
46 | foreach (QPieSlice* s, changeSet.removed()) |
|
54 | foreach (QPieSlice* s, changeSet.removed()) | |
47 | deleteSlice(s); |
|
55 | deleteSlice(s); | |
48 |
|
56 | |||
49 | foreach (QPieSlice* s, changeSet.added()) |
|
57 | // every change possibly changes the actual pie size | |
50 | addSlice(s); |
|
58 | updateGeometry(); | |
51 | } |
|
59 | } | |
52 |
|
60 | |||
53 | void PiePresenter::handleDomainChanged(const Domain& domain) |
|
61 | void PiePresenter::handleDomainChanged(const Domain& domain) | |
@@ -58,15 +66,17 void PiePresenter::handleDomainChanged(const Domain& domain) | |||||
58 | void PiePresenter::handleGeometryChanged(const QRectF& rect) |
|
66 | void PiePresenter::handleGeometryChanged(const QRectF& rect) | |
59 | { |
|
67 | { | |
60 | m_rect = rect; |
|
68 | m_rect = rect; | |
|
69 | prepareGeometryChange(); | |||
61 | updateGeometry(); |
|
70 | updateGeometry(); | |
62 | } |
|
71 | } | |
63 |
|
72 | |||
64 | void PiePresenter::updateGeometry() |
|
73 | void PiePresenter::updateGeometry() | |
65 | { |
|
74 | { | |
66 | prepareGeometryChange(); |
|
75 | if (!m_rect.isValid() || m_rect.isEmpty()) | |
|
76 | return; | |||
67 |
|
77 | |||
|
78 | // calculate maximum rectangle for pie | |||
68 | QRectF pieRect = m_rect; |
|
79 | QRectF pieRect = m_rect; | |
69 |
|
||||
70 | if (pieRect.width() < pieRect.height()) { |
|
80 | if (pieRect.width() < pieRect.height()) { | |
71 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); |
|
81 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); | |
72 | pieRect.setHeight(pieRect.width()); |
|
82 | pieRect.setHeight(pieRect.width()); | |
@@ -77,6 +87,7 void PiePresenter::updateGeometry() | |||||
77 | pieRect.moveCenter(m_rect.center()); |
|
87 | pieRect.moveCenter(m_rect.center()); | |
78 | } |
|
88 | } | |
79 |
|
89 | |||
|
90 | // position the pie rectangle | |||
80 | switch (m_series->position()) { |
|
91 | switch (m_series->position()) { | |
81 | case QPieSeries::PiePositionTopLeft: { |
|
92 | case QPieSeries::PiePositionTopLeft: { | |
82 | pieRect.setHeight(pieRect.height() / 2); |
|
93 | pieRect.setHeight(pieRect.height() / 2); | |
@@ -106,12 +117,43 void PiePresenter::updateGeometry() | |||||
106 | break; |
|
117 | break; | |
107 | } |
|
118 | } | |
108 |
|
119 | |||
|
120 | // calculate how much space we need around the pie rectangle (labels & exploding) | |||
|
121 | qreal delta = 0; | |||
|
122 | qreal pieRadius = pieRect.height() / 2; | |||
|
123 | foreach (QPieSlice* s, m_series->m_slices) { | |||
|
124 | ||||
|
125 | // calculate the farthest point in the slice from the pie center | |||
|
126 | qreal centerAngle = s->angle() + (s->angleSpan() / 2); | |||
|
127 | qreal len = pieRadius + s->labelArmLength() + s->explodeDistance(); | |||
|
128 | QPointF dp(qSin(centerAngle*(PI/180)) * len, -qCos(centerAngle*(PI/180)) * len); | |||
|
129 | QPointF p = pieRect.center() + dp; | |||
|
130 | ||||
|
131 | // TODO: consider the label text | |||
|
132 | ||||
|
133 | // calculate how much the radius must get smaller to fit that point in the base rectangle | |||
|
134 | qreal dt = m_rect.top() - p.y(); | |||
|
135 | if (dt > delta) delta = dt; | |||
|
136 | qreal dl = m_rect.left() - p.x(); | |||
|
137 | if (dl > delta) delta = dl; | |||
|
138 | qreal dr = p.x() - m_rect.right(); | |||
|
139 | if (dr > delta) delta = dr; | |||
|
140 | qreal db = p.y() - m_rect.bottom(); | |||
|
141 | if (db > delta) delta = db; | |||
|
142 | ||||
|
143 | //if (!m_rect.contains(p)) qDebug() << s->label() << dt << dl << dr << db << "delta" << delta; | |||
|
144 | } | |||
|
145 | ||||
|
146 | // shrink the pie rectangle so that everything outside it fits the base rectangle | |||
|
147 | pieRect.adjust(delta, delta, -delta, -delta); | |||
|
148 | ||||
|
149 | // update slices | |||
109 | if (m_pieRect != pieRect) { |
|
150 | if (m_pieRect != pieRect) { | |
110 | m_pieRect = pieRect; |
|
151 | m_pieRect = pieRect; | |
111 | //qDebug() << "PiePresenter::updateGeometry()" << m_pieRect; |
|
152 | //qDebug() << "PiePresenter::updateGeometry()" << m_rect << m_pieRect; | |
112 | foreach (PieSlice* s, m_slices.values()) { |
|
153 | foreach (PieSlice* s, m_slices.values()) { | |
113 | s->setPieRect(m_pieRect); |
|
154 | s->setPieRect(m_pieRect); | |
114 | s->updateGeometry(); |
|
155 | s->updateGeometry(); | |
|
156 | s->update(); | |||
115 | } |
|
157 | } | |
116 | } |
|
158 | } | |
117 | } |
|
159 | } | |
@@ -121,8 +163,7 void PiePresenter::addSlice(QPieSlice* sliceData) | |||||
121 | //qDebug() << "PiePresenter::addSlice()" << sliceData; |
|
163 | //qDebug() << "PiePresenter::addSlice()" << sliceData; | |
122 |
|
164 | |||
123 | if (m_slices.keys().contains(sliceData)) { |
|
165 | if (m_slices.keys().contains(sliceData)) { | |
124 | //qWarning() << "PiePresenter::addSlice(): slice already exists!" << sliceData; |
|
166 | Q_ASSERT(0); // TODO: how to handle this nicely? | |
125 | Q_ASSERT(0); |
|
|||
126 | return; |
|
167 | return; | |
127 | } |
|
168 | } | |
128 |
|
169 | |||
@@ -135,22 +176,33 void PiePresenter::addSlice(QPieSlice* sliceData) | |||||
135 | m_slices.insert(sliceData, slice); |
|
176 | m_slices.insert(sliceData, slice); | |
136 |
|
177 | |||
137 | // connect signals |
|
178 | // connect signals | |
138 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); |
|
|||
139 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); |
|
179 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); | |
140 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); |
|
180 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); | |
141 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); |
|
181 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); | |
142 | } |
|
182 | } | |
143 |
|
183 | |||
|
184 | void PiePresenter::updateSlice(QPieSlice* sliceData) | |||
|
185 | { | |||
|
186 | //qDebug() << "PiePresenter::updateSlice()" << sliceData; | |||
|
187 | ||||
|
188 | if (!m_slices.contains(sliceData)) { | |||
|
189 | Q_ASSERT(0); // TODO: how to handle this nicely? | |||
|
190 | return; | |||
|
191 | } | |||
|
192 | ||||
|
193 | m_slices[sliceData]->updateData(sliceData); | |||
|
194 | } | |||
|
195 | ||||
144 | void PiePresenter::deleteSlice(QPieSlice* sliceData) |
|
196 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | |
145 | { |
|
197 | { | |
146 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; |
|
198 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | |
147 |
|
199 | |||
148 | if (m_slices.contains(sliceData)) |
|
200 | if (!m_slices.contains(sliceData)) { | |
149 | delete m_slices.take(sliceData); |
|
201 | Q_ASSERT(0); // TODO: how to handle this nicely? | |
150 | else { |
|
202 | return; | |
151 | // nothing to remove |
|
|||
152 | Q_ASSERT(0); // TODO: remove before release |
|
|||
153 | } |
|
203 | } | |
|
204 | ||||
|
205 | delete m_slices.take(sliceData); | |||
154 | } |
|
206 | } | |
155 |
|
207 | |||
156 | #include "moc_piepresenter.cpp" |
|
208 | #include "moc_piepresenter.cpp" |
@@ -9,6 +9,8 class QGraphicsItem; | |||||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 | class PieSlice; |
|
10 | class PieSlice; | |
11 |
|
11 | |||
|
12 | #define PI 3.14159265 // TODO: is this defined in some header? | |||
|
13 | ||||
12 | class PiePresenter : public QObject, public ChartItem |
|
14 | class PiePresenter : public QObject, public ChartItem | |
13 | { |
|
15 | { | |
14 | Q_OBJECT |
|
16 | Q_OBJECT | |
@@ -33,6 +35,7 public Q_SLOTS: | |||||
33 |
|
35 | |||
34 | private: |
|
36 | private: | |
35 | void addSlice(QPieSlice* sliceData); |
|
37 | void addSlice(QPieSlice* sliceData); | |
|
38 | void updateSlice(QPieSlice* sliceData); | |||
36 | void deleteSlice(QPieSlice* sliceData); |
|
39 | void deleteSlice(QPieSlice* sliceData); | |
37 |
|
40 | |||
38 | private: |
|
41 | private: |
@@ -11,9 +11,6 | |||||
11 |
|
11 | |||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 |
|
13 | |||
14 | #define PI 3.14159265 |
|
|||
15 | #define EXPLODE_OFFSET 20 |
|
|||
16 |
|
||||
17 | QPointF offset(qreal angle, qreal length) |
|
14 | QPointF offset(qreal angle, qreal length) | |
18 | { |
|
15 | { | |
19 | qreal dx = qSin(angle*(PI/180)) * length; |
|
16 | qreal dx = qSin(angle*(PI/180)) * length; | |
@@ -25,8 +22,9 PieSlice::PieSlice(QGraphicsItem* parent) | |||||
25 | :QGraphicsObject(parent), |
|
22 | :QGraphicsObject(parent), | |
26 | m_slicelabel(new PieSliceLabel(this)), |
|
23 | m_slicelabel(new PieSliceLabel(this)), | |
27 | m_angle(0), |
|
24 | m_angle(0), | |
28 |
m_ |
|
25 | m_angleSpan(0), | |
29 | m_isExploded(false) |
|
26 | m_isExploded(false), | |
|
27 | m_explodeDistance(0) | |||
30 | { |
|
28 | { | |
31 | setAcceptHoverEvents(true); |
|
29 | setAcceptHoverEvents(true); | |
32 | setAcceptedMouseButtons(Qt::LeftButton); |
|
30 | setAcceptedMouseButtons(Qt::LeftButton); | |
@@ -76,24 +74,27 void PieSlice::setPieRect(QRectF rect) | |||||
76 |
|
74 | |||
77 | void PieSlice::updateGeometry() |
|
75 | void PieSlice::updateGeometry() | |
78 | { |
|
76 | { | |
|
77 | if (!m_pieRect.isValid() || m_pieRect.isEmpty()) | |||
|
78 | return; | |||
|
79 | ||||
79 | prepareGeometryChange(); |
|
80 | prepareGeometryChange(); | |
80 |
|
81 | |||
81 | // calculate center angle |
|
82 | // calculate center angle | |
82 |
qreal centerAngle = m_angle + (m_ |
|
83 | qreal centerAngle = m_angle + (m_angleSpan/2); | |
83 |
|
84 | |||
84 | // adjust rect for exploding |
|
85 | // adjust rect for exploding | |
85 | QRectF rect = m_pieRect; |
|
86 | QRectF rect = m_pieRect; | |
86 | rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET); |
|
|||
87 | if (m_isExploded) { |
|
87 | if (m_isExploded) { | |
88 | QPointF d = offset((centerAngle), EXPLODE_OFFSET); |
|
88 | qreal dx = qSin(centerAngle*(PI/180)) * m_explodeDistance; | |
89 | rect.translate(d.x(), d.y()); |
|
89 | qreal dy = -qCos(centerAngle*(PI/180)) * m_explodeDistance; | |
|
90 | rect.translate(dx, dy); | |||
90 | } |
|
91 | } | |
91 |
|
92 | |||
92 | // update slice path |
|
93 | // update slice path | |
93 | // TODO: draw the shape so that it might have a hole in the center |
|
94 | // TODO: draw the shape so that it might have a hole in the center | |
94 | QPainterPath path; |
|
95 | QPainterPath path; | |
95 | path.moveTo(rect.center()); |
|
96 | path.moveTo(rect.center()); | |
96 |
path.arcTo(rect, -m_angle + 90, -m_ |
|
97 | path.arcTo(rect, -m_angle + 90, -m_angleSpan); | |
97 | path.closeSubpath(); |
|
98 | path.closeSubpath(); | |
98 | m_path = path; |
|
99 | m_path = path; | |
99 |
|
100 | |||
@@ -103,24 +104,19 void PieSlice::updateGeometry() | |||||
103 | m_slicelabel->setArmStartPoint(edgeCenter); |
|
104 | m_slicelabel->setArmStartPoint(edgeCenter); | |
104 | m_slicelabel->setArmAngle(centerAngle); |
|
105 | m_slicelabel->setArmAngle(centerAngle); | |
105 | m_slicelabel->updateGeometry(); |
|
106 | m_slicelabel->updateGeometry(); | |
|
107 | m_slicelabel->update(); | |||
106 |
|
108 | |||
107 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; |
|
109 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; | |
108 | } |
|
110 | } | |
109 |
|
111 | |||
110 | void PieSlice::handleSliceDataChanged() |
|
|||
111 | { |
|
|||
112 | QPieSlice *slice = qobject_cast<QPieSlice*>(sender()); |
|
|||
113 | Q_ASSERT(slice); |
|
|||
114 | updateData(slice); |
|
|||
115 | } |
|
|||
116 |
|
||||
117 | void PieSlice::updateData(const QPieSlice* sliceData) |
|
112 | void PieSlice::updateData(const QPieSlice* sliceData) | |
118 | { |
|
113 | { | |
119 | // TODO: compare what has changes to avoid unneccesary geometry updates |
|
114 | // TODO: compare what has changes to avoid unneccesary geometry updates | |
120 |
|
115 | |||
121 | m_angle = sliceData->angle(); |
|
116 | m_angle = sliceData->angle(); | |
122 |
m_ |
|
117 | m_angleSpan = sliceData->angleSpan(); | |
123 | m_isExploded = sliceData->isExploded(); |
|
118 | m_isExploded = sliceData->isExploded(); | |
|
119 | m_explodeDistance = sliceData->explodeDistance(); // TODO: expose to public API | |||
124 | m_pen = sliceData->pen(); |
|
120 | m_pen = sliceData->pen(); | |
125 | m_brush = sliceData->brush(); |
|
121 | m_brush = sliceData->brush(); | |
126 |
|
122 | |||
@@ -128,11 +124,10 void PieSlice::updateData(const QPieSlice* sliceData) | |||||
128 | m_slicelabel->setText(sliceData->label()); |
|
124 | m_slicelabel->setText(sliceData->label()); | |
129 | m_slicelabel->setPen(sliceData->labelPen()); |
|
125 | m_slicelabel->setPen(sliceData->labelPen()); | |
130 | m_slicelabel->setFont(sliceData->labelFont()); |
|
126 | m_slicelabel->setFont(sliceData->labelFont()); | |
131 |
m_slicelabel->setArmLength(sliceData->labelArmLeng |
|
127 | m_slicelabel->setArmLength(sliceData->labelArmLength()); | |
132 |
|
128 | |||
133 | updateGeometry(); |
|
129 | updateGeometry(); | |
134 | update(); |
|
130 | update(); | |
135 | m_slicelabel->update(); |
|
|||
136 | } |
|
131 | } | |
137 |
|
132 | |||
138 | #include "moc_pieslice.cpp" |
|
133 | #include "moc_pieslice.cpp" |
@@ -36,11 +36,13 Q_SIGNALS: | |||||
36 | void hoverLeave(); |
|
36 | void hoverLeave(); | |
37 |
|
37 | |||
38 | public Q_SLOTS: |
|
38 | public Q_SLOTS: | |
39 | void handleSliceDataChanged(); |
|
|||
40 | void setPieRect(QRectF rect); |
|
39 | void setPieRect(QRectF rect); | |
41 | void updateGeometry(); |
|
40 | void updateGeometry(); | |
42 | void updateData(const QPieSlice *sliceData); |
|
41 | void updateData(const QPieSlice *sliceData); | |
43 |
|
42 | |||
|
43 | public: | |||
|
44 | PieSliceLabel* label() { return m_slicelabel; } | |||
|
45 | ||||
44 | private: |
|
46 | private: | |
45 | PieSliceLabel* m_slicelabel; |
|
47 | PieSliceLabel* m_slicelabel; | |
46 |
|
48 | |||
@@ -48,8 +50,10 private: | |||||
48 | QPainterPath m_path; |
|
50 | QPainterPath m_path; | |
49 |
|
51 | |||
50 | qreal m_angle; |
|
52 | qreal m_angle; | |
51 |
qreal m_ |
|
53 | qreal m_angleSpan; | |
|
54 | ||||
52 | bool m_isExploded; |
|
55 | bool m_isExploded; | |
|
56 | qreal m_explodeDistance; | |||
53 |
|
57 | |||
54 | QPen m_pen; |
|
58 | QPen m_pen; | |
55 | QBrush m_brush; |
|
59 | QBrush m_brush; |
@@ -2,6 +2,7 | |||||
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 <QDebug> |
|
6 | #include <QDebug> | |
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
@@ -12,6 +13,12 void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) | |||||
12 | m_added << slice; |
|
13 | m_added << slice; | |
13 | } |
|
14 | } | |
14 |
|
15 | |||
|
16 | void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices) | |||
|
17 | { | |||
|
18 | foreach (QPieSlice* s, slices) | |||
|
19 | appendAdded(s); | |||
|
20 | } | |||
|
21 | ||||
15 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) |
|
22 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) | |
16 | { |
|
23 | { | |
17 | if (!m_changed.contains(slice)) |
|
24 | if (!m_changed.contains(slice)) | |
@@ -114,7 +121,7 QPieSlice* QPieSeries::add(qreal value, QString name) | |||||
114 | void QPieSeries::remove(QPieSlice* slice) |
|
121 | void QPieSeries::remove(QPieSlice* slice) | |
115 | { |
|
122 | { | |
116 | if (!m_slices.removeOne(slice)) { |
|
123 | if (!m_slices.removeOne(slice)) { | |
117 |
Q_ASSERT(0); // TODO: |
|
124 | Q_ASSERT(0); // TODO: how should this be reported? | |
118 | return; |
|
125 | return; | |
119 | } |
|
126 | } | |
120 |
|
127 | |||
@@ -263,7 +270,7 void QPieSeries::updateDerivativeData() | |||||
263 | m_total += s->value(); |
|
270 | m_total += s->value(); | |
264 |
|
271 | |||
265 | // we must have some values |
|
272 | // we must have some values | |
266 | Q_ASSERT(m_total > 0); // TODO |
|
273 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? | |
267 |
|
274 | |||
268 | // update slice attributes |
|
275 | // update slice attributes | |
269 | qreal sliceAngle = m_pieStartAngle; |
|
276 | qreal sliceAngle = m_pieStartAngle; | |
@@ -278,8 +285,8 void QPieSeries::updateDerivativeData() | |||||
278 | } |
|
285 | } | |
279 |
|
286 | |||
280 | qreal sliceSpan = m_pieSpan * percentage; |
|
287 | qreal sliceSpan = m_pieSpan * percentage; | |
281 |
if (s->m_ |
|
288 | if (s->m_angleSpan != sliceSpan) { | |
282 |
s->m_ |
|
289 | s->m_angleSpan = sliceSpan; | |
283 | changed = true; |
|
290 | changed = true; | |
284 | } |
|
291 | } | |
285 |
|
292 |
@@ -32,6 +32,7 public: | |||||
32 | { |
|
32 | { | |
33 | public: |
|
33 | public: | |
34 | void appendAdded(QPieSlice* slice); |
|
34 | void appendAdded(QPieSlice* slice); | |
|
35 | void appendAdded(QList<QPieSlice*> slices); | |||
35 | void appendChanged(QPieSlice* slice); |
|
36 | void appendChanged(QPieSlice* slice); | |
36 | void appendRemoved(QPieSlice* slice); |
|
37 | void appendRemoved(QPieSlice* slice); | |
37 |
|
38 | |||
@@ -67,13 +68,6 public: | |||||
67 |
|
68 | |||
68 | QList<QPieSlice*> slices() const { return m_slices; } |
|
69 | QList<QPieSlice*> slices() const { return m_slices; } | |
69 |
|
70 | |||
70 | // TODO: find slices? |
|
|||
71 | // QList<QPieSlice*> findByValue(qreal value); |
|
|||
72 | // ... |
|
|||
73 |
|
||||
74 | // TODO: sorting slices? |
|
|||
75 | // void sort(QPieSeries::SortByValue) |
|
|||
76 |
|
||||
77 | void setSizeFactor(qreal sizeFactor); |
|
71 | void setSizeFactor(qreal sizeFactor); | |
78 | qreal sizeFactor() const { return m_sizeFactor; } |
|
72 | qreal sizeFactor() const { return m_sizeFactor; } | |
79 |
|
73 | |||
@@ -86,6 +80,17 public: | |||||
86 | void enableClickExplodes(bool enable); |
|
80 | void enableClickExplodes(bool enable); | |
87 | void enableHoverHighlight(bool enable); |
|
81 | void enableHoverHighlight(bool enable); | |
88 |
|
82 | |||
|
83 | // TODO: find slices? | |||
|
84 | // QList<QPieSlice*> findByValue(qreal value); | |||
|
85 | // ... | |||
|
86 | ||||
|
87 | // TODO: sorting slices? | |||
|
88 | // void sort(QPieSeries::SortByValue|label|??) | |||
|
89 | ||||
|
90 | // TODO: general graphics customization | |||
|
91 | // setDrawStyle(2d|3d) | |||
|
92 | // setDropShadows(bool) | |||
|
93 | ||||
89 | Q_SIGNALS: |
|
94 | Q_SIGNALS: | |
90 | void changed(const QPieSeries::ChangeSet& changeSet); |
|
95 | void changed(const QPieSeries::ChangeSet& changeSet); | |
91 | void clicked(QPieSlice* slice); |
|
96 | void clicked(QPieSlice* slice); | |
@@ -94,12 +99,11 Q_SIGNALS: | |||||
94 | void sizeFactorChanged(); |
|
99 | void sizeFactorChanged(); | |
95 | void positionChanged(); |
|
100 | void positionChanged(); | |
96 |
|
101 | |||
97 | private Q_SLOTS: // should be private and not in the interface |
|
102 | private Q_SLOTS: // TODO: should be private and not visible in the interface at all | |
98 | void sliceChanged(); |
|
103 | void sliceChanged(); | |
99 | void sliceClicked(); |
|
104 | void sliceClicked(); | |
100 | void sliceHoverEnter(); |
|
105 | void sliceHoverEnter(); | |
101 | void sliceHoverLeave(); |
|
106 | void sliceHoverLeave(); | |
102 |
|
||||
103 | void toggleExploded(QPieSlice* slice); |
|
107 | void toggleExploded(QPieSlice* slice); | |
104 | void highlightOn(QPieSlice* slice); |
|
108 | void highlightOn(QPieSlice* slice); | |
105 | void highlightOff(QPieSlice* slice); |
|
109 | void highlightOff(QPieSlice* slice); |
@@ -2,18 +2,20 | |||||
2 |
|
2 | |||
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
4 | |||
5 | #define DEFAULT_PEN_COLOR Qt::black |
|
5 | #define DEFAULT_PEN_COLOR Qt::black | |
6 | #define DEFAULT_BRUSH_COLOR Qt::white |
|
6 | #define DEFAULT_BRUSH_COLOR Qt::white | |
7 | #define DEFAULT_LABEL_ARM_LENGTH 50 |
|
7 | #define DEFAULT_LABEL_ARM_LENGTH 50 | |
|
8 | #define DEFAULT_EXPOLODE_DISTANCE 20 | |||
8 |
|
9 | |||
9 | QPieSlice::QPieSlice(QObject *parent) |
|
10 | QPieSlice::QPieSlice(QObject *parent) | |
10 | :QObject(parent), |
|
11 | :QObject(parent), | |
11 | m_value(0), |
|
12 | m_value(0), | |
12 | m_isLabelVisible(true), |
|
13 | m_isLabelVisible(true), | |
13 | m_isExploded(false), |
|
14 | m_isExploded(false), | |
|
15 | m_explodeDistance(DEFAULT_EXPOLODE_DISTANCE), | |||
14 | m_percentage(0), |
|
16 | m_percentage(0), | |
15 | m_angle(0), |
|
17 | m_angle(0), | |
16 |
m_ |
|
18 | m_angleSpan(0), | |
17 | m_pen(DEFAULT_PEN_COLOR), |
|
19 | m_pen(DEFAULT_PEN_COLOR), | |
18 | m_brush(DEFAULT_BRUSH_COLOR), |
|
20 | m_brush(DEFAULT_BRUSH_COLOR), | |
19 | m_labelPen(DEFAULT_PEN_COLOR), |
|
21 | m_labelPen(DEFAULT_PEN_COLOR), | |
@@ -28,9 +30,10 QPieSlice::QPieSlice(qreal value, QString label, bool labelVisible, QObject *par | |||||
28 | m_label(label), |
|
30 | m_label(label), | |
29 | m_isLabelVisible(labelVisible), |
|
31 | m_isLabelVisible(labelVisible), | |
30 | m_isExploded(false), |
|
32 | m_isExploded(false), | |
|
33 | m_explodeDistance(DEFAULT_EXPOLODE_DISTANCE), | |||
31 | m_percentage(0), |
|
34 | m_percentage(0), | |
32 | m_angle(0), |
|
35 | m_angle(0), | |
33 |
m_ |
|
36 | m_angleSpan(0), | |
34 | m_pen(DEFAULT_PEN_COLOR), |
|
37 | m_pen(DEFAULT_PEN_COLOR), | |
35 | m_brush(DEFAULT_BRUSH_COLOR), |
|
38 | m_brush(DEFAULT_BRUSH_COLOR), | |
36 | m_labelPen(DEFAULT_PEN_COLOR), |
|
39 | m_labelPen(DEFAULT_PEN_COLOR), | |
@@ -64,6 +67,11 bool QPieSlice::isExploded() const | |||||
64 | return m_isExploded; |
|
67 | return m_isExploded; | |
65 | } |
|
68 | } | |
66 |
|
69 | |||
|
70 | qreal QPieSlice::explodeDistance() const | |||
|
71 | { | |||
|
72 | return m_explodeDistance; | |||
|
73 | } | |||
|
74 | ||||
67 | qreal QPieSlice::percentage() const |
|
75 | qreal QPieSlice::percentage() const | |
68 | { |
|
76 | { | |
69 | return m_percentage; |
|
77 | return m_percentage; | |
@@ -74,9 +82,9 qreal QPieSlice::angle() const | |||||
74 | return m_angle; |
|
82 | return m_angle; | |
75 | } |
|
83 | } | |
76 |
|
84 | |||
77 |
qreal QPieSlice:: |
|
85 | qreal QPieSlice::angleSpan() const | |
78 | { |
|
86 | { | |
79 |
return m_ |
|
87 | return m_angleSpan; | |
80 | } |
|
88 | } | |
81 |
|
89 | |||
82 | QPen QPieSlice::pen() const |
|
90 | QPen QPieSlice::pen() const | |
@@ -99,7 +107,7 QFont QPieSlice::labelFont() const | |||||
99 | return m_labelFont; |
|
107 | return m_labelFont; | |
100 | } |
|
108 | } | |
101 |
|
109 | |||
102 |
qreal QPieSlice::labelArmLeng |
|
110 | qreal QPieSlice::labelArmLength() const | |
103 | { |
|
111 | { | |
104 | return m_labelArmLength; |
|
112 | return m_labelArmLength; | |
105 | } |
|
113 | } | |
@@ -136,6 +144,14 void QPieSlice::setExploded(bool exploded) | |||||
136 | } |
|
144 | } | |
137 | } |
|
145 | } | |
138 |
|
146 | |||
|
147 | void QPieSlice::setExplodeDistance(qreal distance) | |||
|
148 | { | |||
|
149 | if (m_explodeDistance != distance) { | |||
|
150 | m_explodeDistance = distance; | |||
|
151 | emit changed(); | |||
|
152 | } | |||
|
153 | } | |||
|
154 | ||||
139 | void QPieSlice::setPen(QPen pen) |
|
155 | void QPieSlice::setPen(QPen pen) | |
140 | { |
|
156 | { | |
141 | if (m_pen != pen) { |
|
157 | if (m_pen != pen) { |
@@ -25,18 +25,19 public: | |||||
25 | QString label() const; |
|
25 | QString label() const; | |
26 | bool isLabelVisible() const; |
|
26 | bool isLabelVisible() const; | |
27 | bool isExploded() const; |
|
27 | bool isExploded() const; | |
|
28 | qreal explodeDistance() const; | |||
28 |
|
29 | |||
29 | // generated data |
|
30 | // generated data | |
30 | qreal percentage() const; |
|
31 | qreal percentage() const; | |
31 | qreal angle() const; |
|
32 | qreal angle() const; | |
32 |
qreal |
|
33 | qreal angleSpan() const; | |
33 |
|
34 | |||
34 | // customization |
|
35 | // customization | |
35 | QPen pen() const; |
|
36 | QPen pen() const; | |
36 | QBrush brush() const; |
|
37 | QBrush brush() const; | |
37 | QPen labelPen() const; |
|
38 | QPen labelPen() const; | |
38 | QFont labelFont() const; |
|
39 | QFont labelFont() const; | |
39 |
qreal labelArmLeng |
|
40 | qreal labelArmLength() const; | |
40 |
|
41 | |||
41 | Q_SIGNALS: |
|
42 | Q_SIGNALS: | |
42 | void clicked(); |
|
43 | void clicked(); | |
@@ -51,6 +52,7 public Q_SLOTS: | |||||
51 | void setLabelVisible(bool visible); |
|
52 | void setLabelVisible(bool visible); | |
52 | void setValue(qreal value); |
|
53 | void setValue(qreal value); | |
53 | void setExploded(bool exploded); |
|
54 | void setExploded(bool exploded); | |
|
55 | void setExplodeDistance(qreal distance); | |||
54 |
|
56 | |||
55 | // customization |
|
57 | // customization | |
56 | void setPen(QPen pen); |
|
58 | void setPen(QPen pen); | |
@@ -59,21 +61,27 public Q_SLOTS: | |||||
59 | void setLabelPen(QPen pen); |
|
61 | void setLabelPen(QPen pen); | |
60 | void setLabelArmLength(qreal len); |
|
62 | void setLabelArmLength(qreal len); | |
61 |
|
63 | |||
|
64 | // TODO: label position in general | |||
|
65 | // setLabelFlags(inside|outside|labelArmOn|labelArmOff|???) | |||
|
66 | // setLabelOrientation(horizontal|vertical|same as slice center angle|???) | |||
|
67 | ||||
62 | private: |
|
68 | private: | |
63 |
|
69 | |||
64 | // TODO: use private class |
|
70 | // TODO: use private class | |
65 | friend class QPieSeries; |
|
71 | friend class QPieSeries; | |
|
72 | friend class PiePresenter; | |||
66 |
|
73 | |||
67 | // data |
|
74 | // data | |
68 | qreal m_value; |
|
75 | qreal m_value; | |
69 | QString m_label; |
|
76 | QString m_label; | |
70 | bool m_isLabelVisible; |
|
77 | bool m_isLabelVisible; | |
71 | bool m_isExploded; |
|
78 | bool m_isExploded; | |
|
79 | qreal m_explodeDistance; | |||
72 |
|
80 | |||
73 | // generated data |
|
81 | // generated data | |
74 | qreal m_percentage; |
|
82 | qreal m_percentage; | |
75 | qreal m_angle; |
|
83 | qreal m_angle; | |
76 |
qreal m_ |
|
84 | qreal m_angleSpan; | |
77 |
|
85 | |||
78 | // customization |
|
86 | // customization | |
79 | QPen m_pen; |
|
87 | QPen m_pen; |
General Comments 0
You need to be logged in to leave comments.
Login now