##// END OF EJS Templates
Add horizontal charts to chartviewer
Michal Klocek -
r1755:1a6e0b1256e1
parent child
Show More
@@ -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 "qhorizontalbarseries.h"
24 #include "qbarset.h"
25
26 class HorizontalBarChart: public Chart
27 {
28 public:
29
30 HorizontalBarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("HorizontalBarChart"); }
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("Horizontal bar chart");
44
45 QHorizontalBarSeries* series = new QHorizontalBarSeries(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(HorizontalBarChart)
60
@@ -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 "qhorizontalstackedbarseries.h"
24 #include "qbarset.h"
25
26 class HorizontalStackedBarChart: public Chart
27 {
28 public:
29
30 HorizontalStackedBarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("HorizontalStackedBarChart"); }
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("Horizontal stacked chart");
44
45 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries(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(HorizontalStackedBarChart)
60
@@ -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 "qbarseries.h"
24 #include "qbarset.h"
25
26 class VerticalBarChart: public Chart
27 {
28 public:
29
30 VerticalBarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("VerticalBarChart"); }
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("Vertical bar chart");
44
45 QBarSeries* series = new QBarSeries(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(VerticalBarChart)
60
@@ -0,0 +1,67
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 MODEL_H_
22 #define MODEL_H_
23
24 #include <QList>
25 #include <QPair>
26 #include <QPointF>
27 #include <QTime>
28
29 typedef QPair<QPointF, QString> Data;
30 typedef QList<Data> DataList;
31 typedef QList<DataList> DataTable;
32
33
34 class Model
35 {
36 private:
37 Model(){}
38
39 public:
40 static DataTable generateRandomData(int listCount, int valueMax, int valueCount)
41 {
42 DataTable dataTable;
43
44 // set seed for random stuff
45 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
46
47 // generate random data
48 for (int i(0); i < listCount; i++) {
49 DataList dataList;
50 qreal yValue(0);
51 for (int j(0); j < valueCount; j++) {
52 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
53 QPointF value(
54 (j + (qreal) qrand() / (qreal) RAND_MAX)
55 * ((qreal) valueMax / (qreal) valueCount), yValue);
56 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
57 dataList << Data(value, label);
58 }
59 dataTable << dataList;
60 }
61
62 return dataTable;
63 }
64
65 };
66
67 #endif
@@ -1,86 +1,86
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 #ifndef CHARTS_H
23 23 #define CHARTS_H
24 #include "window.h"
24 #include "model.h"
25 25 #include <QList>
26 26 #include <QString>
27 27 #include <qchartglobal.h>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QChart;
31 31 QTCOMMERCIALCHART_END_NAMESPACE
32 32
33 33 QTCOMMERCIALCHART_USE_NAMESPACE
34 34
35 35 class Chart
36 36 {
37 37 public:
38 38 Chart();
39 39
40 40 virtual ~Chart();
41 41
42 42 virtual void initialize();
43 43 virtual QChart* createChart(const DataTable& table) = 0;
44 44 virtual QString name() = 0;
45 45 virtual QString category() = 0;
46 46 virtual QString subCategory() = 0;
47 47
48 48 };
49 49
50 50 namespace Charts
51 51 {
52 52
53 53 typedef QList<Chart*> ChartList;
54 54
55 55 inline ChartList& chartList()
56 56 {
57 57 static ChartList list;
58 58 return list;
59 59 }
60 60
61 61 inline bool findChart(Chart* chart)
62 62 {
63 63 ChartList& list = chartList();
64 64 if (list.contains(chart)) {
65 65 return true;
66 66 }
67 67 foreach (Chart* item, list) {
68 68 if (item->name() == chart->name() && item->category() == chart->category() && item->subCategory() == chart->subCategory()) {
69 69 return true;
70 70 }
71 71 }
72 72 return false;
73 73 }
74 74
75 75 inline void addChart(Chart* chart)
76 76 {
77 77 ChartList& list = chartList();
78 78 if (!findChart(chart)) {
79 79 list.append(chart);
80 80 }
81 81 }
82 82 }
83 83
84 84 #define DECLARE_CHART(chartName) static chartName t;
85 85
86 86 #endif
@@ -1,4 +1,13
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp No newline at end of file
4 SOURCES+= \
5 linechart.cpp \
6 scatterchart.cpp \
7 splinechart.cpp \
8 piechart.cpp \
9 verticalstackedbarchart.cpp \
10 horizontalstackedbarchart.cpp \
11 verticalbarchart.cpp \
12 horizontalbarchart.cpp \
13 areachart.cpp No newline at end of file
@@ -1,60 +1,60
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qstackedbarseries.h"
24 24 #include "qbarset.h"
25 25
26 class BarChart: public Chart
26 class VerticalStackedBarChart: public Chart
27 27 {
28 28 public:
29 29
30 BarChart(){
30 VerticalStackedBarChart(){
31 31 initialize();
32 32 }
33 33
34 34 QString name() { return QObject::tr("VerticalStackedBarChart"); }
35 35 QString category() { return QObject::tr("BarSeries"); }
36 36 QString subCategory() { return QObject::tr("Vertical"); }
37 37
38 38 QChart* createChart(const DataTable& table)
39 39 {
40 40
41 41 QChart* chart = new QChart();
42 42
43 chart->setTitle("Bar chart");
43 chart->setTitle("Stacked bar chart");
44 44
45 45 QStackedBarSeries* series = new QStackedBarSeries(chart);
46 46 for (int i(0); i < table.count(); i++) {
47 47 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 48 foreach (Data data, table[i])
49 49 *set << data.first.y();
50 50 series->append(set);
51 51 }
52 52 chart->addSeries(series);
53 53 chart->createDefaultAxes();
54 54 return chart;
55 55 }
56 56
57 57 };
58 58
59 DECLARE_CHART(BarChart)
59 DECLARE_CHART(VerticalStackedBarChart)
60 60
@@ -1,7 +1,7
1 1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 2 include(charts/charts.pri)
3 3 TARGET = chartviewer
4 4 QT += opengl
5 5 SOURCES += main.cpp window.cpp view.cpp charts.cpp
6 HEADERS += window.h view.h charts.h
6 HEADERS += window.h view.h charts.h model.h
7 7
@@ -1,433 +1,395
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 "window.h"
22 22 #include "view.h"
23 23 #include "charts.h"
24
25 24 #include <QChartView>
26 #include <QPieSeries>
27 #include <QPieSlice>
28 #include <QPercentBarSeries>
29 #include <QStackedBarSeries>
30 #include <QBarSeries>
31 #include <QBarSet>
32 #include <QLineSeries>
33 #include <QSplineSeries>
34 #include <QScatterSeries>
35 25 #include <QAreaSeries>
36 26 #include <QLegend>
37 27 #include <QGridLayout>
38 28 #include <QFormLayout>
39 29 #include <QComboBox>
40 30 #include <QSpinBox>
41 31 #include <QCheckBox>
42 32 #include <QGroupBox>
43 33 #include <QLabel>
44 #include <QTime>
45 #include <QBarCategoriesAxis>
46 34 #include <QGraphicsScene>
47 35 #include <QGraphicsGridLayout>
48 36 #include <QGraphicsLinearLayout>
49 37 #include <QGraphicsProxyWidget>
50 38 #include <QGLWidget>
51 39 #include <QApplication>
52 40 #include <QDebug>
53 41
54 42 Window::Window(QWidget* parent) :
55 43 QMainWindow(parent),
56 44 m_listCount(3),
57 45 m_valueMax(10),
58 46 m_valueCount(7),
59 47 m_scene(new QGraphicsScene(this)),
60 48 m_view(0),
61 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
49 m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)),
62 50 m_form(0),
63 51 m_themeComboBox(0),
64 52 m_antialiasCheckBox(0),
65 53 m_animatedComboBox(0),
66 54 m_legendComboBox(0),
67 55 m_openGLCheckBox(0),
68 56 m_zoomCheckBox(0),
69 57 m_scrollCheckBox(0),
70 58 m_rubberBand(new QGraphicsRectItem()),
71 59 m_isScrolling(false),
72 60 m_isZooming(false),
73 61 m_scroll(false),
74 62 m_zoom(false)
75 63 {
76 64 createProxyWidgets();
77 65 connectSignals();
78 66
79 67 // create layout
80 68 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
81 69 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
82 70 settingsLayout->setOrientation(Qt::Vertical);
83 71 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
84 72 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
85 73 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
86 74 settingsLayout->addItem(m_widgetHash["themeLabel"]);
87 75 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
88 76 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
89 77 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
90 78 settingsLayout->addItem(m_widgetHash["legendLabel"]);
91 79 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
92 80 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
93 81 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
94 82 settingsLayout->addStretch();
95 83 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
96 84 //create charts
97 85
98 86 Charts::ChartList list = Charts::chartList();
99 87
100 for(int i = 0 ; i < 6 ; ++i)
88 for(int i = 0 ; i < 9 && i<list.size() ; ++i)
101 89 {
102 if(!(i<list.size()) || list.isEmpty()) break;
103 90 QChart* chart = list.at(i)->createChart(m_dataTable);
104 91 baseLayout->addItem(chart, i/3, i%3);
105 92 m_chartList << chart;
106 93 }
107 94
108 95 m_form = new QGraphicsWidget();
109 96 m_form->setLayout(baseLayout);
110 97 m_scene->addItem(m_form);
111 98 m_scene->addItem(m_rubberBand);
112 99 m_rubberBand->setVisible(false);
113 100
114 101 m_view = new View(m_scene, m_form);
115 102 m_view->setMinimumSize(m_form->preferredSize().toSize());
116 103
117 104 // Set defaults
118 105 m_antialiasCheckBox->setChecked(true);
119 106 updateUI();
120 107 setCentralWidget(m_view);
121 108 }
122 109
123 110 Window::~Window()
124 111 {
125 112 }
126 113
127 114 void Window::connectSignals()
128 115 {
129 116 QObject::connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
130 117 QObject::connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
131 118 QObject::connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
132 119 QObject::connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
133 120 QObject::connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
134 121 QObject::connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
135 122 QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
136 123 }
137 124
138 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
139 {
140 DataTable dataTable;
141
142 // set seed for random stuff
143 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
144
145 // generate random data
146 for (int i(0); i < listCount; i++) {
147 DataList dataList;
148 qreal yValue(0);
149 for (int j(0); j < valueCount; j++) {
150 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
151 QPointF value(
152 (j + (qreal) qrand() / (qreal) RAND_MAX)
153 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
154 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
155 dataList << Data(value, label);
156 }
157 dataTable << dataList;
158 }
159
160 return dataTable;
161 }
162
163 125 void Window::createProxyWidgets()
164 126 {
165 127 m_themeComboBox = createThemeBox();
166 128 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
167 129 m_animatedComboBox = createAnimationBox();
168 130 m_legendComboBox = createLegendBox();
169 131 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
170 132 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
171 133 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
172 134 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
173 135 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
174 136 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
175 137 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
176 138 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
177 139 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
178 140 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
179 141 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
180 142 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
181 143 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
182 144 }
183 145
184 146 QComboBox* Window::createThemeBox()
185 147 {
186 148 // settings layoutQGLWidget’
187 149 QComboBox* themeComboBox = new ComboBox(this);
188 150 themeComboBox->addItem("Light", QChart::ChartThemeLight);
189 151 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
190 152 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
191 153 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
192 154 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
193 155 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
194 156 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
195 157 return themeComboBox;
196 158 }
197 159
198 160 QComboBox* Window::createAnimationBox()
199 161 {
200 162 // settings layout
201 163 QComboBox* animationComboBox = new ComboBox(this);
202 164 animationComboBox->addItem("No Animations", QChart::NoAnimation);
203 165 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
204 166 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
205 167 animationComboBox->addItem("All Animations", QChart::AllAnimations);
206 168 return animationComboBox;
207 169 }
208 170
209 171 QComboBox* Window::createLegendBox()
210 172 {
211 173 QComboBox* legendComboBox = new ComboBox(this);
212 174 legendComboBox->addItem("No Legend ", 0);
213 175 legendComboBox->addItem("Legend Top", Qt::AlignTop);
214 176 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
215 177 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
216 178 legendComboBox->addItem("Legend Right", Qt::AlignRight);
217 179 return legendComboBox;
218 180 }
219 181
220 182 void Window::updateUI()
221 183 {
222 184 bool opengl = m_openGLCheckBox->isChecked();
223 185 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
224 186 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
225 187 m_view->deleteLater();
226 188 m_view = new View(m_scene, m_form);
227 189 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
228 190 setCentralWidget(m_view);
229 191 }
230 192
231 193 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
232 194 m_themeComboBox->currentIndex()).toInt();
233 195
234 196 foreach (QChart *chart, m_chartList)
235 197 chart->setTheme(theme);
236 198
237 199 QPalette pal = window()->palette();
238 200 if (theme == QChart::ChartThemeLight) {
239 201 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
240 202 pal.setColor(QPalette::WindowText, QRgb(0x404044));
241 203 }
242 204 else if (theme == QChart::ChartThemeDark) {
243 205 pal.setColor(QPalette::Window, QRgb(0x121218));
244 206 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
245 207 }
246 208 else if (theme == QChart::ChartThemeBlueCerulean) {
247 209 pal.setColor(QPalette::Window, QRgb(0x40434a));
248 210 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
249 211 }
250 212 else if (theme == QChart::ChartThemeBrownSand) {
251 213 pal.setColor(QPalette::Window, QRgb(0x9e8965));
252 214 pal.setColor(QPalette::WindowText, QRgb(0x404044));
253 215 }
254 216 else if (theme == QChart::ChartThemeBlueNcs) {
255 217 pal.setColor(QPalette::Window, QRgb(0x018bba));
256 218 pal.setColor(QPalette::WindowText, QRgb(0x404044));
257 219 }
258 220 else if (theme == QChart::ChartThemeHighContrast) {
259 221 pal.setColor(QPalette::Window, QRgb(0xffab03));
260 222 pal.setColor(QPalette::WindowText, QRgb(0x181818));
261 223 }
262 224 else if (theme == QChart::ChartThemeBlueIcy) {
263 225 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
264 226 pal.setColor(QPalette::WindowText, QRgb(0x404044));
265 227 }
266 228 else {
267 229 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
268 230 pal.setColor(QPalette::WindowText, QRgb(0x404044));
269 231 }
270 232 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
271 233 widget->setPalette(pal);
272 234 }
273 235 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
274 236 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
275 237
276 238 QChart::AnimationOptions options(
277 239 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
278 240 if (!m_chartList.isEmpty() && m_chartList.at(0)->animationOptions() != options) {
279 241 foreach (QChart *chart, m_chartList)
280 242 chart->setAnimationOptions(options);
281 243 }
282 244
283 245 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
284 246
285 247 if (!alignment) {
286 248 foreach (QChart *chart, m_chartList) {
287 249 chart->legend()->hide();
288 250 }
289 251 }
290 252 else {
291 253 foreach (QChart *chart, m_chartList) {
292 254 chart->legend()->setAlignment(alignment);
293 255 chart->legend()->show();
294 256 }
295 257 }
296 258
297 259 bool antialias = m_antialiasCheckBox->isChecked();
298 260
299 261 if (opengl)
300 262 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
301 263 else
302 264 m_view->setRenderHint(QPainter::Antialiasing, antialias);
303 265
304 266 bool scroll = m_scrollCheckBox->isChecked();
305 267
306 268 if(!m_scroll & scroll){
307 269 m_scroll=true;
308 270 m_zoom=false;
309 271 m_zoomCheckBox->setChecked(false);
310 272 }
311 273
312 274 bool zoom = m_zoomCheckBox->isChecked();
313 275
314 276 if(!m_zoom & zoom){
315 277 m_scroll=false;
316 278 m_zoom=true;
317 279 m_scrollCheckBox->setChecked(false);
318 280 }
319 281
320 282 }
321 283
322 284 void Window::mousePressEvent(QMouseEvent *event)
323 285 {
324 286 if (event->button() == Qt::LeftButton) {
325 287
326 288 m_origin = event->pos();
327 289 m_isScrolling = false;
328 290 m_isZooming = false;
329 291
330 292 foreach (QChart *chart, m_chartList) {
331 293
332 294 QRectF geometryRect = chart->geometry();
333 295 QRectF plotArea = chart->plotArea();
334 296 plotArea.translate(geometryRect.topLeft());
335 297 if (plotArea.contains(m_origin)) {
336 298 m_isScrolling = m_scroll;
337 299 m_isZooming = m_zoom;
338 300 break;
339 301 }
340 302 }
341 303
342 304 if (m_isZooming) {
343 305 m_rubberBand->setRect(QRectF(m_origin, QSize()));
344 306 m_rubberBand->setVisible(true);
345 307 }
346 308 event->accept();
347 309 }
348 310
349 311 if (event->button() == Qt::RightButton) {
350 312 m_origin = event->pos();
351 313 m_isZooming = m_zoom;
352 314 }
353 315 }
354 316
355 317 void Window::mouseMoveEvent(QMouseEvent *event)
356 318 {
357 319 if (m_isScrolling || m_isZooming) {
358 320
359 321 foreach (QChart *chart, m_chartList) {
360 322
361 323 QRectF geometryRect = chart->geometry();
362 324 QRectF plotArea = chart->plotArea();
363 325 plotArea.translate(geometryRect.topLeft());
364 326
365 327 if (plotArea.contains(m_origin)) {
366 328 if (m_isScrolling) {
367 329 QPointF delta = m_origin - event->pos();
368 330 chart->scroll(delta.x(), -delta.y());
369 331 }
370 332 if (m_isZooming && plotArea.contains(event->pos())) {
371 333 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
372 334 }
373 335 break;
374 336 }
375 337 }
376 338 if(m_isScrolling) m_origin = event->pos();
377 339 event->accept();
378 340 }
379 341 }
380 342
381 343 void Window::mouseReleaseEvent(QMouseEvent *event)
382 344 {
383 345 if (event->button() == Qt::LeftButton) {
384 346 m_isScrolling = false;
385 347 if (m_isZooming) {
386 348 m_isZooming = false;
387 349 m_rubberBand->setVisible(false);
388 350
389 351 foreach (QChart *chart, m_chartList) {
390 352
391 353 QRectF geometryRect = chart->geometry();
392 354 QRectF plotArea = chart->plotArea();
393 355 plotArea.translate(geometryRect.topLeft());
394 356
395 357 if (plotArea.contains(m_origin)) {
396 358 QRectF rect = m_rubberBand->rect();
397 359 rect.translate(-geometryRect.topLeft());
398 360 chart->zoomIn(rect);
399 361 break;
400 362 }
401 363 }
402 364 }
403 365 event->accept();
404 366 }
405 367
406 368 if (event->button() == Qt::RightButton) {
407 369
408 370 if (m_isZooming) {
409 371 foreach (QChart *chart, m_chartList) {
410 372
411 373 QRectF geometryRect = chart->geometry();
412 374 QRectF plotArea = chart->plotArea();
413 375 plotArea.translate(geometryRect.topLeft());
414 376
415 377 if (plotArea.contains(m_origin)) {
416 378 QRectF rect = m_rubberBand->rect();
417 379 chart->zoomOut();
418 380 break;
419 381 }
420 382 }
421 383 }
422 384 }
423 385 }
424 386
425 387 void Window::comboBoxFocused(QComboBox *combobox)
426 388 {
427 389 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
428 390 if(widget->widget()==combobox)
429 391 widget->setZValue(2.0);
430 392 else
431 393 widget->setZValue(0.0);
432 394 }
433 395 }
@@ -1,116 +1,110
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 WINDOW_H_
22 22 #define WINDOW_H_
23 #include "model.h"
23 24 #include <QMainWindow>
24 25 #include <QChartGlobal>
25 26 #include <QHash>
26 27 #include <QComboBox>
27 28
28 29 class QCheckBox;
29 30 class QGraphicsRectItem;
30 31 class QGraphicsScene;
31 32 class QGraphicsWidget;
32 33 class View;
33 34
34 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 36 class QChart;
36 37 QTCOMMERCIALCHART_END_NAMESPACE
37 38
38 typedef QPair<QPointF, QString> Data;
39 typedef QList<Data> DataList;
40 typedef QList<DataList> DataTable;
41
42 39 QTCOMMERCIALCHART_USE_NAMESPACE
43 40
44 41
45 42 class Window: public QMainWindow
46 43 {
47 44 Q_OBJECT
48 45 public:
49 46 explicit Window(QWidget *parent = 0);
50 47 ~Window();
51 48
52 49 private Q_SLOTS:
53 50 void updateUI();
54
55
56 51 private:
57 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
58 52 QComboBox* createThemeBox();
59 53 QComboBox* createAnimationBox();
60 54 QComboBox* createLegendBox();
61 55 void connectSignals();
62 56 void createProxyWidgets();
63 57 void comboBoxFocused(QComboBox *combox);
64 58
65 59 protected:
66 60 void mousePressEvent(QMouseEvent *event);
67 61 void mouseMoveEvent(QMouseEvent *event);
68 62 void mouseReleaseEvent(QMouseEvent *event);
69 63
70 64
71 65 private:
72 66 int m_listCount;
73 67 int m_valueMax;
74 68 int m_valueCount;
75 69 QGraphicsScene* m_scene;
76 70 View* m_view;
77 71 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
78 72 QList<QChart*> m_chartList;
79 73 DataTable m_dataTable;
80 74
81 75 QGraphicsWidget *m_form;
82 76 QComboBox *m_themeComboBox;
83 77 QCheckBox *m_antialiasCheckBox;
84 78 QComboBox *m_animatedComboBox;
85 79 QComboBox *m_legendComboBox;
86 80 QCheckBox *m_openGLCheckBox;
87 81 QCheckBox *m_zoomCheckBox;
88 82 QCheckBox *m_scrollCheckBox;
89 83 QPoint m_origin;
90 84 QGraphicsRectItem* m_rubberBand;
91 85
92 86 bool m_isScrolling;
93 87 bool m_isZooming;
94 88 bool m_scroll;
95 89 bool m_zoom;
96 90
97 91 friend class ComboBox;
98 92 };
99 93
100 94 class ComboBox: public QComboBox
101 95 {
102 96 public:
103 97 ComboBox(Window* window,QWidget *parent = 0):QComboBox(parent),m_window(window)
104 98 {}
105 99
106 100 protected:
107 101 void focusInEvent(QFocusEvent *e)
108 102 {
109 103 QComboBox::focusInEvent(e);
110 104 m_window->comboBoxFocused(this);
111 105 }
112 106 private:
113 107 Window* m_window;
114 108 };
115 109
116 110 #endif
General Comments 0
You need to be logged in to leave comments. Login now