##// END OF EJS Templates
Adds scroll and zoom to chartviewer
Michal Klocek -
r1748:ee59e61f224b
parent child
Show More
@@ -0,0 +1,55
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "view.h"
22 #include <QGraphicsWidget>
23 #include <QResizeEvent>
24 #include <QDebug>
25
26 View::View(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent):QGraphicsView(scene,parent),
27 m_form(form)
28 {
29 setDragMode(QGraphicsView::NoDrag);
30 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
31 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32 }
33
34 void View::resizeEvent(QResizeEvent *event)
35 {
36 if (scene())
37 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
38 if (m_form)
39 m_form->resize(QSizeF(event->size()));
40 QGraphicsView::resizeEvent(event);
41 }
42
43 void View::mouseMoveEvent(QMouseEvent *event)
44 {
45 //BugFix somehow view always eats the mouse move event;
46 event->setAccepted(false);
47 }
48
49 void View::mouseReleaseEvent(QMouseEvent *event)
50 {
51
52 QGraphicsView::mouseReleaseEvent(event);
53 //BugFix somehow view always eats the mouse release event;
54 event->setAccepted(false);
55 }
@@ -0,0 +1,41
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef VIEW_H_
22 #define VIEW_H_
23 #include <QGraphicsView>
24
25 class QGraphicsScene;
26 class QResizeEvent;
27
28 class View: public QGraphicsView
29 {
30 public:
31 View(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent = 0);
32
33 protected:
34 void resizeEvent(QResizeEvent *event);
35 void mouseMoveEvent(QMouseEvent *event);
36 void mouseReleaseEvent(QMouseEvent *event);
37 private:
38 QGraphicsWidget *m_form;
39 };
40
41 #endif
@@ -1,5 +1,5
1 1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 2 TARGET = chartviewer
3 3 QT += opengl
4 SOURCES = main.cpp chartwindow.cpp
5 HEADERS = chartwindow.h
4 SOURCES = main.cpp window.cpp view.cpp
5 HEADERS = window.h view.h No newline at end of file
@@ -18,14 +18,14
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "chartwindow.h"
21 #include "window.h"
22 22 #include <QApplication>
23 23 #include <QMainWindow>
24 24
25 25 int main(int argc, char *argv[])
26 26 {
27 27 QApplication a(argc, argv);
28 ChartWindow window;
28 Window window;
29 29 window.show();
30 30 return a.exec();
31 31 }
@@ -18,7 +18,8
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "chartwindow.h"
21 #include "window.h"
22 #include "view.h"
22 23
23 24 #include <QChartView>
24 25 #include <QPieSeries>
@@ -49,11 +50,27
49 50 #include <QApplication>
50 51 #include <QDebug>
51 52
52 ChartWindow::ChartWindow(QWidget* parent) :
53 QMainWindow(parent), m_listCount(3), m_valueMax(10), m_valueCount(7), m_scene(
54 new QGraphicsScene(this)), m_view(0), m_dataTable(
55 generateRandomData(m_listCount, m_valueMax, m_valueCount)), m_form(0), m_themeComboBox(0), m_antialiasCheckBox(
56 0), m_animatedComboBox(0), m_legendComboBox(0), m_openGLCheckBox(0)
53 Window::Window(QWidget* parent) :
54 QMainWindow(parent),
55 m_listCount(3),
56 m_valueMax(10),
57 m_valueCount(7),
58 m_scene(new QGraphicsScene(this)),
59 m_view(0),
60 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
61 m_form(0),
62 m_themeComboBox(0),
63 m_antialiasCheckBox(0),
64 m_animatedComboBox(0),
65 m_legendComboBox(0),
66 m_openGLCheckBox(0),
67 m_zoomCheckBox(0),
68 m_scrollCheckBox(0),
69 m_rubberBand(new QGraphicsRectItem()),
70 m_isScrolling(false),
71 m_isZooming(false),
72 m_scroll(false),
73 m_zoom(false)
57 74 {
58 75 createProxyWidgets();
59 76 connectSignals();
@@ -71,6 +88,8 ChartWindow::ChartWindow(QWidget* parent) :
71 88 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
72 89 settingsLayout->addItem(m_widgetHash["legendLabel"]);
73 90 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
91 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
92 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
74 93 settingsLayout->addStretch();
75 94 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
76 95 //create charts
@@ -107,34 +126,37 ChartWindow::ChartWindow(QWidget* parent) :
107 126 baseLayout->addItem(chart, 1, 2);
108 127 m_chartList << chart;
109 128
110 // Set defaults
111 //m_antialiasCheckBox->setChecked(true);
112
113 129 m_form = new QGraphicsWidget();
114 130 m_form->setLayout(baseLayout);
115 131 m_scene->addItem(m_form);
132 m_scene->addItem(m_rubberBand);
133 m_rubberBand->setVisible(false);
116 134
117 m_view = new GraphicsView(m_scene, m_form);
135 m_view = new View(m_scene, m_form);
118 136 m_view->setMinimumSize(m_form->preferredSize().toSize());
119 137
138 // Set defaults
139 m_antialiasCheckBox->setChecked(true);
120 140 updateUI();
121 141 setCentralWidget(m_view);
122 142 }
123 143
124 ChartWindow::~ChartWindow()
144 Window::~Window()
125 145 {
126 146 }
127 147
128 void ChartWindow::connectSignals()
148 void Window::connectSignals()
129 149 {
130 150 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
131 151 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
132 152 connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
153 connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
154 connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
133 155 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
134 156 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
135 157 }
136 158
137 DataTable ChartWindow::generateRandomData(int listCount, int valueMax, int valueCount) const
159 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
138 160 {
139 161 DataTable dataTable;
140 162
@@ -159,13 +181,15 DataTable ChartWindow::generateRandomData(int listCount, int valueMax, int value
159 181 return dataTable;
160 182 }
161 183
162 void ChartWindow::createProxyWidgets()
184 void Window::createProxyWidgets()
163 185 {
164 186 m_themeComboBox = createThemeBox();
165 187 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
166 188 m_animatedComboBox = createAnimationBox();
167 189 m_legendComboBox = createLegendBox();
168 190 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
191 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
192 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
169 193 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
170 194 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
171 195 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
@@ -174,9 +198,11 void ChartWindow::createProxyWidgets()
174 198 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
175 199 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
176 200 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
201 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
202 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
177 203 }
178 204
179 QComboBox* ChartWindow::createThemeBox() const
205 QComboBox* Window::createThemeBox() const
180 206 {
181 207 // settings layoutQGLWidget’
182 208 QComboBox* themeComboBox = new QComboBox();
@@ -190,7 +216,7 QComboBox* ChartWindow::createThemeBox() const
190 216 return themeComboBox;
191 217 }
192 218
193 QComboBox* ChartWindow::createAnimationBox() const
219 QComboBox* Window::createAnimationBox() const
194 220 {
195 221 // settings layout
196 222 QComboBox* animationComboBox = new QComboBox();
@@ -201,7 +227,7 QComboBox* ChartWindow::createAnimationBox() const
201 227 return animationComboBox;
202 228 }
203 229
204 QComboBox* ChartWindow::createLegendBox() const
230 QComboBox* Window::createLegendBox() const
205 231 {
206 232 QComboBox* legendComboBox = new QComboBox();
207 233 legendComboBox->addItem("No Legend ", 0);
@@ -212,7 +238,7 QComboBox* ChartWindow::createLegendBox() const
212 238 return legendComboBox;
213 239 }
214 240
215 QChart* ChartWindow::createAreaChart() const
241 QChart* Window::createAreaChart() const
216 242 {
217 243 QChart *chart = new QChart();
218 244 // chart->axisX()->setNiceNumbersEnabled(true);
@@ -245,7 +271,7 QChart* ChartWindow::createAreaChart() const
245 271 return chart;
246 272 }
247 273
248 QChart* ChartWindow::createBarChart(int valueCount) const
274 QChart* Window::createBarChart(int valueCount) const
249 275 {
250 276 Q_UNUSED(valueCount);
251 277 QChart* chart = new QChart();
@@ -266,7 +292,7 QChart* ChartWindow::createBarChart(int valueCount) const
266 292 return chart;
267 293 }
268 294
269 QChart* ChartWindow::createLineChart() const
295 QChart* Window::createLineChart() const
270 296 {
271 297 QChart* chart = new QChart();
272 298 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
@@ -288,7 +314,7 QChart* ChartWindow::createLineChart() const
288 314 return chart;
289 315 }
290 316
291 QChart* ChartWindow::createPieChart() const
317 QChart* Window::createPieChart() const
292 318 {
293 319 QChart* chart = new QChart();
294 320 chart->setTitle("Pie chart");
@@ -313,7 +339,7 QChart* ChartWindow::createPieChart() const
313 339 return chart;
314 340 }
315 341
316 QChart* ChartWindow::createSplineChart() const
342 QChart* Window::createSplineChart() const
317 343 {
318 344 QChart* chart = new QChart();
319 345 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
@@ -333,7 +359,7 QChart* ChartWindow::createSplineChart() const
333 359 return chart;
334 360 }
335 361
336 QChart* ChartWindow::createScatterChart() const
362 QChart* Window::createScatterChart() const
337 363 {
338 364 QChart* chart = new QChart();
339 365 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
@@ -353,13 +379,13 QChart* ChartWindow::createScatterChart() const
353 379 return chart;
354 380 }
355 381
356 void ChartWindow::updateUI()
382 void Window::updateUI()
357 383 {
358 384 bool opengl = m_openGLCheckBox->isChecked();
359 385 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
360 386 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
361 387 m_view->deleteLater();
362 m_view = new GraphicsView(m_scene, m_form);
388 m_view = new View(m_scene, m_form);
363 389 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
364 390 setCentralWidget(m_view);
365 391 }
@@ -407,6 +433,7 void ChartWindow::updateUI()
407 433 widget->setPalette(pal);
408 434 }
409 435 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
436 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
410 437
411 438 QChart::AnimationOptions options(
412 439 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
@@ -429,10 +456,129 void ChartWindow::updateUI()
429 456 }
430 457 }
431 458
432 bool checked = m_antialiasCheckBox->isChecked();
459 bool antialias = m_antialiasCheckBox->isChecked();
460
433 461 if (opengl)
434 m_view->setRenderHint(QPainter::HighQualityAntialiasing, checked);
462 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
435 463 else
436 m_view->setRenderHint(QPainter::Antialiasing, checked);
464 m_view->setRenderHint(QPainter::Antialiasing, antialias);
465
466 bool scroll = m_scrollCheckBox->isChecked();
467
468 if(!m_scroll & scroll){
469 m_scroll=true;
470 m_zoom=false;
471 m_zoomCheckBox->setChecked(false);
472 }
473
474 bool zoom = m_zoomCheckBox->isChecked();
475
476 if(!m_zoom & zoom){
477 m_scroll=false;
478 m_zoom=true;
479 m_scrollCheckBox->setChecked(false);
480 }
481
482 }
483
484 void Window::mousePressEvent(QMouseEvent *event)
485 {
486 if (event->button() == Qt::LeftButton) {
487
488 m_origin = event->pos();
489 m_isScrolling = false;
490 m_isZooming = false;
491
492 foreach (QChart *chart, m_chartList) {
493
494 QRectF geometryRect = chart->geometry();
495 QRectF plotArea = chart->plotArea();
496 plotArea.translate(geometryRect.topLeft());
497 if (plotArea.contains(m_origin)) {
498 m_isScrolling = m_scroll;
499 m_isZooming = m_zoom;
500 break;
501 }
502 }
503
504 if (m_isZooming) {
505 m_rubberBand->setRect(QRectF(m_origin, QSize()));
506 m_rubberBand->setVisible(true);
507 }
508 event->accept();
509 }
510
511 if (event->button() == Qt::RightButton) {
512 m_origin = event->pos();
513 m_isZooming = m_zoom;
514 }
515 }
516
517 void Window::mouseMoveEvent(QMouseEvent *event)
518 {
519 if (m_isScrolling || m_isZooming) {
520
521 foreach (QChart *chart, m_chartList) {
522
523 QRectF geometryRect = chart->geometry();
524 QRectF plotArea = chart->plotArea();
525 plotArea.translate(geometryRect.topLeft());
526
527 if (plotArea.contains(m_origin)) {
528 if (m_isScrolling) {
529 QPointF delta = m_origin - event->pos();
530 chart->scroll(delta.x(), -delta.y());
531 }
532 if (m_isZooming && plotArea.contains(event->pos())) {
533 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
534 }
535 break;
536 }
537 }
538 if(m_isScrolling) m_origin = event->pos();
539 event->accept();
540 }
541 }
542
543 void Window::mouseReleaseEvent(QMouseEvent *event)
544 {
545 if (event->button() == Qt::LeftButton) {
546 m_isScrolling = false;
547 if (m_isZooming) {
548 m_isZooming = false;
549 m_rubberBand->setVisible(false);
550
551 foreach (QChart *chart, m_chartList) {
552
553 QRectF geometryRect = chart->geometry();
554 QRectF plotArea = chart->plotArea();
555 plotArea.translate(geometryRect.topLeft());
556
557 if (plotArea.contains(m_origin)) {
558 QRectF rect = m_rubberBand->rect();
559 chart->zoomIn(rect);
560 break;
561 }
562 }
437 563 }
564 event->accept();
565 }
566
567 if (event->button() == Qt::RightButton) {
568
569 if (m_isZooming) {
570 foreach (QChart *chart, m_chartList) {
571
572 QRectF geometryRect = chart->geometry();
573 QRectF plotArea = chart->plotArea();
574 plotArea.translate(geometryRect.topLeft());
438 575
576 if (plotArea.contains(m_origin)) {
577 QRectF rect = m_rubberBand->rect();
578 chart->zoomOut();
579 break;
580 }
581 }
582 }
583 }
584 }
@@ -18,17 +18,18
18 18 **
19 19 ****************************************************************************/
20 20
21 #ifndef CHARTWINDOW_H_
22 #define CHARTWINDOW_H_
21 #ifndef WINDOW_H_
22 #define WINDOW_H_
23 23 #include <QMainWindow>
24 24 #include <QChartGlobal>
25 25 #include <QHash>
26 #include <QGraphicsView>
27 #include <QResizeEvent>
28 #include <QGraphicsWidget>
29 26
30 27 class QComboBox;
31 28 class QCheckBox;
29 class QGraphicsRectItem;
30 class QGraphicsScene;
31 class QGraphicsWidget;
32 class View;
32 33
33 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 35 class QChart;
@@ -38,43 +39,19 typedef QPair<QPointF, QString> Data;
38 39 typedef QList<Data> DataList;
39 40 typedef QList<DataList> DataTable;
40 41
41 class QGraphicsScene;
42
43 42 QTCOMMERCIALCHART_USE_NAMESPACE
44 43
45
46 class GraphicsView: public QGraphicsView
47 {
48 public:
49 GraphicsView(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent = 0):QGraphicsView(scene,parent), m_form(form)
50 {
51 setWindowTitle(tr("Charts"));
52 }
53
54 protected:
55 void resizeEvent(QResizeEvent *event)
56 {
57 if (scene())
58 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
59 if (m_form)
60 m_form->resize(QSizeF(event->size()));
61 QGraphicsView::resizeEvent(event);
62 }
63 private:
64 QGraphicsWidget *m_form;
65 };
66
67
68 class ChartWindow: public QMainWindow
44 class Window: public QMainWindow
69 45 {
70 46 Q_OBJECT
71 47 public:
72 explicit ChartWindow(QWidget *parent = 0);
73 ~ChartWindow();
48 explicit Window(QWidget *parent = 0);
49 ~Window();
74 50
75 51 private Q_SLOTS:
76 52 void updateUI();
77 53
54
78 55 private:
79 56 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
80 57 QComboBox* createThemeBox() const;
@@ -89,12 +66,18 private:
89 66 QChart* createScatterChart() const;
90 67 void createProxyWidgets();
91 68
69 protected:
70 void mousePressEvent(QMouseEvent *event);
71 void mouseMoveEvent(QMouseEvent *event);
72 void mouseReleaseEvent(QMouseEvent *event);
73
74
92 75 private:
93 76 int m_listCount;
94 77 int m_valueMax;
95 78 int m_valueCount;
96 79 QGraphicsScene* m_scene;
97 GraphicsView* m_view;
80 View* m_view;
98 81 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
99 82 QList<QChart*> m_chartList;
100 83 DataTable m_dataTable;
@@ -105,6 +88,15 private:
105 88 QComboBox *m_animatedComboBox;
106 89 QComboBox *m_legendComboBox;
107 90 QCheckBox *m_openGLCheckBox;
91 QCheckBox *m_zoomCheckBox;
92 QCheckBox *m_scrollCheckBox;
93 QPoint m_origin;
94 QGraphicsRectItem* m_rubberBand;
95
96 bool m_isScrolling;
97 bool m_isZooming;
98 bool m_scroll;
99 bool m_zoom;
108 100 };
109 101
110 102 #endif
General Comments 0
You need to be logged in to leave comments. Login now