@@ -1,345 +1,348 | |||
|
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 "mainwidget.h" |
|
22 | 22 | #include "dataseriedialog.h" |
|
23 | 23 | #include "qchartview.h" |
|
24 | 24 | #include "qpieseries.h" |
|
25 | 25 | #include "qscatterseries.h" |
|
26 | 26 | #include "qlineseries.h" |
|
27 | 27 | #include <qareaseries.h> |
|
28 | 28 | #include <qsplineseries.h> |
|
29 | 29 | #include <qbarset.h> |
|
30 | 30 | #include <qbarseries.h> |
|
31 | 31 | #include <qstackedbarseries.h> |
|
32 | 32 | #include <qpercentbarseries.h> |
|
33 | 33 | #include <QPushButton> |
|
34 | 34 | #include <QComboBox> |
|
35 | 35 | #include <QSpinBox> |
|
36 | 36 | #include <QCheckBox> |
|
37 | 37 | #include <QGridLayout> |
|
38 | 38 | #include <QHBoxLayout> |
|
39 | 39 | #include <QLabel> |
|
40 | 40 | #include <QSpacerItem> |
|
41 | 41 | #include <QMessageBox> |
|
42 | 42 | #include <cmath> |
|
43 | 43 | #include <QDebug> |
|
44 | 44 | #include <QStandardItemModel> |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
48 | 48 | |
|
49 | 49 | MainWidget::MainWidget(QWidget *parent) : |
|
50 | 50 | QWidget(parent), |
|
51 | 51 | m_addSerieDialog(0), |
|
52 | 52 | m_chart(0) |
|
53 | 53 | { |
|
54 | 54 | m_chart = new QChart(); |
|
55 | 55 | |
|
56 | 56 | // Grid layout for the controls for configuring the chart widget |
|
57 | 57 | QGridLayout *grid = new QGridLayout(); |
|
58 | 58 | QPushButton *addSeriesButton = new QPushButton("Add series"); |
|
59 | 59 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
60 | 60 | grid->addWidget(addSeriesButton, 0, 1); |
|
61 | 61 | initBackroundCombo(grid); |
|
62 | 62 | initScaleControls(grid); |
|
63 | 63 | initThemeCombo(grid); |
|
64 | 64 | initCheckboxes(grid); |
|
65 | 65 | |
|
66 | 66 | // add row with empty label to make all the other rows static |
|
67 | 67 | grid->addWidget(new QLabel(""), grid->rowCount(), 0); |
|
68 | 68 | grid->setRowStretch(grid->rowCount() - 1, 1); |
|
69 | 69 | |
|
70 | 70 | // Create chart view with the chart |
|
71 | 71 | m_chartView = new QChartView(m_chart, this); |
|
72 | 72 | m_chartView->setRubberBand(QChartView::HorizonalRubberBand); |
|
73 | 73 | |
|
74 | 74 | // Another grid layout as a main layout |
|
75 | 75 | QGridLayout *mainLayout = new QGridLayout(); |
|
76 | 76 | mainLayout->addLayout(grid, 0, 0); |
|
77 | 77 | mainLayout->addWidget(m_chartView, 0, 1, 3, 1); |
|
78 | 78 | setLayout(mainLayout); |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | // Combo box for selecting the chart's background |
|
82 | 82 | void MainWidget::initBackroundCombo(QGridLayout *grid) |
|
83 | 83 | { |
|
84 | 84 | QComboBox *backgroundCombo = new QComboBox(this); |
|
85 | 85 | backgroundCombo->addItem("Color"); |
|
86 | 86 | backgroundCombo->addItem("Gradient"); |
|
87 | 87 | backgroundCombo->addItem("Image"); |
|
88 | 88 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
89 | 89 | this, SLOT(backgroundChanged(int))); |
|
90 | 90 | |
|
91 | 91 | grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0); |
|
92 | 92 | grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | // Scale related controls (auto-scale vs. manual min-max values) |
|
96 | 96 | void MainWidget::initScaleControls(QGridLayout *grid) |
|
97 | 97 | { |
|
98 | 98 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
99 | 99 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
100 | 100 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
101 | 101 | m_xMinSpin = new QSpinBox(); |
|
102 | 102 | m_xMinSpin->setMinimum(INT_MIN); |
|
103 | 103 | m_xMinSpin->setMaximum(INT_MAX); |
|
104 | 104 | m_xMinSpin->setValue(0); |
|
105 | 105 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
106 | 106 | m_xMaxSpin = new QSpinBox(); |
|
107 | 107 | m_xMaxSpin->setMinimum(INT_MIN); |
|
108 | 108 | m_xMaxSpin->setMaximum(INT_MAX); |
|
109 | 109 | m_xMaxSpin->setValue(10); |
|
110 | 110 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
111 | 111 | m_yMinSpin = new QSpinBox(); |
|
112 | 112 | m_yMinSpin->setMinimum(INT_MIN); |
|
113 | 113 | m_yMinSpin->setMaximum(INT_MAX); |
|
114 | 114 | m_yMinSpin->setValue(0); |
|
115 | 115 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
116 | 116 | m_yMaxSpin = new QSpinBox(); |
|
117 | 117 | m_yMaxSpin->setMinimum(INT_MIN); |
|
118 | 118 | m_yMaxSpin->setMaximum(INT_MAX); |
|
119 | 119 | m_yMaxSpin->setValue(10); |
|
120 | 120 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
121 | 121 | |
|
122 | 122 | grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0); |
|
123 | 123 | grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0); |
|
124 | 124 | grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1); |
|
125 | 125 | grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0); |
|
126 | 126 | grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1); |
|
127 | 127 | grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0); |
|
128 | 128 | grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1); |
|
129 | 129 | grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0); |
|
130 | 130 | grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1); |
|
131 | 131 | |
|
132 | 132 | m_autoScaleCheck->setChecked(true); |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | // Combo box for selecting theme |
|
136 | 136 | void MainWidget::initThemeCombo(QGridLayout *grid) |
|
137 | 137 | { |
|
138 | 138 | QComboBox *chartTheme = new QComboBox(); |
|
139 | 139 | chartTheme->addItem("Default"); |
|
140 | 140 | chartTheme->addItem("Light"); |
|
141 | 141 | chartTheme->addItem("Blue Cerulean"); |
|
142 | 142 | chartTheme->addItem("Dark"); |
|
143 | 143 | chartTheme->addItem("Brown Sand"); |
|
144 | 144 | chartTheme->addItem("Blue NCS"); |
|
145 | 145 | chartTheme->addItem("High Contrast"); |
|
146 | 146 | chartTheme->addItem("Blue Icy"); |
|
147 | 147 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
148 | 148 | this, SLOT(changeChartTheme(int))); |
|
149 | 149 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); |
|
150 | 150 | grid->addWidget(chartTheme, 8, 1); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | // Different check boxes for customizing chart |
|
154 | 154 | void MainWidget::initCheckboxes(QGridLayout *grid) |
|
155 | 155 | { |
|
156 | 156 | // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off |
|
157 | 157 | QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); |
|
158 | 158 | // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool))); |
|
159 | 159 | zoomCheckBox->setChecked(true); |
|
160 | 160 | grid->addWidget(zoomCheckBox, grid->rowCount(), 0); |
|
161 | 161 | |
|
162 | 162 | QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias"); |
|
163 | 163 | connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool))); |
|
164 | 164 | aliasCheckBox->setChecked(false); |
|
165 | 165 | grid->addWidget(aliasCheckBox, grid->rowCount(), 0); |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | void MainWidget::antiAliasToggled(bool enabled) |
|
169 | 169 | { |
|
170 | 170 | m_chartView->setRenderHint(QPainter::Antialiasing, enabled); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | void MainWidget::addSeries() |
|
174 | 174 | { |
|
175 | 175 | if (!m_addSerieDialog) { |
|
176 | 176 | m_addSerieDialog = new DataSerieDialog(this); |
|
177 | 177 | connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)), |
|
178 | 178 | this, SLOT(addSeries(QString, int, int, QString, bool))); |
|
179 | 179 | } |
|
180 | 180 | m_addSerieDialog->exec(); |
|
181 | 181 | } |
|
182 | 182 | |
|
183 | 183 | QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics) |
|
184 | 184 | { |
|
185 | 185 | // TODO: dataCharacteristics |
|
186 | 186 | QList<RealList> testData; |
|
187 | 187 | for (int j(0); j < columnCount; j++) { |
|
188 | 188 | QList <qreal> newColumn; |
|
189 | 189 | for (int i(0); i < rowCount; i++) { |
|
190 | 190 | if (dataCharacteristics == "Sin") { |
|
191 | 191 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100)); |
|
192 | 192 | } else if (dataCharacteristics == "Sin + random") { |
|
193 | 193 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); |
|
194 | 194 | } else if (dataCharacteristics == "Random") { |
|
195 | 195 | newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX); |
|
196 | 196 | } else if (dataCharacteristics == "Linear") { |
|
197 | 197 | //newColumn.append(i * (j + 1.0)); |
|
198 | 198 | // TODO: temporary hack to make pie work; prevent zero values: |
|
199 | 199 | newColumn.append(i * (j + 1.0) + 0.1); |
|
200 | 200 | } else { // "constant" |
|
201 | 201 | newColumn.append((j + 1.0)); |
|
202 | 202 | } |
|
203 | 203 | } |
|
204 | 204 | testData.append(newColumn); |
|
205 | 205 | } |
|
206 | 206 | return testData; |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | QStringList MainWidget::generateLabels(int count) |
|
210 | 210 | { |
|
211 | 211 | QStringList result; |
|
212 | 212 | for (int i(0); i < count; i++) |
|
213 | 213 | result.append("label" + QString::number(i)); |
|
214 | 214 | return result; |
|
215 | 215 | } |
|
216 | 216 | |
|
217 | 217 | void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled) |
|
218 | 218 | { |
|
219 | 219 | qDebug() << "addSeries: " << seriesName |
|
220 | 220 | << " columnCount: " << columnCount |
|
221 | 221 | << " rowCount: " << rowCount |
|
222 | 222 | << " dataCharacteristics: " << dataCharacteristics |
|
223 | 223 | << " labels enabled: " << labelsEnabled; |
|
224 | 224 | m_defaultSeriesName = seriesName; |
|
225 | 225 | |
|
226 | 226 | QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics); |
|
227 | 227 | |
|
228 | 228 | // Line series and scatter series use similar data |
|
229 | 229 | if (seriesName == "Line") { |
|
230 | 230 | for (int j(0); j < data.count(); j ++) { |
|
231 | 231 | QList<qreal> column = data.at(j); |
|
232 | 232 | QLineSeries *series = new QLineSeries(); |
|
233 | series->setName("line" + QString::number(j)); | |
|
233 | 234 | for (int i(0); i < column.count(); i++) |
|
234 | 235 | series->append(i, column.at(i)); |
|
235 | 236 | m_chart->addSeries(series); |
|
236 | 237 | } |
|
237 | 238 | } else if (seriesName == "Area") { |
|
238 | 239 | // TODO: lower series for the area? |
|
239 | 240 | for (int j(0); j < data.count(); j ++) { |
|
240 | 241 | QList<qreal> column = data.at(j); |
|
241 | 242 | QLineSeries *lineSeries = new QLineSeries(); |
|
242 | 243 | for (int i(0); i < column.count(); i++) |
|
243 | 244 | lineSeries->append(i, column.at(i)); |
|
244 | 245 | QAreaSeries *areaSeries = new QAreaSeries(lineSeries); |
|
246 | areaSeries->setName("area" + QString::number(j)); | |
|
245 | 247 | m_chart->addSeries(areaSeries); |
|
246 | 248 | } |
|
247 | 249 | } else if (seriesName == "Scatter") { |
|
248 | 250 | for (int j(0); j < data.count(); j++) { |
|
249 | 251 | QList<qreal> column = data.at(j); |
|
250 | 252 | QScatterSeries *series = new QScatterSeries(); |
|
253 | series->setName("scatter" + QString::number(j)); | |
|
251 | 254 | for (int i(0); i < column.count(); i++) |
|
252 | 255 | series->append(i, column.at(i)); |
|
253 | 256 | m_chart->addSeries(series); |
|
254 | 257 | } |
|
255 | 258 | } else if (seriesName == "Pie") { |
|
256 | 259 | QStringList labels = generateLabels(rowCount); |
|
257 | 260 | for (int j(0); j < data.count(); j++) { |
|
258 | 261 | QPieSeries *series = new QPieSeries(); |
|
259 | 262 | QList<qreal> column = data.at(j); |
|
260 | 263 | for (int i(0); i < column.count(); i++) |
|
261 | 264 | series->append(column.at(i), labels.at(i)); |
|
262 | 265 | m_chart->addSeries(series); |
|
263 | 266 | } |
|
264 | 267 | } else if (seriesName == "Bar" |
|
265 | 268 | || seriesName == "Stacked bar" |
|
266 | 269 | || seriesName == "Percent bar") { |
|
267 | 270 | QStringList category; |
|
268 | 271 | QStringList labels = generateLabels(rowCount); |
|
269 | 272 | foreach(QString label, labels) |
|
270 | 273 | category << label; |
|
271 | 274 | QBarSeries* series = 0; |
|
272 | 275 | if (seriesName == "Bar") |
|
273 | 276 | series = new QBarSeries(category, this); |
|
274 | 277 | else if (seriesName == "Stacked bar") |
|
275 | 278 | series = new QStackedBarSeries(category, this); |
|
276 | 279 | else |
|
277 | 280 | series = new QPercentBarSeries(category, this); |
|
278 | 281 | |
|
279 | 282 | for (int j(0); j < data.count(); j++) { |
|
280 | 283 | QList<qreal> column = data.at(j); |
|
281 | 284 | QBarSet *set = new QBarSet("set" + QString::number(j)); |
|
282 | 285 | for (int i(0); i < column.count(); i++) |
|
283 | 286 | *set << column.at(i); |
|
284 | 287 | series->appendBarSet(set); |
|
285 | 288 | } |
|
286 | 289 | |
|
287 | 290 | // TODO: new implementation of setFloatingValuesEnabled with signals |
|
288 | 291 | //series->setFloatingValuesEnabled(true); |
|
289 | 292 | series->setToolTipEnabled(true); |
|
290 | 293 | m_chart->addSeries(series); |
|
291 | 294 | } else if (seriesName == "Spline") { |
|
292 | 295 | for (int j(0); j < data.count(); j ++) { |
|
293 | 296 | QList<qreal> column = data.at(j); |
|
294 | 297 | QSplineSeries *series = new QSplineSeries(); |
|
295 | 298 | for (int i(0); i < column.count(); i++) |
|
296 | 299 | series->append(i, column.at(i)); |
|
297 | 300 | m_chart->addSeries(series); |
|
298 | 301 | } |
|
299 | 302 | } |
|
300 | 303 | } |
|
301 | 304 | |
|
302 | 305 | void MainWidget::backgroundChanged(int itemIndex) |
|
303 | 306 | { |
|
304 | 307 | qDebug() << "backgroundChanged: " << itemIndex; |
|
305 | 308 | } |
|
306 | 309 | |
|
307 | 310 | void MainWidget::autoScaleChanged(int value) |
|
308 | 311 | { |
|
309 | 312 | if (value) { |
|
310 | 313 | // TODO: enable auto scaling |
|
311 | 314 | } else { |
|
312 | 315 | // TODO: set scaling manually (and disable auto scaling) |
|
313 | 316 | } |
|
314 | 317 | |
|
315 | 318 | m_xMinSpin->setEnabled(!value); |
|
316 | 319 | m_xMaxSpin->setEnabled(!value); |
|
317 | 320 | m_yMinSpin->setEnabled(!value); |
|
318 | 321 | m_yMaxSpin->setEnabled(!value); |
|
319 | 322 | } |
|
320 | 323 | |
|
321 | 324 | void MainWidget::xMinChanged(int value) |
|
322 | 325 | { |
|
323 | 326 | qDebug() << "xMinChanged: " << value; |
|
324 | 327 | } |
|
325 | 328 | |
|
326 | 329 | void MainWidget::xMaxChanged(int value) |
|
327 | 330 | { |
|
328 | 331 | qDebug() << "xMaxChanged: " << value; |
|
329 | 332 | } |
|
330 | 333 | |
|
331 | 334 | void MainWidget::yMinChanged(int value) |
|
332 | 335 | { |
|
333 | 336 | qDebug() << "yMinChanged: " << value; |
|
334 | 337 | } |
|
335 | 338 | |
|
336 | 339 | void MainWidget::yMaxChanged(int value) |
|
337 | 340 | { |
|
338 | 341 | qDebug() << "yMaxChanged: " << value; |
|
339 | 342 | } |
|
340 | 343 | |
|
341 | 344 | void MainWidget::changeChartTheme(int themeIndex) |
|
342 | 345 | { |
|
343 | 346 | qDebug() << "changeChartTheme: " << themeIndex; |
|
344 | 347 | m_chart->setTheme((QChart::ChartTheme) themeIndex); |
|
345 | 348 | } |
General Comments 0
You need to be logged in to leave comments.
Login now