##// END OF EJS Templates
pie: fix bug with qmlchart demo...
Jani Honkonen -
r1238:8de78239061c
parent child
Show More
@@ -1,190 +1,194
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "piechartitem_p.h"
22 22 #include "piesliceitem_p.h"
23 23 #include "qpieslice.h"
24 24 #include "qpieseries.h"
25 25 #include "qpieseries_p.h"
26 26 #include "chartpresenter_p.h"
27 27 #include "chartdataset_p.h"
28 28 #include "chartanimator_p.h"
29 29 #include <QPainter>
30 30 #include <QTimer>
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
35 35 :ChartItem(presenter),
36 36 m_series(series)
37 37 {
38 38 Q_ASSERT(series);
39 39
40 40 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
41 41 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
42 42 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series);
43 43 connect(d, SIGNAL(piePositionChanged()), this, SLOT(updateLayout()));
44 44 connect(d, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
45 45
46 QTimer::singleShot(0, this, SLOT(initialize())); // TODO: get rid of this
47
48 46 // Note: the following does not affect as long as the item does not have anything to paint
49 47 setZValue(ChartPresenter::PieSeriesZValue);
48
49 // Note: will not create slice items until we have a proper rectangle to draw on.
50 50 }
51 51
52 52 PieChartItem::~PieChartItem()
53 53 {
54 54 // slices deleted automatically through QGraphicsItem
55 55 }
56 56
57 57 void PieChartItem::handleGeometryChanged(const QRectF& rect)
58 58 {
59 59 prepareGeometryChange();
60 60 m_rect = rect;
61 61 updateLayout();
62
63 // This is for delayed initialization of the slice items during startup.
64 // It ensures that startup animation originates from the correct position.
65 if (m_sliceItems.isEmpty())
66 handleSlicesAdded(m_series->slices());
62 67 }
63 68
64 69 void PieChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
65 70 {
66 71 Q_UNUSED(minX);
67 72 Q_UNUSED(maxX);
68 73 Q_UNUSED(minY);
69 74 Q_UNUSED(maxY);
70 75 // does not apply to pie
71 76 }
72 77
73 78 void PieChartItem::rangeXChanged(qreal min, qreal max, int tickXCount)
74 79 {
75 80 Q_UNUSED(min);
76 81 Q_UNUSED(max);
77 82 Q_UNUSED(tickXCount);
78 83 // does not apply to pie
79 84 }
80 85
81 86 void PieChartItem::rangeYChanged(qreal min, qreal max, int tickYCount)
82 87 {
83 88 Q_UNUSED(min);
84 89 Q_UNUSED(max);
85 90 Q_UNUSED(tickYCount);
86 91 // does not apply to pie
87 92 }
88 93
89 void PieChartItem::initialize()
90 {
91 handleSlicesAdded(m_series->slices());
92 }
93
94 94 void PieChartItem::updateLayout()
95 95 {
96 96 // find pie center coordinates
97 97 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
98 98 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
99 99
100 100 // find maximum radius for pie
101 101 m_pieRadius = m_rect.height() / 2;
102 102 if (m_rect.width() < m_rect.height())
103 103 m_pieRadius = m_rect.width() / 2;
104 104
105 105 // apply size factor
106 106 m_pieRadius *= m_series->pieSize();
107 107
108 108 // set layouts for existing slice items
109 109 foreach (QPieSlice* slice, m_series->slices()) {
110 110 PieSliceItem *sliceItem = m_sliceItems.value(slice);
111 111 if (sliceItem) {
112 112 PieSliceData sliceData = updateSliceGeometry(slice);
113 113 if (animator())
114 114 animator()->updateAnimation(this, sliceItem, sliceData);
115 115 else
116 116 sliceItem->setLayout(sliceData);
117 117 }
118 118 }
119 119
120 120 update();
121 121 }
122 122
123 123 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
124 124 {
125 // delay creating slice items until there is a proper rectangle
126 if (!m_rect.isValid() && m_sliceItems.isEmpty())
127 return;
128
125 129 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
126 130
127 131 bool startupAnimation = m_sliceItems.isEmpty();
128 132
129 133 foreach (QPieSlice *slice, slices) {
130 134 PieSliceItem* sliceItem = new PieSliceItem(this);
131 135 m_sliceItems.insert(slice, sliceItem);
132 136
133 137 // note: do need to connect to slice valueChanged(). calculatedDataChanged() is enough.
134 138 // to update the slice.
135 139 connect(slice, SIGNAL(calculatedDataChanged()), this, SLOT(handleSliceChanged()));
136 140 connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged()));
137 141 connect(slice, SIGNAL(appearanceChanged()), this, SLOT(handleSliceChanged()));
138 142 connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked()));
139 143 connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool)));
140 144
141 145 PieSliceData sliceData = updateSliceGeometry(slice);
142 146 if (animator())
143 147 animator()->addAnimation(this, sliceItem, sliceData, startupAnimation);
144 148 else
145 149 sliceItem->setLayout(sliceData);
146 150 }
147 151 }
148 152
149 153 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
150 154 {
151 155 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
152 156
153 157 foreach (QPieSlice *slice, slices) {
154 158 PieSliceItem *sliceItem = m_sliceItems.value(slice);
155 159 Q_ASSERT(sliceItem);
156 160 m_sliceItems.remove(slice);
157 161
158 162 if (animator())
159 163 animator()->removeAnimation(this, sliceItem); // animator deletes the PieSliceItem
160 164 else
161 165 delete sliceItem;
162 166 }
163 167 }
164 168
165 169 void PieChartItem::handleSliceChanged()
166 170 {
167 171 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
168 172 Q_ASSERT(m_sliceItems.contains(slice));
169 173
170 174 PieSliceItem *sliceItem = m_sliceItems.value(slice);
171 175 PieSliceData sliceData = updateSliceGeometry(slice);
172 176 if (animator())
173 177 animator()->updateAnimation(this, sliceItem, sliceData);
174 178 else
175 179 sliceItem->setLayout(sliceData);
176 180
177 181 update();
178 182 }
179 183
180 184 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
181 185 {
182 186 PieSliceData &sliceData = PieSliceData::fromSlice(slice);
183 187 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
184 188 sliceData.m_radius = m_pieRadius;
185 189 return sliceData;
186 190 }
187 191
188 192 #include "moc_piechartitem_p.cpp"
189 193
190 194 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,70
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef PIECHARTITEM_H
22 22 #define PIECHARTITEM_H
23 23
24 24 #include "qpieseries.h"
25 25 #include "chartitem_p.h"
26 26 #include "piesliceitem_p.h"
27 27
28 28 class QGraphicsItem;
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QPieSlice;
31 31 class ChartPresenter;
32 32
33 33 class PieChartItem : public ChartItem
34 34 {
35 35 Q_OBJECT
36 36
37 37 public:
38 38 explicit PieChartItem(QPieSeries *series, ChartPresenter *presenter);
39 39 ~PieChartItem();
40 40
41 41 // from QGraphicsItem
42 42 QRectF boundingRect() const { return m_rect; }
43 43 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {}
44 44
45 45 public Q_SLOTS:
46 46 // from Chart
47 47 virtual void handleGeometryChanged(const QRectF &rect);
48 48 virtual void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
49 49 virtual void rangeXChanged(qreal min, qreal max, int tickXCount);
50 50 virtual void rangeYChanged(qreal min, qreal max, int tickYCount);
51 51
52 void initialize();
53 52 void updateLayout();
54 53 void handleSlicesAdded(QList<QPieSlice*> slices);
55 54 void handleSlicesRemoved(QList<QPieSlice*> slices);
56 55 void handleSliceChanged();
57 56
58 57 private:
59 58 PieSliceData updateSliceGeometry(QPieSlice *slice);
60 59
61 60 private:
62 61 QHash<QPieSlice*, PieSliceItem*> m_sliceItems;
63 62 QPieSeries *m_series;
64 63 QRectF m_rect;
65 64 QPointF m_pieCenter;
66 65 qreal m_pieRadius;
67 66 };
68 67
69 68 QTCOMMERCIALCHART_END_NAMESPACE
70 69
71 70 #endif // PIECHARTITEM_H
General Comments 0
You need to be logged in to leave comments. Login now