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