@@ -18,6 +18,26 QPointF linearPos(QPointF start, QPointF end, qreal pos) | |||
|
18 | 18 | return QPointF(x, y); |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | QPen linearPos(QPen start, QPen end, qreal pos) | |
|
22 | { | |
|
23 | QColor c; | |
|
24 | c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos)); | |
|
25 | c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos)); | |
|
26 | c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos)); | |
|
27 | end.setColor(c); | |
|
28 | return end; | |
|
29 | } | |
|
30 | ||
|
31 | QBrush linearPos(QBrush start, QBrush end, qreal pos) | |
|
32 | { | |
|
33 | QColor c; | |
|
34 | c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos)); | |
|
35 | c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos)); | |
|
36 | c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos)); | |
|
37 | end.setColor(c); | |
|
38 | return end; | |
|
39 | } | |
|
40 | ||
|
21 | 41 | PieSliceAnimation::PieSliceAnimation(PieChartItem *item, QPieSlice *slice) |
|
22 | 42 | :QVariantAnimation(item), |
|
23 | 43 | m_item(item), |
@@ -34,6 +54,8 void PieSliceAnimation::setValue(const PieSliceLayout &startValue, const PieSlic | |||
|
34 | 54 | if (state() != QAbstractAnimation::Stopped) |
|
35 | 55 | stop(); |
|
36 | 56 | |
|
57 | m_currentValue = startValue; | |
|
58 | ||
|
37 | 59 | setKeyValueAt(0.0, qVariantFromValue(startValue)); |
|
38 | 60 | setKeyValueAt(1.0, qVariantFromValue(endValue)); |
|
39 | 61 | } |
@@ -43,13 +65,18 void PieSliceAnimation::updateValue(const PieSliceLayout &endValue) | |||
|
43 | 65 | if (state() != QAbstractAnimation::Stopped) |
|
44 | 66 | stop(); |
|
45 | 67 | |
|
46 |
setKeyValueAt(0.0, qVariantFromValue(current |
|
|
68 | setKeyValueAt(0.0, qVariantFromValue(m_currentValue)); | |
|
47 | 69 | setKeyValueAt(1.0, qVariantFromValue(endValue)); |
|
48 | 70 | } |
|
49 | 71 | |
|
50 | 72 | PieSliceLayout PieSliceAnimation::currentSliceValue() |
|
51 | 73 | { |
|
52 | return qVariantValue<PieSliceLayout>(currentValue()); | |
|
74 | // NOTE: | |
|
75 | // We must use an internal current value because QVariantAnimation::currentValue() is updated | |
|
76 | // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue() | |
|
77 | // will have the end value set from the first call and the second call will interpolate that instead of | |
|
78 | // the original current value as it was before the first call. | |
|
79 | return m_currentValue; | |
|
53 | 80 | } |
|
54 | 81 | |
|
55 | 82 | QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const |
@@ -63,15 +90,18 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant & | |||
|
63 | 90 | result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress); |
|
64 | 91 | result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress); |
|
65 | 92 | result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress); |
|
93 | result.m_pen = linearPos(startValue.m_pen, endValue.m_pen, progress); | |
|
94 | result.m_brush = linearPos(startValue.m_brush, endValue.m_brush, progress); | |
|
66 | 95 | |
|
67 | 96 | return qVariantFromValue(result); |
|
68 | 97 | } |
|
69 | 98 | |
|
70 | 99 | void PieSliceAnimation::updateCurrentValue(const QVariant &value) |
|
71 | 100 | { |
|
72 | PieSliceLayout layout = qVariantValue<PieSliceLayout>(value); | |
|
73 | if (state() != QAbstractAnimation::Stopped) //workaround | |
|
74 |
m_item->setLayout(m_slice, |
|
|
101 | if (state() != QAbstractAnimation::Stopped) { //workaround | |
|
102 | m_currentValue = qVariantValue<PieSliceLayout>(value); | |
|
103 | m_item->setLayout(m_slice, m_currentValue); | |
|
104 | } | |
|
75 | 105 | } |
|
76 | 106 | |
|
77 | 107 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -25,6 +25,7 protected: | |||
|
25 | 25 | private: |
|
26 | 26 | PieChartItem *m_item; |
|
27 | 27 | QPieSlice *m_slice; |
|
28 | PieSliceLayout m_currentValue; | |
|
28 | 29 | }; |
|
29 | 30 | |
|
30 | 31 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -212,7 +212,7 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | |||
|
212 | 212 | |
|
213 | 213 | case QSeries::SeriesTypePie: { |
|
214 | 214 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
215 |
PieChartItem* pie = new PieChartItem( |
|
|
215 | PieChartItem* pie = new PieChartItem(pieSeries, this, m_chart); | |
|
216 | 216 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
217 | 217 | m_animator->addAnimation(pie); |
|
218 | 218 | } |
@@ -39,7 +39,10 public: | |||
|
39 | 39 | int margin() const; |
|
40 | 40 | |
|
41 | 41 | QRectF geometry() const; |
|
42 | ChartAnimator* animator() const {return m_animator;}; | |
|
42 | ||
|
43 | ChartAnimator* animator() const {return m_animator;} | |
|
44 | ChartTheme *theme() { return m_chartTheme; } | |
|
45 | ChartDataSet *dataSet() { return m_dataset; } | |
|
43 | 46 | |
|
44 | 47 | void setChartTheme(QChart::ChartTheme theme,bool force = true); |
|
45 | 48 | QChart::ChartTheme chartTheme(); |
@@ -52,6 +55,7 public: | |||
|
52 | 55 | void zoomOut(); |
|
53 | 56 | void zoomReset(); |
|
54 | 57 | void scroll(int dx,int dy); |
|
58 | ||
|
55 | 59 | private: |
|
56 | 60 | void createConnections(); |
|
57 | 61 | void resetAllElements(); |
@@ -3,6 +3,7 | |||
|
3 | 3 | #include "qpieslice.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "chartpresenter_p.h" |
|
6 | #include "chartdataset_p.h" | |
|
6 | 7 | #include "chartanimator_p.h" |
|
7 | 8 | #include <QDebug> |
|
8 | 9 | #include <QPainter> |
@@ -10,9 +11,10 | |||
|
10 | 11 | |
|
11 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 13 | |
|
13 | PieChartItem::PieChartItem(QGraphicsItem *parent, QPieSeries *series) | |
|
14 | PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter *presenter, QGraphicsItem *parent) | |
|
14 | 15 | :ChartItem(parent), |
|
15 | m_series(series) | |
|
16 | m_series(series), | |
|
17 | m_presenter(presenter) | |
|
16 | 18 | { |
|
17 | 19 | Q_ASSERT(series); |
|
18 | 20 | connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>))); |
@@ -49,6 +51,8 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices) | |||
|
49 | 51 | { |
|
50 | 52 | bool isEmpty = m_slices.isEmpty(); |
|
51 | 53 | |
|
54 | m_presenter->theme()->decorate(m_series, m_presenter->dataSet()->seriesIndex(m_series)); | |
|
55 | ||
|
52 | 56 | foreach (QPieSlice *s, slices) { |
|
53 | 57 | PieSlice* slice = new PieSlice(this); |
|
54 | 58 | m_slices.insert(s, slice); |
@@ -68,6 +72,8 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices) | |||
|
68 | 72 | |
|
69 | 73 | void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices) |
|
70 | 74 | { |
|
75 | m_presenter->theme()->decorate(m_series, m_presenter->dataSet()->seriesIndex(m_series)); | |
|
76 | ||
|
71 | 77 | foreach (QPieSlice *s, slices) { |
|
72 | 78 | if (m_animator) |
|
73 | 79 | m_animator->removeAnimation(this, s); |
@@ -126,6 +132,8 PieSliceLayout PieChartItem::calculateSliceLayout(QPieSlice *slice) | |||
|
126 | 132 | sliceLayout.m_radius = m_pieRadius; |
|
127 | 133 | sliceLayout.m_startAngle = slice->startAngle(); |
|
128 | 134 | sliceLayout.m_angleSpan = slice->m_angleSpan; |
|
135 | sliceLayout.m_pen = slice->m_slicePen; | |
|
136 | sliceLayout.m_brush = slice->m_sliceBrush; | |
|
129 | 137 | return sliceLayout; |
|
130 | 138 | } |
|
131 | 139 |
@@ -8,6 +8,7 | |||
|
8 | 8 | class QGraphicsItem; |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | class QPieSlice; |
|
11 | class ChartPresenter; | |
|
11 | 12 | |
|
12 | 13 | typedef QHash<QPieSlice*, PieSliceLayout> PieLayout; |
|
13 | 14 | |
@@ -17,7 +18,7 class PieChartItem : public QObject, public ChartItem | |||
|
17 | 18 | |
|
18 | 19 | public: |
|
19 | 20 | // TODO: use a generic data class instead of x and y |
|
20 | PieChartItem(QGraphicsItem *parent, QPieSeries *series); | |
|
21 | PieChartItem(QPieSeries *series, ChartPresenter *presenter, QGraphicsItem *parent); | |
|
21 | 22 | ~PieChartItem(); |
|
22 | 23 | |
|
23 | 24 | public: // from QGraphicsItem |
@@ -50,7 +51,7 private: | |||
|
50 | 51 | QRectF m_rect; |
|
51 | 52 | QPointF m_pieCenter; |
|
52 | 53 | qreal m_pieRadius; |
|
53 | QRectF m_debugRect; | |
|
54 | ChartPresenter *m_presenter; | |
|
54 | 55 | }; |
|
55 | 56 | |
|
56 | 57 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -54,8 +54,8 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option | |||
|
54 | 54 | painter->setClipRect(parentItem()->boundingRect()); |
|
55 | 55 | |
|
56 | 56 | painter->save(); |
|
57 |
painter->setPen(m_ |
|
|
58 |
painter->setBrush(m_ |
|
|
57 | painter->setPen(m_layout.m_pen); | |
|
58 | painter->setBrush(m_layout.m_brush); | |
|
59 | 59 | painter->drawPath(m_slicePath); |
|
60 | 60 | painter->restore(); |
|
61 | 61 | |
@@ -122,8 +122,6 void PieSlice::updateData(const QPieSlice* sliceData) | |||
|
122 | 122 | |
|
123 | 123 | m_isExploded = sliceData->isExploded(); |
|
124 | 124 | m_explodeDistanceFactor = sliceData->explodeDistanceFactor(); |
|
125 | m_slicePen = sliceData->slicePen(); | |
|
126 | m_sliceBrush = sliceData->sliceBrush(); | |
|
127 | 125 | |
|
128 | 126 | m_labelVisible = sliceData->isLabelVisible(); |
|
129 | 127 | m_labelText = sliceData->label(); |
@@ -23,6 +23,8 public: | |||
|
23 | 23 | qreal m_radius; |
|
24 | 24 | qreal m_startAngle; |
|
25 | 25 | qreal m_angleSpan; |
|
26 | QPen m_pen; | |
|
27 | QBrush m_brush; | |
|
26 | 28 | }; |
|
27 | 29 | |
|
28 | 30 | class PieSlice : public QGraphicsObject |
@@ -63,8 +65,6 private: | |||
|
63 | 65 | bool m_isExploded; |
|
64 | 66 | qreal m_explodeDistanceFactor; |
|
65 | 67 | bool m_labelVisible; |
|
66 | QPen m_slicePen; | |
|
67 | QBrush m_sliceBrush; | |
|
68 | 68 | |
|
69 | 69 | QPainterPath m_labelArmPath; |
|
70 | 70 | qreal m_labelArmLengthFactor; |
General Comments 0
You need to be logged in to leave comments.
Login now