##// END OF EJS Templates
Removed setting scene rect in QChartView::resizeEvent
Tero Ahola -
r739:6c5910e9d6f4
parent child
Show More
@@ -1,419 +1,416
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QGraphicsView>
5 5 #include <QGraphicsScene>
6 6 #include <QRubberBand>
7 7 #include <QResizeEvent>
8 8 #include <QDebug>
9 9
10 10 /*!
11 11 \enum QChartView::RubberBandPolicy
12 12
13 13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14 14
15 15 \value NoRubberBand
16 16 \value VerticalRubberBand
17 17 \value HorizonalRubberBand
18 18 \value RectangleRubberBand
19 19 */
20 20
21 21 /*!
22 22 \class QChartView
23 23 \brief Standalone charting widget.
24 24
25 25 QChartView is a standalone widget that can display charts. It does not require separate
26 26 QGraphicsScene to work. It manages the graphical representation of different types of
27 27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
28 28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
29 29
30 30 \sa QChart
31 31 */
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 /*!
36 36 Constructs a chartView object which is a child of a\a parent.
37 37 */
38 38 QChartView::QChartView(QWidget *parent) :
39 39 QGraphicsView(parent),
40 40 m_scene(new QGraphicsScene(this)),
41 41 m_chart(new QChart()),
42 42 m_rubberBand(0),
43 43 m_verticalRubberBand(false),
44 44 m_horizonalRubberBand(false)
45 45 {
46 46 setFrameShape(QFrame::NoFrame);
47 47 setBackgroundRole(QPalette::Window);
48 48 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49 49 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 50 setScene(m_scene);
51 51 m_scene->addItem(m_chart);
52 52 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
53 53 }
54 54
55 55
56 56 /*!
57 57 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
58 58 */
59 59 QChartView::~QChartView()
60 60 {
61 61 }
62 62
63 63 /*!
64 64 Resizes and updates the chart area using the \a event data
65 65 */
66 66 void QChartView::resizeEvent(QResizeEvent *event)
67 67 {
68 // If the scene rect is the size of the view, you will get scrolling effect at least on OSX;
69 // i.e. you are able to move the chart inside the view a couple of pixels by mouse flicking
70 m_scene->setSceneRect(0, 0, size().width() - 2, size().height() - 2);
71 68 m_chart->resize(size());
72 QWidget::resizeEvent(event);
69 QGraphicsView::resizeEvent(event);
73 70 }
74 71
75 72 /*!
76 73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
77 74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
78 75 the y axis).
79 76 \sa removeSeries(), removeAllSeries()
80 77 */
81 78 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
82 79 {
83 80 m_chart->addSeries(series,axisY);
84 81 }
85 82
86 83 /*!
87 84 Removes the \a series specified in a perameter from the QChartView.
88 85 It releses its ownership of the specified QChartSeries object.
89 86 It does not delete the pointed QChartSeries data object
90 87 \sa addSeries(), removeAllSeries()
91 88 */
92 89 void QChartView::removeSeries(QSeries* series)
93 90 {
94 91 m_chart->removeSeries(series);
95 92 }
96 93
97 94 /*!
98 95 Removes all the QChartSeries that have been added to the QChartView
99 96 It also deletes the pointed QChartSeries data objects
100 97 \sa addSeries(), removeSeries()
101 98 */
102 99 void QChartView::removeAllSeries()
103 100 {
104 101 m_chart->removeAllSeries();
105 102 }
106 103
107 104 /*!
108 105 Zooms in the view by a factor of 2
109 106 */
110 107 void QChartView::zoomIn()
111 108 {
112 109 m_chart->zoomIn();
113 110 }
114 111
115 112 /*!
116 113 Zooms in the view to a maximum level at which \a rect is still fully visible.
117 114 */
118 115 void QChartView::zoomIn(const QRect& rect)
119 116 {
120 117 m_chart->zoomIn(rect);
121 118 }
122 119
123 120 /*!
124 121 Restores the view zoom level to the previous one.
125 122 */
126 123 void QChartView::zoomOut()
127 124 {
128 125 m_chart->zoomOut();
129 126 }
130 127
131 128 /*!
132 129 Sets the chart \a title. A description text that is drawn above the chart.
133 130 */
134 131 void QChartView::setChartTitle(const QString& title)
135 132 {
136 133 m_chart->setTitle(title);
137 134 }
138 135
139 136 /*!
140 137 Returns the chart's title. A description text that is drawn above the chart.
141 138 */
142 139 QString QChartView::chartTitle() const
143 140 {
144 141 return m_chart->title();
145 142 }
146 143
147 144 /*!
148 145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
149 146 */
150 147 void QChartView::setChartTitleFont(const QFont& font)
151 148 {
152 149 m_chart->setTitleFont(font);
153 150 }
154 151
155 152 /*!
156 153 Sets the \a brush used for rendering the title text.
157 154 */
158 155 void QChartView::setChartTitleBrush(const QBrush &brush)
159 156 {
160 157 m_chart->setTitleBrush(brush);
161 158 }
162 159
163 160 /*!
164 161 Returns the brush used for rendering the title text.
165 162 */
166 163 QBrush QChartView::chartTitleBrush()
167 164 {
168 165 return m_chart->titleBrush();
169 166 }
170 167
171 168 /*!
172 169 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
173 170 */
174 171 void QChartView::setChartBackgroundBrush(const QBrush& brush)
175 172 {
176 173 m_chart->setBackgroundBrush(brush);
177 174 }
178 175
179 176 /*!
180 177 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
181 178 */
182 179 void QChartView::setChartBackgroundPen(const QPen& pen)
183 180 {
184 181 m_chart->setBackgroundPen(pen);
185 182 }
186 183
187 184 /*!
188 185 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
189 186 */
190 187 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
191 188 {
192 189 switch(policy) {
193 190 case VerticalRubberBand:
194 191 m_verticalRubberBand = true;
195 192 m_horizonalRubberBand = false;
196 193 break;
197 194 case HorizonalRubberBand:
198 195 m_verticalRubberBand = false;
199 196 m_horizonalRubberBand = true;
200 197 break;
201 198 case RectangleRubberBand:
202 199 m_verticalRubberBand = true;
203 200 m_horizonalRubberBand = true;
204 201 break;
205 202 case NoRubberBand:
206 203 default:
207 204 delete m_rubberBand;
208 205 m_rubberBand=0;
209 206 m_horizonalRubberBand = false;
210 207 m_verticalRubberBand = false;
211 208 return;
212 209 }
213 210 if(!m_rubberBand) {
214 211 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
215 212 m_rubberBand->setEnabled(true);
216 213 }
217 214 }
218 215
219 216 /*!
220 217 Returns the RubberBandPolicy that is currently being used by the widget.
221 218 */
222 219 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
223 220 {
224 221 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
225 222 if(m_horizonalRubberBand) return HorizonalRubberBand;
226 223 if(m_verticalRubberBand) return VerticalRubberBand;
227 224 return NoRubberBand;
228 225 }
229 226
230 227 /*!
231 228 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.
232 229 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
233 230 */
234 231 void QChartView::mousePressEvent(QMouseEvent *event)
235 232 {
236 233 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
237 234
238 235 int padding = m_chart->padding();
239 236 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
240 237
241 238 if (rect.contains(event->pos())) {
242 239 m_rubberBandOrigin = event->pos();
243 240 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
244 241 m_rubberBand->show();
245 242 event->accept();
246 243 }
247 244 }
248 245 else {
249 246 QGraphicsView::mousePressEvent(event);
250 247 }
251 248 }
252 249
253 250 /*!
254 251 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
255 252 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
256 253 */
257 254 void QChartView::mouseMoveEvent(QMouseEvent *event)
258 255 {
259 256 if(m_rubberBand && m_rubberBand->isVisible()) {
260 257 int padding = m_chart->padding();
261 258 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
262 259 int width = event->pos().x() - m_rubberBandOrigin.x();
263 260 int height = event->pos().y() - m_rubberBandOrigin.y();
264 261 if(!m_verticalRubberBand) {
265 262 m_rubberBandOrigin.setY(rect.top());
266 263 height = rect.height();
267 264 }
268 265 if(!m_horizonalRubberBand) {
269 266 m_rubberBandOrigin.setX(rect.left());
270 267 width= rect.width();
271 268 }
272 269 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
273 270 }
274 271 else {
275 272 QGraphicsView::mouseMoveEvent(event);
276 273 }
277 274 }
278 275
279 276 /*!
280 277 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
281 278 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
282 279 */
283 280 void QChartView::mouseReleaseEvent(QMouseEvent *event)
284 281 {
285 282 if(m_rubberBand) {
286 283 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
287 284 m_rubberBand->hide();
288 285 QRect rect = m_rubberBand->geometry();
289 286 m_chart->zoomIn(rect);
290 287 event->accept();
291 288 }
292 289
293 290 if(event->button()==Qt::RightButton){
294 291 m_chart->zoomOut();
295 292 event->accept();
296 293 }
297 294 }
298 295 else {
299 296 QGraphicsView::mouseReleaseEvent(event);
300 297 }
301 298 }
302 299
303 300 /*!
304 301 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
305 302 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
306 303 */
307 304 void QChartView::keyPressEvent(QKeyEvent *event)
308 305 {
309 306 switch (event->key()) {
310 307 case Qt::Key_Plus:
311 308 zoomIn();
312 309 break;
313 310 case Qt::Key_Minus:
314 311 zoomOut();
315 312 break;
316 313 default:
317 314 QGraphicsView::keyPressEvent(event);
318 315 break;
319 316 }
320 317 }
321 318
322 319 /*!
323 320 Sets the \a theme used by the chart for rendering the graphical representation of the data
324 321 \sa QChart::ChartTheme, chartTheme()
325 322 */
326 323 void QChartView::setChartTheme(QChart::ChartTheme theme)
327 324 {
328 325 m_chart->setChartTheme(theme);
329 326 }
330 327
331 328 /*!
332 329 Returns the theme enum used by the chart.
333 330 \sa setChartTheme()
334 331 */
335 332 QChart::ChartTheme QChartView::chartTheme() const
336 333 {
337 334 return m_chart->chartTheme();
338 335 }
339 336
340 337 /*!
341 338 Returns the pointer to the x axis object of the chart
342 339 */
343 340 QChartAxis* QChartView::axisX() const
344 341 {
345 342 return m_chart->axisX();
346 343 }
347 344
348 345 /*!
349 346 Returns the pointer to the y axis object of the chart
350 347 */
351 348 QChartAxis* QChartView::axisY() const
352 349 {
353 350 return m_chart->axisY();
354 351 }
355 352
356 353 /*!
357 354 Returns the pointer to legend object of the chart
358 355 */
359 356 QLegend& QChartView::legend() const
360 357 {
361 358 return m_chart->legend();
362 359 }
363 360
364 361 /*!
365 362 Gives ownership of legend to user.
366 363 */
367 364 QLegend* QChartView::takeLegend()
368 365 {
369 366 return m_chart->takeLegend();
370 367 }
371 368
372 369 /*!
373 370 Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one
374 371 */
375 372 void QChartView::giveLegend(QLegend* legend)
376 373 {
377 374 m_chart->giveLegend(legend);
378 375 }
379 376
380 377 /*!
381 378 Sets animation \a options for the chart
382 379 */
383 380 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
384 381 {
385 382 m_chart->setAnimationOptions(options);
386 383 }
387 384
388 385 /*!
389 386 Returns animation options for the chart
390 387 */
391 388 QChart::AnimationOptions QChartView::animationOptions() const
392 389 {
393 390 return m_chart->animationOptions();
394 391 }
395 392
396 393 void QChartView::scrollLeft()
397 394 {
398 395 m_chart->scrollLeft();
399 396 }
400 397
401 398 void QChartView::scrollRight()
402 399 {
403 400 m_chart->scrollRight();
404 401 }
405 402
406 403 void QChartView::scrollUp()
407 404 {
408 405 m_chart->scrollUp();
409 406 }
410 407
411 408 void QChartView::scrollDown()
412 409 {
413 410 m_chart->scrollDown();
414 411 }
415 412
416 413
417 414 #include "moc_qchartview.cpp"
418 415
419 416 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now