##// END OF EJS Templates
Fix compilation with GCC 6...
Marc Mutz -
r2872:9c63b5bda170
parent child
Show More
@@ -1,380 +1,380
1 1 /******************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd.
4 4 ** Contact: http://www.qt.io/licensing/
5 5 **
6 6 ** This file is part of the Qt Charts module.
7 7 **
8 8 ** $QT_BEGIN_LICENSE:COMM$
9 9 **
10 10 ** Commercial License Usage
11 11 ** Licensees holding valid commercial Qt licenses may use this file in
12 12 ** accordance with the commercial license agreement provided with the
13 13 ** Software or, alternatively, in accordance with the terms contained in
14 14 ** a written agreement between you and The Qt Company. For licensing terms
15 15 ** and conditions see http://www.qt.io/terms-conditions. For further
16 16 ** information use the contact form at http://www.qt.io/contact-us.
17 17 **
18 18 ** $QT_END_LICENSE$
19 19 **
20 20 ******************************************************************************/
21 21
22 22 #include "mainwidget.h"
23 23 #include "dataseriedialog.h"
24 24 #include <QtCharts/QChartView>
25 25 #include <QtCharts/QPieSeries>
26 26 #include <QtCharts/QScatterSeries>
27 27 #include <QtCharts/QLineSeries>
28 28 #include <QtCharts/QAreaSeries>
29 29 #include <QtCharts/QSplineSeries>
30 30 #include <QtCharts/QBarSet>
31 31 #include <QtCharts/QBarSeries>
32 32 #include <QtCharts/QStackedBarSeries>
33 33 #include <QtCharts/QPercentBarSeries>
34 34 #include <QtWidgets/QPushButton>
35 35 #include <QtWidgets/QComboBox>
36 36 #include <QtWidgets/QSpinBox>
37 37 #include <QtWidgets/QCheckBox>
38 38 #include <QtWidgets/QGridLayout>
39 39 #include <QtWidgets/QHBoxLayout>
40 40 #include <QtWidgets/QLabel>
41 41 #include <QtWidgets/QSpacerItem>
42 42 #include <QtWidgets/QMessageBox>
43 43 #include <cmath>
44 44 #include <QtCore/QDebug>
45 45 #include <QtGui/QStandardItemModel>
46 46 #include <QtCharts/QBarCategoryAxis>
47 47 #include <QtWidgets/QOpenGLWidget>
48 48
49 49 QT_CHARTS_USE_NAMESPACE
50 50
51 51 MainWidget::MainWidget(QWidget *parent) :
52 52 QWidget(parent),
53 53 m_addSerieDialog(0),
54 54 m_chart(0)
55 55 {
56 56 m_chart = new QChart();
57 57
58 58 // Grid layout for the controls for configuring the chart widget
59 59 QGridLayout *grid = new QGridLayout();
60 60 QPushButton *addSeriesButton = new QPushButton("Add series");
61 61 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
62 62 grid->addWidget(addSeriesButton, 0, 1);
63 63 initBackroundCombo(grid);
64 64 initScaleControls(grid);
65 65 initThemeCombo(grid);
66 66 initCheckboxes(grid);
67 67
68 68 // add row with empty label to make all the other rows static
69 69 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
70 70 grid->setRowStretch(grid->rowCount() - 1, 1);
71 71
72 72 // Create chart view with the chart
73 73 m_chartView = new QChartView(m_chart, this);
74 74 m_chartView->setRubberBand(QChartView::HorizontalRubberBand);
75 75
76 76 // Another grid layout as a main layout
77 77 QGridLayout *mainLayout = new QGridLayout();
78 78 mainLayout->addLayout(grid, 0, 0);
79 79 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
80 80 setLayout(mainLayout);
81 81 }
82 82
83 83 // Combo box for selecting the chart's background
84 84 void MainWidget::initBackroundCombo(QGridLayout *grid)
85 85 {
86 86 QComboBox *backgroundCombo = new QComboBox(this);
87 87 backgroundCombo->addItem("Color");
88 88 backgroundCombo->addItem("Gradient");
89 89 backgroundCombo->addItem("Image");
90 90 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
91 91 this, SLOT(backgroundChanged(int)));
92 92
93 93 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
94 94 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
95 95 }
96 96
97 97 // Scale related controls (auto-scale vs. manual min-max values)
98 98 void MainWidget::initScaleControls(QGridLayout *grid)
99 99 {
100 100 m_autoScaleCheck = new QCheckBox("Automatic scaling");
101 101 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
102 102 // Allow setting also non-sense values (like -2147483648 and 2147483647)
103 103 m_xMinSpin = new QSpinBox();
104 104 m_xMinSpin->setMinimum(INT_MIN);
105 105 m_xMinSpin->setMaximum(INT_MAX);
106 106 m_xMinSpin->setValue(0);
107 107 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
108 108 m_xMaxSpin = new QSpinBox();
109 109 m_xMaxSpin->setMinimum(INT_MIN);
110 110 m_xMaxSpin->setMaximum(INT_MAX);
111 111 m_xMaxSpin->setValue(10);
112 112 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
113 113 m_yMinSpin = new QSpinBox();
114 114 m_yMinSpin->setMinimum(INT_MIN);
115 115 m_yMinSpin->setMaximum(INT_MAX);
116 116 m_yMinSpin->setValue(0);
117 117 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
118 118 m_yMaxSpin = new QSpinBox();
119 119 m_yMaxSpin->setMinimum(INT_MIN);
120 120 m_yMaxSpin->setMaximum(INT_MAX);
121 121 m_yMaxSpin->setValue(10);
122 122 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
123 123
124 124 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
125 125 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
126 126 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
127 127 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
128 128 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
129 129 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
130 130 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
131 131 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
132 132 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
133 133
134 134 m_autoScaleCheck->setChecked(true);
135 135 }
136 136
137 137 // Combo box for selecting theme
138 138 void MainWidget::initThemeCombo(QGridLayout *grid)
139 139 {
140 140 QComboBox *chartTheme = new QComboBox();
141 141 chartTheme->addItem("Default");
142 142 chartTheme->addItem("Light");
143 143 chartTheme->addItem("Blue Cerulean");
144 144 chartTheme->addItem("Dark");
145 145 chartTheme->addItem("Brown Sand");
146 146 chartTheme->addItem("Blue NCS");
147 147 chartTheme->addItem("High Contrast");
148 148 chartTheme->addItem("Blue Icy");
149 149 chartTheme->addItem("Qt");
150 150 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
151 151 this, SLOT(changeChartTheme(int)));
152 152 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
153 153 grid->addWidget(chartTheme, 8, 1);
154 154 }
155 155
156 156 // Different check boxes for customizing chart
157 157 void MainWidget::initCheckboxes(QGridLayout *grid)
158 158 {
159 159 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
160 160 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
161 161 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
162 162 zoomCheckBox->setChecked(true);
163 163 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
164 164
165 165 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
166 166 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
167 167 aliasCheckBox->setChecked(false);
168 168 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
169 169
170 170 QCheckBox *openGLCheckBox = new QCheckBox("Use QOpenGLWidget");
171 171 connect(openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(openGLToggled(bool)));
172 172 openGLCheckBox->setChecked(false);
173 173 grid->addWidget(openGLCheckBox, grid->rowCount(), 0);
174 174 }
175 175
176 176 void MainWidget::antiAliasToggled(bool enabled)
177 177 {
178 178 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
179 179 }
180 180
181 181 void MainWidget::openGLToggled(bool enabled)
182 182 {
183 183 if (enabled) {
184 184 QSurfaceFormat f = QSurfaceFormat::defaultFormat();
185 185 f.setSamples(4);
186 186 QSurfaceFormat::setDefaultFormat(f);
187 187 QOpenGLWidget *g = new QOpenGLWidget();
188 188 m_chartView->setViewport(g);
189 189 } else {
190 190 m_chartView->setViewport(0);
191 191 }
192 192 }
193 193
194 194 void MainWidget::addSeries()
195 195 {
196 196 if (!m_addSerieDialog) {
197 197 m_addSerieDialog = new DataSerieDialog(this);
198 198 connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)),
199 199 this, SLOT(addSeries(QString,int,int,QString,bool)));
200 200 }
201 201 m_addSerieDialog->exec();
202 202 }
203 203
204 204 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
205 205 {
206 206 QList<RealList> testData;
207 207 for (int j(0); j < columnCount; j++) {
208 208 QList <qreal> newColumn;
209 209 for (int i(0); i < rowCount; i++) {
210 210 if (dataCharacteristics == "Sin") {
211 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
211 newColumn.append(std::abs(sin(3.14159265358979 / 50 * i) * 100));
212 212 } else if (dataCharacteristics == "Sin + random") {
213 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
213 newColumn.append(std::abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
214 214 } else if (dataCharacteristics == "Random") {
215 215 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
216 216 } else if (dataCharacteristics == "Linear") {
217 217 //newColumn.append(i * (j + 1.0));
218 218 // TODO: temporary hack to make pie work; prevent zero values:
219 219 newColumn.append(i * (j + 1.0) + 0.1);
220 220 } else { // "constant"
221 221 newColumn.append((j + 1.0));
222 222 }
223 223 }
224 224 testData.append(newColumn);
225 225 }
226 226 return testData;
227 227 }
228 228
229 229 QStringList MainWidget::generateLabels(int count)
230 230 {
231 231 QStringList result;
232 232 for (int i(0); i < count; i++)
233 233 result.append("label" + QString::number(i));
234 234 return result;
235 235 }
236 236
237 237 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
238 238 {
239 239 qDebug() << "addSeries: " << seriesName
240 240 << " columnCount: " << columnCount
241 241 << " rowCount: " << rowCount
242 242 << " dataCharacteristics: " << dataCharacteristics
243 243 << " labels enabled: " << labelsEnabled;
244 244 m_defaultSeriesName = seriesName;
245 245
246 246 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
247 247
248 248 // Line series and scatter series use similar data
249 249 if (seriesName == "Line") {
250 250 for (int j(0); j < data.count(); j ++) {
251 251 QList<qreal> column = data.at(j);
252 252 QLineSeries *series = new QLineSeries();
253 253 series->setName("line" + QString::number(j));
254 254 for (int i(0); i < column.count(); i++)
255 255 series->append(i, column.at(i));
256 256 m_chart->addSeries(series);
257 257 }
258 258 } else if (seriesName == "Area") {
259 259 // TODO: lower series for the area?
260 260 for (int j(0); j < data.count(); j ++) {
261 261 QList<qreal> column = data.at(j);
262 262 QLineSeries *lineSeries = new QLineSeries();
263 263 for (int i(0); i < column.count(); i++)
264 264 lineSeries->append(i, column.at(i));
265 265 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
266 266 areaSeries->setName("area" + QString::number(j));
267 267 m_chart->addSeries(areaSeries);
268 268 }
269 269 } else if (seriesName == "Scatter") {
270 270 for (int j(0); j < data.count(); j++) {
271 271 QList<qreal> column = data.at(j);
272 272 QScatterSeries *series = new QScatterSeries();
273 273 series->setName("scatter" + QString::number(j));
274 274 for (int i(0); i < column.count(); i++)
275 275 series->append(i, column.at(i));
276 276 m_chart->addSeries(series);
277 277 }
278 278 } else if (seriesName == "Pie") {
279 279 QStringList labels = generateLabels(rowCount);
280 280 for (int j(0); j < data.count(); j++) {
281 281 QPieSeries *series = new QPieSeries();
282 282 QList<qreal> column = data.at(j);
283 283 for (int i(0); i < column.count(); i++)
284 284 series->append(labels.at(i), column.at(i));
285 285 m_chart->addSeries(series);
286 286 }
287 287 } else if (seriesName == "Bar"
288 288 || seriesName == "Stacked bar"
289 289 || seriesName == "Percent bar") {
290 290 QStringList category;
291 291 QStringList labels = generateLabels(rowCount);
292 292 foreach (QString label, labels)
293 293 category << label;
294 294 QAbstractBarSeries* series = 0;
295 295 if (seriesName == "Bar") {
296 296 series = new QBarSeries(this);
297 297 QBarCategoryAxis* axis = new QBarCategoryAxis();
298 298 axis->append(category);
299 299 m_chart->setAxisX(axis,series);
300 300 } else if (seriesName == "Stacked bar") {
301 301 series = new QStackedBarSeries(this);
302 302 QBarCategoryAxis* axis = new QBarCategoryAxis();
303 303 axis->append(category);
304 304 m_chart->setAxisX(axis,series);
305 305 } else {
306 306 series = new QPercentBarSeries(this);
307 307 QBarCategoryAxis* axis = new QBarCategoryAxis();
308 308 axis->append(category);
309 309 m_chart->setAxisX(axis,series);
310 310 }
311 311
312 312 for (int j(0); j < data.count(); j++) {
313 313 QList<qreal> column = data.at(j);
314 314 QBarSet *set = new QBarSet("set" + QString::number(j));
315 315 for (int i(0); i < column.count(); i++)
316 316 *set << column.at(i);
317 317 series->append(set);
318 318 }
319 319
320 320 m_chart->addSeries(series);
321 321 } else if (seriesName == "Spline") {
322 322 for (int j(0); j < data.count(); j ++) {
323 323 QList<qreal> column = data.at(j);
324 324 QSplineSeries *series = new QSplineSeries();
325 325 series->setName("spline" + QString::number(j));
326 326 for (int i(0); i < column.count(); i++)
327 327 series->append(i, column.at(i));
328 328 m_chart->addSeries(series);
329 329 }
330 330 }
331 331 m_chart->createDefaultAxes();
332 332 }
333 333
334 334 void MainWidget::backgroundChanged(int itemIndex)
335 335 {
336 336 qDebug() << "backgroundChanged: " << itemIndex;
337 337 }
338 338
339 339 void MainWidget::autoScaleChanged(int value)
340 340 {
341 341 if (value) {
342 342 // TODO: enable auto scaling
343 343 } else {
344 344 // TODO: set scaling manually (and disable auto scaling)
345 345 }
346 346
347 347 m_xMinSpin->setEnabled(!value);
348 348 m_xMaxSpin->setEnabled(!value);
349 349 m_yMinSpin->setEnabled(!value);
350 350 m_yMaxSpin->setEnabled(!value);
351 351 }
352 352
353 353 void MainWidget::xMinChanged(int value)
354 354 {
355 355 qDebug() << "xMinChanged: " << value;
356 356 }
357 357
358 358 void MainWidget::xMaxChanged(int value)
359 359 {
360 360 qDebug() << "xMaxChanged: " << value;
361 361 }
362 362
363 363 void MainWidget::yMinChanged(int value)
364 364 {
365 365 qDebug() << "yMinChanged: " << value;
366 366 }
367 367
368 368 void MainWidget::yMaxChanged(int value)
369 369 {
370 370 qDebug() << "yMaxChanged: " << value;
371 371 }
372 372
373 373 void MainWidget::changeChartTheme(int themeIndex)
374 374 {
375 375 qDebug() << "changeChartTheme: " << themeIndex;
376 376 if (themeIndex == 0)
377 377 m_chart->setTheme(QChart::ChartThemeLight);
378 378 else
379 379 m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1));
380 380 }
General Comments 0
You need to be logged in to leave comments. Login now