Debian Codegraph @

tests/manual/boxplottester/mainwidget.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "mainwidget.h"
5 #include "customtablemodel.h"
6 #include "pentool.h"
7 #include <QtCharts/QVBoxPlotModelMapper>
8 #include <QtWidgets/QTableView>
9 #include <QtWidgets/QHeaderView>
10 #include <QtCharts/QChartView>
11 #include <QtCharts/QBoxPlotSeries>
12 #include <QtCharts/QBoxSet>
13 #include <QtCharts/QLegend>
14 #include <QtCharts/QBarCategoryAxis>
15 #include <QtGui/QBrush>
16 #include <QtGui/QColor>
17 #include <QtWidgets/QPushButton>
18 #include <QtWidgets/QComboBox>
19 #include <QtWidgets/QSpinBox>
20 #include <QtWidgets/QCheckBox>
21 #include <QtWidgets/QGridLayout>
22 #include <QtWidgets/QHBoxLayout>
23 #include <QtWidgets/QLabel>
24 #include <QtWidgets/QSpacerItem>
25 #include <QtWidgets/QMessageBox>
26 #include <cmath>
27 #include <QtCore/QDebug>
28 #include <QtGui/QStandardItemModel>
29 #include <QtCharts/QBarCategoryAxis>
30 #include <QtCharts/QLogValueAxis>
31
32 QT_USE_NAMESPACE
33
34 static const QString allCategories[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
35 static const int maxCategories = 12;
36
37 MainWidget::MainWidget(QWidget *parent) :
38 QWidget(parent),
39 m_chart(0),
40 m_axis(0),
41 m_rowPos(0),
42 m_seriesCount(0)
43 {
44 m_chart = new QChart();
45
46 m_penTool = new PenTool("Whiskers pen", this);
47
48 // Grid layout for the controls for configuring the chart widget
49 QGridLayout *grid = new QGridLayout();
50
51 // Create add a series button
52 QPushButton *addSeriesButton = new QPushButton("Add a series");
53 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
54 grid->addWidget(addSeriesButton, m_rowPos++, 1);
55
56 // Create remove a series button
57 QPushButton *removeSeriesButton = new QPushButton("Remove a series");
58 connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
59 grid->addWidget(removeSeriesButton, m_rowPos++, 1);
60
61 // Create add a single box button
62 QPushButton *addBoxButton = new QPushButton("Add a box");
63 connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox()));
64 grid->addWidget(addBoxButton, m_rowPos++, 1);
65
66 // Create insert a box button
67 QPushButton *insertBoxButton = new QPushButton("Insert a box");
68 connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox()));
69 grid->addWidget(insertBoxButton, m_rowPos++, 1);
70
71 // Create add a single box button
72 QPushButton *removeBoxButton = new QPushButton("Remove a box");
73 connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox()));
74 grid->addWidget(removeBoxButton, m_rowPos++, 1);
75
76 // Create clear button
77 QPushButton *clearButton = new QPushButton("Clear");
78 connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
79 grid->addWidget(clearButton, m_rowPos++, 1);
80
81 // Create clear button
82 QPushButton *clearBoxButton = new QPushButton("ClearBox");
83 connect(clearBoxButton, SIGNAL(clicked()), this, SLOT(clearBox()));
84 grid->addWidget(clearBoxButton, m_rowPos++, 1);
85
86 // Create set brush button
87 QPushButton *setBrushButton = new QPushButton("Set brush");
88 connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush()));
89 grid->addWidget(setBrushButton, m_rowPos++, 1);
90
91 // Create set whiskers pen button
92 QPushButton *setWhiskersButton = new QPushButton("Whiskers pen");
93 connect(setWhiskersButton, SIGNAL(clicked()), m_penTool, SLOT(show()));
94 connect(m_penTool, SIGNAL(changed()), this, SLOT(changePen()));
95 grid->addWidget(setWhiskersButton, m_rowPos++, 1);
96
97 // Box width setting
98 m_boxWidthSB = new QDoubleSpinBox();
99 m_boxWidthSB->setMinimum(-1.0);
100 m_boxWidthSB->setMaximum(2.0);
101 m_boxWidthSB->setSingleStep(0.1);
102 m_boxWidthSB->setValue(0.5);
103 grid->addWidget(new QLabel("Box width:"), m_rowPos, 0);
104 grid->addWidget(m_boxWidthSB, m_rowPos++, 1);
105 connect(m_boxWidthSB, SIGNAL(valueChanged(double)), this, SLOT(setBoxWidth(double)));
106
107 initThemeCombo(grid);
108 initCheckboxes(grid);
109
110 QTableView *tableView = new QTableView;
111 m_model = new CustomTableModel(tableView);
112 tableView->setModel(m_model);
113 tableView->setMaximumWidth(200);
114 grid->addWidget(tableView, m_rowPos++, 0, 3, 2, Qt::AlignLeft);
115 tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
116 tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
117
118 // add row with empty label to make all the other rows static
119 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
120 grid->setRowStretch(grid->rowCount() - 1, 1);
121
122 // Create chart view with the chart
123 m_chartView = new QChartView(m_chart, this);
124
125 // As a default antialiasing is off
126 m_chartView->setRenderHint(QPainter::Antialiasing, false);
127
128 // Another grid layout as a main layout
129 QGridLayout *mainLayout = new QGridLayout();
130 mainLayout->addLayout(grid, 0, 0);
131 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
132 setLayout(mainLayout);
133
134 legendToggled(false);
135 animationToggled(false);
136 }
137
138 // Combo box for selecting theme
139 void MainWidget::initThemeCombo(QGridLayout *grid)
140 {
141 QComboBox *chartTheme = new QComboBox();
142 chartTheme->addItem("Default");
143 chartTheme->addItem("Light");
144 chartTheme->addItem("Blue Cerulean");
145 chartTheme->addItem("Dark");
146 chartTheme->addItem("Brown Sand");
147 chartTheme->addItem("Blue NCS");
148 chartTheme->addItem("High Contrast");
149 chartTheme->addItem("Blue Icy");
150 chartTheme->addItem("Qt");
151 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
152 this, SLOT(changeChartTheme(int)));
153 grid->addWidget(new QLabel("Chart theme:"), m_rowPos, 0);
154 grid->addWidget(chartTheme, m_rowPos++, 1);
155 }
156
157 // Different check boxes for customizing chart
158 void MainWidget::initCheckboxes(QGridLayout *grid)
159 {
160 QCheckBox *animationCheckBox = new QCheckBox("Animation");
161 connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool)));
162 animationCheckBox->setChecked(false);
163 grid->addWidget(animationCheckBox, m_rowPos++, 0);
164
165 QCheckBox *legendCheckBox = new QCheckBox("Legend");
166 connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool)));
167 legendCheckBox->setChecked(false);
168 grid->addWidget(legendCheckBox, m_rowPos++, 0);
169
170 QCheckBox *titleCheckBox = new QCheckBox("Title");
171 connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool)));
172 titleCheckBox->setChecked(false);
173 grid->addWidget(titleCheckBox, m_rowPos++, 0);
174
175 QCheckBox *antialiasingCheckBox = new QCheckBox("Antialiasing");
176 connect(antialiasingCheckBox, SIGNAL(toggled(bool)), this, SLOT(antialiasingToggled(bool)));
177 antialiasingCheckBox->setChecked(false);
178 grid->addWidget(antialiasingCheckBox, m_rowPos++, 0);
179
180 QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper");
181 connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool)));
182 modelMapperCheckBox->setChecked(false);
183 grid->addWidget(modelMapperCheckBox, m_rowPos++, 0);
184
185 m_boxOutlined = new QCheckBox("Box outlined");
186 connect(m_boxOutlined, SIGNAL(toggled(bool)), this, SLOT(boxOutlineToggled(bool)));
187 m_boxOutlined->setChecked(true);
188 grid->addWidget(m_boxOutlined, m_rowPos++, 0);
189 }
190
191 void MainWidget::updateAxis(int categoryCount)
192 {
193 if (!m_axis) {
194 m_chart->createDefaultAxes();
195 m_axis = new QBarCategoryAxis();
196 }
197 QStringList categories;
198 for (int i = 0; i < categoryCount; i++)
199 categories << allCategories[i];
200 m_axis->setCategories(categories);
201 }
202
203 void MainWidget::addSeries()
204 {
205 qDebug() << "BoxPlotTester::MainWidget::addSeries()";
206
207 if (m_seriesCount > 9)
208 return;
209
210 // Initial data
211 QBoxSet *set0 = new QBoxSet();
212 QBoxSet *set1 = new QBoxSet();
213 QBoxSet *set2 = new QBoxSet();
214 QBoxSet *set3 = new QBoxSet();
215 QBoxSet *set4 = new QBoxSet();
216 QBoxSet *set5 = new QBoxSet();
217
218 // low bot med top upp
219 *set0 << -1 << 2 << 4 << 13 << 15;
220 *set1 << 5 << 6 << 7.5 << 8 << 12;
221 *set2 << 3 << 5 << 5.7 << 8 << 9;
222 *set3 << 5 << 6 << 6.8 << 7 << 8;
223 *set4 << 4 << 5 << 5.2 << 6 << 7;
224 *set5 << 4 << 7 << 8.2 << 9 << 10;
225
226 m_series[m_seriesCount] = new QBoxPlotSeries();
227 m_series[m_seriesCount]->append(set0);
228 m_series[m_seriesCount]->append(set1);
229 m_series[m_seriesCount]->append(set2);
230 m_series[m_seriesCount]->append(set3);
231 m_series[m_seriesCount]->append(set4);
232 m_series[m_seriesCount]->append(set5);
233 m_series[m_seriesCount]->setName("Box & Whiskers");
234
235 connect(m_series[m_seriesCount], SIGNAL(clicked(QBoxSet*)), this, SLOT(boxClicked(QBoxSet*)));
236 connect(m_series[m_seriesCount], SIGNAL(pressed(QBoxSet*)), this, SLOT(boxPressed(QBoxSet*)));
237 connect(m_series[m_seriesCount], SIGNAL(released(QBoxSet*)), this, SLOT(boxReleased(QBoxSet*)));
238 connect(m_series[m_seriesCount], SIGNAL(doubleClicked(QBoxSet*)),
239 this, SLOT(boxDoubleClicked(QBoxSet*)));
240 connect(m_series[m_seriesCount], SIGNAL(hovered(bool,QBoxSet*)),
241 this, SLOT(boxHovered(bool,QBoxSet*)));
242 connect(set1, SIGNAL(clicked()), this, SLOT(singleBoxClicked()));
243 connect(set1, SIGNAL(pressed()), this, SLOT(singleBoxPressed()));
244 connect(set1, SIGNAL(released()), this, SLOT(singleBoxReleased()));
245 connect(set1, SIGNAL(doubleClicked()), this, SLOT(singleBoxDoubleClicked()));
246 connect(set2, SIGNAL(hovered(bool)), this, SLOT(singleBoxHovered(bool)));
247
248 m_series[m_seriesCount]->setBoxOutlineVisible(m_boxOutlined->checkState());
249 m_series[m_seriesCount]->setBoxWidth(m_boxWidthSB->value());
250
251 m_chart->addSeries(m_series[m_seriesCount]);
252
253 updateAxis(m_series[0]->count());
254 m_chart->setAxisX(m_axis, m_series[m_seriesCount]);
255
256 m_seriesCount++;
257 }
258
259 void MainWidget::removeSeries()
260 {
261 qDebug() << "BoxPlotTester::MainWidget::removeSeries()";
262
263 if (m_seriesCount > 0) {
264 m_seriesCount--;
265 m_chart->removeSeries(m_series[m_seriesCount]);
266 delete m_series[m_seriesCount];
267 m_series[m_seriesCount] = 0;
268 } else {
269 qDebug() << "Create a series first";
270 }
271 }
272
273 void MainWidget::addBox()
274 {
275 qDebug() << "BoxPlotTester::MainWidget::addBox()";
276
277 if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) {
278 QBoxSet *newSet = new QBoxSet();
279 newSet->setValue(QBoxSet::LowerExtreme, 5.0);
280 newSet->setValue(QBoxSet::LowerQuartile, 6.0);
281 newSet->setValue(QBoxSet::Median, 6.8);
282 newSet->setValue(QBoxSet::UpperQuartile, 7.0);
283 newSet->setValue(QBoxSet::UpperExtreme, 8.0);
284
285 updateAxis(m_series[0]->count() + 1);
286
287 m_series[0]->append(newSet);
288 }
289 }
290
291 void MainWidget::insertBox()
292 {
293 qDebug() << "BoxPlotTester::MainWidget::insertBox()";
294
295 if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) {
296 updateAxis(m_series[0]->count() + 1);
297 for (int i = 0; i < m_seriesCount; i++) {
298 QBoxSet *newSet = new QBoxSet();
299 *newSet << 2 << 6 << 6.8 << 7 << 10;
300 m_series[i]->insert(1, newSet);
301 }
302 }
303 }
304
305 void MainWidget::removeBox()
306 {
307 qDebug() << "BoxPlotTester::MainWidget::removeBox";
308
309 if (m_seriesCount > 0) {
310 for (int i = 0; i < m_seriesCount; i++) {
311 qDebug() << "m_series[i]->count() = " << m_series[i]->count();
312 if (m_series[i]->count()) {
313 QList<QBoxSet *> sets = m_series[i]->boxSets();
314 m_series[i]->remove(sets.at(m_series[i]->count() - 1));
315 }
316 }
317
318 updateAxis(m_series[0]->count());
319 } else {
320 qDebug() << "Create a series first";
321 }
322 }
323
324 void MainWidget::clear()
325 {
326 qDebug() << "BoxPlotTester::MainWidget::clear";
327
328 if (m_seriesCount > 0)
329 m_series[0]->clear();
330 else
331 qDebug() << "Create a series first";
332 }
333
334 void MainWidget::clearBox()
335 {
336 qDebug() << "BoxPlotTester::MainWidget::clearBox";
337
338 if (m_seriesCount > 0) {
339 QList<QBoxSet *> sets = m_series[0]->boxSets();
340 if (sets.count() > 1)
341 sets.at(1)->clear();
342 else
343 qDebug() << "Create a series with at least two items first";
344 } else {
345 qDebug() << "Create a series first";
346 }
347 }
348
349 void MainWidget::setBrush()
350 {
351 qDebug() << "BoxPlotTester::MainWidget::setBrush";
352
353 if (m_seriesCount > 0) {
354 QList<QBoxSet *> sets = m_series[0]->boxSets();
355 if (sets.count() > 1)
356 sets.at(1)->setBrush(QBrush(QColor(Qt::yellow)));
357 else
358 qDebug() << "Create a series with at least two items first";
359 } else {
360 qDebug() << "Create a series first";
361 }
362 }
363
364 void MainWidget::animationToggled(bool enabled)
365 {
366 qDebug() << "BoxPlotTester::Animation toggled to " << enabled;
367 if (enabled)
368 m_chart->setAnimationOptions(QChart::SeriesAnimations);
369 else
370 m_chart->setAnimationOptions(QChart::NoAnimation);
371 }
372
373 void MainWidget::legendToggled(bool enabled)
374 {
375 qDebug() << "BoxPlotTester::Legend toggled to " << enabled;
376 m_chart->legend()->setVisible(enabled);
377 if (enabled)
378 m_chart->legend()->setAlignment(Qt::AlignBottom);
379 }
380
381 void MainWidget::titleToggled(bool enabled)
382 {
383 qDebug() << "BoxPlotTester::Title toggled to " << enabled;
384 if (enabled)
385 m_chart->setTitle("Simple boxplotchart example");
386 else
387 m_chart->setTitle("");
388 }
389
390 void MainWidget::antialiasingToggled(bool enabled)
391 {
392 qDebug() << "BoxPlotTester::antialiasingToggled toggled to " << enabled;
393 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
394 }
395
396 void MainWidget::boxOutlineToggled(bool visible)
397 {
398 qDebug() << "BoxPlotTester::boxOutlineToggled toggled to " << visible;
399 for (int i = 0; i < m_seriesCount; i++)
400 m_series[i]->setBoxOutlineVisible(visible);
401 }
402
403 void MainWidget::modelMapperToggled(bool enabled)
404 {
405 if (enabled) {
406 m_series[m_seriesCount] = new QBoxPlotSeries();
407
408 int first = 0;
409 int count = 5;
410 QVBoxPlotModelMapper *mapper = new QVBoxPlotModelMapper(this);
411 mapper->setFirstBoxSetColumn(0);
412 mapper->setLastBoxSetColumn(5);
413 mapper->setFirstRow(first);
414 mapper->setRowCount(count);
415 mapper->setSeries(m_series[m_seriesCount]);
416 mapper->setModel(m_model);
417 m_chart->addSeries(m_series[m_seriesCount]);
418
419 m_seriesCount++;
420 } else {
421 removeSeries();
422 }
423 }
424
425 void MainWidget::changeChartTheme(int themeIndex)
426 {
427 qDebug() << "BoxPlotTester::changeChartTheme: " << themeIndex;
428 if (themeIndex == 0)
429 m_chart->setTheme(QChart::ChartThemeLight);
430 else
431 m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1));
432 }
433
434 void MainWidget::boxClicked(QBoxSet *set)
435 {
436 qDebug() << "boxClicked, median = " << set->at(QBoxSet::Median);
437 }
438
439 void MainWidget::boxHovered(bool state, QBoxSet *set)
440 {
441 if (state)
442 qDebug() << "box median " << set->at(QBoxSet::Median) << " hover started";
443 else
444 qDebug() << "box median " << set->at(QBoxSet::Median) << " hover ended";
445 }
446
447 void MainWidget::boxPressed(QBoxSet *set)
448 {
449 qDebug() << "boxPressed, median = " << set->at(QBoxSet::Median);
450 }
451
452 void MainWidget::boxReleased(QBoxSet *set)
453 {
454 qDebug() << "boxReleased, median = " << set->at(QBoxSet::Median);
455 }
456
457 void MainWidget::boxDoubleClicked(QBoxSet *set)
458 {
459 qDebug() << "boxDoubleClicked, median = " << set->at(QBoxSet::Median);
460 }
461
462 void MainWidget::singleBoxClicked()
463 {
464 qDebug() << "singleBoxClicked";
465 }
466
467 void MainWidget::singleBoxPressed()
468 {
469 qDebug() << "singleBoxPressed";
470 }
471
472 void MainWidget::singleBoxReleased()
473 {
474 qDebug() << "singleBoxReleased";
475 }
476
477 void MainWidget::singleBoxDoubleClicked()
478 {
479 qDebug() << "singleBoxDoubleClicked";
480 }
481
482 void MainWidget::singleBoxHovered(bool state)
483 {
484 if (state)
485 qDebug() << "single box hover started";
486 else
487 qDebug() << "single box hover ended";
488 }
489
490 void MainWidget::changePen()
491 {
492 qDebug() << "changePen() = " << m_penTool->pen();
493 for (int i = 0; i < m_seriesCount; i++)
494 m_series[i]->setPen(m_penTool->pen());
495 }
496
497 void MainWidget::setBoxWidth(double width)
498 {
499 qDebug() << "setBoxWidth to " << width;
500
501 for (int i = 0; i < m_seriesCount; i++)
502 m_series[i]->setBoxWidth(qreal(width));
503 }