##// END OF EJS Templates
Fix regression with right click zooming...
Titta Heikkala -
r2656:632e2826c0ad
parent child
Show More
@@ -1,318 +1,317
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 \inmodule Qt Charts
41 41 \brief Standalone charting widget.
42 42
43 43 QChartView is a standalone widget that can display charts. It does not require separate
44 44 QGraphicsScene to work. If you want to display a chart in your existing QGraphicsScene,
45 45 you need to use the QChart (or QPolarChart) class instead.
46 46
47 47 \sa QChart, QPolarChart
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 Ownership of the \a chart is passed to chartview.
66 66 */
67 67
68 68 QChartView::QChartView(QChart *chart, QWidget *parent)
69 69 : QGraphicsView(parent),
70 70 d_ptr(new QChartViewPrivate(this, chart))
71 71 {
72 72
73 73 }
74 74
75 75
76 76 /*!
77 77 Destroys the chartview object and the associated chart.
78 78 */
79 79 QChartView::~QChartView()
80 80 {
81 81 }
82 82
83 83 /*!
84 84 Returns the pointer to the associated chart.
85 85 */
86 86 QChart *QChartView::chart() const
87 87 {
88 88 return d_ptr->m_chart;
89 89 }
90 90
91 91 /*!
92 92 Sets the current chart to \a chart. Ownership of the new chart is passed to chartview
93 93 and ownership of the previous chart is released.
94 94
95 95 To avoid memory leaks users need to make sure the previous chart is deleted.
96 96 */
97 97
98 98 void QChartView::setChart(QChart *chart)
99 99 {
100 100 d_ptr->setChart(chart);
101 101 }
102 102
103 103 /*!
104 104 Sets the rubber band flags to \a rubberBand.
105 105 Selected flags determine the way zooming is performed.
106 106
107 107 \note Rubber band zooming is not supported for polar charts.
108 108 */
109 109 void QChartView::setRubberBand(const RubberBands &rubberBand)
110 110 {
111 111 #ifndef QT_NO_RUBBERBAND
112 112 d_ptr->m_rubberBandFlags = rubberBand;
113 113
114 114 if (!d_ptr->m_rubberBandFlags) {
115 115 delete d_ptr->m_rubberBand;
116 116 d_ptr->m_rubberBand = 0;
117 117 return;
118 118 }
119 119
120 120 if (!d_ptr->m_rubberBand) {
121 121 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
122 122 d_ptr->m_rubberBand->setEnabled(true);
123 123 }
124 124 #else
125 125 Q_UNUSED(rubberBand);
126 126 qWarning("Unable to set rubber band because Qt is configured without it.");
127 127 #endif
128 128 }
129 129
130 130 /*!
131 131 Returns the rubber band flags that are currently being used by the widget.
132 132 */
133 133 QChartView::RubberBands QChartView::rubberBand() const
134 134 {
135 135 return d_ptr->m_rubberBandFlags;
136 136 }
137 137
138 138 /*!
139 139 If Left mouse button is pressed and the rubber band is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
140 140 If different mouse button is pressed and/or the rubber band is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
141 141 */
142 142 void QChartView::mousePressEvent(QMouseEvent *event)
143 143 {
144 144 #ifndef QT_NO_RUBBERBAND
145 145 QRectF plotArea = d_ptr->m_chart->plotArea();
146 146 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled()
147 147 && event->button() == Qt::LeftButton && plotArea.contains(event->pos())) {
148 148 d_ptr->m_rubberBandOrigin = event->pos();
149 149 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
150 150 d_ptr->m_rubberBand->show();
151 151 event->accept();
152 152 } else {
153 153 #endif
154 154 QGraphicsView::mousePressEvent(event);
155 155 #ifndef QT_NO_RUBBERBAND
156 156 }
157 157 #endif
158 158 }
159 159
160 160 /*!
161 161 If the rubber band rectange has been displayed in pressEvent then \a event data is used to update the rubber band geometry.
162 162 Otherwise the default QGraphicsView::mouseMoveEvent implementation is called.
163 163 */
164 164 void QChartView::mouseMoveEvent(QMouseEvent *event)
165 165 {
166 166 #ifndef QT_NO_RUBBERBAND
167 167 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
168 168 QRect rect = d_ptr->m_chart->plotArea().toRect();
169 169 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
170 170 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
171 171 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
172 172 d_ptr->m_rubberBandOrigin.setY(rect.top());
173 173 height = rect.height();
174 174 }
175 175 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
176 176 d_ptr->m_rubberBandOrigin.setX(rect.left());
177 177 width = rect.width();
178 178 }
179 179 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized());
180 180 } else {
181 181 #endif
182 182 QGraphicsView::mouseMoveEvent(event);
183 183 #ifndef QT_NO_RUBBERBAND
184 184 }
185 185 #endif
186 186 }
187 187
188 188 /*!
189 189 If left mouse button is released and the rubber band is enabled then \a event is accepted and
190 190 the view is zoomed into the rect specified by the rubber band.
191 191 If it is a right mouse button \a event then the view is zoomed out.
192 192 */
193 193 void QChartView::mouseReleaseEvent(QMouseEvent *event)
194 194 {
195 195 #ifndef QT_NO_RUBBERBAND
196 196 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
197 197 if (event->button() == Qt::LeftButton) {
198 198 d_ptr->m_rubberBand->hide();
199 199 QRectF rect = d_ptr->m_rubberBand->geometry();
200 200 // Since plotArea uses QRectF and rubberband uses QRect, we can't just blindly use
201 201 // rubberband's dimensions for vertical and horizontal rubberbands, where one
202 202 // dimension must match the corresponding plotArea dimension exactly.
203 203 if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
204 204 rect.setX(d_ptr->m_chart->plotArea().x());
205 205 rect.setWidth(d_ptr->m_chart->plotArea().width());
206 206 } else if (d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
207 207 rect.setY(d_ptr->m_chart->plotArea().y());
208 208 rect.setHeight(d_ptr->m_chart->plotArea().height());
209 209 }
210 210 d_ptr->m_chart->zoomIn(rect);
211 211 event->accept();
212 212 }
213 213
214 if (event->button() == Qt::RightButton) {
214 } else if (d_ptr->m_rubberBand && event->button() == Qt::RightButton) {
215 215 // If vertical or horizontal rubberband mode, restrict zoom out to specified axis.
216 216 // Since there is no suitable API for that, use zoomIn with rect bigger than the
217 217 // plot area.
218 218 if (d_ptr->m_rubberBandFlags == VerticalRubberBand
219 219 || d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
220 220 QRectF rect = d_ptr->m_chart->plotArea();
221 221 if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
222 222 qreal adjustment = rect.height() / 2;
223 223 rect.adjust(0, -adjustment, 0, adjustment);
224 224 } else if (d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
225 225 qreal adjustment = rect.width() / 2;
226 226 rect.adjust(-adjustment, 0, adjustment, 0);
227 227 }
228 228 d_ptr->m_chart->zoomIn(rect);
229 229 } else {
230 230 d_ptr->m_chart->zoomOut();
231 231 }
232 232 event->accept();
233 }
234 233 } else {
235 234 #endif
236 235 QGraphicsView::mouseReleaseEvent(event);
237 236 #ifndef QT_NO_RUBBERBAND
238 237 }
239 238 #endif
240 239 }
241 240
242 241 /*!
243 242 Resizes and updates the chart area using the \a event data
244 243 */
245 244 void QChartView::resizeEvent(QResizeEvent *event)
246 245 {
247 246 QGraphicsView::resizeEvent(event);
248 247 d_ptr->resize();
249 248 }
250 249
251 250 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
252 251
253 252 QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart *chart)
254 253 : q_ptr(q),
255 254 m_scene(new QGraphicsScene(q)),
256 255 m_chart(chart),
257 256 #ifndef QT_NO_RUBBERBAND
258 257 m_rubberBand(0),
259 258 #endif
260 259 m_rubberBandFlags(QChartView::NoRubberBand)
261 260 {
262 261 q_ptr->setFrameShape(QFrame::NoFrame);
263 262 q_ptr->setBackgroundRole(QPalette::Window);
264 263 q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
265 264 q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
266 265 q_ptr->setScene(m_scene);
267 266 q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
268 267 if (!m_chart)
269 268 m_chart = new QChart();
270 269 m_scene->addItem(m_chart);
271 270 }
272 271
273 272 QChartViewPrivate::~QChartViewPrivate()
274 273 {
275 274 }
276 275
277 276 void QChartViewPrivate::setChart(QChart *chart)
278 277 {
279 278 Q_ASSERT(chart);
280 279
281 280 if (m_chart == chart)
282 281 return;
283 282
284 283 if (m_chart)
285 284 m_scene->removeItem(m_chart);
286 285
287 286 m_chart = chart;
288 287 m_scene->addItem(m_chart);
289 288
290 289 resize();
291 290 }
292 291
293 292 void QChartViewPrivate::resize()
294 293 {
295 294 // Fit the chart into view if it has been rotated
296 295 qreal sinA = qAbs(q_ptr->transform().m21());
297 296 qreal cosA = qAbs(q_ptr->transform().m11());
298 297 QSize chartSize = q_ptr->size();
299 298
300 299 if (sinA == 1.0) {
301 300 chartSize.setHeight(q_ptr->size().width());
302 301 chartSize.setWidth(q_ptr->size().height());
303 302 } else if (sinA != 0.0) {
304 303 // Non-90 degree rotation, find largest square chart that can fit into the view.
305 304 qreal minDimension = qMin(q_ptr->size().width(), q_ptr->size().height());
306 305 qreal h = (minDimension - (minDimension / ((sinA / cosA) + 1.0))) / sinA;
307 306 chartSize.setHeight(h);
308 307 chartSize.setWidth(h);
309 308 }
310 309
311 310 m_chart->resize(chartSize);
312 311 q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
313 312 q_ptr->setSceneRect(m_chart->geometry());
314 313 }
315 314
316 315 #include "moc_qchartview.cpp"
317 316
318 317 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now