##// END OF EJS Templates
Implement QChartView::setChart()
Jani Honkonen -
r2056:4b7228265b30
parent child
Show More
@@ -1,258 +1,259
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 "chartlayout_p.h"
21 #include "chartlayout_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlegend_p.h"
23 #include "qlegend_p.h"
24 #include "chartaxis_p.h"
24 #include "chartaxis_p.h"
25 #include "charttitle_p.h"
25 #include "charttitle_p.h"
26 #include "chartbackground_p.h"
26 #include "chartbackground_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include <QDebug>
28 #include <QDebug>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 static const qreal golden_ratio = 0.25;
32 static const qreal golden_ratio = 0.25;
33
33
34 ChartLayout::ChartLayout(ChartPresenter* presenter):
34 ChartLayout::ChartLayout(ChartPresenter* presenter):
35 m_presenter(presenter),
35 m_presenter(presenter),
36 m_margins(20,20,20,20),
36 m_margins(20,20,20,20),
37 m_minChartRect(0,0,200,200)
37 m_minChartRect(0,0,200,200)
38 {
38 {
39
39
40 }
40 }
41
41
42 ChartLayout::~ChartLayout()
42 ChartLayout::~ChartLayout()
43 {
43 {
44
44
45 }
45 }
46
46
47 void ChartLayout::setGeometry(const QRectF& rect)
47 void ChartLayout::setGeometry(const QRectF& rect)
48 {
48 {
49 Q_ASSERT(rect.isValid());
49 if (!rect.isValid())
50 return;
50
51
51 QList<ChartAxis*> axes = m_presenter->axisItems();
52 QList<ChartAxis*> axes = m_presenter->axisItems();
52 ChartTitle* title = m_presenter->titleElement();
53 ChartTitle* title = m_presenter->titleElement();
53 QLegend* legend = m_presenter->legend();
54 QLegend* legend = m_presenter->legend();
54 ChartBackground* background = m_presenter->backgroundElement();
55 ChartBackground* background = m_presenter->backgroundElement();
55
56
56 QRectF contentGeometry = calculateBackgroundGeometry(rect,background);
57 QRectF contentGeometry = calculateBackgroundGeometry(rect,background);
57
58
58 contentGeometry = calculateContentGeometry(contentGeometry);
59 contentGeometry = calculateContentGeometry(contentGeometry);
59
60
60 if (title && title->isVisible()) {
61 if (title && title->isVisible()) {
61 contentGeometry = calculateTitleGeometry(contentGeometry,title);
62 contentGeometry = calculateTitleGeometry(contentGeometry,title);
62 }
63 }
63
64
64 if (legend->isAttachedToChart() && legend->isVisible()) {
65 if (legend->isAttachedToChart() && legend->isVisible()) {
65 contentGeometry = calculateLegendGeometry(contentGeometry,legend);
66 contentGeometry = calculateLegendGeometry(contentGeometry,legend);
66 }
67 }
67
68
68 calculateChartGeometry(contentGeometry,axes);
69 calculateChartGeometry(contentGeometry,axes);
69
70
70 //TODO remove me
71 //TODO remove me
71 #ifdef SHOW_LAYOUT
72 #ifdef SHOW_LAYOUT
72 LayoutDebuger* debuger = LayoutDebuger::instance();
73 LayoutDebuger* debuger = LayoutDebuger::instance();
73 debuger->reset();
74 debuger->reset();
74 debuger->setPen(QPen(Qt::red));
75 debuger->setPen(QPen(Qt::red));
75 debuger->add(backgroundGeometry,m_presenter->rootItem());
76 debuger->add(backgroundGeometry,m_presenter->rootItem());
76 debuger->add(titleGeometry,m_presenter->rootItem());
77 debuger->add(titleGeometry,m_presenter->rootItem());
77 debuger->add(legendGeometry ,m_presenter->rootItem());
78 debuger->add(legendGeometry ,m_presenter->rootItem());
78 debuger->add(axisGeometry ,m_presenter->rootItem());
79 debuger->add(axisGeometry ,m_presenter->rootItem());
79 debuger->add(geometry,m_presenter->rootItem());
80 debuger->add(geometry,m_presenter->rootItem());
80 foreach(LegendMarker* marker,legend->d_ptr->markers()){
81 foreach(LegendMarker* marker,legend->d_ptr->markers()){
81 debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem());
82 debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem());
82 }
83 }
83 #endif
84 #endif
84
85
85 QGraphicsLayout::setGeometry(rect);
86 QGraphicsLayout::setGeometry(rect);
86 }
87 }
87
88
88 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
89 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
89 {
90 {
90 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
91 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
91 }
92 }
92
93
93 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
94 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
94 {
95 {
95 return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom());
96 return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom());
96 }
97 }
97
98
98
99
99 QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const
100 QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const
100 {
101 {
101 qreal left, top, right, bottom;
102 qreal left, top, right, bottom;
102 getContentsMargins(&left, &top, &right, &bottom);
103 getContentsMargins(&left, &top, &right, &bottom);
103 QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom);
104 QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom);
104 if(background) background->setRect(backgroundGeometry);
105 if(background) background->setRect(backgroundGeometry);
105 return backgroundGeometry;
106 return backgroundGeometry;
106 }
107 }
107
108
108 QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const
109 QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const
109 {
110 {
110 qreal left, top, right, bottom;
111 qreal left, top, right, bottom;
111 getContentsMargins(&left, &top, &right, &bottom);
112 getContentsMargins(&left, &top, &right, &bottom);
112 return minimum.adjusted(0,0,left + right,top+bottom);
113 return minimum.adjusted(0,0,left + right,top+bottom);
113 }
114 }
114
115
115 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const
116 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const
116 {
117 {
117
118
118 QSizeF vertical(0,0);
119 QSizeF vertical(0,0);
119 QSizeF horizontal(0,0);
120 QSizeF horizontal(0,0);
120
121
121 // check axis size
122 // check axis size
122 foreach(ChartAxis* axis , axes) {
123 foreach(ChartAxis* axis , axes) {
123 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
124 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
124 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
125 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
125 }
126 }
126 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
127 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
127 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
128 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
128 }
129 }
129
130
130 }
131 }
131
132
132 qreal width = qMin(vertical.width(),geometry.width() * golden_ratio);
133 qreal width = qMin(vertical.width(),geometry.width() * golden_ratio);
133
134
134 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
135 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
135
136
136 m_presenter->setChartsGeometry(rect);
137 m_presenter->setChartsGeometry(rect);
137
138
138 foreach(ChartAxis* axis , axes) {
139 foreach(ChartAxis* axis , axes) {
139 axis->setGeometry(geometry);
140 axis->setGeometry(geometry);
140 }
141 }
141
142
142 return rect;
143 return rect;
143 }
144 }
144
145
145 QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const
146 QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const
146 {
147 {
147 QSizeF vertical(0,0);
148 QSizeF vertical(0,0);
148 QSizeF horizontal(0,0);
149 QSizeF horizontal(0,0);
149
150
150 // check axis size
151 // check axis size
151 foreach(ChartAxis* axis , axes) {
152 foreach(ChartAxis* axis , axes) {
152 if(axis->orientation()==Qt::Vertical && axis->isVisible()){
153 if(axis->orientation()==Qt::Vertical && axis->isVisible()){
153 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
154 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
154 }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
155 }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
155 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
156 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
156 }
157 }
157 }
158 }
158 return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height());
159 return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height());
159 }
160 }
160
161
161 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const
162 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const
162 {
163 {
163 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1));
164 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1));
164 QRectF legendRect;
165 QRectF legendRect;
165 QRectF result;
166 QRectF result;
166
167
167 switch (legend->alignment()) {
168 switch (legend->alignment()) {
168
169
169 case Qt::AlignTop: {
170 case Qt::AlignTop: {
170 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
171 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
171 result = geometry.adjusted(0,legendRect.height(),0,0);
172 result = geometry.adjusted(0,legendRect.height(),0,0);
172 break;
173 break;
173 }
174 }
174 case Qt::AlignBottom: {
175 case Qt::AlignBottom: {
175 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
176 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
176 result = geometry.adjusted(0,0,0,-legendRect.height());
177 result = geometry.adjusted(0,0,0,-legendRect.height());
177 break;
178 break;
178 }
179 }
179 case Qt::AlignLeft: {
180 case Qt::AlignLeft: {
180 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
181 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
181 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
182 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
182 result = geometry.adjusted(width,0,0,0);
183 result = geometry.adjusted(width,0,0,0);
183 break;
184 break;
184 }
185 }
185 case Qt::AlignRight: {
186 case Qt::AlignRight: {
186 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
187 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
187 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
188 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
188 result = geometry.adjusted(0,0,-width,0);
189 result = geometry.adjusted(0,0,-width,0);
189 break;
190 break;
190 }
191 }
191 default: {
192 default: {
192 break;
193 break;
193 }
194 }
194 }
195 }
195
196
196 legend->setGeometry(legendRect);
197 legend->setGeometry(legendRect);
197
198
198 return result;
199 return result;
199 }
200 }
200
201
201 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
202 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
202 {
203 {
203 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
204 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
204 return geometry.adjusted(0,0,minSize.width(),minSize.height());
205 return geometry.adjusted(0,0,minSize.width(),minSize.height());
205 }
206 }
206
207
207 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
208 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
208 {
209 {
209 title->setGeometry(geometry);
210 title->setGeometry(geometry);
210 QPointF center = geometry.center() - title->boundingRect().center();
211 QPointF center = geometry.center() - title->boundingRect().center();
211 title->setPos(center.x(),title->pos().y());
212 title->setPos(center.x(),title->pos().y());
212 return geometry.adjusted(0,title->boundingRect().height(),0,0);
213 return geometry.adjusted(0,title->boundingRect().height(),0,0);
213 }
214 }
214
215
215 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
216 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
216 {
217 {
217 QSizeF min = title->sizeHint(Qt::MinimumSize);
218 QSizeF min = title->sizeHint(Qt::MinimumSize);
218 return minimum.adjusted(0,0,min.width(),min.height());
219 return minimum.adjusted(0,0,min.width(),min.height());
219 }
220 }
220
221
221 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
222 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
222 {
223 {
223 Q_UNUSED(constraint);
224 Q_UNUSED(constraint);
224 if(which == Qt::MinimumSize){
225 if(which == Qt::MinimumSize){
225 QList<ChartAxis*> axes = m_presenter->axisItems();
226 QList<ChartAxis*> axes = m_presenter->axisItems();
226 ChartTitle* title = m_presenter->titleElement();
227 ChartTitle* title = m_presenter->titleElement();
227 QLegend* legend = m_presenter->legend();
228 QLegend* legend = m_presenter->legend();
228 QRectF minimumRect(0,0,0,0);
229 QRectF minimumRect(0,0,0,0);
229 minimumRect = calculateBackgroundMinimum(minimumRect);
230 minimumRect = calculateBackgroundMinimum(minimumRect);
230 minimumRect = calculateContentMinimum(minimumRect);
231 minimumRect = calculateContentMinimum(minimumRect);
231 minimumRect = calculateTitleMinimum(minimumRect,title);
232 minimumRect = calculateTitleMinimum(minimumRect,title);
232 minimumRect = calculateLegendMinimum(minimumRect,legend);
233 minimumRect = calculateLegendMinimum(minimumRect,legend);
233 minimumRect = calculateAxisMinimum(minimumRect,axes);
234 minimumRect = calculateAxisMinimum(minimumRect,axes);
234 return minimumRect.united(m_minChartRect).size().toSize();
235 return minimumRect.united(m_minChartRect).size().toSize();
235 }else
236 }else
236 return QSize(-1,-1);
237 return QSize(-1,-1);
237 }
238 }
238
239
239 void ChartLayout::setMargins(const QMargins& margins)
240 void ChartLayout::setMargins(const QMargins& margins)
240 {
241 {
241
242
242 if(m_margins != margins){
243 if(m_margins != margins){
243 m_margins = margins;
244 m_margins = margins;
244 updateGeometry();
245 updateGeometry();
245 }
246 }
246 }
247 }
247
248
248 QMargins ChartLayout::margins() const
249 QMargins ChartLayout::margins() const
249 {
250 {
250 return m_margins;
251 return m_margins;
251 }
252 }
252
253
253 void ChartLayout::adjustChartGeometry()
254 void ChartLayout::adjustChartGeometry()
254 {
255 {
255 setGeometry(geometry());
256 setGeometry(geometry());
256 }
257 }
257
258
258 QTCOMMERCIALCHART_END_NAMESPACE
259 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,226 +1,259
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 "qchartview_p.h"
22 #include "qchartview_p.h"
23 #include "qchart_p.h"
23 #include "qchart_p.h"
24 #include <QGraphicsScene>
24 #include <QGraphicsScene>
25 #include <QRubberBand>
25 #include <QRubberBand>
26
26
27 /*!
27 /*!
28 \enum QChartView::RubberBand
28 \enum QChartView::RubberBand
29
29
30 This enum describes the different types of rubber bands that can be used for zoom rect selection
30 This enum describes the different types of rubber bands that can be used for zoom rect selection
31
31
32 \value NoRubberBand
32 \value NoRubberBand
33 \value VerticalRubberBand
33 \value VerticalRubberBand
34 \value HorizonalRubberBand
34 \value HorizonalRubberBand
35 \value RectangleRubberBand
35 \value RectangleRubberBand
36 */
36 */
37
37
38 /*!
38 /*!
39 \class QChartView
39 \class QChartView
40 \brief Standalone charting widget.
40 \brief Standalone charting widget.
41
41
42 QChartView is a standalone widget that can display charts. It does not require separate
42 QChartView is a standalone widget that can display charts. It does not require separate
43 QGraphicsScene to work. It manages the graphical representation of different types of
43 QGraphicsScene to work. It manages the graphical representation of different types of
44 series and other chart related objects like QAxis and QLegend. If you want to
44 series and other chart related objects like QAxis and QLegend. If you want to
45 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
45 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
46
46
47 \sa QChart
47 \sa QChart
48 */
48 */
49
49
50 QTCOMMERCIALCHART_BEGIN_NAMESPACE
50 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51
51
52 /*!
52 /*!
53 Constructs a chartView object with parent \a parent.
53 Constructs a chartView object with parent \a parent.
54 */
54 */
55
55
56 QChartView::QChartView(QWidget *parent) :
56 QChartView::QChartView(QWidget *parent) :
57 QGraphicsView(parent),
57 QGraphicsView(parent),
58 d_ptr(new QChartViewPrivate(this))
58 d_ptr(new QChartViewPrivate(this))
59 {
59 {
60
60
61 }
61 }
62
62
63 /*!
63 /*!
64 Constructs a chartView object with parent \a parent to display a \a chart.
64 Constructs a chartView object with parent \a parent to display a \a chart.
65 */
65 */
66
66
67 QChartView::QChartView(QChart *chart,QWidget *parent) :
67 QChartView::QChartView(QChart *chart,QWidget *parent) :
68 QGraphicsView(parent),
68 QGraphicsView(parent),
69 d_ptr(new QChartViewPrivate(this,chart))
69 d_ptr(new QChartViewPrivate(this,chart))
70 {
70 {
71
71
72 }
72 }
73
73
74
74
75 /*!
75 /*!
76 Destroys the object and it's children, like series and axis objects added to it.
76 Destroys the object and it's children, like series and axis objects added to it.
77 */
77 */
78 QChartView::~QChartView()
78 QChartView::~QChartView()
79 {
79 {
80 }
80 }
81
81
82 /*!
82 /*!
83 Returns the pointer to the associated chart
83 Returns the pointer to the associated chart
84 */
84 */
85 QChart* QChartView::chart() const
85 QChart* QChartView::chart() const
86 {
86 {
87 return d_ptr->m_chart;
87 return d_ptr->m_chart;
88 }
88 }
89
89
90 /*!
90 /*!
91 Sets the current chart to \a chart. Ownership of the new chart is passed to chartview
92 and ownership of the previous chart is released.
93
94 To avoid memory leaks users needs to make sure the previous chart is deleted.
95 */
96
97 void QChartView::setChart(QChart *chart)
98 {
99 d_ptr->setChart(chart);
100 }
101
102 /*!
91 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
103 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
92 */
104 */
93 void QChartView::setRubberBand(const RubberBands& rubberBand)
105 void QChartView::setRubberBand(const RubberBands& rubberBand)
94 {
106 {
95 d_ptr->m_rubberBandFlags=rubberBand;
107 d_ptr->m_rubberBandFlags=rubberBand;
96
108
97 if (!d_ptr->m_rubberBandFlags) {
109 if (!d_ptr->m_rubberBandFlags) {
98 delete d_ptr->m_rubberBand;
110 delete d_ptr->m_rubberBand;
99 d_ptr->m_rubberBand=0;
111 d_ptr->m_rubberBand=0;
100 return;
112 return;
101 }
113 }
102
114
103 if (!d_ptr->m_rubberBand) {
115 if (!d_ptr->m_rubberBand) {
104 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
116 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
105 d_ptr->m_rubberBand->setEnabled(true);
117 d_ptr->m_rubberBand->setEnabled(true);
106 }
118 }
107 }
119 }
108
120
109 /*!
121 /*!
110 Returns the RubberBandPolicy that is currently being used by the widget.
122 Returns the RubberBandPolicy that is currently being used by the widget.
111 */
123 */
112 QChartView::RubberBands QChartView::rubberBand() const
124 QChartView::RubberBands QChartView::rubberBand() const
113 {
125 {
114 return d_ptr->m_rubberBandFlags;
126 return d_ptr->m_rubberBandFlags;
115 }
127 }
116
128
117 /*!
129 /*!
118 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.
130 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.
119 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
131 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
120 */
132 */
121 void QChartView::mousePressEvent(QMouseEvent *event)
133 void QChartView::mousePressEvent(QMouseEvent *event)
122 {
134 {
123 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
135 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
124
136
125 QRectF plotArea = d_ptr->m_chart->plotArea();
137 QRectF plotArea = d_ptr->m_chart->plotArea();
126
138
127 if (plotArea.contains(event->pos())) {
139 if (plotArea.contains(event->pos())) {
128 d_ptr->m_rubberBandOrigin = event->pos();
140 d_ptr->m_rubberBandOrigin = event->pos();
129 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
141 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
130 d_ptr->m_rubberBand->show();
142 d_ptr->m_rubberBand->show();
131 event->accept();
143 event->accept();
132 }
144 }
133 }
145 }
134 else {
146 else {
135 QGraphicsView::mousePressEvent(event);
147 QGraphicsView::mousePressEvent(event);
136 }
148 }
137 }
149 }
138
150
139 /*!
151 /*!
140 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
152 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
141 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
153 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
142 */
154 */
143 void QChartView::mouseMoveEvent(QMouseEvent *event)
155 void QChartView::mouseMoveEvent(QMouseEvent *event)
144 {
156 {
145 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
157 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
146 QRect rect = d_ptr->m_chart->plotArea().toRect();
158 QRect rect = d_ptr->m_chart->plotArea().toRect();
147 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
159 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
148 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
160 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
149 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
161 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
150 d_ptr->m_rubberBandOrigin.setY(rect.top());
162 d_ptr->m_rubberBandOrigin.setY(rect.top());
151 height = rect.height();
163 height = rect.height();
152 }
164 }
153 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
165 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
154 d_ptr->m_rubberBandOrigin.setX(rect.left());
166 d_ptr->m_rubberBandOrigin.setX(rect.left());
155 width= rect.width();
167 width= rect.width();
156 }
168 }
157 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
169 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
158 }
170 }
159 else {
171 else {
160 QGraphicsView::mouseMoveEvent(event);
172 QGraphicsView::mouseMoveEvent(event);
161 }
173 }
162 }
174 }
163
175
164 /*!
176 /*!
165 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
177 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 it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
178 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
167 */
179 */
168 void QChartView::mouseReleaseEvent(QMouseEvent *event)
180 void QChartView::mouseReleaseEvent(QMouseEvent *event)
169 {
181 {
170 if(d_ptr->m_rubberBand) {
182 if(d_ptr->m_rubberBand) {
171 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
183 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
172 d_ptr->m_rubberBand->hide();
184 d_ptr->m_rubberBand->hide();
173 QRect rect = d_ptr->m_rubberBand->geometry();
185 QRect rect = d_ptr->m_rubberBand->geometry();
174 d_ptr->m_chart->zoomIn(rect);
186 d_ptr->m_chart->zoomIn(rect);
175 event->accept();
187 event->accept();
176 }
188 }
177
189
178 if(event->button()==Qt::RightButton){
190 if(event->button()==Qt::RightButton){
179 d_ptr->m_chart->zoomOut();
191 d_ptr->m_chart->zoomOut();
180 event->accept();
192 event->accept();
181 }
193 }
182 }
194 }
183 else {
195 else {
184 QGraphicsView::mouseReleaseEvent(event);
196 QGraphicsView::mouseReleaseEvent(event);
185 }
197 }
186 }
198 }
187
199
188 /*!
200 /*!
189 Resizes and updates the chart area using the \a event data
201 Resizes and updates the chart area using the \a event data
190 */
202 */
191 void QChartView::resizeEvent(QResizeEvent *event)
203 void QChartView::resizeEvent(QResizeEvent *event)
192 {
204 {
193 QGraphicsView::resizeEvent(event);
205 QGraphicsView::resizeEvent(event);
194 d_ptr->m_chart->resize(size());
206 d_ptr->resize();
195 setMinimumSize(d_ptr->m_chart->minimumSize().toSize());
196 setSceneRect(d_ptr->m_chart->geometry());
197 }
207 }
198
208
199 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
200
210
201 QChartViewPrivate::QChartViewPrivate(QChartView *q,QChart* chart):
211 QChartViewPrivate::QChartViewPrivate(QChartView *q,QChart* chart):
202 q_ptr(q),
212 q_ptr(q),
203 m_scene(new QGraphicsScene(q)),
213 m_scene(new QGraphicsScene(q)),
204 m_chart(chart),
214 m_chart(chart),
205 m_presenter(0),
215 m_presenter(0),
206 m_rubberBand(0),
216 m_rubberBand(0),
207 m_rubberBandFlags(QChartView::NoRubberBand)
217 m_rubberBandFlags(QChartView::NoRubberBand)
208 {
218 {
209 q_ptr->setFrameShape(QFrame::NoFrame);
219 q_ptr->setFrameShape(QFrame::NoFrame);
210 q_ptr->setBackgroundRole(QPalette::Window);
220 q_ptr->setBackgroundRole(QPalette::Window);
211 q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
221 q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
212 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
222 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
213 q_ptr->setScene(m_scene);
223 q_ptr->setScene(m_scene);
214 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
224 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
215 if(!m_chart) m_chart = new QChart();
225 if(!m_chart) m_chart = new QChart();
216 m_scene->addItem(m_chart);
226 m_scene->addItem(m_chart);
217 }
227 }
218
228
219 QChartViewPrivate::~QChartViewPrivate()
229 QChartViewPrivate::~QChartViewPrivate()
220 {
230 {
221
231
222 }
232 }
223
233
234 void QChartViewPrivate::setChart(QChart *chart)
235 {
236 Q_ASSERT(chart);
237
238 if (m_chart == chart)
239 return;
240
241 if (m_chart)
242 m_scene->removeItem(m_chart);
243
244 m_chart = chart;
245 m_scene->addItem(m_chart);
246
247 resize();
248 }
249
250 void QChartViewPrivate::resize()
251 {
252 m_chart->resize(q_ptr->size());
253 q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
254 q_ptr->setSceneRect(m_chart->geometry());
255 }
256
224 #include "moc_qchartview.cpp"
257 #include "moc_qchartview.cpp"
225
258
226 QTCOMMERCIALCHART_END_NAMESPACE
259 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,74
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 #ifndef QCHARTVIEW_H
21 #ifndef QCHARTVIEW_H
22 #define QCHARTVIEW_H
22 #define QCHARTVIEW_H
23
23
24 #include <QAbstractAxis>
24 #include <QAbstractAxis>
25 #include <QAbstractSeries>
25 #include <QAbstractSeries>
26 #include <QChart>
26 #include <QChart>
27 #include <QGraphicsView>
27 #include <QGraphicsView>
28
28
29 class QGraphicsScene;
29 class QGraphicsScene;
30 class QRubberBand;
30 class QRubberBand;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 class QChartViewPrivate;
34 class QChartViewPrivate;
35
35
36 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
36 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
37 {
37 {
38 Q_OBJECT
38 Q_OBJECT
39 Q_ENUMS(RubberBand)
39 Q_ENUMS(RubberBand)
40 public:
40 public:
41
41
42 enum RubberBand{
42 enum RubberBand{
43 NoRubberBand = 0x0,
43 NoRubberBand = 0x0,
44 VerticalRubberBand = 0x1,
44 VerticalRubberBand = 0x1,
45 HorizonalRubberBand = 0x2,
45 HorizonalRubberBand = 0x2,
46 RectangleRubberBand = 0x3
46 RectangleRubberBand = 0x3
47 };
47 };
48
48
49 Q_DECLARE_FLAGS(RubberBands, RubberBand)
49 Q_DECLARE_FLAGS(RubberBands, RubberBand)
50
50
51 explicit QChartView(QWidget *parent = 0);
51 explicit QChartView(QWidget *parent = 0);
52 explicit QChartView(QChart *chart,QWidget *parent = 0);
52 explicit QChartView(QChart *chart,QWidget *parent = 0);
53 ~QChartView();
53 ~QChartView();
54
54
55 void setRubberBand(const RubberBands& rubberBands);
55 void setRubberBand(const RubberBands& rubberBands);
56 RubberBands rubberBand() const;
56 RubberBands rubberBand() const;
57
57 QChart* chart() const;
58 QChart* chart() const;
59 void setChart(QChart *chart);
58
60
59 protected:
61 protected:
60 void resizeEvent(QResizeEvent *event);
62 void resizeEvent(QResizeEvent *event);
61 void mousePressEvent(QMouseEvent *event);
63 void mousePressEvent(QMouseEvent *event);
62 void mouseMoveEvent(QMouseEvent *event);
64 void mouseMoveEvent(QMouseEvent *event);
63 void mouseReleaseEvent(QMouseEvent *event);
65 void mouseReleaseEvent(QMouseEvent *event);
64
66
65 protected:
67 protected:
66 QScopedPointer<QChartViewPrivate> d_ptr;
68 QScopedPointer<QChartViewPrivate> d_ptr;
67 Q_DISABLE_COPY(QChartView)
69 Q_DISABLE_COPY(QChartView)
68 };
70 };
69
71
70 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
71
73
72 #endif // QCHARTVIEW_H
74 #endif // QCHARTVIEW_H
@@ -1,62 +1,64
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QCHARTVIEW_P_H
30 #ifndef QCHARTVIEW_P_H
31 #define QCHARTVIEW_P_H
31 #define QCHARTVIEW_P_H
32
32
33 #include "qchartview.h"
33 #include "qchartview.h"
34
34
35 class QGraphicsScene;
35 class QGraphicsScene;
36 class ChartPresenter;
36 class ChartPresenter;
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 class QChart;
40 class QChart;
41 class QChartView;
41 class QChartView;
42
42
43 class QChartViewPrivate {
43 class QChartViewPrivate {
44
44
45 public:
45 public:
46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
47 ~QChartViewPrivate();
47 ~QChartViewPrivate();
48 void setChart(QChart *chart);
49 void resize();
48
50
49 protected:
51 protected:
50 QChartView *q_ptr;
52 QChartView *q_ptr;
51
53
52 public:
54 public:
53 QGraphicsScene *m_scene;
55 QGraphicsScene *m_scene;
54 QChart *m_chart;
56 QChart *m_chart;
55 ChartPresenter *m_presenter;
57 ChartPresenter *m_presenter;
56 QPoint m_rubberBandOrigin;
58 QPoint m_rubberBandOrigin;
57 QRubberBand *m_rubberBand;
59 QRubberBand *m_rubberBand;
58 QChartView::RubberBands m_rubberBandFlags;
60 QChartView::RubberBands m_rubberBandFlags;
59 };
61 };
60
62
61 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
62 #endif
64 #endif
@@ -1,188 +1,223
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 <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qchartview.h>
22 #include <qchartview.h>
23 #include <qlineseries.h>
23 #include <qlineseries.h>
24 #include <cmath>
24 #include <cmath>
25 #include <tst_definitions.h>
25 #include <tst_definitions.h>
26 #include <qvalueaxis.h>
26 #include <qvalueaxis.h>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30
30
31 Q_DECLARE_METATYPE(QChart*)
31 Q_DECLARE_METATYPE(QChart*)
32 Q_DECLARE_METATYPE(QChartView::RubberBands)
32 Q_DECLARE_METATYPE(QChartView::RubberBands)
33 Q_DECLARE_METATYPE(Qt::Key)
33 Q_DECLARE_METATYPE(Qt::Key)
34
34
35 class tst_QChartView : public QObject
35 class tst_QChartView : public QObject
36 {
36 {
37 Q_OBJECT
37 Q_OBJECT
38
38
39 public Q_SLOTS:
39 public Q_SLOTS:
40 void initTestCase();
40 void initTestCase();
41 void cleanupTestCase();
41 void cleanupTestCase();
42 void init();
42 void init();
43 void cleanup();
43 void cleanup();
44
44
45 private Q_SLOTS:
45 private Q_SLOTS:
46 void qchartview_data();
46 void qchartview_data();
47 void qchartview();
47 void qchartview();
48 void chart_data();
48 void chart_data();
49 void chart();
49 void chart();
50 void rubberBand_data();
50 void rubberBand_data();
51 void rubberBand();
51 void rubberBand();
52 void setChart();
52
53
53 private:
54 private:
54 QChartView* m_view;
55 QChartView* m_view;
55 };
56 };
56
57
57 void tst_QChartView::initTestCase()
58 void tst_QChartView::initTestCase()
58 {
59 {
59 //test tracks mouse, give a while to user to relese it
60 //test tracks mouse, give a while to user to relese it
60 QTest::qWait(1000);
61 QTest::qWait(1000);
61 }
62 }
62
63
63 void tst_QChartView::cleanupTestCase()
64 void tst_QChartView::cleanupTestCase()
64 {
65 {
65 }
66 }
66
67
67 void tst_QChartView::init()
68 void tst_QChartView::init()
68 {
69 {
69 m_view = new QChartView(new QChart());
70 m_view = new QChartView(new QChart());
70 m_view->chart()->legend()->setVisible(false);
71 m_view->chart()->legend()->setVisible(false);
71 }
72 }
72
73
73 void tst_QChartView::cleanup()
74 void tst_QChartView::cleanup()
74 {
75 {
75 delete m_view;
76 delete m_view;
76 m_view =0;
77 m_view =0;
77 }
78 }
78
79
79 void tst_QChartView::qchartview_data()
80 void tst_QChartView::qchartview_data()
80 {
81 {
81
82
82 }
83 }
83
84
84 void tst_QChartView::qchartview()
85 void tst_QChartView::qchartview()
85 {
86 {
86 QVERIFY(m_view->chart());
87 QVERIFY(m_view->chart());
87 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
88 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
88 m_view->show();
89 m_view->show();
89 QTest::qWaitForWindowShown(m_view);
90 QTest::qWaitForWindowShown(m_view);
90
91
91 delete(new QChartView());
92 delete(new QChartView());
92
93
93 QChartView view;
94 QChartView view;
94 QVERIFY(view.chart());
95 QVERIFY(view.chart());
95
96
96 }
97 }
97
98
98 void tst_QChartView::chart_data()
99 void tst_QChartView::chart_data()
99 {
100 {
100
101
101 QTest::addColumn<QChart*>("chart");
102 QTest::addColumn<QChart*>("chart");
102 QTest::newRow("qchart") << new QChart();
103 QTest::newRow("qchart") << new QChart();
103 }
104 }
104
105
105 void tst_QChartView::chart()
106 void tst_QChartView::chart()
106 {
107 {
107 QFETCH(QChart*, chart);
108 QFETCH(QChart*, chart);
108 QChartView* view = new QChartView(chart);
109 QChartView* view = new QChartView(chart);
109 QCOMPARE(view->chart(), chart);
110 QCOMPARE(view->chart(), chart);
110 delete view;
111 delete view;
111 }
112 }
112
113
113 void tst_QChartView::rubberBand_data()
114 void tst_QChartView::rubberBand_data()
114 {
115 {
115 QTest::addColumn<QChartView::RubberBands>("rubberBand");
116 QTest::addColumn<QChartView::RubberBands>("rubberBand");
116 QTest::addColumn<int>("Xcount");
117 QTest::addColumn<int>("Xcount");
117 QTest::addColumn<int>("Ycount");
118 QTest::addColumn<int>("Ycount");
118
119
119 QTest::addColumn<QPoint>("min");
120 QTest::addColumn<QPoint>("min");
120 QTest::addColumn<QPoint>("max");
121 QTest::addColumn<QPoint>("max");
121
122
122 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << QPoint(5,5) << QPoint(5,5);
123 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << QPoint(5,5) << QPoint(5,5);
123 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << QPoint(5,5) << QPoint(5,5);
124 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << QPoint(5,5) << QPoint(5,5);
124 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 << QPoint(5,5) << QPoint(5,5);
125 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 << QPoint(5,5) << QPoint(5,5);
125 }
126 }
126
127
127 void tst_QChartView::rubberBand()
128 void tst_QChartView::rubberBand()
128 {
129 {
129 QFETCH(QChartView::RubberBands, rubberBand);
130 QFETCH(QChartView::RubberBands, rubberBand);
130 QFETCH(int, Xcount);
131 QFETCH(int, Xcount);
131 QFETCH(int, Ycount);
132 QFETCH(int, Ycount);
132 QFETCH(QPoint, min);
133 QFETCH(QPoint, min);
133 QFETCH(QPoint, max);
134 QFETCH(QPoint, max);
134
135
135 m_view->setRubberBand(rubberBand);
136 m_view->setRubberBand(rubberBand);
136
137
137 QCOMPARE(m_view->rubberBand(), rubberBand);
138 QCOMPARE(m_view->rubberBand(), rubberBand);
138
139
139 QLineSeries* line = new QLineSeries();
140 QLineSeries* line = new QLineSeries();
140 *line << QPointF(0, 0) << QPointF(200, 200);
141 *line << QPointF(0, 0) << QPointF(200, 200);
141
142
142 m_view->chart()->addSeries(line);
143 m_view->chart()->addSeries(line);
143 m_view->chart()->createDefaultAxes();
144 m_view->chart()->createDefaultAxes();
144 m_view->show();
145 m_view->show();
145
146
146 QRectF plotArea = m_view->chart()->plotArea();
147 QRectF plotArea = m_view->chart()->plotArea();
147 //this is hack since view does not get events otherwise
148 //this is hack since view does not get events otherwise
148 m_view->setMouseTracking(true);
149 m_view->setMouseTracking(true);
149
150
150 QAbstractAxis* axisY = m_view->chart()->axisY();
151 QAbstractAxis* axisY = m_view->chart()->axisY();
151 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
152 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
152 QAbstractAxis* axisX = m_view->chart()->axisX();
153 QAbstractAxis* axisX = m_view->chart()->axisX();
153 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
154 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
154
155
155
156
156 QValueAxis* vaxisX = qobject_cast<QValueAxis*>(axisX);
157 QValueAxis* vaxisX = qobject_cast<QValueAxis*>(axisX);
157 QValueAxis* vaxisY = qobject_cast<QValueAxis*>(axisY);
158 QValueAxis* vaxisY = qobject_cast<QValueAxis*>(axisY);
158
159
159 int minX = vaxisX->min();
160 int minX = vaxisX->min();
160 int minY = vaxisY->min();
161 int minY = vaxisY->min();
161 int maxX = vaxisX->max();
162 int maxX = vaxisX->max();
162 int maxY = vaxisY->max();
163 int maxY = vaxisY->max();
163
164
164 QTest::qWaitForWindowShown(m_view);
165 QTest::qWaitForWindowShown(m_view);
165 QTest::mouseMove(m_view->viewport(), min + plotArea.topLeft().toPoint());
166 QTest::mouseMove(m_view->viewport(), min + plotArea.topLeft().toPoint());
166 QTest::qWait(2000);
167 QTest::qWait(2000);
167 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, min + plotArea.topLeft().toPoint());
168 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, min + plotArea.topLeft().toPoint());
168 QTest::qWait(2000);
169 QTest::qWait(2000);
169 QTest::mouseMove(m_view->viewport(), plotArea.bottomRight().toPoint() - max);
170 QTest::mouseMove(m_view->viewport(), plotArea.bottomRight().toPoint() - max);
170 QTest::qWait(2000);
171 QTest::qWait(2000);
171 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, plotArea.bottomRight().toPoint() - max);
172 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, plotArea.bottomRight().toPoint() - max);
172
173
173 TRY_COMPARE(spy0.count(), Xcount);
174 TRY_COMPARE(spy0.count(), Xcount);
174 TRY_COMPARE(spy1.count(), Ycount);
175 TRY_COMPARE(spy1.count(), Ycount);
175
176
176 //this is hack since view does not get events otherwise
177 //this is hack since view does not get events otherwise
177 m_view->setMouseTracking(false);
178 m_view->setMouseTracking(false);
178
179
179 QVERIFY(vaxisX->min() >= minX );
180 QVERIFY(vaxisX->min() >= minX );
180 QVERIFY(vaxisX->max() <= maxX );
181 QVERIFY(vaxisX->max() <= maxX );
181 QVERIFY(vaxisY->min() >= minY );
182 QVERIFY(vaxisY->min() >= minY );
182 QVERIFY(vaxisY->max() <= maxY );
183 QVERIFY(vaxisY->max() <= maxY );
183
184
184 }
185 }
185
186
187 void tst_QChartView::setChart()
188 {
189 QPointer<QChart> oldChart = m_view->chart();
190 QLineSeries *series1 = new QLineSeries();
191 series1->append(0,0);
192 series1->append(1,1);
193 oldChart->addSeries(series1);
194
195 QPointer<QChart> newChart = new QChart();
196 QLineSeries *series2 = new QLineSeries();
197 series2->append(0,1);
198 series2->append(1,0);
199 newChart->addSeries(series2);
200
201 // show current chart
202 m_view->show();
203 QTest::qWaitForWindowShown(m_view);
204 QTest::qWait(1000);
205
206 // set new chart
207 m_view->setChart(newChart);
208 QVERIFY(m_view->chart() == newChart);
209 QVERIFY(!oldChart.isNull()); // ownership of the oldChart is released and not deleted
210 QTest::qWait(1000);
211
212 // delete the view
213 delete m_view;
214 m_view = 0;
215 QVERIFY(newChart.isNull()); // view owns and deletes it
216 QVERIFY(!oldChart.isNull()); // not owned by view
217
218 delete oldChart;
219 }
220
186 QTEST_MAIN(tst_QChartView)
221 QTEST_MAIN(tst_QChartView)
187 #include "tst_qchartview.moc"
222 #include "tst_qchartview.moc"
188
223
General Comments 0
You need to be logged in to leave comments. Login now