##// END OF EJS Templates
Fix zooming when presenter dimensions are not integers...
Miikka Heikkinen -
r2416:25b2e1c316cb
parent child
Show More
@@ -1,256 +1,266
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
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.
92 and ownership of the previous chart is released.
93
93
94 To avoid memory leaks users needs to make sure the previous chart is deleted.
94 To avoid memory leaks users needs to make sure the previous chart is deleted.
95 */
95 */
96
96
97 void QChartView::setChart(QChart *chart)
97 void QChartView::setChart(QChart *chart)
98 {
98 {
99 d_ptr->setChart(chart);
99 d_ptr->setChart(chart);
100 }
100 }
101
101
102 /*!
102 /*!
103 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.
104 */
104 */
105 void QChartView::setRubberBand(const RubberBands &rubberBand)
105 void QChartView::setRubberBand(const RubberBands &rubberBand)
106 {
106 {
107 d_ptr->m_rubberBandFlags = rubberBand;
107 d_ptr->m_rubberBandFlags = rubberBand;
108
108
109 if (!d_ptr->m_rubberBandFlags) {
109 if (!d_ptr->m_rubberBandFlags) {
110 delete d_ptr->m_rubberBand;
110 delete d_ptr->m_rubberBand;
111 d_ptr->m_rubberBand = 0;
111 d_ptr->m_rubberBand = 0;
112 return;
112 return;
113 }
113 }
114
114
115 if (!d_ptr->m_rubberBand) {
115 if (!d_ptr->m_rubberBand) {
116 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
116 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
117 d_ptr->m_rubberBand->setEnabled(true);
117 d_ptr->m_rubberBand->setEnabled(true);
118 }
118 }
119 }
119 }
120
120
121 /*!
121 /*!
122 Returns the RubberBandPolicy that is currently being used by the widget.
122 Returns the RubberBandPolicy that is currently being used by the widget.
123 */
123 */
124 QChartView::RubberBands QChartView::rubberBand() const
124 QChartView::RubberBands QChartView::rubberBand() const
125 {
125 {
126 return d_ptr->m_rubberBandFlags;
126 return d_ptr->m_rubberBandFlags;
127 }
127 }
128
128
129 /*!
129 /*!
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.
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.
131 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.
132 */
132 */
133 void QChartView::mousePressEvent(QMouseEvent *event)
133 void QChartView::mousePressEvent(QMouseEvent *event)
134 {
134 {
135 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) {
136
136
137 QRectF plotArea = d_ptr->m_chart->plotArea();
137 QRectF plotArea = d_ptr->m_chart->plotArea();
138
138
139 if (plotArea.contains(event->pos())) {
139 if (plotArea.contains(event->pos())) {
140 d_ptr->m_rubberBandOrigin = event->pos();
140 d_ptr->m_rubberBandOrigin = event->pos();
141 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
141 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
142 d_ptr->m_rubberBand->show();
142 d_ptr->m_rubberBand->show();
143 event->accept();
143 event->accept();
144 }
144 }
145 } else {
145 } else {
146 QGraphicsView::mousePressEvent(event);
146 QGraphicsView::mousePressEvent(event);
147 }
147 }
148 }
148 }
149
149
150 /*!
150 /*!
151 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
151 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
152 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
152 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
153 */
153 */
154 void QChartView::mouseMoveEvent(QMouseEvent *event)
154 void QChartView::mouseMoveEvent(QMouseEvent *event)
155 {
155 {
156 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
156 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
157 QRect rect = d_ptr->m_chart->plotArea().toRect();
157 QRect rect = d_ptr->m_chart->plotArea().toRect();
158 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
158 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
159 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
159 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
160 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
160 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
161 d_ptr->m_rubberBandOrigin.setY(rect.top());
161 d_ptr->m_rubberBandOrigin.setY(rect.top());
162 height = rect.height();
162 height = rect.height();
163 }
163 }
164 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
164 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
165 d_ptr->m_rubberBandOrigin.setX(rect.left());
165 d_ptr->m_rubberBandOrigin.setX(rect.left());
166 width = rect.width();
166 width = rect.width();
167 }
167 }
168 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized());
168 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized());
169 } else {
169 } else {
170 QGraphicsView::mouseMoveEvent(event);
170 QGraphicsView::mouseMoveEvent(event);
171 }
171 }
172 }
172 }
173
173
174 /*!
174 /*!
175 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
175 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
176 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
176 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
177 */
177 */
178 void QChartView::mouseReleaseEvent(QMouseEvent *event)
178 void QChartView::mouseReleaseEvent(QMouseEvent *event)
179 {
179 {
180 if (d_ptr->m_rubberBand) {
180 if (d_ptr->m_rubberBand) {
181 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
181 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
182 d_ptr->m_rubberBand->hide();
182 d_ptr->m_rubberBand->hide();
183 QRect rect = d_ptr->m_rubberBand->geometry();
183 QRectF rect = d_ptr->m_rubberBand->geometry();
184 // Since plotArea uses QRectF and rubberband uses QRect, we can't just blindly use
185 // rubberband's dimensions for vertical and horizontal rubberbands, where one
186 // dimension must match the corresponding plotArea dimension exactly.
187 if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
188 rect.setX(d_ptr->m_chart->plotArea().x());
189 rect.setWidth(d_ptr->m_chart->plotArea().width());
190 } else if (d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
191 rect.setY(d_ptr->m_chart->plotArea().y());
192 rect.setHeight(d_ptr->m_chart->plotArea().height());
193 }
184 d_ptr->m_chart->zoomIn(rect);
194 d_ptr->m_chart->zoomIn(rect);
185 event->accept();
195 event->accept();
186 }
196 }
187
197
188 if (event->button() == Qt::RightButton) {
198 if (event->button() == Qt::RightButton) {
189 d_ptr->m_chart->zoomOut();
199 d_ptr->m_chart->zoomOut();
190 event->accept();
200 event->accept();
191 }
201 }
192 } else {
202 } else {
193 QGraphicsView::mouseReleaseEvent(event);
203 QGraphicsView::mouseReleaseEvent(event);
194 }
204 }
195 }
205 }
196
206
197 /*!
207 /*!
198 Resizes and updates the chart area using the \a event data
208 Resizes and updates the chart area using the \a event data
199 */
209 */
200 void QChartView::resizeEvent(QResizeEvent *event)
210 void QChartView::resizeEvent(QResizeEvent *event)
201 {
211 {
202 QGraphicsView::resizeEvent(event);
212 QGraphicsView::resizeEvent(event);
203 d_ptr->resize();
213 d_ptr->resize();
204 }
214 }
205
215
206 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
207
217
208 QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart *chart)
218 QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart *chart)
209 : q_ptr(q),
219 : q_ptr(q),
210 m_scene(new QGraphicsScene(q)),
220 m_scene(new QGraphicsScene(q)),
211 m_chart(chart),
221 m_chart(chart),
212 m_rubberBand(0),
222 m_rubberBand(0),
213 m_rubberBandFlags(QChartView::NoRubberBand)
223 m_rubberBandFlags(QChartView::NoRubberBand)
214 {
224 {
215 q_ptr->setFrameShape(QFrame::NoFrame);
225 q_ptr->setFrameShape(QFrame::NoFrame);
216 q_ptr->setBackgroundRole(QPalette::Window);
226 q_ptr->setBackgroundRole(QPalette::Window);
217 q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
227 q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
218 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
228 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
219 q_ptr->setScene(m_scene);
229 q_ptr->setScene(m_scene);
220 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
230 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
221 if (!m_chart)
231 if (!m_chart)
222 m_chart = new QChart();
232 m_chart = new QChart();
223 m_scene->addItem(m_chart);
233 m_scene->addItem(m_chart);
224 }
234 }
225
235
226 QChartViewPrivate::~QChartViewPrivate()
236 QChartViewPrivate::~QChartViewPrivate()
227 {
237 {
228
238
229 }
239 }
230
240
231 void QChartViewPrivate::setChart(QChart *chart)
241 void QChartViewPrivate::setChart(QChart *chart)
232 {
242 {
233 Q_ASSERT(chart);
243 Q_ASSERT(chart);
234
244
235 if (m_chart == chart)
245 if (m_chart == chart)
236 return;
246 return;
237
247
238 if (m_chart)
248 if (m_chart)
239 m_scene->removeItem(m_chart);
249 m_scene->removeItem(m_chart);
240
250
241 m_chart = chart;
251 m_chart = chart;
242 m_scene->addItem(m_chart);
252 m_scene->addItem(m_chart);
243
253
244 resize();
254 resize();
245 }
255 }
246
256
247 void QChartViewPrivate::resize()
257 void QChartViewPrivate::resize()
248 {
258 {
249 m_chart->resize(q_ptr->size());
259 m_chart->resize(q_ptr->size());
250 q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
260 q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
251 q_ptr->setSceneRect(m_chart->geometry());
261 q_ptr->setSceneRect(m_chart->geometry());
252 }
262 }
253
263
254 #include "moc_qchartview.cpp"
264 #include "moc_qchartview.cpp"
255
265
256 QTCOMMERCIALCHART_END_NAMESPACE
266 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now