@@ -1,155 +1,156 | |||||
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->updateData(sliceData); |
|
132 | slice->updateData(sliceData); | |
132 | m_slices.insert(sliceData, slice); |
|
133 | m_slices.insert(sliceData, slice); | |
133 |
|
134 | |||
134 | // connect signals |
|
135 | // connect signals | |
135 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); |
|
136 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); | |
136 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); |
|
137 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); | |
137 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); |
|
138 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); | |
138 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); |
|
139 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); | |
139 | } |
|
140 | } | |
140 |
|
141 | |||
141 | void PiePresenter::deleteSlice(QPieSlice* sliceData) |
|
142 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | |
142 | { |
|
143 | { | |
143 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; |
|
144 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | |
144 |
|
145 | |||
145 | if (m_slices.contains(sliceData)) |
|
146 | if (m_slices.contains(sliceData)) | |
146 | delete m_slices.take(sliceData); |
|
147 | delete m_slices.take(sliceData); | |
147 | else { |
|
148 | else { | |
148 | // nothing to remove |
|
149 | // nothing to remove | |
149 | Q_ASSERT(0); // TODO: remove before release |
|
150 | Q_ASSERT(0); // TODO: remove before release | |
150 | } |
|
151 | } | |
151 | } |
|
152 | } | |
152 |
|
153 | |||
153 | #include "moc_piepresenter.cpp" |
|
154 | #include "moc_piepresenter.cpp" | |
154 |
|
155 | |||
155 | QTCOMMERCIALCHART_END_NAMESPACE |
|
156 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now