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