@@ -1,331 +1,331 | |||
|
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:BSD$ |
|
10 | 10 | ** You may use this file under the terms of the BSD license as follows: |
|
11 | 11 | ** |
|
12 | 12 | ** "Redistribution and use in source and binary forms, with or without |
|
13 | 13 | ** modification, are permitted provided that the following conditions are |
|
14 | 14 | ** met: |
|
15 | 15 | ** * Redistributions of source code must retain the above copyright |
|
16 | 16 | ** notice, this list of conditions and the following disclaimer. |
|
17 | 17 | ** * Redistributions in binary form must reproduce the above copyright |
|
18 | 18 | ** notice, this list of conditions and the following disclaimer in |
|
19 | 19 | ** the documentation and/or other materials provided with the |
|
20 | 20 | ** distribution. |
|
21 | 21 | ** * Neither the name of Digia nor the names of its contributors |
|
22 | 22 | ** may be used to endorse or promote products derived from this |
|
23 | 23 | ** software without specific prior written permission. |
|
24 | 24 | ** |
|
25 | 25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
26 | 26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
27 | 27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
28 | 28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
29 | 29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
30 | 30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
31 | 31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
32 | 32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
33 | 33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
34 | 34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
35 | 35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
36 | 36 | ** $QT_END_LICENSE$ |
|
37 | 37 | ** |
|
38 | 38 | ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). |
|
39 | 39 | ** |
|
40 | 40 | ****************************************************************************/ |
|
41 | 41 | |
|
42 | 42 | #include "themewidget.h" |
|
43 | 43 | |
|
44 | 44 | #include <QChartView> |
|
45 | 45 | #include <QPieSeries> |
|
46 | 46 | #include <QPieSlice> |
|
47 | 47 | #include <QBarSeries> |
|
48 | 48 | #include <QPercentBarSeries> |
|
49 | 49 | #include <QStackedBarSeries> |
|
50 | 50 | #include <QBarSet> |
|
51 | 51 | #include <QLineSeries> |
|
52 | 52 | #include <QSplineSeries> |
|
53 | 53 | #include <QScatterSeries> |
|
54 | 54 | #include <QAreaSeries> |
|
55 | 55 | #include <QGridLayout> |
|
56 | 56 | #include <QFormLayout> |
|
57 | 57 | #include <QComboBox> |
|
58 | 58 | #include <QSpinBox> |
|
59 | 59 | #include <QCheckBox> |
|
60 | 60 | #include <QGroupBox> |
|
61 | 61 | #include <QLabel> |
|
62 | 62 | #include <QTime> |
|
63 | 63 | |
|
64 | 64 | ThemeWidget::ThemeWidget(QWidget* parent) : |
|
65 | 65 | QWidget(parent), |
|
66 | 66 | m_listCount(3), |
|
67 | 67 | m_valueMax(100), |
|
68 | 68 | m_valueCount(11), |
|
69 | 69 | m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)), |
|
70 | 70 | m_themeComboBox(createThemeBox()), |
|
71 | 71 | m_antialiasCheckBox(new QCheckBox("Anti aliasing")), |
|
72 | 72 | m_animatedComboBox(createAnimationBox()) |
|
73 | 73 | { |
|
74 | 74 | |
|
75 | 75 | connectSignals(); |
|
76 | 76 | // create layout |
|
77 | 77 | QGridLayout* baseLayout = new QGridLayout(); |
|
78 | 78 | QHBoxLayout *settingsLayout = new QHBoxLayout(); |
|
79 | 79 | settingsLayout->addWidget(new QLabel("Theme:")); |
|
80 | 80 | settingsLayout->addWidget(m_themeComboBox); |
|
81 | 81 | settingsLayout->addWidget(new QLabel("Animation:")); |
|
82 | 82 | settingsLayout->addWidget(m_animatedComboBox); |
|
83 | 83 | settingsLayout->addWidget(m_antialiasCheckBox); |
|
84 | 84 | settingsLayout->addStretch(); |
|
85 | 85 | baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); |
|
86 | 86 | |
|
87 | 87 | //create charts |
|
88 | 88 | |
|
89 | 89 | QChartView *chartView; |
|
90 | 90 | |
|
91 | 91 | chartView = new QChartView(createAreaChart()); |
|
92 | 92 | baseLayout->addWidget(chartView, 1, 0); |
|
93 | 93 | m_charts << chartView; |
|
94 | 94 | |
|
95 | 95 | chartView = new QChartView(createBarChart(m_valueCount)); |
|
96 | 96 | baseLayout->addWidget(chartView, 1, 1); |
|
97 | 97 | m_charts << chartView; |
|
98 | 98 | |
|
99 | 99 | chartView = new QChartView(createLineChart()); |
|
100 | 100 | baseLayout->addWidget(chartView, 1, 2); |
|
101 | 101 | m_charts << chartView; |
|
102 | 102 | |
|
103 | 103 | chartView = new QChartView(createPieChart()); |
|
104 | 104 | chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen... |
|
105 | 105 | baseLayout->addWidget(chartView, 2, 0); |
|
106 | 106 | m_charts << chartView; |
|
107 | 107 | |
|
108 | 108 | chartView = new QChartView(createSplineChart()); |
|
109 | 109 | baseLayout->addWidget(chartView, 2, 1); |
|
110 | 110 | m_charts << chartView; |
|
111 | 111 | |
|
112 |
chartView = new QChartView(createS |
|
|
112 | chartView = new QChartView(createScatterChart()); | |
|
113 | 113 | baseLayout->addWidget(chartView, 2, 2); |
|
114 | 114 | m_charts << chartView; |
|
115 | 115 | |
|
116 | 116 | setLayout(baseLayout); |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | ThemeWidget::~ThemeWidget() |
|
120 | 120 | { |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | void ThemeWidget::connectSignals() |
|
124 | 124 | { |
|
125 | 125 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
126 | 126 | connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); |
|
127 | 127 | connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const |
|
131 | 131 | { |
|
132 | 132 | DataTable dataTable; |
|
133 | 133 | |
|
134 | 134 | // set seed for random stuff |
|
135 | 135 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); |
|
136 | 136 | |
|
137 | 137 | // generate random data |
|
138 | 138 | for (int i(0); i < listCount; i++) { |
|
139 | 139 | DataList dataList; |
|
140 | 140 | for (int j(0); j < valueCount; j++) { |
|
141 | 141 | QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax); |
|
142 | 142 | QString label = "Item " + QString::number(i) + ":" + QString::number(j); |
|
143 | 143 | dataList << Data(value, label); |
|
144 | 144 | } |
|
145 | 145 | dataTable << dataList; |
|
146 | 146 | } |
|
147 | 147 | |
|
148 | 148 | return dataTable; |
|
149 | 149 | } |
|
150 | 150 | |
|
151 | 151 | QComboBox* ThemeWidget::createThemeBox() const |
|
152 | 152 | { |
|
153 | 153 | // settings layout |
|
154 | 154 | QComboBox* themeComboBox = new QComboBox(); |
|
155 | 155 | themeComboBox->addItem("Default", QChart::ChartThemeDefault); |
|
156 | 156 | themeComboBox->addItem("Light", QChart::ChartThemeLight); |
|
157 | 157 | themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); |
|
158 | 158 | themeComboBox->addItem("Dark", QChart::ChartThemeDark); |
|
159 | 159 | themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); |
|
160 | 160 | themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); |
|
161 | 161 | themeComboBox->addItem("Icy", QChart::ChartThemeIcy); |
|
162 | 162 | themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); |
|
163 | 163 | return themeComboBox; |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | QComboBox* ThemeWidget::createAnimationBox() const |
|
167 | 167 | { |
|
168 | 168 | // settings layout |
|
169 | 169 | QComboBox* animationComboBox = new QComboBox(); |
|
170 | 170 | animationComboBox->addItem("No Animations", QChart::NoAnimation); |
|
171 | 171 | animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); |
|
172 | 172 | animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); |
|
173 | 173 | animationComboBox->addItem("All Animations", QChart::AllAnimations); |
|
174 | 174 | return animationComboBox; |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | QChart* ThemeWidget::createAreaChart() const |
|
178 | 178 | { |
|
179 | 179 | // area chart |
|
180 | 180 | QChart *chart = new QChart(); |
|
181 | 181 | chart->setTitle("Area chart"); |
|
182 | 182 | { |
|
183 | 183 | for (int i(0); i < m_dataTable.count(); i++) { |
|
184 | 184 | QLineSeries *series1 = new QLineSeries(chart); |
|
185 | 185 | QLineSeries *series2 = new QLineSeries(chart); |
|
186 | 186 | foreach (Data data, m_dataTable[i]) { |
|
187 | 187 | series1->add(data.first); |
|
188 | 188 | series2->add(QPointF(data.first.x(), 0.0)); |
|
189 | 189 | } |
|
190 | 190 | QAreaSeries *area = new QAreaSeries(series1, series2); |
|
191 | 191 | chart->addSeries(area); |
|
192 | 192 | } |
|
193 | 193 | } |
|
194 | 194 | return chart; |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | QChart* ThemeWidget::createBarChart(int valueCount) const |
|
198 | 198 | { |
|
199 | 199 | // bar chart |
|
200 | 200 | QChart* chart = new QChart(); |
|
201 | 201 | chart->setTitle("Bar chart"); |
|
202 | 202 | { |
|
203 | 203 | QBarCategories categories; |
|
204 | 204 | // TODO: categories |
|
205 | 205 | for (int i(0); i < valueCount; i++) |
|
206 | 206 | categories << QString::number(i); |
|
207 | 207 | // QBarSeries* series = new QBarSeries(categories, chart); |
|
208 | 208 | // QPercentBarSeries* series = new QPercentBarSeries(categories, chart); |
|
209 | 209 | QStackedBarSeries* series = new QStackedBarSeries(categories, chart); |
|
210 | 210 | for (int i(0); i < m_dataTable.count(); i++) { |
|
211 | 211 | QBarSet *set = new QBarSet("Set" + QString::number(i)); |
|
212 | 212 | foreach (Data data, m_dataTable[i]) |
|
213 | 213 | *set << data.first.y(); |
|
214 | 214 | series->addBarSet(set); |
|
215 | 215 | } |
|
216 | 216 | chart->addSeries(series); |
|
217 | 217 | } |
|
218 | 218 | return chart; |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | 221 | QChart* ThemeWidget::createLineChart() const |
|
222 | 222 | { |
|
223 | 223 | // line chart |
|
224 | 224 | QChart* chart = new QChart(); |
|
225 | 225 | chart->setTitle("Line chart"); |
|
226 | 226 | foreach (DataList list, m_dataTable) { |
|
227 | 227 | QLineSeries *series = new QLineSeries(chart); |
|
228 | 228 | foreach (Data data, list) |
|
229 | 229 | series->add(data.first); |
|
230 | 230 | chart->addSeries(series); |
|
231 | 231 | } |
|
232 | 232 | return chart; |
|
233 | 233 | } |
|
234 | 234 | |
|
235 | 235 | QChart* ThemeWidget::createPieChart() const |
|
236 | 236 | { |
|
237 | 237 | // pie chart |
|
238 | 238 | QChart* chart = new QChart(); |
|
239 | 239 | chart->setTitle("Pie chart"); |
|
240 | 240 | qreal pieSize = 1.0 / m_dataTable.count(); |
|
241 | 241 | for (int i = 0; i < m_dataTable.count(); i++) { |
|
242 | 242 | QPieSeries *series = new QPieSeries(chart); |
|
243 | 243 | foreach (Data data, m_dataTable[i]) { |
|
244 | 244 | QPieSlice *slice = series->add(data.first.y(), data.second); |
|
245 | 245 | if (data == m_dataTable[i].first()) { |
|
246 | 246 | slice->setLabelVisible(); |
|
247 | 247 | slice->setExploded(); |
|
248 | 248 | } |
|
249 | 249 | } |
|
250 | 250 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); |
|
251 | 251 | series->setPieSize(pieSize); |
|
252 | 252 | series->setPiePosition(hPos, 0.5); |
|
253 | 253 | chart->addSeries(series); |
|
254 | 254 | } |
|
255 | 255 | |
|
256 | 256 | return chart; |
|
257 | 257 | } |
|
258 | 258 | |
|
259 | 259 | QChart* ThemeWidget::createSplineChart() const |
|
260 | 260 | { // spine chart |
|
261 | 261 | QChart* chart = new QChart(); |
|
262 | 262 | chart->setTitle("Spline chart"); |
|
263 | 263 | foreach (DataList list, m_dataTable) { |
|
264 | 264 | QSplineSeries *series = new QSplineSeries(chart); |
|
265 | 265 | foreach (Data data, list) |
|
266 | 266 | series->add(data.first); |
|
267 | 267 | chart->addSeries(series); |
|
268 | 268 | } |
|
269 | 269 | return chart; |
|
270 | 270 | } |
|
271 | 271 | |
|
272 | 272 | QChart* ThemeWidget::createScatterChart() const |
|
273 | 273 | { // scatter chart |
|
274 | 274 | QChart* chart = new QChart(); |
|
275 | 275 | chart->setTitle("Scatter chart"); |
|
276 | 276 | foreach (DataList list, m_dataTable) { |
|
277 | 277 | QScatterSeries *series = new QScatterSeries(chart); |
|
278 | 278 | foreach (Data data, list) |
|
279 | 279 | series->add(data.first); |
|
280 | 280 | chart->addSeries(series); |
|
281 | 281 | } |
|
282 | 282 | return chart; |
|
283 | 283 | } |
|
284 | 284 | |
|
285 | 285 | void ThemeWidget::updateUI() |
|
286 | 286 | { |
|
287 | 287 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); |
|
288 | 288 | |
|
289 | 289 | if (m_charts.at(0)->chart()->theme() != theme) { |
|
290 | 290 | foreach (QChartView *chartView, m_charts) |
|
291 | 291 | chartView->chart()->setTheme(theme); |
|
292 | 292 | |
|
293 | 293 | QPalette pal = window()->palette(); |
|
294 | 294 | if (theme == QChart::ChartThemeLight) { |
|
295 | 295 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
296 | 296 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
297 | 297 | } |
|
298 | 298 | else if (theme == QChart::ChartThemeDark) { |
|
299 | 299 | pal.setColor(QPalette::Window, QRgb(0x121218)); |
|
300 | 300 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
301 | 301 | } |
|
302 | 302 | else if (theme == QChart::ChartThemeBlueCerulean) { |
|
303 | 303 | pal.setColor(QPalette::Window, QRgb(0x40434a)); |
|
304 | 304 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
305 | 305 | } |
|
306 | 306 | else if (theme == QChart::ChartThemeBrownSand) { |
|
307 | 307 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); |
|
308 | 308 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
309 | 309 | } |
|
310 | 310 | else if (theme == QChart::ChartThemeBlueNcs) { |
|
311 | 311 | pal.setColor(QPalette::Window, QRgb(0x018bba)); |
|
312 | 312 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
313 | 313 | } |
|
314 | 314 | else { |
|
315 | 315 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
316 | 316 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
317 | 317 | } |
|
318 | 318 | window()->setPalette(pal); |
|
319 | 319 | } |
|
320 | 320 | |
|
321 | 321 | bool checked = m_antialiasCheckBox->isChecked(); |
|
322 | 322 | foreach (QChartView *chart, m_charts) |
|
323 | 323 | chart->setRenderHint(QPainter::Antialiasing, checked); |
|
324 | 324 | |
|
325 | 325 | QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); |
|
326 | 326 | if (m_charts.at(0)->chart()->animationOptions() != options) { |
|
327 | 327 | foreach (QChartView *chartView, m_charts) |
|
328 | 328 | chartView->chart()->setAnimationOptions(options); |
|
329 | 329 | } |
|
330 | 330 | } |
|
331 | 331 |
General Comments 0
You need to be logged in to leave comments.
Login now