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