##// END OF EJS Templates
Fix padding to be int, update unit test
Michal Klocek -
r804:02c84d5a1cd8
parent child
Show More
@@ -1,501 +1,501
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 "qchart.h"
22 22 #include "qchart_p.h"
23 23 #include <QGraphicsScene>
24 24 #include <QGraphicsSceneResizeEvent>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 /*!
29 29 \enum QChart::ChartTheme
30 30
31 31 This enum describes the theme used by the chart.
32 32
33 33 \value ChartThemeDefault Follows the GUI style of the Operating System
34 34 \value ChartThemeLight
35 35 \value ChartThemeBlueCerulean
36 36 \value ChartThemeDark
37 37 \value ChartThemeBrownSand
38 38 \value ChartThemeBlueNcs
39 39 \value ChartThemeHighContrast
40 40 \value ChartThemeBlueIcy
41 41 \value ChartThemeCount Not really a theme; the total count of themes.
42 42 */
43 43
44 44 /*!
45 45 \enum QChart::AnimationOption
46 46
47 47 For enabling/disabling animations. Defaults to NoAnimation.
48 48
49 49 \value NoAnimation
50 50 \value GridAxisAnimations
51 51 \value SeriesAnimations
52 52 \value AllAnimations
53 53 */
54 54
55 55 /*!
56 56 \class QChart
57 57 \brief QtCommercial chart API.
58 58
59 59 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
60 60 representation of different types of QChartSeries and other chart related objects like
61 61 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
62 62 convenience class QChartView instead of QChart.
63 63 \sa QChartView
64 64 */
65 65
66 66 /*!
67 67 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
68 68 */
69 69 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
70 70 d_ptr(new QChartPrivate(this))
71 71 {
72 72 //setMinimumSize(200,200);
73 73 d_ptr->m_legend = new QLegend(this);
74 74 d_ptr->m_dataset = new ChartDataSet(this);
75 75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
76 76 setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3);
77 77 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
78 78 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
79 79 }
80 80
81 81 /*!
82 82 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
83 83 */
84 84 QChart::~QChart()
85 85 {
86 86 //delete first presenter , since this is a root of all the graphical items
87 87 delete d_ptr->m_presenter;
88 88 d_ptr->m_presenter=0;
89 89 }
90 90
91 91 /*!
92 92 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
93 93 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
94 94 the y axis).
95 95 */
96 96 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
97 97 {
98 98 d_ptr->m_dataset->addSeries(series, axisY);
99 99 }
100 100
101 101 /*!
102 102 Removes the \a series specified in a perameter from the QChartView.
103 103 It releses its ownership of the specified QChartSeries object.
104 104 It does not delete the pointed QChartSeries data object
105 105 \sa addSeries(), removeAllSeries()
106 106 */
107 107 void QChart::removeSeries(QSeries* series)
108 108 {
109 109 d_ptr->m_dataset->removeSeries(series);
110 110 }
111 111
112 112 /*!
113 113 Removes all the QChartSeries that have been added to the QChartView
114 114 It also deletes the pointed QChartSeries data objects
115 115 \sa addSeries(), removeSeries()
116 116 */
117 117 void QChart::removeAllSeries()
118 118 {
119 119 d_ptr->m_dataset->removeAllSeries();
120 120 }
121 121
122 122 /*!
123 123 Sets the \a brush that is used for painting the background of the chart area.
124 124 */
125 125 void QChart::setBackgroundBrush(const QBrush& brush)
126 126 {
127 127 d_ptr->createChartBackgroundItem();
128 128 d_ptr->m_backgroundItem->setBrush(brush);
129 129 d_ptr->m_backgroundItem->update();
130 130 }
131 131
132 132 QBrush QChart::backgroundBrush() const
133 133 {
134 134 if (!d_ptr->m_backgroundItem) return QBrush();
135 135 return (d_ptr->m_backgroundItem)->brush();
136 136 }
137 137
138 138 /*!
139 139 Sets the \a pen that is used for painting the background of the chart area.
140 140 */
141 141 void QChart::setBackgroundPen(const QPen& pen)
142 142 {
143 143 d_ptr->createChartBackgroundItem();
144 144 d_ptr->m_backgroundItem->setPen(pen);
145 145 d_ptr->m_backgroundItem->update();
146 146 }
147 147
148 148 QPen QChart::backgroundPen() const
149 149 {
150 150 if (!d_ptr->m_backgroundItem) return QPen();
151 151 return d_ptr->m_backgroundItem->pen();
152 152 }
153 153
154 154 /*!
155 155 Sets the chart \a title. The description text that is drawn above the chart.
156 156 */
157 157 void QChart::setTitle(const QString& title)
158 158 {
159 159 d_ptr->createChartTitleItem();
160 160 d_ptr->m_titleItem->setText(title);
161 161 d_ptr->updateLayout();
162 162 }
163 163
164 164 /*!
165 165 Returns the chart title. The description text that is drawn above the chart.
166 166 */
167 167 QString QChart::title() const
168 168 {
169 169 if (d_ptr->m_titleItem)
170 170 return d_ptr->m_titleItem->text();
171 171 else
172 172 return QString();
173 173 }
174 174
175 175 /*!
176 176 Sets the \a font that is used for rendering the description text that is rendered above the chart.
177 177 */
178 178 void QChart::setTitleFont(const QFont& font)
179 179 {
180 180 d_ptr->createChartTitleItem();
181 181 d_ptr->m_titleItem->setFont(font);
182 182 d_ptr->updateLayout();
183 183 }
184 184
185 185 /*!
186 186 Sets the \a brush used for rendering the title text.
187 187 */
188 188 void QChart::setTitleBrush(const QBrush &brush)
189 189 {
190 190 d_ptr->createChartTitleItem();
191 191 d_ptr->m_titleItem->setBrush(brush);
192 192 d_ptr->updateLayout();
193 193 }
194 194
195 195 /*!
196 196 Returns the brush used for rendering the title text.
197 197 */
198 198 QBrush QChart::titleBrush() const
199 199 {
200 200 if (!d_ptr->m_titleItem) return QBrush();
201 201 return d_ptr->m_titleItem->brush();
202 202 }
203 203
204 204 /*!
205 205 Sets the \a theme used by the chart for rendering the graphical representation of the data
206 206 \sa ChartTheme, chartTheme()
207 207 */
208 208 void QChart::setTheme(QChart::ChartTheme theme)
209 209 {
210 210 d_ptr->m_presenter->setTheme(theme);
211 211 }
212 212
213 213 /*!
214 214 Returns the theme enum used by the chart.
215 215 \sa ChartTheme, setChartTheme()
216 216 */
217 217 QChart::ChartTheme QChart::theme() const
218 218 {
219 219 return d_ptr->m_presenter->theme();
220 220 }
221 221
222 222 /*!
223 223 Zooms in the view by a factor of 2
224 224 */
225 225 void QChart::zoomIn()
226 226 {
227 227 d_ptr->m_presenter->zoomIn();
228 228 }
229 229
230 230 /*!
231 231 Zooms in the view to a maximum level at which \a rect is still fully visible.
232 232 */
233 233 void QChart::zoomIn(const QRectF& rect)
234 234 {
235 235 if (!rect.isValid()) return;
236 236 d_ptr->m_presenter->zoomIn(rect);
237 237 }
238 238
239 239 /*!
240 240 Restores the view zoom level to the previous one.
241 241 */
242 242 void QChart::zoomOut()
243 243 {
244 244 d_ptr->m_presenter->zoomOut();
245 245 }
246 246
247 247 /*!
248 248 Returns the pointer to the x axis object of the chart
249 249 */
250 250 QChartAxis* QChart::axisX() const
251 251 {
252 252 return d_ptr->m_dataset->axisX();
253 253 }
254 254
255 255 /*!
256 256 Returns the pointer to the y axis object of the chart
257 257 */
258 258 QChartAxis* QChart::axisY() const
259 259 {
260 260 return d_ptr->m_dataset->axisY();
261 261 }
262 262
263 263 /*!
264 264 Returns the legend object of the chart. Ownership stays in chart.
265 265 */
266 266 QLegend* QChart::legend() const
267 267 {
268 268 return d_ptr->m_legend;
269 269 }
270 270
271 QRectF QChart::padding() const
271 QRect QChart::padding() const
272 272 {
273 273 return d_ptr->m_padding;
274 274 }
275 275
276 276
277 277 /*!
278 278 Resizes and updates the chart area using the \a event data
279 279 */
280 280 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
281 281 {
282 282 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
283 283 d_ptr->updateLayout();
284 284 QGraphicsWidget::resizeEvent(event);
285 285 update();
286 286 }
287 287
288 288 /*!
289 289 Sets animation \a options for the chart
290 290 */
291 291 void QChart::setAnimationOptions(AnimationOptions options)
292 292 {
293 293 d_ptr->m_presenter->setAnimationOptions(options);
294 294 }
295 295
296 296 /*!
297 297 Returns animation options for the chart
298 298 */
299 299 QChart::AnimationOptions QChart::animationOptions() const
300 300 {
301 301 return d_ptr->m_presenter->animationOptions();
302 302 }
303 303
304 304 void QChart::scrollLeft()
305 305 {
306 306 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
307 307 }
308 308
309 309 void QChart::scrollRight()
310 310 {
311 311 d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
312 312 }
313 313
314 314 void QChart::scrollUp()
315 315 {
316 316 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
317 317 }
318 318
319 319 void QChart::scrollDown()
320 320 {
321 321 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
322 322 }
323 323
324 324 void QChart::setBackgroundVisible(bool visible)
325 325 {
326 326 d_ptr->createChartBackgroundItem();
327 327 d_ptr->m_backgroundItem->setVisible(visible);
328 328 }
329 329
330 330 bool QChart::isBackgroundVisible() const
331 331 {
332 332 if (!d_ptr->m_backgroundItem) return false;
333 333 return d_ptr->m_backgroundItem->isVisible();
334 334 }
335 335
336 336 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
337 337
338 338 QChartPrivate::QChartPrivate(QChart *parent):
339 339 q_ptr(parent),
340 340 m_backgroundItem(0),
341 341 m_titleItem(0),
342 342 m_legend(0),
343 343 m_dataset(0),
344 344 m_presenter(0),
345 m_padding(QRectF(50,50,50,50))
345 m_padding(QRect(50,50,0,0))
346 346 {
347 347
348 348 }
349 349
350 350 QChartPrivate::~QChartPrivate()
351 351 {
352 352
353 353 }
354 354
355 355 void QChartPrivate::createChartBackgroundItem()
356 356 {
357 357 if (!m_backgroundItem) {
358 358 m_backgroundItem = new ChartBackground(q_ptr);
359 359 m_backgroundItem->setPen(Qt::NoPen);
360 360 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
361 361 }
362 362 }
363 363
364 364 void QChartPrivate::createChartTitleItem()
365 365 {
366 366 if (!m_titleItem) {
367 367 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
368 368 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
369 369 }
370 370 }
371 371
372 372 void QChartPrivate::updateLegendLayout()
373 373 {
374 374 //int legendPadding = m_chart->legend()->padding();
375 375 int legendPadding = 30;
376 376 QRectF rect = m_rect;
377 377
378 378 if ((m_legend->attachedToChart()) && (m_legend->isVisible())) {
379 379
380 380 // Reserve some space for legend
381 381 switch (m_legend->alignment()) {
382 382 case QLegend::AlignmentTop: {
383 383 rect.adjust(m_padding.left(),
384 384 m_padding.top() + legendPadding,
385 385 -m_padding.right(),
386 386 -m_padding.bottom());
387 387 break;
388 388 }
389 389 case QLegend::AlignmentBottom: {
390 390 rect.adjust(m_padding.left(),
391 391 m_padding.top(),
392 392 -m_padding.right(),
393 393 -m_padding.bottom() - legendPadding);
394 394 break;
395 395 }
396 396 case QLegend::AlignmentLeft: {
397 397 rect.adjust(m_padding.left() + legendPadding,
398 398 m_padding.top(),
399 399 -m_padding.right(),
400 400 -m_padding.bottom());
401 401 break;
402 402 }
403 403 case QLegend::AlignmentRight: {
404 404 rect.adjust(m_padding.left(),
405 405 m_padding.top(),
406 406 -m_padding.right() - legendPadding,
407 407 -m_padding.bottom());
408 408 break;
409 409 }
410 410 default: {
411 411 rect.adjust(m_padding.left(),
412 412 m_padding.top(),
413 413 -m_padding.right(),
414 414 -m_padding.bottom());
415 415 break;
416 416 }
417 417 }
418 418 } else {
419 419
420 420 rect.adjust(m_padding.left(),
421 421 m_padding.top(),
422 422 -m_padding.right(),
423 423 -m_padding.bottom());
424 424 }
425 425
426 426 QRectF plotRect = m_rect.adjusted(m_padding.left()
427 427 ,m_padding.top()
428 428 ,-m_padding.right()
429 429 ,-m_padding.bottom());
430 430 QRectF legendRect;
431 431
432 432 int padding = 0; // TODO: fix this
433 433 switch (m_legend->alignment())
434 434 {
435 435 case QLegend::AlignmentTop: {
436 436 legendRect = m_rect.adjusted(0,padding,0,-padding - plotRect.height());
437 437 break;
438 438 }
439 439 case QLegend::AlignmentBottom: {
440 440 legendRect = m_rect.adjusted(padding,padding + plotRect.height(),-padding,0);
441 441 break;
442 442 }
443 443 case QLegend::AlignmentLeft: {
444 444 legendRect = m_rect.adjusted(0,padding,-padding - plotRect.width(),-padding);
445 445 break;
446 446 }
447 447 case QLegend::AlignmentRight: {
448 448 legendRect = m_rect.adjusted(padding + plotRect.width(),padding,0,-padding);
449 449 break;
450 450 }
451 451 default: {
452 452 legendRect = plotRect;
453 453 break;
454 454 }
455 455 }
456 456
457 457 m_legend->setMaximumSize(legendRect.size());
458 458
459 459 qreal width = legendRect.width() - m_legend->size().width();
460 460 qreal height = legendRect.height() - m_legend->size().height();
461 461
462 462 QPointF pos = legendRect.topLeft();
463 463 if (width > 0) {
464 464 pos.setX(pos.x() + width/2);
465 465 }
466 466 if (height > 0) {
467 467 pos.setY(pos.y() + height/2);
468 468 }
469 469
470 470 m_legend->setPos(pos);
471 471 }
472 472
473 473 void QChartPrivate::updateLayout()
474 474 {
475 475 if (!m_rect.isValid()) return;
476 476
477 477 int padding = m_padding.top();
478 478 int backgroundPadding = m_presenter->backgroundPadding();
479 479
480 480 // recalculate title position
481 481 if (m_titleItem) {
482 482 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
483 483 m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2);
484 484 }
485 485
486 486 //recalculate background gradient
487 487 if (m_backgroundItem) {
488 488 m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding));
489 489 }
490 490
491 491 // recalculate legend position
492 492 if (m_legend) {
493 493 if ((m_legend->attachedToChart()) && (m_legend->parentObject() == q_ptr)) {
494 494 updateLegendLayout();
495 495 }
496 496 }
497 497 }
498 498
499 499 #include "moc_qchart.cpp"
500 500
501 501 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,117
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 #ifndef QCHART_H
22 22 #define QCHART_H
23 23
24 24 #include <QSeries>
25 25 #include <QGraphicsWidget>
26 26
27 27 class QGraphicsSceneResizeEvent;
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QSeries;
32 32 class QChartAxis;
33 33 class QLegend;
34 34 struct QChartPrivate;
35 35
36 36 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
37 37 {
38 38 Q_OBJECT
39 39 public:
40 40 enum ChartTheme {
41 41 ChartThemeDefault,
42 42 ChartThemeLight,
43 43 ChartThemeBlueCerulean,
44 44 ChartThemeDark,
45 45 ChartThemeBrownSand,
46 46 ChartThemeBlueNcs,
47 47 ChartThemeHighContrast,
48 48 ChartThemeBlueIcy,
49 49 ChartThemeCount
50 50 };
51 51
52 52 enum AnimationOption {
53 53 NoAnimation = 0x0,
54 54 GridAxisAnimations = 0x1,
55 55 SeriesAnimations =0x2,
56 56 AllAnimations = 0x3
57 57 };
58 58
59 59 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
60 60
61 61 public:
62 62 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
63 63 ~QChart();
64 64
65 65 void addSeries(QSeries *series, QChartAxis *axisY = 0);
66 66 void removeSeries(QSeries *series);
67 67 void removeAllSeries();
68 68
69 69 void setTheme(QChart::ChartTheme theme);
70 70 QChart::ChartTheme theme() const;
71 71
72 72 void setTitle(const QString& title);
73 73 QString title() const;
74 74 void setTitleFont(const QFont& font);
75 75 QFont titleFont() const;
76 76 void setTitleBrush(const QBrush &brush);
77 77 QBrush titleBrush() const;
78 78 void setBackgroundBrush(const QBrush &brush);
79 79 QBrush backgroundBrush() const;
80 80 void setBackgroundPen(const QPen &pen);
81 81 QPen backgroundPen() const;
82 82
83 83 void setBackgroundVisible(bool visible);
84 84 bool isBackgroundVisible() const;
85 85
86 86 void setAnimationOptions(AnimationOptions options);
87 87 AnimationOptions animationOptions() const;
88 88
89 89 void zoomIn();
90 90 void zoomIn(const QRectF &rect);
91 91 void zoomOut();
92 92 void scrollLeft();
93 93 void scrollRight();
94 94 void scrollUp();
95 95 void scrollDown();
96 96
97 97 QChartAxis* axisX() const;
98 98 QChartAxis* axisY() const;
99 99
100 100 QLegend* legend() const;
101 101
102 QRectF padding() const;
102 QRect padding() const;
103 103
104 104 protected:
105 105 void resizeEvent(QGraphicsSceneResizeEvent *event);
106 106
107 107 protected:
108 108 QScopedPointer<QChartPrivate> d_ptr;
109 friend class QChartView;
110 109 friend class QLegend;
111 110 Q_DISABLE_COPY(QChart)
112 111 };
113 112
114 113 QTCOMMERCIALCHART_END_NAMESPACE
115 114
116 115 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
117 116
118 117 #endif
@@ -1,64 +1,64
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QCHART_P_H
31 31 #define QCHART_P_H
32 32
33 33 #include "qchartaxis.h"
34 34 #include "qlegend.h"
35 35 #include "chartpresenter_p.h"
36 36 #include "chartdataset_p.h"
37 37 #include "chartbackground_p.h"
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 class QChart;
42 42
43 43 struct QChartPrivate
44 44 {
45 45 QChartPrivate(QChart *parent);
46 46 ~QChartPrivate();
47 47
48 48 void createChartBackgroundItem();
49 49 void createChartTitleItem();
50 50 void updateLayout();
51 51 void updateLegendLayout();
52 52
53 53 QChart *q_ptr;
54 54 ChartBackground* m_backgroundItem;
55 55 QGraphicsSimpleTextItem* m_titleItem;
56 56 QRectF m_rect;
57 57 QLegend* m_legend;
58 58 ChartDataSet *m_dataset;
59 59 ChartPresenter *m_presenter;
60 QRectF m_padding;
60 QRect m_padding;
61 61 };
62 62
63 63 QTCOMMERCIALCHART_END_NAMESPACE
64 64 #endif
@@ -1,233 +1,244
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 "qchart_p.h"
23 23 #include "qchartview_p.h"
24 24 #include <QGraphicsScene>
25 25 #include <QRubberBand>
26 26
27 27
28 28 /*!
29 29 \enum QChartView::RubberBandPolicy
30 30
31 31 This enum describes the different types of rubber bands that can be used for zoom rect selection
32 32
33 33 \value NoRubberBand
34 34 \value VerticalRubberBand
35 35 \value HorizonalRubberBand
36 36 \value RectangleRubberBand
37 37 */
38 38
39 39 /*!
40 40 \class QChartView
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. It manages the graphical representation of different types of
45 45 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
46 46 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
47 47
48 48 \sa QChart
49 49 */
50 50
51 51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52 52
53 53 /*!
54 54 Constructs a chartView object which is a child of a\a parent.
55 55 */
56 56 QChartView::QChartView(QChart *chart,QWidget *parent) :
57 57 QGraphicsView(parent),
58 58 d_ptr(new QChartViewPrivate())
59 59 {
60 60 d_ptr->m_scene = new QGraphicsScene(this);
61 61 d_ptr->m_chart = chart;
62 d_ptr->m_presenter = chart->d_ptr->m_presenter;
63 62 setFrameShape(QFrame::NoFrame);
64 63 setBackgroundRole(QPalette::Window);
65 64 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 65 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 66 setScene(d_ptr->m_scene);
68 67 d_ptr->m_scene->addItem(chart);
69 68 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
70 69 }
71 70
72 71
73 72 /*!
74 73 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
75 74 */
76 75 QChartView::~QChartView()
77 76 {
78 77 }
79 78
80 79 QChart* QChartView::chart() const
81 80 {
82 81 return d_ptr->m_chart;
83 82 }
84 83
85 84 /*!
86 85 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
87 86 */
88 87 void QChartView::setRubberBand(const RubberBands& rubberBand)
89 88 {
90 89 d_ptr->m_rubberBandFlags=rubberBand;
91 90
92 91 if (!d_ptr->m_rubberBandFlags) {
93 92 delete d_ptr->m_rubberBand;
94 93 d_ptr->m_rubberBand=0;
95 94 return;
96 95 }
97 96
98 97 if (!d_ptr->m_rubberBand) {
99 98 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
100 99 d_ptr->m_rubberBand->setEnabled(true);
101 100 }
102 101 }
103 102
104 103 /*!
105 104 Returns the RubberBandPolicy that is currently being used by the widget.
106 105 */
107 106 QChartView::RubberBands QChartView::rubberBand() const
108 107 {
109 108 return d_ptr->m_rubberBandFlags;
110 109 }
111 110
112 111 /*!
113 112 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.
114 113 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
115 114 */
116 115 void QChartView::mousePressEvent(QMouseEvent *event)
117 116 {
118 117 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
119 118
120 119 int padding = d_ptr->m_chart->padding().top();
121 120 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
122 121
123 122 if (rect.contains(event->pos())) {
124 123 d_ptr->m_rubberBandOrigin = event->pos();
125 124 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
126 125 d_ptr->m_rubberBand->show();
127 126 event->accept();
128 127 }
129 128 }
130 129 else {
131 130 QGraphicsView::mousePressEvent(event);
132 131 }
133 132 }
134 133
135 134 /*!
136 135 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
137 136 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
138 137 */
139 138 void QChartView::mouseMoveEvent(QMouseEvent *event)
140 139 {
141 140 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
142 141 int padding = d_ptr->m_chart->padding().top();
143 142 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
144 143 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
145 144 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
146 145 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
147 146 d_ptr->m_rubberBandOrigin.setY(rect.top());
148 147 height = rect.height();
149 148 }
150 149 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
151 150 d_ptr->m_rubberBandOrigin.setX(rect.left());
152 151 width= rect.width();
153 152 }
154 153 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
155 154 }
156 155 else {
157 156 QGraphicsView::mouseMoveEvent(event);
158 157 }
159 158 }
160 159
161 160 /*!
162 161 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
163 162 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
164 163 */
165 164 void QChartView::mouseReleaseEvent(QMouseEvent *event)
166 165 {
167 166 if(d_ptr->m_rubberBand) {
168 167 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
169 168 d_ptr->m_rubberBand->hide();
170 169 QRect rect = d_ptr->m_rubberBand->geometry();
171 170 d_ptr->m_chart->zoomIn(rect);
172 171 event->accept();
173 172 }
174 173
175 174 if(event->button()==Qt::RightButton){
176 175 d_ptr->m_chart->zoomOut();
177 176 event->accept();
178 177 }
179 178 }
180 179 else {
181 180 QGraphicsView::mouseReleaseEvent(event);
182 181 }
183 182 }
184 183
185 184 /*!
186 185 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
187 186 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
188 187 */
189 188 void QChartView::keyPressEvent(QKeyEvent *event)
190 189 {
191 190 switch (event->key()) {
192 191 case Qt::Key_Plus:
193 d_ptr->m_chart->zoomIn();
194 break;
192 d_ptr->m_chart->zoomIn();
193 break;
195 194 case Qt::Key_Minus:
196 d_ptr->m_chart->zoomOut();
197 break;
195 d_ptr->m_chart->zoomOut();
196 break;
197 case Qt::Key_Left:
198 d_ptr->m_chart->scrollLeft();
199 break;
200 case Qt::Key_Right:
201 d_ptr->m_chart->scrollRight();
202 break;
203 case Qt::Key_Up:
204 d_ptr->m_chart->scrollUp();
205 break;
206 case Qt::Key_Down:
207 d_ptr->m_chart->scrollDown();
208 break;
198 209 default:
199 QGraphicsView::keyPressEvent(event);
200 break;
210 QGraphicsView::keyPressEvent(event);
211 break;
201 212 }
202 213 }
203 214
204 215 /*!
205 216 Resizes and updates the chart area using the \a event data
206 217 */
207 218 void QChartView::resizeEvent(QResizeEvent *event)
208 219 {
209 220 QGraphicsView::resizeEvent(event);
210 221 d_ptr->m_chart->resize(size());
211 222 setSceneRect(d_ptr->m_chart->geometry());
212 223 }
213 224
214 225 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
215 226
216 227 QChartViewPrivate::QChartViewPrivate():
217 228 m_scene(0),
218 229 m_chart(0),
219 230 m_presenter(0),
220 231 m_rubberBand(0),
221 232 m_rubberBandFlags(QChartView::NoRubberBand)
222 233 {
223 234
224 235 }
225 236
226 237 QChartViewPrivate::~QChartViewPrivate()
227 238 {
228 239
229 240 }
230 241
231 242 #include "moc_qchartview.cpp"
232 243
233 244 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,162 +1,216
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
22 22 #include <QtTest/QtTest>
23 23 #include <qchartview.h>
24 24 #include <qlineseries.h>
25 25 #include <cmath>
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29
30 30 Q_DECLARE_METATYPE(QChart*)
31 31 Q_DECLARE_METATYPE(QChartView::RubberBands)
32 Q_DECLARE_METATYPE(Qt::Key)
32 33
33 34 class tst_QChartView : public QObject
34 35 {
35 36 Q_OBJECT
36 37
37 38 public Q_SLOTS:
38 39 void initTestCase();
39 40 void cleanupTestCase();
40 41 void init();
41 42 void cleanup();
42 43
43 44 private Q_SLOTS:
44 45 void qchartview_data();
45 46 void qchartview();
46 47 void chart_data();
47 48 void chart();
48 49 void rubberBand_data();
49 50 void rubberBand();
51 void keys_data();
52 void keys();
50 53
51 54 private:
52 55 QChartView* m_view;
53 56 };
54 57
55 58 void tst_QChartView::initTestCase()
56 59 {
60 //test tracks mouse, give a while to user to relese it
61 QTest::qWait(1000);
57 62 }
58 63
59 64 void tst_QChartView::cleanupTestCase()
60 65 {
61 66 }
62 67
63 68 void tst_QChartView::init()
64 69 {
65 70 m_view = new QChartView(new QChart());
66 71 }
67 72
68 73 void tst_QChartView::cleanup()
69 74 {
70 75 delete m_view;
71 76 m_view =0;
72 77 }
73 78
74 79 void tst_QChartView::qchartview_data()
75 80 {
76 81
77 82 }
78 83
79 84 void tst_QChartView::qchartview()
80 85 {
81 86 QVERIFY(m_view->chart());
82 87 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
83 88 m_view->show();
84 89 QTest::qWaitForWindowShown(m_view);
85 90 }
86 91
87 92 void tst_QChartView::chart_data()
88 93 {
89 94
90 95 QTest::addColumn<QChart*>("chart");
91 96 QTest::newRow("qchart") << new QChart();
92 97 }
93 98
94 99 void tst_QChartView::chart()
95 100 {
96 101 QFETCH(QChart*, chart);
97 102 QChartView* view = new QChartView(chart);
98 103 QCOMPARE(view->chart(), chart);
99 104 delete view;
100 105 }
101 106
102 107 void tst_QChartView::rubberBand_data()
103 108 {
104 109 QTest::addColumn<QChartView::RubberBands>("rubberBand");
105 110 QTest::addColumn<int>("Xcount");
106 111 QTest::addColumn<int>("Ycount");
107 112
108 113 QTest::addColumn<int>("minX");
109 114 QTest::addColumn<int>("maxX");
110 115 QTest::addColumn<int>("minY");
111 116 QTest::addColumn<int>("maxY");
112 117
113 118 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 10 << 90 << 0<< 100;
114 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 10 << 90 << 0<< 100;
115 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<10 << 90 << 0<< 100;
119 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 100 << 10<< 90;
120 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<10 << 90 << 10<< 90;
116 121 }
117 122
118 123 void tst_QChartView::rubberBand()
119 124 {
120 125 QFETCH(QChartView::RubberBands, rubberBand);
121 126 QFETCH(int, Xcount);
122 127 QFETCH(int, Ycount);
123 128 QFETCH(int, minX);
124 129 QFETCH(int, maxX);
125 130 QFETCH(int, minY);
126 131 QFETCH(int, maxY);
127 132
128 133 m_view->setRubberBand(rubberBand);
134 QRect padding = m_view->chart()->padding();
129 135 QCOMPARE(m_view->rubberBand(), rubberBand);
130 136
131 137 QLineSeries* line = new QLineSeries();
132 138 *line << QPointF(0, 0) << QPointF(100, 100);
133 139
134 140 m_view->chart()->addSeries(line);
135 m_view->resize(1000, 1000);
141 m_view->resize(100 + padding.left() + padding.right(), 100 + padding.top()+ padding.bottom());
136 142 m_view->show();
143
144 //this is hack since view does not get events otherwise
137 145 m_view->setMouseTracking(true);
138 146
139 147 QChartAxis* axisY = m_view->chart()->axisY();
140 148 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal, qreal)));
141 149 QChartAxis* axisX = m_view->chart()->axisX();
142 150 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal, qreal)));
143 151
152
144 153 QTest::qWaitForWindowShown(m_view);
145 QTest::mouseMove(m_view->viewport(), QPoint(250, 250));
146 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(250, 250));
147 QTest::mouseMove(m_view->viewport(), QPoint(900, 900));
148 QTest::qWait(3000);
149 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(900, 900));
150 QTest::qWait(3000);
154 QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft());
155
156 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft());
157 QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft());
158
159 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft());
160
151 161 QCOMPARE(spy0.count(), Xcount);
152 162 QCOMPARE(spy1.count(), Ycount);
153 qDebug()<<axisX->min();
154 QVERIFY(int(ceil(axisX->min())) - minX < 1);
155 QVERIFY(int(ceil(axisX->max())) - maxX < 1);
156 QVERIFY(int(ceil(axisY->min())) - minY < 1);
157 QVERIFY(int(ceil(axisY->max())) - maxY < 1);
163
164 //this is hack since view does not get events otherwise
165 m_view->setMouseTracking(false);
166
167 QVERIFY(axisX->min() - minX < 1);
168 QVERIFY(axisX->max() - maxX < 1);
169 QVERIFY(axisY->min() - minY < 1);
170 QVERIFY(axisY->max() - maxY < 1);
171 }
172
173 void tst_QChartView::keys_data()
174 {
175 QTest::addColumn<Qt::Key>("key");
176 QTest::addColumn<int>("Xcount");
177 QTest::addColumn<int>("Ycount");
178 QTest::newRow("Qt::Key_Plus") << Qt::Key_Plus << 1 << 1;
179 QTest::newRow("Qt::Key_Minus") << Qt::Key_Minus << 1 << 1;
180 QTest::newRow("Qt::Key_Up") << Qt::Key_Up << 0 << 1;
181 QTest::newRow("Qt::Key_Down") << Qt::Key_Down << 0 << 1;
182 QTest::newRow("Qt::Key_Left") << Qt::Key_Left << 1 << 0;
183 QTest::newRow("Qt::Key_Right") << Qt::Key_Right << 1 << 0;
184 }
185
186 void tst_QChartView::keys()
187 {
188 QFETCH(Qt::Key,key);
189 QFETCH(int, Xcount);
190 QFETCH(int, Ycount);
191
192 QRect padding = m_view->chart()->padding();
193
194 QLineSeries* line = new QLineSeries();
195 *line << QPointF(0, 0) << QPointF(100, 100);
196
197 m_view->chart()->addSeries(line);
198 m_view->resize(100 + padding.left() + padding.right(), 100 + padding.top()+ padding.bottom());
199 m_view->show();
200
201 QChartAxis* axisY = m_view->chart()->axisY();
202 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal, qreal)));
203 QChartAxis* axisX = m_view->chart()->axisX();
204 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal, qreal)));
205
206 QTest::keyPress(m_view, key);
207 QTest::keyRelease(m_view, key);
208
209 QCOMPARE(spy0.count(), Ycount);
210 QCOMPARE(spy1.count(), Xcount);
211
158 212 }
159 213
160 214 QTEST_MAIN(tst_QChartView)
161 215 #include "tst_qchartview.moc"
162 216
General Comments 0
You need to be logged in to leave comments. Login now