@@ -1,210 +1,220 | |||||
1 |
|
1 | |||
2 | #include "piepresenter_p.h" |
|
2 | #include "piepresenter_p.h" | |
3 | #include "pieslice_p.h" |
|
3 | #include "pieslice_p.h" | |
4 | #include "qpieslice.h" |
|
4 | #include "qpieslice.h" | |
5 | #include "pieslicelabel_p.h" |
|
5 | #include "pieslicelabel_p.h" | |
6 | #include "qpieseries.h" |
|
6 | #include "qpieseries.h" | |
7 | #include <qmath.h> |
|
7 | #include <qmath.h> | |
8 | #include <QDebug> |
|
8 | #include <QDebug> | |
9 | #include <QFontMetrics> |
|
9 | #include <QFontMetrics> | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 |
|
13 | |||
14 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) |
|
14 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) | |
15 | :ChartItem(parent), |
|
15 | :ChartItem(parent), | |
16 | m_series(series) |
|
16 | m_series(series) | |
17 | { |
|
17 | { | |
18 | Q_ASSERT(series); |
|
18 | Q_ASSERT(series); | |
19 | 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&))); | |
20 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); |
|
20 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); | |
21 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); |
|
21 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); | |
22 |
|
22 | |||
23 | if (m_series->count()) { |
|
23 | if (m_series->count()) { | |
24 | QPieSeries::ChangeSet changeSet; |
|
24 | QPieSeries::ChangeSet changeSet; | |
25 | changeSet.appendAdded(m_series->m_slices); |
|
25 | changeSet.appendAdded(m_series->m_slices); | |
26 | handleSeriesChanged(changeSet); |
|
26 | handleSeriesChanged(changeSet); | |
27 | } |
|
27 | } | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | PiePresenter::~PiePresenter() |
|
30 | PiePresenter::~PiePresenter() | |
31 | { |
|
31 | { | |
32 | // slices deleted automatically through QGraphicsItem |
|
32 | // slices deleted automatically through QGraphicsItem | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | void PiePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
35 | void PiePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | |
36 | { |
|
36 | { | |
37 | // TODO: paint shadows for all components |
|
37 | // TODO: paint shadows for all components | |
38 | // - get paths from items & merge & offset and draw with shadow color? |
|
38 | // - get paths from items & merge & offset and draw with shadow color? | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) |
|
41 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) | |
42 | { |
|
42 | { | |
43 | //qDebug() << "PiePresenter::handleSeriesChanged()"; |
|
43 | //qDebug() << "PiePresenter::handleSeriesChanged()"; | |
44 | //qDebug() << " added : " << changeSet.added(); |
|
44 | //qDebug() << " added : " << changeSet.added(); | |
45 | //qDebug() << " changed: " << changeSet.changed(); |
|
45 | //qDebug() << " changed: " << changeSet.changed(); | |
46 | //qDebug() << " removed: " << changeSet.removed(); |
|
46 | //qDebug() << " removed: " << changeSet.removed(); | |
47 |
|
47 | |||
48 | foreach (QPieSlice* s, changeSet.added()) |
|
48 | foreach (QPieSlice* s, changeSet.added()) | |
49 | addSlice(s); |
|
49 | addSlice(s); | |
50 |
|
50 | |||
51 | foreach (QPieSlice* s, changeSet.changed()) |
|
51 | foreach (QPieSlice* s, changeSet.changed()) | |
52 | updateSlice(s); |
|
52 | updateSlice(s); | |
53 |
|
53 | |||
54 | foreach (QPieSlice* s, changeSet.removed()) |
|
54 | foreach (QPieSlice* s, changeSet.removed()) | |
55 | deleteSlice(s); |
|
55 | deleteSlice(s); | |
56 |
|
56 | |||
57 | // every change possibly changes the actual pie size |
|
57 | // every change possibly changes the actual pie size | |
58 | updateGeometry(); |
|
58 | updateGeometry(); | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | void PiePresenter::handleDomainChanged(const Domain& domain) |
|
61 | void PiePresenter::handleDomainChanged(const Domain& domain) | |
62 | { |
|
62 | { | |
63 | // TODO |
|
63 | // TODO | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | void PiePresenter::handleGeometryChanged(const QRectF& rect) |
|
66 | void PiePresenter::handleGeometryChanged(const QRectF& rect) | |
67 | { |
|
67 | { | |
68 | m_rect = rect; |
|
68 | m_rect = rect; | |
69 | prepareGeometryChange(); |
|
69 | prepareGeometryChange(); | |
70 | updateGeometry(); |
|
70 | updateGeometry(); | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | void PiePresenter::updateGeometry() |
|
73 | void PiePresenter::updateGeometry() | |
74 | { |
|
74 | { | |
75 | if (!m_rect.isValid() || m_rect.isEmpty()) |
|
75 | if (!m_rect.isValid() || m_rect.isEmpty()) | |
76 | return; |
|
76 | return; | |
77 |
|
77 | |||
78 | // calculate maximum rectangle for pie |
|
78 | // calculate maximum rectangle for pie | |
79 | QRectF pieRect = m_rect; |
|
79 | QRectF pieRect = m_rect; | |
80 | if (pieRect.width() < pieRect.height()) { |
|
80 | if (pieRect.width() < pieRect.height()) { | |
81 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); |
|
81 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); | |
82 | pieRect.setHeight(pieRect.width()); |
|
82 | pieRect.setHeight(pieRect.width()); | |
83 | pieRect.moveCenter(m_rect.center()); |
|
83 | pieRect.moveCenter(m_rect.center()); | |
84 | } else { |
|
84 | } else { | |
85 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); |
|
85 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); | |
86 | pieRect.setWidth(pieRect.height()); |
|
86 | pieRect.setWidth(pieRect.height()); | |
87 | pieRect.moveCenter(m_rect.center()); |
|
87 | pieRect.moveCenter(m_rect.center()); | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | // position the pie rectangle |
|
90 | // position the pie rectangle | |
91 | switch (m_series->position()) { |
|
91 | switch (m_series->position()) { | |
92 | case QPieSeries::PiePositionTopLeft: { |
|
92 | case QPieSeries::PiePositionTopLeft: { | |
93 | pieRect.setHeight(pieRect.height() / 2); |
|
93 | pieRect.setHeight(pieRect.height() / 2); | |
94 | pieRect.setWidth(pieRect.height()); |
|
94 | pieRect.setWidth(pieRect.height()); | |
95 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); |
|
95 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); | |
96 | break; |
|
96 | break; | |
97 | } |
|
97 | } | |
98 | case QPieSeries::PiePositionTopRight: { |
|
98 | case QPieSeries::PiePositionTopRight: { | |
99 | pieRect.setHeight(pieRect.height() / 2); |
|
99 | pieRect.setHeight(pieRect.height() / 2); | |
100 | pieRect.setWidth(pieRect.height()); |
|
100 | pieRect.setWidth(pieRect.height()); | |
101 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); |
|
101 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); | |
102 | break; |
|
102 | break; | |
103 | } |
|
103 | } | |
104 | case QPieSeries::PiePositionBottomLeft: { |
|
104 | case QPieSeries::PiePositionBottomLeft: { | |
105 | pieRect.setHeight(pieRect.height() / 2); |
|
105 | pieRect.setHeight(pieRect.height() / 2); | |
106 | pieRect.setWidth(pieRect.height()); |
|
106 | pieRect.setWidth(pieRect.height()); | |
107 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); |
|
107 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); | |
108 | break; |
|
108 | break; | |
109 | } |
|
109 | } | |
110 | case QPieSeries::PiePositionBottomRight: { |
|
110 | case QPieSeries::PiePositionBottomRight: { | |
111 | pieRect.setHeight(pieRect.height() / 2); |
|
111 | pieRect.setHeight(pieRect.height() / 2); | |
112 | pieRect.setWidth(pieRect.height()); |
|
112 | pieRect.setWidth(pieRect.height()); | |
113 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); |
|
113 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); | |
114 | break; |
|
114 | break; | |
115 | } |
|
115 | } | |
116 | default: |
|
116 | default: | |
117 | break; |
|
117 | break; | |
118 | } |
|
118 | } | |
119 |
|
119 | |||
120 | // calculate how much space we need around the pie rectangle (labels & exploding) |
|
120 | // calculate how much space we need around the pie rectangle (labels & exploding) | |
121 | qreal delta = 0; |
|
121 | qreal delta = 0; | |
122 | qreal pieRadius = pieRect.height() / 2; |
|
122 | qreal pieRadius = pieRect.height() / 2; | |
123 | foreach (QPieSlice* s, m_series->m_slices) { |
|
123 | foreach (QPieSlice* s, m_series->m_slices) { | |
124 |
|
124 | |||
125 | // calculate the farthest point in the slice from the pie center |
|
125 | // calculate the farthest point in the slice from the pie center | |
|
126 | ||||
|
127 | // the arm | |||
126 | qreal centerAngle = s->m_startAngle + (s->m_angleSpan / 2); |
|
128 | qreal centerAngle = s->m_startAngle + (s->m_angleSpan / 2); | |
127 | qreal len = pieRadius + s->labelArmLength() + s->explodeDistance(); |
|
129 | qreal len = pieRadius + PIESLICE_LABEL_GAP + s->labelArmLength() + s->explodeDistance(); | |
128 | QPointF dp(qSin(centerAngle*(PI/180)) * len, -qCos(centerAngle*(PI/180)) * len); |
|
130 | QPointF dp(qSin(centerAngle*(PI/180)) * len, -qCos(centerAngle*(PI/180)) * len); | |
129 | QPointF p = pieRect.center() + dp; |
|
131 | QPointF p = pieRect.center() + dp; | |
130 |
|
132 | |||
131 |
// |
|
133 | // the label text | |
|
134 | QFontMetricsF fm(s->labelFont()); | |||
|
135 | QRectF labelRect = fm.boundingRect(s->label()); | |||
|
136 | if (centerAngle < 90 || centerAngle > 270) | |||
|
137 | p += QPointF(0, -labelRect.height()); | |||
|
138 | if (centerAngle < 180) | |||
|
139 | p += QPointF(labelRect.width(), 0); | |||
|
140 | else | |||
|
141 | p += QPointF(-labelRect.width(), 0); | |||
132 |
|
142 | |||
133 | // calculate how much the radius must get smaller to fit that point in the base rectangle |
|
143 | // calculate how much the radius must get smaller to fit that point in the base rectangle | |
134 | qreal dt = m_rect.top() - p.y(); |
|
144 | qreal dt = m_rect.top() - p.y(); | |
135 | if (dt > delta) delta = dt; |
|
145 | if (dt > delta) delta = dt; | |
136 | qreal dl = m_rect.left() - p.x(); |
|
146 | qreal dl = m_rect.left() - p.x(); | |
137 | if (dl > delta) delta = dl; |
|
147 | if (dl > delta) delta = dl; | |
138 | qreal dr = p.x() - m_rect.right(); |
|
148 | qreal dr = p.x() - m_rect.right(); | |
139 | if (dr > delta) delta = dr; |
|
149 | if (dr > delta) delta = dr; | |
140 | qreal db = p.y() - m_rect.bottom(); |
|
150 | qreal db = p.y() - m_rect.bottom(); | |
141 | if (db > delta) delta = db; |
|
151 | if (db > delta) delta = db; | |
142 |
|
152 | |||
143 | //if (!m_rect.contains(p)) qDebug() << s->label() << dt << dl << dr << db << "delta" << delta; |
|
153 | //if (!m_rect.contains(p)) qDebug() << s->label() << dt << dl << dr << db << "delta" << delta; | |
144 | } |
|
154 | } | |
145 |
|
155 | |||
146 | // shrink the pie rectangle so that everything outside it fits the base rectangle |
|
156 | // shrink the pie rectangle so that everything outside it fits the base rectangle | |
147 | pieRect.adjust(delta, delta, -delta, -delta); |
|
157 | pieRect.adjust(delta, delta, -delta, -delta); | |
148 |
|
158 | |||
149 | // update slices |
|
159 | // update slices | |
150 | if (m_pieRect != pieRect) { |
|
160 | if (m_pieRect != pieRect) { | |
151 | m_pieRect = pieRect; |
|
161 | m_pieRect = pieRect; | |
152 | //qDebug() << "PiePresenter::updateGeometry()" << m_rect << m_pieRect; |
|
162 | //qDebug() << "PiePresenter::updateGeometry()" << m_rect << m_pieRect; | |
153 | foreach (PieSlice* s, m_slices.values()) { |
|
163 | foreach (PieSlice* s, m_slices.values()) { | |
154 | s->setPieRect(m_pieRect); |
|
164 | s->setPieRect(m_pieRect); | |
155 | s->updateGeometry(); |
|
165 | s->updateGeometry(); | |
156 | s->update(); |
|
166 | s->update(); | |
157 | } |
|
167 | } | |
158 | } |
|
168 | } | |
159 | } |
|
169 | } | |
160 |
|
170 | |||
161 | void PiePresenter::addSlice(QPieSlice* sliceData) |
|
171 | void PiePresenter::addSlice(QPieSlice* sliceData) | |
162 | { |
|
172 | { | |
163 | //qDebug() << "PiePresenter::addSlice()" << sliceData; |
|
173 | //qDebug() << "PiePresenter::addSlice()" << sliceData; | |
164 |
|
174 | |||
165 | if (m_slices.keys().contains(sliceData)) { |
|
175 | if (m_slices.keys().contains(sliceData)) { | |
166 | Q_ASSERT(0); // TODO: how to handle this nicely? |
|
176 | Q_ASSERT(0); // TODO: how to handle this nicely? | |
167 | return; |
|
177 | return; | |
168 | } |
|
178 | } | |
169 |
|
179 | |||
170 | // create slice |
|
180 | // create slice | |
171 | PieSlice *slice = new PieSlice(this); |
|
181 | PieSlice *slice = new PieSlice(this); | |
172 | slice->setPieRect(m_pieRect); |
|
182 | slice->setPieRect(m_pieRect); | |
173 | slice->updateData(sliceData); |
|
183 | slice->updateData(sliceData); | |
174 | slice->updateGeometry(); |
|
184 | slice->updateGeometry(); | |
175 | slice->update(); |
|
185 | slice->update(); | |
176 | m_slices.insert(sliceData, slice); |
|
186 | m_slices.insert(sliceData, slice); | |
177 |
|
187 | |||
178 | // connect signals |
|
188 | // connect signals | |
179 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); |
|
189 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); | |
180 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); |
|
190 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); | |
181 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); |
|
191 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); | |
182 | } |
|
192 | } | |
183 |
|
193 | |||
184 | void PiePresenter::updateSlice(QPieSlice* sliceData) |
|
194 | void PiePresenter::updateSlice(QPieSlice* sliceData) | |
185 | { |
|
195 | { | |
186 | //qDebug() << "PiePresenter::updateSlice()" << sliceData; |
|
196 | //qDebug() << "PiePresenter::updateSlice()" << sliceData; | |
187 |
|
197 | |||
188 | if (!m_slices.contains(sliceData)) { |
|
198 | if (!m_slices.contains(sliceData)) { | |
189 | Q_ASSERT(0); // TODO: how to handle this nicely? |
|
199 | Q_ASSERT(0); // TODO: how to handle this nicely? | |
190 | return; |
|
200 | return; | |
191 | } |
|
201 | } | |
192 |
|
202 | |||
193 | m_slices[sliceData]->updateData(sliceData); |
|
203 | m_slices[sliceData]->updateData(sliceData); | |
194 | } |
|
204 | } | |
195 |
|
205 | |||
196 | void PiePresenter::deleteSlice(QPieSlice* sliceData) |
|
206 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | |
197 | { |
|
207 | { | |
198 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; |
|
208 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | |
199 |
|
209 | |||
200 | if (!m_slices.contains(sliceData)) { |
|
210 | if (!m_slices.contains(sliceData)) { | |
201 | Q_ASSERT(0); // TODO: how to handle this nicely? |
|
211 | Q_ASSERT(0); // TODO: how to handle this nicely? | |
202 | return; |
|
212 | return; | |
203 | } |
|
213 | } | |
204 |
|
214 | |||
205 | delete m_slices.take(sliceData); |
|
215 | delete m_slices.take(sliceData); | |
206 | } |
|
216 | } | |
207 |
|
217 | |||
208 | #include "moc_piepresenter_p.cpp" |
|
218 | #include "moc_piepresenter_p.cpp" | |
209 |
|
219 | |||
210 | QTCOMMERCIALCHART_END_NAMESPACE |
|
220 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,135 +1,135 | |||||
1 | #include "pieslice_p.h" |
|
1 | #include "pieslice_p.h" | |
2 | #include "pieslicelabel_p.h" |
|
2 | #include "pieslicelabel_p.h" | |
3 | #include "piepresenter_p.h" |
|
3 | #include "piepresenter_p.h" | |
4 | #include "qpieseries.h" |
|
4 | #include "qpieseries.h" | |
5 | #include "qpieslice.h" |
|
5 | #include "qpieslice.h" | |
6 | #include <QPainter> |
|
6 | #include <QPainter> | |
7 | #include <QDebug> |
|
7 | #include <QDebug> | |
8 | #include <qmath.h> |
|
8 | #include <qmath.h> | |
9 | #include <QGraphicsSceneEvent> |
|
9 | #include <QGraphicsSceneEvent> | |
10 | #include <QTime> |
|
10 | #include <QTime> | |
11 |
|
11 | |||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 |
|
13 | |||
14 | QPointF offset(qreal angle, qreal length) |
|
14 | QPointF offset(qreal angle, qreal length) | |
15 | { |
|
15 | { | |
16 | qreal dx = qSin(angle*(PI/180)) * length; |
|
16 | qreal dx = qSin(angle*(PI/180)) * length; | |
17 | qreal dy = qCos(angle*(PI/180)) * length; |
|
17 | qreal dy = qCos(angle*(PI/180)) * length; | |
18 | return QPointF(dx, -dy); |
|
18 | return QPointF(dx, -dy); | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | PieSlice::PieSlice(QGraphicsItem* parent) |
|
21 | PieSlice::PieSlice(QGraphicsItem* parent) | |
22 | :QGraphicsObject(parent), |
|
22 | :QGraphicsObject(parent), | |
23 | m_slicelabel(new PieSliceLabel(this)), |
|
23 | m_slicelabel(new PieSliceLabel(this)), | |
24 | m_startAngle(0), |
|
24 | m_startAngle(0), | |
25 | m_angleSpan(0), |
|
25 | m_angleSpan(0), | |
26 | m_isExploded(false), |
|
26 | m_isExploded(false), | |
27 | m_explodeDistance(0) |
|
27 | m_explodeDistance(0) | |
28 | { |
|
28 | { | |
29 | setAcceptHoverEvents(true); |
|
29 | setAcceptHoverEvents(true); | |
30 | setAcceptedMouseButtons(Qt::LeftButton); |
|
30 | setAcceptedMouseButtons(Qt::LeftButton); | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | PieSlice::~PieSlice() |
|
33 | PieSlice::~PieSlice() | |
34 | { |
|
34 | { | |
35 |
|
35 | |||
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | QRectF PieSlice::boundingRect() const |
|
38 | QRectF PieSlice::boundingRect() const | |
39 | { |
|
39 | { | |
40 | return m_path.boundingRect(); |
|
40 | return m_path.boundingRect(); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | QPainterPath PieSlice::shape() const |
|
43 | QPainterPath PieSlice::shape() const | |
44 | { |
|
44 | { | |
45 | return m_path; |
|
45 | return m_path; | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
48 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | |
49 | { |
|
49 | { | |
50 | painter->setPen(m_pen); |
|
50 | painter->setPen(m_pen); | |
51 | painter->setBrush(m_brush); |
|
51 | painter->setBrush(m_brush); | |
52 | painter->drawPath(m_path); |
|
52 | painter->drawPath(m_path); | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
55 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | |
56 | { |
|
56 | { | |
57 | emit hoverEnter(); |
|
57 | emit hoverEnter(); | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
60 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | |
61 | { |
|
61 | { | |
62 | emit hoverLeave(); |
|
62 | emit hoverLeave(); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) |
|
65 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) | |
66 | { |
|
66 | { | |
67 | emit clicked(); |
|
67 | emit clicked(); | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | void PieSlice::setPieRect(QRectF rect) |
|
70 | void PieSlice::setPieRect(QRectF rect) | |
71 | { |
|
71 | { | |
72 | m_pieRect = rect; |
|
72 | m_pieRect = rect; | |
73 | } |
|
73 | } | |
74 |
|
74 | |||
75 | void PieSlice::updateGeometry() |
|
75 | void PieSlice::updateGeometry() | |
76 | { |
|
76 | { | |
77 | if (!m_pieRect.isValid() || m_pieRect.isEmpty()) |
|
77 | if (!m_pieRect.isValid() || m_pieRect.isEmpty()) | |
78 | return; |
|
78 | return; | |
79 |
|
79 | |||
80 | prepareGeometryChange(); |
|
80 | prepareGeometryChange(); | |
81 |
|
81 | |||
82 | // calculate center angle |
|
82 | // calculate center angle | |
83 | qreal centerAngle = m_startAngle + (m_angleSpan/2); |
|
83 | qreal centerAngle = m_startAngle + (m_angleSpan/2); | |
84 |
|
84 | |||
85 | // adjust rect for exploding |
|
85 | // adjust rect for exploding | |
86 | QRectF rect = m_pieRect; |
|
86 | QRectF rect = m_pieRect; | |
87 | if (m_isExploded) { |
|
87 | if (m_isExploded) { | |
88 | qreal dx = qSin(centerAngle*(PI/180)) * m_explodeDistance; |
|
88 | qreal dx = qSin(centerAngle*(PI/180)) * m_explodeDistance; | |
89 | qreal dy = -qCos(centerAngle*(PI/180)) * m_explodeDistance; |
|
89 | qreal dy = -qCos(centerAngle*(PI/180)) * m_explodeDistance; | |
90 | rect.translate(dx, dy); |
|
90 | rect.translate(dx, dy); | |
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 | // update slice path |
|
93 | // update slice path | |
94 | // 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 | |
95 | QPainterPath path; |
|
95 | QPainterPath path; | |
96 | path.moveTo(rect.center()); |
|
96 | path.moveTo(rect.center()); | |
97 | path.arcTo(rect, -m_startAngle + 90, -m_angleSpan); |
|
97 | path.arcTo(rect, -m_startAngle + 90, -m_angleSpan); | |
98 | path.closeSubpath(); |
|
98 | path.closeSubpath(); | |
99 | m_path = path; |
|
99 | m_path = path; | |
100 |
|
100 | |||
101 | // update label position |
|
101 | // update label position | |
102 | qreal radius = rect.height() / 2; |
|
102 | qreal radius = rect.height() / 2; | |
103 |
QPointF edgeCenter = rect.center() + offset(centerAngle, radius + |
|
103 | QPointF edgeCenter = rect.center() + offset(centerAngle, radius + PIESLICE_LABEL_GAP); | |
104 | m_slicelabel->setArmStartPoint(edgeCenter); |
|
104 | m_slicelabel->setArmStartPoint(edgeCenter); | |
105 | m_slicelabel->setArmAngle(centerAngle); |
|
105 | m_slicelabel->setArmAngle(centerAngle); | |
106 | m_slicelabel->updateGeometry(); |
|
106 | m_slicelabel->updateGeometry(); | |
107 | m_slicelabel->update(); |
|
107 | m_slicelabel->update(); | |
108 |
|
108 | |||
109 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; |
|
109 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 | void PieSlice::updateData(const QPieSlice* sliceData) |
|
112 | void PieSlice::updateData(const QPieSlice* sliceData) | |
113 | { |
|
113 | { | |
114 | // TODO: compare what has changes to avoid unneccesary geometry updates |
|
114 | // TODO: compare what has changes to avoid unneccesary geometry updates | |
115 |
|
115 | |||
116 | m_startAngle = sliceData->startAngle(); |
|
116 | m_startAngle = sliceData->startAngle(); | |
117 | m_angleSpan = sliceData->m_angleSpan; |
|
117 | m_angleSpan = sliceData->m_angleSpan; | |
118 | m_isExploded = sliceData->isExploded(); |
|
118 | m_isExploded = sliceData->isExploded(); | |
119 | m_explodeDistance = sliceData->explodeDistance(); // TODO: expose to public API |
|
119 | m_explodeDistance = sliceData->explodeDistance(); // TODO: expose to public API | |
120 | m_pen = sliceData->pen(); |
|
120 | m_pen = sliceData->pen(); | |
121 | m_brush = sliceData->brush(); |
|
121 | m_brush = sliceData->brush(); | |
122 |
|
122 | |||
123 | m_slicelabel->setVisible(sliceData->isLabelVisible()); |
|
123 | m_slicelabel->setVisible(sliceData->isLabelVisible()); | |
124 | m_slicelabel->setText(sliceData->label()); |
|
124 | m_slicelabel->setText(sliceData->label()); | |
125 | m_slicelabel->setPen(sliceData->labelPen()); |
|
125 | m_slicelabel->setPen(sliceData->labelPen()); | |
126 | m_slicelabel->setFont(sliceData->labelFont()); |
|
126 | m_slicelabel->setFont(sliceData->labelFont()); | |
127 | m_slicelabel->setArmLength(sliceData->labelArmLength()); |
|
127 | m_slicelabel->setArmLength(sliceData->labelArmLength()); | |
128 |
|
128 | |||
129 | updateGeometry(); |
|
129 | updateGeometry(); | |
130 | update(); |
|
130 | update(); | |
131 | } |
|
131 | } | |
132 |
|
132 | |||
133 | #include "moc_pieslice_p.cpp" |
|
133 | #include "moc_pieslice_p.cpp" | |
134 |
|
134 | |||
135 | QTCOMMERCIALCHART_END_NAMESPACE |
|
135 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,64 +1,66 | |||||
1 | #ifndef PIESLICE_H |
|
1 | #ifndef PIESLICE_H | |
2 | #define PIESLICE_H |
|
2 | #define PIESLICE_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "charttheme_p.h" |
|
5 | #include "charttheme_p.h" | |
6 | #include "qpieseries.h" |
|
6 | #include "qpieseries.h" | |
7 | #include <QGraphicsItem> |
|
7 | #include <QGraphicsItem> | |
8 | #include <QRectF> |
|
8 | #include <QRectF> | |
9 | #include <QColor> |
|
9 | #include <QColor> | |
10 | #include <QPen> |
|
10 | #include <QPen> | |
11 |
|
11 | |||
|
12 | #define PIESLICE_LABEL_GAP 5 | |||
|
13 | ||||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
14 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 | class PiePresenter; |
|
15 | class PiePresenter; | |
14 | class PieSliceLabel; |
|
16 | class PieSliceLabel; | |
15 | class QPieSlice; |
|
17 | class QPieSlice; | |
16 |
|
18 | |||
17 | class PieSlice : public QGraphicsObject |
|
19 | class PieSlice : public QGraphicsObject | |
18 | { |
|
20 | { | |
19 | Q_OBJECT |
|
21 | Q_OBJECT | |
20 |
|
22 | |||
21 | public: |
|
23 | public: | |
22 | PieSlice(QGraphicsItem* parent = 0); |
|
24 | PieSlice(QGraphicsItem* parent = 0); | |
23 | ~PieSlice(); |
|
25 | ~PieSlice(); | |
24 |
|
26 | |||
25 | public: // from QGraphicsItem |
|
27 | public: // from QGraphicsItem | |
26 | QRectF boundingRect() const; |
|
28 | QRectF boundingRect() const; | |
27 | QPainterPath shape() const; |
|
29 | QPainterPath shape() const; | |
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
29 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event); |
|
31 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event); | |
30 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); |
|
32 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); | |
31 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
33 | void mousePressEvent(QGraphicsSceneMouseEvent *event); | |
32 |
|
34 | |||
33 | Q_SIGNALS: |
|
35 | Q_SIGNALS: | |
34 | void clicked(); |
|
36 | void clicked(); | |
35 | void hoverEnter(); |
|
37 | void hoverEnter(); | |
36 | void hoverLeave(); |
|
38 | void hoverLeave(); | |
37 |
|
39 | |||
38 | public Q_SLOTS: |
|
40 | public Q_SLOTS: | |
39 | void setPieRect(QRectF rect); |
|
41 | void setPieRect(QRectF rect); | |
40 | void updateGeometry(); |
|
42 | void updateGeometry(); | |
41 | void updateData(const QPieSlice *sliceData); |
|
43 | void updateData(const QPieSlice *sliceData); | |
42 |
|
44 | |||
43 | public: |
|
45 | public: | |
44 | PieSliceLabel* label() { return m_slicelabel; } |
|
46 | PieSliceLabel* label() { return m_slicelabel; } | |
45 |
|
47 | |||
46 | private: |
|
48 | private: | |
47 | PieSliceLabel* m_slicelabel; |
|
49 | PieSliceLabel* m_slicelabel; | |
48 |
|
50 | |||
49 | QRectF m_pieRect; |
|
51 | QRectF m_pieRect; | |
50 | QPainterPath m_path; |
|
52 | QPainterPath m_path; | |
51 |
|
53 | |||
52 | qreal m_startAngle; |
|
54 | qreal m_startAngle; | |
53 | qreal m_angleSpan; |
|
55 | qreal m_angleSpan; | |
54 |
|
56 | |||
55 | bool m_isExploded; |
|
57 | bool m_isExploded; | |
56 | qreal m_explodeDistance; |
|
58 | qreal m_explodeDistance; | |
57 |
|
59 | |||
58 | QPen m_pen; |
|
60 | QPen m_pen; | |
59 | QBrush m_brush; |
|
61 | QBrush m_brush; | |
60 | }; |
|
62 | }; | |
61 |
|
63 | |||
62 | QTCOMMERCIALCHART_END_NAMESPACE |
|
64 | QTCOMMERCIALCHART_END_NAMESPACE | |
63 |
|
65 | |||
64 | #endif // PIESLICE_H |
|
66 | #endif // PIESLICE_H |
General Comments 0
You need to be logged in to leave comments.
Login now