##// END OF EJS Templates
Adds charts to chartsviewer
Michal Klocek -
r1753:6c2ac002bf7e
parent child
Show More
@@ -0,0 +1,29
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 "charts.h"
22
23 Chart::Chart(){};
24 Chart::~Chart(){};
25
26 void Chart::initialize()
27 {
28 Charts::addChart(this);
29 }
@@ -0,0 +1,86
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
22 #ifndef CHARTS_H
23 #define CHARTS_H
24 #include "window.h"
25 #include <QList>
26 #include <QString>
27 #include <qchartglobal.h>
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QChart;
31 QTCOMMERCIALCHART_END_NAMESPACE
32
33 QTCOMMERCIALCHART_USE_NAMESPACE
34
35 class Chart
36 {
37 public:
38 Chart();
39
40 virtual ~Chart();
41
42 virtual void initialize();
43 virtual QChart* createChart(const DataTable& table) = 0;
44 virtual QString name() = 0;
45 virtual QString category() = 0;
46 virtual QString subCategory() = 0;
47
48 };
49
50 namespace Charts
51 {
52
53 typedef QList<Chart*> ChartList;
54
55 inline ChartList& chartList()
56 {
57 static ChartList list;
58 return list;
59 }
60
61 inline bool findChart(Chart* chart)
62 {
63 ChartList& list = chartList();
64 if (list.contains(chart)) {
65 return true;
66 }
67 foreach (Chart* item, list) {
68 if (item->name() == chart->name() && item->category() == chart->category() && item->subCategory() == chart->subCategory()) {
69 return true;
70 }
71 }
72 return false;
73 }
74
75 inline void addChart(Chart* chart)
76 {
77 ChartList& list = chartList();
78 if (!findChart(chart)) {
79 list.append(chart);
80 }
81 }
82 }
83
84 #define DECLARE_CHART(chartName) static chartName t;
85
86 #endif
@@ -0,0 +1,74
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 "charts.h"
22 #include "qchart.h"
23 #include "qareaseries.h"
24 #include "qlineseries.h"
25
26 class AreaChart: public Chart
27 {
28 public:
29
30 AreaChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("AreaChart"); }
35 QString category() { return QObject::tr("XYSeries"); }
36 QString subCategory() { return QString::null; }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart *chart = new QChart();
42 chart->setTitle("Area chart");
43
44 // The lower series initialized to zero values
45 QLineSeries *lowerSeries = 0;
46 QString name("Series ");
47 int nameIndex = 0;
48 for (int i(0); i < table.count(); i++) {
49 QLineSeries *upperSeries = new QLineSeries(chart);
50 for (int j(0); j < table[i].count(); j++) {
51 Data data = table[i].at(j);
52 if (lowerSeries) {
53 const QList<QPointF>& points = lowerSeries->points();
54 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
55 }
56 else
57 upperSeries->append(QPointF(j, data.first.y()));
58 }
59 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
60 area->setName(name + QString::number(nameIndex));
61 nameIndex++;
62 chart->addSeries(area);
63 chart->createDefaultAxes();
64 lowerSeries = upperSeries;
65 }
66
67 return chart;
68
69 }
70
71 };
72
73 DECLARE_CHART(AreaChart)
74
@@ -0,0 +1,60
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 "charts.h"
22 #include "qchart.h"
23 #include "qstackedbarseries.h"
24 #include "qbarset.h"
25
26 class BarChart: public Chart
27 {
28 public:
29
30 BarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("VerticalStackedBarChart"); }
35 QString category() { return QObject::tr("BarSeries"); }
36 QString subCategory() { return QObject::tr("Vertical"); }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart* chart = new QChart();
42
43 chart->setTitle("Bar chart");
44
45 QStackedBarSeries* series = new QStackedBarSeries(chart);
46 for (int i(0); i < table.count(); i++) {
47 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 foreach (Data data, table[i])
49 *set << data.first.y();
50 series->append(set);
51 }
52 chart->addSeries(series);
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(BarChart)
60
@@ -0,0 +1,4
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
3
4 SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp No newline at end of file
@@ -0,0 +1,61
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 "charts.h"
22 #include "qchart.h"
23 #include "qlineseries.h"
24
25 class LineChart: public Chart
26 {
27 public:
28
29 LineChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("LineChart"); }
34 QString category() { return QObject::tr("XYSeries"); }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table) {
38
39 QChart* chart = new QChart();
40 chart->setTitle("Line chart");
41
42 QString name("Series ");
43 int nameIndex = 0;
44 foreach (DataList list, table) {
45 QLineSeries *series = new QLineSeries(chart);
46 foreach (Data data, list)
47 series->append(data.first);
48 series->setName(name + QString::number(nameIndex));
49 nameIndex++;
50 chart->addSeries(series);
51 }
52
53 chart->createDefaultAxes();
54
55 return chart;
56 }
57
58 };
59
60 DECLARE_CHART(LineChart)
61
@@ -0,0 +1,64
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 "charts.h"
22 #include "qchart.h"
23 #include "qpieseries.h"
24
25 class PieChart: public Chart
26 {
27 public:
28
29 PieChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("PieChart"); }
34 QString category() { return QString::null; }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table)
38 {
39 QChart* chart = new QChart();
40 chart->setTitle("Pie chart");
41
42 qreal pieSize = 1.0 / table.count();
43 for (int i = 0; i < table.count(); i++) {
44 QPieSeries *series = new QPieSeries(chart);
45 foreach (Data data, table[i]) {
46 QPieSlice *slice = series->append(data.second, data.first.y());
47 if (data == table[i].first()) {
48 slice->setLabelVisible();
49 slice->setExploded();
50 }
51 }
52 qreal hPos = (pieSize / 2) + (i / (qreal) table.count());
53 series->setPieSize(pieSize);
54 series->setHorizontalPosition(hPos);
55 series->setVerticalPosition(0.5);
56 chart->addSeries(series);
57 }
58 return chart;
59 }
60
61 };
62
63 DECLARE_CHART(PieChart)
64
@@ -0,0 +1,59
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 "charts.h"
22 #include "qchart.h"
23 #include "qscatterseries.h"
24
25 class ScatterChart: public Chart
26 {
27 public:
28
29 ScatterChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("ScatterChart"); }
34 QString category() { return QObject::tr("XYSeries"); }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table)
38 {
39
40 QChart* chart = new QChart();
41 chart->setTitle("Scatter chart");
42 QString name("Series ");
43 int nameIndex = 0;
44 foreach (DataList list, table) {
45 QScatterSeries *series = new QScatterSeries(chart);
46 foreach (Data data, list)
47 series->append(data.first);
48 series->setName(name + QString::number(nameIndex));
49 nameIndex++;
50 chart->addSeries(series);
51 }
52 chart->createDefaultAxes();
53
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(ScatterChart)
@@ -0,0 +1,60
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 "charts.h"
22 #include "qchart.h"
23 #include "qsplineseries.h"
24
25 class SplineChart: public Chart
26 {
27 public:
28
29 SplineChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("SplineChart"); }
34 QString category() { return QObject::tr("XYSeries"); }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table)
38 {
39
40 QChart* chart = new QChart();
41
42 chart->setTitle("Spline chart");
43 QString name("Series ");
44 int nameIndex = 0;
45 foreach (DataList list, table) {
46 QSplineSeries *series = new QSplineSeries(chart);
47 foreach (Data data, list)
48 series->append(data.first);
49 series->setName(name + QString::number(nameIndex));
50 nameIndex++;
51 chart->addSeries(series);
52 }
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(SplineChart)
60
@@ -1,5 +1,7
1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 include(charts/charts.pri)
2 TARGET = chartviewer
3 TARGET = chartviewer
3 QT += opengl
4 QT += opengl
4 SOURCES = main.cpp window.cpp view.cpp
5 SOURCES += main.cpp window.cpp view.cpp charts.cpp
5 HEADERS = window.h view.h No newline at end of file
6 HEADERS += window.h view.h charts.h
7
@@ -1,585 +1,430
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "window.h"
21 #include "window.h"
22 #include "view.h"
22 #include "view.h"
23 #include "charts.h"
23
24
24 #include <QChartView>
25 #include <QChartView>
25 #include <QPieSeries>
26 #include <QPieSeries>
26 #include <QPieSlice>
27 #include <QPieSlice>
27 #include <QPercentBarSeries>
28 #include <QPercentBarSeries>
28 #include <QStackedBarSeries>
29 #include <QStackedBarSeries>
29 #include <QBarSeries>
30 #include <QBarSeries>
30 #include <QBarSet>
31 #include <QBarSet>
31 #include <QLineSeries>
32 #include <QLineSeries>
32 #include <QSplineSeries>
33 #include <QSplineSeries>
33 #include <QScatterSeries>
34 #include <QScatterSeries>
34 #include <QAreaSeries>
35 #include <QAreaSeries>
35 #include <QLegend>
36 #include <QLegend>
36 #include <QGridLayout>
37 #include <QGridLayout>
37 #include <QFormLayout>
38 #include <QFormLayout>
38 #include <QComboBox>
39 #include <QComboBox>
39 #include <QSpinBox>
40 #include <QSpinBox>
40 #include <QCheckBox>
41 #include <QCheckBox>
41 #include <QGroupBox>
42 #include <QGroupBox>
42 #include <QLabel>
43 #include <QLabel>
43 #include <QTime>
44 #include <QTime>
44 #include <QBarCategoriesAxis>
45 #include <QBarCategoriesAxis>
45 #include <QGraphicsScene>
46 #include <QGraphicsScene>
46 #include <QGraphicsGridLayout>
47 #include <QGraphicsGridLayout>
47 #include <QGraphicsLinearLayout>
48 #include <QGraphicsLinearLayout>
48 #include <QGraphicsProxyWidget>
49 #include <QGraphicsProxyWidget>
49 #include <QGLWidget>
50 #include <QGLWidget>
50 #include <QApplication>
51 #include <QApplication>
51 #include <QDebug>
52 #include <QDebug>
52
53
53 Window::Window(QWidget* parent) :
54 Window::Window(QWidget* parent) :
54 QMainWindow(parent),
55 QMainWindow(parent),
55 m_listCount(3),
56 m_listCount(3),
56 m_valueMax(10),
57 m_valueMax(10),
57 m_valueCount(7),
58 m_valueCount(7),
58 m_scene(new QGraphicsScene(this)),
59 m_scene(new QGraphicsScene(this)),
59 m_view(0),
60 m_view(0),
60 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
61 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
61 m_form(0),
62 m_form(0),
62 m_themeComboBox(0),
63 m_themeComboBox(0),
63 m_antialiasCheckBox(0),
64 m_antialiasCheckBox(0),
64 m_animatedComboBox(0),
65 m_animatedComboBox(0),
65 m_legendComboBox(0),
66 m_legendComboBox(0),
66 m_openGLCheckBox(0),
67 m_openGLCheckBox(0),
67 m_zoomCheckBox(0),
68 m_zoomCheckBox(0),
68 m_scrollCheckBox(0),
69 m_scrollCheckBox(0),
69 m_rubberBand(new QGraphicsRectItem()),
70 m_rubberBand(new QGraphicsRectItem()),
70 m_isScrolling(false),
71 m_isScrolling(false),
71 m_isZooming(false),
72 m_isZooming(false),
72 m_scroll(false),
73 m_scroll(false),
73 m_zoom(false)
74 m_zoom(false)
74 {
75 {
75 createProxyWidgets();
76 createProxyWidgets();
76 connectSignals();
77 connectSignals();
77
78
78 // create layout
79 // create layout
79 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
80 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
80 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
81 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
81 settingsLayout->setOrientation(Qt::Vertical);
82 settingsLayout->setOrientation(Qt::Vertical);
82 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
83 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
83 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
84 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
84 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
85 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
85 settingsLayout->addItem(m_widgetHash["themeLabel"]);
86 settingsLayout->addItem(m_widgetHash["themeLabel"]);
86 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
87 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
87 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
88 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
88 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
89 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
89 settingsLayout->addItem(m_widgetHash["legendLabel"]);
90 settingsLayout->addItem(m_widgetHash["legendLabel"]);
90 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
91 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
91 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
92 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
92 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
93 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
93 settingsLayout->addStretch();
94 settingsLayout->addStretch();
94 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
95 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
95 //create charts
96 //create charts
96
97
97 int i = m_widgetHash.count();
98 int i = m_widgetHash.count();
98 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
99 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
99 widget->setZValue(i--);
100 widget->setZValue(i--);
100 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
101 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
101 }
102 }
102
103
103 QChart* chart;
104
104
105 chart = createAreaChart();
105 Charts::ChartList list = Charts::chartList();
106 baseLayout->addItem(chart, 0, 0);
107 m_chartList << chart;
108
109 chart = createBarChart(m_valueCount);
110 baseLayout->addItem(chart, 0, 1);
111 m_chartList << chart;
112
106
113 chart = createLineChart();
107 for(int i = 0 ; i < 6 ; ++i)
114 baseLayout->addItem(chart, 0, 2);
108 {
115 m_chartList << chart;
109 if(!(i<list.size()) || list.isEmpty()) break;
116
110 QChart* chart = list.at(i)->createChart(m_dataTable);
117 chart = createPieChart();
111 baseLayout->addItem(chart, i/3, i%3);
118 baseLayout->addItem(chart, 1, 0);
119 m_chartList << chart;
120
121 chart = createSplineChart();
122 baseLayout->addItem(chart, 1, 1);
123 m_chartList << chart;
124
125 chart = createScatterChart();
126 baseLayout->addItem(chart, 1, 2);
127 m_chartList << chart;
112 m_chartList << chart;
113 }
128
114
129 m_form = new QGraphicsWidget();
115 m_form = new QGraphicsWidget();
130 m_form->setLayout(baseLayout);
116 m_form->setLayout(baseLayout);
131 m_scene->addItem(m_form);
117 m_scene->addItem(m_form);
132 m_scene->addItem(m_rubberBand);
118 m_scene->addItem(m_rubberBand);
133 m_rubberBand->setVisible(false);
119 m_rubberBand->setVisible(false);
134
120
135 m_view = new View(m_scene, m_form);
121 m_view = new View(m_scene, m_form);
136 m_view->setMinimumSize(m_form->preferredSize().toSize());
122 m_view->setMinimumSize(m_form->preferredSize().toSize());
137
123
138 // Set defaults
124 // Set defaults
139 m_antialiasCheckBox->setChecked(true);
125 m_antialiasCheckBox->setChecked(true);
140 updateUI();
126 updateUI();
141 setCentralWidget(m_view);
127 setCentralWidget(m_view);
142 }
128 }
143
129
144 Window::~Window()
130 Window::~Window()
145 {
131 {
146 }
132 }
147
133
148 void Window::connectSignals()
134 void Window::connectSignals()
149 {
135 {
150 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
136 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
151 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
137 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
152 connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
138 connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
153 connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
139 connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
154 connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
140 connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
155 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
141 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
156 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
142 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
157 }
143 }
158
144
159 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
145 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
160 {
146 {
161 DataTable dataTable;
147 DataTable dataTable;
162
148
163 // set seed for random stuff
149 // set seed for random stuff
164 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
150 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
165
151
166 // generate random data
152 // generate random data
167 for (int i(0); i < listCount; i++) {
153 for (int i(0); i < listCount; i++) {
168 DataList dataList;
154 DataList dataList;
169 qreal yValue(0);
155 qreal yValue(0);
170 for (int j(0); j < valueCount; j++) {
156 for (int j(0); j < valueCount; j++) {
171 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
157 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
172 QPointF value(
158 QPointF value(
173 (j + (qreal) qrand() / (qreal) RAND_MAX)
159 (j + (qreal) qrand() / (qreal) RAND_MAX)
174 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
160 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
175 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
161 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
176 dataList << Data(value, label);
162 dataList << Data(value, label);
177 }
163 }
178 dataTable << dataList;
164 dataTable << dataList;
179 }
165 }
180
166
181 return dataTable;
167 return dataTable;
182 }
168 }
183
169
184 void Window::createProxyWidgets()
170 void Window::createProxyWidgets()
185 {
171 {
186 m_themeComboBox = createThemeBox();
172 m_themeComboBox = createThemeBox();
187 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
173 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
188 m_animatedComboBox = createAnimationBox();
174 m_animatedComboBox = createAnimationBox();
189 m_legendComboBox = createLegendBox();
175 m_legendComboBox = createLegendBox();
190 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
176 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
191 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
177 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
192 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
178 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
193 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
179 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
194 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
180 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
195 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
181 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
196 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
182 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
197 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
183 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
198 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
184 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
199 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
185 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
200 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
186 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
201 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
187 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
202 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
188 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
203 }
189 }
204
190
205 QComboBox* Window::createThemeBox() const
191 QComboBox* Window::createThemeBox() const
206 {
192 {
207 // settings layoutQGLWidget’
193 // settings layoutQGLWidget’
208 QComboBox* themeComboBox = new QComboBox();
194 QComboBox* themeComboBox = new QComboBox();
209 themeComboBox->addItem("Light", QChart::ChartThemeLight);
195 themeComboBox->addItem("Light", QChart::ChartThemeLight);
210 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
196 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
211 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
197 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
212 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
198 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
213 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
199 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
214 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
200 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
215 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
201 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
216 return themeComboBox;
202 return themeComboBox;
217 }
203 }
218
204
219 QComboBox* Window::createAnimationBox() const
205 QComboBox* Window::createAnimationBox() const
220 {
206 {
221 // settings layout
207 // settings layout
222 QComboBox* animationComboBox = new QComboBox();
208 QComboBox* animationComboBox = new QComboBox();
223 animationComboBox->addItem("No Animations", QChart::NoAnimation);
209 animationComboBox->addItem("No Animations", QChart::NoAnimation);
224 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
210 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
225 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
211 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
226 animationComboBox->addItem("All Animations", QChart::AllAnimations);
212 animationComboBox->addItem("All Animations", QChart::AllAnimations);
227 return animationComboBox;
213 return animationComboBox;
228 }
214 }
229
215
230 QComboBox* Window::createLegendBox() const
216 QComboBox* Window::createLegendBox() const
231 {
217 {
232 QComboBox* legendComboBox = new QComboBox();
218 QComboBox* legendComboBox = new QComboBox();
233 legendComboBox->addItem("No Legend ", 0);
219 legendComboBox->addItem("No Legend ", 0);
234 legendComboBox->addItem("Legend Top", Qt::AlignTop);
220 legendComboBox->addItem("Legend Top", Qt::AlignTop);
235 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
221 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
236 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
222 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
237 legendComboBox->addItem("Legend Right", Qt::AlignRight);
223 legendComboBox->addItem("Legend Right", Qt::AlignRight);
238 return legendComboBox;
224 return legendComboBox;
239 }
225 }
240
226
241 QChart* Window::createAreaChart() const
242 {
243 QChart *chart = new QChart();
244 // chart->axisX()->setNiceNumbersEnabled(true);
245 // chart->axisY()->setNiceNumbersEnabled(true);
246 chart->setTitle("Area chart");
247
248 // The lower series initialized to zero values
249 QLineSeries *lowerSeries = 0;
250 QString name("Series ");
251 int nameIndex = 0;
252 for (int i(0); i < m_dataTable.count(); i++) {
253 QLineSeries *upperSeries = new QLineSeries(chart);
254 for (int j(0); j < m_dataTable[i].count(); j++) {
255 Data data = m_dataTable[i].at(j);
256 if (lowerSeries) {
257 const QList<QPointF>& points = lowerSeries->points();
258 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
259 }
260 else
261 upperSeries->append(QPointF(j, data.first.y()));
262 }
263 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
264 area->setName(name + QString::number(nameIndex));
265 nameIndex++;
266 chart->addSeries(area);
267 chart->createDefaultAxes();
268 lowerSeries = upperSeries;
269 }
270
271 return chart;
272 }
273
274 QChart* Window::createBarChart(int valueCount) const
275 {
276 Q_UNUSED(valueCount);
277 QChart* chart = new QChart();
278 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
279 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
280 chart->setTitle("Bar chart");
281
282 QStackedBarSeries* series = new QStackedBarSeries(chart);
283 for (int i(0); i < m_dataTable.count(); i++) {
284 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
285 foreach (Data data, m_dataTable[i])
286 *set << data.first.y();
287 series->append(set);
288 }
289 chart->addSeries(series);
290 chart->createDefaultAxes();
291
292 return chart;
293 }
294
295 QChart* Window::createLineChart() const
296 {
297 QChart* chart = new QChart();
298 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
299 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
300 chart->setTitle("Line chart");
301
302 QString name("Series ");
303 int nameIndex = 0;
304 foreach (DataList list, m_dataTable) {
305 QLineSeries *series = new QLineSeries(chart);
306 foreach (Data data, list)
307 series->append(data.first);
308 series->setName(name + QString::number(nameIndex));
309 nameIndex++;
310 chart->addSeries(series);
311 }
312 chart->createDefaultAxes();
313
314 return chart;
315 }
316
317 QChart* Window::createPieChart() const
318 {
319 QChart* chart = new QChart();
320 chart->setTitle("Pie chart");
321
322 qreal pieSize = 1.0 / m_dataTable.count();
323 for (int i = 0; i < m_dataTable.count(); i++) {
324 QPieSeries *series = new QPieSeries(chart);
325 foreach (Data data, m_dataTable[i]) {
326 QPieSlice *slice = series->append(data.second, data.first.y());
327 if (data == m_dataTable[i].first()) {
328 slice->setLabelVisible();
329 slice->setExploded();
330 }
331 }
332 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
333 series->setPieSize(pieSize);
334 series->setHorizontalPosition(hPos);
335 series->setVerticalPosition(0.5);
336 chart->addSeries(series);
337 }
338
339 return chart;
340 }
341
342 QChart* Window::createSplineChart() const
343 {
344 QChart* chart = new QChart();
345 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
346 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
347 chart->setTitle("Spline chart");
348 QString name("Series ");
349 int nameIndex = 0;
350 foreach (DataList list, m_dataTable) {
351 QSplineSeries *series = new QSplineSeries(chart);
352 foreach (Data data, list)
353 series->append(data.first);
354 series->setName(name + QString::number(nameIndex));
355 nameIndex++;
356 chart->addSeries(series);
357 }
358 chart->createDefaultAxes();
359 return chart;
360 }
361
362 QChart* Window::createScatterChart() const
363 {
364 QChart* chart = new QChart();
365 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
366 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
367 chart->setTitle("Scatter chart");
368 QString name("Series ");
369 int nameIndex = 0;
370 foreach (DataList list, m_dataTable) {
371 QScatterSeries *series = new QScatterSeries(chart);
372 foreach (Data data, list)
373 series->append(data.first);
374 series->setName(name + QString::number(nameIndex));
375 nameIndex++;
376 chart->addSeries(series);
377 }
378 chart->createDefaultAxes();
379 return chart;
380 }
381
382 void Window::updateUI()
227 void Window::updateUI()
383 {
228 {
384 bool opengl = m_openGLCheckBox->isChecked();
229 bool opengl = m_openGLCheckBox->isChecked();
385 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
230 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
386 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
231 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
387 m_view->deleteLater();
232 m_view->deleteLater();
388 m_view = new View(m_scene, m_form);
233 m_view = new View(m_scene, m_form);
389 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
234 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
390 setCentralWidget(m_view);
235 setCentralWidget(m_view);
391 }
236 }
392
237
393 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
238 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
394 m_themeComboBox->currentIndex()).toInt();
239 m_themeComboBox->currentIndex()).toInt();
395
240
396 foreach (QChart *chart, m_chartList)
241 foreach (QChart *chart, m_chartList)
397 chart->setTheme(theme);
242 chart->setTheme(theme);
398
243
399 QPalette pal = window()->palette();
244 QPalette pal = window()->palette();
400 if (theme == QChart::ChartThemeLight) {
245 if (theme == QChart::ChartThemeLight) {
401 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
246 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
402 pal.setColor(QPalette::WindowText, QRgb(0x404044));
247 pal.setColor(QPalette::WindowText, QRgb(0x404044));
403 }
248 }
404 else if (theme == QChart::ChartThemeDark) {
249 else if (theme == QChart::ChartThemeDark) {
405 pal.setColor(QPalette::Window, QRgb(0x121218));
250 pal.setColor(QPalette::Window, QRgb(0x121218));
406 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
251 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
407 }
252 }
408 else if (theme == QChart::ChartThemeBlueCerulean) {
253 else if (theme == QChart::ChartThemeBlueCerulean) {
409 pal.setColor(QPalette::Window, QRgb(0x40434a));
254 pal.setColor(QPalette::Window, QRgb(0x40434a));
410 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
255 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
411 }
256 }
412 else if (theme == QChart::ChartThemeBrownSand) {
257 else if (theme == QChart::ChartThemeBrownSand) {
413 pal.setColor(QPalette::Window, QRgb(0x9e8965));
258 pal.setColor(QPalette::Window, QRgb(0x9e8965));
414 pal.setColor(QPalette::WindowText, QRgb(0x404044));
259 pal.setColor(QPalette::WindowText, QRgb(0x404044));
415 }
260 }
416 else if (theme == QChart::ChartThemeBlueNcs) {
261 else if (theme == QChart::ChartThemeBlueNcs) {
417 pal.setColor(QPalette::Window, QRgb(0x018bba));
262 pal.setColor(QPalette::Window, QRgb(0x018bba));
418 pal.setColor(QPalette::WindowText, QRgb(0x404044));
263 pal.setColor(QPalette::WindowText, QRgb(0x404044));
419 }
264 }
420 else if (theme == QChart::ChartThemeHighContrast) {
265 else if (theme == QChart::ChartThemeHighContrast) {
421 pal.setColor(QPalette::Window, QRgb(0xffab03));
266 pal.setColor(QPalette::Window, QRgb(0xffab03));
422 pal.setColor(QPalette::WindowText, QRgb(0x181818));
267 pal.setColor(QPalette::WindowText, QRgb(0x181818));
423 }
268 }
424 else if (theme == QChart::ChartThemeBlueIcy) {
269 else if (theme == QChart::ChartThemeBlueIcy) {
425 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
270 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
426 pal.setColor(QPalette::WindowText, QRgb(0x404044));
271 pal.setColor(QPalette::WindowText, QRgb(0x404044));
427 }
272 }
428 else {
273 else {
429 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
274 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
430 pal.setColor(QPalette::WindowText, QRgb(0x404044));
275 pal.setColor(QPalette::WindowText, QRgb(0x404044));
431 }
276 }
432 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
277 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
433 widget->setPalette(pal);
278 widget->setPalette(pal);
434 }
279 }
435 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
280 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
436 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
281 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
437
282
438 QChart::AnimationOptions options(
283 QChart::AnimationOptions options(
439 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
284 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
440 if (m_chartList.at(0)->animationOptions() != options) {
285 if (!m_chartList.isEmpty() && m_chartList.at(0)->animationOptions() != options) {
441 foreach (QChart *chart, m_chartList)
286 foreach (QChart *chart, m_chartList)
442 chart->setAnimationOptions(options);
287 chart->setAnimationOptions(options);
443 }
288 }
444
289
445 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
290 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
446
291
447 if (!alignment) {
292 if (!alignment) {
448 foreach (QChart *chart, m_chartList) {
293 foreach (QChart *chart, m_chartList) {
449 chart->legend()->hide();
294 chart->legend()->hide();
450 }
295 }
451 }
296 }
452 else {
297 else {
453 foreach (QChart *chart, m_chartList) {
298 foreach (QChart *chart, m_chartList) {
454 chart->legend()->setAlignment(alignment);
299 chart->legend()->setAlignment(alignment);
455 chart->legend()->show();
300 chart->legend()->show();
456 }
301 }
457 }
302 }
458
303
459 bool antialias = m_antialiasCheckBox->isChecked();
304 bool antialias = m_antialiasCheckBox->isChecked();
460
305
461 if (opengl)
306 if (opengl)
462 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
307 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
463 else
308 else
464 m_view->setRenderHint(QPainter::Antialiasing, antialias);
309 m_view->setRenderHint(QPainter::Antialiasing, antialias);
465
310
466 bool scroll = m_scrollCheckBox->isChecked();
311 bool scroll = m_scrollCheckBox->isChecked();
467
312
468 if(!m_scroll & scroll){
313 if(!m_scroll & scroll){
469 m_scroll=true;
314 m_scroll=true;
470 m_zoom=false;
315 m_zoom=false;
471 m_zoomCheckBox->setChecked(false);
316 m_zoomCheckBox->setChecked(false);
472 }
317 }
473
318
474 bool zoom = m_zoomCheckBox->isChecked();
319 bool zoom = m_zoomCheckBox->isChecked();
475
320
476 if(!m_zoom & zoom){
321 if(!m_zoom & zoom){
477 m_scroll=false;
322 m_scroll=false;
478 m_zoom=true;
323 m_zoom=true;
479 m_scrollCheckBox->setChecked(false);
324 m_scrollCheckBox->setChecked(false);
480 }
325 }
481
326
482 }
327 }
483
328
484 void Window::mousePressEvent(QMouseEvent *event)
329 void Window::mousePressEvent(QMouseEvent *event)
485 {
330 {
486 if (event->button() == Qt::LeftButton) {
331 if (event->button() == Qt::LeftButton) {
487
332
488 m_origin = event->pos();
333 m_origin = event->pos();
489 m_isScrolling = false;
334 m_isScrolling = false;
490 m_isZooming = false;
335 m_isZooming = false;
491
336
492 foreach (QChart *chart, m_chartList) {
337 foreach (QChart *chart, m_chartList) {
493
338
494 QRectF geometryRect = chart->geometry();
339 QRectF geometryRect = chart->geometry();
495 QRectF plotArea = chart->plotArea();
340 QRectF plotArea = chart->plotArea();
496 plotArea.translate(geometryRect.topLeft());
341 plotArea.translate(geometryRect.topLeft());
497 if (plotArea.contains(m_origin)) {
342 if (plotArea.contains(m_origin)) {
498 m_isScrolling = m_scroll;
343 m_isScrolling = m_scroll;
499 m_isZooming = m_zoom;
344 m_isZooming = m_zoom;
500 break;
345 break;
501 }
346 }
502 }
347 }
503
348
504 if (m_isZooming) {
349 if (m_isZooming) {
505 m_rubberBand->setRect(QRectF(m_origin, QSize()));
350 m_rubberBand->setRect(QRectF(m_origin, QSize()));
506 m_rubberBand->setVisible(true);
351 m_rubberBand->setVisible(true);
507 }
352 }
508 event->accept();
353 event->accept();
509 }
354 }
510
355
511 if (event->button() == Qt::RightButton) {
356 if (event->button() == Qt::RightButton) {
512 m_origin = event->pos();
357 m_origin = event->pos();
513 m_isZooming = m_zoom;
358 m_isZooming = m_zoom;
514 }
359 }
515 }
360 }
516
361
517 void Window::mouseMoveEvent(QMouseEvent *event)
362 void Window::mouseMoveEvent(QMouseEvent *event)
518 {
363 {
519 if (m_isScrolling || m_isZooming) {
364 if (m_isScrolling || m_isZooming) {
520
365
521 foreach (QChart *chart, m_chartList) {
366 foreach (QChart *chart, m_chartList) {
522
367
523 QRectF geometryRect = chart->geometry();
368 QRectF geometryRect = chart->geometry();
524 QRectF plotArea = chart->plotArea();
369 QRectF plotArea = chart->plotArea();
525 plotArea.translate(geometryRect.topLeft());
370 plotArea.translate(geometryRect.topLeft());
526
371
527 if (plotArea.contains(m_origin)) {
372 if (plotArea.contains(m_origin)) {
528 if (m_isScrolling) {
373 if (m_isScrolling) {
529 QPointF delta = m_origin - event->pos();
374 QPointF delta = m_origin - event->pos();
530 chart->scroll(delta.x(), -delta.y());
375 chart->scroll(delta.x(), -delta.y());
531 }
376 }
532 if (m_isZooming && plotArea.contains(event->pos())) {
377 if (m_isZooming && plotArea.contains(event->pos())) {
533 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
378 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
534 }
379 }
535 break;
380 break;
536 }
381 }
537 }
382 }
538 if(m_isScrolling) m_origin = event->pos();
383 if(m_isScrolling) m_origin = event->pos();
539 event->accept();
384 event->accept();
540 }
385 }
541 }
386 }
542
387
543 void Window::mouseReleaseEvent(QMouseEvent *event)
388 void Window::mouseReleaseEvent(QMouseEvent *event)
544 {
389 {
545 if (event->button() == Qt::LeftButton) {
390 if (event->button() == Qt::LeftButton) {
546 m_isScrolling = false;
391 m_isScrolling = false;
547 if (m_isZooming) {
392 if (m_isZooming) {
548 m_isZooming = false;
393 m_isZooming = false;
549 m_rubberBand->setVisible(false);
394 m_rubberBand->setVisible(false);
550
395
551 foreach (QChart *chart, m_chartList) {
396 foreach (QChart *chart, m_chartList) {
552
397
553 QRectF geometryRect = chart->geometry();
398 QRectF geometryRect = chart->geometry();
554 QRectF plotArea = chart->plotArea();
399 QRectF plotArea = chart->plotArea();
555 plotArea.translate(geometryRect.topLeft());
400 plotArea.translate(geometryRect.topLeft());
556
401
557 if (plotArea.contains(m_origin)) {
402 if (plotArea.contains(m_origin)) {
558 QRectF rect = m_rubberBand->rect();
403 QRectF rect = m_rubberBand->rect();
559 rect.translate(-geometryRect.topLeft());
404 rect.translate(-geometryRect.topLeft());
560 chart->zoomIn(rect);
405 chart->zoomIn(rect);
561 break;
406 break;
562 }
407 }
563 }
408 }
564 }
409 }
565 event->accept();
410 event->accept();
566 }
411 }
567
412
568 if (event->button() == Qt::RightButton) {
413 if (event->button() == Qt::RightButton) {
569
414
570 if (m_isZooming) {
415 if (m_isZooming) {
571 foreach (QChart *chart, m_chartList) {
416 foreach (QChart *chart, m_chartList) {
572
417
573 QRectF geometryRect = chart->geometry();
418 QRectF geometryRect = chart->geometry();
574 QRectF plotArea = chart->plotArea();
419 QRectF plotArea = chart->plotArea();
575 plotArea.translate(geometryRect.topLeft());
420 plotArea.translate(geometryRect.topLeft());
576
421
577 if (plotArea.contains(m_origin)) {
422 if (plotArea.contains(m_origin)) {
578 QRectF rect = m_rubberBand->rect();
423 QRectF rect = m_rubberBand->rect();
579 chart->zoomOut();
424 chart->zoomOut();
580 break;
425 break;
581 }
426 }
582 }
427 }
583 }
428 }
584 }
429 }
585 }
430 }
@@ -1,102 +1,96
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef WINDOW_H_
21 #ifndef WINDOW_H_
22 #define WINDOW_H_
22 #define WINDOW_H_
23 #include <QMainWindow>
23 #include <QMainWindow>
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QHash>
25 #include <QHash>
26
26
27 class QComboBox;
27 class QComboBox;
28 class QCheckBox;
28 class QCheckBox;
29 class QGraphicsRectItem;
29 class QGraphicsRectItem;
30 class QGraphicsScene;
30 class QGraphicsScene;
31 class QGraphicsWidget;
31 class QGraphicsWidget;
32 class View;
32 class View;
33
33
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 class QChart;
35 class QChart;
36 QTCOMMERCIALCHART_END_NAMESPACE
36 QTCOMMERCIALCHART_END_NAMESPACE
37
37
38 typedef QPair<QPointF, QString> Data;
38 typedef QPair<QPointF, QString> Data;
39 typedef QList<Data> DataList;
39 typedef QList<Data> DataList;
40 typedef QList<DataList> DataTable;
40 typedef QList<DataList> DataTable;
41
41
42 QTCOMMERCIALCHART_USE_NAMESPACE
42 QTCOMMERCIALCHART_USE_NAMESPACE
43
43
44 class Window: public QMainWindow
44 class Window: public QMainWindow
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48 explicit Window(QWidget *parent = 0);
48 explicit Window(QWidget *parent = 0);
49 ~Window();
49 ~Window();
50
50
51 private Q_SLOTS:
51 private Q_SLOTS:
52 void updateUI();
52 void updateUI();
53
53
54
54
55 private:
55 private:
56 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
56 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
57 QComboBox* createThemeBox() const;
57 QComboBox* createThemeBox() const;
58 QComboBox* createAnimationBox() const;
58 QComboBox* createAnimationBox() const;
59 QComboBox* createLegendBox() const;
59 QComboBox* createLegendBox() const;
60 void connectSignals();
60 void connectSignals();
61 QChart* createAreaChart() const;
62 QChart* createBarChart(int valueCount) const;
63 QChart* createPieChart() const;
64 QChart* createLineChart() const;
65 QChart* createSplineChart() const;
66 QChart* createScatterChart() const;
67 void createProxyWidgets();
61 void createProxyWidgets();
68
62
69 protected:
63 protected:
70 void mousePressEvent(QMouseEvent *event);
64 void mousePressEvent(QMouseEvent *event);
71 void mouseMoveEvent(QMouseEvent *event);
65 void mouseMoveEvent(QMouseEvent *event);
72 void mouseReleaseEvent(QMouseEvent *event);
66 void mouseReleaseEvent(QMouseEvent *event);
73
67
74
68
75 private:
69 private:
76 int m_listCount;
70 int m_listCount;
77 int m_valueMax;
71 int m_valueMax;
78 int m_valueCount;
72 int m_valueCount;
79 QGraphicsScene* m_scene;
73 QGraphicsScene* m_scene;
80 View* m_view;
74 View* m_view;
81 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
75 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
82 QList<QChart*> m_chartList;
76 QList<QChart*> m_chartList;
83 DataTable m_dataTable;
77 DataTable m_dataTable;
84
78
85 QGraphicsWidget *m_form;
79 QGraphicsWidget *m_form;
86 QComboBox *m_themeComboBox;
80 QComboBox *m_themeComboBox;
87 QCheckBox *m_antialiasCheckBox;
81 QCheckBox *m_antialiasCheckBox;
88 QComboBox *m_animatedComboBox;
82 QComboBox *m_animatedComboBox;
89 QComboBox *m_legendComboBox;
83 QComboBox *m_legendComboBox;
90 QCheckBox *m_openGLCheckBox;
84 QCheckBox *m_openGLCheckBox;
91 QCheckBox *m_zoomCheckBox;
85 QCheckBox *m_zoomCheckBox;
92 QCheckBox *m_scrollCheckBox;
86 QCheckBox *m_scrollCheckBox;
93 QPoint m_origin;
87 QPoint m_origin;
94 QGraphicsRectItem* m_rubberBand;
88 QGraphicsRectItem* m_rubberBand;
95
89
96 bool m_isScrolling;
90 bool m_isScrolling;
97 bool m_isZooming;
91 bool m_isZooming;
98 bool m_scroll;
92 bool m_scroll;
99 bool m_zoom;
93 bool m_zoom;
100 };
94 };
101
95
102 #endif
96 #endif
General Comments 0
You need to be logged in to leave comments. Login now