##// END OF EJS Templates
Adds proper series counter for theme initialization
Michal Klocek -
r533:3b56d6625202
parent child
Show More
@@ -1,291 +1,302
1 #include "chartdataset_p.h"
1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 //series
3 //series
4 #include "qlineseries.h"
4 #include "qlineseries.h"
5 #include "qareaseries.h"
5 #include "qareaseries.h"
6 #include "qbarseries.h"
6 #include "qbarseries.h"
7 #include "qstackedbarseries.h"
7 #include "qstackedbarseries.h"
8 #include "qpercentbarseries.h"
8 #include "qpercentbarseries.h"
9 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include "qscatterseries.h"
10 #include "qscatterseries.h"
11 #include "qsplineseries.h"
11 #include "qsplineseries.h"
12
12
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14
14
15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
16 m_axisX(new QChartAxis(this)),
16 m_axisX(new QChartAxis(this)),
17 m_axisY(new QChartAxis(this)),
17 m_axisY(new QChartAxis(this)),
18 m_domainIndex(0),
18 m_domainIndex(0),
19 m_axisXInitialized(false)
19 m_axisXInitialized(false)
20 {
20 {
21 }
21 }
22
22
23 ChartDataSet::~ChartDataSet()
23 ChartDataSet::~ChartDataSet()
24 {
24 {
25 }
25 }
26
26
27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
28 {
28 {
29 if(axisY==0) axisY = m_axisY;
29 if(axisY==0) axisY = m_axisY;
30
30
31 QChartAxis* axis = m_seriesAxisMap.value(series);
31 QChartAxis* axis = m_seriesAxisMap.value(series);
32
32
33 if(axis) {
33 if(axis) {
34 qWarning() << "Can not add series. Series already on the chart";
34 qWarning() << "Can not add series. Series already on the chart";
35 return;
35 return;
36 }
36 }
37
37
38 if(!series->parent()){
38 if(!series->parent()){
39 series->setParent(this); // take ownership
39 series->setParent(this); // take ownership
40 };
40 };
41
41
42 if(!axisY->parent()){
42 if(!axisY->parent()){
43 axisY->setParent(this); // take ownership
43 axisY->setParent(this); // take ownership
44 }
44 }
45
45
46 Domain* domain = m_axisDomainMap.value(axisY);
46 Domain* domain = m_axisDomainMap.value(axisY);
47
47
48 if(!domain) {
48 if(!domain) {
49 domain = new Domain();
49 domain = new Domain();
50 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal,int)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal,int)));
50 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal,int)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal,int)));
51 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal,int)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal,int)));
51 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal,int)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal,int)));
52 //initialize
52 //initialize
53 m_axisDomainMap.insert(axisY,domain);
53 m_axisDomainMap.insert(axisY,domain);
54 emit axisAdded(axisY,domain);
54 emit axisAdded(axisY,domain);
55 }
55 }
56
56
57 if(!m_axisXInitialized){
57 if(!m_axisXInitialized){
58 emit axisAdded(axisX(),domain);
58 emit axisAdded(axisX(),domain);
59 m_axisXInitialized=true;
59 m_axisXInitialized=true;
60 }
60 }
61
61
62 calculateDomain(series,domain);
62 calculateDomain(series,domain);
63
63
64 m_seriesAxisMap.insert(series,axisY);
64 m_seriesAxisMap.insert(series,axisY);
65 emit seriesAdded(series,domain);
65 emit seriesAdded(series,domain);
66
66
67 }
67 }
68
68
69 void ChartDataSet::removeSeries(QSeries* series)
69 void ChartDataSet::removeSeries(QSeries* series)
70 {
70 {
71
71
72 QChartAxis* axis = m_seriesAxisMap.value(series);
72 QChartAxis* axis = m_seriesAxisMap.value(series);
73
73
74 if(!axis){
74 if(!axis){
75 qWarning()<<"Can not remove series. Series not found on the chart.";
75 qWarning()<<"Can not remove series. Series not found on the chart.";
76 return;
76 return;
77 }
77 }
78 emit seriesRemoved(series);
78 emit seriesRemoved(series);
79 m_seriesAxisMap.remove(series);
79 m_seriesAxisMap.remove(series);
80
80
81 if(series->parent()==this){
81 if(series->parent()==this){
82 delete series;
82 delete series;
83 series=0;
83 series=0;
84 }
84 }
85
85
86 QList<QChartAxis*> axes = m_seriesAxisMap.values();
86 QList<QChartAxis*> axes = m_seriesAxisMap.values();
87
87
88 int i = axes.indexOf(axis);
88 int i = axes.indexOf(axis);
89
89
90 if(i==-1){
90 if(i==-1){
91 Domain* domain = m_axisDomainMap.take(axis);
91 Domain* domain = m_axisDomainMap.take(axis);
92 emit axisRemoved(axis);
92 emit axisRemoved(axis);
93 if(axis!=axisY()){
93 if(axis!=axisY()){
94 if(axis->parent()==this){
94 if(axis->parent()==this){
95 delete axis;
95 delete axis;
96 axis=0;
96 axis=0;
97 }
97 }
98 }
98 }
99 delete domain;
99 delete domain;
100 }
100 }
101
101
102 if(m_seriesAxisMap.values().size()==0)
102 if(m_seriesAxisMap.values().size()==0)
103 {
103 {
104 m_axisXInitialized=false;
104 m_axisXInitialized=false;
105 emit axisRemoved(axisX());
105 emit axisRemoved(axisX());
106 }
106 }
107 }
107 }
108
108
109 void ChartDataSet::removeAllSeries()
109 void ChartDataSet::removeAllSeries()
110 {
110 {
111
111
112 QList<QSeries*> series = m_seriesAxisMap.keys();
112 QList<QSeries*> series = m_seriesAxisMap.keys();
113
113
114 foreach(QSeries* s , series) {
114 foreach(QSeries* s , series) {
115 removeSeries(s);
115 removeSeries(s);
116 }
116 }
117
117
118 Q_ASSERT(m_seriesAxisMap.count()==0);
118 Q_ASSERT(m_seriesAxisMap.count()==0);
119 Q_ASSERT(m_axisDomainMap.count()==0);
119 Q_ASSERT(m_axisDomainMap.count()==0);
120
120
121 }
121 }
122
122
123 //to be removed with PIMPL
123 //to be removed with PIMPL
124 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
124 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
125 {
125 {
126 switch(series->type())
126 switch(series->type())
127 {
127 {
128 case QSeries::SeriesTypeLine:
128 case QSeries::SeriesTypeLine:
129 case QSeries::SeriesTypeSpline:
129 case QSeries::SeriesTypeSpline:
130 case QSeries::SeriesTypeScatter:
130 case QSeries::SeriesTypeScatter:
131 {
131 {
132
132
133 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
133 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
134
134
135 qreal minX(domain->minX());
135 qreal minX(domain->minX());
136 qreal minY(domain->minY());
136 qreal minY(domain->minY());
137 qreal maxX(domain->maxX());
137 qreal maxX(domain->maxX());
138 qreal maxY(domain->maxY());
138 qreal maxY(domain->maxY());
139
139
140 for (int i = 0; i < xySeries->count(); i++)
140 for (int i = 0; i < xySeries->count(); i++)
141 {
141 {
142 qreal x = xySeries->x(i);
142 qreal x = xySeries->x(i);
143 qreal y = xySeries->y(i);
143 qreal y = xySeries->y(i);
144 minX = qMin(minX, x);
144 minX = qMin(minX, x);
145 minY = qMin(minY, y);
145 minY = qMin(minY, y);
146 maxX = qMax(maxX, x);
146 maxX = qMax(maxX, x);
147 maxY = qMax(maxY, y);
147 maxY = qMax(maxY, y);
148 }
148 }
149
149
150 domain->setRange(minX, maxX, minY, maxY);
150 domain->setRange(minX, maxX, minY, maxY);
151 break;
151 break;
152 }
152 }
153 case QSeries::SeriesTypeArea: {
153 case QSeries::SeriesTypeArea: {
154
154
155 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
155 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
156
156
157 QLineSeries* upperSeries = areaSeries->upperSeries();
157 QLineSeries* upperSeries = areaSeries->upperSeries();
158 QLineSeries* lowerSeries = areaSeries->lowerSeries();
158 QLineSeries* lowerSeries = areaSeries->lowerSeries();
159
159
160 for (int i = 0; i < upperSeries->count(); i++)
160 for (int i = 0; i < upperSeries->count(); i++)
161 {
161 {
162 qreal x = upperSeries->x(i);
162 qreal x = upperSeries->x(i);
163 qreal y = upperSeries->y(i);
163 qreal y = upperSeries->y(i);
164 domain->setMinX(qMin(domain->minX(),x));
164 domain->setMinX(qMin(domain->minX(),x));
165 domain->setMinY(qMin(domain->minY(),y));
165 domain->setMinY(qMin(domain->minY(),y));
166 domain->setMaxX(qMax(domain->maxX(),x));
166 domain->setMaxX(qMax(domain->maxX(),x));
167 domain->setMaxY(qMax(domain->maxY(),y));
167 domain->setMaxY(qMax(domain->maxY(),y));
168 }
168 }
169 if(lowerSeries) {
169 if(lowerSeries) {
170 for (int i = 0; i < lowerSeries->count(); i++)
170 for (int i = 0; i < lowerSeries->count(); i++)
171 {
171 {
172 qreal x = lowerSeries->x(i);
172 qreal x = lowerSeries->x(i);
173 qreal y = lowerSeries->y(i);
173 qreal y = lowerSeries->y(i);
174 domain->setMinX(qMin(domain->minX(),x));
174 domain->setMinX(qMin(domain->minX(),x));
175 domain->setMinY(qMin(domain->minY(),y));
175 domain->setMinY(qMin(domain->minY(),y));
176 domain->setMaxX(qMax(domain->maxX(),x));
176 domain->setMaxX(qMax(domain->maxX(),x));
177 domain->setMaxY(qMax(domain->maxY(),y));
177 domain->setMaxY(qMax(domain->maxY(),y));
178 }}
178 }}
179 break;
179 break;
180 }
180 }
181 case QSeries::SeriesTypeBar: {
181 case QSeries::SeriesTypeBar: {
182 qDebug() << "QChartSeries::SeriesTypeBar";
182 qDebug() << "QChartSeries::SeriesTypeBar";
183 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
183 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
184 qreal x = barSeries->categoryCount();
184 qreal x = barSeries->categoryCount();
185 qreal y = barSeries->max();
185 qreal y = barSeries->max();
186 domain->setMinX(qMin(domain->minX(),x));
186 domain->setMinX(qMin(domain->minX(),x));
187 domain->setMinY(qMin(domain->minY(),y));
187 domain->setMinY(qMin(domain->minY(),y));
188 domain->setMaxX(qMax(domain->maxX(),x));
188 domain->setMaxX(qMax(domain->maxX(),x));
189 domain->setMaxY(qMax(domain->maxY(),y));
189 domain->setMaxY(qMax(domain->maxY(),y));
190 break;
190 break;
191 }
191 }
192 case QSeries::SeriesTypeStackedBar: {
192 case QSeries::SeriesTypeStackedBar: {
193 qDebug() << "QChartSeries::SeriesTypeStackedBar";
193 qDebug() << "QChartSeries::SeriesTypeStackedBar";
194
194
195 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
195 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
196 qreal x = stackedBarSeries->categoryCount();
196 qreal x = stackedBarSeries->categoryCount();
197 qreal y = stackedBarSeries->maxCategorySum();
197 qreal y = stackedBarSeries->maxCategorySum();
198 domain->setMinX(qMin(domain->minX(),x));
198 domain->setMinX(qMin(domain->minX(),x));
199 domain->setMinY(qMin(domain->minY(),y));
199 domain->setMinY(qMin(domain->minY(),y));
200 domain->setMaxX(qMax(domain->maxX(),x));
200 domain->setMaxX(qMax(domain->maxX(),x));
201 domain->setMaxY(qMax(domain->maxY(),y));
201 domain->setMaxY(qMax(domain->maxY(),y));
202 break;
202 break;
203 }
203 }
204 case QSeries::SeriesTypePercentBar: {
204 case QSeries::SeriesTypePercentBar: {
205 qDebug() << "QChartSeries::SeriesTypePercentBar";
205 qDebug() << "QChartSeries::SeriesTypePercentBar";
206
206
207 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
207 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
208 qreal x = percentBarSeries->categoryCount();
208 qreal x = percentBarSeries->categoryCount();
209 domain->setMinX(qMin(domain->minX(),x));
209 domain->setMinX(qMin(domain->minX(),x));
210 domain->setMinY(0);
210 domain->setMinY(0);
211 domain->setMaxX(qMax(domain->maxX(),x));
211 domain->setMaxX(qMax(domain->maxX(),x));
212 domain->setMaxY(100);
212 domain->setMaxY(100);
213 break;
213 break;
214 }
214 }
215
215
216 case QSeries::SeriesTypePie: {
216 case QSeries::SeriesTypePie: {
217 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
217 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
218 // TODO: domain stuff
218 // TODO: domain stuff
219 break;
219 break;
220 }
220 }
221
221
222
222
223 default: {
223 default: {
224 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
224 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
225 return;
225 return;
226 break;
226 break;
227 }
227 }
228
228
229 }
229 }
230 }
230 }
231
231
232 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
232 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
233 {
233 {
234 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
234 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
235 while (i.hasNext()) {
235 while (i.hasNext()) {
236 i.next();
236 i.next();
237 i.value()->zoomIn(rect,size);
237 i.value()->zoomIn(rect,size);
238 }
238 }
239 }
239 }
240
240
241 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
241 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
242 {
242 {
243 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
243 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
244 while (i.hasNext()) {
244 while (i.hasNext()) {
245 i.next();
245 i.next();
246 i.value()->zoomOut(rect,size);
246 i.value()->zoomOut(rect,size);
247 }
247 }
248 }
248 }
249
249
250 int ChartDataSet::seriesCount(QSeries::QSeriesType type)
251 {
252 int count=0;
253 QMapIterator<QSeries*, QChartAxis*> i( m_seriesAxisMap);
254 while (i.hasNext()) {
255 i.next();
256 if(i.key()->type()==type) count++;
257 }
258 return count;
259 }
260
250 QChartAxis* ChartDataSet::axisY(QSeries* series) const
261 QChartAxis* ChartDataSet::axisY(QSeries* series) const
251 {
262 {
252 if(series == 0) return m_axisY;
263 if(series == 0) return m_axisY;
253 return m_seriesAxisMap.value(series);
264 return m_seriesAxisMap.value(series);
254 }
265 }
255
266
256 Domain* ChartDataSet::domain(QSeries* series) const
267 Domain* ChartDataSet::domain(QSeries* series) const
257 {
268 {
258 QChartAxis* axis = m_seriesAxisMap.value(series);
269 QChartAxis* axis = m_seriesAxisMap.value(series);
259 if(axis){
270 if(axis){
260 return m_axisDomainMap.value(axis);
271 return m_axisDomainMap.value(axis);
261 }else
272 }else
262 return 0;
273 return 0;
263 }
274 }
264
275
265 Domain* ChartDataSet::domain(QChartAxis* axis) const
276 Domain* ChartDataSet::domain(QChartAxis* axis) const
266 {
277 {
267 if(axis==axisX()) {
278 if(axis==axisX()) {
268 return m_axisDomainMap.value(axisY());
279 return m_axisDomainMap.value(axisY());
269 }
280 }
270 else {
281 else {
271 return m_axisDomainMap.value(axis);
282 return m_axisDomainMap.value(axis);
272 }
283 }
273 }
284 }
274
285
275 QChartAxis* ChartDataSet::axis(QSeries* series) const
286 QChartAxis* ChartDataSet::axis(QSeries* series) const
276 {
287 {
277 return m_seriesAxisMap.value(series);
288 return m_seriesAxisMap.value(series);
278 }
289 }
279
290
280 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
291 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
281 {
292 {
282 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
293 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
283 while (i.hasNext()) {
294 while (i.hasNext()) {
284 i.next();
295 i.next();
285 i.value()->move(dx,dy,size);
296 i.value()->move(dx,dy,size);
286 }
297 }
287 }
298 }
288
299
289 #include "moc_chartdataset_p.cpp"
300 #include "moc_chartdataset_p.cpp"
290
301
291 QTCOMMERCIALCHART_END_NAMESPACE
302 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,58
1 #ifndef CHARTDATASET_P_H_
1 #ifndef CHARTDATASET_P_H_
2 #define CHARTDATASET_P_H_
2 #define CHARTDATASET_P_H_
3
3
4 #include "qseries.h"
4 #include "qseries.h"
5 #include "domain_p.h"
5 #include "domain_p.h"
6 #include <QVector>
6 #include <QVector>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class ChartDataSet : public QObject
12 class ChartDataSet : public QObject
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 ChartDataSet(QObject* parent=0);
16 ChartDataSet(QObject* parent=0);
17 virtual ~ChartDataSet();
17 virtual ~ChartDataSet();
18
18
19 void addSeries(QSeries* series,QChartAxis *axisY = 0);
19 void addSeries(QSeries* series,QChartAxis *axisY = 0);
20 void removeSeries(QSeries* series);
20 void removeSeries(QSeries* series);
21 void removeAllSeries();
21 void removeAllSeries();
22
22
23 void zoomInDomain(const QRectF& rect, const QSizeF& size);
23 void zoomInDomain(const QRectF& rect, const QSizeF& size);
24 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
24 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
25 void scrollDomain(int dx,int dy,const QSizeF& size);
25 void scrollDomain(int dx,int dy,const QSizeF& size);
26
26
27 int seriesCount(QSeries::QSeriesType type);
28
27 Domain* domain(QSeries* series) const;
29 Domain* domain(QSeries* series) const;
28 Domain* domain(QChartAxis* axis) const;
30 Domain* domain(QChartAxis* axis) const;
29 QChartAxis* axis(QSeries* series) const;
31 QChartAxis* axis(QSeries* series) const;
30
32
31 QChartAxis* axisX() const { return m_axisX;};
33 QChartAxis* axisX() const { return m_axisX;};
32 QChartAxis* axisY(QSeries* series = 0) const;
34 QChartAxis* axisY(QSeries* series = 0) const;
33
35
34 signals:
36 signals:
35 void seriesAdded(QSeries* series,Domain* domain);
37 void seriesAdded(QSeries* series,Domain* domain);
36 void seriesRemoved(QSeries* series);
38 void seriesRemoved(QSeries* series);
37 void axisAdded(QChartAxis* axis,Domain* domain);
39 void axisAdded(QChartAxis* axis,Domain* domain);
38 void axisRemoved(QChartAxis* axis);
40 void axisRemoved(QChartAxis* axis);
39
41
40 private:
42 private:
41 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
43 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
42 void calculateDomain(QSeries* series,Domain* domain) const;
44 void calculateDomain(QSeries* series,Domain* domain) const;
43
45
44 private:
46 private:
45 QMap<QSeries*, QChartAxis*> m_seriesAxisMap;
47 QMap<QSeries*, QChartAxis*> m_seriesAxisMap;
46 QMap<QChartAxis*, Domain*> m_axisDomainMap;
48 QMap<QChartAxis*, Domain*> m_axisDomainMap;
47 QChartAxis* m_axisX;
49 QChartAxis* m_axisX;
48 QChartAxis* m_axisY;
50 QChartAxis* m_axisY;
49
51
50 int m_domainIndex;
52 int m_domainIndex;
51 bool m_axisXInitialized;
53 bool m_axisXInitialized;
52 };
54 };
53
55
54 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
55
57
56 #endif /* CHARTENGINE_P_H_ */
58 #endif /* CHARTENGINE_P_H_ */
@@ -1,396 +1,396
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 "barpresenter_p.h"
19 #include "barpresenter_p.h"
20 #include "stackedbarpresenter_p.h"
20 #include "stackedbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
22 #include "linechartitem_p.h"
22 #include "linechartitem_p.h"
23 #include "piepresenter_p.h"
23 #include "piepresenter_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_marginSize(0),
35 m_marginSize(0),
36 m_rect(QRectF(QPoint(0,0),m_chart->size())),
36 m_rect(QRectF(QPoint(0,0),m_chart->size())),
37 m_options(QChart::NoAnimation)
37 m_options(QChart::NoAnimation)
38 {
38 {
39 createConnections();
39 createConnections();
40 setChartTheme(QChart::ChartThemeDefault);
40 setChartTheme(QChart::ChartThemeDefault);
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_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
66 rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
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 int ChartPresenter::margin() const
89 int ChartPresenter::margin() const
90 {
90 {
91 return m_marginSize;
91 return m_marginSize;
92 }
92 }
93
93
94 void ChartPresenter::setMargin(int margin)
94 void ChartPresenter::setMargin(int margin)
95 {
95 {
96 m_marginSize = margin;
96 m_marginSize = margin;
97 }
97 }
98
98
99 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
99 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
100 {
100 {
101 AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
101 AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
102
102
103 if(m_options.testFlag(QChart::GridAxisAnimations)){
103 if(m_options.testFlag(QChart::GridAxisAnimations)){
104 m_animator->addAnimation(item);
104 m_animator->addAnimation(item);
105 }
105 }
106
106
107 if(axis==m_dataset->axisX()){
107 if(axis==m_dataset->axisX()){
108 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
108 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
109 //initialize
109 //initialize
110 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
110 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
111 }
111 }
112 else{
112 else{
113 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
113 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
114 //initialize
114 //initialize
115 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
115 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
116 }
116 }
117
117
118 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
118 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
119 //initialize
119 //initialize
120 item->handleGeometryChanged(m_rect);
120 item->handleGeometryChanged(m_rect);
121 m_chartTheme->decorate(axis,item);
121 m_chartTheme->decorate(axis,item);
122 m_axisItems.insert(axis,item);
122 m_axisItems.insert(axis,item);
123
123
124 }
124 }
125
125
126 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
126 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
127 {
127 {
128 AxisItem* item = m_axisItems.take(axis);
128 AxisItem* item = m_axisItems.take(axis);
129 Q_ASSERT(item);
129 Q_ASSERT(item);
130 if(m_animator) m_animator->removeAnimation(item);
130 if(m_animator) m_animator->removeAnimation(item);
131 delete item;
131 delete item;
132 }
132 }
133
133
134
134
135 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
135 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
136 {
136 {
137 ChartItem *item = 0 ;
137 ChartItem *item = 0 ;
138
138
139 switch(series->type())
139 switch(series->type())
140 {
140 {
141 case QSeries::SeriesTypeLine: {
141 case QSeries::SeriesTypeLine: {
142
142
143 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
143 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
144 LineChartItem* line = new LineChartItem(lineSeries,m_chart);
144 LineChartItem* line = new LineChartItem(lineSeries,m_chart);
145 if(m_options.testFlag(QChart::SeriesAnimations)) {
145 if(m_options.testFlag(QChart::SeriesAnimations)) {
146 m_animator->addAnimation(line);
146 m_animator->addAnimation(line);
147 }
147 }
148 m_chartTheme->decorate(line,lineSeries,m_chartItems.count());
148 m_chartTheme->decorate(line,lineSeries,m_dataset->seriesCount(series->type()));
149 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
149 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
150 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
150 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
151 item = line;
151 item = line;
152 break;
152 break;
153 }
153 }
154
154
155 case QSeries::SeriesTypeArea: {
155 case QSeries::SeriesTypeArea: {
156
156
157 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
157 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
158 AreaChartItem* area = new AreaChartItem(areaSeries,m_chart);
158 AreaChartItem* area = new AreaChartItem(areaSeries,m_chart);
159 if(m_options.testFlag(QChart::SeriesAnimations)) {
159 if(m_options.testFlag(QChart::SeriesAnimations)) {
160 // m_animator->addAnimation(area);
160 // m_animator->addAnimation(area);
161 }
161 }
162 m_chartTheme->decorate(area,areaSeries,m_chartItems.count());
162 m_chartTheme->decorate(area,areaSeries,m_dataset->seriesCount(series->type()));
163 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
163 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
164 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
164 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
165 item=area;
165 item=area;
166 break;
166 break;
167 }
167 }
168
168
169 case QSeries::SeriesTypeBar: {
169 case QSeries::SeriesTypeBar: {
170 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
170 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
171 BarPresenter* bar = new BarPresenter(barSeries,m_chart);
171 BarPresenter* bar = new BarPresenter(barSeries,m_chart);
172 if(m_options.testFlag(QChart::SeriesAnimations)) {
172 if(m_options.testFlag(QChart::SeriesAnimations)) {
173 // m_animator->addAnimation(bar);
173 // m_animator->addAnimation(bar);
174 }
174 }
175 m_chartTheme->decorate(bar,barSeries,m_chartItems.count());
175 m_chartTheme->decorate(bar,barSeries,m_dataset->seriesCount(series->type()));
176 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
176 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
177 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
177 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
178 item=bar;
178 item=bar;
179 break;
179 break;
180 }
180 }
181
181
182 case QSeries::SeriesTypeStackedBar: {
182 case QSeries::SeriesTypeStackedBar: {
183 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
183 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
184 StackedBarPresenter* bar = new StackedBarPresenter(stackedBarSeries,m_chart);
184 StackedBarPresenter* bar = new StackedBarPresenter(stackedBarSeries,m_chart);
185 if(m_options.testFlag(QChart::SeriesAnimations)) {
185 if(m_options.testFlag(QChart::SeriesAnimations)) {
186 // m_animator->addAnimation(bar);
186 // m_animator->addAnimation(bar);
187 }
187 }
188 m_chartTheme->decorate(bar,stackedBarSeries,m_chartItems.count());
188 m_chartTheme->decorate(bar,stackedBarSeries,m_dataset->seriesCount(series->type()));
189 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
189 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
190 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
190 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
191 item=bar;
191 item=bar;
192 break;
192 break;
193 }
193 }
194
194
195 case QSeries::SeriesTypePercentBar: {
195 case QSeries::SeriesTypePercentBar: {
196 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
196 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
197 PercentBarPresenter* bar = new PercentBarPresenter(percentBarSeries,m_chart);
197 PercentBarPresenter* bar = new PercentBarPresenter(percentBarSeries,m_chart);
198 if(m_options.testFlag(QChart::SeriesAnimations)) {
198 if(m_options.testFlag(QChart::SeriesAnimations)) {
199 // m_animator->addAnimation(bar);
199 // m_animator->addAnimation(bar);
200 }
200 }
201 m_chartTheme->decorate(bar,percentBarSeries ,m_chartItems.count());
201 m_chartTheme->decorate(bar,percentBarSeries ,m_dataset->seriesCount(series->type()));
202 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
202 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
203 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
203 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
204 item=bar;
204 item=bar;
205 break;
205 break;
206 }
206 }
207
207
208 case QSeries::SeriesTypeScatter: {
208 case QSeries::SeriesTypeScatter: {
209 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
209 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
210 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart);
210 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart);
211 if(m_options.testFlag(QChart::SeriesAnimations)) {
211 if(m_options.testFlag(QChart::SeriesAnimations)) {
212 m_animator->addAnimation(scatter);
212 m_animator->addAnimation(scatter);
213 }
213 }
214 m_chartTheme->decorate(scatter, scatterSeries, m_chartItems.count());
214 m_chartTheme->decorate(scatter, scatterSeries, m_dataset->seriesCount(series->type()));
215 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
215 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
216 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
216 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
217 item=scatter;
217 item=scatter;
218 break;
218 break;
219 }
219 }
220
220
221 case QSeries::SeriesTypePie: {
221 case QSeries::SeriesTypePie: {
222 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
222 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
223 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
223 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
224 if(m_options.testFlag(QChart::SeriesAnimations)) {
224 if(m_options.testFlag(QChart::SeriesAnimations)) {
225 // m_animator->addAnimation(pie);
225 // m_animator->addAnimation(pie);
226 }
226 }
227 m_chartTheme->decorate(pie, pieSeries, m_chartItems.count());
227 m_chartTheme->decorate(pie, pieSeries, m_dataset->seriesCount(series->type()));
228 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
228 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
229 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
229 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
230 // Hide all from background when there is only piechart
230 // Hide all from background when there is only piechart
231 // TODO: refactor this ugly code... should be one setting for this
231 // TODO: refactor this ugly code... should be one setting for this
232 if (m_chartItems.count() == 0) {
232 if (m_chartItems.count() == 0) {
233 m_chart->axisX()->setAxisVisible(false);
233 m_chart->axisX()->setAxisVisible(false);
234 m_chart->axisY()->setAxisVisible(false);
234 m_chart->axisY()->setAxisVisible(false);
235 m_chart->axisX()->setGridVisible(false);
235 m_chart->axisX()->setGridVisible(false);
236 m_chart->axisY()->setGridVisible(false);
236 m_chart->axisY()->setGridVisible(false);
237 m_chart->axisX()->setLabelsVisible(false);
237 m_chart->axisX()->setLabelsVisible(false);
238 m_chart->axisY()->setLabelsVisible(false);
238 m_chart->axisY()->setLabelsVisible(false);
239 m_chart->axisX()->setShadesVisible(false);
239 m_chart->axisX()->setShadesVisible(false);
240 m_chart->axisY()->setShadesVisible(false);
240 m_chart->axisY()->setShadesVisible(false);
241 m_chart->setChartBackgroundBrush(Qt::transparent);
241 m_chart->setChartBackgroundBrush(Qt::transparent);
242 }
242 }
243 item=pie;
243 item=pie;
244 break;
244 break;
245 }
245 }
246
246
247 case QSeries::SeriesTypeSpline: {
247 case QSeries::SeriesTypeSpline: {
248
248
249 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
249 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
250 SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart);
250 SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart);
251 if(m_options.testFlag(QChart::SeriesAnimations)) {
251 if(m_options.testFlag(QChart::SeriesAnimations)) {
252 m_animator->addAnimation(spline);
252 m_animator->addAnimation(spline);
253 }
253 }
254 m_chartTheme->decorate(spline, splineSeries, m_chartItems.count());
254 m_chartTheme->decorate(spline, splineSeries, m_dataset->seriesCount(series->type()));
255 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
255 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
256 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
256 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
257 item=spline;
257 item=spline;
258 break;
258 break;
259 }
259 }
260 default: {
260 default: {
261 qDebug()<< "Series type" << series->type() << "not implemented.";
261 qDebug()<< "Series type" << series->type() << "not implemented.";
262 break;
262 break;
263 }
263 }
264 }
264 }
265
265
266 //initialize
266 //initialize
267 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
267 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
268 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
268 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
269 m_chartItems.insert(series,item);
269 m_chartItems.insert(series,item);
270 zoomReset();
270 zoomReset();
271 }
271 }
272
272
273 void ChartPresenter::handleSeriesRemoved(QSeries* series)
273 void ChartPresenter::handleSeriesRemoved(QSeries* series)
274 {
274 {
275 ChartItem* item = m_chartItems.take(series);
275 ChartItem* item = m_chartItems.take(series);
276 Q_ASSERT(item);
276 Q_ASSERT(item);
277 if(m_animator) m_animator->removeAnimation(item);
277 if(m_animator) m_animator->removeAnimation(item);
278 delete item;
278 delete item;
279 }
279 }
280
280
281 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
281 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
282 {
282 {
283 if(m_chartTheme && m_chartTheme->id() == theme) return;
283 if(m_chartTheme && m_chartTheme->id() == theme) return;
284 delete m_chartTheme;
284 delete m_chartTheme;
285 m_chartTheme = ChartTheme::createTheme(theme);
285 m_chartTheme = ChartTheme::createTheme(theme);
286 m_chartTheme->decorate(m_chart);
286 m_chartTheme->decorate(m_chart);
287 resetAllElements();
287 resetAllElements();
288 }
288 }
289
289
290 QChart::ChartTheme ChartPresenter::chartTheme()
290 QChart::ChartTheme ChartPresenter::chartTheme()
291 {
291 {
292 return m_chartTheme->id();
292 return m_chartTheme->id();
293 }
293 }
294
294
295 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
295 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
296 {
296 {
297 if(m_options!=options) {
297 if(m_options!=options) {
298
298
299 m_options=options;
299 m_options=options;
300
300
301 if(m_options!=QChart::NoAnimation && !m_animator) {
301 if(m_options!=QChart::NoAnimation && !m_animator) {
302 m_animator= new ChartAnimator(this);
302 m_animator= new ChartAnimator(this);
303
303
304 }
304 }
305 resetAllElements();
305 resetAllElements();
306 }
306 }
307
307
308 }
308 }
309
309
310 void ChartPresenter::resetAllElements()
310 void ChartPresenter::resetAllElements()
311 {
311 {
312 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
312 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
313 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
313 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
314
314
315 foreach(QChartAxis* axis, axisList) {
315 foreach(QChartAxis* axis, axisList) {
316 handleAxisRemoved(axis);
316 handleAxisRemoved(axis);
317 handleAxisAdded(axis,m_dataset->domain(axis));
317 handleAxisAdded(axis,m_dataset->domain(axis));
318 }
318 }
319 foreach(QSeries* series, seriesList) {
319 foreach(QSeries* series, seriesList) {
320 handleSeriesRemoved(series);
320 handleSeriesRemoved(series);
321 handleSeriesAdded(series,m_dataset->domain(series));
321 handleSeriesAdded(series,m_dataset->domain(series));
322 }
322 }
323 }
323 }
324
324
325 void ChartPresenter::zoomIn()
325 void ChartPresenter::zoomIn()
326 {
326 {
327 QRectF rect = geometry();
327 QRectF rect = geometry();
328 rect.setWidth(rect.width()/2);
328 rect.setWidth(rect.width()/2);
329 rect.setHeight(rect.height()/2);
329 rect.setHeight(rect.height()/2);
330 rect.moveCenter(geometry().center());
330 rect.moveCenter(geometry().center());
331 zoomIn(rect);
331 zoomIn(rect);
332 }
332 }
333
333
334 void ChartPresenter::zoomIn(const QRectF& rect)
334 void ChartPresenter::zoomIn(const QRectF& rect)
335 {
335 {
336 QRectF r = rect.normalized();
336 QRectF r = rect.normalized();
337 r.translate(-m_marginSize, -m_marginSize);
337 r.translate(-m_marginSize, -m_marginSize);
338 if(m_animator) {
338 if(m_animator) {
339
339
340 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
340 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
341 m_animator->setState(ChartAnimator::ZoomInState,point);
341 m_animator->setState(ChartAnimator::ZoomInState,point);
342 }
342 }
343 m_dataset->zoomInDomain(r,geometry().size());
343 m_dataset->zoomInDomain(r,geometry().size());
344 m_zoomStack<<r;
344 m_zoomStack<<r;
345 m_zoomIndex++;
345 m_zoomIndex++;
346 if(m_animator) {
346 if(m_animator) {
347 m_animator->setState(ChartAnimator::ShowState);
347 m_animator->setState(ChartAnimator::ShowState);
348 }
348 }
349 }
349 }
350
350
351 void ChartPresenter::zoomOut()
351 void ChartPresenter::zoomOut()
352 {
352 {
353 if(m_zoomIndex==0) return;
353 if(m_zoomIndex==0) return;
354 if(m_animator)
354 if(m_animator)
355 {
355 {
356 m_animator->setState(ChartAnimator::ZoomOutState);
356 m_animator->setState(ChartAnimator::ZoomOutState);
357 }
357 }
358 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
358 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
359 m_zoomIndex--;
359 m_zoomIndex--;
360 m_zoomStack.resize(m_zoomIndex);
360 m_zoomStack.resize(m_zoomIndex);
361 if(m_animator){
361 if(m_animator){
362 m_animator->setState(ChartAnimator::ShowState);
362 m_animator->setState(ChartAnimator::ShowState);
363 }
363 }
364 }
364 }
365
365
366 void ChartPresenter::zoomReset()
366 void ChartPresenter::zoomReset()
367 {
367 {
368 m_zoomIndex=0;
368 m_zoomIndex=0;
369 m_zoomStack.resize(m_zoomIndex);
369 m_zoomStack.resize(m_zoomIndex);
370 }
370 }
371
371
372 void ChartPresenter::scroll(int dx,int dy)
372 void ChartPresenter::scroll(int dx,int dy)
373 {
373 {
374 if(m_animator){
374 if(m_animator){
375 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
375 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
376 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
376 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
377 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
377 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
378 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
378 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
379 }
379 }
380
380
381 m_dataset->scrollDomain(dx,dy,geometry().size());
381 m_dataset->scrollDomain(dx,dy,geometry().size());
382
382
383 if(m_animator){
383 if(m_animator){
384 m_animator->setState(ChartAnimator::ShowState);
384 m_animator->setState(ChartAnimator::ShowState);
385 }
385 }
386 }
386 }
387
387
388 QChart::AnimationOptions ChartPresenter::animationOptions() const
388 QChart::AnimationOptions ChartPresenter::animationOptions() const
389 {
389 {
390 return m_options;
390 return m_options;
391 }
391 }
392
392
393
393
394 #include "moc_chartpresenter_p.cpp"
394 #include "moc_chartpresenter_p.cpp"
395
395
396 QTCOMMERCIALCHART_END_NAMESPACE
396 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now