##// END OF EJS Templates
Add minimum size back to chartview and qchart
Michal Klocek -
r913:d1751eb423f7
parent child
Show More
@@ -1,529 +1,537
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qchartaxis.h"
23 #include "qchartaxis.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 //series
28 //series
29 #include "qbarseries.h"
29 #include "qbarseries.h"
30 #include "qstackedbarseries.h"
30 #include "qstackedbarseries.h"
31 #include "qpercentbarseries.h"
31 #include "qpercentbarseries.h"
32 #include "qlineseries.h"
32 #include "qlineseries.h"
33 #include "qareaseries.h"
33 #include "qareaseries.h"
34 #include "qpieseries.h"
34 #include "qpieseries.h"
35 #include "qscatterseries.h"
35 #include "qscatterseries.h"
36 #include "qsplineseries.h"
36 #include "qsplineseries.h"
37 //items
37 //items
38 #include "axisitem_p.h"
38 #include "axisitem_p.h"
39 #include "areachartitem_p.h"
39 #include "areachartitem_p.h"
40 #include "barchartitem_p.h"
40 #include "barchartitem_p.h"
41 #include "stackedbarchartitem_p.h"
41 #include "stackedbarchartitem_p.h"
42 #include "percentbarchartitem_p.h"
42 #include "percentbarchartitem_p.h"
43 #include "linechartitem_p.h"
43 #include "linechartitem_p.h"
44 #include "piechartitem_p.h"
44 #include "piechartitem_p.h"
45 #include "scatterchartitem_p.h"
45 #include "scatterchartitem_p.h"
46 #include "splinechartitem_p.h"
46 #include "splinechartitem_p.h"
47
47
48 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48 QTCOMMERCIALCHART_BEGIN_NAMESPACE
49
49
50 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
50 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
51 m_chart(chart),
51 m_chart(chart),
52 m_animator(0),
52 m_animator(0),
53 m_dataset(dataset),
53 m_dataset(dataset),
54 m_chartTheme(0),
54 m_chartTheme(0),
55 m_chartRect(QRectF(QPoint(0,0),m_chart->size())),
55 m_chartRect(QRectF(QPoint(0,0),m_chart->size())),
56 m_options(QChart::NoAnimation),
56 m_options(QChart::NoAnimation),
57 m_themeForce(false),
57 m_themeForce(false),
58 m_minLeftMargin(0),
58 m_minLeftMargin(0),
59 m_minBottomMargin(0),
59 m_minBottomMargin(0),
60 m_backgroundItem(0),
60 m_backgroundItem(0),
61 m_titleItem(0),
61 m_titleItem(0),
62 m_marginBig(60),
62 m_marginBig(60),
63 m_marginSmall(20),
63 m_marginSmall(20),
64 m_marginTiny(10),
64 m_marginTiny(10),
65 m_chartMargins(QRect(m_marginBig,m_marginBig,0,0))
65 m_chartMargins(QRect(m_marginBig,m_marginBig,0,0))
66 {
66 {
67 }
67 }
68
68
69 ChartPresenter::~ChartPresenter()
69 ChartPresenter::~ChartPresenter()
70 {
70 {
71 delete m_chartTheme;
71 delete m_chartTheme;
72 }
72 }
73
73
74 void ChartPresenter::setGeometry(const QRectF& rect)
74 void ChartPresenter::setGeometry(const QRectF& rect)
75 {
75 {
76 m_rect = rect;
76 m_rect = rect;
77 Q_ASSERT(m_rect.isValid());
77 Q_ASSERT(m_rect.isValid());
78 updateLayout();
78 updateLayout();
79 }
79 }
80
80
81 void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width)
81 void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width)
82 {
82 {
83 switch(axis->axisType()){
83 switch(axis->axisType()){
84 case Axis::X_AXIS:
84 case Axis::X_AXIS:
85 {
85 {
86 if(width>m_chartRect.width()+ m_chartMargins.left()) {
86 if(width>m_chartRect.width()+ m_chartMargins.left()) {
87 m_minLeftMargin= width - m_chartRect.width();
87 m_minLeftMargin= width - m_chartRect.width();
88 updateLayout();
88 updateLayout();
89 }
89 }
90 break;
90 break;
91 }
91 }
92 case Axis::Y_AXIS:
92 case Axis::Y_AXIS:
93 {
93 {
94
94
95 if(m_minLeftMargin!=width){
95 if(m_minLeftMargin!=width){
96 m_minLeftMargin= width;
96 m_minLeftMargin= width;
97 updateLayout();
97 updateLayout();
98 }
98 }
99 break;
99 break;
100 }
100 }
101
101
102 }
102 }
103 }
103 }
104
104
105 void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height)
105 void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height)
106 {
106 {
107 switch(axis->axisType()){
107 switch(axis->axisType()){
108 case Axis::X_AXIS:
108 case Axis::X_AXIS:
109 {
109 {
110 if(m_minBottomMargin!=height) {
110 if(m_minBottomMargin!=height) {
111 m_minBottomMargin= height;
111 m_minBottomMargin= height;
112 updateLayout();
112 updateLayout();
113 }
113 }
114 break;
114 break;
115 }
115 }
116 case Axis::Y_AXIS:
116 case Axis::Y_AXIS:
117 {
117 {
118
118
119 if(height>m_chartMargins.bottom()+m_chartRect.height()){
119 if(height>m_chartMargins.bottom()+m_chartRect.height()){
120 m_minBottomMargin= height - m_chartRect.height();
120 m_minBottomMargin= height - m_chartRect.height();
121 updateLayout();
121 updateLayout();
122 }
122 }
123 break;
123 break;
124 }
124 }
125
125
126 }
126 }
127 }
127 }
128
128
129 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
129 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
130 {
130 {
131 Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS);
131 Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS);
132
132
133 if(m_options.testFlag(QChart::GridAxisAnimations)){
133 if(m_options.testFlag(QChart::GridAxisAnimations)){
134 m_animator->addAnimation(item);
134 m_animator->addAnimation(item);
135 }
135 }
136
136
137 if(axis==m_dataset->axisX()){
137 if(axis==m_dataset->axisX()){
138 m_chartTheme->decorate(axis,true,m_themeForce);
138 m_chartTheme->decorate(axis,true,m_themeForce);
139 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
139 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
140 //initialize
140 //initialize
141 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
141 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
142
142
143 }
143 }
144 else{
144 else{
145 m_chartTheme->decorate(axis,false,m_themeForce);
145 m_chartTheme->decorate(axis,false,m_themeForce);
146 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
146 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
147 //initialize
147 //initialize
148 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
148 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
149 }
149 }
150
150
151 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
151 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
152 //initialize
152 //initialize
153 item->handleGeometryChanged(m_chartRect);
153 item->handleGeometryChanged(m_chartRect);
154 m_axisItems.insert(axis, item);
154 m_axisItems.insert(axis, item);
155 }
155 }
156
156
157 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
157 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
158 {
158 {
159 Axis* item = m_axisItems.take(axis);
159 Axis* item = m_axisItems.take(axis);
160 Q_ASSERT(item);
160 Q_ASSERT(item);
161 if(m_animator) m_animator->removeAnimation(item);
161 if(m_animator) m_animator->removeAnimation(item);
162 delete item;
162 delete item;
163 }
163 }
164
164
165
165
166 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
166 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
167 {
167 {
168 Chart *item = 0 ;
168 Chart *item = 0 ;
169
169
170 switch(series->type())
170 switch(series->type())
171 {
171 {
172 case QSeries::SeriesTypeLine: {
172 case QSeries::SeriesTypeLine: {
173
173
174 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
174 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
175 LineChartItem* line = new LineChartItem(lineSeries,this);
175 LineChartItem* line = new LineChartItem(lineSeries,this);
176 if(m_options.testFlag(QChart::SeriesAnimations)) {
176 if(m_options.testFlag(QChart::SeriesAnimations)) {
177 m_animator->addAnimation(line);
177 m_animator->addAnimation(line);
178 }
178 }
179 m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series),m_themeForce);
179 m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series),m_themeForce);
180 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
180 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
181 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
181 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
182 item = line;
182 item = line;
183 break;
183 break;
184 }
184 }
185
185
186 case QSeries::SeriesTypeArea: {
186 case QSeries::SeriesTypeArea: {
187
187
188 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
188 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
189 AreaChartItem* area = new AreaChartItem(areaSeries,this);
189 AreaChartItem* area = new AreaChartItem(areaSeries,this);
190 if(m_options.testFlag(QChart::SeriesAnimations)) {
190 if(m_options.testFlag(QChart::SeriesAnimations)) {
191 m_animator->addAnimation(area->upperLineItem());
191 m_animator->addAnimation(area->upperLineItem());
192 if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem());
192 if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem());
193 }
193 }
194 m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series),m_themeForce);
194 m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series),m_themeForce);
195 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
195 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
196 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
196 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
197 item=area;
197 item=area;
198 break;
198 break;
199 }
199 }
200
200
201 case QSeries::SeriesTypeBar: {
201 case QSeries::SeriesTypeBar: {
202 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
202 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
203 BarChartItem* bar = new BarChartItem(barSeries,this);
203 BarChartItem* bar = new BarChartItem(barSeries,this);
204 if(m_options.testFlag(QChart::SeriesAnimations)) {
204 if(m_options.testFlag(QChart::SeriesAnimations)) {
205 m_animator->addAnimation(bar);
205 m_animator->addAnimation(bar);
206 }
206 }
207 m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries),m_themeForce);
207 m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries),m_themeForce);
208 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
208 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
209 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
209 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
210 item=bar;
210 item=bar;
211 break;
211 break;
212 }
212 }
213
213
214 case QSeries::SeriesTypeStackedBar: {
214 case QSeries::SeriesTypeStackedBar: {
215 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
215 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
216 StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,this);
216 StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,this);
217 if(m_options.testFlag(QChart::SeriesAnimations)) {
217 if(m_options.testFlag(QChart::SeriesAnimations)) {
218 m_animator->addAnimation(bar);
218 m_animator->addAnimation(bar);
219 }
219 }
220 m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries),m_themeForce);
220 m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries),m_themeForce);
221 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
221 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
222 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
222 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
223 item=bar;
223 item=bar;
224 break;
224 break;
225 }
225 }
226
226
227 case QSeries::SeriesTypePercentBar: {
227 case QSeries::SeriesTypePercentBar: {
228 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
228 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
229 PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,this);
229 PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,this);
230 if(m_options.testFlag(QChart::SeriesAnimations)) {
230 if(m_options.testFlag(QChart::SeriesAnimations)) {
231 m_animator->addAnimation(bar);
231 m_animator->addAnimation(bar);
232 }
232 }
233 m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries),m_themeForce);
233 m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries),m_themeForce);
234 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
234 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
235 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
235 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
236 item=bar;
236 item=bar;
237 break;
237 break;
238 }
238 }
239
239
240 case QSeries::SeriesTypeScatter: {
240 case QSeries::SeriesTypeScatter: {
241
241
242 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
242 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
243 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries,this);
243 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries,this);
244 if(m_options.testFlag(QChart::SeriesAnimations)) {
244 if(m_options.testFlag(QChart::SeriesAnimations)) {
245 m_animator->addAnimation(scatter);
245 m_animator->addAnimation(scatter);
246 }
246 }
247 m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series),m_themeForce);
247 m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series),m_themeForce);
248 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
248 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
249 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
249 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
250 item = scatter;
250 item = scatter;
251 break;
251 break;
252 }
252 }
253
253
254 case QSeries::SeriesTypePie: {
254 case QSeries::SeriesTypePie: {
255 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
255 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
256 PieChartItem* pie = new PieChartItem(pieSeries, this);
256 PieChartItem* pie = new PieChartItem(pieSeries, this);
257 if(m_options.testFlag(QChart::SeriesAnimations)) {
257 if(m_options.testFlag(QChart::SeriesAnimations)) {
258 m_animator->addAnimation(pie);
258 m_animator->addAnimation(pie);
259 }
259 }
260 m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series),m_themeForce);
260 m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series),m_themeForce);
261 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
261 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
262 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
262 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
263 // Hide all from background when there is only piechart
263 // Hide all from background when there is only piechart
264 // TODO: refactor this ugly code... should be one setting for this
264 // TODO: refactor this ugly code... should be one setting for this
265 if (m_chartItems.count() == 0) {
265 if (m_chartItems.count() == 0) {
266 m_chart->axisX()->hide();
266 m_chart->axisX()->hide();
267 m_chart->axisY()->hide();
267 m_chart->axisY()->hide();
268 }
268 }
269 item=pie;
269 item=pie;
270 break;
270 break;
271 }
271 }
272
272
273 case QSeries::SeriesTypeSpline: {
273 case QSeries::SeriesTypeSpline: {
274 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
274 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
275 SplineChartItem* spline = new SplineChartItem(splineSeries, this);
275 SplineChartItem* spline = new SplineChartItem(splineSeries, this);
276 if(m_options.testFlag(QChart::SeriesAnimations)) {
276 if(m_options.testFlag(QChart::SeriesAnimations)) {
277 m_animator->addAnimation(spline);
277 m_animator->addAnimation(spline);
278 }
278 }
279 m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series),m_themeForce);
279 m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series),m_themeForce);
280 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
280 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
281 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
281 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
282 item=spline;
282 item=spline;
283 break;
283 break;
284 }
284 }
285 default: {
285 default: {
286 qDebug()<< "Series type" << series->type() << "not implemented.";
286 qDebug()<< "Series type" << series->type() << "not implemented.";
287 break;
287 break;
288 }
288 }
289 }
289 }
290
290
291 //initialize
291 //initialize
292 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
292 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
293 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
293 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
294 m_chartItems.insert(series,item);
294 m_chartItems.insert(series,item);
295 }
295 }
296
296
297 void ChartPresenter::handleSeriesRemoved(QSeries* series)
297 void ChartPresenter::handleSeriesRemoved(QSeries* series)
298 {
298 {
299 Chart* item = m_chartItems.take(series);
299 Chart* item = m_chartItems.take(series);
300 Q_ASSERT(item);
300 Q_ASSERT(item);
301 if(m_animator) {
301 if(m_animator) {
302 //small hack to handle area animations
302 //small hack to handle area animations
303 if(series->type()==QSeries::SeriesTypeArea){
303 if(series->type()==QSeries::SeriesTypeArea){
304 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
304 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
305 AreaChartItem* area = static_cast<AreaChartItem*>(item);
305 AreaChartItem* area = static_cast<AreaChartItem*>(item);
306 m_animator->removeAnimation(area->upperLineItem());
306 m_animator->removeAnimation(area->upperLineItem());
307 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
307 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
308 }else
308 }else
309 m_animator->removeAnimation(item);
309 m_animator->removeAnimation(item);
310 }
310 }
311 delete item;
311 delete item;
312 }
312 }
313
313
314 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
314 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
315 {
315 {
316 if(m_chartTheme && m_chartTheme->id() == theme) return;
316 if(m_chartTheme && m_chartTheme->id() == theme) return;
317 delete m_chartTheme;
317 delete m_chartTheme;
318 m_themeForce = force;
318 m_themeForce = force;
319 m_chartTheme = ChartTheme::createTheme(theme);
319 m_chartTheme = ChartTheme::createTheme(theme);
320 m_chartTheme->decorate(m_chart,m_themeForce);
320 m_chartTheme->decorate(m_chart,m_themeForce);
321 m_chartTheme->decorate(m_chart->legend(),m_themeForce);
321 m_chartTheme->decorate(m_chart->legend(),m_themeForce);
322 resetAllElements();
322 resetAllElements();
323 }
323 }
324
324
325 QChart::ChartTheme ChartPresenter::theme()
325 QChart::ChartTheme ChartPresenter::theme()
326 {
326 {
327 return m_chartTheme->id();
327 return m_chartTheme->id();
328 }
328 }
329
329
330 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
330 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
331 {
331 {
332 if(m_options!=options) {
332 if(m_options!=options) {
333
333
334 m_options=options;
334 m_options=options;
335
335
336 if(m_options!=QChart::NoAnimation && !m_animator) {
336 if(m_options!=QChart::NoAnimation && !m_animator) {
337 m_animator= new ChartAnimator(this);
337 m_animator= new ChartAnimator(this);
338 }
338 }
339 resetAllElements();
339 resetAllElements();
340 }
340 }
341
341
342 }
342 }
343
343
344 void ChartPresenter::resetAllElements()
344 void ChartPresenter::resetAllElements()
345 {
345 {
346 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
346 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
347 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
347 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
348
348
349 foreach(QChartAxis* axis, axisList) {
349 foreach(QChartAxis* axis, axisList) {
350 handleAxisRemoved(axis);
350 handleAxisRemoved(axis);
351 handleAxisAdded(axis,m_dataset->domain(axis));
351 handleAxisAdded(axis,m_dataset->domain(axis));
352 }
352 }
353 foreach(QSeries* series, seriesList) {
353 foreach(QSeries* series, seriesList) {
354 handleSeriesRemoved(series);
354 handleSeriesRemoved(series);
355 handleSeriesAdded(series,m_dataset->domain(series));
355 handleSeriesAdded(series,m_dataset->domain(series));
356 }
356 }
357 }
357 }
358
358
359 void ChartPresenter::zoomIn()
359 void ChartPresenter::zoomIn()
360 {
360 {
361 QRectF rect = chartGeometry();
361 QRectF rect = chartGeometry();
362 rect.setWidth(rect.width()/2);
362 rect.setWidth(rect.width()/2);
363 rect.setHeight(rect.height()/2);
363 rect.setHeight(rect.height()/2);
364 rect.moveCenter(chartGeometry().center());
364 rect.moveCenter(chartGeometry().center());
365 zoomIn(rect);
365 zoomIn(rect);
366 }
366 }
367
367
368 void ChartPresenter::zoomIn(const QRectF& rect)
368 void ChartPresenter::zoomIn(const QRectF& rect)
369 {
369 {
370 QRectF r = rect.normalized();
370 QRectF r = rect.normalized();
371 r.translate(-m_chartMargins.topLeft());
371 r.translate(-m_chartMargins.topLeft());
372 if(m_animator) {
372 if(m_animator) {
373
373
374 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
374 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
375 m_animator->setState(ChartAnimator::ZoomInState,point);
375 m_animator->setState(ChartAnimator::ZoomInState,point);
376 }
376 }
377 m_dataset->zoomInDomain(r,chartGeometry().size());
377 m_dataset->zoomInDomain(r,chartGeometry().size());
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 void ChartPresenter::zoomOut()
383 void ChartPresenter::zoomOut()
384 {
384 {
385 if(m_animator)
385 if(m_animator)
386 {
386 {
387 m_animator->setState(ChartAnimator::ZoomOutState);
387 m_animator->setState(ChartAnimator::ZoomOutState);
388 }
388 }
389
389
390 QSizeF size = chartGeometry().size();
390 QSizeF size = chartGeometry().size();
391 QRectF rect = chartGeometry();
391 QRectF rect = chartGeometry();
392 rect.translate(-m_chartMargins.topLeft());
392 rect.translate(-m_chartMargins.topLeft());
393 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
393 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
394 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
394 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
395
395
396 if(m_animator){
396 if(m_animator){
397 m_animator->setState(ChartAnimator::ShowState);
397 m_animator->setState(ChartAnimator::ShowState);
398 }
398 }
399 }
399 }
400
400
401 void ChartPresenter::scroll(int dx,int dy)
401 void ChartPresenter::scroll(int dx,int dy)
402 {
402 {
403 if(m_animator){
403 if(m_animator){
404 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
404 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
405 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
405 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
406 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
406 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
407 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
407 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
408 }
408 }
409
409
410 m_dataset->scrollDomain(dx,dy,chartGeometry().size());
410 m_dataset->scrollDomain(dx,dy,chartGeometry().size());
411
411
412 if(m_animator){
412 if(m_animator){
413 m_animator->setState(ChartAnimator::ShowState);
413 m_animator->setState(ChartAnimator::ShowState);
414 }
414 }
415 }
415 }
416
416
417 QChart::AnimationOptions ChartPresenter::animationOptions() const
417 QChart::AnimationOptions ChartPresenter::animationOptions() const
418 {
418 {
419 return m_options;
419 return m_options;
420 }
420 }
421
421
422 void ChartPresenter::updateLayout()
422 void ChartPresenter::updateLayout()
423 {
423 {
424 if (!m_rect.isValid()) return;
424 if (!m_rect.isValid()) return;
425
425
426 // recalculate title size
426 // recalculate title size
427
427
428 QSize titleSize;
428 QSize titleSize;
429 int titlePadding=0;
429 int titlePadding=0;
430
430
431 if (m_titleItem) {
431 if (m_titleItem) {
432 titleSize= m_titleItem->boundingRect().size().toSize();
432 titleSize= m_titleItem->boundingRect().size().toSize();
433 }
433 }
434
434
435 //defaults
435 //defaults
436 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));
436 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));
437 titlePadding = m_chartMargins.top()/2;
437 titlePadding = m_chartMargins.top()/2;
438
438
439 QLegend* legend = m_chart->d_ptr->m_legend;
439 QLegend* legend = m_chart->d_ptr->m_legend;
440
440
441 // recalculate legend position
441 // recalculate legend position
442 if (legend->isAttachedToChart() && legend->isEnabled()) {
442 if (legend->isAttachedToChart() && legend->isEnabled()) {
443
443
444 QRect legendRect;
444 QRect legendRect;
445
445
446 // Reserve some space for legend
446 // Reserve some space for legend
447 switch (legend->alignment()) {
447 switch (legend->alignment()) {
448
448
449 case QLegend::AlignmentTop: {
449 case QLegend::AlignmentTop: {
450 int ledgendSize = legend->minHeight();
450 int ledgendSize = legend->minHeight();
451 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
451 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
452 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
452 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
453 m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny));
453 m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny));
454 titlePadding = m_marginTiny + m_marginTiny;
454 titlePadding = m_marginTiny + m_marginTiny;
455 break;
455 break;
456 }
456 }
457 case QLegend::AlignmentBottom: {
457 case QLegend::AlignmentBottom: {
458 int ledgendSize = legend->minHeight();
458 int ledgendSize = legend->minHeight();
459 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
459 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
460 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
460 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
461 m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall));
461 m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall));
462 titlePadding = m_chartMargins.top()/2;
462 titlePadding = m_chartMargins.top()/2;
463 break;
463 break;
464 }
464 }
465 case QLegend::AlignmentLeft: {
465 case QLegend::AlignmentLeft: {
466 int ledgendSize = legend->minWidth();
466 int ledgendSize = legend->minWidth();
467 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
467 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
468 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
468 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
469 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom()));
469 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom()));
470 titlePadding = m_chartMargins.top()/2;
470 titlePadding = m_chartMargins.top()/2;
471 break;
471 break;
472 }
472 }
473 case QLegend::AlignmentRight: {
473 case QLegend::AlignmentRight: {
474 int ledgendSize = legend->minWidth();
474 int ledgendSize = legend->minWidth();
475 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
475 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
476 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
476 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
477 m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom()));
477 m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom()));
478 titlePadding = m_chartMargins.top()/2;
478 titlePadding = m_chartMargins.top()/2;
479 break;
479 break;
480 }
480 }
481 default: {
481 default: {
482 break;
482 break;
483 }
483 }
484 }
484 }
485 }
485 }
486
486
487 if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom()))
488 {
489 m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
490 return;
491 }
492
493
487 // recalculate title position
494 // recalculate title position
488 if (m_titleItem) {
495 if (m_titleItem) {
489 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
496 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
490 m_titleItem->setPos(center.x(),titlePadding);
497 m_titleItem->setPos(center.x(),titlePadding);
491 }
498 }
492
499
493 //recalculate background gradient
500 //recalculate background gradient
494 if (m_backgroundItem) {
501 if (m_backgroundItem) {
495 m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
502 m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
496 }
503 }
497
504
505
498 QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom());
506 QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom());
499
507
500 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
508 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
501
509
502 if(m_chartRect!=chartRect){
510 if(m_chartRect!=chartRect){
503 m_chartRect=chartRect;
511 m_chartRect=chartRect;
504 emit geometryChanged(m_chartRect);
512 emit geometryChanged(m_chartRect);
505 }
513 }
506
514
507
515
508 }
516 }
509
517
510 void ChartPresenter::createChartBackgroundItem()
518 void ChartPresenter::createChartBackgroundItem()
511 {
519 {
512 if (!m_backgroundItem) {
520 if (!m_backgroundItem) {
513 m_backgroundItem = new ChartBackground(rootItem());
521 m_backgroundItem = new ChartBackground(rootItem());
514 m_backgroundItem->setPen(Qt::NoPen);
522 m_backgroundItem->setPen(Qt::NoPen);
515 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
523 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
516 }
524 }
517 }
525 }
518
526
519 void ChartPresenter::createChartTitleItem()
527 void ChartPresenter::createChartTitleItem()
520 {
528 {
521 if (!m_titleItem) {
529 if (!m_titleItem) {
522 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
530 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
523 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
531 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
524 }
532 }
525 }
533 }
526
534
527 #include "moc_chartpresenter_p.cpp"
535 #include "moc_chartpresenter_p.cpp"
528
536
529 QTCOMMERCIALCHART_END_NAMESPACE
537 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,249 +1,250
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchartview.h"
21 #include "qchartview.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qchartview_p.h"
23 #include "qchartview_p.h"
24 #include <QGraphicsScene>
24 #include <QGraphicsScene>
25 #include <QRubberBand>
25 #include <QRubberBand>
26
26
27
27
28 /*!
28 /*!
29 \enum QChartView::RubberBandPolicy
29 \enum QChartView::RubberBandPolicy
30
30
31 This enum describes the different types of rubber bands that can be used for zoom rect selection
31 This enum describes the different types of rubber bands that can be used for zoom rect selection
32
32
33 \value NoRubberBand
33 \value NoRubberBand
34 \value VerticalRubberBand
34 \value VerticalRubberBand
35 \value HorizonalRubberBand
35 \value HorizonalRubberBand
36 \value RectangleRubberBand
36 \value RectangleRubberBand
37 */
37 */
38
38
39 /*!
39 /*!
40 \class QChartView
40 \class QChartView
41 \brief Standalone charting widget.
41 \brief Standalone charting widget.
42
42
43 QChartView is a standalone widget that can display charts. It does not require separate
43 QChartView is a standalone widget that can display charts. It does not require separate
44 QGraphicsScene to work. It manages the graphical representation of different types of
44 QGraphicsScene to work. It manages the graphical representation of different types of
45 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
45 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
46 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
46 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
47
47
48 \sa QChart
48 \sa QChart
49 */
49 */
50
50
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52
52
53 /*!
53 /*!
54 Constructs a chartView object which is a child of a\a parent.
54 Constructs a chartView object which is a child of a\a parent.
55 */
55 */
56 QChartView::QChartView(QChart *chart,QWidget *parent) :
56 QChartView::QChartView(QChart *chart,QWidget *parent) :
57 QGraphicsView(parent),
57 QGraphicsView(parent),
58 d_ptr(new QChartViewPrivate())
58 d_ptr(new QChartViewPrivate())
59 {
59 {
60 Q_ASSERT(chart);
60 Q_ASSERT(chart);
61 d_ptr->m_scene = new QGraphicsScene(this);
61 d_ptr->m_scene = new QGraphicsScene(this);
62 d_ptr->m_chart = chart;
62 d_ptr->m_chart = chart;
63 setFrameShape(QFrame::NoFrame);
63 setFrameShape(QFrame::NoFrame);
64 setBackgroundRole(QPalette::Window);
64 setBackgroundRole(QPalette::Window);
65 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 setScene(d_ptr->m_scene);
67 setScene(d_ptr->m_scene);
68 d_ptr->m_scene->addItem(chart);
68 d_ptr->m_scene->addItem(chart);
69 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
69 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
70 }
70 }
71
71
72
72
73 /*!
73 /*!
74 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
74 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
75 */
75 */
76 QChartView::~QChartView()
76 QChartView::~QChartView()
77 {
77 {
78 }
78 }
79
79
80 /*!
80 /*!
81 Returns the pointer to the associated chart
81 Returns the pointer to the associated chart
82 */
82 */
83 QChart* QChartView::chart() const
83 QChart* QChartView::chart() const
84 {
84 {
85 return d_ptr->m_chart;
85 return d_ptr->m_chart;
86 }
86 }
87
87
88 /*!
88 /*!
89 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
89 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
90 */
90 */
91 void QChartView::setRubberBand(const RubberBands& rubberBand)
91 void QChartView::setRubberBand(const RubberBands& rubberBand)
92 {
92 {
93 d_ptr->m_rubberBandFlags=rubberBand;
93 d_ptr->m_rubberBandFlags=rubberBand;
94
94
95 if (!d_ptr->m_rubberBandFlags) {
95 if (!d_ptr->m_rubberBandFlags) {
96 delete d_ptr->m_rubberBand;
96 delete d_ptr->m_rubberBand;
97 d_ptr->m_rubberBand=0;
97 d_ptr->m_rubberBand=0;
98 return;
98 return;
99 }
99 }
100
100
101 if (!d_ptr->m_rubberBand) {
101 if (!d_ptr->m_rubberBand) {
102 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
102 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
103 d_ptr->m_rubberBand->setEnabled(true);
103 d_ptr->m_rubberBand->setEnabled(true);
104 }
104 }
105 }
105 }
106
106
107 /*!
107 /*!
108 Returns the RubberBandPolicy that is currently being used by the widget.
108 Returns the RubberBandPolicy that is currently being used by the widget.
109 */
109 */
110 QChartView::RubberBands QChartView::rubberBand() const
110 QChartView::RubberBands QChartView::rubberBand() const
111 {
111 {
112 return d_ptr->m_rubberBandFlags;
112 return d_ptr->m_rubberBandFlags;
113 }
113 }
114
114
115 /*!
115 /*!
116 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
116 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
117 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
117 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
118 */
118 */
119 void QChartView::mousePressEvent(QMouseEvent *event)
119 void QChartView::mousePressEvent(QMouseEvent *event)
120 {
120 {
121 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
121 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
122
122
123 int padding = d_ptr->m_chart->margins().top();
123 int padding = d_ptr->m_chart->margins().top();
124 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
124 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
125
125
126 if (rect.contains(event->pos())) {
126 if (rect.contains(event->pos())) {
127 d_ptr->m_rubberBandOrigin = event->pos();
127 d_ptr->m_rubberBandOrigin = event->pos();
128 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
128 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
129 d_ptr->m_rubberBand->show();
129 d_ptr->m_rubberBand->show();
130 event->accept();
130 event->accept();
131 }
131 }
132 }
132 }
133 else {
133 else {
134 QGraphicsView::mousePressEvent(event);
134 QGraphicsView::mousePressEvent(event);
135 }
135 }
136 }
136 }
137
137
138 /*!
138 /*!
139 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
139 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
140 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
140 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
141 */
141 */
142 void QChartView::mouseMoveEvent(QMouseEvent *event)
142 void QChartView::mouseMoveEvent(QMouseEvent *event)
143 {
143 {
144 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
144 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
145 QRectF margins = d_ptr->m_chart->margins();
145 QRectF margins = d_ptr->m_chart->margins();
146 QRectF geometry = d_ptr->m_chart->geometry();
146 QRectF geometry = d_ptr->m_chart->geometry();
147 QRectF rect =geometry.adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom());
147 QRectF rect =geometry.adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom());
148 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
148 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
149 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
149 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
150 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
150 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
151 d_ptr->m_rubberBandOrigin.setY(rect.top());
151 d_ptr->m_rubberBandOrigin.setY(rect.top());
152 height = rect.height();
152 height = rect.height();
153 }
153 }
154 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
154 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
155 d_ptr->m_rubberBandOrigin.setX(rect.left());
155 d_ptr->m_rubberBandOrigin.setX(rect.left());
156 width= rect.width();
156 width= rect.width();
157 }
157 }
158 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
158 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
159 }
159 }
160 else {
160 else {
161 QGraphicsView::mouseMoveEvent(event);
161 QGraphicsView::mouseMoveEvent(event);
162 }
162 }
163 }
163 }
164
164
165 /*!
165 /*!
166 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
166 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
167 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
167 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
168 */
168 */
169 void QChartView::mouseReleaseEvent(QMouseEvent *event)
169 void QChartView::mouseReleaseEvent(QMouseEvent *event)
170 {
170 {
171 if(d_ptr->m_rubberBand) {
171 if(d_ptr->m_rubberBand) {
172 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
172 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
173 d_ptr->m_rubberBand->hide();
173 d_ptr->m_rubberBand->hide();
174 QRect rect = d_ptr->m_rubberBand->geometry();
174 QRect rect = d_ptr->m_rubberBand->geometry();
175 d_ptr->m_chart->zoomIn(rect);
175 d_ptr->m_chart->zoomIn(rect);
176 event->accept();
176 event->accept();
177 }
177 }
178
178
179 if(event->button()==Qt::RightButton){
179 if(event->button()==Qt::RightButton){
180 d_ptr->m_chart->zoomOut();
180 d_ptr->m_chart->zoomOut();
181 event->accept();
181 event->accept();
182 }
182 }
183 }
183 }
184 else {
184 else {
185 QGraphicsView::mouseReleaseEvent(event);
185 QGraphicsView::mouseReleaseEvent(event);
186 }
186 }
187 }
187 }
188
188
189 /*!
189 /*!
190 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
190 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
191 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
191 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
192 */
192 */
193 void QChartView::keyPressEvent(QKeyEvent *event)
193 void QChartView::keyPressEvent(QKeyEvent *event)
194 {
194 {
195 switch (event->key()) {
195 switch (event->key()) {
196 case Qt::Key_Plus:
196 case Qt::Key_Plus:
197 d_ptr->m_chart->zoomIn();
197 d_ptr->m_chart->zoomIn();
198 break;
198 break;
199 case Qt::Key_Minus:
199 case Qt::Key_Minus:
200 d_ptr->m_chart->zoomOut();
200 d_ptr->m_chart->zoomOut();
201 break;
201 break;
202 case Qt::Key_Left:
202 case Qt::Key_Left:
203 d_ptr->m_chart->scrollLeft();
203 d_ptr->m_chart->scrollLeft();
204 break;
204 break;
205 case Qt::Key_Right:
205 case Qt::Key_Right:
206 d_ptr->m_chart->scrollRight();
206 d_ptr->m_chart->scrollRight();
207 break;
207 break;
208 case Qt::Key_Up:
208 case Qt::Key_Up:
209 d_ptr->m_chart->scrollUp();
209 d_ptr->m_chart->scrollUp();
210 break;
210 break;
211 case Qt::Key_Down:
211 case Qt::Key_Down:
212 d_ptr->m_chart->scrollDown();
212 d_ptr->m_chart->scrollDown();
213 break;
213 break;
214 default:
214 default:
215 QGraphicsView::keyPressEvent(event);
215 QGraphicsView::keyPressEvent(event);
216 break;
216 break;
217 }
217 }
218 }
218 }
219
219
220 /*!
220 /*!
221 Resizes and updates the chart area using the \a event data
221 Resizes and updates the chart area using the \a event data
222 */
222 */
223 void QChartView::resizeEvent(QResizeEvent *event)
223 void QChartView::resizeEvent(QResizeEvent *event)
224 {
224 {
225 QGraphicsView::resizeEvent(event);
225 QGraphicsView::resizeEvent(event);
226 d_ptr->m_chart->resize(size());
226 d_ptr->m_chart->resize(size());
227 setMinimumSize(d_ptr->m_chart->minimumSize().toSize());
227 setSceneRect(d_ptr->m_chart->geometry());
228 setSceneRect(d_ptr->m_chart->geometry());
228 }
229 }
229
230
230 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231
232
232 QChartViewPrivate::QChartViewPrivate():
233 QChartViewPrivate::QChartViewPrivate():
233 m_scene(0),
234 m_scene(0),
234 m_chart(0),
235 m_chart(0),
235 m_presenter(0),
236 m_presenter(0),
236 m_rubberBand(0),
237 m_rubberBand(0),
237 m_rubberBandFlags(QChartView::NoRubberBand)
238 m_rubberBandFlags(QChartView::NoRubberBand)
238 {
239 {
239
240
240 }
241 }
241
242
242 QChartViewPrivate::~QChartViewPrivate()
243 QChartViewPrivate::~QChartViewPrivate()
243 {
244 {
244
245
245 }
246 }
246
247
247 #include "moc_qchartview.cpp"
248 #include "moc_qchartview.cpp"
248
249
249 QTCOMMERCIALCHART_END_NAMESPACE
250 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now