@@ -1,406 +1,411 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 | #include "chartpresenter_p.h" |
|
20 | #include "chartpresenter_p.h" | |
21 | #include "qchart.h" |
|
21 | #include "qchart.h" | |
22 | #include "qchart_p.h" |
|
22 | #include "qchart_p.h" | |
23 | #include "qaxis.h" |
|
23 | #include "qaxis.h" | |
24 | #include "chartdataset_p.h" |
|
24 | #include "chartdataset_p.h" | |
25 | #include "charttheme_p.h" |
|
25 | #include "charttheme_p.h" | |
26 | #include "chartanimator_p.h" |
|
26 | #include "chartanimator_p.h" | |
27 | #include "qabstractseries_p.h" |
|
27 | #include "qabstractseries_p.h" | |
28 | #include "qareaseries.h" |
|
28 | #include "qareaseries.h" | |
29 | #include "chartaxis_p.h" |
|
29 | #include "chartaxis_p.h" | |
30 | #include "areachartitem_p.h" |
|
30 | #include "areachartitem_p.h" | |
31 | #include "chartbackground_p.h" |
|
31 | #include "chartbackground_p.h" | |
32 |
|
32 | |||
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
34 |
|
34 | |||
35 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
35 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | |
36 | m_chart(chart), |
|
36 | m_chart(chart), | |
37 | m_animator(0), |
|
37 | m_animator(0), | |
38 | m_dataset(dataset), |
|
38 | m_dataset(dataset), | |
39 | m_chartTheme(0), |
|
39 | m_chartTheme(0), | |
40 | m_chartRect(QRectF(QPoint(0,0),m_chart->size())), |
|
40 | m_chartRect(QRectF(QPoint(0,0),m_chart->size())), | |
41 | m_options(QChart::NoAnimation), |
|
41 | m_options(QChart::NoAnimation), | |
42 | m_minLeftMargin(0), |
|
42 | m_minLeftMargin(0), | |
43 | m_minBottomMargin(0), |
|
43 | m_minBottomMargin(0), | |
44 | m_backgroundItem(0), |
|
44 | m_backgroundItem(0), | |
45 | m_titleItem(0), |
|
45 | m_titleItem(0), | |
46 | m_marginBig(60), |
|
46 | m_marginBig(60), | |
47 | m_marginSmall(20), |
|
47 | m_marginSmall(20), | |
48 | m_marginTiny(10), |
|
48 | m_marginTiny(10), | |
49 | m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) |
|
49 | m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) | |
50 | { |
|
50 | { | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | ChartPresenter::~ChartPresenter() |
|
53 | ChartPresenter::~ChartPresenter() | |
54 | { |
|
54 | { | |
55 | delete m_chartTheme; |
|
55 | delete m_chartTheme; | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void ChartPresenter::setGeometry(const QRectF& rect) |
|
58 | void ChartPresenter::setGeometry(const QRectF& rect) | |
59 | { |
|
59 | { | |
60 | m_rect = rect; |
|
60 | m_rect = rect; | |
61 | Q_ASSERT(m_rect.isValid()); |
|
61 | Q_ASSERT(m_rect.isValid()); | |
62 | updateLayout(); |
|
62 | updateLayout(); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width) |
|
65 | void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width) | |
66 | { |
|
66 | { | |
67 | switch(axis->axisType()){ |
|
67 | switch(axis->axisType()){ | |
68 | case ChartAxis::X_AXIS: |
|
68 | case ChartAxis::X_AXIS: | |
69 | { |
|
69 | { | |
70 | if(width>m_chartRect.width()+ m_chartMargins.left()) { |
|
70 | if(width>m_chartRect.width()+ m_chartMargins.left()) { | |
71 | m_minLeftMargin= width - m_chartRect.width(); |
|
71 | m_minLeftMargin= width - m_chartRect.width(); | |
72 | updateLayout(); |
|
72 | updateLayout(); | |
73 | } |
|
73 | } | |
74 | break; |
|
74 | break; | |
75 | } |
|
75 | } | |
76 | case ChartAxis::Y_AXIS: |
|
76 | case ChartAxis::Y_AXIS: | |
77 | { |
|
77 | { | |
78 |
|
78 | |||
79 | if(m_minLeftMargin!=width){ |
|
79 | if(m_minLeftMargin!=width){ | |
80 | m_minLeftMargin= width; |
|
80 | m_minLeftMargin= width; | |
81 | updateLayout(); |
|
81 | updateLayout(); | |
82 | } |
|
82 | } | |
83 | break; |
|
83 | break; | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | } |
|
86 | } | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height) |
|
89 | void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height) | |
90 | { |
|
90 | { | |
91 | switch(axis->axisType()){ |
|
91 | switch(axis->axisType()){ | |
92 | case ChartAxis::X_AXIS: |
|
92 | case ChartAxis::X_AXIS: | |
93 | { |
|
93 | { | |
94 | if(m_minBottomMargin!=height) { |
|
94 | if(m_minBottomMargin!=height) { | |
95 | m_minBottomMargin= height; |
|
95 | m_minBottomMargin= height; | |
96 | updateLayout(); |
|
96 | updateLayout(); | |
97 | } |
|
97 | } | |
98 | break; |
|
98 | break; | |
99 | } |
|
99 | } | |
100 | case ChartAxis::Y_AXIS: |
|
100 | case ChartAxis::Y_AXIS: | |
101 | { |
|
101 | { | |
102 |
|
102 | |||
103 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ |
|
103 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ | |
104 | m_minBottomMargin= height - m_chartRect.height(); |
|
104 | m_minBottomMargin= height - m_chartRect.height(); | |
105 | updateLayout(); |
|
105 | updateLayout(); | |
106 | } |
|
106 | } | |
107 | break; |
|
107 | break; | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 | } |
|
110 | } | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain) |
|
113 | void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain) | |
114 | { |
|
114 | { | |
115 | ChartAxis* item = new ChartAxis(axis,this,axis==m_dataset->axisX()?ChartAxis::X_AXIS : ChartAxis::Y_AXIS); |
|
115 | ChartAxis* item = new ChartAxis(axis,this,axis==m_dataset->axisX()?ChartAxis::X_AXIS : ChartAxis::Y_AXIS); | |
116 |
|
116 | |||
117 | if(m_options.testFlag(QChart::GridAxisAnimations)){ |
|
117 | if(m_options.testFlag(QChart::GridAxisAnimations)){ | |
118 | m_animator->addAnimation(item); |
|
118 | m_animator->addAnimation(item); | |
119 | } |
|
119 | } | |
120 |
|
120 | |||
121 | if(axis==m_dataset->axisX()){ |
|
121 | if(axis==m_dataset->axisX()){ | |
122 | m_chartTheme->decorate(axis,true); |
|
122 | m_chartTheme->decorate(axis,true); | |
123 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
123 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | |
124 | //initialize |
|
124 | //initialize | |
125 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); |
|
125 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); | |
126 |
|
126 | |||
127 | } |
|
127 | } | |
128 | else{ |
|
128 | else{ | |
129 | m_chartTheme->decorate(axis,false); |
|
129 | m_chartTheme->decorate(axis,false); | |
130 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
130 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | |
131 | //initialize |
|
131 | //initialize | |
132 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); |
|
132 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); | |
133 | } |
|
133 | } | |
134 |
|
134 | |||
135 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); |
|
135 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); | |
136 | //initialize |
|
136 | //initialize | |
137 | item->handleGeometryChanged(m_chartRect); |
|
137 | item->handleGeometryChanged(m_chartRect); | |
138 | m_axisItems.insert(axis, item); |
|
138 | m_axisItems.insert(axis, item); | |
139 | } |
|
139 | } | |
140 |
|
140 | |||
141 | void ChartPresenter::handleAxisRemoved(QAxis* axis) |
|
141 | void ChartPresenter::handleAxisRemoved(QAxis* axis) | |
142 | { |
|
142 | { | |
143 | ChartAxis* item = m_axisItems.take(axis); |
|
143 | ChartAxis* item = m_axisItems.take(axis); | |
144 | Q_ASSERT(item); |
|
144 | Q_ASSERT(item); | |
145 | if(m_animator) m_animator->removeAnimation(item); |
|
145 | if(m_animator) m_animator->removeAnimation(item); | |
146 | delete item; |
|
146 | delete item; | |
147 | } |
|
147 | } | |
148 |
|
148 | |||
149 |
|
149 | |||
150 | void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain) |
|
150 | void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain) | |
151 | { |
|
151 | { | |
152 | Chart *item = series->d_ptr->createGraphics(this); |
|
152 | Chart *item = series->d_ptr->createGraphics(this); | |
153 | Q_ASSERT(item); |
|
153 | Q_ASSERT(item); | |
154 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); |
|
154 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); | |
155 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
155 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
156 | //initialize |
|
156 | //initialize | |
157 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
157 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | |
158 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); |
|
158 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); | |
159 | m_chartItems.insert(series,item); |
|
159 | m_chartItems.insert(series,item); | |
160 | } |
|
160 | } | |
161 |
|
161 | |||
162 | void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series) |
|
162 | void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series) | |
163 | { |
|
163 | { | |
164 | Chart* item = m_chartItems.take(series); |
|
164 | Chart* item = m_chartItems.take(series); | |
165 | Q_ASSERT(item); |
|
165 | Q_ASSERT(item); | |
166 | if(m_animator) { |
|
166 | if(m_animator) { | |
167 | //small hack to handle area animations |
|
167 | //small hack to handle area animations | |
168 | if(series->type() == QAbstractSeries::SeriesTypeArea){ |
|
168 | if(series->type() == QAbstractSeries::SeriesTypeArea){ | |
169 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
169 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |
170 | AreaChartItem* area = static_cast<AreaChartItem*>(item); |
|
170 | AreaChartItem* area = static_cast<AreaChartItem*>(item); | |
171 | m_animator->removeAnimation(area->upperLineItem()); |
|
171 | m_animator->removeAnimation(area->upperLineItem()); | |
172 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); |
|
172 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); | |
173 | }else |
|
173 | }else | |
174 | m_animator->removeAnimation(item); |
|
174 | m_animator->removeAnimation(item); | |
175 | } |
|
175 | } | |
176 | delete item; |
|
176 | delete item; | |
177 | } |
|
177 | } | |
178 |
|
178 | |||
179 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) |
|
179 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) | |
180 | { |
|
180 | { | |
181 | if(m_chartTheme && m_chartTheme->id() == theme) return; |
|
181 | if(m_chartTheme && m_chartTheme->id() == theme) return; | |
182 | delete m_chartTheme; |
|
182 | delete m_chartTheme; | |
183 | m_chartTheme = ChartTheme::createTheme(theme); |
|
183 | m_chartTheme = ChartTheme::createTheme(theme); | |
184 | m_chartTheme->setForced(force); |
|
184 | m_chartTheme->setForced(force); | |
185 | m_chartTheme->decorate(m_chart); |
|
185 | m_chartTheme->decorate(m_chart); | |
186 | m_chartTheme->decorate(m_chart->legend()); |
|
186 | m_chartTheme->decorate(m_chart->legend()); | |
187 | resetAllElements(); |
|
187 | resetAllElements(); | |
|
188 | ||||
|
189 | // We do not want "force" to stay on. | |||
|
190 | // Bar/pie are calling decorate when adding/removing slices/bars which means | |||
|
191 | // that to preserve users colors "force" must not be on. | |||
|
192 | m_chartTheme->setForced(false); | |||
188 | } |
|
193 | } | |
189 |
|
194 | |||
190 | QChart::ChartTheme ChartPresenter::theme() |
|
195 | QChart::ChartTheme ChartPresenter::theme() | |
191 | { |
|
196 | { | |
192 | return m_chartTheme->id(); |
|
197 | return m_chartTheme->id(); | |
193 | } |
|
198 | } | |
194 |
|
199 | |||
195 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
200 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | |
196 | { |
|
201 | { | |
197 | if(m_options!=options) { |
|
202 | if(m_options!=options) { | |
198 |
|
203 | |||
199 | m_options=options; |
|
204 | m_options=options; | |
200 |
|
205 | |||
201 | if(m_options!=QChart::NoAnimation && !m_animator) { |
|
206 | if(m_options!=QChart::NoAnimation && !m_animator) { | |
202 | m_animator= new ChartAnimator(this); |
|
207 | m_animator= new ChartAnimator(this); | |
203 | } |
|
208 | } | |
204 | resetAllElements(); |
|
209 | resetAllElements(); | |
205 | } |
|
210 | } | |
206 |
|
211 | |||
207 | } |
|
212 | } | |
208 |
|
213 | |||
209 | void ChartPresenter::resetAllElements() |
|
214 | void ChartPresenter::resetAllElements() | |
210 | { |
|
215 | { | |
211 | QList<QAxis *> axisList = m_axisItems.uniqueKeys(); |
|
216 | QList<QAxis *> axisList = m_axisItems.uniqueKeys(); | |
212 | QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys(); |
|
217 | QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys(); | |
213 |
|
218 | |||
214 | foreach(QAxis *axis, axisList) { |
|
219 | foreach(QAxis *axis, axisList) { | |
215 | handleAxisRemoved(axis); |
|
220 | handleAxisRemoved(axis); | |
216 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
221 | handleAxisAdded(axis,m_dataset->domain(axis)); | |
217 | } |
|
222 | } | |
218 | foreach(QAbstractSeries *series, seriesList) { |
|
223 | foreach(QAbstractSeries *series, seriesList) { | |
219 | handleSeriesRemoved(series); |
|
224 | handleSeriesRemoved(series); | |
220 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
225 | handleSeriesAdded(series,m_dataset->domain(series)); | |
221 | // m_dataset->removeSeries(series); |
|
226 | // m_dataset->removeSeries(series); | |
222 | // m_dataset->addSeries(series); |
|
227 | // m_dataset->addSeries(series); | |
223 | } |
|
228 | } | |
224 | } |
|
229 | } | |
225 |
|
230 | |||
226 | void ChartPresenter::zoomIn() |
|
231 | void ChartPresenter::zoomIn() | |
227 | { |
|
232 | { | |
228 | QRectF rect = chartGeometry(); |
|
233 | QRectF rect = chartGeometry(); | |
229 | rect.setWidth(rect.width()/2); |
|
234 | rect.setWidth(rect.width()/2); | |
230 | rect.setHeight(rect.height()/2); |
|
235 | rect.setHeight(rect.height()/2); | |
231 | rect.moveCenter(chartGeometry().center()); |
|
236 | rect.moveCenter(chartGeometry().center()); | |
232 | zoomIn(rect); |
|
237 | zoomIn(rect); | |
233 | } |
|
238 | } | |
234 |
|
239 | |||
235 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
240 | void ChartPresenter::zoomIn(const QRectF& rect) | |
236 | { |
|
241 | { | |
237 | QRectF r = rect.normalized(); |
|
242 | QRectF r = rect.normalized(); | |
238 | r.translate(-m_chartMargins.topLeft()); |
|
243 | r.translate(-m_chartMargins.topLeft()); | |
239 | if(!r.isValid()) return; |
|
244 | if(!r.isValid()) return; | |
240 | if(m_animator) { |
|
245 | if(m_animator) { | |
241 |
|
246 | |||
242 | QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); |
|
247 | QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); | |
243 | m_animator->setState(ChartAnimator::ZoomInState,point); |
|
248 | m_animator->setState(ChartAnimator::ZoomInState,point); | |
244 | } |
|
249 | } | |
245 | m_dataset->zoomInDomain(r,chartGeometry().size()); |
|
250 | m_dataset->zoomInDomain(r,chartGeometry().size()); | |
246 | if(m_animator) { |
|
251 | if(m_animator) { | |
247 | m_animator->setState(ChartAnimator::ShowState); |
|
252 | m_animator->setState(ChartAnimator::ShowState); | |
248 | } |
|
253 | } | |
249 | } |
|
254 | } | |
250 |
|
255 | |||
251 | void ChartPresenter::zoomOut() |
|
256 | void ChartPresenter::zoomOut() | |
252 | { |
|
257 | { | |
253 | if(m_animator) |
|
258 | if(m_animator) | |
254 | { |
|
259 | { | |
255 | m_animator->setState(ChartAnimator::ZoomOutState); |
|
260 | m_animator->setState(ChartAnimator::ZoomOutState); | |
256 | } |
|
261 | } | |
257 |
|
262 | |||
258 | QSizeF size = chartGeometry().size(); |
|
263 | QSizeF size = chartGeometry().size(); | |
259 | QRectF rect = chartGeometry(); |
|
264 | QRectF rect = chartGeometry(); | |
260 | rect.translate(-m_chartMargins.topLeft()); |
|
265 | rect.translate(-m_chartMargins.topLeft()); | |
261 | if(!rect.isValid()) return; |
|
266 | if(!rect.isValid()) return; | |
262 | m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); |
|
267 | m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); | |
263 | //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); |
|
268 | //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); | |
264 |
|
269 | |||
265 | if(m_animator){ |
|
270 | if(m_animator){ | |
266 | m_animator->setState(ChartAnimator::ShowState); |
|
271 | m_animator->setState(ChartAnimator::ShowState); | |
267 | } |
|
272 | } | |
268 | } |
|
273 | } | |
269 |
|
274 | |||
270 | void ChartPresenter::scroll(int dx,int dy) |
|
275 | void ChartPresenter::scroll(int dx,int dy) | |
271 | { |
|
276 | { | |
272 | if(m_animator){ |
|
277 | if(m_animator){ | |
273 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); |
|
278 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); | |
274 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); |
|
279 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); | |
275 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); |
|
280 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); | |
276 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); |
|
281 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); | |
277 | } |
|
282 | } | |
278 |
|
283 | |||
279 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); |
|
284 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); | |
280 |
|
285 | |||
281 | if(m_animator){ |
|
286 | if(m_animator){ | |
282 | m_animator->setState(ChartAnimator::ShowState); |
|
287 | m_animator->setState(ChartAnimator::ShowState); | |
283 | } |
|
288 | } | |
284 | } |
|
289 | } | |
285 |
|
290 | |||
286 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
291 | QChart::AnimationOptions ChartPresenter::animationOptions() const | |
287 | { |
|
292 | { | |
288 | return m_options; |
|
293 | return m_options; | |
289 | } |
|
294 | } | |
290 |
|
295 | |||
291 | void ChartPresenter::updateLayout() |
|
296 | void ChartPresenter::updateLayout() | |
292 | { |
|
297 | { | |
293 | if (!m_rect.isValid()) return; |
|
298 | if (!m_rect.isValid()) return; | |
294 |
|
299 | |||
295 | // recalculate title size |
|
300 | // recalculate title size | |
296 |
|
301 | |||
297 | QSize titleSize; |
|
302 | QSize titleSize; | |
298 | int titlePadding=0; |
|
303 | int titlePadding=0; | |
299 |
|
304 | |||
300 | if (m_titleItem) { |
|
305 | if (m_titleItem) { | |
301 | titleSize= m_titleItem->boundingRect().size().toSize(); |
|
306 | titleSize= m_titleItem->boundingRect().size().toSize(); | |
302 | } |
|
307 | } | |
303 |
|
308 | |||
304 | //defaults |
|
309 | //defaults | |
305 | m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); |
|
310 | m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); | |
306 | titlePadding = m_chartMargins.top()/2; |
|
311 | titlePadding = m_chartMargins.top()/2; | |
307 |
|
312 | |||
308 | QLegend* legend = m_chart->d_ptr->m_legend; |
|
313 | QLegend* legend = m_chart->d_ptr->m_legend; | |
309 |
|
314 | |||
310 | // recalculate legend position |
|
315 | // recalculate legend position | |
311 | if (legend->isAttachedToChart() && legend->isEnabled()) { |
|
316 | if (legend->isAttachedToChart() && legend->isEnabled()) { | |
312 |
|
317 | |||
313 | QRect legendRect; |
|
318 | QRect legendRect; | |
314 |
|
319 | |||
315 | // Reserve some space for legend |
|
320 | // Reserve some space for legend | |
316 | switch (legend->alignment()) { |
|
321 | switch (legend->alignment()) { | |
317 |
|
322 | |||
318 | case QLegend::AlignmentTop: { |
|
323 | case QLegend::AlignmentTop: { | |
319 | int ledgendSize = legend->minHeight(); |
|
324 | int ledgendSize = legend->minHeight(); | |
320 | int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; |
|
325 | int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; | |
321 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
326 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); | |
322 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); |
|
327 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); | |
323 | titlePadding = m_marginTiny + m_marginTiny; |
|
328 | titlePadding = m_marginTiny + m_marginTiny; | |
324 | break; |
|
329 | break; | |
325 | } |
|
330 | } | |
326 | case QLegend::AlignmentBottom: { |
|
331 | case QLegend::AlignmentBottom: { | |
327 | int ledgendSize = legend->minHeight(); |
|
332 | int ledgendSize = legend->minHeight(); | |
328 | int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; |
|
333 | int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; | |
329 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); |
|
334 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); | |
330 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); |
|
335 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); | |
331 | titlePadding = m_chartMargins.top()/2; |
|
336 | titlePadding = m_chartMargins.top()/2; | |
332 | break; |
|
337 | break; | |
333 | } |
|
338 | } | |
334 | case QLegend::AlignmentLeft: { |
|
339 | case QLegend::AlignmentLeft: { | |
335 | int ledgendSize = legend->minWidth(); |
|
340 | int ledgendSize = legend->minWidth(); | |
336 | int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; |
|
341 | int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; | |
337 | m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
342 | m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); | |
338 | m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); |
|
343 | m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); | |
339 | titlePadding = m_chartMargins.top()/2; |
|
344 | titlePadding = m_chartMargins.top()/2; | |
340 | break; |
|
345 | break; | |
341 | } |
|
346 | } | |
342 | case QLegend::AlignmentRight: { |
|
347 | case QLegend::AlignmentRight: { | |
343 | int ledgendSize = legend->minWidth(); |
|
348 | int ledgendSize = legend->minWidth(); | |
344 | int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; |
|
349 | int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; | |
345 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); |
|
350 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); | |
346 | m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); |
|
351 | m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); | |
347 | titlePadding = m_chartMargins.top()/2; |
|
352 | titlePadding = m_chartMargins.top()/2; | |
348 | break; |
|
353 | break; | |
349 | } |
|
354 | } | |
350 | default: { |
|
355 | default: { | |
351 | break; |
|
356 | break; | |
352 | } |
|
357 | } | |
353 | } |
|
358 | } | |
354 | } |
|
359 | } | |
355 |
|
360 | |||
356 | if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom())) |
|
361 | if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom())) | |
357 | { |
|
362 | { | |
358 | m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); |
|
363 | m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); | |
359 | return; |
|
364 | return; | |
360 | } |
|
365 | } | |
361 |
|
366 | |||
362 |
|
367 | |||
363 | // recalculate title position |
|
368 | // recalculate title position | |
364 | if (m_titleItem) { |
|
369 | if (m_titleItem) { | |
365 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
370 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | |
366 | m_titleItem->setPos(center.x(),titlePadding); |
|
371 | m_titleItem->setPos(center.x(),titlePadding); | |
367 | } |
|
372 | } | |
368 |
|
373 | |||
369 | //recalculate background gradient |
|
374 | //recalculate background gradient | |
370 | if (m_backgroundItem) { |
|
375 | if (m_backgroundItem) { | |
371 | m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); |
|
376 | m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); | |
372 | } |
|
377 | } | |
373 |
|
378 | |||
374 |
|
379 | |||
375 | QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); |
|
380 | QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); | |
376 |
|
381 | |||
377 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); |
|
382 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); | |
378 |
|
383 | |||
379 | if(m_chartRect!=chartRect){ |
|
384 | if(m_chartRect!=chartRect){ | |
380 | m_chartRect=chartRect; |
|
385 | m_chartRect=chartRect; | |
381 | emit geometryChanged(m_chartRect); |
|
386 | emit geometryChanged(m_chartRect); | |
382 | } |
|
387 | } | |
383 |
|
388 | |||
384 |
|
389 | |||
385 | } |
|
390 | } | |
386 |
|
391 | |||
387 | void ChartPresenter::createChartBackgroundItem() |
|
392 | void ChartPresenter::createChartBackgroundItem() | |
388 | { |
|
393 | { | |
389 | if (!m_backgroundItem) { |
|
394 | if (!m_backgroundItem) { | |
390 | m_backgroundItem = new ChartBackground(rootItem()); |
|
395 | m_backgroundItem = new ChartBackground(rootItem()); | |
391 | m_backgroundItem->setPen(Qt::NoPen); |
|
396 | m_backgroundItem->setPen(Qt::NoPen); | |
392 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
397 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | |
393 | } |
|
398 | } | |
394 | } |
|
399 | } | |
395 |
|
400 | |||
396 | void ChartPresenter::createChartTitleItem() |
|
401 | void ChartPresenter::createChartTitleItem() | |
397 | { |
|
402 | { | |
398 | if (!m_titleItem) { |
|
403 | if (!m_titleItem) { | |
399 | m_titleItem = new QGraphicsSimpleTextItem(rootItem()); |
|
404 | m_titleItem = new QGraphicsSimpleTextItem(rootItem()); | |
400 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
405 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | |
401 | } |
|
406 | } | |
402 | } |
|
407 | } | |
403 |
|
408 | |||
404 | #include "moc_chartpresenter_p.cpp" |
|
409 | #include "moc_chartpresenter_p.cpp" | |
405 |
|
410 | |||
406 | QTCOMMERCIALCHART_END_NAMESPACE |
|
411 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now