@@ -1,156 +1,158 | |||||
1 |
|
1 | |||
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 <QDebug> |
|
5 | #include <QDebug> | |
6 | #include <QTime> |
|
6 | #include <QTime> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) |
|
10 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) | |
11 | :ChartItem(parent), |
|
11 | :ChartItem(parent), | |
12 | m_series(series) |
|
12 | m_series(series) | |
13 | { |
|
13 | { | |
14 | Q_ASSERT(series); |
|
14 | Q_ASSERT(series); | |
15 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); |
|
15 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); | |
16 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); |
|
16 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); | |
17 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); |
|
17 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | PiePresenter::~PiePresenter() |
|
20 | PiePresenter::~PiePresenter() | |
21 | { |
|
21 | { | |
22 | // slices deleted automatically through QGraphicsItem |
|
22 | // slices deleted automatically through QGraphicsItem | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | void PiePresenter::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) |
|
25 | void PiePresenter::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) | |
26 | { |
|
26 | { | |
27 | // TODO: paint shadows for all components |
|
27 | // TODO: paint shadows for all components | |
28 | // - get paths from items & merge & offset and draw with shadow color? |
|
28 | // - get paths from items & merge & offset and draw with shadow color? | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) |
|
31 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) | |
32 | { |
|
32 | { | |
33 | //qDebug() << "PiePresenter::handleSeriesChanged()"; |
|
33 | //qDebug() << "PiePresenter::handleSeriesChanged()"; | |
34 | //qDebug() << " added : " << changeSet.added(); |
|
34 | //qDebug() << " added : " << changeSet.added(); | |
35 | //qDebug() << " changed: " << changeSet.changed(); |
|
35 | //qDebug() << " changed: " << changeSet.changed(); | |
36 | //qDebug() << " removed: " << changeSet.removed(); |
|
36 | //qDebug() << " removed: " << changeSet.removed(); | |
37 |
|
37 | |||
38 | // ignore changeset when there are no visual slices |
|
38 | // ignore changeset when there are no visual slices | |
39 | // changeset might not be valid about the added slices |
|
39 | // changeset might not be valid about the added slices | |
40 | if (m_slices.count() == 0) { |
|
40 | if (m_slices.count() == 0) { | |
41 | foreach (QPieSlice* s, m_series->m_slices) |
|
41 | foreach (QPieSlice* s, m_series->m_slices) | |
42 | addSlice(s); |
|
42 | addSlice(s); | |
43 | return; |
|
43 | return; | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | foreach (QPieSlice* s, changeSet.removed()) |
|
46 | foreach (QPieSlice* s, changeSet.removed()) | |
47 | deleteSlice(s); |
|
47 | deleteSlice(s); | |
48 |
|
48 | |||
49 | foreach (QPieSlice* s, changeSet.added()) |
|
49 | foreach (QPieSlice* s, changeSet.added()) | |
50 | addSlice(s); |
|
50 | addSlice(s); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | void PiePresenter::handleDomainChanged(const Domain& domain) |
|
53 | void PiePresenter::handleDomainChanged(const Domain& domain) | |
54 | { |
|
54 | { | |
55 | // TODO |
|
55 | // TODO | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void PiePresenter::handleGeometryChanged(const QRectF& rect) |
|
58 | void PiePresenter::handleGeometryChanged(const QRectF& rect) | |
59 | { |
|
59 | { | |
60 | m_rect = rect; |
|
60 | m_rect = rect; | |
61 | updateGeometry(); |
|
61 | updateGeometry(); | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | void PiePresenter::updateGeometry() |
|
64 | void PiePresenter::updateGeometry() | |
65 | { |
|
65 | { | |
66 | prepareGeometryChange(); |
|
66 | prepareGeometryChange(); | |
67 |
|
67 | |||
68 | QRectF pieRect = m_rect; |
|
68 | QRectF pieRect = m_rect; | |
69 |
|
69 | |||
70 | if (pieRect.width() < pieRect.height()) { |
|
70 | if (pieRect.width() < pieRect.height()) { | |
71 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); |
|
71 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); | |
72 | pieRect.setHeight(pieRect.width()); |
|
72 | pieRect.setHeight(pieRect.width()); | |
73 | pieRect.moveCenter(m_rect.center()); |
|
73 | pieRect.moveCenter(m_rect.center()); | |
74 | } else { |
|
74 | } else { | |
75 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); |
|
75 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); | |
76 | pieRect.setWidth(pieRect.height()); |
|
76 | pieRect.setWidth(pieRect.height()); | |
77 | pieRect.moveCenter(m_rect.center()); |
|
77 | pieRect.moveCenter(m_rect.center()); | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | switch (m_series->position()) { |
|
80 | switch (m_series->position()) { | |
81 | case QPieSeries::PiePositionTopLeft: { |
|
81 | case QPieSeries::PiePositionTopLeft: { | |
82 | pieRect.setHeight(pieRect.height() / 2); |
|
82 | pieRect.setHeight(pieRect.height() / 2); | |
83 | pieRect.setWidth(pieRect.height()); |
|
83 | pieRect.setWidth(pieRect.height()); | |
84 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); |
|
84 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); | |
85 | break; |
|
85 | break; | |
86 | } |
|
86 | } | |
87 | case QPieSeries::PiePositionTopRight: { |
|
87 | case QPieSeries::PiePositionTopRight: { | |
88 | pieRect.setHeight(pieRect.height() / 2); |
|
88 | pieRect.setHeight(pieRect.height() / 2); | |
89 | pieRect.setWidth(pieRect.height()); |
|
89 | pieRect.setWidth(pieRect.height()); | |
90 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); |
|
90 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); | |
91 | break; |
|
91 | break; | |
92 | } |
|
92 | } | |
93 | case QPieSeries::PiePositionBottomLeft: { |
|
93 | case QPieSeries::PiePositionBottomLeft: { | |
94 | pieRect.setHeight(pieRect.height() / 2); |
|
94 | pieRect.setHeight(pieRect.height() / 2); | |
95 | pieRect.setWidth(pieRect.height()); |
|
95 | pieRect.setWidth(pieRect.height()); | |
96 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); |
|
96 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); | |
97 | break; |
|
97 | break; | |
98 | } |
|
98 | } | |
99 | case QPieSeries::PiePositionBottomRight: { |
|
99 | case QPieSeries::PiePositionBottomRight: { | |
100 | pieRect.setHeight(pieRect.height() / 2); |
|
100 | pieRect.setHeight(pieRect.height() / 2); | |
101 | pieRect.setWidth(pieRect.height()); |
|
101 | pieRect.setWidth(pieRect.height()); | |
102 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); |
|
102 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); | |
103 | break; |
|
103 | break; | |
104 | } |
|
104 | } | |
105 | default: |
|
105 | default: | |
106 | break; |
|
106 | break; | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | if (m_pieRect != pieRect) { |
|
109 | if (m_pieRect != pieRect) { | |
110 | m_pieRect = pieRect; |
|
110 | m_pieRect = pieRect; | |
111 | //qDebug() << "PiePresenter::updateGeometry()" << m_pieRect; |
|
111 | //qDebug() << "PiePresenter::updateGeometry()" << m_pieRect; | |
112 | foreach (PieSlice* s, m_slices.values()) { |
|
112 | foreach (PieSlice* s, m_slices.values()) { | |
113 | s->setPieRect(m_pieRect); |
|
113 | s->setPieRect(m_pieRect); | |
114 | s->updateGeometry(); |
|
114 | s->updateGeometry(); | |
115 | } |
|
115 | } | |
116 | } |
|
116 | } | |
117 | } |
|
117 | } | |
118 |
|
118 | |||
119 | void PiePresenter::addSlice(QPieSlice* sliceData) |
|
119 | void PiePresenter::addSlice(QPieSlice* sliceData) | |
120 | { |
|
120 | { | |
121 | //qDebug() << "PiePresenter::addSlice()" << sliceData; |
|
121 | //qDebug() << "PiePresenter::addSlice()" << sliceData; | |
122 |
|
122 | |||
123 | if (m_slices.keys().contains(sliceData)) { |
|
123 | if (m_slices.keys().contains(sliceData)) { | |
124 | //qWarning() << "PiePresenter::addSlice(): slice already exists!" << sliceData; |
|
124 | //qWarning() << "PiePresenter::addSlice(): slice already exists!" << sliceData; | |
125 | Q_ASSERT(0); |
|
125 | Q_ASSERT(0); | |
126 | return; |
|
126 | return; | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | // create slice |
|
129 | // create slice | |
130 | PieSlice *slice = new PieSlice(this); |
|
130 | PieSlice *slice = new PieSlice(this); | |
131 | slice->setPieRect(m_pieRect); |
|
131 | slice->setPieRect(m_pieRect); | |
132 | slice->updateData(sliceData); |
|
132 | slice->updateData(sliceData); | |
|
133 | slice->updateGeometry(); | |||
|
134 | slice->update(); | |||
133 | m_slices.insert(sliceData, slice); |
|
135 | m_slices.insert(sliceData, slice); | |
134 |
|
136 | |||
135 | // connect signals |
|
137 | // connect signals | |
136 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); |
|
138 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); | |
137 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); |
|
139 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); | |
138 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); |
|
140 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); | |
139 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); |
|
141 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); | |
140 | } |
|
142 | } | |
141 |
|
143 | |||
142 | void PiePresenter::deleteSlice(QPieSlice* sliceData) |
|
144 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | |
143 | { |
|
145 | { | |
144 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; |
|
146 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | |
145 |
|
147 | |||
146 | if (m_slices.contains(sliceData)) |
|
148 | if (m_slices.contains(sliceData)) | |
147 | delete m_slices.take(sliceData); |
|
149 | delete m_slices.take(sliceData); | |
148 | else { |
|
150 | else { | |
149 | // nothing to remove |
|
151 | // nothing to remove | |
150 | Q_ASSERT(0); // TODO: remove before release |
|
152 | Q_ASSERT(0); // TODO: remove before release | |
151 | } |
|
153 | } | |
152 | } |
|
154 | } | |
153 |
|
155 | |||
154 | #include "moc_piepresenter.cpp" |
|
156 | #include "moc_piepresenter.cpp" | |
155 |
|
157 | |||
156 | QTCOMMERCIALCHART_END_NAMESPACE |
|
158 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,143 +1,141 | |||||
1 | #include "pieslice.h" |
|
1 | #include "pieslice.h" | |
2 | #include "pieslicelabel.h" |
|
2 | #include "pieslicelabel.h" | |
3 | #include "piepresenter.h" |
|
3 | #include "piepresenter.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 | #define PI 3.14159265 |
|
14 | #define PI 3.14159265 | |
15 | #define EXPLODE_OFFSET 20 |
|
15 | #define EXPLODE_OFFSET 20 | |
16 |
|
16 | |||
17 | QPointF offset(qreal angle, qreal length) |
|
17 | QPointF offset(qreal angle, qreal length) | |
18 | { |
|
18 | { | |
19 | qreal dx = qSin(angle*(PI/180)) * length; |
|
19 | qreal dx = qSin(angle*(PI/180)) * length; | |
20 | qreal dy = qCos(angle*(PI/180)) * length; |
|
20 | qreal dy = qCos(angle*(PI/180)) * length; | |
21 | return QPointF(dx, -dy); |
|
21 | return QPointF(dx, -dy); | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | PieSlice::PieSlice(QGraphicsItem* parent) |
|
24 | PieSlice::PieSlice(QGraphicsItem* parent) | |
25 | :QGraphicsObject(parent), |
|
25 | :QGraphicsObject(parent), | |
26 | m_slicelabel(new PieSliceLabel(this)), |
|
26 | m_slicelabel(new PieSliceLabel(this)), | |
27 | m_angle(0), |
|
27 | m_angle(0), | |
28 | m_span(0), |
|
28 | m_span(0), | |
29 | m_isExploded(false) |
|
29 | m_isExploded(false) | |
30 | { |
|
30 | { | |
31 | setAcceptHoverEvents(true); |
|
31 | setAcceptHoverEvents(true); | |
32 | setAcceptedMouseButtons(Qt::LeftButton); |
|
32 | setAcceptedMouseButtons(Qt::LeftButton); | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | PieSlice::~PieSlice() |
|
35 | PieSlice::~PieSlice() | |
36 | { |
|
36 | { | |
37 |
|
37 | |||
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | QRectF PieSlice::boundingRect() const |
|
40 | QRectF PieSlice::boundingRect() const | |
41 | { |
|
41 | { | |
42 | return m_path.boundingRect(); |
|
42 | return m_path.boundingRect(); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | QPainterPath PieSlice::shape() const |
|
45 | QPainterPath PieSlice::shape() const | |
46 | { |
|
46 | { | |
47 | return m_path; |
|
47 | return m_path; | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
50 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | |
51 | { |
|
51 | { | |
52 | painter->setRenderHint(QPainter::Antialiasing); |
|
52 | painter->setRenderHint(QPainter::Antialiasing); | |
53 | painter->setPen(m_pen); |
|
53 | painter->setPen(m_pen); | |
54 | painter->setBrush(m_brush); |
|
54 | painter->setBrush(m_brush); | |
55 | painter->drawPath(m_path); |
|
55 | painter->drawPath(m_path); | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
58 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | |
59 | { |
|
59 | { | |
60 | emit hoverEnter(); |
|
60 | emit hoverEnter(); | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
63 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | |
64 | { |
|
64 | { | |
65 | emit hoverLeave(); |
|
65 | emit hoverLeave(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) |
|
68 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) | |
69 | { |
|
69 | { | |
70 | emit clicked(); |
|
70 | emit clicked(); | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | void PieSlice::setPieRect(QRectF rect) |
|
73 | void PieSlice::setPieRect(QRectF rect) | |
74 | { |
|
74 | { | |
75 | m_pieRect = rect; |
|
75 | m_pieRect = rect; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | void PieSlice::updateGeometry() |
|
78 | void PieSlice::updateGeometry() | |
79 | { |
|
79 | { | |
80 | prepareGeometryChange(); |
|
80 | prepareGeometryChange(); | |
81 |
|
81 | |||
82 | // calculate center angle |
|
82 | // calculate center angle | |
83 | qreal centerAngle = m_angle + (m_span/2); |
|
83 | qreal centerAngle = m_angle + (m_span/2); | |
84 |
|
84 | |||
85 | // adjust rect for exploding |
|
85 | // adjust rect for exploding | |
86 | QRectF rect = m_pieRect; |
|
86 | QRectF rect = m_pieRect; | |
87 | rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET); |
|
87 | rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET); | |
88 | if (m_isExploded) { |
|
88 | if (m_isExploded) { | |
89 | QPointF d = offset((centerAngle), EXPLODE_OFFSET); |
|
89 | QPointF d = offset((centerAngle), EXPLODE_OFFSET); | |
90 | rect.translate(d.x(), d.y()); |
|
90 | rect.translate(d.x(), d.y()); | |
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_angle + 90, -m_span); |
|
97 | path.arcTo(rect, -m_angle + 90, -m_span); | |
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 + 5); |
|
103 | QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); | |
104 |
|
||||
105 | m_slicelabel->setArmStartPoint(edgeCenter); |
|
104 | m_slicelabel->setArmStartPoint(edgeCenter); | |
106 | m_slicelabel->setArmAngle(centerAngle); |
|
105 | m_slicelabel->setArmAngle(centerAngle); | |
107 | m_slicelabel->updateGeometry(); |
|
106 | m_slicelabel->updateGeometry(); | |
108 |
|
107 | |||
109 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; |
|
108 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; | |
110 | } |
|
109 | } | |
111 |
|
110 | |||
112 | void PieSlice::handleSliceDataChanged() |
|
111 | void PieSlice::handleSliceDataChanged() | |
113 | { |
|
112 | { | |
114 | QPieSlice *slice = qobject_cast<QPieSlice*>(sender()); |
|
113 | QPieSlice *slice = qobject_cast<QPieSlice*>(sender()); | |
115 | Q_ASSERT(slice); |
|
114 | Q_ASSERT(slice); | |
116 | updateData(slice); |
|
115 | updateData(slice); | |
117 | } |
|
116 | } | |
118 |
|
117 | |||
119 | void PieSlice::updateData(const QPieSlice* sliceData) |
|
118 | void PieSlice::updateData(const QPieSlice* sliceData) | |
120 | { |
|
119 | { | |
121 | // TODO: compare what has changes to avoid unneccesary geometry updates |
|
120 | // TODO: compare what has changes to avoid unneccesary geometry updates | |
122 |
|
121 | |||
123 | m_angle = sliceData->angle(); |
|
122 | m_angle = sliceData->angle(); | |
124 | m_span = sliceData->span(); |
|
123 | m_span = sliceData->span(); | |
125 | m_isExploded = sliceData->isExploded(); |
|
124 | m_isExploded = sliceData->isExploded(); | |
126 | m_pen = sliceData->pen(); |
|
125 | m_pen = sliceData->pen(); | |
127 | m_brush = sliceData->brush(); |
|
126 | m_brush = sliceData->brush(); | |
128 |
|
127 | |||
129 | updateGeometry(); |
|
|||
130 | update(); |
|
|||
131 |
|
||||
132 | m_slicelabel->setVisible(sliceData->isLabelVisible()); |
|
128 | m_slicelabel->setVisible(sliceData->isLabelVisible()); | |
133 | m_slicelabel->setText(sliceData->label()); |
|
129 | m_slicelabel->setText(sliceData->label()); | |
134 | m_slicelabel->setPen(sliceData->labelPen()); |
|
130 | m_slicelabel->setPen(sliceData->labelPen()); | |
135 | m_slicelabel->setFont(sliceData->labelFont()); |
|
131 | m_slicelabel->setFont(sliceData->labelFont()); | |
136 | m_slicelabel->setArmLength(sliceData->labelArmLenght()); |
|
132 | m_slicelabel->setArmLength(sliceData->labelArmLenght()); | |
137 | m_slicelabel->updateGeometry(); // text size & font modifies the geometry |
|
133 | ||
|
134 | updateGeometry(); | |||
|
135 | update(); | |||
138 | m_slicelabel->update(); |
|
136 | m_slicelabel->update(); | |
139 | } |
|
137 | } | |
140 |
|
138 | |||
141 | #include "moc_pieslice.cpp" |
|
139 | #include "moc_pieslice.cpp" | |
142 |
|
140 | |||
143 | QTCOMMERCIALCHART_END_NAMESPACE |
|
141 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,64 +1,70 | |||||
1 | #include "pieslicelabel.h" |
|
1 | #include "pieslicelabel.h" | |
2 | #include <QPainter> |
|
2 | #include <QPainter> | |
3 | #include <qmath.h> |
|
3 | #include <qmath.h> | |
4 | #include <QGraphicsTextItem> |
|
4 | #include <QGraphicsTextItem> | |
|
5 | #include <QDebug> | |||
5 |
|
6 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
8 | |||
8 | #define PI 3.14159265 |
|
9 | #define PI 3.14159265 | |
9 |
|
10 | |||
10 | PieSliceLabel::PieSliceLabel(QGraphicsItem* parent) |
|
11 | PieSliceLabel::PieSliceLabel(QGraphicsItem* parent) | |
11 | :QGraphicsItem(parent) |
|
12 | :QGraphicsItem(parent), | |
|
13 | m_armAngle(0), | |||
|
14 | m_armLength(0) | |||
12 | { |
|
15 | { | |
13 | // set defaults |
|
16 | ||
14 | m_pen = QPen(Qt::black); |
|
|||
15 | } |
|
17 | } | |
16 |
|
18 | |||
17 | void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
19 | void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | |
18 | { |
|
20 | { | |
19 | painter->setRenderHint(QPainter::Antialiasing); |
|
21 | painter->setRenderHint(QPainter::Antialiasing); | |
20 |
|
22 | |||
21 | painter->setPen(m_pen); |
|
23 | painter->setPen(m_pen); | |
22 | painter->drawPath(m_armPath); |
|
24 | painter->drawPath(m_armPath); | |
23 |
|
25 | |||
24 | // TODO: do we need a pen for text? |
|
26 | // TODO: do we need a pen for text? | |
25 | painter->setFont(m_font); |
|
27 | painter->setFont(m_font); | |
26 | painter->drawText(m_textRect, m_text); |
|
28 | painter->drawText(m_textRect, m_text); | |
|
29 | ||||
|
30 | //qDebug() << "PieSliceLabel::paint" << m_text << m_textRect; | |||
27 | } |
|
31 | } | |
28 |
|
32 | |||
29 | void PieSliceLabel::updateGeometry() |
|
33 | void PieSliceLabel::updateGeometry() | |
30 | { |
|
34 | { | |
31 | prepareGeometryChange(); |
|
35 | prepareGeometryChange(); | |
32 |
|
36 | |||
33 | // calculate text size |
|
37 | // calculate text size | |
34 | QFontMetricsF fm(m_font); |
|
38 | QFontMetricsF fm(m_font); | |
35 | QRectF textRect = fm.boundingRect(m_text); |
|
39 | QRectF textRect = fm.boundingRect(m_text); | |
36 |
|
40 | |||
37 | // calculate path for arm and text start point |
|
41 | // calculate path for arm and text start point | |
38 | qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; |
|
42 | qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; | |
39 | qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; |
|
43 | qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; | |
40 | QPointF parm1 = m_armStartPoint + QPointF(dx, dy); |
|
44 | QPointF parm1 = m_armStartPoint + QPointF(dx, dy); | |
41 |
|
45 | |||
42 | // calculate horizontal arm and text position |
|
46 | // calculate horizontal arm and text position | |
43 | QPointF parm2 = parm1; |
|
47 | QPointF parm2 = parm1; | |
44 | textRect.moveBottomLeft(parm1); |
|
48 | textRect.moveBottomLeft(parm1); | |
45 | if (m_armAngle < 180) { // arm swings the other way on the left side |
|
49 | if (m_armAngle < 180) { // arm swings the other way on the left side | |
46 | parm2 += QPointF(m_textRect.width(), 0); |
|
50 | parm2 += QPointF(m_textRect.width(), 0); | |
47 | } else { |
|
51 | } else { | |
48 | parm2 += QPointF(-m_textRect.width(),0); |
|
52 | parm2 += QPointF(-m_textRect.width(),0); | |
49 | textRect.moveBottomLeft(parm2); |
|
53 | textRect.moveBottomLeft(parm2); | |
50 | } |
|
54 | } | |
51 |
|
55 | |||
52 | // update arm path |
|
56 | // update arm path | |
53 | QPainterPath path; |
|
57 | QPainterPath path; | |
54 | path.moveTo(m_armStartPoint); |
|
58 | path.moveTo(m_armStartPoint); | |
55 | path.lineTo(parm1); |
|
59 | path.lineTo(parm1); | |
56 | path.lineTo(parm2); |
|
60 | path.lineTo(parm2); | |
57 |
|
61 | |||
58 | // update paths & rects |
|
62 | // update paths & rects | |
59 | m_armPath = path; |
|
63 | m_armPath = path; | |
60 | m_textRect = textRect; |
|
64 | m_textRect = textRect; | |
61 | m_rect = path.boundingRect().united(m_textRect); |
|
65 | m_rect = path.boundingRect().united(m_textRect); | |
|
66 | ||||
|
67 | //qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect; | |||
62 | } |
|
68 | } | |
63 |
|
69 | |||
64 | QTCOMMERCIALCHART_END_NAMESPACE |
|
70 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now