@@ -1,52 +1,51 | |||||
1 | #include <QApplication> |
|
1 | #include <QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
3 | #include <QChartView> |
|
3 | #include <QChartView> | |
4 | #include <QLineSeries> |
|
4 | #include <QLineSeries> | |
5 | #include <cmath> |
|
5 | #include <cmath> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
8 |
|
8 | |||
9 | int main(int argc, char *argv[]) |
|
9 | int main(int argc, char *argv[]) | |
10 | { |
|
10 | { | |
11 | QApplication a(argc, argv); |
|
11 | QApplication a(argc, argv); | |
12 |
|
12 | |||
13 | //![1] |
|
13 | //![1] | |
14 |
|
||||
15 | QLineSeries* series0 = new QLineSeries(); |
|
14 | QLineSeries* series0 = new QLineSeries(); | |
16 | QPen blue(Qt::blue); |
|
15 | QPen blue(Qt::blue); | |
17 | blue.setWidth(3); |
|
16 | blue.setWidth(3); | |
18 | series0->setPen(blue); |
|
17 | series0->setPen(blue); | |
19 |
|
18 | |||
20 | QLineSeries* series1 = new QLineSeries(); |
|
19 | QLineSeries* series1 = new QLineSeries(); | |
21 | QPen red(Qt::red); |
|
20 | QPen red(Qt::red); | |
22 | red.setWidth(3); |
|
21 | red.setWidth(3); | |
23 | series1->setPen(red); |
|
22 | series1->setPen(red); | |
24 | //![1] |
|
23 | //![1] | |
25 |
|
24 | |||
26 | //![2] |
|
25 | //![2] | |
27 | series0->add(0, 6); |
|
26 | series0->add(0, 6); | |
28 | series0->add(2, 4); |
|
27 | series0->add(2, 4); | |
29 | series0->add(3, 8); |
|
28 | series0->add(3, 8); | |
30 | series0->add(7, 4); |
|
29 | series0->add(7, 4); | |
31 | series0->add(10,5); |
|
30 | series0->add(10,5); | |
32 |
|
31 | |||
33 | *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10,2); |
|
32 | *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10,2); | |
34 | //![2] |
|
33 | //![2] | |
35 | //![3] |
|
34 | //![3] | |
36 | QMainWindow window; |
|
35 | QMainWindow window; | |
37 | QChartView* chartView = new QChartView(&window); |
|
36 | QChartView* chartView = new QChartView(&window); | |
38 |
|
37 | |||
39 | chartView->setChartTitle("Basic line chart example"); |
|
38 | chartView->setChartTitle("Basic line chart example"); | |
40 | chartView->setRenderHint(QPainter::Antialiasing); |
|
39 | chartView->setRenderHint(QPainter::Antialiasing); | |
41 |
|
40 | |||
42 | chartView->addSeries(series0); |
|
41 | chartView->addSeries(series0); | |
43 | chartView->addSeries(series1); |
|
42 | chartView->addSeries(series1); | |
44 | //![3] |
|
43 | //![3] | |
45 | //![4] |
|
44 | //![4] | |
46 | window.setCentralWidget(chartView); |
|
45 | window.setCentralWidget(chartView); | |
47 | window.resize(400, 300); |
|
46 | window.resize(400, 300); | |
48 | window.show(); |
|
47 | window.show(); | |
49 | //![4] |
|
48 | //![4] | |
50 |
|
49 | |||
51 | return a.exec(); |
|
50 | return a.exec(); | |
52 | } |
|
51 | } |
@@ -1,45 +1,45 | |||||
1 | #include "axisanimation_p.h" |
|
1 | #include "axisanimation_p.h" | |
2 | #include <QTimer> |
|
2 | #include <QTimer> | |
3 |
|
3 | |||
4 | Q_DECLARE_METATYPE(QVector<qreal>) |
|
4 | Q_DECLARE_METATYPE(QVector<qreal>) | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 |
|
8 | |||
9 |
AxisAnimation::AxisAnimation(Axis |
|
9 | AxisAnimation::AxisAnimation(Axis *axis): ChartAnimation(axis), | |
10 | m_axis(axis) |
|
10 | m_axis(axis) | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | AxisAnimation::~AxisAnimation() |
|
14 | AxisAnimation::~AxisAnimation() | |
15 | { |
|
15 | { | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const |
|
18 | QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const | |
19 | { |
|
19 | { | |
20 | QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start); |
|
20 | QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start); | |
21 | QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end); |
|
21 | QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end); | |
22 | QVector<qreal> result; |
|
22 | QVector<qreal> result; | |
23 |
|
23 | |||
24 | Q_ASSERT(startVector.count() == endVecotr.count()) ; |
|
24 | Q_ASSERT(startVector.count() == endVecotr.count()) ; | |
25 |
|
25 | |||
26 | for(int i =0 ;i< startVector.count();i++){ |
|
26 | for(int i =0 ;i< startVector.count();i++){ | |
27 | qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0)); |
|
27 | qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0)); | |
28 | result << value; |
|
28 | result << value; | |
29 | } |
|
29 | } | |
30 | return qVariantFromValue(result); |
|
30 | return qVariantFromValue(result); | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | void AxisAnimation::updateCurrentValue (const QVariant & value ) |
|
34 | void AxisAnimation::updateCurrentValue (const QVariant & value ) | |
35 | { |
|
35 | { | |
36 | if(state()!=QAbstractAnimation::Stopped)//workaround |
|
36 | if(state()!=QAbstractAnimation::Stopped)//workaround | |
37 | { |
|
37 | { | |
38 | QVector<qreal> vector = qVariantValue<QVector<qreal> >(value); |
|
38 | QVector<qreal> vector = qVariantValue<QVector<qreal> >(value); | |
39 | Q_ASSERT(vector.count()!=0); |
|
39 | Q_ASSERT(vector.count()!=0); | |
40 | m_axis->setLayout(vector); |
|
40 | m_axis->setLayout(vector); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | QTCOMMERCIALCHART_END_NAMESPACE |
|
45 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,26 +1,26 | |||||
1 | #ifndef AXISANIMATIONITEM_H_ |
|
1 | #ifndef AXISANIMATIONITEM_H_ | |
2 | #define AXISANIMATIONITEM_H_ |
|
2 | #define AXISANIMATIONITEM_H_ | |
3 |
|
3 | |||
4 | #include "axisitem_p.h" |
|
4 | #include "axisitem_p.h" | |
5 | #include "chartanimation_p.h" |
|
5 | #include "chartanimation_p.h" | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class AxisAnimation: public ChartAnimation |
|
10 | class AxisAnimation: public ChartAnimation | |
11 | { |
|
11 | { | |
12 | public: |
|
12 | public: | |
13 |
AxisAnimation(Axis |
|
13 | AxisAnimation(Axis *axis); | |
14 | ~AxisAnimation(); |
|
14 | ~AxisAnimation(); | |
15 | protected: |
|
15 | protected: | |
16 | virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const; |
|
16 | virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const; | |
17 | virtual void updateCurrentValue (const QVariant & value ); |
|
17 | virtual void updateCurrentValue (const QVariant & value ); | |
18 | private: |
|
18 | private: | |
19 |
Axis |
|
19 | Axis* m_axis; | |
20 | }; |
|
20 | }; | |
21 |
|
21 | |||
22 | QTCOMMERCIALCHART_END_NAMESPACE |
|
22 | QTCOMMERCIALCHART_END_NAMESPACE | |
23 |
|
23 | |||
24 |
|
24 | |||
25 |
|
25 | |||
26 | #endif /* AXISITEM_H_ */ |
|
26 | #endif /* AXISITEM_H_ */ |
@@ -1,285 +1,285 | |||||
1 | #include "chartanimator_p.h" |
|
1 | #include "chartanimator_p.h" | |
2 | #include "axisanimation_p.h" |
|
2 | #include "axisanimation_p.h" | |
3 | #include "xyanimation_p.h" |
|
3 | #include "xyanimation_p.h" | |
4 | #include "splineanimation_p.h" |
|
4 | #include "splineanimation_p.h" | |
5 | #include "xychartitem_p.h" |
|
5 | #include "xychartitem_p.h" | |
6 | #include "pieanimation_p.h" |
|
6 | #include "pieanimation_p.h" | |
7 | #include "baranimation_p.h" |
|
7 | #include "baranimation_p.h" | |
8 | #include "barchartitem_p.h" |
|
8 | #include "barchartitem_p.h" | |
9 | #include "areachartitem_p.h" |
|
9 | #include "areachartitem_p.h" | |
10 | #include "splinechartitem_p.h" |
|
10 | #include "splinechartitem_p.h" | |
11 | #include "scatterchartitem_p.h" |
|
11 | #include "scatterchartitem_p.h" | |
12 | #include <QTimer> |
|
12 | #include <QTimer> | |
13 |
|
13 | |||
14 | Q_DECLARE_METATYPE(QVector<QPointF>) |
|
14 | Q_DECLARE_METATYPE(QVector<QPointF>) | |
15 | Q_DECLARE_METATYPE(QVector<qreal>) |
|
15 | Q_DECLARE_METATYPE(QVector<qreal>) | |
16 |
|
16 | |||
17 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
17 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
18 |
|
18 | |||
19 | const static int duration = 1000; |
|
19 | const static int duration = 1000; | |
20 |
|
20 | |||
21 | ChartAnimator::ChartAnimator(QObject *parent):QObject(parent) |
|
21 | ChartAnimator::ChartAnimator(QObject *parent):QObject(parent) | |
22 | { |
|
22 | { | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | ChartAnimator::~ChartAnimator() |
|
25 | ChartAnimator::~ChartAnimator() | |
26 | { |
|
26 | { | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 |
void ChartAnimator::addAnimation(Axis |
|
29 | void ChartAnimator::addAnimation(Axis* item) | |
30 | { |
|
30 | { | |
31 | ChartAnimation* animation = m_animations.value(item); |
|
31 | ChartAnimation* animation = m_animations.value(item); | |
32 |
|
32 | |||
33 | if(!animation) { |
|
33 | if(!animation) { | |
34 | animation = new AxisAnimation(item); |
|
34 | animation = new AxisAnimation(item); | |
35 | m_animations.insert(item,animation); |
|
35 | m_animations.insert(item,animation); | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | item->setAnimator(this); |
|
38 | item->setAnimator(this); | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | void ChartAnimator::addAnimation(SplineChartItem* item) |
|
41 | void ChartAnimator::addAnimation(SplineChartItem* item) | |
42 | { |
|
42 | { | |
43 | ChartAnimation* animation = m_animations.value(item); |
|
43 | ChartAnimation* animation = m_animations.value(item); | |
44 |
|
44 | |||
45 | if(!animation) { |
|
45 | if(!animation) { | |
46 | animation = new SplineAnimation(item); |
|
46 | animation = new SplineAnimation(item); | |
47 | m_animations.insert(item,animation); |
|
47 | m_animations.insert(item,animation); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | item->setAnimator(this); |
|
50 | item->setAnimator(this); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | void ChartAnimator::addAnimation(ScatterChartItem* item) |
|
53 | void ChartAnimator::addAnimation(ScatterChartItem* item) | |
54 | { |
|
54 | { | |
55 | ChartAnimation* animation = m_animations.value(item); |
|
55 | ChartAnimation* animation = m_animations.value(item); | |
56 |
|
56 | |||
57 | if(!animation) { |
|
57 | if(!animation) { | |
58 | animation = new XYAnimation(item); |
|
58 | animation = new XYAnimation(item); | |
59 | m_animations.insert(item,animation); |
|
59 | m_animations.insert(item,animation); | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | item->setAnimator(this); |
|
62 | item->setAnimator(this); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | void ChartAnimator::addAnimation(LineChartItem* item) |
|
65 | void ChartAnimator::addAnimation(LineChartItem* item) | |
66 | { |
|
66 | { | |
67 | ChartAnimation* animation = m_animations.value(item); |
|
67 | ChartAnimation* animation = m_animations.value(item); | |
68 |
|
68 | |||
69 | if(!animation) { |
|
69 | if(!animation) { | |
70 | animation = new XYAnimation(item); |
|
70 | animation = new XYAnimation(item); | |
71 | m_animations.insert(item,animation); |
|
71 | m_animations.insert(item,animation); | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | item->setAnimator(this); |
|
74 | item->setAnimator(this); | |
75 | } |
|
75 | } | |
76 |
|
76 | |||
77 | void ChartAnimator::addAnimation(PieChartItem* item) |
|
77 | void ChartAnimator::addAnimation(PieChartItem* item) | |
78 | { |
|
78 | { | |
79 | ChartAnimation* animation = m_animations.value(item); |
|
79 | ChartAnimation* animation = m_animations.value(item); | |
80 |
|
80 | |||
81 | if(!animation) { |
|
81 | if(!animation) { | |
82 | animation = new PieAnimation(item); |
|
82 | animation = new PieAnimation(item); | |
83 | m_animations.insert(item,animation); |
|
83 | m_animations.insert(item,animation); | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | item->setAnimator(this); |
|
86 | item->setAnimator(this); | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void ChartAnimator::addAnimation(BarChartItem* item) |
|
89 | void ChartAnimator::addAnimation(BarChartItem* item) | |
90 | { |
|
90 | { | |
91 | ChartAnimation* animation = m_animations.value(item); |
|
91 | ChartAnimation* animation = m_animations.value(item); | |
92 |
|
92 | |||
93 | if(!animation) { |
|
93 | if(!animation) { | |
94 | animation = new BarAnimation(item); |
|
94 | animation = new BarAnimation(item); | |
95 | m_animations.insert(item,animation); |
|
95 | m_animations.insert(item,animation); | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 | item->setAnimator(this); |
|
98 | item->setAnimator(this); | |
99 | } |
|
99 | } | |
100 |
|
100 | |||
101 |
|
101 | |||
102 |
void ChartAnimator::removeAnimation(Chart |
|
102 | void ChartAnimator::removeAnimation(Chart* item) | |
103 | { |
|
103 | { | |
104 | item->setAnimator(0); |
|
104 | item->setAnimator(0); | |
105 | m_animations.remove(item); |
|
105 | m_animations.remove(item); | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 |
void ChartAnimator::updateLayout(Axis |
|
108 | void ChartAnimator::updateLayout(Axis* item , QVector<qreal>& newLayout) | |
109 | { |
|
109 | { | |
110 | AxisAnimation* animation = static_cast<AxisAnimation*>(m_animations.value(item)); |
|
110 | AxisAnimation* animation = static_cast<AxisAnimation*>(m_animations.value(item)); | |
111 |
|
111 | |||
112 | Q_ASSERT(animation); |
|
112 | Q_ASSERT(animation); | |
113 |
|
113 | |||
114 | QVector<qreal> oldLayout = item->layout(); |
|
114 | QVector<qreal> oldLayout = item->layout(); | |
115 |
|
115 | |||
116 | if(newLayout.count()==0) return; |
|
116 | if(newLayout.count()==0) return; | |
117 |
|
117 | |||
118 | switch(m_state) |
|
118 | switch(m_state) | |
119 | { |
|
119 | { | |
120 | case ZoomOutState: { |
|
120 | case ZoomOutState: { | |
121 | QRectF rect = item->geometry(); |
|
121 | QRectF rect = item->geometry(); | |
122 | oldLayout.resize(newLayout.count()); |
|
122 | oldLayout.resize(newLayout.count()); | |
123 |
|
123 | |||
124 | for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--) |
|
124 | for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--) | |
125 | { |
|
125 | { | |
126 |
oldLayout[i]= item->axisType()==Axis |
|
126 | oldLayout[i]= item->axisType()==Axis::X_AXIS?rect.left():rect.bottom(); | |
127 |
oldLayout[j]= item->axisType()==Axis |
|
127 | oldLayout[j]= item->axisType()==Axis::X_AXIS?rect.right():rect.top(); | |
128 | } |
|
128 | } | |
129 | } |
|
129 | } | |
130 | break; |
|
130 | break; | |
131 | case ZoomInState: { |
|
131 | case ZoomInState: { | |
132 |
int index = qMin(oldLayout.count()*(item->axisType()==Axis |
|
132 | int index = qMin(oldLayout.count()*(item->axisType()==Axis::X_AXIS?m_point.x():(1 -m_point.y())),newLayout.count()-1.0); | |
133 | oldLayout.resize(newLayout.count()); |
|
133 | oldLayout.resize(newLayout.count()); | |
134 |
|
134 | |||
135 | for(int i=0;i<oldLayout.count();i++) |
|
135 | for(int i=0;i<oldLayout.count();i++) | |
136 | { |
|
136 | { | |
137 | oldLayout[i]= oldLayout[index]; |
|
137 | oldLayout[i]= oldLayout[index]; | |
138 | } |
|
138 | } | |
139 | } |
|
139 | } | |
140 | break; |
|
140 | break; | |
141 | case ScrollDownState: |
|
141 | case ScrollDownState: | |
142 | case ScrollRightState: { |
|
142 | case ScrollRightState: { | |
143 | oldLayout.resize(newLayout.count()); |
|
143 | oldLayout.resize(newLayout.count()); | |
144 |
|
144 | |||
145 | for(int i=0, j=i+1;i<oldLayout.count()-1;i++,j++) |
|
145 | for(int i=0, j=i+1;i<oldLayout.count()-1;i++,j++) | |
146 | { |
|
146 | { | |
147 | oldLayout[i]= oldLayout[j]; |
|
147 | oldLayout[i]= oldLayout[j]; | |
148 | } |
|
148 | } | |
149 | } |
|
149 | } | |
150 | break; |
|
150 | break; | |
151 | case ScrollUpState: |
|
151 | case ScrollUpState: | |
152 | case ScrollLeftState: { |
|
152 | case ScrollLeftState: { | |
153 | oldLayout.resize(newLayout.count()); |
|
153 | oldLayout.resize(newLayout.count()); | |
154 |
|
154 | |||
155 | for(int i=oldLayout.count()-1, j=i-1;i>0;i--,j--) |
|
155 | for(int i=oldLayout.count()-1, j=i-1;i>0;i--,j--) | |
156 | { |
|
156 | { | |
157 | oldLayout[i]= oldLayout[j]; |
|
157 | oldLayout[i]= oldLayout[j]; | |
158 | } |
|
158 | } | |
159 | } |
|
159 | } | |
160 | break; |
|
160 | break; | |
161 | default: { |
|
161 | default: { | |
162 | oldLayout.resize(newLayout.count()); |
|
162 | oldLayout.resize(newLayout.count()); | |
163 | QRectF rect = item->geometry(); |
|
163 | QRectF rect = item->geometry(); | |
164 | for(int i=0, j=oldLayout.count()-1;i<oldLayout.count();i++,j--) |
|
164 | for(int i=0, j=oldLayout.count()-1;i<oldLayout.count();i++,j--) | |
165 | { |
|
165 | { | |
166 |
oldLayout[i]= item->axisType()==Axis |
|
166 | oldLayout[i]= item->axisType()==Axis::X_AXIS?rect.left():rect.top(); | |
167 | } |
|
167 | } | |
168 | } |
|
168 | } | |
169 | break; |
|
169 | break; | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | if(animation->state()!=QAbstractAnimation::Stopped) { |
|
173 | if(animation->state()!=QAbstractAnimation::Stopped) { | |
174 | animation->stop(); |
|
174 | animation->stop(); | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | animation->setDuration(duration); |
|
177 | animation->setDuration(duration); | |
178 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
178 | animation->setEasingCurve(QEasingCurve::OutQuart); | |
179 | QVariantAnimation::KeyValues value; |
|
179 | QVariantAnimation::KeyValues value; | |
180 | animation->setKeyValues(value); //workaround for wrong interpolation call |
|
180 | animation->setKeyValues(value); //workaround for wrong interpolation call | |
181 | animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); |
|
181 | animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); | |
182 | animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); |
|
182 | animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); | |
183 |
|
183 | |||
184 | QTimer::singleShot(0,animation,SLOT(start())); |
|
184 | QTimer::singleShot(0,animation,SLOT(start())); | |
185 | } |
|
185 | } | |
186 |
|
186 | |||
187 | void ChartAnimator::updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints ,QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newControlPoints,int index) |
|
187 | void ChartAnimator::updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints ,QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newControlPoints,int index) | |
188 | { |
|
188 | { | |
189 | SplineAnimation* animation = static_cast<SplineAnimation*>(m_animations.value(item)); |
|
189 | SplineAnimation* animation = static_cast<SplineAnimation*>(m_animations.value(item)); | |
190 |
|
190 | |||
191 | Q_ASSERT(animation); |
|
191 | Q_ASSERT(animation); | |
192 |
|
192 | |||
193 | if(newPoints.count()<2 || newControlPoints.count()<2) return; |
|
193 | if(newPoints.count()<2 || newControlPoints.count()<2) return; | |
194 |
|
194 | |||
195 | bool empty = oldPoints.count()==0; |
|
195 | bool empty = oldPoints.count()==0; | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | if(animation->state()!=QAbstractAnimation::Stopped) { |
|
198 | if(animation->state()!=QAbstractAnimation::Stopped) { | |
199 | animation->stop(); |
|
199 | animation->stop(); | |
200 | } |
|
200 | } | |
201 |
|
201 | |||
202 | animation->setDuration(duration); |
|
202 | animation->setDuration(duration); | |
203 | if(!empty) |
|
203 | if(!empty) | |
204 | animation->setAnimationType(ChartAnimation::MoveDownAnimation); |
|
204 | animation->setAnimationType(ChartAnimation::MoveDownAnimation); | |
205 | else |
|
205 | else | |
206 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); |
|
206 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); | |
207 |
|
207 | |||
208 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
208 | animation->setEasingCurve(QEasingCurve::OutQuart); | |
209 | animation->setValues(oldPoints,newPoints,oldControlPoints,newControlPoints,index); |
|
209 | animation->setValues(oldPoints,newPoints,oldControlPoints,newControlPoints,index); | |
210 |
|
210 | |||
211 | QTimer::singleShot(0,animation,SLOT(start())); |
|
211 | QTimer::singleShot(0,animation,SLOT(start())); | |
212 | } |
|
212 | } | |
213 |
|
213 | |||
214 |
|
214 | |||
215 | void ChartAnimator::updateLayout(XYChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, int index) |
|
215 | void ChartAnimator::updateLayout(XYChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, int index) | |
216 | { |
|
216 | { | |
217 | XYAnimation* animation = static_cast<XYAnimation*>(m_animations.value(item)); |
|
217 | XYAnimation* animation = static_cast<XYAnimation*>(m_animations.value(item)); | |
218 |
|
218 | |||
219 | Q_ASSERT(animation); |
|
219 | Q_ASSERT(animation); | |
220 |
|
220 | |||
221 | if(newPoints.count()==0) return; |
|
221 | if(newPoints.count()==0) return; | |
222 |
|
222 | |||
223 | bool empty = oldPoints.count()==0; |
|
223 | bool empty = oldPoints.count()==0; | |
224 |
|
224 | |||
225 |
|
225 | |||
226 | if(animation->state()!=QAbstractAnimation::Stopped) { |
|
226 | if(animation->state()!=QAbstractAnimation::Stopped) { | |
227 | animation->stop(); |
|
227 | animation->stop(); | |
228 | } |
|
228 | } | |
229 |
|
229 | |||
230 | animation->setDuration(duration); |
|
230 | animation->setDuration(duration); | |
231 | if(!empty) |
|
231 | if(!empty) | |
232 | animation->setAnimationType(ChartAnimation::MoveDownAnimation); |
|
232 | animation->setAnimationType(ChartAnimation::MoveDownAnimation); | |
233 | else |
|
233 | else | |
234 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); |
|
234 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); | |
235 |
|
235 | |||
236 | animation->setEasingCurve(QEasingCurve::OutQuart); |
|
236 | animation->setEasingCurve(QEasingCurve::OutQuart); | |
237 | animation->setValues(oldPoints,newPoints,index); |
|
237 | animation->setValues(oldPoints,newPoints,index); | |
238 |
|
238 | |||
239 | QTimer::singleShot(0,animation,SLOT(start())); |
|
239 | QTimer::singleShot(0,animation,SLOT(start())); | |
240 | } |
|
240 | } | |
241 |
|
241 | |||
242 | void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) |
|
242 | void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) | |
243 | { |
|
243 | { | |
244 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
244 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); | |
245 | Q_ASSERT(animation); |
|
245 | Q_ASSERT(animation); | |
246 | animation->addSlice(slice, sliceData, isEmpty); |
|
246 | animation->addSlice(slice, sliceData, isEmpty); | |
247 | } |
|
247 | } | |
248 |
|
248 | |||
249 | void ChartAnimator::removeAnimation(PieChartItem* item, QPieSlice *slice) |
|
249 | void ChartAnimator::removeAnimation(PieChartItem* item, QPieSlice *slice) | |
250 | { |
|
250 | { | |
251 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
251 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); | |
252 | Q_ASSERT(animation); |
|
252 | Q_ASSERT(animation); | |
253 | animation->removeSlice(slice); |
|
253 | animation->removeSlice(slice); | |
254 | } |
|
254 | } | |
255 |
|
255 | |||
256 | void ChartAnimator::updateLayout(PieChartItem* item, const PieLayout &layout) |
|
256 | void ChartAnimator::updateLayout(PieChartItem* item, const PieLayout &layout) | |
257 | { |
|
257 | { | |
258 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
258 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); | |
259 | Q_ASSERT(animation); |
|
259 | Q_ASSERT(animation); | |
260 | animation->updateValues(layout); |
|
260 | animation->updateValues(layout); | |
261 | } |
|
261 | } | |
262 |
|
262 | |||
263 | void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData) |
|
263 | void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData) | |
264 | { |
|
264 | { | |
265 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); |
|
265 | PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); | |
266 | Q_ASSERT(animation); |
|
266 | Q_ASSERT(animation); | |
267 | animation->updateValue(slice, sliceData); |
|
267 | animation->updateValue(slice, sliceData); | |
268 | } |
|
268 | } | |
269 |
|
269 | |||
270 | void ChartAnimator::updateLayout(BarChartItem* item, const BarLayout &layout) |
|
270 | void ChartAnimator::updateLayout(BarChartItem* item, const BarLayout &layout) | |
271 | { |
|
271 | { | |
272 | qDebug() << "ChartAnimator::updateLayout"; |
|
272 | qDebug() << "ChartAnimator::updateLayout"; | |
273 | BarAnimation* animation = static_cast<BarAnimation*>(m_animations.value(item)); |
|
273 | BarAnimation* animation = static_cast<BarAnimation*>(m_animations.value(item)); | |
274 | Q_ASSERT(animation); |
|
274 | Q_ASSERT(animation); | |
275 | animation->updateValues(layout); |
|
275 | animation->updateValues(layout); | |
276 | } |
|
276 | } | |
277 |
|
277 | |||
278 |
|
278 | |||
279 | void ChartAnimator::setState(State state,const QPointF& point) |
|
279 | void ChartAnimator::setState(State state,const QPointF& point) | |
280 | { |
|
280 | { | |
281 | m_state=state; |
|
281 | m_state=state; | |
282 | m_point=point; |
|
282 | m_point=point; | |
283 | } |
|
283 | } | |
284 |
|
284 | |||
285 | QTCOMMERCIALCHART_END_NAMESPACE |
|
285 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,58 | |||||
1 | #ifndef CHARTANIMATOR_P_H_ |
|
1 | #ifndef CHARTANIMATOR_P_H_ | |
2 | #define CHARTANIMATOR_P_H_ |
|
2 | #define CHARTANIMATOR_P_H_ | |
3 | #include "qchartglobal.h" |
|
3 | #include "qchartglobal.h" | |
4 | #include "chartanimation_p.h" |
|
4 | #include "chartanimation_p.h" | |
5 | #include "piechartitem_p.h" |
|
5 | #include "piechartitem_p.h" | |
6 | #include "barchartitem_p.h" |
|
6 | #include "barchartitem_p.h" | |
7 | #include <QPointF> |
|
7 | #include <QPointF> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class ChartItem; |
|
11 | class ChartItem; | |
12 |
class Axis |
|
12 | class Axis; | |
13 | class AreaChartItem; |
|
13 | class AreaChartItem; | |
14 | class SplineChartItem; |
|
14 | class SplineChartItem; | |
15 | class ScatterChartItem; |
|
15 | class ScatterChartItem; | |
16 | class LineChartItem; |
|
16 | class LineChartItem; | |
17 | class XYChartItem; |
|
17 | class XYChartItem; | |
18 |
|
18 | |||
19 | class ChartAnimator : public QObject { |
|
19 | class ChartAnimator : public QObject { | |
20 |
|
20 | |||
21 | public: |
|
21 | public: | |
22 | //TODO: this should be flags in case of two state at the time |
|
|||
23 | enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState,ScrollRightState,ZoomInState,ZoomOutState}; |
|
22 | enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState,ScrollRightState,ZoomInState,ZoomOutState}; | |
|
23 | Q_DECLARE_FLAGS(States, State); | |||
|
24 | ||||
24 | ChartAnimator(QObject *parent = 0); |
|
25 | ChartAnimator(QObject *parent = 0); | |
25 | virtual ~ChartAnimator(); |
|
26 | virtual ~ChartAnimator(); | |
26 |
|
27 | |||
27 |
void addAnimation(Axis |
|
28 | void addAnimation(Axis* item); | |
28 | void addAnimation(PieChartItem* item); |
|
29 | void addAnimation(PieChartItem* item); | |
29 | void addAnimation(ScatterChartItem* item); |
|
30 | void addAnimation(ScatterChartItem* item); | |
30 | void addAnimation(LineChartItem* item); |
|
31 | void addAnimation(LineChartItem* item); | |
31 | void addAnimation(SplineChartItem* item); |
|
32 | void addAnimation(SplineChartItem* item); | |
32 | void addAnimation(BarChartItem* item); |
|
33 | void addAnimation(BarChartItem* item); | |
33 |
void removeAnimation(Chart |
|
34 | void removeAnimation(Chart* item); | |
34 |
|
35 | |||
35 | void animationStarted(); |
|
36 | void animationStarted(); | |
36 | void updateLayout(XYChartItem* item, QVector<QPointF>& oldLayout,QVector<QPointF>& newLayout,int index); |
|
37 | void updateLayout(XYChartItem* item, QVector<QPointF>& oldLayout,QVector<QPointF>& newLayout,int index); | |
37 | void updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newContorlPoints,int index); |
|
38 | void updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newContorlPoints,int index); | |
38 |
void updateLayout(Axis |
|
39 | void updateLayout(Axis* item, QVector<qreal>& layout); | |
39 |
|
40 | |||
40 | void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty); |
|
41 | void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty); | |
41 | void removeAnimation(PieChartItem* item, QPieSlice *slice); |
|
42 | void removeAnimation(PieChartItem* item, QPieSlice *slice); | |
42 | void updateLayout(PieChartItem* item, const PieLayout &layout); |
|
43 | void updateLayout(PieChartItem* item, const PieLayout &layout); | |
43 | void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData); |
|
44 | void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData); | |
44 |
|
45 | |||
45 | void updateLayout(BarChartItem* item, const BarLayout &layout); |
|
46 | void updateLayout(BarChartItem* item, const BarLayout &layout); | |
46 |
|
47 | |||
47 | void setState(State state,const QPointF& point = QPointF()); |
|
48 | void setState(State state,const QPointF& point = QPointF()); | |
48 |
|
49 | |||
49 | private: |
|
50 | private: | |
50 |
QMap<Chart |
|
51 | QMap<Chart*,ChartAnimation*> m_animations; | |
51 | State m_state; |
|
52 | State m_state; | |
52 | QPointF m_point; |
|
53 | QPointF m_point; | |
53 | }; |
|
54 | }; | |
54 |
|
55 | |||
55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | QTCOMMERCIALCHART_END_NAMESPACE | |
56 |
|
57 | |||
57 | #endif |
|
58 | #endif |
@@ -1,88 +1,88 | |||||
1 | #include "xyanimation_p.h" |
|
1 | #include "xyanimation_p.h" | |
2 | #include "xychartitem_p.h" |
|
2 | #include "xychartitem_p.h" | |
3 |
|
3 | |||
4 | Q_DECLARE_METATYPE(QVector<QPointF>) |
|
4 | Q_DECLARE_METATYPE(QVector<QPointF>) | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item), |
|
8 | XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item), | |
9 | m_item(item), |
|
9 | m_item(item), | |
10 | m_dirty(false) |
|
10 | m_dirty(false) | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | XYAnimation::~XYAnimation() |
|
14 | XYAnimation::~XYAnimation() | |
15 | { |
|
15 | { | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | void XYAnimation::setValues(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index) |
|
18 | void XYAnimation::setValues(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index) | |
19 | { |
|
19 | { | |
20 |
|
20 | |||
21 | int x = oldPoints.count(); |
|
21 | int x = oldPoints.count(); | |
22 | int y = newPoints.count(); |
|
22 | int y = newPoints.count(); | |
23 |
|
23 | |||
24 | if(x!=y && abs(x-y)!=1) { |
|
24 | if(x!=y && abs(x-y)!=1) { | |
25 | m_oldPoints = newPoints; |
|
25 | m_oldPoints = newPoints; | |
26 | oldPoints.resize(newPoints.size()); |
|
26 | oldPoints.resize(newPoints.size()); | |
27 | setKeyValueAt(0.0, qVariantFromValue(oldPoints)); |
|
27 | setKeyValueAt(0.0, qVariantFromValue(oldPoints)); | |
28 | setKeyValueAt(1.0, qVariantFromValue(newPoints)); |
|
28 | setKeyValueAt(1.0, qVariantFromValue(newPoints)); | |
29 | m_dirty=false; |
|
29 | m_dirty=false; | |
30 | } |
|
30 | } | |
31 | else { |
|
31 | else { | |
32 | if(m_dirty) { |
|
32 | if(m_dirty) { | |
33 | m_oldPoints = oldPoints; |
|
33 | m_oldPoints = oldPoints; | |
34 | m_dirty=false; |
|
34 | m_dirty=false; | |
35 | } |
|
35 | } | |
36 | oldPoints = newPoints; |
|
36 | oldPoints = newPoints; | |
37 | if (y<x) (m_oldPoints.remove(index)); //remove |
|
37 | if (y<x) (m_oldPoints.remove(index)); //remove | |
38 | if (y>x) (m_oldPoints.insert(index,x>0?m_oldPoints[index-1]:newPoints[index])); //add |
|
38 | if (y>x) (m_oldPoints.insert(index,x>0?m_oldPoints[index-1]:newPoints[index])); //add | |
39 | setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); |
|
39 | setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); | |
40 | setKeyValueAt(1.0, qVariantFromValue(newPoints)); |
|
40 | setKeyValueAt(1.0, qVariantFromValue(newPoints)); | |
41 | Q_ASSERT(m_oldPoints.count() == newPoints.count()); |
|
41 | Q_ASSERT(m_oldPoints.count() == newPoints.count()); | |
42 | } |
|
42 | } | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | QVariant XYAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const |
|
45 | QVariant XYAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const | |
46 | { |
|
46 | { | |
47 | QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start); |
|
47 | QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start); | |
48 | QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end); |
|
48 | QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end); | |
49 | QVector<QPointF> result; |
|
49 | QVector<QPointF> result; | |
50 |
|
50 | |||
51 | switch(m_type) { |
|
51 | switch(m_type) { | |
52 |
|
52 | |||
53 | case MoveDownAnimation: { |
|
53 | case MoveDownAnimation: { | |
54 |
|
54 | |||
55 | if(startVector.count() != endVector.count()) break; |
|
55 | if(startVector.count() != endVector.count()) break; | |
56 |
|
56 | |||
57 | for(int i =0;i< startVector.count();i++) { |
|
57 | for(int i =0;i< startVector.count();i++) { | |
58 | qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress); |
|
58 | qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress); | |
59 | qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress); |
|
59 | qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress); | |
60 | result << QPointF(x,y); |
|
60 | result << QPointF(x,y); | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | } |
|
63 | } | |
64 | break; |
|
64 | break; | |
65 | case LineDrawAnimation:{ |
|
65 | case LineDrawAnimation:{ | |
66 | for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) { |
|
66 | for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) { | |
67 | result << endVector[i]; |
|
67 | result << endVector[i]; | |
68 | } |
|
68 | } | |
69 | } |
|
69 | } | |
70 | break; |
|
70 | break; | |
71 | default: |
|
71 | default: | |
72 | qWarning()<<"Unknow type of animation"; |
|
72 | qWarning()<<"Unknown type of animation"; | |
73 | break; |
|
73 | break; | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | return qVariantFromValue(result); |
|
76 | return qVariantFromValue(result); | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | void XYAnimation::updateCurrentValue (const QVariant & value ) |
|
79 | void XYAnimation::updateCurrentValue (const QVariant & value ) | |
80 | { |
|
80 | { | |
81 | if(state()!=QAbstractAnimation::Stopped){ //workaround |
|
81 | if(state()!=QAbstractAnimation::Stopped){ //workaround | |
82 | m_dirty=true; |
|
82 | m_dirty=true; | |
83 | QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value); |
|
83 | QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value); | |
84 | m_item->setLayout(vector); |
|
84 | m_item->setLayout(vector); | |
85 | } |
|
85 | } | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | QTCOMMERCIALCHART_END_NAMESPACE |
|
88 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,121 +1,121 | |||||
1 | #include "areachartitem_p.h" |
|
1 | #include "areachartitem_p.h" | |
2 | #include "qareaseries.h" |
|
2 | #include "qareaseries.h" | |
3 | #include "qlineseries.h" |
|
3 | #include "qlineseries.h" | |
4 | #include "chartpresenter_p.h" |
|
4 | #include "chartpresenter_p.h" | |
5 | #include <QPainter> |
|
5 | #include <QPainter> | |
6 | #include <QGraphicsSceneMouseEvent> |
|
6 | #include <QGraphicsSceneMouseEvent> | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | //TODO: optimize : remove points which are not visible |
|
11 | //TODO: optimize : remove points which are not visible | |
12 |
|
12 | |||
13 |
AreaChartItem::AreaChartItem(QAreaSeries* areaSeries, |
|
13 | AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,ChartPresenter *presenter):Chart(presenter),QGraphicsItem(presenter->rootItem()), | |
14 | m_series(areaSeries), |
|
14 | m_series(areaSeries), | |
15 | m_upper(0), |
|
15 | m_upper(0), | |
16 | m_lower(0), |
|
16 | m_lower(0), | |
17 | m_pointsVisible(false) |
|
17 | m_pointsVisible(false) | |
18 | { |
|
18 | { | |
19 | setZValue(ChartPresenter::LineChartZValue); |
|
19 | setZValue(ChartPresenter::LineChartZValue); | |
20 | m_upper = new AreaBoundItem(this,m_series->upperSeries()); |
|
20 | m_upper = new AreaBoundItem(this,m_series->upperSeries(),presenter); | |
21 | if(m_series->lowerSeries()){ |
|
21 | if(m_series->lowerSeries()){ | |
22 | m_lower = new AreaBoundItem(this,m_series->lowerSeries()); |
|
22 | m_lower = new AreaBoundItem(this,m_series->lowerSeries(),presenter); | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
25 | QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated())); | |
26 | QObject::connect(this,SIGNAL(clicked(const QPointF&)),areaSeries,SIGNAL(clicked(const QPointF&))); |
|
26 | QObject::connect(this,SIGNAL(clicked(const QPointF&)),areaSeries,SIGNAL(clicked(const QPointF&))); | |
27 |
|
27 | |||
28 | handleUpdated(); |
|
28 | handleUpdated(); | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | AreaChartItem::~AreaChartItem() |
|
31 | AreaChartItem::~AreaChartItem() | |
32 | { |
|
32 | { | |
33 | delete m_upper; |
|
33 | delete m_upper; | |
34 | delete m_lower; |
|
34 | delete m_lower; | |
35 | }; |
|
35 | }; | |
36 |
|
36 | |||
37 | QRectF AreaChartItem::boundingRect() const |
|
37 | QRectF AreaChartItem::boundingRect() const | |
38 | { |
|
38 | { | |
39 | return m_rect; |
|
39 | return m_rect; | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | QPainterPath AreaChartItem::shape() const |
|
42 | QPainterPath AreaChartItem::shape() const | |
43 | { |
|
43 | { | |
44 | return m_path; |
|
44 | return m_path; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | void AreaChartItem::updatePath() |
|
47 | void AreaChartItem::updatePath() | |
48 | { |
|
48 | { | |
49 | QPainterPath path; |
|
49 | QPainterPath path; | |
50 |
|
50 | |||
51 | path.connectPath(m_upper->shape()); |
|
51 | path.connectPath(m_upper->shape()); | |
52 | if(m_lower){ |
|
52 | if(m_lower){ | |
53 | path.connectPath(m_lower->shape().toReversed()); |
|
53 | path.connectPath(m_lower->shape().toReversed()); | |
54 | } |
|
54 | } | |
55 | else{ |
|
55 | else{ | |
56 | QPointF first = path.pointAtPercent(0); |
|
56 | QPointF first = path.pointAtPercent(0); | |
57 | QPointF last = path.pointAtPercent(1); |
|
57 | QPointF last = path.pointAtPercent(1); | |
58 | path.lineTo(last.x(),m_clipRect.bottom()); |
|
58 | path.lineTo(last.x(),m_clipRect.bottom()); | |
59 | path.lineTo(first.x(),m_clipRect.bottom()); |
|
59 | path.lineTo(first.x(),m_clipRect.bottom()); | |
60 | } |
|
60 | } | |
61 | path.closeSubpath(); |
|
61 | path.closeSubpath(); | |
62 | prepareGeometryChange(); |
|
62 | prepareGeometryChange(); | |
63 | m_path=path; |
|
63 | m_path=path; | |
64 | m_rect=path.boundingRect(); |
|
64 | m_rect=path.boundingRect(); | |
65 | update(); |
|
65 | update(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | void AreaChartItem::handleUpdated() |
|
68 | void AreaChartItem::handleUpdated() | |
69 | { |
|
69 | { | |
70 | m_pointsVisible = m_series->pointsVisible(); |
|
70 | m_pointsVisible = m_series->pointsVisible(); | |
71 | m_linePen = m_series->pen(); |
|
71 | m_linePen = m_series->pen(); | |
72 | m_brush = m_series->brush(); |
|
72 | m_brush = m_series->brush(); | |
73 | m_pointPen = m_series->pen(); |
|
73 | m_pointPen = m_series->pen(); | |
74 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
74 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
75 |
|
75 | |||
76 | update(); |
|
76 | update(); | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
79 | void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | |
80 | { |
|
80 | { | |
81 | m_upper->handleDomainChanged(minX,maxX,minY,maxY); |
|
81 | m_upper->handleDomainChanged(minX,maxX,minY,maxY); | |
82 | if(m_lower) |
|
82 | if(m_lower) | |
83 | m_lower->handleDomainChanged(minX,maxX,minY,maxY); |
|
83 | m_lower->handleDomainChanged(minX,maxX,minY,maxY); | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | void AreaChartItem::handleGeometryChanged(const QRectF& rect) |
|
86 | void AreaChartItem::handleGeometryChanged(const QRectF& rect) | |
87 | { |
|
87 | { | |
88 | m_clipRect=rect.translated(-rect.topLeft()); |
|
88 | m_clipRect=rect.translated(-rect.topLeft()); | |
89 | setPos(rect.topLeft()); |
|
89 | setPos(rect.topLeft()); | |
90 | m_upper->handleGeometryChanged(rect); |
|
90 | m_upper->handleGeometryChanged(rect); | |
91 | if(m_lower) |
|
91 | if(m_lower) | |
92 | m_lower->handleGeometryChanged(rect); |
|
92 | m_lower->handleGeometryChanged(rect); | |
93 | } |
|
93 | } | |
94 | //painter |
|
94 | //painter | |
95 |
|
95 | |||
96 | void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
96 | void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
97 | { |
|
97 | { | |
98 | Q_UNUSED(widget) |
|
98 | Q_UNUSED(widget) | |
99 | Q_UNUSED(option) |
|
99 | Q_UNUSED(option) | |
100 |
|
100 | |||
101 | painter->save(); |
|
101 | painter->save(); | |
102 | painter->setPen(m_linePen); |
|
102 | painter->setPen(m_linePen); | |
103 | painter->setBrush(m_brush); |
|
103 | painter->setBrush(m_brush); | |
104 | painter->setClipRect(m_clipRect); |
|
104 | painter->setClipRect(m_clipRect); | |
105 | painter->drawPath(m_path); |
|
105 | painter->drawPath(m_path); | |
106 | if(m_pointsVisible){ |
|
106 | if(m_pointsVisible){ | |
107 | painter->setPen(m_pointPen); |
|
107 | painter->setPen(m_pointPen); | |
108 | painter->drawPoints(m_upper->points()); |
|
108 | painter->drawPoints(m_upper->points()); | |
109 | if(m_lower) painter->drawPoints(m_lower->points()); |
|
109 | if(m_lower) painter->drawPoints(m_lower->points()); | |
110 | } |
|
110 | } | |
111 | painter->restore(); |
|
111 | painter->restore(); | |
112 | } |
|
112 | } | |
113 |
|
113 | |||
114 | void AreaChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event ) |
|
114 | void AreaChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event ) | |
115 | { |
|
115 | { | |
116 | emit clicked(m_upper->calculateDomainPoint(event->pos())); |
|
116 | emit clicked(m_upper->calculateDomainPoint(event->pos())); | |
117 | } |
|
117 | } | |
118 |
|
118 | |||
119 | #include "moc_areachartitem_p.cpp" |
|
119 | #include "moc_areachartitem_p.cpp" | |
120 |
|
120 | |||
121 | QTCOMMERCIALCHART_END_NAMESPACE |
|
121 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,75 +1,75 | |||||
1 | #ifndef AREACHARTITEM_H |
|
1 | #ifndef AREACHARTITEM_H | |
2 | #define AREACHARTITEM_H |
|
2 | #define AREACHARTITEM_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "linechartitem_p.h" |
|
5 | #include "linechartitem_p.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QAreaSeries; |
|
10 | class QAreaSeries; | |
11 | class AreaChartItem; |
|
11 | class AreaChartItem; | |
12 |
|
12 | |||
13 |
class AreaChartItem : |
|
13 | class AreaChartItem : public Chart, public QGraphicsItem | |
14 | { |
|
14 | { | |
15 | Q_OBJECT |
|
15 | Q_OBJECT | |
16 | public: |
|
16 | public: | |
17 |
AreaChartItem(QAreaSeries* areaSeries, |
|
17 | AreaChartItem(QAreaSeries* areaSeries, ChartPresenter *presenter); | |
18 | ~ AreaChartItem(); |
|
18 | ~ AreaChartItem(); | |
19 |
|
19 | |||
20 | //from QGraphicsItem |
|
20 | //from QGraphicsItem | |
21 | QRectF boundingRect() const; |
|
21 | QRectF boundingRect() const; | |
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
23 | QPainterPath shape() const; |
|
23 | QPainterPath shape() const; | |
24 |
|
24 | |||
25 | LineChartItem* upperLineItem() const { return m_upper ;} |
|
25 | LineChartItem* upperLineItem() const { return m_upper ;} | |
26 | LineChartItem* lowerLineItem() const { return m_lower ;} |
|
26 | LineChartItem* lowerLineItem() const { return m_lower ;} | |
27 |
|
27 | |||
28 | void updatePath(); |
|
28 | void updatePath(); | |
29 |
|
29 | |||
30 | protected: |
|
30 | protected: | |
31 | void mousePressEvent( QGraphicsSceneMouseEvent * event ); |
|
31 | void mousePressEvent( QGraphicsSceneMouseEvent * event ); | |
32 |
|
32 | |||
33 | signals: |
|
33 | signals: | |
34 | void clicked(const QPointF& point); |
|
34 | void clicked(const QPointF& point); | |
35 |
|
35 | |||
36 | public slots: |
|
36 | public slots: | |
37 | void handleUpdated(); |
|
37 | void handleUpdated(); | |
38 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
38 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); | |
39 | void handleGeometryChanged(const QRectF& size); |
|
39 | void handleGeometryChanged(const QRectF& size); | |
40 |
|
40 | |||
41 | private: |
|
41 | private: | |
42 | QAreaSeries* m_series; |
|
42 | QAreaSeries* m_series; | |
43 | LineChartItem* m_upper; |
|
43 | LineChartItem* m_upper; | |
44 | LineChartItem* m_lower; |
|
44 | LineChartItem* m_lower; | |
45 | QPainterPath m_path; |
|
45 | QPainterPath m_path; | |
46 | QRectF m_rect; |
|
46 | QRectF m_rect; | |
47 | QRectF m_clipRect; |
|
47 | QRectF m_clipRect; | |
48 | QPen m_linePen; |
|
48 | QPen m_linePen; | |
49 | QPen m_pointPen; |
|
49 | QPen m_pointPen; | |
50 | QBrush m_brush; |
|
50 | QBrush m_brush; | |
51 | bool m_pointsVisible; |
|
51 | bool m_pointsVisible; | |
52 |
|
52 | |||
53 | }; |
|
53 | }; | |
54 |
|
54 | |||
55 | class AreaBoundItem : public LineChartItem |
|
55 | class AreaBoundItem : public LineChartItem | |
56 | { |
|
56 | { | |
57 | public: |
|
57 | public: | |
58 | AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries), |
|
58 | AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries,ChartPresenter *presenter):LineChartItem(lineSeries,presenter), | |
59 | m_item(item){}; |
|
59 | m_item(item){}; | |
60 |
|
60 | |||
61 | ~AreaBoundItem(){}; |
|
61 | ~AreaBoundItem(){}; | |
62 |
|
62 | |||
63 | void setLayout(QVector<QPointF>& points){ |
|
63 | void setLayout(QVector<QPointF>& points){ | |
64 | LineChartItem::setLayout(points); |
|
64 | LineChartItem::setLayout(points); | |
65 | m_item->updatePath(); |
|
65 | m_item->updatePath(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | private: |
|
68 | private: | |
69 | AreaChartItem* m_item; |
|
69 | AreaChartItem* m_item; | |
70 |
|
70 | |||
71 | }; |
|
71 | }; | |
72 |
|
72 | |||
73 | QTCOMMERCIALCHART_END_NAMESPACE |
|
73 | QTCOMMERCIALCHART_END_NAMESPACE | |
74 |
|
74 | |||
75 | #endif |
|
75 | #endif |
@@ -1,406 +1,397 | |||||
1 | #include "axisitem_p.h" |
|
1 | #include "axisitem_p.h" | |
2 | #include "qchartaxis.h" |
|
2 | #include "qchartaxis.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include "chartanimator_p.h" |
|
4 | #include "chartanimator_p.h" | |
5 | #include <QPainter> |
|
5 | #include <QPainter> | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | static int label_padding = 5; |
|
8 | static int label_padding = 5; | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 |
Axis |
|
12 | Axis::Axis(QChartAxis* axis,ChartPresenter* presenter,AxisType type) : | |
13 |
Chart |
|
13 | Chart(presenter), | |
14 | m_presenter(presenter), |
|
|||
15 | m_chartAxis(axis), |
|
14 | m_chartAxis(axis), | |
16 | m_type(type), |
|
15 | m_type(type), | |
17 | m_labelsAngle(0), |
|
16 | m_labelsAngle(0), | |
18 |
m_grid(p |
|
17 | m_grid(presenter->rootItem()), | |
19 |
m_shades(p |
|
18 | m_shades(presenter->rootItem()), | |
20 |
m_labels(p |
|
19 | m_labels(presenter->rootItem()), | |
21 |
m_axis(p |
|
20 | m_axis(presenter->rootItem()), | |
22 | m_min(0), |
|
21 | m_min(0), | |
23 | m_max(0), |
|
22 | m_max(0), | |
24 | m_ticksCount(0) |
|
23 | m_ticksCount(0) | |
25 | { |
|
24 | { | |
26 | //initial initialization |
|
25 | //initial initialization | |
27 | m_axis.setZValue(ChartPresenter::AxisZValue); |
|
26 | m_axis.setZValue(ChartPresenter::AxisZValue); | |
|
27 | m_axis.setHandlesChildEvents(false); | |||
|
28 | ||||
28 | m_shades.setZValue(ChartPresenter::ShadesZValue); |
|
29 | m_shades.setZValue(ChartPresenter::ShadesZValue); | |
29 | m_grid.setZValue(ChartPresenter::GridZValue); |
|
30 | m_grid.setZValue(ChartPresenter::GridZValue); | |
30 | setFlags(QGraphicsItem::ItemHasNoContents); |
|
|||
31 |
|
31 | |||
32 | QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); |
|
32 | QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); | |
33 | QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); |
|
33 | QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); | |
34 |
|
34 | |||
35 | handleAxisUpdated(); |
|
35 | handleAxisUpdated(); | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 |
Axis |
|
38 | Axis::~Axis() | |
39 | { |
|
|||
40 | } |
|
|||
41 |
|
||||
42 | QRectF AxisItem::boundingRect() const |
|
|||
43 | { |
|
39 | { | |
44 | return QRectF(); |
|
|||
45 | } |
|
40 | } | |
46 |
|
41 | |||
47 |
void Axis |
|
42 | void Axis::createItems(int count) | |
48 | { |
|
43 | { | |
49 |
|
44 | |||
50 | if(m_axis.children().size()==0) |
|
45 | if(m_axis.children().size()==0) | |
51 |
m_axis.addToGroup(new |
|
46 | m_axis.addToGroup(new AxisItem(this)); | |
52 | for (int i = 0; i < count; ++i) { |
|
47 | for (int i = 0; i < count; ++i) { | |
53 | m_grid.addToGroup(new QGraphicsLineItem()); |
|
48 | m_grid.addToGroup(new QGraphicsLineItem()); | |
54 | m_labels.addToGroup(new QGraphicsSimpleTextItem()); |
|
49 | m_labels.addToGroup(new QGraphicsSimpleTextItem()); | |
55 | m_axis.addToGroup(new QGraphicsLineItem()); |
|
50 | m_axis.addToGroup(new QGraphicsLineItem()); | |
56 | if((m_grid.childItems().size())%2 && m_grid.childItems().size()>2) m_shades.addToGroup(new QGraphicsRectItem()); |
|
51 | if((m_grid.childItems().size())%2 && m_grid.childItems().size()>2) m_shades.addToGroup(new QGraphicsRectItem()); | |
57 | } |
|
52 | } | |
58 | } |
|
53 | } | |
59 |
|
54 | |||
60 |
void Axis |
|
55 | void Axis::deleteItems(int count) | |
61 | { |
|
56 | { | |
62 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
57 | QList<QGraphicsItem *> lines = m_grid.childItems(); | |
63 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
|
58 | QList<QGraphicsItem *> labels = m_labels.childItems(); | |
64 | QList<QGraphicsItem *> shades = m_shades.childItems(); |
|
59 | QList<QGraphicsItem *> shades = m_shades.childItems(); | |
65 | QList<QGraphicsItem *> axis = m_axis.childItems(); |
|
60 | QList<QGraphicsItem *> axis = m_axis.childItems(); | |
66 |
|
61 | |||
67 | for (int i = 0; i < count; ++i) { |
|
62 | for (int i = 0; i < count; ++i) { | |
68 | if(lines.size()%2 && lines.size()>1) delete(shades.takeLast()); |
|
63 | if(lines.size()%2 && lines.size()>1) delete(shades.takeLast()); | |
69 | delete(lines.takeLast()); |
|
64 | delete(lines.takeLast()); | |
70 | delete(labels.takeLast()); |
|
65 | delete(labels.takeLast()); | |
71 | delete(axis.takeLast()); |
|
66 | delete(axis.takeLast()); | |
72 | } |
|
67 | } | |
73 | } |
|
68 | } | |
74 |
|
69 | |||
75 |
void Axis |
|
70 | void Axis::updateLayout(QVector<qreal>& layout) | |
76 | { |
|
71 | { | |
77 |
if( |
|
72 | if(animator()){ | |
78 |
|
|
73 | animator()->updateLayout(this,layout); | |
79 | } |
|
74 | } | |
80 | else setLayout(layout); |
|
75 | else setLayout(layout); | |
81 | } |
|
76 | } | |
82 |
|
77 | |||
83 |
QStringList Axis |
|
78 | QStringList Axis::createLabels(int ticks, qreal min, qreal max) const | |
84 | { |
|
79 | { | |
85 | Q_ASSERT(max>=min); |
|
80 | Q_ASSERT(max>=min); | |
86 | Q_ASSERT(ticks>0); |
|
81 | Q_ASSERT(ticks>0); | |
87 |
|
82 | |||
88 | QStringList labels; |
|
83 | QStringList labels; | |
89 |
|
84 | |||
90 | QChartAxisCategories* categories = m_chartAxis->categories(); |
|
85 | QChartAxisCategories* categories = m_chartAxis->categories(); | |
91 |
|
86 | |||
92 | for(int i=0; i< ticks; i++) { |
|
87 | for(int i=0; i< ticks; i++) { | |
93 | qreal value = min + (i * (max - min)/ (ticks-1)); |
|
88 | qreal value = min + (i * (max - min)/ (ticks-1)); | |
94 | if(categories->count()==0) { |
|
89 | if(categories->count()==0) { | |
95 | labels << QString::number(value); |
|
90 | labels << QString::number(value); | |
96 | } |
|
91 | } | |
97 | else { |
|
92 | else { | |
98 |
|
93 | |||
99 | QString label = categories->label(value); |
|
94 | QString label = categories->label(value); | |
100 | labels << label; |
|
95 | labels << label; | |
101 | } |
|
96 | } | |
102 | } |
|
97 | } | |
103 | return labels; |
|
98 | return labels; | |
104 | } |
|
99 | } | |
105 |
|
100 | |||
106 |
void Axis |
|
101 | void Axis::setAxisOpacity(qreal opacity) | |
107 | { |
|
102 | { | |
108 | m_axis.setOpacity(opacity); |
|
103 | m_axis.setOpacity(opacity); | |
109 | } |
|
104 | } | |
110 |
|
105 | |||
111 |
qreal Axis |
|
106 | qreal Axis::axisOpacity() const | |
112 | { |
|
107 | { | |
113 | return m_axis.opacity(); |
|
108 | return m_axis.opacity(); | |
114 | } |
|
109 | } | |
115 |
|
110 | |||
116 |
void Axis |
|
111 | void Axis::setGridOpacity(qreal opacity) | |
117 | { |
|
112 | { | |
118 | m_grid.setOpacity(opacity); |
|
113 | m_grid.setOpacity(opacity); | |
119 | } |
|
114 | } | |
120 |
|
115 | |||
121 |
qreal Axis |
|
116 | qreal Axis::gridOpacity() const | |
122 | { |
|
117 | { | |
123 | return m_grid.opacity(); |
|
118 | return m_grid.opacity(); | |
124 | } |
|
119 | } | |
125 |
|
120 | |||
126 |
void Axis |
|
121 | void Axis::setLabelsOpacity(qreal opacity) | |
127 | { |
|
122 | { | |
128 | m_labels.setOpacity(opacity); |
|
123 | m_labels.setOpacity(opacity); | |
129 | } |
|
124 | } | |
130 |
|
125 | |||
131 |
qreal Axis |
|
126 | qreal Axis::labelsOpacity() const | |
132 | { |
|
127 | { | |
133 | return m_labels.opacity(); |
|
128 | return m_labels.opacity(); | |
134 | } |
|
129 | } | |
135 |
|
130 | |||
136 |
void Axis |
|
131 | void Axis::setShadesOpacity(qreal opacity) | |
137 | { |
|
132 | { | |
138 | m_shades.setOpacity(opacity); |
|
133 | m_shades.setOpacity(opacity); | |
139 | } |
|
134 | } | |
140 |
|
135 | |||
141 |
qreal Axis |
|
136 | qreal Axis::shadesOpacity() const | |
142 | { |
|
137 | { | |
143 | return m_shades.opacity(); |
|
138 | return m_shades.opacity(); | |
144 | } |
|
139 | } | |
145 |
|
140 | |||
146 |
void Axis |
|
141 | void Axis::setLabelsAngle(int angle) | |
147 | { |
|
142 | { | |
148 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
143 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
149 | QPointF center = item->boundingRect().center(); |
|
144 | QPointF center = item->boundingRect().center(); | |
150 | item->setRotation(angle); |
|
145 | item->setRotation(angle); | |
151 | } |
|
146 | } | |
152 |
|
147 | |||
153 | m_labelsAngle=angle; |
|
148 | m_labelsAngle=angle; | |
154 | } |
|
149 | } | |
155 |
|
150 | |||
156 |
void Axis |
|
151 | void Axis::setLabelsPen(const QPen& pen) | |
157 | { |
|
152 | { | |
158 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
153 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
159 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); |
|
154 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); | |
160 | } |
|
155 | } | |
161 | } |
|
156 | } | |
162 |
|
157 | |||
163 |
void Axis |
|
158 | void Axis::setLabelsBrush(const QBrush& brush) | |
164 | { |
|
159 | { | |
165 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
160 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
166 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); |
|
161 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); | |
167 | } |
|
162 | } | |
168 | } |
|
163 | } | |
169 |
|
164 | |||
170 |
void Axis |
|
165 | void Axis::setLabelsFont(const QFont& font) | |
171 | { |
|
166 | { | |
172 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
167 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
173 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); |
|
168 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); | |
174 | } |
|
169 | } | |
175 | } |
|
170 | } | |
176 |
|
171 | |||
177 |
void Axis |
|
172 | void Axis::setShadesBrush(const QBrush& brush) | |
178 | { |
|
173 | { | |
179 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
174 | foreach(QGraphicsItem* item , m_shades.childItems()) { | |
180 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); |
|
175 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); | |
181 | } |
|
176 | } | |
182 | } |
|
177 | } | |
183 |
|
178 | |||
184 |
void Axis |
|
179 | void Axis::setShadesPen(const QPen& pen) | |
185 | { |
|
180 | { | |
186 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
181 | foreach(QGraphicsItem* item , m_shades.childItems()) { | |
187 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); |
|
182 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); | |
188 | } |
|
183 | } | |
189 | } |
|
184 | } | |
190 |
|
185 | |||
191 |
void Axis |
|
186 | void Axis::setAxisPen(const QPen& pen) | |
192 | { |
|
187 | { | |
193 | foreach(QGraphicsItem* item , m_axis.childItems()) { |
|
188 | foreach(QGraphicsItem* item , m_axis.childItems()) { | |
194 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
189 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | |
195 | } |
|
190 | } | |
196 | } |
|
191 | } | |
197 |
|
192 | |||
198 |
void Axis |
|
193 | void Axis::setGridPen(const QPen& pen) | |
199 | { |
|
194 | { | |
200 | foreach(QGraphicsItem* item , m_grid.childItems()) { |
|
195 | foreach(QGraphicsItem* item , m_grid.childItems()) { | |
201 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
196 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | |
202 | } |
|
197 | } | |
203 | } |
|
198 | } | |
204 |
|
199 | |||
205 |
QVector<qreal> Axis |
|
200 | QVector<qreal> Axis::calculateLayout() const | |
206 | { |
|
201 | { | |
207 | Q_ASSERT(m_ticksCount>=2); |
|
202 | Q_ASSERT(m_ticksCount>=2); | |
208 |
|
203 | |||
209 | QVector<qreal> points; |
|
204 | QVector<qreal> points; | |
210 | points.resize(m_ticksCount); |
|
205 | points.resize(m_ticksCount); | |
211 |
|
206 | |||
212 | switch (m_type) |
|
207 | switch (m_type) | |
213 | { |
|
208 | { | |
214 | case X_AXIS: |
|
209 | case X_AXIS: | |
215 | { |
|
210 | { | |
216 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); |
|
211 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); | |
217 | for (int i = 0; i < m_ticksCount; ++i) { |
|
212 | for (int i = 0; i < m_ticksCount; ++i) { | |
218 | int x = i * deltaX + m_rect.left(); |
|
213 | int x = i * deltaX + m_rect.left(); | |
219 | points[i] = x; |
|
214 | points[i] = x; | |
220 | } |
|
215 | } | |
221 | } |
|
216 | } | |
222 | break; |
|
217 | break; | |
223 | case Y_AXIS: |
|
218 | case Y_AXIS: | |
224 | { |
|
219 | { | |
225 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); |
|
220 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); | |
226 | for (int i = 0; i < m_ticksCount; ++i) { |
|
221 | for (int i = 0; i < m_ticksCount; ++i) { | |
227 | int y = i * -deltaY + m_rect.bottom(); |
|
222 | int y = i * -deltaY + m_rect.bottom(); | |
228 | points[i] = y; |
|
223 | points[i] = y; | |
229 | } |
|
224 | } | |
230 | } |
|
225 | } | |
231 | break; |
|
226 | break; | |
232 | } |
|
227 | } | |
233 | return points; |
|
228 | return points; | |
234 | } |
|
229 | } | |
235 |
|
230 | |||
236 |
void Axis |
|
231 | void Axis::setLayout(QVector<qreal>& layout) | |
237 | { |
|
232 | { | |
238 | int diff = m_layoutVector.size() - layout.size(); |
|
233 | int diff = m_layoutVector.size() - layout.size(); | |
239 |
|
234 | |||
240 | if(diff>0) { |
|
235 | if(diff>0) { | |
241 | deleteItems(diff); |
|
236 | deleteItems(diff); | |
242 | } |
|
237 | } | |
243 | else if(diff<0) { |
|
238 | else if(diff<0) { | |
244 | createItems(-diff); |
|
239 | createItems(-diff); | |
245 | } |
|
240 | } | |
246 |
|
241 | |||
247 | if(diff!=0) handleAxisUpdated(); |
|
242 | if(diff!=0) handleAxisUpdated(); | |
248 |
|
243 | |||
249 | QStringList ticksList = createLabels(layout.size(),m_min,m_max); |
|
244 | QStringList ticksList = createLabels(layout.size(),m_min,m_max); | |
250 |
|
245 | |||
251 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
246 | QList<QGraphicsItem *> lines = m_grid.childItems(); | |
252 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
|
247 | QList<QGraphicsItem *> labels = m_labels.childItems(); | |
253 | QList<QGraphicsItem *> shades = m_shades.childItems(); |
|
248 | QList<QGraphicsItem *> shades = m_shades.childItems(); | |
254 | QList<QGraphicsItem *> axis = m_axis.childItems(); |
|
249 | QList<QGraphicsItem *> axis = m_axis.childItems(); | |
255 |
|
250 | |||
256 | Q_ASSERT(labels.size() == ticksList.size()); |
|
251 | Q_ASSERT(labels.size() == ticksList.size()); | |
257 | Q_ASSERT(layout.size() == ticksList.size()); |
|
252 | Q_ASSERT(layout.size() == ticksList.size()); | |
258 |
|
253 | |||
259 | switch (m_type) |
|
254 | switch (m_type) | |
260 | { |
|
255 | { | |
261 | case X_AXIS: |
|
256 | case X_AXIS: | |
262 | { |
|
257 | { | |
263 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
258 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
264 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
259 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
265 |
|
260 | |||
266 | for (int i = 0; i < layout.size(); ++i) { |
|
261 | for (int i = 0; i < layout.size(); ++i) { | |
267 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
262 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
268 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
263 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); | |
269 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
264 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
270 | labelItem->setText(ticksList.at(i)); |
|
265 | labelItem->setText(ticksList.at(i)); | |
271 | QPointF center = labelItem->boundingRect().center(); |
|
266 | QPointF center = labelItem->boundingRect().center(); | |
272 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
267 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
273 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); |
|
268 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); | |
274 | if((i+1)%2 && i>1) { |
|
269 | if((i+1)%2 && i>1) { | |
275 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
270 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
276 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); |
|
271 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); | |
277 | } |
|
272 | } | |
278 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
273 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
279 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); |
|
274 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); | |
280 | } |
|
275 | } | |
281 | } |
|
276 | } | |
282 | break; |
|
277 | break; | |
283 |
|
278 | |||
284 | case Y_AXIS: |
|
279 | case Y_AXIS: | |
285 | { |
|
280 | { | |
286 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
281 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
287 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
282 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
288 |
|
283 | |||
289 | for (int i = 0; i < layout.size(); ++i) { |
|
284 | for (int i = 0; i < layout.size(); ++i) { | |
290 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
285 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
291 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); |
|
286 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); | |
292 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
287 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
293 | labelItem->setText(ticksList.at(i)); |
|
288 | labelItem->setText(ticksList.at(i)); | |
294 | QPointF center = labelItem->boundingRect().center(); |
|
289 | QPointF center = labelItem->boundingRect().center(); | |
295 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
290 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
296 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); |
|
291 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); | |
297 | if((i+1)%2 && i>1) { |
|
292 | if((i+1)%2 && i>1) { | |
298 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
293 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
299 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); |
|
294 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); | |
300 | } |
|
295 | } | |
301 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
296 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
302 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); |
|
297 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); | |
303 | } |
|
298 | } | |
304 | } |
|
299 | } | |
305 | break; |
|
300 | break; | |
306 | default: |
|
301 | default: | |
307 | qDebug()<<"Unknown axis type"; |
|
302 | qDebug()<<"Unknown axis type"; | |
308 | break; |
|
303 | break; | |
309 | } |
|
304 | } | |
310 |
|
305 | |||
311 | m_layoutVector=layout; |
|
306 | m_layoutVector=layout; | |
312 | } |
|
307 | } | |
313 |
|
308 | |||
314 |
bool Axis |
|
309 | bool Axis::isEmpty() | |
315 | { |
|
310 | { | |
316 | return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0; |
|
311 | return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0; | |
317 | } |
|
312 | } | |
318 |
|
313 | |||
319 | //handlers |
|
314 | //handlers | |
320 |
|
315 | |||
321 |
void Axis |
|
316 | void Axis::handleAxisCategoriesUpdated() | |
322 | { |
|
317 | { | |
323 | if(isEmpty()) return; |
|
318 | if(isEmpty()) return; | |
324 | updateLayout(m_layoutVector); |
|
319 | updateLayout(m_layoutVector); | |
325 | } |
|
320 | } | |
326 |
|
321 | |||
327 |
void Axis |
|
322 | void Axis::handleAxisUpdated() | |
328 | { |
|
323 | { | |
329 |
|
324 | |||
330 | if(isEmpty()) return; |
|
325 | if(isEmpty()) return; | |
331 |
|
326 | |||
332 | if(m_chartAxis->isAxisVisible()) { |
|
327 | if(m_chartAxis->isAxisVisible()) { | |
333 | setAxisOpacity(100); |
|
328 | setAxisOpacity(100); | |
334 | } |
|
329 | } | |
335 | else { |
|
330 | else { | |
336 | setAxisOpacity(0); |
|
331 | setAxisOpacity(0); | |
337 | } |
|
332 | } | |
338 |
|
333 | |||
339 | if(m_chartAxis->isGridLineVisible()) { |
|
334 | if(m_chartAxis->isGridLineVisible()) { | |
340 | setGridOpacity(100); |
|
335 | setGridOpacity(100); | |
341 | } |
|
336 | } | |
342 | else { |
|
337 | else { | |
343 | setGridOpacity(0); |
|
338 | setGridOpacity(0); | |
344 | } |
|
339 | } | |
345 |
|
340 | |||
346 | if(m_chartAxis->labelsVisible()) |
|
341 | if(m_chartAxis->labelsVisible()) | |
347 | { |
|
342 | { | |
348 | setLabelsOpacity(100); |
|
343 | setLabelsOpacity(100); | |
349 | } |
|
344 | } | |
350 | else { |
|
345 | else { | |
351 | setLabelsOpacity(0); |
|
346 | setLabelsOpacity(0); | |
352 | } |
|
347 | } | |
353 |
|
348 | |||
354 | if(m_chartAxis->shadesVisible()) { |
|
349 | if(m_chartAxis->shadesVisible()) { | |
355 | setShadesOpacity(m_chartAxis->shadesOpacity()); |
|
350 | setShadesOpacity(m_chartAxis->shadesOpacity()); | |
356 | } |
|
351 | } | |
357 | else { |
|
352 | else { | |
358 | setShadesOpacity(0); |
|
353 | setShadesOpacity(0); | |
359 | } |
|
354 | } | |
360 |
|
355 | |||
361 | setLabelsAngle(m_chartAxis->labelsAngle()); |
|
356 | setLabelsAngle(m_chartAxis->labelsAngle()); | |
362 | setAxisPen(m_chartAxis->axisPen()); |
|
357 | setAxisPen(m_chartAxis->axisPen()); | |
363 | setLabelsPen(m_chartAxis->labelsPen()); |
|
358 | setLabelsPen(m_chartAxis->labelsPen()); | |
364 | setLabelsBrush(m_chartAxis->labelsBrush()); |
|
359 | setLabelsBrush(m_chartAxis->labelsBrush()); | |
365 | setLabelsFont(m_chartAxis->labelsFont()); |
|
360 | setLabelsFont(m_chartAxis->labelsFont()); | |
366 | setGridPen(m_chartAxis->gridLinePen()); |
|
361 | setGridPen(m_chartAxis->gridLinePen()); | |
367 | setShadesPen(m_chartAxis->shadesPen()); |
|
362 | setShadesPen(m_chartAxis->shadesPen()); | |
368 | setShadesBrush(m_chartAxis->shadesBrush()); |
|
363 | setShadesBrush(m_chartAxis->shadesBrush()); | |
369 |
|
364 | |||
370 | } |
|
365 | } | |
371 |
|
366 | |||
372 |
void Axis |
|
367 | void Axis::handleRangeChanged(qreal min, qreal max,int tickCount) | |
373 | { |
|
368 | { | |
374 | if(min==max || tickCount<2) return; |
|
369 | if(min==max || tickCount<2) return; | |
375 |
|
370 | |||
376 | m_min = min; |
|
371 | m_min = min; | |
377 | m_max = max; |
|
372 | m_max = max; | |
378 | m_ticksCount= tickCount; |
|
373 | m_ticksCount= tickCount; | |
379 |
|
374 | |||
380 | if(isEmpty()) return; |
|
375 | if(isEmpty()) return; | |
381 | QVector<qreal> layout = calculateLayout(); |
|
376 | QVector<qreal> layout = calculateLayout(); | |
382 | updateLayout(layout); |
|
377 | updateLayout(layout); | |
383 |
|
378 | |||
384 | } |
|
379 | } | |
385 |
|
380 | |||
386 |
void Axis |
|
381 | void Axis::handleGeometryChanged(const QRectF& rect) | |
387 | { |
|
382 | { | |
388 | m_rect = rect; |
|
383 | m_rect = rect; | |
389 | if(isEmpty()) return; |
|
384 | if(isEmpty()) return; | |
390 | QVector<qreal> layout = calculateLayout(); |
|
385 | QVector<qreal> layout = calculateLayout(); | |
391 | updateLayout(layout); |
|
386 | updateLayout(layout); | |
392 | } |
|
387 | } | |
393 |
|
388 | |||
394 | //painter |
|
389 | void Axis::axisSelected() | |
395 |
|
||||
396 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
|||
397 | { |
|
390 | { | |
398 | Q_UNUSED(painter) |
|
391 | qDebug()<<"TODO axis clicked"; | |
399 | Q_UNUSED(option) |
|
|||
400 | Q_UNUSED(widget) |
|
|||
401 | } |
|
392 | } | |
402 |
|
393 | |||
403 | //TODO "nice numbers algorithm" |
|
394 | //TODO "nice numbers algorithm" | |
404 | #include "moc_axisitem_p.cpp" |
|
395 | #include "moc_axisitem_p.cpp" | |
405 |
|
396 | |||
406 | QTCOMMERCIALCHART_END_NAMESPACE |
|
397 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,96 +1,122 | |||||
1 | #ifndef AXISITEM_H_ |
|
1 | #ifndef AXISITEM_H_ | |
2 | #define AXISITEM_H_ |
|
2 | #define AXISITEM_H_ | |
3 |
|
3 | |||
4 | #include "domain_p.h" |
|
4 | #include "domain_p.h" | |
5 |
#include "chart |
|
5 | #include "chart_p.h" | |
6 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QChartAxis; |
|
10 | class QChartAxis; | |
11 | class ChartPresenter; |
|
11 | class ChartPresenter; | |
12 |
|
12 | |||
13 |
class Axis |
|
13 | class Axis : public Chart | |
14 | { |
|
14 | { | |
15 | Q_OBJECT |
|
15 | Q_OBJECT | |
16 | public: |
|
16 | public: | |
17 | enum AxisType{X_AXIS,Y_AXIS}; |
|
17 | enum AxisType{X_AXIS,Y_AXIS}; | |
18 |
|
18 | |||
19 |
Axis |
|
19 | Axis(QChartAxis* axis,ChartPresenter* presenter,AxisType type = X_AXIS); | |
20 |
~Axis |
|
20 | ~Axis(); | |
21 |
|
||||
22 | //from QGraphicsItem |
|
|||
23 | QRectF boundingRect() const; |
|
|||
24 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
|||
25 |
|
21 | |||
26 | AxisType axisType() const {return m_type;}; |
|
22 | AxisType axisType() const {return m_type;}; | |
27 |
|
23 | |||
28 | void setAxisOpacity(qreal opacity); |
|
24 | void setAxisOpacity(qreal opacity); | |
29 | qreal axisOpacity() const; |
|
25 | qreal axisOpacity() const; | |
30 |
|
26 | |||
31 | void setGridOpacity(qreal opacity); |
|
27 | void setGridOpacity(qreal opacity); | |
32 | qreal gridOpacity() const; |
|
28 | qreal gridOpacity() const; | |
33 |
|
29 | |||
34 | void setLabelsOpacity(qreal opacity); |
|
30 | void setLabelsOpacity(qreal opacity); | |
35 | qreal labelsOpacity() const; |
|
31 | qreal labelsOpacity() const; | |
36 |
|
32 | |||
37 | void setShadesOpacity(qreal opacity); |
|
33 | void setShadesOpacity(qreal opacity); | |
38 | qreal shadesOpacity() const; |
|
34 | qreal shadesOpacity() const; | |
39 |
|
35 | |||
40 | void setLabelsAngle(int angle); |
|
36 | void setLabelsAngle(int angle); | |
41 | int labelsAngle()const { return m_labelsAngle; } |
|
37 | int labelsAngle()const { return m_labelsAngle; } | |
42 |
|
38 | |||
43 | void setShadesBrush(const QBrush& brush); |
|
39 | void setShadesBrush(const QBrush& brush); | |
44 | void setShadesPen(const QPen& pen); |
|
40 | void setShadesPen(const QPen& pen); | |
45 |
|
41 | |||
46 | void setAxisPen(const QPen& pen); |
|
42 | void setAxisPen(const QPen& pen); | |
47 | void setGridPen(const QPen& pen); |
|
43 | void setGridPen(const QPen& pen); | |
48 |
|
44 | |||
49 | void setLabelsPen(const QPen& pen); |
|
45 | void setLabelsPen(const QPen& pen); | |
50 | void setLabelsBrush(const QBrush& brush); |
|
46 | void setLabelsBrush(const QBrush& brush); | |
51 | void setLabelsFont(const QFont& font); |
|
47 | void setLabelsFont(const QFont& font); | |
52 |
|
48 | |||
53 | inline QRectF geometry() const { return m_rect; } |
|
49 | inline QRectF geometry() const { return m_rect; } | |
54 | inline QVector<qreal> layout() { return m_layoutVector;}; |
|
50 | inline QVector<qreal> layout() { return m_layoutVector;}; | |
55 |
|
51 | |||
56 | public slots: |
|
52 | public slots: | |
57 | void handleAxisUpdated(); |
|
53 | void handleAxisUpdated(); | |
58 | void handleAxisCategoriesUpdated(); |
|
54 | void handleAxisCategoriesUpdated(); | |
59 | void handleRangeChanged(qreal min , qreal max,int tickCount); |
|
55 | void handleRangeChanged(qreal min , qreal max,int tickCount); | |
60 | void handleGeometryChanged(const QRectF& size); |
|
56 | void handleGeometryChanged(const QRectF& size); | |
61 |
|
57 | |||
62 |
|
58 | |||
63 | private: |
|
59 | private: | |
64 | inline bool isEmpty(); |
|
60 | inline bool isEmpty(); | |
65 | void createItems(int count); |
|
61 | void createItems(int count); | |
66 | void deleteItems(int count); |
|
62 | void deleteItems(int count); | |
67 |
|
63 | |||
68 | QVector<qreal> calculateLayout() const; |
|
64 | QVector<qreal> calculateLayout() const; | |
69 | void updateLayout(QVector<qreal>& layout); |
|
65 | void updateLayout(QVector<qreal>& layout); | |
70 | void setLayout(QVector<qreal>& layout); |
|
66 | void setLayout(QVector<qreal>& layout); | |
71 |
|
67 | |||
72 | QStringList createLabels(int ticks, qreal min, qreal max) const; |
|
68 | QStringList createLabels(int ticks, qreal min, qreal max) const; | |
|
69 | void axisSelected(); | |||
73 |
|
70 | |||
74 | private: |
|
71 | private: | |
75 | ChartPresenter* m_presenter; |
|
|||
76 | QChartAxis* m_chartAxis; |
|
72 | QChartAxis* m_chartAxis; | |
77 | AxisType m_type; |
|
73 | AxisType m_type; | |
78 | QRectF m_rect; |
|
74 | QRectF m_rect; | |
79 | int m_labelsAngle; |
|
75 | int m_labelsAngle; | |
80 | QGraphicsItemGroup m_grid; |
|
76 | QGraphicsItemGroup m_grid; | |
81 | QGraphicsItemGroup m_shades; |
|
77 | QGraphicsItemGroup m_shades; | |
82 | QGraphicsItemGroup m_labels; |
|
78 | QGraphicsItemGroup m_labels; | |
83 | QGraphicsItemGroup m_axis; |
|
79 | QGraphicsItemGroup m_axis; | |
84 | QVector<qreal> m_layoutVector; |
|
80 | QVector<qreal> m_layoutVector; | |
85 | qreal m_min; |
|
81 | qreal m_min; | |
86 | qreal m_max; |
|
82 | qreal m_max; | |
87 | int m_ticksCount; |
|
83 | int m_ticksCount; | |
88 | qreal m_zoomFactor; |
|
84 | qreal m_zoomFactor; | |
89 |
|
85 | |||
90 | friend class AxisAnimation; |
|
86 | friend class AxisAnimation; | |
|
87 | friend class AxisItem; | |||
|
88 | ||||
|
89 | }; | |||
|
90 | ||||
|
91 | class AxisItem: public QGraphicsLineItem | |||
|
92 | { | |||
|
93 | public: | |||
|
94 | ||||
|
95 | AxisItem(Axis* axis,QGraphicsItem* parent=0):QGraphicsLineItem(parent),m_axis(axis){}; | |||
|
96 | ||||
|
97 | protected: | |||
|
98 | void mousePressEvent(QGraphicsSceneMouseEvent *event) | |||
|
99 | { | |||
|
100 | Q_UNUSED(event) | |||
|
101 | m_axis->axisSelected(); | |||
|
102 | } | |||
|
103 | ||||
|
104 | QRectF boundingRect() const | |||
|
105 | { | |||
|
106 | return QGraphicsLineItem::boundingRect().adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?10:0,m_axis->axisType()!=Axis::Y_AXIS?10:0); | |||
|
107 | } | |||
|
108 | ||||
|
109 | QPainterPath shape() const | |||
|
110 | { | |||
|
111 | QPainterPath path; | |||
|
112 | path.addRect(boundingRect()); | |||
|
113 | return path; | |||
|
114 | } | |||
|
115 | private: | |||
|
116 | Axis* m_axis; | |||
91 |
|
117 | |||
92 | }; |
|
118 | }; | |
93 |
|
119 | |||
94 | QTCOMMERCIALCHART_END_NAMESPACE |
|
120 | QTCOMMERCIALCHART_END_NAMESPACE | |
95 |
|
121 | |||
96 | #endif /* AXISITEM_H_ */ |
|
122 | #endif /* AXISITEM_H_ */ |
@@ -1,281 +1,280 | |||||
1 | #include "barchartitem_p.h" |
|
1 | #include "barchartitem_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include "qbarseries.h" |
|
5 | #include "qbarseries.h" | |
6 | #include "qchart.h" |
|
6 | #include "qchart.h" | |
7 | #include "qchartaxis.h" |
|
7 | #include "qchartaxis.h" | |
8 | #include "qchartaxiscategories.h" |
|
8 | #include "qchartaxiscategories.h" | |
9 | #include "chartpresenter_p.h" |
|
9 | #include "chartpresenter_p.h" | |
10 | #include "chartanimator_p.h" |
|
10 | #include "chartanimator_p.h" | |
11 | #include <QDebug> |
|
11 | #include <QDebug> | |
12 | #include <QToolTip> |
|
12 | #include <QToolTip> | |
13 |
|
13 | |||
14 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
14 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
15 |
|
15 | |||
16 |
BarChartItem::BarChartItem(QBarSeries *series, |
|
16 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : | |
17 |
ChartItem(p |
|
17 | ChartItem(presenter), | |
18 | mHeight(0), |
|
18 | mHeight(0), | |
19 | mWidth(0), |
|
19 | mWidth(0), | |
20 | mLayoutSet(false), |
|
20 | mLayoutSet(false), | |
21 |
mSeries(series) |
|
21 | mSeries(series) | |
22 | mChart(parent) |
|
|||
23 | { |
|
22 | { | |
24 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); |
|
23 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | |
25 | connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); |
|
24 | connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); | |
26 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); |
|
25 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); | |
27 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); |
|
26 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); | |
28 | setZValue(ChartPresenter::BarSeriesZValue); |
|
27 | setZValue(ChartPresenter::BarSeriesZValue); | |
29 | initAxisLabels(); |
|
28 | initAxisLabels(); | |
30 | dataChanged(); |
|
29 | dataChanged(); | |
31 | } |
|
30 | } | |
32 |
|
31 | |||
33 | BarChartItem::~BarChartItem() |
|
32 | BarChartItem::~BarChartItem() | |
34 | { |
|
33 | { | |
35 | disconnect(this,SLOT(showToolTip(QPoint,QString))); |
|
34 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | |
36 | } |
|
35 | } | |
37 |
|
36 | |||
38 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
37 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
39 | { |
|
38 | { | |
40 | if (!mLayoutSet) { |
|
39 | if (!mLayoutSet) { | |
41 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; |
|
40 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; | |
42 | return; |
|
41 | return; | |
43 | } |
|
42 | } | |
44 | foreach(QGraphicsItem* i, childItems()) { |
|
43 | foreach(QGraphicsItem* i, childItems()) { | |
45 | i->paint(painter,option,widget); |
|
44 | i->paint(painter,option,widget); | |
46 | } |
|
45 | } | |
47 | } |
|
46 | } | |
48 |
|
47 | |||
49 | QRectF BarChartItem::boundingRect() const |
|
48 | QRectF BarChartItem::boundingRect() const | |
50 | { |
|
49 | { | |
51 | return QRectF(0, 0, mWidth, mHeight); |
|
50 | return QRectF(0, 0, mWidth, mHeight); | |
52 | } |
|
51 | } | |
53 |
|
52 | |||
54 | void BarChartItem::dataChanged() |
|
53 | void BarChartItem::dataChanged() | |
55 | { |
|
54 | { | |
56 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
55 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
57 | // Delete old bars |
|
56 | // Delete old bars | |
58 | foreach (QGraphicsItem* item, childItems()) { |
|
57 | foreach (QGraphicsItem* item, childItems()) { | |
59 | delete item; |
|
58 | delete item; | |
60 | } |
|
59 | } | |
61 |
|
60 | |||
62 | mBars.clear(); |
|
61 | mBars.clear(); | |
63 | mFloatingValues.clear(); |
|
62 | mFloatingValues.clear(); | |
64 |
|
63 | |||
65 | // Create new graphic items for bars |
|
64 | // Create new graphic items for bars | |
66 | for (int c=0; c<mSeries->categoryCount(); c++) { |
|
65 | for (int c=0; c<mSeries->categoryCount(); c++) { | |
67 | QString category = mSeries->categoryName(c); |
|
66 | QString category = mSeries->categoryName(c); | |
68 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
67 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
69 | QBarSet *set = mSeries->barsetAt(s); |
|
68 | QBarSet *set = mSeries->barsetAt(s); | |
70 | Bar *bar = new Bar(category,this); |
|
69 | Bar *bar = new Bar(category,this); | |
71 | childItems().append(bar); |
|
70 | childItems().append(bar); | |
72 | mBars.append(bar); |
|
71 | mBars.append(bar); | |
73 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); |
|
72 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | |
74 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); |
|
73 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | |
75 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); |
|
74 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | |
76 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); |
|
75 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | |
77 | } |
|
76 | } | |
78 | } |
|
77 | } | |
79 |
|
78 | |||
80 | // Create floating values |
|
79 | // Create floating values | |
81 | for (int category=0; category<mSeries->categoryCount(); category++) { |
|
80 | for (int category=0; category<mSeries->categoryCount(); category++) { | |
82 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
81 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
83 | QBarSet *set = mSeries->barsetAt(s); |
|
82 | QBarSet *set = mSeries->barsetAt(s); | |
84 | BarValue *value = new BarValue(*set, this); |
|
83 | BarValue *value = new BarValue(*set, this); | |
85 | childItems().append(value); |
|
84 | childItems().append(value); | |
86 | mFloatingValues.append(value); |
|
85 | mFloatingValues.append(value); | |
87 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
86 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | |
88 | } |
|
87 | } | |
89 | } |
|
88 | } | |
90 | } |
|
89 | } | |
91 |
|
90 | |||
92 | void BarChartItem::layoutChanged() |
|
91 | void BarChartItem::layoutChanged() | |
93 | { |
|
92 | { | |
94 | // Scale bars to new layout |
|
93 | // Scale bars to new layout | |
95 | // Layout for bars: |
|
94 | // Layout for bars: | |
96 | if (mSeries->barsetCount() <= 0) { |
|
95 | if (mSeries->barsetCount() <= 0) { | |
97 | qDebug() << "No sets in model!"; |
|
96 | qDebug() << "No sets in model!"; | |
98 | return; |
|
97 | return; | |
99 | } |
|
98 | } | |
100 |
|
99 | |||
101 | if (childItems().count() == 0) { |
|
100 | if (childItems().count() == 0) { | |
102 | qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; |
|
101 | qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; | |
103 | return; |
|
102 | return; | |
104 | } |
|
103 | } | |
105 |
|
104 | |||
106 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
105 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
107 | int categoryCount = mSeries->categoryCount(); |
|
106 | int categoryCount = mSeries->categoryCount(); | |
108 | int setCount = mSeries->barsetCount(); |
|
107 | int setCount = mSeries->barsetCount(); | |
109 |
|
108 | |||
110 | qreal tW = mWidth; |
|
109 | qreal tW = mWidth; | |
111 | qreal tH = mHeight; |
|
110 | qreal tH = mHeight; | |
112 | qreal tM = mSeries->max(); |
|
111 | qreal tM = mSeries->max(); | |
113 |
|
112 | |||
114 | // Domain: |
|
113 | // Domain: | |
115 | if (mDomainMaxY > tM) { |
|
114 | if (mDomainMaxY > tM) { | |
116 | tM = mDomainMaxY; |
|
115 | tM = mDomainMaxY; | |
117 | } |
|
116 | } | |
118 |
|
117 | |||
119 | qreal scale = (tH/tM); |
|
118 | qreal scale = (tH/tM); | |
120 | qreal tC = categoryCount + 1; |
|
119 | qreal tC = categoryCount + 1; | |
121 | qreal categoryWidth = tW/tC; |
|
120 | qreal categoryWidth = tW/tC; | |
122 | mBarWidth = categoryWidth / (setCount+1); |
|
121 | mBarWidth = categoryWidth / (setCount+1); | |
123 |
|
122 | |||
124 | int itemIndex(0); |
|
123 | int itemIndex(0); | |
125 | for (int category=0; category < categoryCount; category++) { |
|
124 | for (int category=0; category < categoryCount; category++) { | |
126 | qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; |
|
125 | qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; | |
127 | qreal yPos = mHeight; |
|
126 | qreal yPos = mHeight; | |
128 | for (int set = 0; set < setCount; set++) { |
|
127 | for (int set = 0; set < setCount; set++) { | |
129 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
128 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
130 | Bar* bar = mBars.at(itemIndex); |
|
129 | Bar* bar = mBars.at(itemIndex); | |
131 |
|
130 | |||
132 | // TODO: width settable per bar? |
|
131 | // TODO: width settable per bar? | |
133 | bar->resize(mBarWidth, barHeight); |
|
132 | bar->resize(mBarWidth, barHeight); | |
134 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
133 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
135 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
134 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
136 | bar->setPos(xPos, yPos-barHeight); |
|
135 | bar->setPos(xPos, yPos-barHeight); | |
137 | itemIndex++; |
|
136 | itemIndex++; | |
138 | xPos += mBarWidth; |
|
137 | xPos += mBarWidth; | |
139 | } |
|
138 | } | |
140 | } |
|
139 | } | |
141 |
|
140 | |||
142 | // Position floating values |
|
141 | // Position floating values | |
143 | itemIndex = 0; |
|
142 | itemIndex = 0; | |
144 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
143 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
145 | qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; |
|
144 | qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; | |
146 | qreal yPos = mHeight; |
|
145 | qreal yPos = mHeight; | |
147 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
146 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
148 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
147 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
149 | BarValue* value = mFloatingValues.at(itemIndex); |
|
148 | BarValue* value = mFloatingValues.at(itemIndex); | |
150 |
|
149 | |||
151 | QBarSet* barSet = mSeries->barsetAt(set); |
|
150 | QBarSet* barSet = mSeries->barsetAt(set); | |
152 | value->resize(100,50); // TODO: proper layout for this. |
|
151 | value->resize(100,50); // TODO: proper layout for this. | |
153 | value->setPos(xPos, yPos-barHeight/2); |
|
152 | value->setPos(xPos, yPos-barHeight/2); | |
154 | value->setPen(barSet->floatingValuePen()); |
|
153 | value->setPen(barSet->floatingValuePen()); | |
155 |
|
154 | |||
156 | if (mSeries->valueAt(set,category) != 0) { |
|
155 | if (mSeries->valueAt(set,category) != 0) { | |
157 | value->setValueString(QString::number(mSeries->valueAt(set,category))); |
|
156 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
158 | } else { |
|
157 | } else { | |
159 | value->setValueString(QString("")); |
|
158 | value->setValueString(QString("")); | |
160 | } |
|
159 | } | |
161 |
|
160 | |||
162 | itemIndex++; |
|
161 | itemIndex++; | |
163 | xPos += mBarWidth; |
|
162 | xPos += mBarWidth; | |
164 | } |
|
163 | } | |
165 | } |
|
164 | } | |
166 | update(); |
|
165 | update(); | |
167 | } |
|
166 | } | |
168 |
|
167 | |||
169 | BarLayout BarChartItem::calculateLayout() |
|
168 | BarLayout BarChartItem::calculateLayout() | |
170 | { |
|
169 | { | |
171 | BarLayout layout; |
|
170 | BarLayout layout; | |
172 | foreach(Bar* bar, mBars) { |
|
171 | foreach(Bar* bar, mBars) { | |
173 | layout.insert(bar,bar->boundingRect().size()); |
|
172 | layout.insert(bar,bar->boundingRect().size()); | |
174 | } |
|
173 | } | |
175 |
|
174 | |||
176 | return layout; |
|
175 | return layout; | |
177 | } |
|
176 | } | |
178 |
|
177 | |||
179 | void BarChartItem::applyLayout(const BarLayout &layout) |
|
178 | void BarChartItem::applyLayout(const BarLayout &layout) | |
180 | { |
|
179 | { | |
181 | if (m_animator) |
|
180 | if (m_animator) | |
182 | m_animator->updateLayout(this, layout); |
|
181 | m_animator->updateLayout(this, layout); | |
183 | else |
|
182 | else | |
184 | setLayout(layout); |
|
183 | setLayout(layout); | |
185 | } |
|
184 | } | |
186 |
|
185 | |||
187 | void BarChartItem::setLayout(const BarLayout &layout) |
|
186 | void BarChartItem::setLayout(const BarLayout &layout) | |
188 | { |
|
187 | { | |
189 | foreach (Bar *bar, layout.keys()) { |
|
188 | foreach (Bar *bar, layout.keys()) { | |
190 | bar->setSize(layout.value(bar)); |
|
189 | bar->setSize(layout.value(bar)); | |
191 | } |
|
190 | } | |
192 | update(); |
|
191 | update(); | |
193 | } |
|
192 | } | |
194 |
|
193 | |||
195 | void BarChartItem::initAxisLabels() |
|
194 | void BarChartItem::initAxisLabels() | |
196 | { |
|
195 | { | |
197 | int count = mSeries->categoryCount(); |
|
196 | int count = mSeries->categoryCount(); | |
198 | if (0 == count) { |
|
197 | if (0 == count) { | |
199 | return; |
|
198 | return; | |
200 | } |
|
199 | } | |
201 |
|
200 | |||
202 | mChart->axisX()->setTicksCount(count+2); |
|
201 | mChart->axisX()->setTicksCount(count+2); | |
203 |
|
202 | |||
204 | qreal min = 0; |
|
203 | qreal min = 0; | |
205 | qreal max = count+1; |
|
204 | qreal max = count+1; | |
206 |
|
205 | |||
207 | mChart->axisX()->setMin(min); |
|
206 | mChart->axisX()->setMin(min); | |
208 | mChart->axisX()->setMax(max); |
|
207 | mChart->axisX()->setMax(max); | |
209 |
|
208 | |||
210 | QChartAxisCategories* categories = mChart->axisX()->categories(); |
|
209 | QChartAxisCategories* categories = mChart->axisX()->categories(); | |
211 | categories->clear(); |
|
210 | categories->clear(); | |
212 | for (int i=0; i<count; i++) { |
|
211 | for (int i=0; i<count; i++) { | |
213 | categories->insert(i+1,mSeries->categoryName(i)); |
|
212 | categories->insert(i+1,mSeries->categoryName(i)); | |
214 | } |
|
213 | } | |
215 |
|
214 | |||
216 |
|
215 | |||
217 |
|
216 | |||
218 | mChart->axisX()->setLabelsVisible(true); |
|
217 | mChart->axisX()->setLabelsVisible(true); | |
219 | } |
|
218 | } | |
220 |
|
219 | |||
221 | //handlers |
|
220 | //handlers | |
222 |
|
221 | |||
223 | void BarChartItem::handleModelChanged(int index) |
|
222 | void BarChartItem::handleModelChanged(int index) | |
224 | { |
|
223 | { | |
225 | Q_UNUSED(index) |
|
224 | Q_UNUSED(index) | |
226 | dataChanged(); |
|
225 | dataChanged(); | |
227 | } |
|
226 | } | |
228 |
|
227 | |||
229 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
228 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | |
230 | { |
|
229 | { | |
231 | mDomainMinX = minX; |
|
230 | mDomainMinX = minX; | |
232 | mDomainMaxX = maxX; |
|
231 | mDomainMaxX = maxX; | |
233 | mDomainMinY = minY; |
|
232 | mDomainMinY = minY; | |
234 | mDomainMaxY = maxY; |
|
233 | mDomainMaxY = maxY; | |
235 | layoutChanged(); |
|
234 | layoutChanged(); | |
236 |
|
235 | |||
237 | /* |
|
236 | /* | |
238 | int count = mSeries->categoryCount(); |
|
237 | int count = mSeries->categoryCount(); | |
239 | if (0 == count) { |
|
238 | if (0 == count) { | |
240 | return; |
|
239 | return; | |
241 | } |
|
240 | } | |
242 |
|
241 | |||
243 | // Position labels to domain |
|
242 | // Position labels to domain | |
244 | qreal min = domain.minX(); |
|
243 | qreal min = domain.minX(); | |
245 | qreal max = domain.maxX(); |
|
244 | qreal max = domain.maxX(); | |
246 | qreal step = (max-min)/count; |
|
245 | qreal step = (max-min)/count; | |
247 | QChartAxisCategories& categories = mChart->axisX()->categories(); |
|
246 | QChartAxisCategories& categories = mChart->axisX()->categories(); | |
248 | categories.clear(); |
|
247 | categories.clear(); | |
249 | for (int i=0; i<count; i++) { |
|
248 | for (int i=0; i<count; i++) { | |
250 | categories.insert(min,mSeries->categoryName(i)); |
|
249 | categories.insert(min,mSeries->categoryName(i)); | |
251 | min += step; |
|
250 | min += step; | |
252 | } |
|
251 | } | |
253 | */ |
|
252 | */ | |
254 | } |
|
253 | } | |
255 |
|
254 | |||
256 | void BarChartItem::handleGeometryChanged(const QRectF& rect) |
|
255 | void BarChartItem::handleGeometryChanged(const QRectF& rect) | |
257 | { |
|
256 | { | |
258 | mWidth = rect.width(); |
|
257 | mWidth = rect.width(); | |
259 | mHeight = rect.height(); |
|
258 | mHeight = rect.height(); | |
260 | layoutChanged(); |
|
259 | layoutChanged(); | |
261 | mLayoutSet = true; |
|
260 | mLayoutSet = true; | |
262 | setPos(rect.topLeft()); |
|
261 | setPos(rect.topLeft()); | |
263 | } |
|
262 | } | |
264 |
|
263 | |||
265 | void BarChartItem::handleLayoutChanged() |
|
264 | void BarChartItem::handleLayoutChanged() | |
266 | { |
|
265 | { | |
267 | BarLayout layout = calculateLayout(); |
|
266 | BarLayout layout = calculateLayout(); | |
268 | applyLayout(layout); |
|
267 | applyLayout(layout); | |
269 | update(); |
|
268 | update(); | |
270 | } |
|
269 | } | |
271 |
|
270 | |||
272 |
|
271 | |||
273 | void BarChartItem::showToolTip(QPoint pos, QString tip) |
|
272 | void BarChartItem::showToolTip(QPoint pos, QString tip) | |
274 | { |
|
273 | { | |
275 | // TODO: cool tooltip instead of default |
|
274 | // TODO: cool tooltip instead of default | |
276 | QToolTip::showText(pos,tip); |
|
275 | QToolTip::showText(pos,tip); | |
277 | } |
|
276 | } | |
278 |
|
277 | |||
279 | #include "moc_barchartitem_p.cpp" |
|
278 | #include "moc_barchartitem_p.cpp" | |
280 |
|
279 | |||
281 | QTCOMMERCIALCHART_END_NAMESPACE |
|
280 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,76 +1,78 | |||||
1 | #ifndef BARCHARTITEM_H |
|
1 | #ifndef BARCHARTITEM_H | |
2 | #define BARCHARTITEM_H |
|
2 | #define BARCHARTITEM_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include "qbarseries.h" |
|
5 | #include "qbarseries.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 | #include <QBrush> |
|
7 | #include <QBrush> | |
8 | #include <QGraphicsItem> |
|
8 | #include <QGraphicsItem> | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | class Bar; |
|
12 | class Bar; | |
13 | class BarValue; |
|
13 | class BarValue; | |
14 | class QChartAxisCategories; |
|
14 | class QChartAxisCategories; | |
15 | class QChart; |
|
15 | class QChart; | |
16 |
|
16 | |||
17 | typedef QHash<Bar*, QSizeF> BarLayout; |
|
17 | typedef QHash<Bar*, QSizeF> BarLayout; | |
18 |
|
18 | |||
19 | class BarChartItem : public QObject, public ChartItem |
|
19 | class BarChartItem : public QObject, public ChartItem | |
20 | { |
|
20 | { | |
21 | Q_OBJECT |
|
21 | Q_OBJECT | |
22 | public: |
|
22 | public: | |
23 |
BarChartItem(QBarSeries *series, |
|
23 | BarChartItem(QBarSeries *series, ChartPresenter *presenter); | |
24 | virtual ~BarChartItem(); |
|
24 | virtual ~BarChartItem(); | |
25 |
|
25 | |||
|
26 | // Common implemantation of different presenters. Not to be instantiated. | |||
|
27 | // TODO: combine this with BarPresenter and derive other presenters from it? | |||
|
28 | ||||
26 | public: |
|
29 | public: | |
27 | // From QGraphicsItem |
|
30 | // From QGraphicsItem | |
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
31 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
29 | QRectF boundingRect() const; |
|
32 | QRectF boundingRect() const; | |
30 |
|
33 | |||
31 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it |
|
34 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it | |
32 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
35 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
33 | private slots: |
|
36 | private slots: | |
34 | virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
37 | virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
35 |
|
38 | |||
36 | public: |
|
39 | public: | |
37 | BarLayout calculateLayout(); |
|
40 | BarLayout calculateLayout(); | |
38 | void applyLayout(const BarLayout &layout); |
|
41 | void applyLayout(const BarLayout &layout); | |
39 | void setLayout(const BarLayout &layout); |
|
42 | void setLayout(const BarLayout &layout); | |
40 |
|
43 | |||
41 | protected: |
|
44 | protected: | |
42 | void initAxisLabels(); |
|
45 | void initAxisLabels(); | |
43 |
|
46 | |||
44 | public slots: |
|
47 | public slots: | |
45 | void handleModelChanged(int index); |
|
48 | void handleModelChanged(int index); | |
46 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
49 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); | |
47 | void handleGeometryChanged(const QRectF& size); |
|
50 | void handleGeometryChanged(const QRectF& size); | |
48 | void handleLayoutChanged(); |
|
51 | void handleLayoutChanged(); | |
49 |
|
52 | |||
50 | // Internal slots |
|
53 | // Internal slots | |
51 | void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled) |
|
54 | void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled) | |
52 |
|
55 | |||
53 | protected: |
|
56 | protected: | |
54 |
|
57 | |||
55 | // TODO: consider these. |
|
58 | // TODO: consider these. | |
56 | int mHeight; // Layout spesific |
|
59 | int mHeight; // Layout spesific | |
57 | int mWidth; |
|
60 | int mWidth; | |
58 | qreal mBarWidth; |
|
61 | qreal mBarWidth; | |
59 |
|
62 | |||
60 | qreal mDomainMinX; |
|
63 | qreal mDomainMinX; | |
61 | qreal mDomainMaxX; |
|
64 | qreal mDomainMaxX; | |
62 | qreal mDomainMinY; |
|
65 | qreal mDomainMinY; | |
63 | qreal mDomainMaxY; |
|
66 | qreal mDomainMaxY; | |
64 |
|
67 | |||
65 | bool mLayoutSet; // True, if component has been laid out. |
|
68 | bool mLayoutSet; // True, if component has been laid out. | |
66 |
|
69 | |||
67 | // Not owned. |
|
70 | // Not owned. | |
68 | QBarSeries* mSeries; |
|
71 | QBarSeries* mSeries; | |
69 | QList<Bar*> mBars; |
|
72 | QList<Bar*> mBars; | |
70 | QList<BarValue*> mFloatingValues; |
|
73 | QList<BarValue*> mFloatingValues; | |
71 | QChart* mChart; |
|
|||
72 | }; |
|
74 | }; | |
73 |
|
75 | |||
74 | QTCOMMERCIALCHART_END_NAMESPACE |
|
76 | QTCOMMERCIALCHART_END_NAMESPACE | |
75 |
|
77 | |||
76 | #endif // BARCHARTITEM_H |
|
78 | #endif // BARCHARTITEM_H |
@@ -1,94 +1,93 | |||||
1 | #include "percentbarchartitem_p.h" |
|
1 | #include "percentbarchartitem_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
|
9 | PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter* presenter) : | ||
10 | PercentBarChartItem::PercentBarChartItem(QBarSeries *series, QChart *parent) : |
|
10 | BarChartItem(series, presenter) | |
11 | BarChartItem(series, parent) |
|
|||
12 | { |
|
11 | { | |
13 | } |
|
12 | } | |
14 |
|
13 | |||
15 | void PercentBarChartItem::layoutChanged() |
|
14 | void PercentBarChartItem::layoutChanged() | |
16 | { |
|
15 | { | |
17 | // Scale bars to new layout |
|
16 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
17 | // Layout for bars: | |
19 | if (mSeries->barsetCount() <= 0) { |
|
18 | if (mSeries->barsetCount() <= 0) { | |
20 | qDebug() << "No sets in model!"; |
|
19 | qDebug() << "No sets in model!"; | |
21 | // Nothing to do. |
|
20 | // Nothing to do. | |
22 | return; |
|
21 | return; | |
23 | } |
|
22 | } | |
24 |
|
23 | |||
25 | if (childItems().count() == 0) { |
|
24 | if (childItems().count() == 0) { | |
26 | qDebug() << "WARNING: PercentBarChartItem::layoutChanged called before graphics items are created!"; |
|
25 | qDebug() << "WARNING: PercentBarChartItem::layoutChanged called before graphics items are created!"; | |
27 | return; |
|
26 | return; | |
28 | } |
|
27 | } | |
29 |
|
28 | |||
30 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
29 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
31 | qreal tW = mWidth; |
|
30 | qreal tW = mWidth; | |
32 | qreal tC = mSeries->categoryCount() + 1; |
|
31 | qreal tC = mSeries->categoryCount() + 1; | |
33 | qreal cC = mSeries->categoryCount() * 2 + 1; |
|
32 | qreal cC = mSeries->categoryCount() * 2 + 1; | |
34 | mBarWidth = tW / cC; |
|
33 | mBarWidth = tW / cC; | |
35 | qreal xStep = (tW/tC); |
|
34 | qreal xStep = (tW/tC); | |
36 | qreal xPos = ((tW/tC) - mBarWidth / 2); |
|
35 | qreal xPos = ((tW/tC) - mBarWidth / 2); | |
37 | qreal h = mHeight; |
|
36 | qreal h = mHeight; | |
38 |
|
37 | |||
39 | int itemIndex(0); |
|
38 | int itemIndex(0); | |
40 | for (int category = 0; category < mSeries->categoryCount(); category++) { |
|
39 | for (int category = 0; category < mSeries->categoryCount(); category++) { | |
41 | qreal colSum = mSeries->categorySum(category); |
|
40 | qreal colSum = mSeries->categorySum(category); | |
42 | qreal scale = (h / colSum); |
|
41 | qreal scale = (h / colSum); | |
43 | qreal yPos = h; |
|
42 | qreal yPos = h; | |
44 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
43 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
45 | qreal barHeight = mSeries->valueAt(set, category) * scale; |
|
44 | qreal barHeight = mSeries->valueAt(set, category) * scale; | |
46 | Bar* bar = mBars.at(itemIndex); |
|
45 | Bar* bar = mBars.at(itemIndex); | |
47 |
|
46 | |||
48 | // TODO: width settable per bar? |
|
47 | // TODO: width settable per bar? | |
49 | bar->resize(mBarWidth, barHeight); |
|
48 | bar->resize(mBarWidth, barHeight); | |
50 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
49 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
51 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
50 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
52 | bar->setPos(xPos, yPos-barHeight); |
|
51 | bar->setPos(xPos, yPos-barHeight); | |
53 | itemIndex++; |
|
52 | itemIndex++; | |
54 | yPos -= barHeight; |
|
53 | yPos -= barHeight; | |
55 | } |
|
54 | } | |
56 | xPos += xStep; |
|
55 | xPos += xStep; | |
57 | } |
|
56 | } | |
58 |
|
57 | |||
59 | // Position floating values |
|
58 | // Position floating values | |
60 | itemIndex = 0; |
|
59 | itemIndex = 0; | |
61 | xPos = (tW/tC); |
|
60 | xPos = (tW/tC); | |
62 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
61 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
63 | qreal yPos = h; |
|
62 | qreal yPos = h; | |
64 | qreal colSum = mSeries->categorySum(category); |
|
63 | qreal colSum = mSeries->categorySum(category); | |
65 | qreal scale = (h / colSum); |
|
64 | qreal scale = (h / colSum); | |
66 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
65 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
67 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
66 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
68 | BarValue* value = mFloatingValues.at(itemIndex); |
|
67 | BarValue* value = mFloatingValues.at(itemIndex); | |
69 |
|
68 | |||
70 | QBarSet* barSet = mSeries->barsetAt(set); |
|
69 | QBarSet* barSet = mSeries->barsetAt(set); | |
71 | value->resize(100,50); // TODO: proper layout for this. |
|
70 | value->resize(100,50); // TODO: proper layout for this. | |
72 | value->setPos(xPos, yPos-barHeight/2); |
|
71 | value->setPos(xPos, yPos-barHeight/2); | |
73 | value->setPen(barSet->floatingValuePen()); |
|
72 | value->setPen(barSet->floatingValuePen()); | |
74 |
|
73 | |||
75 | if (mSeries->valueAt(set,category) != 0) { |
|
74 | if (mSeries->valueAt(set,category) != 0) { | |
76 | int p = mSeries->percentageAt(set,category) * 100; |
|
75 | int p = mSeries->percentageAt(set,category) * 100; | |
77 | QString vString(QString::number(p)); |
|
76 | QString vString(QString::number(p)); | |
78 | vString.truncate(3); |
|
77 | vString.truncate(3); | |
79 | vString.append("%"); |
|
78 | vString.append("%"); | |
80 | value->setValueString(vString); |
|
79 | value->setValueString(vString); | |
81 | } else { |
|
80 | } else { | |
82 | value->setValueString(QString("")); |
|
81 | value->setValueString(QString("")); | |
83 | } |
|
82 | } | |
84 |
|
83 | |||
85 | itemIndex++; |
|
84 | itemIndex++; | |
86 | yPos -= barHeight; |
|
85 | yPos -= barHeight; | |
87 | } |
|
86 | } | |
88 | xPos += xStep; |
|
87 | xPos += xStep; | |
89 | } |
|
88 | } | |
90 | } |
|
89 | } | |
91 |
|
90 | |||
92 | #include "moc_percentbarchartitem_p.cpp" |
|
91 | #include "moc_percentbarchartitem_p.cpp" | |
93 |
|
92 | |||
94 | QTCOMMERCIALCHART_END_NAMESPACE |
|
93 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,48 +1,48 | |||||
1 | #include "separator_p.h" |
|
1 | #include "separator_p.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | Separator::Separator(QGraphicsItem *parent) |
|
7 | Separator::Separator(QGraphicsItem *parent) | |
8 |
: |
|
8 | : QGraphicsItem(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 | void Separator::setPos(qreal x, qreal y) |
|
12 | void Separator::setPos(qreal x, qreal y) | |
13 | { |
|
13 | { | |
14 | mXpos = x; |
|
14 | mXpos = x; | |
15 | mYpos = y; |
|
15 | mYpos = y; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | void Separator::setColor(QColor color) |
|
18 | void Separator::setColor(QColor color) | |
19 | { |
|
19 | { | |
20 | mColor = color; |
|
20 | mColor = color; | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 | void Separator::setSize(const QSizeF &size) |
|
23 | void Separator::setSize(const QSizeF &size) | |
24 | { |
|
24 | { | |
25 | mWidth = size.width(); |
|
25 | mWidth = size.width(); | |
26 | mHeight = size.height(); |
|
26 | mHeight = size.height(); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
29 | void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
30 | { |
|
30 | { | |
31 | Q_UNUSED(option) |
|
31 | Q_UNUSED(option) | |
32 | Q_UNUSED(widget) |
|
32 | Q_UNUSED(widget) | |
33 |
|
33 | |||
34 | if (isVisible()) { |
|
34 | if (isVisible()) { | |
35 | QPen pen(mColor); |
|
35 | QPen pen(mColor); | |
36 | painter->setPen(pen); |
|
36 | painter->setPen(pen); | |
37 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); |
|
37 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); | |
38 | } |
|
38 | } | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | QRectF Separator::boundingRect() const |
|
41 | QRectF Separator::boundingRect() const | |
42 | { |
|
42 | { | |
43 | QRectF r(mXpos,mYpos,mWidth,mHeight); |
|
43 | QRectF r(mXpos,mYpos,mWidth,mHeight); | |
44 | return r; |
|
44 | return r; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | QTCOMMERCIALCHART_END_NAMESPACE |
|
48 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,35 +1,35 | |||||
1 | #ifndef SEPARATOR_H |
|
1 | #ifndef SEPARATOR_H | |
2 | #define SEPARATOR_H |
|
2 | #define SEPARATOR_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 |
class Separator : public |
|
8 | class Separator : public QGraphicsItem | |
9 | { |
|
9 | { | |
10 | public: |
|
10 | public: | |
11 | Separator(QGraphicsItem *parent = 0); |
|
11 | Separator(QGraphicsItem *parent = 0); | |
12 |
|
12 | |||
13 | void setPos(qreal x, qreal y); |
|
13 | void setPos(qreal x, qreal y); | |
14 | void setColor(QColor color); |
|
14 | void setColor(QColor color); | |
15 |
|
15 | |||
16 | // From ChartItem |
|
16 | // From ChartItem | |
17 | void setSize(const QSizeF &size); |
|
17 | void setSize(const QSizeF &size); | |
18 |
|
18 | |||
19 | // From QGraphicsItem |
|
19 | // From QGraphicsItem | |
20 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
20 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |
21 | QRectF boundingRect() const; |
|
21 | QRectF boundingRect() const; | |
22 |
|
22 | |||
23 | private: |
|
23 | private: | |
24 |
|
24 | |||
25 | QColor mColor; |
|
25 | QColor mColor; | |
26 | qreal mXpos; |
|
26 | qreal mXpos; | |
27 | qreal mYpos; |
|
27 | qreal mYpos; | |
28 | qreal mHeight; |
|
28 | qreal mHeight; | |
29 | qreal mWidth; |
|
29 | qreal mWidth; | |
30 |
|
30 | |||
31 | }; |
|
31 | }; | |
32 |
|
32 | |||
33 | QTCOMMERCIALCHART_END_NAMESPACE |
|
33 | QTCOMMERCIALCHART_END_NAMESPACE | |
34 |
|
34 | |||
35 | #endif // SEPARATOR_H |
|
35 | #endif // SEPARATOR_H |
@@ -1,102 +1,102 | |||||
1 | #include "stackedbarchartitem_p.h" |
|
1 | #include "stackedbarchartitem_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
StackedBarChartItem::StackedBarChartItem(QBarSeries *series, |
|
9 | StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) : | |
10 |
BarChartItem(series,p |
|
10 | BarChartItem(series,presenter) | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | StackedBarChartItem::~StackedBarChartItem() |
|
14 | StackedBarChartItem::~StackedBarChartItem() | |
15 | { |
|
15 | { | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | void StackedBarChartItem::layoutChanged() |
|
19 | void StackedBarChartItem::layoutChanged() | |
20 | { |
|
20 | { | |
21 | // Scale bars to new layout |
|
21 | // Scale bars to new layout | |
22 | // Layout for bars: |
|
22 | // Layout for bars: | |
23 | if (mSeries->barsetCount() <= 0) { |
|
23 | if (mSeries->barsetCount() <= 0) { | |
24 | qDebug() << "No sets in model!"; |
|
24 | qDebug() << "No sets in model!"; | |
25 | // Nothing to do. |
|
25 | // Nothing to do. | |
26 | return; |
|
26 | return; | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | if (mSeries->categoryCount() == 0) { |
|
29 | if (mSeries->categoryCount() == 0) { | |
30 | qDebug() << "No categories in model!"; |
|
30 | qDebug() << "No categories in model!"; | |
31 | // Nothing to do |
|
31 | // Nothing to do | |
32 | return; |
|
32 | return; | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | if (childItems().count() == 0) { |
|
35 | if (childItems().count() == 0) { | |
36 | qDebug() << "WARNING: StackedBarChartItem::layoutChanged called before graphics items are created!"; |
|
36 | qDebug() << "WARNING: StackedBarChartItem::layoutChanged called before graphics items are created!"; | |
37 | return; |
|
37 | return; | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
40 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
41 | qreal maxSum = mSeries->maxCategorySum(); |
|
41 | qreal maxSum = mSeries->maxCategorySum(); | |
42 | // Domain: |
|
42 | // Domain: | |
43 | if (mDomainMaxY > maxSum) { |
|
43 | if (mDomainMaxY > maxSum) { | |
44 | maxSum = mDomainMaxY; |
|
44 | maxSum = mDomainMaxY; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | qreal h = mHeight; |
|
47 | qreal h = mHeight; | |
48 | qreal scale = (h / maxSum); |
|
48 | qreal scale = (h / maxSum); | |
49 | qreal tW = mWidth; |
|
49 | qreal tW = mWidth; | |
50 | qreal tC = mSeries->categoryCount() + 1; |
|
50 | qreal tC = mSeries->categoryCount() + 1; | |
51 | qreal cC = mSeries->categoryCount() * 2 + 1; |
|
51 | qreal cC = mSeries->categoryCount() * 2 + 1; | |
52 | mBarWidth = tW / cC; |
|
52 | mBarWidth = tW / cC; | |
53 | qreal xStep = (tW/tC); |
|
53 | qreal xStep = (tW/tC); | |
54 | qreal xPos = ((tW/tC) - mBarWidth / 2); |
|
54 | qreal xPos = ((tW/tC) - mBarWidth / 2); | |
55 |
|
55 | |||
56 | int itemIndex(0); |
|
56 | int itemIndex(0); | |
57 | for (int category = 0; category < mSeries->categoryCount(); category++) { |
|
57 | for (int category = 0; category < mSeries->categoryCount(); category++) { | |
58 | qreal yPos = h; |
|
58 | qreal yPos = h; | |
59 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
59 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
60 | qreal barHeight = mSeries->valueAt(set, category) * scale; |
|
60 | qreal barHeight = mSeries->valueAt(set, category) * scale; | |
61 | Bar* bar = mBars.at(itemIndex); |
|
61 | Bar* bar = mBars.at(itemIndex); | |
62 |
|
62 | |||
63 | bar->resize(mBarWidth, barHeight); |
|
63 | bar->resize(mBarWidth, barHeight); | |
64 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
64 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
65 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
65 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
66 | bar->setPos(xPos, yPos-barHeight); |
|
66 | bar->setPos(xPos, yPos-barHeight); | |
67 | itemIndex++; |
|
67 | itemIndex++; | |
68 | yPos -= barHeight; |
|
68 | yPos -= barHeight; | |
69 | } |
|
69 | } | |
70 | xPos += xStep; |
|
70 | xPos += xStep; | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | // Position floating values |
|
73 | // Position floating values | |
74 | itemIndex = 0; |
|
74 | itemIndex = 0; | |
75 | xPos = (tW/tC); |
|
75 | xPos = (tW/tC); | |
76 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
76 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
77 | qreal yPos = h; |
|
77 | qreal yPos = h; | |
78 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
78 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
79 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
79 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
80 | BarValue* value = mFloatingValues.at(itemIndex); |
|
80 | BarValue* value = mFloatingValues.at(itemIndex); | |
81 |
|
81 | |||
82 | QBarSet* barSet = mSeries->barsetAt(set); |
|
82 | QBarSet* barSet = mSeries->barsetAt(set); | |
83 | value->resize(100,50); // TODO: proper layout for this. |
|
83 | value->resize(100,50); // TODO: proper layout for this. | |
84 | value->setPos(xPos, yPos-barHeight/2); |
|
84 | value->setPos(xPos, yPos-barHeight/2); | |
85 | value->setPen(barSet->floatingValuePen()); |
|
85 | value->setPen(barSet->floatingValuePen()); | |
86 |
|
86 | |||
87 | if (mSeries->valueAt(set,category) != 0) { |
|
87 | if (mSeries->valueAt(set,category) != 0) { | |
88 | value->setValueString(QString::number(mSeries->valueAt(set,category))); |
|
88 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
89 | } else { |
|
89 | } else { | |
90 | value->setValueString(QString("")); |
|
90 | value->setValueString(QString("")); | |
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 | itemIndex++; |
|
93 | itemIndex++; | |
94 | yPos -= barHeight; |
|
94 | yPos -= barHeight; | |
95 | } |
|
95 | } | |
96 | xPos += xStep; |
|
96 | xPos += xStep; | |
97 | } |
|
97 | } | |
98 | } |
|
98 | } | |
99 |
|
99 | |||
100 | #include "moc_stackedbarchartitem_p.cpp" |
|
100 | #include "moc_stackedbarchartitem_p.cpp" | |
101 |
|
101 | |||
102 | QTCOMMERCIALCHART_END_NAMESPACE |
|
102 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,33 +1,19 | |||||
1 | #ifndef CHARTITEM_H_ |
|
1 | #ifndef CHARTITEM_H_ | |
2 | #define CHARTITEM_H_ |
|
2 | #define CHARTITEM_H_ | |
3 |
|
3 | |||
4 |
#include " |
|
4 | #include "chart_p.h" | |
|
5 | #include "chartpresenter_p.h" | |||
5 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
9 | |||
9 | class ChartAnimator; |
|
10 | class ChartItem : public QGraphicsItem, public Chart | |
10 |
|
||||
11 | class ChartItem : public QGraphicsItem |
|
|||
12 | { |
|
11 | { | |
13 | enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM}; |
|
12 | enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM}; | |
14 | public: |
|
13 | public: | |
15 | ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent), |
|
14 | ChartItem(ChartPresenter *presenter):QGraphicsItem(presenter->rootItem()),Chart(presenter){}; | |
16 | m_animator(0){}; |
|
|||
17 | //TODO make pure |
|
|||
18 | virtual void handleGeometryChanged(const QRectF&){}; |
|
|||
19 | virtual void handleDomainChanged(qreal,qreal,qreal,qreal){}; |
|
|||
20 |
|
||||
21 | void setAnimator(ChartAnimator* animator){ |
|
|||
22 | m_animator=animator; |
|
|||
23 | } |
|
|||
24 |
|
||||
25 | virtual ~ChartItem(){}; |
|
|||
26 |
|
||||
27 | protected: |
|
|||
28 | ChartAnimator* m_animator; |
|
|||
29 | }; |
|
15 | }; | |
30 |
|
16 | |||
31 | QTCOMMERCIALCHART_END_NAMESPACE |
|
17 | QTCOMMERCIALCHART_END_NAMESPACE | |
32 |
|
18 | |||
33 | #endif /* CHARTITEM_H_ */ |
|
19 | #endif /* CHARTITEM_H_ */ |
@@ -1,391 +1,391 | |||||
1 | #include "qchart.h" |
|
1 | #include "qchart.h" | |
2 | #include "qchartaxis.h" |
|
2 | #include "qchartaxis.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include "chartdataset_p.h" |
|
4 | #include "chartdataset_p.h" | |
5 | #include "charttheme_p.h" |
|
5 | #include "charttheme_p.h" | |
6 | #include "chartanimator_p.h" |
|
6 | #include "chartanimator_p.h" | |
7 | //series |
|
7 | //series | |
8 | #include "qbarseries.h" |
|
8 | #include "qbarseries.h" | |
9 | #include "qstackedbarseries.h" |
|
9 | #include "qstackedbarseries.h" | |
10 | #include "qpercentbarseries.h" |
|
10 | #include "qpercentbarseries.h" | |
11 | #include "qlineseries.h" |
|
11 | #include "qlineseries.h" | |
12 | #include "qareaseries.h" |
|
12 | #include "qareaseries.h" | |
13 | #include "qpieseries.h" |
|
13 | #include "qpieseries.h" | |
14 | #include "qscatterseries.h" |
|
14 | #include "qscatterseries.h" | |
15 | #include "qsplineseries.h" |
|
15 | #include "qsplineseries.h" | |
16 | //items |
|
16 | //items | |
17 | #include "axisitem_p.h" |
|
17 | #include "axisitem_p.h" | |
18 | #include "areachartitem_p.h" |
|
18 | #include "areachartitem_p.h" | |
19 | #include "barchartitem_p.h" |
|
19 | #include "barchartitem_p.h" | |
20 | #include "stackedbarchartitem_p.h" |
|
20 | #include "stackedbarchartitem_p.h" | |
21 | #include "percentbarchartitem_p.h" |
|
21 | #include "percentbarchartitem_p.h" | |
22 | #include "linechartitem_p.h" |
|
22 | #include "linechartitem_p.h" | |
23 | #include "piechartitem_p.h" |
|
23 | #include "piechartitem_p.h" | |
24 | #include "scatterchartitem_p.h" |
|
24 | #include "scatterchartitem_p.h" | |
25 | #include "splinechartitem_p.h" |
|
25 | #include "splinechartitem_p.h" | |
26 |
|
26 | |||
27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
28 |
|
28 | |||
29 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
29 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | |
30 | m_chart(chart), |
|
30 | m_chart(chart), | |
31 | m_animator(0), |
|
31 | m_animator(0), | |
32 | m_dataset(dataset), |
|
32 | m_dataset(dataset), | |
33 | m_chartTheme(0), |
|
33 | m_chartTheme(0), | |
34 | m_zoomIndex(0), |
|
34 | m_zoomIndex(0), | |
35 | m_rect(QRectF(QPoint(0,0),m_chart->size())), |
|
35 | m_rect(QRectF(QPoint(0,0),m_chart->size())), | |
36 | m_options(QChart::NoAnimation), |
|
36 | m_options(QChart::NoAnimation), | |
37 | m_themeForce(false) |
|
37 | m_themeForce(false) | |
38 | { |
|
38 | { | |
39 | createConnections(); |
|
39 | createConnections(); | |
40 | setChartTheme(QChart::ChartThemeDefault,false); |
|
40 | setChartTheme(QChart::ChartThemeDefault,false); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | ChartPresenter::~ChartPresenter() |
|
43 | ChartPresenter::~ChartPresenter() | |
44 | { |
|
44 | { | |
45 | delete m_chartTheme; |
|
45 | delete m_chartTheme; | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | void ChartPresenter::createConnections() |
|
48 | void ChartPresenter::createConnections() | |
49 | { |
|
49 | { | |
50 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
50 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); | |
51 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
51 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*))); | |
52 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); |
|
52 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); | |
53 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*))); |
|
53 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*))); | |
54 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
54 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | QRectF ChartPresenter::geometry() const |
|
58 | QRectF ChartPresenter::geometry() const | |
59 | { |
|
59 | { | |
60 | return m_rect; |
|
60 | return m_rect; | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | void ChartPresenter::handleGeometryChanged() |
|
63 | void ChartPresenter::handleGeometryChanged() | |
64 | { |
|
64 | { | |
65 | QRectF rect(QPoint(0,0),m_chart->size()); |
|
65 | QRectF rect(QPoint(0,0),m_chart->size()); | |
66 | rect.adjust(m_chart->padding(),m_chart->padding(), -m_chart->padding(), -m_chart->padding()); |
|
66 | rect.adjust(m_chart->padding(),m_chart->padding(), -m_chart->padding(), -m_chart->padding()); | |
67 |
|
67 | |||
68 | //rewrite zoom stack |
|
68 | //rewrite zoom stack | |
69 | for(int i=0;i<m_zoomStack.count();i++){ |
|
69 | for(int i=0;i<m_zoomStack.count();i++){ | |
70 | QRectF r = m_zoomStack[i]; |
|
70 | QRectF r = m_zoomStack[i]; | |
71 | qreal w = rect.width()/m_rect.width(); |
|
71 | qreal w = rect.width()/m_rect.width(); | |
72 | qreal h = rect.height()/m_rect.height(); |
|
72 | qreal h = rect.height()/m_rect.height(); | |
73 | QPointF tl = r.topLeft(); |
|
73 | QPointF tl = r.topLeft(); | |
74 | tl.setX(tl.x()*w); |
|
74 | tl.setX(tl.x()*w); | |
75 | tl.setY(tl.y()*h); |
|
75 | tl.setY(tl.y()*h); | |
76 | QPointF br = r.bottomRight(); |
|
76 | QPointF br = r.bottomRight(); | |
77 | br.setX(br.x()*w); |
|
77 | br.setX(br.x()*w); | |
78 | br.setY(br.y()*h); |
|
78 | br.setY(br.y()*h); | |
79 | r.setTopLeft(tl); |
|
79 | r.setTopLeft(tl); | |
80 | r.setBottomRight(br); |
|
80 | r.setBottomRight(br); | |
81 | m_zoomStack[i]=r; |
|
81 | m_zoomStack[i]=r; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | m_rect = rect; |
|
84 | m_rect = rect; | |
85 | Q_ASSERT(m_rect.isValid()); |
|
85 | Q_ASSERT(m_rect.isValid()); | |
86 | emit geometryChanged(m_rect); |
|
86 | emit geometryChanged(m_rect); | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) |
|
89 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) | |
90 | { |
|
90 | { | |
91 |
Axis |
|
91 | Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS); | |
92 |
|
92 | |||
93 | if(m_options.testFlag(QChart::GridAxisAnimations)){ |
|
93 | if(m_options.testFlag(QChart::GridAxisAnimations)){ | |
94 | m_animator->addAnimation(item); |
|
94 | m_animator->addAnimation(item); | |
95 | } |
|
95 | } | |
96 |
|
96 | |||
97 | if(axis==m_dataset->axisX()){ |
|
97 | if(axis==m_dataset->axisX()){ | |
98 | m_chartTheme->decorate(axis,true,m_themeForce); |
|
98 | m_chartTheme->decorate(axis,true,m_themeForce); | |
99 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
99 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | |
100 | //initialize |
|
100 | //initialize | |
101 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); |
|
101 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); | |
102 |
|
102 | |||
103 | } |
|
103 | } | |
104 | else{ |
|
104 | else{ | |
105 | m_chartTheme->decorate(axis,false,m_themeForce); |
|
105 | m_chartTheme->decorate(axis,false,m_themeForce); | |
106 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
106 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | |
107 | //initialize |
|
107 | //initialize | |
108 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); |
|
108 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); | |
109 | } |
|
109 | } | |
110 |
|
110 | |||
111 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
111 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
112 | //initialize |
|
112 | //initialize | |
113 | item->handleGeometryChanged(m_rect); |
|
113 | item->handleGeometryChanged(m_rect); | |
114 | m_axisItems.insert(axis, item); |
|
114 | m_axisItems.insert(axis, item); | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
117 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) | |
118 | { |
|
118 | { | |
119 |
Axis |
|
119 | Axis* item = m_axisItems.take(axis); | |
120 | Q_ASSERT(item); |
|
120 | Q_ASSERT(item); | |
121 | if(m_animator) m_animator->removeAnimation(item); |
|
121 | if(m_animator) m_animator->removeAnimation(item); | |
122 | delete item; |
|
122 | delete item; | |
123 | } |
|
123 | } | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) |
|
126 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | |
127 | { |
|
127 | { | |
128 |
Chart |
|
128 | Chart *item = 0 ; | |
129 |
|
129 | |||
130 | switch(series->type()) |
|
130 | switch(series->type()) | |
131 | { |
|
131 | { | |
132 | case QSeries::SeriesTypeLine: { |
|
132 | case QSeries::SeriesTypeLine: { | |
133 |
|
133 | |||
134 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
134 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | |
135 |
LineChartItem* line = new LineChartItem(lineSeries, |
|
135 | LineChartItem* line = new LineChartItem(lineSeries,this); | |
136 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
136 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
137 | m_animator->addAnimation(line); |
|
137 | m_animator->addAnimation(line); | |
138 | } |
|
138 | } | |
139 | m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
139 | m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series),m_themeForce); | |
140 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&))); |
|
140 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&))); | |
141 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
141 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
142 | item = line; |
|
142 | item = line; | |
143 | break; |
|
143 | break; | |
144 | } |
|
144 | } | |
145 |
|
145 | |||
146 | case QSeries::SeriesTypeArea: { |
|
146 | case QSeries::SeriesTypeArea: { | |
147 |
|
147 | |||
148 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
148 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |
149 |
AreaChartItem* area = new AreaChartItem(areaSeries, |
|
149 | AreaChartItem* area = new AreaChartItem(areaSeries,this); | |
150 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
150 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
151 | m_animator->addAnimation(area->upperLineItem()); |
|
151 | m_animator->addAnimation(area->upperLineItem()); | |
152 | if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem()); |
|
152 | if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem()); | |
153 | } |
|
153 | } | |
154 | m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
154 | m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series),m_themeForce); | |
155 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&))); |
|
155 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&))); | |
156 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
156 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
157 | item=area; |
|
157 | item=area; | |
158 | break; |
|
158 | break; | |
159 | } |
|
159 | } | |
160 |
|
160 | |||
161 | case QSeries::SeriesTypeBar: { |
|
161 | case QSeries::SeriesTypeBar: { | |
162 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
162 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); | |
163 |
BarChartItem* bar = new BarChartItem(barSeries, |
|
163 | BarChartItem* bar = new BarChartItem(barSeries,this); | |
164 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
164 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
165 | m_animator->addAnimation(bar); |
|
165 | m_animator->addAnimation(bar); | |
166 | } |
|
166 | } | |
167 | m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries),m_themeForce); |
|
167 | m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries),m_themeForce); | |
168 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); |
|
168 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); | |
169 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
169 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
170 | item=bar; |
|
170 | item=bar; | |
171 | break; |
|
171 | break; | |
172 | } |
|
172 | } | |
173 |
|
173 | |||
174 | case QSeries::SeriesTypeStackedBar: { |
|
174 | case QSeries::SeriesTypeStackedBar: { | |
175 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
175 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | |
176 |
StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries, |
|
176 | StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,this); | |
177 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
177 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
178 | m_animator->addAnimation(bar); |
|
178 | m_animator->addAnimation(bar); | |
179 | } |
|
179 | } | |
180 | m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries),m_themeForce); |
|
180 | m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries),m_themeForce); | |
181 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); |
|
181 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); | |
182 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
182 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
183 | item=bar; |
|
183 | item=bar; | |
184 | break; |
|
184 | break; | |
185 | } |
|
185 | } | |
186 |
|
186 | |||
187 | case QSeries::SeriesTypePercentBar: { |
|
187 | case QSeries::SeriesTypePercentBar: { | |
188 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
188 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | |
189 |
PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries, |
|
189 | PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,this); | |
190 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
190 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
191 | m_animator->addAnimation(bar); |
|
191 | m_animator->addAnimation(bar); | |
192 | } |
|
192 | } | |
193 | m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries),m_themeForce); |
|
193 | m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries),m_themeForce); | |
194 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); |
|
194 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); | |
195 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
195 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
196 | item=bar; |
|
196 | item=bar; | |
197 | break; |
|
197 | break; | |
198 | } |
|
198 | } | |
199 |
|
199 | |||
200 | case QSeries::SeriesTypeScatter: { |
|
200 | case QSeries::SeriesTypeScatter: { | |
201 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
201 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | |
202 |
ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, |
|
202 | ScatterChartItem *scatter = new ScatterChartItem(scatterSeries,this); | |
203 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
203 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
204 | m_animator->addAnimation(scatter); |
|
204 | m_animator->addAnimation(scatter); | |
205 | } |
|
205 | } | |
206 | m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
206 | m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series),m_themeForce); | |
207 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&))); |
|
207 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&))); | |
208 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
208 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
209 | item = scatter; |
|
209 | item = scatter; | |
210 | break; |
|
210 | break; | |
211 | } |
|
211 | } | |
212 |
|
212 | |||
213 | case QSeries::SeriesTypePie: { |
|
213 | case QSeries::SeriesTypePie: { | |
214 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
214 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |
215 |
PieChartItem* pie = new PieChartItem(pieSeries, this |
|
215 | PieChartItem* pie = new PieChartItem(pieSeries, this); | |
216 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
216 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
217 | m_animator->addAnimation(pie); |
|
217 | m_animator->addAnimation(pie); | |
218 | } |
|
218 | } | |
219 | m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
219 | m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series),m_themeForce); | |
220 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&))); |
|
220 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&))); | |
221 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
221 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
222 | // Hide all from background when there is only piechart |
|
222 | // Hide all from background when there is only piechart | |
223 | // TODO: refactor this ugly code... should be one setting for this |
|
223 | // TODO: refactor this ugly code... should be one setting for this | |
224 | if (m_chartItems.count() == 0) { |
|
224 | if (m_chartItems.count() == 0) { | |
225 | m_chart->axisX()->hide(); |
|
225 | m_chart->axisX()->hide(); | |
226 | m_chart->axisY()->hide(); |
|
226 | m_chart->axisY()->hide(); | |
227 | } |
|
227 | } | |
228 | item=pie; |
|
228 | item=pie; | |
229 | break; |
|
229 | break; | |
230 | } |
|
230 | } | |
231 |
|
231 | |||
232 | case QSeries::SeriesTypeSpline: { |
|
232 | case QSeries::SeriesTypeSpline: { | |
233 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
233 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | |
234 |
SplineChartItem* spline = new SplineChartItem(splineSeries, |
|
234 | SplineChartItem* spline = new SplineChartItem(splineSeries, this); | |
235 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
235 | if(m_options.testFlag(QChart::SeriesAnimations)) { | |
236 | m_animator->addAnimation(spline); |
|
236 | m_animator->addAnimation(spline); | |
237 | } |
|
237 | } | |
238 | m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series),m_themeForce); |
|
238 | m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series),m_themeForce); | |
239 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&))); |
|
239 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&))); | |
240 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
240 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
241 | item=spline; |
|
241 | item=spline; | |
242 | break; |
|
242 | break; | |
243 | } |
|
243 | } | |
244 | default: { |
|
244 | default: { | |
245 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
245 | qDebug()<< "Series type" << series->type() << "not implemented."; | |
246 | break; |
|
246 | break; | |
247 | } |
|
247 | } | |
248 | } |
|
248 | } | |
249 |
|
249 | |||
250 | //initialize |
|
250 | //initialize | |
251 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
251 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | |
252 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
252 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | |
253 | m_chartItems.insert(series,item); |
|
253 | m_chartItems.insert(series,item); | |
254 | zoomReset(); |
|
254 | zoomReset(); | |
255 | } |
|
255 | } | |
256 |
|
256 | |||
257 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
257 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | |
258 | { |
|
258 | { | |
259 |
Chart |
|
259 | Chart* item = m_chartItems.take(series); | |
260 | Q_ASSERT(item); |
|
260 | Q_ASSERT(item); | |
261 | if(m_animator) { |
|
261 | if(m_animator) { | |
262 | //small hack to handle area animations |
|
262 | //small hack to handle area animations | |
263 | if(series->type()==QSeries::SeriesTypeArea){ |
|
263 | if(series->type()==QSeries::SeriesTypeArea){ | |
264 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
264 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |
265 | AreaChartItem* area = static_cast<AreaChartItem*>(item); |
|
265 | AreaChartItem* area = static_cast<AreaChartItem*>(item); | |
266 | m_animator->removeAnimation(area->upperLineItem()); |
|
266 | m_animator->removeAnimation(area->upperLineItem()); | |
267 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); |
|
267 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); | |
268 | }else |
|
268 | }else | |
269 | m_animator->removeAnimation(item); |
|
269 | m_animator->removeAnimation(item); | |
270 | } |
|
270 | } | |
271 | delete item; |
|
271 | delete item; | |
272 | } |
|
272 | } | |
273 |
|
273 | |||
274 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme,bool force) |
|
274 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme,bool force) | |
275 | { |
|
275 | { | |
276 | if(m_chartTheme && m_chartTheme->id() == theme) return; |
|
276 | if(m_chartTheme && m_chartTheme->id() == theme) return; | |
277 | delete m_chartTheme; |
|
277 | delete m_chartTheme; | |
278 | m_themeForce = force; |
|
278 | m_themeForce = force; | |
279 | m_chartTheme = ChartTheme::createTheme(theme); |
|
279 | m_chartTheme = ChartTheme::createTheme(theme); | |
280 | m_chartTheme->decorate(m_chart,m_themeForce); |
|
280 | m_chartTheme->decorate(m_chart,m_themeForce); | |
281 | m_chartTheme->decorate(m_chart->legend(),m_themeForce); |
|
281 | m_chartTheme->decorate(m_chart->legend(),m_themeForce); | |
282 | resetAllElements(); |
|
282 | resetAllElements(); | |
283 | } |
|
283 | } | |
284 |
|
284 | |||
285 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
285 | QChart::ChartTheme ChartPresenter::chartTheme() | |
286 | { |
|
286 | { | |
287 | return m_chartTheme->id(); |
|
287 | return m_chartTheme->id(); | |
288 | } |
|
288 | } | |
289 |
|
289 | |||
290 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
290 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | |
291 | { |
|
291 | { | |
292 | if(m_options!=options) { |
|
292 | if(m_options!=options) { | |
293 |
|
293 | |||
294 | m_options=options; |
|
294 | m_options=options; | |
295 |
|
295 | |||
296 | if(m_options!=QChart::NoAnimation && !m_animator) { |
|
296 | if(m_options!=QChart::NoAnimation && !m_animator) { | |
297 | m_animator= new ChartAnimator(this); |
|
297 | m_animator= new ChartAnimator(this); | |
298 |
|
298 | |||
299 | } |
|
299 | } | |
300 | resetAllElements(); |
|
300 | resetAllElements(); | |
301 | } |
|
301 | } | |
302 |
|
302 | |||
303 | } |
|
303 | } | |
304 |
|
304 | |||
305 | void ChartPresenter::resetAllElements() |
|
305 | void ChartPresenter::resetAllElements() | |
306 | { |
|
306 | { | |
307 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
307 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); | |
308 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
308 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); | |
309 |
|
309 | |||
310 | foreach(QChartAxis* axis, axisList) { |
|
310 | foreach(QChartAxis* axis, axisList) { | |
311 | handleAxisRemoved(axis); |
|
311 | handleAxisRemoved(axis); | |
312 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
312 | handleAxisAdded(axis,m_dataset->domain(axis)); | |
313 | } |
|
313 | } | |
314 | foreach(QSeries* series, seriesList) { |
|
314 | foreach(QSeries* series, seriesList) { | |
315 | handleSeriesRemoved(series); |
|
315 | handleSeriesRemoved(series); | |
316 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
316 | handleSeriesAdded(series,m_dataset->domain(series)); | |
317 | } |
|
317 | } | |
318 | } |
|
318 | } | |
319 |
|
319 | |||
320 | void ChartPresenter::zoomIn() |
|
320 | void ChartPresenter::zoomIn() | |
321 | { |
|
321 | { | |
322 | QRectF rect = geometry(); |
|
322 | QRectF rect = geometry(); | |
323 | rect.setWidth(rect.width()/2); |
|
323 | rect.setWidth(rect.width()/2); | |
324 | rect.setHeight(rect.height()/2); |
|
324 | rect.setHeight(rect.height()/2); | |
325 | rect.moveCenter(geometry().center()); |
|
325 | rect.moveCenter(geometry().center()); | |
326 | zoomIn(rect); |
|
326 | zoomIn(rect); | |
327 | } |
|
327 | } | |
328 |
|
328 | |||
329 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
329 | void ChartPresenter::zoomIn(const QRectF& rect) | |
330 | { |
|
330 | { | |
331 | QRectF r = rect.normalized(); |
|
331 | QRectF r = rect.normalized(); | |
332 | r.translate(-m_chart->padding(), -m_chart->padding()); |
|
332 | r.translate(-m_chart->padding(), -m_chart->padding()); | |
333 | if(m_animator) { |
|
333 | if(m_animator) { | |
334 |
|
334 | |||
335 | QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height()); |
|
335 | QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height()); | |
336 | m_animator->setState(ChartAnimator::ZoomInState,point); |
|
336 | m_animator->setState(ChartAnimator::ZoomInState,point); | |
337 | } |
|
337 | } | |
338 | m_dataset->zoomInDomain(r,geometry().size()); |
|
338 | m_dataset->zoomInDomain(r,geometry().size()); | |
339 | m_zoomStack<<r; |
|
339 | m_zoomStack<<r; | |
340 | m_zoomIndex++; |
|
340 | m_zoomIndex++; | |
341 | if(m_animator) { |
|
341 | if(m_animator) { | |
342 | m_animator->setState(ChartAnimator::ShowState); |
|
342 | m_animator->setState(ChartAnimator::ShowState); | |
343 | } |
|
343 | } | |
344 | } |
|
344 | } | |
345 |
|
345 | |||
346 | void ChartPresenter::zoomOut() |
|
346 | void ChartPresenter::zoomOut() | |
347 | { |
|
347 | { | |
348 | if(m_zoomIndex==0) return; |
|
348 | if(m_zoomIndex==0) return; | |
349 | if(m_animator) |
|
349 | if(m_animator) | |
350 | { |
|
350 | { | |
351 | m_animator->setState(ChartAnimator::ZoomOutState); |
|
351 | m_animator->setState(ChartAnimator::ZoomOutState); | |
352 | } |
|
352 | } | |
353 | m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); |
|
353 | m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); | |
354 | m_zoomIndex--; |
|
354 | m_zoomIndex--; | |
355 | m_zoomStack.resize(m_zoomIndex); |
|
355 | m_zoomStack.resize(m_zoomIndex); | |
356 | if(m_animator){ |
|
356 | if(m_animator){ | |
357 | m_animator->setState(ChartAnimator::ShowState); |
|
357 | m_animator->setState(ChartAnimator::ShowState); | |
358 | } |
|
358 | } | |
359 | } |
|
359 | } | |
360 |
|
360 | |||
361 | void ChartPresenter::zoomReset() |
|
361 | void ChartPresenter::zoomReset() | |
362 | { |
|
362 | { | |
363 | m_zoomIndex=0; |
|
363 | m_zoomIndex=0; | |
364 | m_zoomStack.resize(m_zoomIndex); |
|
364 | m_zoomStack.resize(m_zoomIndex); | |
365 | } |
|
365 | } | |
366 |
|
366 | |||
367 | void ChartPresenter::scroll(int dx,int dy) |
|
367 | void ChartPresenter::scroll(int dx,int dy) | |
368 | { |
|
368 | { | |
369 | if(m_animator){ |
|
369 | if(m_animator){ | |
370 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); |
|
370 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); | |
371 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); |
|
371 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); | |
372 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); |
|
372 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); | |
373 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); |
|
373 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); | |
374 | } |
|
374 | } | |
375 |
|
375 | |||
376 | m_dataset->scrollDomain(dx,dy,geometry().size()); |
|
376 | m_dataset->scrollDomain(dx,dy,geometry().size()); | |
377 |
|
377 | |||
378 | if(m_animator){ |
|
378 | if(m_animator){ | |
379 | m_animator->setState(ChartAnimator::ShowState); |
|
379 | m_animator->setState(ChartAnimator::ShowState); | |
380 | } |
|
380 | } | |
381 | } |
|
381 | } | |
382 |
|
382 | |||
383 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
383 | QChart::AnimationOptions ChartPresenter::animationOptions() const | |
384 | { |
|
384 | { | |
385 | return m_options; |
|
385 | return m_options; | |
386 | } |
|
386 | } | |
387 |
|
387 | |||
388 |
|
388 | |||
389 | #include "moc_chartpresenter_p.cpp" |
|
389 | #include "moc_chartpresenter_p.cpp" | |
390 |
|
390 | |||
391 | QTCOMMERCIALCHART_END_NAMESPACE |
|
391 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,90 +1,92 | |||||
1 | #ifndef CHARTPRESENTER_H_ |
|
1 | #ifndef CHARTPRESENTER_H_ | |
2 | #define CHARTPRESENTER_H_ |
|
2 | #define CHARTPRESENTER_H_ | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO |
|
5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO | |
6 | #include "qchartaxis.h" |
|
6 | #include "qchartaxis.h" | |
7 | #include <QRectF> |
|
7 | #include <QRectF> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 |
class Chart |
|
11 | class Chart; | |
12 | class QSeries; |
|
12 | class QSeries; | |
13 | class ChartDataSet; |
|
13 | class ChartDataSet; | |
14 | class Domain; |
|
14 | class Domain; | |
15 |
class Axis |
|
15 | class Axis; | |
16 | class ChartTheme; |
|
16 | class ChartTheme; | |
17 | class ChartAnimator; |
|
17 | class ChartAnimator; | |
18 |
|
18 | |||
19 | class ChartPresenter: public QObject |
|
19 | class ChartPresenter: public QObject | |
20 | { |
|
20 | { | |
21 | Q_OBJECT |
|
21 | Q_OBJECT | |
22 | public: |
|
22 | public: | |
23 | enum ZValues { |
|
23 | enum ZValues { | |
24 | BackgroundZValue = -1, |
|
24 | BackgroundZValue = -1, | |
25 | ShadesZValue, |
|
25 | ShadesZValue, | |
26 | GridZValue, |
|
26 | GridZValue, | |
27 | AxisZValue, |
|
27 | AxisZValue, | |
28 | LineChartZValue, |
|
28 | LineChartZValue, | |
29 | BarSeriesZValue, |
|
29 | BarSeriesZValue, | |
30 | ScatterSeriesZValue, |
|
30 | ScatterSeriesZValue, | |
31 | PieSeriesZValue, |
|
31 | PieSeriesZValue, | |
32 | LegendZValue |
|
32 | LegendZValue | |
33 | }; |
|
33 | }; | |
34 |
|
34 | |||
35 | ChartPresenter(QChart* chart,ChartDataSet *dataset); |
|
35 | ChartPresenter(QChart* chart,ChartDataSet *dataset); | |
36 | virtual ~ChartPresenter(); |
|
36 | virtual ~ChartPresenter(); | |
37 |
|
37 | |||
38 | void setMargin(int margin); |
|
38 | void setMargin(int margin); | |
39 | int margin() const; |
|
39 | int margin() const; | |
40 |
|
40 | |||
41 | QRectF geometry() const; |
|
41 | QRectF geometry() const; | |
42 |
|
42 | |||
43 | ChartAnimator* animator() const {return m_animator;} |
|
43 | ChartAnimator* animator() const {return m_animator;} | |
44 | ChartTheme *theme() { return m_chartTheme; } |
|
44 | ChartTheme *theme() { return m_chartTheme; } | |
45 | ChartDataSet *dataSet() { return m_dataset; } |
|
45 | ChartDataSet *dataSet() { return m_dataset; } | |
46 |
|
46 | |||
47 | void setChartTheme(QChart::ChartTheme theme,bool force = true); |
|
47 | void setChartTheme(QChart::ChartTheme theme,bool force = true); | |
48 | QChart::ChartTheme chartTheme(); |
|
48 | QChart::ChartTheme chartTheme(); | |
49 |
|
49 | |||
50 | void setAnimationOptions(QChart::AnimationOptions options); |
|
50 | void setAnimationOptions(QChart::AnimationOptions options); | |
51 | QChart::AnimationOptions animationOptions() const; |
|
51 | QChart::AnimationOptions animationOptions() const; | |
52 |
|
52 | |||
|
53 | QGraphicsItem* rootItem() const {return m_chart;}; | |||
|
54 | ||||
53 | void zoomIn(); |
|
55 | void zoomIn(); | |
54 | void zoomIn(const QRectF& rect); |
|
56 | void zoomIn(const QRectF& rect); | |
55 | void zoomOut(); |
|
57 | void zoomOut(); | |
56 | void zoomReset(); |
|
58 | void zoomReset(); | |
57 | void scroll(int dx,int dy); |
|
59 | void scroll(int dx,int dy); | |
58 |
|
60 | |||
59 | private: |
|
61 | private: | |
60 | void createConnections(); |
|
62 | void createConnections(); | |
61 | void resetAllElements(); |
|
63 | void resetAllElements(); | |
62 |
|
64 | |||
63 | public slots: |
|
65 | public slots: | |
64 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
66 | void handleSeriesAdded(QSeries* series,Domain* domain); | |
65 | void handleSeriesRemoved(QSeries* series); |
|
67 | void handleSeriesRemoved(QSeries* series); | |
66 | void handleAxisAdded(QChartAxis* axis,Domain* domain); |
|
68 | void handleAxisAdded(QChartAxis* axis,Domain* domain); | |
67 | void handleAxisRemoved(QChartAxis* axis); |
|
69 | void handleAxisRemoved(QChartAxis* axis); | |
68 | void handleGeometryChanged(); |
|
70 | void handleGeometryChanged(); | |
69 |
|
71 | |||
70 | signals: |
|
72 | signals: | |
71 | void geometryChanged(const QRectF& rect); |
|
73 | void geometryChanged(const QRectF& rect); | |
72 |
|
74 | |||
73 | private: |
|
75 | private: | |
74 | QChart* m_chart; |
|
76 | QChart* m_chart; | |
75 | ChartAnimator* m_animator; |
|
77 | ChartAnimator* m_animator; | |
76 | ChartDataSet* m_dataset; |
|
78 | ChartDataSet* m_dataset; | |
77 | ChartTheme *m_chartTheme; |
|
79 | ChartTheme *m_chartTheme; | |
78 | int m_zoomIndex; |
|
80 | int m_zoomIndex; | |
79 |
QMap<QSeries*,Chart |
|
81 | QMap<QSeries*,Chart*> m_chartItems; | |
80 |
QMap<QChartAxis*,Axis |
|
82 | QMap<QChartAxis*,Axis*> m_axisItems; | |
81 | QVector<QRectF> m_zoomStack; |
|
83 | QVector<QRectF> m_zoomStack; | |
82 | QRectF m_rect; |
|
84 | QRectF m_rect; | |
83 | QChart::AnimationOptions m_options; |
|
85 | QChart::AnimationOptions m_options; | |
84 | bool m_themeForce; |
|
86 | bool m_themeForce; | |
85 |
|
87 | |||
86 | }; |
|
88 | }; | |
87 |
|
89 | |||
88 | QTCOMMERCIALCHART_END_NAMESPACE |
|
90 | QTCOMMERCIALCHART_END_NAMESPACE | |
89 |
|
91 | |||
90 | #endif /* CHARTPRESENTER_H_ */ |
|
92 | #endif /* CHARTPRESENTER_H_ */ |
@@ -1,83 +1,83 | |||||
1 | #include "linechartitem_p.h" |
|
1 | #include "linechartitem_p.h" | |
2 | #include "qlineseries.h" |
|
2 | #include "qlineseries.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | //TODO: optimize : remove points which are not visible |
|
9 | //TODO: optimize : remove points which are not visible | |
10 |
|
10 | |||
11 |
LineChartItem::LineChartItem(QLineSeries* series, |
|
11 | LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChartItem(series,presenter), | |
12 | m_series(series), |
|
12 | m_series(series), | |
13 | m_pointsVisible(false) |
|
13 | m_pointsVisible(false) | |
14 | { |
|
14 | { | |
15 | setZValue(ChartPresenter::LineChartZValue); |
|
15 | setZValue(ChartPresenter::LineChartZValue); | |
16 | QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
16 | QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); | |
17 | handleUpdated(); |
|
17 | handleUpdated(); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | QRectF LineChartItem::boundingRect() const |
|
20 | QRectF LineChartItem::boundingRect() const | |
21 | { |
|
21 | { | |
22 | return m_rect; |
|
22 | return m_rect; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | QPainterPath LineChartItem::shape() const |
|
25 | QPainterPath LineChartItem::shape() const | |
26 | { |
|
26 | { | |
27 | return m_path; |
|
27 | return m_path; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | void LineChartItem::setLayout(QVector<QPointF>& points) |
|
30 | void LineChartItem::setLayout(QVector<QPointF>& points) | |
31 | { |
|
31 | { | |
32 | if(points.size()==0) |
|
32 | if(points.size()==0) | |
33 | { |
|
33 | { | |
34 | XYChartItem::setLayout(points); |
|
34 | XYChartItem::setLayout(points); | |
35 | return; |
|
35 | return; | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
38 | QList<QGraphicsItem*> items = m_items.childItems(); | |
39 |
|
39 | |||
40 | QPainterPath linePath(points.at(0)); |
|
40 | QPainterPath linePath(points.at(0)); | |
41 |
|
41 | |||
42 | for(int i=1; i< points.size();i++) { |
|
42 | for(int i=1; i< points.size();i++) { | |
43 | linePath.lineTo(points.at(i)); |
|
43 | linePath.lineTo(points.at(i)); | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | prepareGeometryChange(); |
|
46 | prepareGeometryChange(); | |
47 | m_path = linePath; |
|
47 | m_path = linePath; | |
48 | m_rect = linePath.boundingRect(); |
|
48 | m_rect = linePath.boundingRect(); | |
49 |
|
49 | |||
50 | XYChartItem::setLayout(points); |
|
50 | XYChartItem::setLayout(points); | |
51 |
|
51 | |||
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | void LineChartItem::handleUpdated() |
|
54 | void LineChartItem::handleUpdated() | |
55 | { |
|
55 | { | |
56 | m_pointsVisible = m_series->pointsVisible(); |
|
56 | m_pointsVisible = m_series->pointsVisible(); | |
57 | m_linePen = m_series->pen(); |
|
57 | m_linePen = m_series->pen(); | |
58 | m_pointPen = m_series->pen(); |
|
58 | m_pointPen = m_series->pen(); | |
59 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
59 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
60 | update(); |
|
60 | update(); | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | //painter |
|
63 | //painter | |
64 |
|
64 | |||
65 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
65 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
66 | { |
|
66 | { | |
67 | Q_UNUSED(widget) |
|
67 | Q_UNUSED(widget) | |
68 | Q_UNUSED(option) |
|
68 | Q_UNUSED(option) | |
69 |
|
69 | |||
70 | painter->save(); |
|
70 | painter->save(); | |
71 | painter->setPen(m_linePen); |
|
71 | painter->setPen(m_linePen); | |
72 | painter->setClipRect(clipRect()); |
|
72 | painter->setClipRect(clipRect()); | |
73 | painter->drawPath(m_path); |
|
73 | painter->drawPath(m_path); | |
74 | if(m_pointsVisible){ |
|
74 | if(m_pointsVisible){ | |
75 | painter->setPen(m_pointPen); |
|
75 | painter->setPen(m_pointPen); | |
76 | painter->drawPoints(points()); |
|
76 | painter->drawPoints(points()); | |
77 | } |
|
77 | } | |
78 | painter->restore(); |
|
78 | painter->restore(); | |
79 | } |
|
79 | } | |
80 |
|
80 | |||
81 | #include "moc_linechartitem_p.cpp" |
|
81 | #include "moc_linechartitem_p.cpp" | |
82 |
|
82 | |||
83 | QTCOMMERCIALCHART_END_NAMESPACE |
|
83 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,43 +1,44 | |||||
1 | #ifndef LINECHARTITEM_H |
|
1 | #ifndef LINECHARTITEM_H | |
2 | #define LINECHARTITEM_H |
|
2 | #define LINECHARTITEM_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "xychartitem_p.h" |
|
5 | #include "xychartitem_p.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QLineSeries; |
|
10 | class QLineSeries; | |
|
11 | class ChartPresenter; | |||
11 |
|
12 | |||
12 | class LineChartItem : public XYChartItem |
|
13 | class LineChartItem : public XYChartItem | |
13 | { |
|
14 | { | |
14 | Q_OBJECT |
|
15 | Q_OBJECT | |
15 | public: |
|
16 | public: | |
16 |
explicit LineChartItem(QLineSeries* series, |
|
17 | explicit LineChartItem(QLineSeries* series,ChartPresenter *presenter); | |
17 | ~ LineChartItem(){}; |
|
18 | ~ LineChartItem(){}; | |
18 |
|
19 | |||
19 | //from QGraphicsItem |
|
20 | //from QGraphicsItem | |
20 | QRectF boundingRect() const; |
|
21 | QRectF boundingRect() const; | |
21 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
22 | QPainterPath shape() const; |
|
23 | QPainterPath shape() const; | |
23 |
|
24 | |||
24 | public slots: |
|
25 | public slots: | |
25 | void handleUpdated(); |
|
26 | void handleUpdated(); | |
26 |
|
27 | |||
27 | protected: |
|
28 | protected: | |
28 | void setLayout(QVector<QPointF>& points); |
|
29 | void setLayout(QVector<QPointF>& points); | |
29 |
|
30 | |||
30 | private: |
|
31 | private: | |
31 | QLineSeries* m_series; |
|
32 | QLineSeries* m_series; | |
32 | QGraphicsItemGroup m_items; |
|
33 | QGraphicsItemGroup m_items; | |
33 | QPainterPath m_path; |
|
34 | QPainterPath m_path; | |
34 | QRectF m_rect; |
|
35 | QRectF m_rect; | |
35 | QPen m_linePen; |
|
36 | QPen m_linePen; | |
36 | QPen m_pointPen; |
|
37 | QPen m_pointPen; | |
37 | bool m_pointsVisible; |
|
38 | bool m_pointsVisible; | |
38 |
|
39 | |||
39 | }; |
|
40 | }; | |
40 |
|
41 | |||
41 | QTCOMMERCIALCHART_END_NAMESPACE |
|
42 | QTCOMMERCIALCHART_END_NAMESPACE | |
42 |
|
43 | |||
43 | #endif |
|
44 | #endif |
@@ -1,193 +1,192 | |||||
1 | #include "piechartitem_p.h" |
|
1 | #include "piechartitem_p.h" | |
2 | #include "piesliceitem_p.h" |
|
2 | #include "piesliceitem_p.h" | |
3 | #include "qpieslice.h" |
|
3 | #include "qpieslice.h" | |
4 | #include "qpiesliceprivate_p.h" |
|
4 | #include "qpiesliceprivate_p.h" | |
5 | #include "qpieseries.h" |
|
5 | #include "qpieseries.h" | |
6 | #include "chartpresenter_p.h" |
|
6 | #include "chartpresenter_p.h" | |
7 | #include "chartdataset_p.h" |
|
7 | #include "chartdataset_p.h" | |
8 | #include "chartanimator_p.h" |
|
8 | #include "chartanimator_p.h" | |
9 | #include <QDebug> |
|
9 | #include <QDebug> | |
10 | #include <QPainter> |
|
10 | #include <QPainter> | |
11 | #include <QTimer> |
|
11 | #include <QTimer> | |
12 |
|
12 | |||
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
14 |
|
14 | |||
15 |
PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter |
|
15 | PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter) | |
16 |
:ChartItem(p |
|
16 | :ChartItem(presenter), | |
17 |
m_series(series) |
|
17 | m_series(series) | |
18 | m_presenter(presenter) |
|
|||
19 | { |
|
18 | { | |
20 | Q_ASSERT(series); |
|
19 | Q_ASSERT(series); | |
21 | connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>))); |
|
20 | connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>))); | |
22 | connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>))); |
|
21 | connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>))); | |
23 | connect(series, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged())); |
|
22 | connect(series, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged())); | |
24 | connect(series, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged())); |
|
23 | connect(series, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged())); | |
25 |
|
24 | |||
26 | QTimer::singleShot(0, this, SLOT(initialize())); |
|
25 | QTimer::singleShot(0, this, SLOT(initialize())); | |
27 |
|
26 | |||
28 | // Note: the following does not affect as long as the item does not have anything to paint |
|
27 | // Note: the following does not affect as long as the item does not have anything to paint | |
29 | setZValue(ChartPresenter::PieSeriesZValue); |
|
28 | setZValue(ChartPresenter::PieSeriesZValue); | |
30 | } |
|
29 | } | |
31 |
|
30 | |||
32 | PieChartItem::~PieChartItem() |
|
31 | PieChartItem::~PieChartItem() | |
33 | { |
|
32 | { | |
34 | // slices deleted automatically through QGraphicsItem |
|
33 | // slices deleted automatically through QGraphicsItem | |
35 | } |
|
34 | } | |
36 |
|
35 | |||
37 | void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
36 | void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | |
38 | { |
|
37 | { | |
39 | Q_UNUSED(painter) |
|
38 | Q_UNUSED(painter) | |
40 | // TODO: paint shadows for all components |
|
39 | // TODO: paint shadows for all components | |
41 | // - get paths from items & merge & offset and draw with shadow color? |
|
40 | // - get paths from items & merge & offset and draw with shadow color? | |
42 | //painter->setBrush(QBrush(Qt::red)); |
|
41 | //painter->setBrush(QBrush(Qt::red)); | |
43 | //painter->drawRect(m_debugRect); |
|
42 | //painter->drawRect(m_debugRect); | |
44 | } |
|
43 | } | |
45 |
|
44 | |||
46 | void PieChartItem::initialize() |
|
45 | void PieChartItem::initialize() | |
47 | { |
|
46 | { | |
48 | handleSlicesAdded(m_series->slices()); |
|
47 | handleSlicesAdded(m_series->slices()); | |
49 | } |
|
48 | } | |
50 |
|
49 | |||
51 | void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices) |
|
50 | void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices) | |
52 | { |
|
51 | { | |
53 | bool isEmpty = m_slices.isEmpty(); |
|
52 | bool isEmpty = m_slices.isEmpty(); | |
54 |
|
53 | |||
55 |
|
|
54 | presenter()->theme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | |
56 |
|
55 | |||
57 | foreach (QPieSlice *s, slices) { |
|
56 | foreach (QPieSlice *s, slices) { | |
58 | PieSliceItem* item = new PieSliceItem(this); |
|
57 | PieSliceItem* item = new PieSliceItem(this); | |
59 | m_slices.insert(s, item); |
|
58 | m_slices.insert(s, item); | |
60 | connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged())); |
|
59 | connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged())); | |
61 | connect(item, SIGNAL(clicked()), s, SIGNAL(clicked())); |
|
60 | connect(item, SIGNAL(clicked()), s, SIGNAL(clicked())); | |
62 | connect(item, SIGNAL(hoverEnter()), s, SIGNAL(hoverEnter())); |
|
61 | connect(item, SIGNAL(hoverEnter()), s, SIGNAL(hoverEnter())); | |
63 | connect(item, SIGNAL(hoverLeave()), s, SIGNAL(hoverLeave())); |
|
62 | connect(item, SIGNAL(hoverLeave()), s, SIGNAL(hoverLeave())); | |
64 |
|
63 | |||
65 | PieSliceData data = sliceData(s); |
|
64 | PieSliceData data = sliceData(s); | |
66 |
|
65 | |||
67 | if (m_animator) |
|
66 | if (m_animator) | |
68 | m_animator->addAnimation(this, s, data, isEmpty); |
|
67 | m_animator->addAnimation(this, s, data, isEmpty); | |
69 | else |
|
68 | else | |
70 | setLayout(s, data); |
|
69 | setLayout(s, data); | |
71 | } |
|
70 | } | |
72 | } |
|
71 | } | |
73 |
|
72 | |||
74 | void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices) |
|
73 | void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices) | |
75 | { |
|
74 | { | |
76 |
|
|
75 | presenter()->theme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | |
77 |
|
76 | |||
78 | foreach (QPieSlice *s, slices) { |
|
77 | foreach (QPieSlice *s, slices) { | |
79 |
if ( |
|
78 | if (animator()) | |
80 |
|
|
79 | animator()->removeAnimation(this, s); | |
81 | else |
|
80 | else | |
82 | destroySlice(s); |
|
81 | destroySlice(s); | |
83 | } |
|
82 | } | |
84 | } |
|
83 | } | |
85 |
|
84 | |||
86 | void PieChartItem::handlePieLayoutChanged() |
|
85 | void PieChartItem::handlePieLayoutChanged() | |
87 | { |
|
86 | { | |
88 | PieLayout layout = calculateLayout(); |
|
87 | PieLayout layout = calculateLayout(); | |
89 | applyLayout(layout); |
|
88 | applyLayout(layout); | |
90 | update(); |
|
89 | update(); | |
91 | } |
|
90 | } | |
92 |
|
91 | |||
93 | void PieChartItem::handleSliceChanged() |
|
92 | void PieChartItem::handleSliceChanged() | |
94 | { |
|
93 | { | |
95 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
94 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
96 | Q_ASSERT(m_slices.contains(slice)); |
|
95 | Q_ASSERT(m_slices.contains(slice)); | |
97 | PieSliceData data = sliceData(slice); |
|
96 | PieSliceData data = sliceData(slice); | |
98 | updateLayout(slice, data); |
|
97 | updateLayout(slice, data); | |
99 | update(); |
|
98 | update(); | |
100 | } |
|
99 | } | |
101 |
|
100 | |||
102 | void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal) |
|
101 | void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal) | |
103 | { |
|
102 | { | |
104 | // TODO |
|
103 | // TODO | |
105 | } |
|
104 | } | |
106 |
|
105 | |||
107 | void PieChartItem::handleGeometryChanged(const QRectF& rect) |
|
106 | void PieChartItem::handleGeometryChanged(const QRectF& rect) | |
108 | { |
|
107 | { | |
109 | prepareGeometryChange(); |
|
108 | prepareGeometryChange(); | |
110 | m_rect = rect; |
|
109 | m_rect = rect; | |
111 | handlePieLayoutChanged(); |
|
110 | handlePieLayoutChanged(); | |
112 | } |
|
111 | } | |
113 |
|
112 | |||
114 | void PieChartItem::calculatePieLayout() |
|
113 | void PieChartItem::calculatePieLayout() | |
115 | { |
|
114 | { | |
116 | // find pie center coordinates |
|
115 | // find pie center coordinates | |
117 | m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->pieHorizontalPosition())); |
|
116 | m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->pieHorizontalPosition())); | |
118 | m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->pieVerticalPosition())); |
|
117 | m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->pieVerticalPosition())); | |
119 |
|
118 | |||
120 | // find maximum radius for pie |
|
119 | // find maximum radius for pie | |
121 | m_pieRadius = m_rect.height() / 2; |
|
120 | m_pieRadius = m_rect.height() / 2; | |
122 | if (m_rect.width() < m_rect.height()) |
|
121 | if (m_rect.width() < m_rect.height()) | |
123 | m_pieRadius = m_rect.width() / 2; |
|
122 | m_pieRadius = m_rect.width() / 2; | |
124 |
|
123 | |||
125 | // apply size factor |
|
124 | // apply size factor | |
126 | m_pieRadius *= m_series->pieSize(); |
|
125 | m_pieRadius *= m_series->pieSize(); | |
127 | } |
|
126 | } | |
128 |
|
127 | |||
129 | PieSliceData PieChartItem::sliceData(QPieSlice *slice) |
|
128 | PieSliceData PieChartItem::sliceData(QPieSlice *slice) | |
130 | { |
|
129 | { | |
131 | PieSliceData sliceData = slice->data_ptr()->m_data; |
|
130 | PieSliceData sliceData = slice->data_ptr()->m_data; | |
132 | sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice); |
|
131 | sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice); | |
133 | sliceData.m_radius = m_pieRadius; |
|
132 | sliceData.m_radius = m_pieRadius; | |
134 | sliceData.m_angleSpan = slice->endAngle() - slice->startAngle(); |
|
133 | sliceData.m_angleSpan = slice->endAngle() - slice->startAngle(); | |
135 | return sliceData; |
|
134 | return sliceData; | |
136 | } |
|
135 | } | |
137 |
|
136 | |||
138 | PieLayout PieChartItem::calculateLayout() |
|
137 | PieLayout PieChartItem::calculateLayout() | |
139 | { |
|
138 | { | |
140 | calculatePieLayout(); |
|
139 | calculatePieLayout(); | |
141 | PieLayout layout; |
|
140 | PieLayout layout; | |
142 | foreach (QPieSlice* s, m_series->slices()) { |
|
141 | foreach (QPieSlice* s, m_series->slices()) { | |
143 | if (m_slices.contains(s)) // calculate layout only for those slices that are already visible |
|
142 | if (m_slices.contains(s)) // calculate layout only for those slices that are already visible | |
144 | layout.insert(s, sliceData(s)); |
|
143 | layout.insert(s, sliceData(s)); | |
145 | } |
|
144 | } | |
146 | return layout; |
|
145 | return layout; | |
147 | } |
|
146 | } | |
148 |
|
147 | |||
149 | void PieChartItem::applyLayout(const PieLayout &layout) |
|
148 | void PieChartItem::applyLayout(const PieLayout &layout) | |
150 | { |
|
149 | { | |
151 |
if ( |
|
150 | if (animator()) | |
152 |
|
|
151 | animator()->updateLayout(this, layout); | |
153 | else |
|
152 | else | |
154 | setLayout(layout); |
|
153 | setLayout(layout); | |
155 | } |
|
154 | } | |
156 |
|
155 | |||
157 | void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData) |
|
156 | void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData) | |
158 | { |
|
157 | { | |
159 | if (m_animator) |
|
158 | if (m_animator) | |
160 | m_animator->updateLayout(this, slice, sliceData); |
|
159 | m_animator->updateLayout(this, slice, sliceData); | |
161 | else |
|
160 | else | |
162 | setLayout(slice, sliceData); |
|
161 | setLayout(slice, sliceData); | |
163 | } |
|
162 | } | |
164 |
|
163 | |||
165 | void PieChartItem::setLayout(const PieLayout &layout) |
|
164 | void PieChartItem::setLayout(const PieLayout &layout) | |
166 | { |
|
165 | { | |
167 | foreach (QPieSlice *slice, layout.keys()) { |
|
166 | foreach (QPieSlice *slice, layout.keys()) { | |
168 | PieSliceItem *item = m_slices.value(slice); |
|
167 | PieSliceItem *item = m_slices.value(slice); | |
169 | Q_ASSERT(item); |
|
168 | Q_ASSERT(item); | |
170 | item->setSliceData(layout.value(slice)); |
|
169 | item->setSliceData(layout.value(slice)); | |
171 | item->updateGeometry(); |
|
170 | item->updateGeometry(); | |
172 | item->update(); |
|
171 | item->update(); | |
173 | } |
|
172 | } | |
174 | } |
|
173 | } | |
175 |
|
174 | |||
176 | void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData) |
|
175 | void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData) | |
177 | { |
|
176 | { | |
178 | // find slice |
|
177 | // find slice | |
179 | PieSliceItem *item = m_slices.value(slice); |
|
178 | PieSliceItem *item = m_slices.value(slice); | |
180 | Q_ASSERT(item); |
|
179 | Q_ASSERT(item); | |
181 | item->setSliceData(sliceData); |
|
180 | item->setSliceData(sliceData); | |
182 | item->updateGeometry(); |
|
181 | item->updateGeometry(); | |
183 | item->update(); |
|
182 | item->update(); | |
184 | } |
|
183 | } | |
185 |
|
184 | |||
186 | void PieChartItem::destroySlice(QPieSlice *slice) |
|
185 | void PieChartItem::destroySlice(QPieSlice *slice) | |
187 | { |
|
186 | { | |
188 | delete m_slices.take(slice); |
|
187 | delete m_slices.take(slice); | |
189 | } |
|
188 | } | |
190 |
|
189 | |||
191 | #include "moc_piechartitem_p.cpp" |
|
190 | #include "moc_piechartitem_p.cpp" | |
192 |
|
191 | |||
193 | QTCOMMERCIALCHART_END_NAMESPACE |
|
192 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,59 +1,58 | |||||
1 | #ifndef PIECHARTITEM_H |
|
1 | #ifndef PIECHARTITEM_H | |
2 | #define PIECHARTITEM_H |
|
2 | #define PIECHARTITEM_H | |
3 |
|
3 | |||
4 | #include "qpieseries.h" |
|
4 | #include "qpieseries.h" | |
5 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
6 | #include "piesliceitem_p.h" |
|
6 | #include "piesliceitem_p.h" | |
7 |
|
7 | |||
8 | class QGraphicsItem; |
|
8 | class QGraphicsItem; | |
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 | class QPieSlice; |
|
10 | class QPieSlice; | |
11 | class ChartPresenter; |
|
11 | class ChartPresenter; | |
12 |
|
12 | |||
13 | typedef QHash<QPieSlice*, PieSliceData> PieLayout; |
|
13 | typedef QHash<QPieSlice*, PieSliceData> PieLayout; | |
14 |
|
14 | |||
15 |
class PieChartItem : public |
|
15 | class PieChartItem : public ChartItem | |
16 | { |
|
16 | { | |
17 | Q_OBJECT |
|
17 | Q_OBJECT | |
18 |
|
18 | |||
19 | public: |
|
19 | public: | |
20 | // TODO: use a generic data class instead of x and y |
|
20 | // TODO: use a generic data class instead of x and y | |
21 |
PieChartItem(QPieSeries *series, ChartPresenter *presenter |
|
21 | PieChartItem(QPieSeries *series, ChartPresenter *presenter); | |
22 | ~PieChartItem(); |
|
22 | ~PieChartItem(); | |
23 |
|
23 | |||
24 | public: // from QGraphicsItem |
|
24 | public: // from QGraphicsItem | |
25 | QRectF boundingRect() const { return m_rect; } |
|
25 | QRectF boundingRect() const { return m_rect; } | |
26 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); |
|
26 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); | |
27 |
|
27 | |||
28 | public Q_SLOTS: |
|
28 | public Q_SLOTS: | |
29 | void initialize(); |
|
29 | void initialize(); | |
30 | void handleSlicesAdded(QList<QPieSlice*> slices); |
|
30 | void handleSlicesAdded(QList<QPieSlice*> slices); | |
31 | void handleSlicesRemoved(QList<QPieSlice*> slices); |
|
31 | void handleSlicesRemoved(QList<QPieSlice*> slices); | |
32 | void handlePieLayoutChanged(); |
|
32 | void handlePieLayoutChanged(); | |
33 | void handleSliceChanged(); |
|
33 | void handleSliceChanged(); | |
34 | void handleDomainChanged(qreal, qreal, qreal, qreal); |
|
34 | void handleDomainChanged(qreal, qreal, qreal, qreal); | |
35 | void handleGeometryChanged(const QRectF& rect); |
|
35 | void handleGeometryChanged(const QRectF& rect); | |
36 |
|
36 | |||
37 | public: |
|
37 | public: | |
38 | void calculatePieLayout(); |
|
38 | void calculatePieLayout(); | |
39 | PieSliceData sliceData(QPieSlice *slice); |
|
39 | PieSliceData sliceData(QPieSlice *slice); | |
40 | PieLayout calculateLayout(); |
|
40 | PieLayout calculateLayout(); | |
41 | void applyLayout(const PieLayout &layout); |
|
41 | void applyLayout(const PieLayout &layout); | |
42 | void updateLayout(QPieSlice *slice, const PieSliceData &sliceData); |
|
42 | void updateLayout(QPieSlice *slice, const PieSliceData &sliceData); | |
43 | void setLayout(const PieLayout &layout); |
|
43 | void setLayout(const PieLayout &layout); | |
44 | void setLayout(QPieSlice *slice, const PieSliceData &sliceData); |
|
44 | void setLayout(QPieSlice *slice, const PieSliceData &sliceData); | |
45 | void destroySlice(QPieSlice *slice); |
|
45 | void destroySlice(QPieSlice *slice); | |
46 |
|
46 | |||
47 | private: |
|
47 | private: | |
48 | friend class PieSliceItem; |
|
48 | friend class PieSliceItem; | |
49 | QHash<QPieSlice*, PieSliceItem*> m_slices; |
|
49 | QHash<QPieSlice*, PieSliceItem*> m_slices; | |
50 | QPieSeries *m_series; |
|
50 | QPieSeries *m_series; | |
51 | QRectF m_rect; |
|
51 | QRectF m_rect; | |
52 | QPointF m_pieCenter; |
|
52 | QPointF m_pieCenter; | |
53 | qreal m_pieRadius; |
|
53 | qreal m_pieRadius; | |
54 | ChartPresenter *m_presenter; |
|
|||
55 | }; |
|
54 | }; | |
56 |
|
55 | |||
57 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | QTCOMMERCIALCHART_END_NAMESPACE | |
58 |
|
57 | |||
59 | #endif // PIECHARTITEM_H |
|
58 | #endif // PIECHARTITEM_H |
@@ -1,124 +1,124 | |||||
1 | #ifndef QCHART_H |
|
1 | #ifndef QCHART_H | |
2 | #define QCHART_H |
|
2 | #define QCHART_H | |
3 |
|
3 | |||
4 | #include <qchartglobal.h> |
|
4 | #include <qchartglobal.h> | |
5 | #include <qseries.h> |
|
5 | #include <qseries.h> | |
6 | #include <QGraphicsWidget> |
|
6 | #include <QGraphicsWidget> | |
7 | #include <QLinearGradient> |
|
7 | #include <QLinearGradient> | |
8 | #include <QFont> |
|
8 | #include <QFont> | |
9 |
|
9 | |||
10 | class QGraphicsSceneResizeEvent; |
|
10 | class QGraphicsSceneResizeEvent; | |
11 |
|
11 | |||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 |
|
13 | |||
14 |
class Axis |
|
14 | class Axis; | |
15 | class QSeries; |
|
15 | class QSeries; | |
16 | class PlotDomain; |
|
16 | class PlotDomain; | |
17 | class BarChartItem; |
|
17 | class BarChartItem; | |
18 | class QChartAxis; |
|
18 | class QChartAxis; | |
19 | class ChartTheme; |
|
19 | class ChartTheme; | |
20 | class ChartItem; |
|
20 | class ChartItem; | |
21 | class ChartDataSet; |
|
21 | class ChartDataSet; | |
22 | class ChartPresenter; |
|
22 | class ChartPresenter; | |
23 | class QLegend; |
|
23 | class QLegend; | |
24 | class ChartBackground; |
|
24 | class ChartBackground; | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
27 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget | |
28 | { |
|
28 | { | |
29 | Q_OBJECT |
|
29 | Q_OBJECT | |
30 | public: |
|
30 | public: | |
31 | enum ChartTheme { |
|
31 | enum ChartTheme { | |
32 | ChartThemeDefault, |
|
32 | ChartThemeDefault, | |
33 | ChartThemeLight, |
|
33 | ChartThemeLight, | |
34 | ChartThemeBlueCerulean, |
|
34 | ChartThemeBlueCerulean, | |
35 | ChartThemeDark, |
|
35 | ChartThemeDark, | |
36 | ChartThemeBrownSand, |
|
36 | ChartThemeBrownSand, | |
37 | ChartThemeBlueNcs, |
|
37 | ChartThemeBlueNcs, | |
38 | ChartThemeVanilla, |
|
38 | ChartThemeVanilla, | |
39 | ChartThemeIcy, |
|
39 | ChartThemeIcy, | |
40 | ChartThemeGrayscale, |
|
40 | ChartThemeGrayscale, | |
41 | ChartThemeScientific, |
|
41 | ChartThemeScientific, | |
42 | ChartThemeCount |
|
42 | ChartThemeCount | |
43 | }; |
|
43 | }; | |
44 |
|
44 | |||
45 | enum AnimationOption { |
|
45 | enum AnimationOption { | |
46 | NoAnimation = 0x0, |
|
46 | NoAnimation = 0x0, | |
47 | GridAxisAnimations = 0x1, |
|
47 | GridAxisAnimations = 0x1, | |
48 | SeriesAnimations =0x2, |
|
48 | SeriesAnimations =0x2, | |
49 | AllAnimations = 0x3 |
|
49 | AllAnimations = 0x3 | |
50 | }; |
|
50 | }; | |
51 | Q_DECLARE_FLAGS(AnimationOptions, AnimationOption) |
|
51 | Q_DECLARE_FLAGS(AnimationOptions, AnimationOption) | |
52 |
|
52 | |||
53 | public: |
|
53 | public: | |
54 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); |
|
54 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); | |
55 | ~QChart(); |
|
55 | ~QChart(); | |
56 |
|
56 | |||
57 | void addSeries(QSeries* series, QChartAxis* axisY = 0); |
|
57 | void addSeries(QSeries* series, QChartAxis* axisY = 0); | |
58 | void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached |
|
58 | void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached | |
59 | void removeAllSeries(); // deletes series and axis |
|
59 | void removeAllSeries(); // deletes series and axis | |
60 |
|
60 | |||
61 | void setChartTheme(QChart::ChartTheme theme); |
|
61 | void setChartTheme(QChart::ChartTheme theme); | |
62 | QChart::ChartTheme chartTheme() const; |
|
62 | QChart::ChartTheme chartTheme() const; | |
63 |
|
63 | |||
64 | void setTitle(const QString& title); |
|
64 | void setTitle(const QString& title); | |
65 | QString title() const; |
|
65 | QString title() const; | |
66 | void setTitleFont(const QFont& font); |
|
66 | void setTitleFont(const QFont& font); | |
67 | QFont titleFont() const; |
|
67 | QFont titleFont() const; | |
68 | void setTitleBrush(const QBrush &brush); |
|
68 | void setTitleBrush(const QBrush &brush); | |
69 | QBrush titleBrush() const; |
|
69 | QBrush titleBrush() const; | |
70 | void setBackgroundBrush(const QBrush& brush); |
|
70 | void setBackgroundBrush(const QBrush& brush); | |
71 | QBrush backgroundBrush() const; |
|
71 | QBrush backgroundBrush() const; | |
72 | void setBackgroundPen(const QPen& pen); |
|
72 | void setBackgroundPen(const QPen& pen); | |
73 | QPen backgroundPen() const; |
|
73 | QPen backgroundPen() const; | |
74 |
|
74 | |||
75 | void setBackgroundVisible(bool visible); |
|
75 | void setBackgroundVisible(bool visible); | |
76 | bool isBackgroundVisible() const; |
|
76 | bool isBackgroundVisible() const; | |
77 |
|
77 | |||
78 | void setAnimationOptions(AnimationOptions options); |
|
78 | void setAnimationOptions(AnimationOptions options); | |
79 | AnimationOptions animationOptions() const; |
|
79 | AnimationOptions animationOptions() const; | |
80 |
|
80 | |||
81 | void zoomIn(); |
|
81 | void zoomIn(); | |
82 | void zoomIn(const QRectF& rect); |
|
82 | void zoomIn(const QRectF& rect); | |
83 | void zoomOut(); |
|
83 | void zoomOut(); | |
84 | void zoomReset(); |
|
84 | void zoomReset(); | |
85 | void scrollLeft(); |
|
85 | void scrollLeft(); | |
86 | void scrollRight(); |
|
86 | void scrollRight(); | |
87 | void scrollUp(); |
|
87 | void scrollUp(); | |
88 | void scrollDown(); |
|
88 | void scrollDown(); | |
89 |
|
89 | |||
90 | QChartAxis* axisX() const; |
|
90 | QChartAxis* axisX() const; | |
91 | QChartAxis* axisY() const; |
|
91 | QChartAxis* axisY() const; | |
92 |
|
92 | |||
93 | QLegend* legend() const; |
|
93 | QLegend* legend() const; | |
94 |
|
94 | |||
95 | int padding() const; |
|
95 | int padding() const; | |
96 |
|
96 | |||
97 | protected: |
|
97 | protected: | |
98 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
98 | void resizeEvent(QGraphicsSceneResizeEvent *event); | |
99 |
|
99 | |||
100 | private: |
|
100 | private: | |
101 | inline void createChartBackgroundItem(); |
|
101 | inline void createChartBackgroundItem(); | |
102 | inline void createChartTitleItem(); |
|
102 | inline void createChartTitleItem(); | |
103 | void setPadding(int padding); |
|
103 | void setPadding(int padding); | |
104 | void setBackgroundPadding(int padding); |
|
104 | void setBackgroundPadding(int padding); | |
105 | void setBackgroundDiameter(int diameter); |
|
105 | void setBackgroundDiameter(int diameter); | |
106 | void updateLayout(); |
|
106 | void updateLayout(); | |
107 |
|
107 | |||
108 | private: |
|
108 | private: | |
109 | Q_DISABLE_COPY(QChart) |
|
109 | Q_DISABLE_COPY(QChart) | |
110 | ChartBackground* m_backgroundItem; |
|
110 | ChartBackground* m_backgroundItem; | |
111 | QGraphicsSimpleTextItem* m_titleItem; |
|
111 | QGraphicsSimpleTextItem* m_titleItem; | |
112 | QRectF m_rect; |
|
112 | QRectF m_rect; | |
113 | QLegend* m_legend; |
|
113 | QLegend* m_legend; | |
114 | ChartDataSet *m_dataset; |
|
114 | ChartDataSet *m_dataset; | |
115 | ChartPresenter *m_presenter; |
|
115 | ChartPresenter *m_presenter; | |
116 | int m_padding; |
|
116 | int m_padding; | |
117 | int m_backgroundPadding; |
|
117 | int m_backgroundPadding; | |
118 | }; |
|
118 | }; | |
119 |
|
119 | |||
120 | QTCOMMERCIALCHART_END_NAMESPACE |
|
120 | QTCOMMERCIALCHART_END_NAMESPACE | |
121 |
|
121 | |||
122 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions) |
|
122 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions) | |
123 |
|
123 | |||
124 | #endif |
|
124 | #endif |
@@ -1,173 +1,170 | |||||
1 | #include "scatterchartitem_p.h" |
|
1 | #include "scatterchartitem_p.h" | |
2 | #include "qscatterseries.h" |
|
2 | #include "qscatterseries.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 | #include <QGraphicsScene> |
|
5 | #include <QGraphicsScene> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
ScatterChartItem::ScatterChartItem(QScatterSeries *series, |
|
9 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) : | |
10 |
XYChartItem(series,p |
|
10 | XYChartItem(series,presenter), | |
11 | m_series(series), |
|
11 | m_series(series), | |
12 | m_items(this), |
|
12 | m_items(this), | |
13 | m_shape(QScatterSeries::MarkerShapeRectangle), |
|
13 | m_shape(QScatterSeries::MarkerShapeRectangle), | |
14 | m_size(15) |
|
14 | m_size(15) | |
15 |
|
15 | |||
16 | { |
|
16 | { | |
17 | Q_ASSERT(parent); |
|
|||
18 | Q_ASSERT(series); |
|
|||
19 |
|
||||
20 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
17 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); | |
21 |
|
18 | |||
22 | setZValue(ChartPresenter::ScatterSeriesZValue); |
|
19 | setZValue(ChartPresenter::ScatterSeriesZValue); | |
23 | setFlags(QGraphicsItem::ItemHasNoContents); |
|
20 | setFlags(QGraphicsItem::ItemHasNoContents); | |
24 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
21 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | |
25 |
|
22 | |||
26 | handleUpdated(); |
|
23 | handleUpdated(); | |
27 |
|
24 | |||
28 | m_items.setHandlesChildEvents(false); |
|
25 | m_items.setHandlesChildEvents(false); | |
29 |
|
26 | |||
30 | // TODO: how to draw a drop shadow? |
|
27 | // TODO: how to draw a drop shadow? | |
31 | // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); |
|
28 | // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); | |
32 | // dropShadow->setOffset(2.0); |
|
29 | // dropShadow->setOffset(2.0); | |
33 | // dropShadow->setBlurRadius(2.0); |
|
30 | // dropShadow->setBlurRadius(2.0); | |
34 | // setGraphicsEffect(dropShadow); |
|
31 | // setGraphicsEffect(dropShadow); | |
35 | } |
|
32 | } | |
36 |
|
33 | |||
37 |
|
34 | |||
38 | QRectF ScatterChartItem::boundingRect() const |
|
35 | QRectF ScatterChartItem::boundingRect() const | |
39 | { |
|
36 | { | |
40 | return m_rect; |
|
37 | return m_rect; | |
41 | } |
|
38 | } | |
42 |
|
39 | |||
43 | void ScatterChartItem::createPoints(int count) |
|
40 | void ScatterChartItem::createPoints(int count) | |
44 | { |
|
41 | { | |
45 | for (int i = 0; i < count; ++i) { |
|
42 | for (int i = 0; i < count; ++i) { | |
46 |
|
43 | |||
47 | QGraphicsItem *item; |
|
44 | QGraphicsItem *item; | |
48 |
|
45 | |||
49 | switch (m_shape) { |
|
46 | switch (m_shape) { | |
50 | case QScatterSeries::MarkerShapeCircle:{ |
|
47 | case QScatterSeries::MarkerShapeCircle:{ | |
51 | QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); |
|
48 | QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); | |
52 | const QRectF& rect = i->boundingRect(); |
|
49 | const QRectF& rect = i->boundingRect(); | |
53 | i->setPos(-rect.width()/2,-rect.height()/2); |
|
50 | i->setPos(-rect.width()/2,-rect.height()/2); | |
54 | item = new Marker(i,this); |
|
51 | item = new Marker(i,this); | |
55 | break; |
|
52 | break; | |
56 | } |
|
53 | } | |
57 | case QScatterSeries::MarkerShapeRectangle:{ |
|
54 | case QScatterSeries::MarkerShapeRectangle:{ | |
58 | QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); |
|
55 | QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); | |
59 | i->setPos(-m_size/2,-m_size/2); |
|
56 | i->setPos(-m_size/2,-m_size/2); | |
60 | item = new Marker(i,this); |
|
57 | item = new Marker(i,this); | |
61 | break; |
|
58 | break; | |
62 | } |
|
59 | } | |
63 | default: |
|
60 | default: | |
64 | qWarning()<<"Unsupported marker type"; |
|
61 | qWarning()<<"Unsupported marker type"; | |
65 | break; |
|
62 | break; | |
66 |
|
63 | |||
67 | } |
|
64 | } | |
68 | m_items.addToGroup(item); |
|
65 | m_items.addToGroup(item); | |
69 | } |
|
66 | } | |
70 | } |
|
67 | } | |
71 |
|
68 | |||
72 | void ScatterChartItem::deletePoints(int count) |
|
69 | void ScatterChartItem::deletePoints(int count) | |
73 | { |
|
70 | { | |
74 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
71 | QList<QGraphicsItem *> items = m_items.childItems(); | |
75 |
|
72 | |||
76 | for (int i = 0; i < count; ++i) { |
|
73 | for (int i = 0; i < count; ++i) { | |
77 | delete(items.takeLast()); |
|
74 | delete(items.takeLast()); | |
78 | } |
|
75 | } | |
79 | } |
|
76 | } | |
80 |
|
77 | |||
81 | void ScatterChartItem::markerSelected(Marker* marker) |
|
78 | void ScatterChartItem::markerSelected(Marker* marker) | |
82 | { |
|
79 | { | |
83 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); |
|
80 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); | |
84 | } |
|
81 | } | |
85 |
|
82 | |||
86 | void ScatterChartItem::setLayout(QVector<QPointF>& points) |
|
83 | void ScatterChartItem::setLayout(QVector<QPointF>& points) | |
87 | { |
|
84 | { | |
88 | if(points.size()==0) |
|
85 | if(points.size()==0) | |
89 | { |
|
86 | { | |
90 | XYChartItem::setLayout(points); |
|
87 | XYChartItem::setLayout(points); | |
91 | return; |
|
88 | return; | |
92 | } |
|
89 | } | |
93 |
|
90 | |||
94 | int diff = XYChartItem::points().size() - points.size(); |
|
91 | int diff = XYChartItem::points().size() - points.size(); | |
95 |
|
92 | |||
96 | if(diff>0) { |
|
93 | if(diff>0) { | |
97 | deletePoints(diff); |
|
94 | deletePoints(diff); | |
98 | } |
|
95 | } | |
99 | else if(diff<0) { |
|
96 | else if(diff<0) { | |
100 | createPoints(-diff); |
|
97 | createPoints(-diff); | |
101 | } |
|
98 | } | |
102 |
|
99 | |||
103 | if(diff!=0) handleUpdated(); |
|
100 | if(diff!=0) handleUpdated(); | |
104 |
|
101 | |||
105 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
102 | QList<QGraphicsItem*> items = m_items.childItems(); | |
106 |
|
103 | |||
107 | for(int i=0; i< points.size();i++) { |
|
104 | for(int i=0; i< points.size();i++) { | |
108 | Marker* item = static_cast<Marker*>(items.at(i)); |
|
105 | Marker* item = static_cast<Marker*>(items.at(i)); | |
109 | const QPointF& point = points.at(i); |
|
106 | const QPointF& point = points.at(i); | |
110 | const QRectF& rect = item->boundingRect(); |
|
107 | const QRectF& rect = item->boundingRect(); | |
111 | item->setIndex(i); |
|
108 | item->setIndex(i); | |
112 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); |
|
109 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); | |
113 | if(!clipRect().contains(point)) { |
|
110 | if(!clipRect().contains(point)) { | |
114 | item->setVisible(false); |
|
111 | item->setVisible(false); | |
115 | } |
|
112 | } | |
116 | else { |
|
113 | else { | |
117 | item->setVisible(true); |
|
114 | item->setVisible(true); | |
118 | } |
|
115 | } | |
119 | } |
|
116 | } | |
120 |
|
117 | |||
121 | prepareGeometryChange(); |
|
118 | prepareGeometryChange(); | |
122 | m_rect = clipRect(); |
|
119 | m_rect = clipRect(); | |
123 | XYChartItem::setLayout(points); |
|
120 | XYChartItem::setLayout(points); | |
124 | } |
|
121 | } | |
125 |
|
122 | |||
126 |
|
123 | |||
127 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
124 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
128 | { |
|
125 | { | |
129 | Q_UNUSED(painter) |
|
126 | Q_UNUSED(painter) | |
130 | Q_UNUSED(option) |
|
127 | Q_UNUSED(option) | |
131 | Q_UNUSED(widget) |
|
128 | Q_UNUSED(widget) | |
132 | } |
|
129 | } | |
133 |
|
130 | |||
134 | void ScatterChartItem::setPen(const QPen& pen) |
|
131 | void ScatterChartItem::setPen(const QPen& pen) | |
135 | { |
|
132 | { | |
136 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
133 | foreach(QGraphicsItem* item , m_items.childItems()) { | |
137 | static_cast<Marker*>(item)->setPen(pen); |
|
134 | static_cast<Marker*>(item)->setPen(pen); | |
138 | } |
|
135 | } | |
139 | } |
|
136 | } | |
140 |
|
137 | |||
141 | void ScatterChartItem::setBrush(const QBrush& brush) |
|
138 | void ScatterChartItem::setBrush(const QBrush& brush) | |
142 | { |
|
139 | { | |
143 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
140 | foreach(QGraphicsItem* item , m_items.childItems()) { | |
144 | static_cast<Marker*>(item)->setBrush(brush); |
|
141 | static_cast<Marker*>(item)->setBrush(brush); | |
145 | } |
|
142 | } | |
146 | } |
|
143 | } | |
147 |
|
144 | |||
148 | void ScatterChartItem::handleUpdated() |
|
145 | void ScatterChartItem::handleUpdated() | |
149 | { |
|
146 | { | |
150 |
|
147 | |||
151 | int count = m_items.childItems().count(); |
|
148 | int count = m_items.childItems().count(); | |
152 |
|
149 | |||
153 | if(count==0) return; |
|
150 | if(count==0) return; | |
154 |
|
151 | |||
155 | bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); |
|
152 | bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); | |
156 |
|
153 | |||
157 | //TODO: only rewrite on size change |
|
154 | //TODO: only rewrite on size change | |
158 |
|
155 | |||
159 | m_size = m_series->size(); |
|
156 | m_size = m_series->size(); | |
160 | m_shape = m_series->shape(); |
|
157 | m_shape = m_series->shape(); | |
161 |
|
158 | |||
162 | if(recreate){ |
|
159 | if(recreate){ | |
163 | deletePoints(count); |
|
160 | deletePoints(count); | |
164 | createPoints(count); |
|
161 | createPoints(count); | |
165 | } |
|
162 | } | |
166 |
|
163 | |||
167 | setPen(m_series->pen()); |
|
164 | setPen(m_series->pen()); | |
168 | setBrush(m_series->brush()); |
|
165 | setBrush(m_series->brush()); | |
169 | } |
|
166 | } | |
170 |
|
167 | |||
171 | #include "moc_scatterchartitem_p.cpp" |
|
168 | #include "moc_scatterchartitem_p.cpp" | |
172 |
|
169 | |||
173 | QTCOMMERCIALCHART_END_NAMESPACE |
|
170 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,120 +1,120 | |||||
1 | #ifndef SCATTERPRESENTER_H |
|
1 | #ifndef SCATTERPRESENTER_H | |
2 | #define SCATTERPRESENTER_H |
|
2 | #define SCATTERPRESENTER_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "xychartitem_p.h" |
|
5 | #include "xychartitem_p.h" | |
6 | #include <QGraphicsEllipseItem> |
|
6 | #include <QGraphicsEllipseItem> | |
7 | #include <QPen> |
|
7 | #include <QPen> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class QScatterSeries; |
|
11 | class QScatterSeries; | |
12 | class Marker; |
|
12 | class Marker; | |
13 |
|
13 | |||
14 | class ScatterChartItem : public XYChartItem |
|
14 | class ScatterChartItem : public XYChartItem | |
15 | { |
|
15 | { | |
16 | Q_OBJECT |
|
16 | Q_OBJECT | |
17 | public: |
|
17 | public: | |
18 |
explicit ScatterChartItem(QScatterSeries *series, |
|
18 | explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter); | |
19 |
|
19 | |||
20 | public: |
|
20 | public: | |
21 | //from QGraphicsItem |
|
21 | //from QGraphicsItem | |
22 | QRectF boundingRect() const; |
|
22 | QRectF boundingRect() const; | |
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
24 |
|
24 | |||
25 | void setPen(const QPen& pen); |
|
25 | void setPen(const QPen& pen); | |
26 | void setBrush(const QBrush& brush); |
|
26 | void setBrush(const QBrush& brush); | |
27 |
|
27 | |||
28 | void markerSelected(Marker* item); |
|
28 | void markerSelected(Marker* item); | |
29 |
|
29 | |||
30 | public slots: |
|
30 | public slots: | |
31 | void handleUpdated(); |
|
31 | void handleUpdated(); | |
32 |
|
32 | |||
33 | private: |
|
33 | private: | |
34 | void createPoints(int count); |
|
34 | void createPoints(int count); | |
35 | void deletePoints(int count); |
|
35 | void deletePoints(int count); | |
36 |
|
36 | |||
37 | protected: |
|
37 | protected: | |
38 | void setLayout(QVector<QPointF>& points); |
|
38 | void setLayout(QVector<QPointF>& points); | |
39 |
|
39 | |||
40 | private: |
|
40 | private: | |
41 | QScatterSeries *m_series; |
|
41 | QScatterSeries *m_series; | |
42 | QGraphicsItemGroup m_items; |
|
42 | QGraphicsItemGroup m_items; | |
43 | int m_shape; |
|
43 | int m_shape; | |
44 | int m_size; |
|
44 | int m_size; | |
45 | QRectF m_rect; |
|
45 | QRectF m_rect; | |
46 |
|
46 | |||
47 | }; |
|
47 | }; | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | class Marker: public QAbstractGraphicsShapeItem |
|
50 | class Marker: public QAbstractGraphicsShapeItem | |
51 | { |
|
51 | { | |
52 |
|
52 | |||
53 | public: |
|
53 | public: | |
54 |
|
54 | |||
55 | Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent) |
|
55 | Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent) | |
56 | { |
|
56 | { | |
57 | }; |
|
57 | }; | |
58 |
|
58 | |||
59 | ~Marker() |
|
59 | ~Marker() | |
60 | { |
|
60 | { | |
61 | delete m_item; |
|
61 | delete m_item; | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | void setIndex(int index) |
|
64 | void setIndex(int index) | |
65 | { |
|
65 | { | |
66 | m_index=index; |
|
66 | m_index=index; | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | int index() const |
|
69 | int index() const | |
70 | { |
|
70 | { | |
71 | return m_index; |
|
71 | return m_index; | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | QPainterPath shape() const |
|
74 | QPainterPath shape() const | |
75 | { |
|
75 | { | |
76 | return m_item->shape(); |
|
76 | return m_item->shape(); | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | QRectF boundingRect() const |
|
79 | QRectF boundingRect() const | |
80 | { |
|
80 | { | |
81 | return m_item->boundingRect(); |
|
81 | return m_item->boundingRect(); | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | bool contains(const QPointF &point) const |
|
84 | bool contains(const QPointF &point) const | |
85 | { |
|
85 | { | |
86 | return m_item->contains(point); |
|
86 | return m_item->contains(point); | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void setPen(const QPen& pen) |
|
89 | void setPen(const QPen& pen) | |
90 | { |
|
90 | { | |
91 | m_item->setPen(pen); |
|
91 | m_item->setPen(pen); | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | void setBrush(const QBrush& brush) |
|
94 | void setBrush(const QBrush& brush) | |
95 | { |
|
95 | { | |
96 | m_item->setBrush(brush); |
|
96 | m_item->setBrush(brush); | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
99 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
100 | { |
|
100 | { | |
101 | m_item->paint(painter,option,widget); |
|
101 | m_item->paint(painter,option,widget); | |
102 | } |
|
102 | } | |
103 |
|
103 | |||
104 | protected: |
|
104 | protected: | |
105 |
|
105 | |||
106 | void mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
106 | void mousePressEvent(QGraphicsSceneMouseEvent *event) | |
107 | { |
|
107 | { | |
108 | Q_UNUSED(event) |
|
108 | Q_UNUSED(event) | |
109 | m_parent->markerSelected(this); |
|
109 | m_parent->markerSelected(this); | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 | private: |
|
112 | private: | |
113 | QAbstractGraphicsShapeItem* m_item; |
|
113 | QAbstractGraphicsShapeItem* m_item; | |
114 | ScatterChartItem* m_parent; |
|
114 | ScatterChartItem* m_parent; | |
115 | int m_index; |
|
115 | int m_index; | |
116 | }; |
|
116 | }; | |
117 |
|
117 | |||
118 | QTCOMMERCIALCHART_END_NAMESPACE |
|
118 | QTCOMMERCIALCHART_END_NAMESPACE | |
119 |
|
119 | |||
120 | #endif // SCATTERPRESENTER_H |
|
120 | #endif // SCATTERPRESENTER_H |
@@ -1,116 +1,116 | |||||
1 | #include "splinechartitem_p.h" |
|
1 | #include "splinechartitem_p.h" | |
2 | #include "chartpresenter_p.h" |
|
2 | #include "chartpresenter_p.h" | |
3 | #include "chartanimator_p.h" |
|
3 | #include "chartanimator_p.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 |
SplineChartItem::SplineChartItem(QSplineSeries* series, |
|
8 | SplineChartItem::SplineChartItem(QSplineSeries* series, ChartPresenter *presenter) : | |
9 |
XYChartItem(series, p |
|
9 | XYChartItem(series, presenter), | |
10 | m_series(series), |
|
10 | m_series(series), | |
11 | m_pointsVisible(false) |
|
11 | m_pointsVisible(false) | |
12 | { |
|
12 | { | |
13 | setZValue(ChartPresenter::LineChartZValue); |
|
13 | setZValue(ChartPresenter::LineChartZValue); | |
14 | QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
14 | QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); | |
15 | handleUpdated(); |
|
15 | handleUpdated(); | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | QRectF SplineChartItem::boundingRect() const |
|
18 | QRectF SplineChartItem::boundingRect() const | |
19 | { |
|
19 | { | |
20 | return m_rect; |
|
20 | return m_rect; | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 | QPainterPath SplineChartItem::shape() const |
|
23 | QPainterPath SplineChartItem::shape() const | |
24 | { |
|
24 | { | |
25 | return m_path; |
|
25 | return m_path; | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | void SplineChartItem::updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index) |
|
28 | void SplineChartItem::updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index) | |
29 | { |
|
29 | { | |
30 | QVector<QPointF> controlPoints; |
|
30 | QVector<QPointF> controlPoints; | |
31 |
|
31 | |||
32 | controlPoints.resize(newPoints.count()*2-2); |
|
32 | controlPoints.resize(newPoints.count()*2-2); | |
33 |
|
33 | |||
34 | for (int i = 0; i < newPoints.size() - 1; i++) |
|
34 | for (int i = 0; i < newPoints.size() - 1; i++) | |
35 | { |
|
35 | { | |
36 | controlPoints[2*i] = calculateGeometryControlPoint(2 * i); |
|
36 | controlPoints[2*i] = calculateGeometryControlPoint(2 * i); | |
37 | controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); |
|
37 | controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | if(controlPoints.count()<2) { |
|
40 | if(controlPoints.count()<2) { | |
41 | setLayout(newPoints,controlPoints); |
|
41 | setLayout(newPoints,controlPoints); | |
42 | return; |
|
42 | return; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 |
if( |
|
45 | if(animator()){ | |
46 |
|
|
46 | animator()->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); | |
47 | }else{ |
|
47 | }else{ | |
48 | setLayout(newPoints,controlPoints); |
|
48 | setLayout(newPoints,controlPoints); | |
49 | } |
|
49 | } | |
50 | } |
|
50 | } | |
51 |
|
51 | |||
52 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const |
|
52 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const | |
53 | { |
|
53 | { | |
54 | return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); |
|
54 | return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | void SplineChartItem::setLayout(QVector<QPointF>& points,QVector<QPointF>& controlPoints) |
|
57 | void SplineChartItem::setLayout(QVector<QPointF>& points,QVector<QPointF>& controlPoints) | |
58 | { |
|
58 | { | |
59 | if(points.size()<2 || controlPoints.size()<2) |
|
59 | if(points.size()<2 || controlPoints.size()<2) | |
60 | { |
|
60 | { | |
61 | XYChartItem::setLayout(points); |
|
61 | XYChartItem::setLayout(points); | |
62 | m_controlPoints=controlPoints; |
|
62 | m_controlPoints=controlPoints; | |
63 | return; |
|
63 | return; | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | Q_ASSERT(points.count()*2-2 == controlPoints.count()); |
|
66 | Q_ASSERT(points.count()*2-2 == controlPoints.count()); | |
67 |
|
67 | |||
68 | QPainterPath splinePath(points.at(0)); |
|
68 | QPainterPath splinePath(points.at(0)); | |
69 |
|
69 | |||
70 | for (int i = 0; i < points.size() - 1; i++) |
|
70 | for (int i = 0; i < points.size() - 1; i++) | |
71 | { |
|
71 | { | |
72 | const QPointF& point = points.at(i + 1); |
|
72 | const QPointF& point = points.at(i + 1); | |
73 | splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); |
|
73 | splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | prepareGeometryChange(); |
|
76 | prepareGeometryChange(); | |
77 | m_path = splinePath; |
|
77 | m_path = splinePath; | |
78 | m_rect = splinePath.boundingRect(); |
|
78 | m_rect = splinePath.boundingRect(); | |
79 | XYChartItem::setLayout(points); |
|
79 | XYChartItem::setLayout(points); | |
80 | m_controlPoints=controlPoints; |
|
80 | m_controlPoints=controlPoints; | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | //handlers |
|
83 | //handlers | |
84 |
|
84 | |||
85 | void SplineChartItem::handleUpdated() |
|
85 | void SplineChartItem::handleUpdated() | |
86 | { |
|
86 | { | |
87 | m_pointsVisible = m_series->pointsVisible(); |
|
87 | m_pointsVisible = m_series->pointsVisible(); | |
88 | m_linePen = m_series->pen(); |
|
88 | m_linePen = m_series->pen(); | |
89 | m_pointPen = m_series->pen(); |
|
89 | m_pointPen = m_series->pen(); | |
90 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
90 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
91 | update(); |
|
91 | update(); | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | //painter |
|
94 | //painter | |
95 |
|
95 | |||
96 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
96 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
97 | { |
|
97 | { | |
98 | Q_UNUSED(widget) |
|
98 | Q_UNUSED(widget) | |
99 | Q_UNUSED(option) |
|
99 | Q_UNUSED(option) | |
100 |
|
100 | |||
101 | painter->save(); |
|
101 | painter->save(); | |
102 | painter->setClipRect(clipRect()); |
|
102 | painter->setClipRect(clipRect()); | |
103 | painter->setPen(m_linePen); |
|
103 | painter->setPen(m_linePen); | |
104 | painter->drawPath(m_path); |
|
104 | painter->drawPath(m_path); | |
105 | if(m_pointsVisible){ |
|
105 | if(m_pointsVisible){ | |
106 | painter->setPen(m_pointPen); |
|
106 | painter->setPen(m_pointPen); | |
107 | painter->drawPoints(points()); |
|
107 | painter->drawPoints(points()); | |
108 | } |
|
108 | } | |
109 | painter->restore(); |
|
109 | painter->restore(); | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 |
|
112 | |||
113 |
|
113 | |||
114 | #include "moc_splinechartitem_p.cpp" |
|
114 | #include "moc_splinechartitem_p.cpp" | |
115 |
|
115 | |||
116 | QTCOMMERCIALCHART_END_NAMESPACE |
|
116 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,46 +1,46 | |||||
1 | #ifndef SPLINECHARTITEM_P_H |
|
1 | #ifndef SPLINECHARTITEM_P_H | |
2 | #define SPLINECHARTITEM_P_H |
|
2 | #define SPLINECHARTITEM_P_H | |
3 |
|
3 | |||
4 | #include "qsplineseries.h" |
|
4 | #include "qsplineseries.h" | |
5 | #include "xychartitem_p.h" |
|
5 | #include "xychartitem_p.h" | |
6 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class SplineChartItem : public XYChartItem |
|
11 | class SplineChartItem : public XYChartItem | |
12 | { |
|
12 | { | |
13 | Q_OBJECT |
|
13 | Q_OBJECT | |
14 | public: |
|
14 | public: | |
15 |
SplineChartItem(QSplineSeries* series, |
|
15 | SplineChartItem(QSplineSeries* series, ChartPresenter *presenter); | |
16 |
|
16 | |||
17 | //from QGraphicsItem |
|
17 | //from QGraphicsItem | |
18 | QRectF boundingRect() const; |
|
18 | QRectF boundingRect() const; | |
19 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
19 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
20 | QPainterPath shape() const; |
|
20 | QPainterPath shape() const; | |
21 |
|
21 | |||
22 | public slots: |
|
22 | public slots: | |
23 | void handleUpdated(); |
|
23 | void handleUpdated(); | |
24 |
|
24 | |||
25 | protected: |
|
25 | protected: | |
26 | void setLayout(QVector<QPointF>& points,QVector<QPointF>& controlPoints); |
|
26 | void setLayout(QVector<QPointF>& points,QVector<QPointF>& controlPoints); | |
27 | void updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index); |
|
27 | void updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index); | |
28 |
|
28 | |||
29 | private: |
|
29 | private: | |
30 | QPointF calculateGeometryControlPoint(int index) const; |
|
30 | QPointF calculateGeometryControlPoint(int index) const; | |
31 |
|
31 | |||
32 | private: |
|
32 | private: | |
33 | QSplineSeries* m_series; |
|
33 | QSplineSeries* m_series; | |
34 | QPainterPath m_path; |
|
34 | QPainterPath m_path; | |
35 | QRectF m_rect; |
|
35 | QRectF m_rect; | |
36 | QPen m_linePen; |
|
36 | QPen m_linePen; | |
37 | QPen m_pointPen; |
|
37 | QPen m_pointPen; | |
38 | bool m_pointsVisible; |
|
38 | bool m_pointsVisible; | |
39 | QVector<QPointF> m_controlPoints; |
|
39 | QVector<QPointF> m_controlPoints; | |
40 |
|
40 | |||
41 | friend class SplineAnimation; |
|
41 | friend class SplineAnimation; | |
42 | }; |
|
42 | }; | |
43 |
|
43 | |||
44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
44 | QTCOMMERCIALCHART_END_NAMESPACE | |
45 |
|
45 | |||
46 | #endif // SPLINECHARTITEM_P_H |
|
46 | #endif // SPLINECHARTITEM_P_H |
@@ -1,147 +1,149 | |||||
1 | !include( ../common.pri ):error( Couldn't find the common.pri file! ) |
|
1 | !include( ../common.pri ):error( Couldn't find the common.pri file! ) | |
2 | TARGET = QtCommercialChart |
|
2 | TARGET = QtCommercialChart | |
3 | DESTDIR = $$CHART_BUILD_LIB_DIR |
|
3 | DESTDIR = $$CHART_BUILD_LIB_DIR | |
4 | TEMPLATE = lib |
|
4 | TEMPLATE = lib | |
5 | QT += core \ |
|
5 | QT += core \ | |
6 | gui |
|
6 | gui | |
7 | win32-msvc*: LIBS += User32.lib |
|
7 | win32-msvc*: LIBS += User32.lib | |
8 | CONFIG += debug_and_release |
|
8 | CONFIG += debug_and_release | |
9 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
9 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd | |
10 | SOURCES += \ |
|
10 | SOURCES += \ | |
11 | chartdataset.cpp \ |
|
11 | chartdataset.cpp \ | |
12 | chartpresenter.cpp \ |
|
12 | chartpresenter.cpp \ | |
13 | charttheme.cpp \ |
|
13 | charttheme.cpp \ | |
14 | domain.cpp \ |
|
14 | domain.cpp \ | |
15 | qchart.cpp \ |
|
15 | qchart.cpp \ | |
16 | qchartview.cpp \ |
|
16 | qchartview.cpp \ | |
17 | qseries.cpp \ |
|
17 | qseries.cpp \ | |
18 | qlegend.cpp \ |
|
18 | qlegend.cpp \ | |
19 | legendmarker.cpp \ |
|
19 | legendmarker.cpp \ | |
20 | chartbackground.cpp |
|
20 | chartbackground.cpp \ | |
|
21 | chart.cpp | |||
21 | PRIVATE_HEADERS += \ |
|
22 | PRIVATE_HEADERS += \ | |
22 | chartdataset_p.h \ |
|
23 | chartdataset_p.h \ | |
23 | chartitem_p.h \ |
|
24 | chartitem_p.h \ | |
24 | chartpresenter_p.h \ |
|
25 | chartpresenter_p.h \ | |
25 | charttheme_p.h \ |
|
26 | charttheme_p.h \ | |
26 | domain_p.h \ |
|
27 | domain_p.h \ | |
27 | legendmarker_p.h \ |
|
28 | legendmarker_p.h \ | |
28 | chartbackground_p.h |
|
29 | chartbackground_p.h \ | |
|
30 | chart_p.h | |||
29 | PUBLIC_HEADERS += \ |
|
31 | PUBLIC_HEADERS += \ | |
30 | qchart.h \ |
|
32 | qchart.h \ | |
31 | qchartglobal.h \ |
|
33 | qchartglobal.h \ | |
32 | qseries.h \ |
|
34 | qseries.h \ | |
33 | qchartview.h \ |
|
35 | qchartview.h \ | |
34 | qlegend.h |
|
36 | qlegend.h | |
35 |
|
37 | |||
36 | include(animations/animations.pri) |
|
38 | include(animations/animations.pri) | |
37 | include(axis/axis.pri) |
|
39 | include(axis/axis.pri) | |
38 | include(xychart/xychart.pri) |
|
40 | include(xychart/xychart.pri) | |
39 | include(linechart/linechart.pri) |
|
41 | include(linechart/linechart.pri) | |
40 | include(areachart/areachart.pri) |
|
42 | include(areachart/areachart.pri) | |
41 | include(barchart/barchart.pri) |
|
43 | include(barchart/barchart.pri) | |
42 | include(piechart/piechart.pri) |
|
44 | include(piechart/piechart.pri) | |
43 | include(scatterseries/scatter.pri) |
|
45 | include(scatterseries/scatter.pri) | |
44 | include(splinechart/splinechart.pri) |
|
46 | include(splinechart/splinechart.pri) | |
45 |
|
47 | |||
46 | THEMES += themes/chartthemedefault_p.h \ |
|
48 | THEMES += themes/chartthemedefault_p.h \ | |
47 | themes/chartthemelight_p.h \ |
|
49 | themes/chartthemelight_p.h \ | |
48 | themes/chartthemebluecerulean_p.h \ |
|
50 | themes/chartthemebluecerulean_p.h \ | |
49 | themes/chartthemedark_p.h \ |
|
51 | themes/chartthemedark_p.h \ | |
50 | themes/chartthemebrownsand_p.h \ |
|
52 | themes/chartthemebrownsand_p.h \ | |
51 | themes/chartthemebluencs_p.h \ |
|
53 | themes/chartthemebluencs_p.h \ | |
52 | themes/chartthemeicy_p.h \ |
|
54 | themes/chartthemeicy_p.h \ | |
53 | themes/chartthemegrayscale_p.h \ |
|
55 | themes/chartthemegrayscale_p.h \ | |
54 | themes/chartthemescientific_p.h \ |
|
56 | themes/chartthemescientific_p.h \ | |
55 | themes/chartthemevanilla_p.h |
|
57 | themes/chartthemevanilla_p.h | |
56 |
|
58 | |||
57 | HEADERS += $$PUBLIC_HEADERS |
|
59 | HEADERS += $$PUBLIC_HEADERS | |
58 | HEADERS += $$PRIVATE_HEADERS |
|
60 | HEADERS += $$PRIVATE_HEADERS | |
59 | HEADERS += $$THEMES |
|
61 | HEADERS += $$THEMES | |
60 | INCLUDEPATH += linechart \ |
|
62 | INCLUDEPATH += linechart \ | |
61 | barchart \ |
|
63 | barchart \ | |
62 | themes \ |
|
64 | themes \ | |
63 | . |
|
65 | . | |
64 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib |
|
66 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib | |
65 | MOC_DIR = $$CHART_BUILD_DIR/lib |
|
67 | MOC_DIR = $$CHART_BUILD_DIR/lib | |
66 | UI_DIR = $$CHART_BUILD_DIR/lib |
|
68 | UI_DIR = $$CHART_BUILD_DIR/lib | |
67 | RCC_DIR = $$CHART_BUILD_DIR/lib |
|
69 | RCC_DIR = $$CHART_BUILD_DIR/lib | |
68 | DEFINES += QTCOMMERCIALCHART_LIBRARY |
|
70 | DEFINES += QTCOMMERCIALCHART_LIBRARY | |
69 |
|
71 | |||
70 | #qt public headers |
|
72 | #qt public headers | |
71 | #this is very primitive and lame parser , TODO: make perl script insted |
|
73 | #this is very primitive and lame parser , TODO: make perl script insted | |
72 | !exists($$CHART_BUILD_PUBLIC_HEADER_DIR) |
|
74 | !exists($$CHART_BUILD_PUBLIC_HEADER_DIR) | |
73 | { |
|
75 | { | |
74 | system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR) |
|
76 | system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR) | |
75 | } |
|
77 | } | |
76 |
|
78 | |||
77 | for(file, PUBLIC_HEADERS) { |
|
79 | for(file, PUBLIC_HEADERS) { | |
78 | name = $$split(file,'/') |
|
80 | name = $$split(file,'/') | |
79 | name = $$last(name) |
|
81 | name = $$last(name) | |
80 | class = "$$cat($$file)" |
|
82 | class = "$$cat($$file)" | |
81 | class = $$find(class,class) |
|
83 | class = $$find(class,class) | |
82 | !isEmpty(class){ |
|
84 | !isEmpty(class){ | |
83 | class = $$split(class,QTCOMMERCIALCHART_EXPORT) |
|
85 | class = $$split(class,QTCOMMERCIALCHART_EXPORT) | |
84 | class = $$member(class,1) |
|
86 | class = $$member(class,1) | |
85 | class = $$split(class,' ') |
|
87 | class = $$split(class,' ') | |
86 | class = $$replace(class,' ','') |
|
88 | class = $$replace(class,' ','') | |
87 | class = $$member(class,0) |
|
89 | class = $$member(class,0) | |
88 | win32:{ |
|
90 | win32:{ | |
89 | command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class" |
|
91 | command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class" | |
90 | }else{ |
|
92 | }else{ | |
91 | command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class" |
|
93 | command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class" | |
92 | } |
|
94 | } | |
93 | PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class |
|
95 | PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class | |
94 | system($$command) |
|
96 | system($$command) | |
95 | } |
|
97 | } | |
96 | } |
|
98 | } | |
97 |
|
99 | |||
98 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart |
|
100 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart | |
99 | public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS |
|
101 | public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS | |
100 |
|
102 | |||
101 | target.path = $$[QT_INSTALL_LIBS] |
|
103 | target.path = $$[QT_INSTALL_LIBS] | |
102 | INSTALLS += target public_headers |
|
104 | INSTALLS += target public_headers | |
103 |
|
105 | |||
104 | install_build_public_headers.name = build_public_headers |
|
106 | install_build_public_headers.name = build_public_headers | |
105 | install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
107 | install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h | |
106 | install_build_public_headers.input = PUBLIC_HEADERS |
|
108 | install_build_public_headers.input = PUBLIC_HEADERS | |
107 | install_build_public_headers.commands = $$QMAKE_COPY \ |
|
109 | install_build_public_headers.commands = $$QMAKE_COPY \ | |
108 | ${QMAKE_FILE_NAME} \ |
|
110 | ${QMAKE_FILE_NAME} \ | |
109 | $$CHART_BUILD_PUBLIC_HEADER_DIR |
|
111 | $$CHART_BUILD_PUBLIC_HEADER_DIR | |
110 | install_build_public_headers.CONFIG += target_predeps \ |
|
112 | install_build_public_headers.CONFIG += target_predeps \ | |
111 | no_link |
|
113 | no_link | |
112 |
|
114 | |||
113 | install_build_private_headers.name = buld_private_headers |
|
115 | install_build_private_headers.name = buld_private_headers | |
114 | install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
116 | install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h | |
115 | install_build_private_headers.input = PRIVATE_HEADERS |
|
117 | install_build_private_headers.input = PRIVATE_HEADERS | |
116 | install_build_private_headers.commands = $$QMAKE_COPY \ |
|
118 | install_build_private_headers.commands = $$QMAKE_COPY \ | |
117 | ${QMAKE_FILE_NAME} \ |
|
119 | ${QMAKE_FILE_NAME} \ | |
118 | $$CHART_BUILD_PRIVATE_HEADER_DIR |
|
120 | $$CHART_BUILD_PRIVATE_HEADER_DIR | |
119 | install_build_private_headers.CONFIG += target_predeps \ |
|
121 | install_build_private_headers.CONFIG += target_predeps \ | |
120 | no_link |
|
122 | no_link | |
121 |
|
123 | |||
122 | QMAKE_EXTRA_COMPILERS += install_build_public_headers \ |
|
124 | QMAKE_EXTRA_COMPILERS += install_build_public_headers \ | |
123 | install_build_private_headers \ |
|
125 | install_build_private_headers \ | |
124 |
|
126 | |||
125 | chartversion.target = qchartversion_p.h |
|
127 | chartversion.target = qchartversion_p.h | |
126 | chartversion.commands = @echo \ |
|
128 | chartversion.commands = @echo \ | |
127 | "build_time" \ |
|
129 | "build_time" \ | |
128 | > \ |
|
130 | > \ | |
129 | $$chartversion.target; |
|
131 | $$chartversion.target; | |
130 | chartversion.depends = $$HEADERS \ |
|
132 | chartversion.depends = $$HEADERS \ | |
131 | $$SOURCES |
|
133 | $$SOURCES | |
132 | PRE_TARGETDEPS += qchartversion_p.h |
|
134 | PRE_TARGETDEPS += qchartversion_p.h | |
133 | QMAKE_CLEAN += qchartversion_p.h |
|
135 | QMAKE_CLEAN += qchartversion_p.h | |
134 | QMAKE_EXTRA_TARGETS += chartversion |
|
136 | QMAKE_EXTRA_TARGETS += chartversion | |
135 | unix:QMAKE_DISTCLEAN += -r \ |
|
137 | unix:QMAKE_DISTCLEAN += -r \ | |
136 | $$CHART_BUILD_HEADER_DIR \ |
|
138 | $$CHART_BUILD_HEADER_DIR \ | |
137 | $$CHART_BUILD_LIB_DIR |
|
139 | $$CHART_BUILD_LIB_DIR | |
138 | win32:QMAKE_DISTCLEAN += /Q \ |
|
140 | win32:QMAKE_DISTCLEAN += /Q \ | |
139 | $$CHART_BUILD_HEADER_DIR \ |
|
141 | $$CHART_BUILD_HEADER_DIR \ | |
140 | $$CHART_BUILD_LIB_DIR |
|
142 | $$CHART_BUILD_LIB_DIR | |
141 |
|
143 | |||
142 | # treat warnings as errors |
|
144 | # treat warnings as errors | |
143 | win32-msvc*: { |
|
145 | win32-msvc*: { | |
144 | QMAKE_CXXFLAGS += /WX |
|
146 | QMAKE_CXXFLAGS += /WX | |
145 | } else { |
|
147 | } else { | |
146 | QMAKE_CXXFLAGS += -Werror |
|
148 | QMAKE_CXXFLAGS += -Werror | |
147 | } |
|
149 | } |
@@ -1,156 +1,156 | |||||
1 | #include "xychartitem_p.h" |
|
1 | #include "xychartitem_p.h" | |
2 | #include "qxyseries.h" |
|
2 | #include "qxyseries.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include "chartanimator_p.h" |
|
4 | #include "chartanimator_p.h" | |
5 | #include <QPainter> |
|
5 | #include <QPainter> | |
6 | #include <QGraphicsSceneMouseEvent> |
|
6 | #include <QGraphicsSceneMouseEvent> | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | //TODO: optimize : remove points which are not visible |
|
11 | //TODO: optimize : remove points which are not visible | |
12 |
|
12 | |||
13 |
XYChartItem::XYChartItem(QXYSeries* series, |
|
13 | XYChartItem::XYChartItem(QXYSeries* series, ChartPresenter *presenter):ChartItem(presenter), | |
14 | m_minX(0), |
|
14 | m_minX(0), | |
15 | m_maxX(0), |
|
15 | m_maxX(0), | |
16 | m_minY(0), |
|
16 | m_minY(0), | |
17 | m_maxY(0), |
|
17 | m_maxY(0), | |
18 | m_series(series) |
|
18 | m_series(series) | |
19 | { |
|
19 | { | |
20 | QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int))); |
|
20 | QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int))); | |
21 | QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int))); |
|
21 | QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int))); | |
22 | QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int))); |
|
22 | QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int))); | |
23 | QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&))); |
|
23 | QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&))); | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const |
|
26 | QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const | |
27 | { |
|
27 | { | |
28 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
28 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |
29 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
29 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |
30 | qreal x = (point.x() - m_minX)* deltaX; |
|
30 | qreal x = (point.x() - m_minX)* deltaX; | |
31 | qreal y = (point.y() - m_minY)*-deltaY + m_size.height(); |
|
31 | qreal y = (point.y() - m_minY)*-deltaY + m_size.height(); | |
32 | return QPointF(x,y); |
|
32 | return QPointF(x,y); | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | QPointF XYChartItem::calculateGeometryPoint(int index) const |
|
36 | QPointF XYChartItem::calculateGeometryPoint(int index) const | |
37 | { |
|
37 | { | |
38 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
38 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |
39 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
39 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |
40 | qreal x = (m_series->x(index) - m_minX)* deltaX; |
|
40 | qreal x = (m_series->x(index) - m_minX)* deltaX; | |
41 | qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height(); |
|
41 | qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height(); | |
42 | return QPointF(x,y); |
|
42 | return QPointF(x,y); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | QVector<QPointF> XYChartItem::calculateGeometryPoints() const |
|
45 | QVector<QPointF> XYChartItem::calculateGeometryPoints() const | |
46 | { |
|
46 | { | |
47 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
47 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |
48 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
48 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |
49 |
|
49 | |||
50 | QVector<QPointF> points; |
|
50 | QVector<QPointF> points; | |
51 | points.reserve(m_series->count()); |
|
51 | points.reserve(m_series->count()); | |
52 | for (int i = 0; i < m_series->count(); ++i) { |
|
52 | for (int i = 0; i < m_series->count(); ++i) { | |
53 | qreal x = (m_series->x(i) - m_minX)* deltaX; |
|
53 | qreal x = (m_series->x(i) - m_minX)* deltaX; | |
54 | qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height(); |
|
54 | qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height(); | |
55 | points << QPointF(x,y); |
|
55 | points << QPointF(x,y); | |
56 | } |
|
56 | } | |
57 | return points; |
|
57 | return points; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const |
|
60 | QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const | |
61 | { |
|
61 | { | |
62 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
62 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | |
63 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
63 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); | |
64 | qreal x = point.x()/deltaX +m_minX; |
|
64 | qreal x = point.x()/deltaX +m_minX; | |
65 | qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY; |
|
65 | qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY; | |
66 | return QPointF(x,y); |
|
66 | return QPointF(x,y); | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | void XYChartItem::updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index) |
|
69 | void XYChartItem::updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index) | |
70 | { |
|
70 | { | |
71 |
if( |
|
71 | if(animator()){ | |
72 |
|
|
72 | animator()->updateLayout(this,oldPoints,newPoints,index); | |
73 | }else{ |
|
73 | }else{ | |
74 | setLayout(newPoints); |
|
74 | setLayout(newPoints); | |
75 | } |
|
75 | } | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | void XYChartItem::setLayout(QVector<QPointF>& points) |
|
78 | void XYChartItem::setLayout(QVector<QPointF>& points) | |
79 | { |
|
79 | { | |
80 | m_points = points; |
|
80 | m_points = points; | |
81 | update(); |
|
81 | update(); | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | //handlers |
|
84 | //handlers | |
85 |
|
85 | |||
86 | void XYChartItem::handlePointAdded(int index) |
|
86 | void XYChartItem::handlePointAdded(int index) | |
87 | { |
|
87 | { | |
88 | Q_ASSERT(index<m_series->count()); |
|
88 | Q_ASSERT(index<m_series->count()); | |
89 | Q_ASSERT(index>=0); |
|
89 | Q_ASSERT(index>=0); | |
90 | QPointF point = calculateGeometryPoint(index); |
|
90 | QPointF point = calculateGeometryPoint(index); | |
91 | QVector<QPointF> points = m_points; |
|
91 | QVector<QPointF> points = m_points; | |
92 | points.insert(index,point); |
|
92 | points.insert(index,point); | |
93 | updateLayout(m_points,points,index); |
|
93 | updateLayout(m_points,points,index); | |
94 | update(); |
|
94 | update(); | |
95 | } |
|
95 | } | |
96 | void XYChartItem::handlePointRemoved(int index) |
|
96 | void XYChartItem::handlePointRemoved(int index) | |
97 | { |
|
97 | { | |
98 | Q_ASSERT(index<m_series->count() + 1); |
|
98 | Q_ASSERT(index<m_series->count() + 1); | |
99 | Q_ASSERT(index>=0); |
|
99 | Q_ASSERT(index>=0); | |
100 | QVector<QPointF> points = m_points; |
|
100 | QVector<QPointF> points = m_points; | |
101 | points.remove(index); |
|
101 | points.remove(index); | |
102 | updateLayout(m_points,points,index); |
|
102 | updateLayout(m_points,points,index); | |
103 | update(); |
|
103 | update(); | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | void XYChartItem::handlePointReplaced(int index) |
|
106 | void XYChartItem::handlePointReplaced(int index) | |
107 | { |
|
107 | { | |
108 | Q_ASSERT(index<m_series->count()); |
|
108 | Q_ASSERT(index<m_series->count()); | |
109 | Q_ASSERT(index>=0); |
|
109 | Q_ASSERT(index>=0); | |
110 | QPointF point = calculateGeometryPoint(index); |
|
110 | QPointF point = calculateGeometryPoint(index); | |
111 | QVector<QPointF> points = m_points; |
|
111 | QVector<QPointF> points = m_points; | |
112 | points.replace(index,point); |
|
112 | points.replace(index,point); | |
113 | updateLayout(m_points,points,index); |
|
113 | updateLayout(m_points,points,index); | |
114 | update(); |
|
114 | update(); | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
117 | void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | |
118 | { |
|
118 | { | |
119 | m_minX=minX; |
|
119 | m_minX=minX; | |
120 | m_maxX=maxX; |
|
120 | m_maxX=maxX; | |
121 | m_minY=minY; |
|
121 | m_minY=minY; | |
122 | m_maxY=maxY; |
|
122 | m_maxY=maxY; | |
123 |
|
123 | |||
124 | if(isEmpty()) return; |
|
124 | if(isEmpty()) return; | |
125 | QVector<QPointF> points = calculateGeometryPoints(); |
|
125 | QVector<QPointF> points = calculateGeometryPoints(); | |
126 | updateLayout(m_points,points); |
|
126 | updateLayout(m_points,points); | |
127 | update(); |
|
127 | update(); | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | void XYChartItem::handleGeometryChanged(const QRectF& rect) |
|
130 | void XYChartItem::handleGeometryChanged(const QRectF& rect) | |
131 | { |
|
131 | { | |
132 | Q_ASSERT(rect.isValid()); |
|
132 | Q_ASSERT(rect.isValid()); | |
133 | m_size=rect.size(); |
|
133 | m_size=rect.size(); | |
134 | m_clipRect=rect.translated(-rect.topLeft()); |
|
134 | m_clipRect=rect.translated(-rect.topLeft()); | |
135 | setPos(rect.topLeft()); |
|
135 | setPos(rect.topLeft()); | |
136 |
|
136 | |||
137 | if(isEmpty()) return; |
|
137 | if(isEmpty()) return; | |
138 | QVector<QPointF> points = calculateGeometryPoints(); |
|
138 | QVector<QPointF> points = calculateGeometryPoints(); | |
139 | updateLayout(m_points,points); |
|
139 | updateLayout(m_points,points); | |
140 | update(); |
|
140 | update(); | |
141 | } |
|
141 | } | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | bool XYChartItem::isEmpty() |
|
144 | bool XYChartItem::isEmpty() | |
145 | { |
|
145 | { | |
146 | return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ; |
|
146 | return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ; | |
147 | } |
|
147 | } | |
148 |
|
148 | |||
149 | void XYChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event ) |
|
149 | void XYChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event ) | |
150 | { |
|
150 | { | |
151 | emit clicked(calculateDomainPoint(event->pos())); |
|
151 | emit clicked(calculateDomainPoint(event->pos())); | |
152 | } |
|
152 | } | |
153 |
|
153 | |||
154 | #include "moc_xychartitem_p.cpp" |
|
154 | #include "moc_xychartitem_p.cpp" | |
155 |
|
155 | |||
156 | QTCOMMERCIALCHART_END_NAMESPACE |
|
156 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,62 +1,65 | |||||
1 | #ifndef XYCHARTITEM_H |
|
1 | #ifndef XYCHARTITEM_H | |
2 | #define XYCHARTITEM_H |
|
2 | #define XYCHARTITEM_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class ChartPresenter; |
|
10 | class ChartPresenter; | |
11 | class QXYSeries; |
|
11 | class QXYSeries; | |
12 |
|
12 | |||
13 |
class XYChartItem : public |
|
13 | class XYChartItem : public ChartItem | |
14 | { |
|
14 | { | |
15 | Q_OBJECT |
|
15 | Q_OBJECT | |
16 | public: |
|
16 | public: | |
17 |
explicit XYChartItem(QXYSeries* series, |
|
17 | explicit XYChartItem(QXYSeries* series, ChartPresenter *presenter); | |
18 | ~ XYChartItem(){}; |
|
18 | ~ XYChartItem(){}; | |
19 |
|
19 | |||
20 | QVector<QPointF> points() const {return m_points;} |
|
20 | QVector<QPointF> points() const {return m_points;} | |
21 | QRectF clipRect() const { return m_clipRect;} |
|
21 | QRectF clipRect() const { return m_clipRect;} | |
22 |
|
22 | |||
|
23 | ||||
23 | public slots: |
|
24 | public slots: | |
24 | void handlePointAdded(int index); |
|
25 | void handlePointAdded(int index); | |
25 | void handlePointRemoved(int index); |
|
26 | void handlePointRemoved(int index); | |
26 | void handlePointReplaced(int index); |
|
27 | void handlePointReplaced(int index); | |
27 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
28 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); | |
28 | void handleGeometryChanged(const QRectF& size); |
|
29 | void handleGeometryChanged(const QRectF& size); | |
29 |
|
30 | |||
30 | signals: |
|
31 | signals: | |
31 | void clicked(const QPointF& point); |
|
32 | void clicked(const QPointF& point); | |
32 |
|
33 | |||
33 | protected: |
|
34 | protected: | |
|
35 | ||||
34 | virtual void setLayout(QVector<QPointF>& points); |
|
36 | virtual void setLayout(QVector<QPointF>& points); | |
35 | virtual void updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index = 0); |
|
37 | virtual void updateLayout(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints,int index = 0); | |
|
38 | ||||
36 | QPointF calculateGeometryPoint(const QPointF& point) const; |
|
39 | QPointF calculateGeometryPoint(const QPointF& point) const; | |
37 | QPointF calculateGeometryPoint(int index) const; |
|
40 | QPointF calculateGeometryPoint(int index) const; | |
38 | QPointF calculateDomainPoint(const QPointF& point) const; |
|
41 | QPointF calculateDomainPoint(const QPointF& point) const; | |
39 | QVector<QPointF> calculateGeometryPoints() const; |
|
42 | QVector<QPointF> calculateGeometryPoints() const; | |
40 | void mousePressEvent( QGraphicsSceneMouseEvent * event ); |
|
43 | void mousePressEvent( QGraphicsSceneMouseEvent * event ); | |
41 |
|
44 | |||
42 | private: |
|
45 | private: | |
43 | inline bool isEmpty(); |
|
46 | inline bool isEmpty(); | |
44 |
|
47 | |||
45 | private: |
|
48 | private: | |
46 | qreal m_minX; |
|
49 | qreal m_minX; | |
47 | qreal m_maxX; |
|
50 | qreal m_maxX; | |
48 | qreal m_minY; |
|
51 | qreal m_minY; | |
49 | qreal m_maxY; |
|
52 | qreal m_maxY; | |
50 | QXYSeries* m_series; |
|
53 | QXYSeries* m_series; | |
51 | QSizeF m_size; |
|
54 | QSizeF m_size; | |
52 | QRectF m_clipRect; |
|
55 | QRectF m_clipRect; | |
53 | QVector<QPointF> m_points; |
|
56 | QVector<QPointF> m_points; | |
54 |
|
57 | |||
55 | friend class XYAnimation; |
|
58 | friend class XYAnimation; | |
56 | friend class AreaChartItem; |
|
59 | friend class AreaChartItem; | |
57 |
|
60 | |||
58 | }; |
|
61 | }; | |
59 |
|
62 | |||
60 | QTCOMMERCIALCHART_END_NAMESPACE |
|
63 | QTCOMMERCIALCHART_END_NAMESPACE | |
61 |
|
64 | |||
62 | #endif |
|
65 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now