##// END OF EJS Templates
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
Michal Klocek -
r1290:d3d22419eb01
parent child
Show More
@@ -1,218 +1,229
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 Constructs a chartView object with parent \a parent.
54 */
55
56 QChartView::QChartView(QWidget *parent) :
57 QGraphicsView(parent),
58 d_ptr(new QChartViewPrivate(this))
59 {
60
61 }
62
63 /*!
53 64 Constructs a chartView object with parent \a parent to display a \a chart.
54 65 */
66
55 67 QChartView::QChartView(QChart *chart,QWidget *parent) :
56 68 QGraphicsView(parent),
57 d_ptr(new QChartViewPrivate())
69 d_ptr(new QChartViewPrivate(this,chart))
58 70 {
59 Q_ASSERT(chart);
60 d_ptr->m_scene = new QGraphicsScene(this);
61 d_ptr->m_chart = chart;
62 setFrameShape(QFrame::NoFrame);
63 setBackgroundRole(QPalette::Window);
64 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 setScene(d_ptr->m_scene);
67 d_ptr->m_scene->addItem(chart);
68 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
71
69 72 }
70 73
71 74
72 75 /*!
73 76 Destroys the object and it's children, like series and axis objects added to it.
74 77 */
75 78 QChartView::~QChartView()
76 79 {
77 80 }
78 81
79 82 /*!
80 83 Returns the pointer to the associated chart
81 84 */
82 85 QChart* QChartView::chart() const
83 86 {
84 87 return d_ptr->m_chart;
85 88 }
86 89
87 90 /*!
88 91 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
89 92 */
90 93 void QChartView::setRubberBand(const RubberBands& rubberBand)
91 94 {
92 95 d_ptr->m_rubberBandFlags=rubberBand;
93 96
94 97 if (!d_ptr->m_rubberBandFlags) {
95 98 delete d_ptr->m_rubberBand;
96 99 d_ptr->m_rubberBand=0;
97 100 return;
98 101 }
99 102
100 103 if (!d_ptr->m_rubberBand) {
101 104 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
102 105 d_ptr->m_rubberBand->setEnabled(true);
103 106 }
104 107 }
105 108
106 109 /*!
107 110 Returns the RubberBandPolicy that is currently being used by the widget.
108 111 */
109 112 QChartView::RubberBands QChartView::rubberBand() const
110 113 {
111 114 return d_ptr->m_rubberBandFlags;
112 115 }
113 116
114 117 /*!
115 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.
116 119 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
117 120 */
118 121 void QChartView::mousePressEvent(QMouseEvent *event)
119 122 {
120 123 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
121 124
122 125 int padding = d_ptr->m_chart->margins().top();
123 126 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
124 127
125 128 if (rect.contains(event->pos())) {
126 129 d_ptr->m_rubberBandOrigin = event->pos();
127 130 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
128 131 d_ptr->m_rubberBand->show();
129 132 event->accept();
130 133 }
131 134 }
132 135 else {
133 136 QGraphicsView::mousePressEvent(event);
134 137 }
135 138 }
136 139
137 140 /*!
138 141 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
139 142 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
140 143 */
141 144 void QChartView::mouseMoveEvent(QMouseEvent *event)
142 145 {
143 146 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
144 147 QRectF margins = d_ptr->m_chart->margins();
145 148 QRectF geometry = d_ptr->m_chart->geometry();
146 149 QRectF rect =geometry.adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom());
147 150 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
148 151 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
149 152 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
150 153 d_ptr->m_rubberBandOrigin.setY(rect.top());
151 154 height = rect.height();
152 155 }
153 156 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
154 157 d_ptr->m_rubberBandOrigin.setX(rect.left());
155 158 width= rect.width();
156 159 }
157 160 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
158 161 }
159 162 else {
160 163 QGraphicsView::mouseMoveEvent(event);
161 164 }
162 165 }
163 166
164 167 /*!
165 168 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 169 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
167 170 */
168 171 void QChartView::mouseReleaseEvent(QMouseEvent *event)
169 172 {
170 173 if(d_ptr->m_rubberBand) {
171 174 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
172 175 d_ptr->m_rubberBand->hide();
173 176 QRect rect = d_ptr->m_rubberBand->geometry();
174 177 d_ptr->m_chart->zoomIn(rect);
175 178 event->accept();
176 179 }
177 180
178 181 if(event->button()==Qt::RightButton){
179 182 d_ptr->m_chart->zoomOut();
180 183 event->accept();
181 184 }
182 185 }
183 186 else {
184 187 QGraphicsView::mouseReleaseEvent(event);
185 188 }
186 189 }
187 190
188 191 /*!
189 192 Resizes and updates the chart area using the \a event data
190 193 */
191 194 void QChartView::resizeEvent(QResizeEvent *event)
192 195 {
193 196 QGraphicsView::resizeEvent(event);
194 197 d_ptr->m_chart->resize(size());
195 198 setMinimumSize(d_ptr->m_chart->minimumSize().toSize());
196 199 setSceneRect(d_ptr->m_chart->geometry());
197 200 }
198 201
199 202 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
200 203
201 QChartViewPrivate::QChartViewPrivate():
202 m_scene(0),
203 m_chart(0),
204 QChartViewPrivate::QChartViewPrivate(QChartView *q,QChart* chart):
205 q_ptr(q),
206 m_scene(new QGraphicsScene(q)),
207 m_chart(chart),
204 208 m_presenter(0),
205 209 m_rubberBand(0),
206 210 m_rubberBandFlags(QChartView::NoRubberBand)
207 211 {
208
212 q_ptr->setFrameShape(QFrame::NoFrame);
213 q_ptr->setBackgroundRole(QPalette::Window);
214 q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
215 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
216 q_ptr->setScene(m_scene);
217 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
218 if(!m_chart) m_chart = new QChart();
219 m_scene->addItem(m_chart);
209 220 }
210 221
211 222 QChartViewPrivate::~QChartViewPrivate()
212 223 {
213 224
214 225 }
215 226
216 227 #include "moc_qchartview.cpp"
217 228
218 229 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,72
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 <QAxis>
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 struct 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 explicit QChartView(QWidget *parent = 0);
51 52 explicit QChartView(QChart *chart,QWidget *parent = 0);
52 53 ~QChartView();
53 54
54 55 void setRubberBand(const RubberBands& rubberBands);
55 56 RubberBands rubberBand() const;
56 57 QChart* chart() const;
57 58
58 59 protected:
59 60 void resizeEvent(QResizeEvent *event);
60 61 void mousePressEvent(QMouseEvent *event);
61 62 void mouseMoveEvent(QMouseEvent *event);
62 63 void mouseReleaseEvent(QMouseEvent *event);
63 64
64 65 protected:
65 66 QScopedPointer<QChartViewPrivate> d_ptr;
66 67 Q_DISABLE_COPY(QChartView)
67 68 };
68 69
69 70 QTCOMMERCIALCHART_END_NAMESPACE
70 71
71 72 #endif // QCHARTWIDGET_H
@@ -1,56 +1,62
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 class QChartView;
41 42
42 struct QChartViewPrivate {
43 class QChartViewPrivate {
43 44
44 QChartViewPrivate();
45 public:
46 QChartViewPrivate(QChartView *q,QChart *chart = 0);
45 47 ~QChartViewPrivate();
46 48
49 protected:
50 QChartView *q_ptr;
51
52 public:
47 53 QGraphicsScene *m_scene;
48 54 QChart *m_chart;
49 55 ChartPresenter *m_presenter;
50 56 QPoint m_rubberBandOrigin;
51 57 QRubberBand *m_rubberBand;
52 58 QChartView::RubberBands m_rubberBandFlags;
53 59 };
54 60
55 61 QTCOMMERCIALCHART_END_NAMESPACE
56 62 #endif
@@ -1,171 +1,177
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
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29
30 30 Q_DECLARE_METATYPE(QChart*)
31 31 Q_DECLARE_METATYPE(QChartView::RubberBands)
32 32 Q_DECLARE_METATYPE(Qt::Key)
33 33
34 34 class tst_QChartView : public QObject
35 35 {
36 36 Q_OBJECT
37 37
38 38 public Q_SLOTS:
39 39 void initTestCase();
40 40 void cleanupTestCase();
41 41 void init();
42 42 void cleanup();
43 43
44 44 private Q_SLOTS:
45 45 void qchartview_data();
46 46 void qchartview();
47 47 void chart_data();
48 48 void chart();
49 49 void rubberBand_data();
50 50 void rubberBand();
51 51
52 52 private:
53 53 QChartView* m_view;
54 54 };
55 55
56 56 void tst_QChartView::initTestCase()
57 57 {
58 58 //test tracks mouse, give a while to user to relese it
59 59 QTest::qWait(1000);
60 60 }
61 61
62 62 void tst_QChartView::cleanupTestCase()
63 63 {
64 64 }
65 65
66 66 void tst_QChartView::init()
67 67 {
68 68 m_view = new QChartView(new QChart());
69 69 m_view->chart()->legend()->setVisible(false);
70 70 }
71 71
72 72 void tst_QChartView::cleanup()
73 73 {
74 74 delete m_view;
75 75 m_view =0;
76 76 }
77 77
78 78 void tst_QChartView::qchartview_data()
79 79 {
80 80
81 81 }
82 82
83 83 void tst_QChartView::qchartview()
84 84 {
85 85 QVERIFY(m_view->chart());
86 86 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
87 87 m_view->show();
88 88 QTest::qWaitForWindowShown(m_view);
89
90 delete(new QChartView());
91
92 QChartView view;
93 QVERIFY(view.chart());
94
89 95 }
90 96
91 97 void tst_QChartView::chart_data()
92 98 {
93 99
94 100 QTest::addColumn<QChart*>("chart");
95 101 QTest::newRow("qchart") << new QChart();
96 102 }
97 103
98 104 void tst_QChartView::chart()
99 105 {
100 106 QFETCH(QChart*, chart);
101 107 QChartView* view = new QChartView(chart);
102 108 QCOMPARE(view->chart(), chart);
103 109 delete view;
104 110 }
105 111
106 112 void tst_QChartView::rubberBand_data()
107 113 {
108 114 QTest::addColumn<QChartView::RubberBands>("rubberBand");
109 115 QTest::addColumn<int>("Xcount");
110 116 QTest::addColumn<int>("Ycount");
111 117
112 118 QTest::addColumn<int>("minX");
113 119 QTest::addColumn<int>("maxX");
114 120 QTest::addColumn<int>("minY");
115 121 QTest::addColumn<int>("maxY");
116 122
117 123 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 20 << 180 << 0<< 200;
118 124 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 200 << 20<< 180;
119 125 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<20 << 180 << 20<< 180;
120 126 }
121 127
122 128 void tst_QChartView::rubberBand()
123 129 {
124 130 QFETCH(QChartView::RubberBands, rubberBand);
125 131 QFETCH(int, Xcount);
126 132 QFETCH(int, Ycount);
127 133 QFETCH(int, minX);
128 134 QFETCH(int, maxX);
129 135 QFETCH(int, minY);
130 136 QFETCH(int, maxY);
131 137
132 138 m_view->setRubberBand(rubberBand);
133 139 QRectF padding = m_view->chart()->margins();
134 140 QCOMPARE(m_view->rubberBand(), rubberBand);
135 141
136 142 QLineSeries* line = new QLineSeries();
137 143 *line << QPointF(0, 0) << QPointF(200, 200);
138 144
139 145 m_view->chart()->addSeries(line);
140 146 m_view->resize(200 + padding.left() + padding.right(), 200 + padding.top()+ padding.bottom());
141 147 m_view->show();
142 148
143 149 //this is hack since view does not get events otherwise
144 150 m_view->setMouseTracking(true);
145 151
146 152 QAxis* axisY = m_view->chart()->axisY();
147 153 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
148 154 QAxis* axisX = m_view->chart()->axisX();
149 155 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
150 156
151 157 QTest::qWaitForWindowShown(m_view);
152 158 QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft().toPoint());
153 159 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft().toPoint());
154 160 QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft().toPoint());
155 161 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft().toPoint());
156 162
157 163 TRY_COMPARE(spy0.count(), Xcount);
158 164 TRY_COMPARE(spy1.count(), Ycount);
159 165
160 166 //this is hack since view does not get events otherwise
161 167 m_view->setMouseTracking(false);
162 168
163 169 QVERIFY(axisX->min() - minX < 1);
164 170 QVERIFY(axisX->max() - maxX < 1);
165 171 QVERIFY(axisY->min() - minY < 1);
166 172 QVERIFY(axisY->max() - maxY < 1);
167 173 }
168 174
169 175 QTEST_MAIN(tst_QChartView)
170 176 #include "tst_qchartview.moc"
171 177
General Comments 0
You need to be logged in to leave comments. Login now