@@ -1,155 +1,156 | |||
|
1 | 1 | |
|
2 | 2 | #include "piepresenter.h" |
|
3 | 3 | #include "pieslice.h" |
|
4 | 4 | #include "qpieslice.h" |
|
5 | 5 | #include <QDebug> |
|
6 | 6 | #include <QTime> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) |
|
11 | 11 | :ChartItem(parent), |
|
12 | 12 | m_series(series) |
|
13 | 13 | { |
|
14 | 14 | Q_ASSERT(series); |
|
15 | 15 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); |
|
16 | 16 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); |
|
17 | 17 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | PiePresenter::~PiePresenter() |
|
21 | 21 | { |
|
22 | 22 | // slices deleted automatically through QGraphicsItem |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | void PiePresenter::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) |
|
26 | 26 | { |
|
27 | 27 | // TODO: paint shadows for all components |
|
28 | 28 | // - get paths from items & merge & offset and draw with shadow color? |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) |
|
32 | 32 | { |
|
33 | 33 | //qDebug() << "PiePresenter::handleSeriesChanged()"; |
|
34 | 34 | //qDebug() << " added : " << changeSet.added(); |
|
35 | 35 | //qDebug() << " changed: " << changeSet.changed(); |
|
36 | 36 | //qDebug() << " removed: " << changeSet.removed(); |
|
37 | 37 | |
|
38 | 38 | // ignore changeset when there are no visual slices |
|
39 | 39 | // changeset might not be valid about the added slices |
|
40 | 40 | if (m_slices.count() == 0) { |
|
41 | 41 | foreach (QPieSlice* s, m_series->m_slices) |
|
42 | 42 | addSlice(s); |
|
43 | 43 | return; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | foreach (QPieSlice* s, changeSet.removed()) |
|
47 | 47 | deleteSlice(s); |
|
48 | 48 | |
|
49 | 49 | foreach (QPieSlice* s, changeSet.added()) |
|
50 | 50 | addSlice(s); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | void PiePresenter::handleDomainChanged(const Domain& domain) |
|
54 | 54 | { |
|
55 | 55 | // TODO |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void PiePresenter::handleGeometryChanged(const QRectF& rect) |
|
59 | 59 | { |
|
60 | 60 | m_rect = rect; |
|
61 | 61 | updateGeometry(); |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | void PiePresenter::updateGeometry() |
|
65 | 65 | { |
|
66 | 66 | prepareGeometryChange(); |
|
67 | 67 | |
|
68 | 68 | QRectF pieRect = m_rect; |
|
69 | 69 | |
|
70 | 70 | if (pieRect.width() < pieRect.height()) { |
|
71 | 71 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); |
|
72 | 72 | pieRect.setHeight(pieRect.width()); |
|
73 | 73 | pieRect.moveCenter(m_rect.center()); |
|
74 | 74 | } else { |
|
75 | 75 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); |
|
76 | 76 | pieRect.setWidth(pieRect.height()); |
|
77 | 77 | pieRect.moveCenter(m_rect.center()); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | switch (m_series->position()) { |
|
81 | 81 | case QPieSeries::PiePositionTopLeft: { |
|
82 | 82 | pieRect.setHeight(pieRect.height() / 2); |
|
83 | 83 | pieRect.setWidth(pieRect.height()); |
|
84 | 84 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); |
|
85 | 85 | break; |
|
86 | 86 | } |
|
87 | 87 | case QPieSeries::PiePositionTopRight: { |
|
88 | 88 | pieRect.setHeight(pieRect.height() / 2); |
|
89 | 89 | pieRect.setWidth(pieRect.height()); |
|
90 | 90 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); |
|
91 | 91 | break; |
|
92 | 92 | } |
|
93 | 93 | case QPieSeries::PiePositionBottomLeft: { |
|
94 | 94 | pieRect.setHeight(pieRect.height() / 2); |
|
95 | 95 | pieRect.setWidth(pieRect.height()); |
|
96 | 96 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); |
|
97 | 97 | break; |
|
98 | 98 | } |
|
99 | 99 | case QPieSeries::PiePositionBottomRight: { |
|
100 | 100 | pieRect.setHeight(pieRect.height() / 2); |
|
101 | 101 | pieRect.setWidth(pieRect.height()); |
|
102 | 102 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); |
|
103 | 103 | break; |
|
104 | 104 | } |
|
105 | 105 | default: |
|
106 | 106 | break; |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | if (m_pieRect != pieRect) { |
|
110 | 110 | m_pieRect = pieRect; |
|
111 | 111 | //qDebug() << "PiePresenter::updateGeometry()" << m_pieRect; |
|
112 | 112 | foreach (PieSlice* s, m_slices.values()) { |
|
113 | 113 | s->setPieRect(m_pieRect); |
|
114 | 114 | s->updateGeometry(); |
|
115 | 115 | } |
|
116 | 116 | } |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | void PiePresenter::addSlice(QPieSlice* sliceData) |
|
120 | 120 | { |
|
121 | 121 | //qDebug() << "PiePresenter::addSlice()" << sliceData; |
|
122 | 122 | |
|
123 | 123 | if (m_slices.keys().contains(sliceData)) { |
|
124 | 124 | //qWarning() << "PiePresenter::addSlice(): slice already exists!" << sliceData; |
|
125 | 125 | Q_ASSERT(0); |
|
126 | 126 | return; |
|
127 | 127 | } |
|
128 | 128 | |
|
129 | 129 | // create slice |
|
130 | 130 | PieSlice *slice = new PieSlice(this); |
|
131 | slice->setPieRect(m_pieRect); | |
|
131 | 132 | slice->updateData(sliceData); |
|
132 | 133 | m_slices.insert(sliceData, slice); |
|
133 | 134 | |
|
134 | 135 | // connect signals |
|
135 | 136 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); |
|
136 | 137 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); |
|
137 | 138 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); |
|
138 | 139 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); |
|
139 | 140 | } |
|
140 | 141 | |
|
141 | 142 | void PiePresenter::deleteSlice(QPieSlice* sliceData) |
|
142 | 143 | { |
|
143 | 144 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; |
|
144 | 145 | |
|
145 | 146 | if (m_slices.contains(sliceData)) |
|
146 | 147 | delete m_slices.take(sliceData); |
|
147 | 148 | else { |
|
148 | 149 | // nothing to remove |
|
149 | 150 | Q_ASSERT(0); // TODO: remove before release |
|
150 | 151 | } |
|
151 | 152 | } |
|
152 | 153 | |
|
153 | 154 | #include "moc_piepresenter.cpp" |
|
154 | 155 | |
|
155 | 156 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now