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