1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "window.h"
31#include "view.h"
32#include "grid.h"
33#include "charts.h"
34#include <QtCharts/QChartView>
35#include <QtCharts/QAreaSeries>
36#include <QtCharts/QLegend>
37#include <QtCharts/QValueAxis>
38#include <QtWidgets/QGridLayout>
39#include <QtWidgets/QFormLayout>
40#include <QtWidgets/QComboBox>
41#include <QtWidgets/QSpinBox>
42#include <QtWidgets/QCheckBox>
43#include <QtWidgets/QGroupBox>
44#include <QtWidgets/QLabel>
45#include <QtWidgets/QGraphicsScene>
46#include <QtWidgets/QGraphicsLinearLayout>
47#include <QtWidgets/QGraphicsProxyWidget>
48#include <QtWidgets/QOpenGLWidget>
49#include <QtWidgets/QApplication>
50#include <QtCore/QDebug>
51#include <QtCore/QRegularExpression>
52#include <QtWidgets/QMenu>
53#include <QtWidgets/QPushButton>
54
55Window::Window(const QVariantHash &parameters, QWidget *parent)
56 : QMainWindow(parent),
57 m_scene(new QGraphicsScene(this)),
58 m_view(0),
59 m_form(0),
60 m_themeComboBox(0),
61 m_antialiasCheckBox(0),
62 m_animatedComboBox(0),
63 m_legendComboBox(0),
64 m_templateComboBox(0),
65 m_viewComboBox(0),
66 m_xTickSpinBox(0),
67 m_yTickSpinBox(0),
68 m_minorXTickSpinBox(0),
69 m_minorYTickSpinBox(0),
70 m_openGLCheckBox(0),
71 m_zoomCheckBox(0),
72 m_scrollCheckBox(0),
73 m_gridCheckBox(0),
74 m_baseLayout(new QGraphicsLinearLayout()),
75 m_menu(createMenu()),
76 m_template(0),
77 m_grid(new Grid(-1))
78{
79 createProxyWidgets();
80 // create layout
81 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
82
83 settingsLayout->setOrientation(Qt::Vertical);
84 settingsLayout->setSizePolicy(hPolicy: QSizePolicy::Maximum, vPolicy: QSizePolicy::Maximum);
85 settingsLayout->addItem(item: m_widgetHash["openGLCheckBox"]);
86 settingsLayout->addItem(item: m_widgetHash["antialiasCheckBox"]);
87 settingsLayout->addItem(item: m_widgetHash["viewLabel"]);
88 settingsLayout->addItem(item: m_widgetHash["viewComboBox"]);
89 settingsLayout->addItem(item: m_widgetHash["themeLabel"]);
90 settingsLayout->addItem(item: m_widgetHash["themeComboBox"]);
91 settingsLayout->addItem(item: m_widgetHash["animationsLabel"]);
92 settingsLayout->addItem(item: m_widgetHash["animatedComboBox"]);
93 settingsLayout->addItem(item: m_widgetHash["legendLabel"]);
94 settingsLayout->addItem(item: m_widgetHash["legendComboBox"]);
95 settingsLayout->addItem(item: m_widgetHash["templateLabel"]);
96 settingsLayout->addItem(item: m_widgetHash["templateComboBox"]);
97 settingsLayout->addItem(item: m_widgetHash["scrollCheckBox"]);
98 settingsLayout->addItem(item: m_widgetHash["zoomCheckBox"]);
99 settingsLayout->addItem(item: m_widgetHash["gridCheckBox"]);
100 settingsLayout->addItem(item: m_widgetHash["xTickLabel"]);
101 settingsLayout->addItem(item: m_widgetHash["xTickSpinBox"]);
102 settingsLayout->addItem(item: m_widgetHash["yTickLabel"]);
103 settingsLayout->addItem(item: m_widgetHash["yTickSpinBox"]);
104 settingsLayout->addItem(item: m_widgetHash["minorXTickLabel"]);
105 settingsLayout->addItem(item: m_widgetHash["minorXTickSpinBox"]);
106 settingsLayout->addItem(item: m_widgetHash["minorYTickLabel"]);
107 settingsLayout->addItem(item: m_widgetHash["minorYTickSpinBox"]);
108 settingsLayout->addStretch();
109
110 m_baseLayout->setOrientation(Qt::Horizontal);
111 m_baseLayout->addItem(item: m_grid);
112 m_baseLayout->addItem(item: settingsLayout);
113
114 m_form = new QGraphicsWidget();
115 m_form->setLayout(m_baseLayout);
116 m_scene->addItem(item: m_form);
117
118 m_view = new View(m_scene, m_form);
119 m_view->setMinimumSize(m_form->minimumSize().toSize());
120
121 // Set defaults
122 m_antialiasCheckBox->setChecked(true);
123 initializeFromParamaters(parameters);
124 updateUI();
125 if(!m_category.isEmpty() && !m_subcategory.isEmpty() && !m_name.isEmpty())
126 m_grid->createCharts(category: m_category,subcategory: m_subcategory,name: m_name);
127
128
129 handleGeometryChanged();
130 setCentralWidget(m_view);
131
132 connectSignals();
133}
134
135Window::~Window()
136{
137}
138
139void Window::connectSignals()
140{
141 QObject::connect(sender: m_form, SIGNAL(geometryChanged()), receiver: this , SLOT(handleGeometryChanged()));
142 QObject::connect(sender: m_viewComboBox, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateUI()));
143 QObject::connect(sender: m_themeComboBox, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateUI()));
144 QObject::connect(sender: m_xTickSpinBox, SIGNAL(valueChanged(int)), receiver: this, SLOT(updateUI()));
145 QObject::connect(sender: m_yTickSpinBox, SIGNAL(valueChanged(int)), receiver: this, SLOT(updateUI()));
146 QObject::connect(sender: m_minorXTickSpinBox, SIGNAL(valueChanged(int)), receiver: this, SLOT(updateUI()));
147 QObject::connect(sender: m_minorYTickSpinBox, SIGNAL(valueChanged(int)), receiver: this, SLOT(updateUI()));
148 QObject::connect(sender: m_antialiasCheckBox, SIGNAL(toggled(bool)), receiver: this, SLOT(updateUI()));
149 QObject::connect(sender: m_openGLCheckBox, SIGNAL(toggled(bool)), receiver: this, SLOT(updateUI()));
150 QObject::connect(sender: m_zoomCheckBox, SIGNAL(toggled(bool)), receiver: this, SLOT(updateUI()));
151 QObject::connect(sender: m_scrollCheckBox, SIGNAL(toggled(bool)), receiver: this, SLOT(updateUI()));
152 QObject::connect(sender: m_gridCheckBox, SIGNAL(toggled(bool)), receiver: this, SLOT(updateUI()));
153 QObject::connect(sender: m_animatedComboBox, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateUI()));
154 QObject::connect(sender: m_legendComboBox, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateUI()));
155 QObject::connect(sender: m_templateComboBox, SIGNAL(currentIndexChanged(int)), receiver: this, SLOT(updateUI()));
156 QObject::connect(sender: m_grid, SIGNAL(chartSelected(QChart*)), receiver: this, SLOT(handleChartSelected(QChart*)));
157}
158
159void Window::createProxyWidgets()
160{
161 m_themeComboBox = createThemeBox();
162 m_viewComboBox = createViewBox();
163 m_xTickSpinBox = new QSpinBox();
164 m_xTickSpinBox->setMinimum(2);
165 m_xTickSpinBox->setValue(5);
166 m_yTickSpinBox = new QSpinBox();
167 m_yTickSpinBox->setMinimum(2);
168 m_yTickSpinBox->setValue(5);
169 m_minorXTickSpinBox = new QSpinBox();
170 m_minorYTickSpinBox = new QSpinBox();
171 m_antialiasCheckBox = new QCheckBox(tr(s: "Anti-aliasing"));
172 m_animatedComboBox = createAnimationBox();
173 m_legendComboBox = createLegendBox();
174 m_openGLCheckBox = new QCheckBox(tr(s: "OpenGL"));
175 m_zoomCheckBox = new QCheckBox(tr(s: "Zoom"));
176 m_scrollCheckBox = new QCheckBox(tr(s: "Scroll"));
177 m_gridCheckBox = new QCheckBox(tr(s: "Grid lines"));
178 m_templateComboBox = createTempleteBox();
179 m_widgetHash["viewLabel"] = m_scene->addWidget(widget: new QLabel("View"));
180 m_widgetHash["viewComboBox"] = m_scene->addWidget(widget: m_viewComboBox);
181 m_widgetHash["themeComboBox"] = m_scene->addWidget(widget: m_themeComboBox);
182 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(widget: m_antialiasCheckBox);
183 m_widgetHash["animatedComboBox"] = m_scene->addWidget(widget: m_animatedComboBox);
184 m_widgetHash["legendComboBox"] = m_scene->addWidget(widget: m_legendComboBox);
185 m_widgetHash["xTickLabel"] = m_scene->addWidget(widget: new QLabel("X Tick"));
186 m_widgetHash["xTickSpinBox"] = m_scene->addWidget(widget: m_xTickSpinBox);
187 m_widgetHash["yTickLabel"] = m_scene->addWidget(widget: new QLabel("Y Tick"));
188 m_widgetHash["yTickSpinBox"] = m_scene->addWidget(widget: m_yTickSpinBox);
189 m_widgetHash["minorXTickLabel"] = m_scene->addWidget(widget: new QLabel("Minor X Tick"));
190 m_widgetHash["minorXTickSpinBox"] = m_scene->addWidget(widget: m_minorXTickSpinBox);
191 m_widgetHash["minorYTickLabel"] = m_scene->addWidget(widget: new QLabel("Minor Y Tick"));
192 m_widgetHash["minorYTickSpinBox"] = m_scene->addWidget(widget: m_minorYTickSpinBox);
193 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(widget: m_openGLCheckBox);
194 m_widgetHash["themeLabel"] = m_scene->addWidget(widget: new QLabel("Theme"));
195 m_widgetHash["animationsLabel"] = m_scene->addWidget(widget: new QLabel("Animations"));
196 m_widgetHash["legendLabel"] = m_scene->addWidget(widget: new QLabel("Legend"));
197 m_widgetHash["templateLabel"] = m_scene->addWidget(widget: new QLabel("Chart template"));
198 m_widgetHash["templateComboBox"] = m_scene->addWidget(widget: m_templateComboBox);
199 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(widget: m_zoomCheckBox);
200 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(widget: m_scrollCheckBox);
201 m_widgetHash["gridCheckBox"] = m_scene->addWidget(widget: m_gridCheckBox);
202}
203
204QComboBox *Window::createThemeBox()
205{
206 QComboBox *themeComboBox = new ComboBox(this);
207 themeComboBox->addItem(atext: "Light", auserData: QChart::ChartThemeLight);
208 themeComboBox->addItem(atext: "Blue Cerulean", auserData: QChart::ChartThemeBlueCerulean);
209 themeComboBox->addItem(atext: "Dark", auserData: QChart::ChartThemeDark);
210 themeComboBox->addItem(atext: "Brown Sand", auserData: QChart::ChartThemeBrownSand);
211 themeComboBox->addItem(atext: "Blue NCS", auserData: QChart::ChartThemeBlueNcs);
212 themeComboBox->addItem(atext: "High Contrast", auserData: QChart::ChartThemeHighContrast);
213 themeComboBox->addItem(atext: "Blue Icy", auserData: QChart::ChartThemeBlueIcy);
214 themeComboBox->addItem(atext: "Qt", auserData: QChart::ChartThemeQt);
215 return themeComboBox;
216}
217
218QComboBox *Window::createViewBox()
219{
220 QComboBox *viewComboBox = new ComboBox(this);
221 viewComboBox->addItem(atext: "1 chart", auserData: 1);
222 viewComboBox->addItem(atext: "4 charts", auserData: 2);
223 viewComboBox->addItem(atext: "9 charts", auserData: 3);
224 viewComboBox->addItem(atext: "16 charts", auserData: 4);
225 return viewComboBox;
226}
227
228QComboBox *Window::createAnimationBox()
229{
230 QComboBox *animationComboBox = new ComboBox(this);
231 animationComboBox->addItem(atext: "No Animations", auserData: QChart::NoAnimation);
232 animationComboBox->addItem(atext: "GridAxis Animations", auserData: QChart::GridAxisAnimations);
233 animationComboBox->addItem(atext: "Series Animations", auserData: QChart::SeriesAnimations);
234 animationComboBox->addItem(atext: "All Animations", auserData: QChart::AllAnimations);
235 return animationComboBox;
236}
237
238QComboBox *Window::createLegendBox()
239{
240 QComboBox *legendComboBox = new ComboBox(this);
241 legendComboBox->addItem(atext: "No Legend ", auserData: 0);
242 legendComboBox->addItem(atext: "Legend Top", auserData: Qt::AlignTop);
243 legendComboBox->addItem(atext: "Legend Bottom", auserData: Qt::AlignBottom);
244 legendComboBox->addItem(atext: "Legend Left", auserData: Qt::AlignLeft);
245 legendComboBox->addItem(atext: "Legend Right", auserData: Qt::AlignRight);
246 return legendComboBox;
247}
248
249QComboBox *Window::createTempleteBox()
250{
251 QComboBox *templateComboBox = new ComboBox(this);
252 templateComboBox->addItem(atext: "No Template", auserData: 0);
253
254 Charts::ChartList list = Charts::chartList();
255 QMultiMap<QString, Chart *> categoryMap;
256
257 foreach (Chart *chart, list)
258 categoryMap.insertMulti(key: chart->category(), value: chart);
259
260 foreach (const QString &category, categoryMap.uniqueKeys())
261 templateComboBox->addItem(atext: category, auserData: category);
262
263 return templateComboBox;
264}
265
266void Window::initializeFromParamaters(const QVariantHash &parameters)
267{
268 if (parameters.contains(akey: "view")) {
269 int t = parameters["view"].toInt();
270 for (int i = 0; i < m_viewComboBox->count(); ++i) {
271 if (m_viewComboBox->itemData(index: i).toInt() == t) {
272 m_viewComboBox->setCurrentIndex(i);
273 break;
274 }
275 }
276 }
277
278 if (parameters.contains(akey: "chart")) {
279 QString t = parameters["chart"].toString();
280
281 QRegularExpression rx("([a-zA-Z0-9_]*)::([a-zA-Z0-9_]*)::([a-zA-Z0-9_]*)");
282 QRegularExpressionMatch rmatch;
283 int pos = t.indexOf(re: rx, from: 0, rmatch: &rmatch);
284
285 if (pos > -1) {
286 m_category = rmatch.captured(nth: 1);
287 m_subcategory = rmatch.captured(nth: 2);
288 m_name = rmatch.captured(nth: 3);
289 m_templateComboBox->setCurrentIndex(0);
290 }
291 else {
292 for (int i = 0; i < m_templateComboBox->count(); ++i) {
293 if (m_templateComboBox->itemText(index: i) == t) {
294 m_templateComboBox->setCurrentIndex(i);
295 break;
296 }
297 }
298 }
299 }
300 if (parameters.contains(akey: "opengl")) {
301 bool checked = parameters["opengl"].toBool();
302 m_openGLCheckBox->setChecked(checked);
303 }
304 if (parameters.contains(akey: "theme")) {
305 QString t = parameters["theme"].toString();
306 for (int i = 0; i < m_themeComboBox->count(); ++i) {
307 if (m_themeComboBox->itemText(index: i) == t) {
308 m_themeComboBox->setCurrentIndex(i);
309 break;
310 }
311 }
312 }
313 if (parameters.contains(akey: "animation")) {
314 QString t = parameters["animation"].toString();
315 for (int i = 0; i < m_animatedComboBox->count(); ++i) {
316 if (m_animatedComboBox->itemText(index: i) == t) {
317 m_animatedComboBox->setCurrentIndex(i);
318 break;
319 }
320 }
321 }
322 if (parameters.contains(akey: "legend")) {
323 QString t = parameters["legend"].toString();
324 for (int i = 0; i < m_legendComboBox->count(); ++i) {
325 if (m_legendComboBox->itemText(index: i) == t) {
326 m_legendComboBox->setCurrentIndex(i);
327 break;
328 }
329 }
330 }
331}
332
333void Window::updateUI()
334{
335 checkView();
336 checkTemplate();
337 checkOpenGL();
338 checkTheme();
339 checkAnimationOptions();
340 checkLegend();
341 checkState();
342 checkXTick();
343 checkYTick();
344 checkMinorXTick();
345 checkMinorYTick();
346}
347
348void Window::checkView()
349{
350 int count(m_viewComboBox->itemData(index: m_viewComboBox->currentIndex()).toInt());
351 if(m_grid->size()!=count){
352 m_grid->setSize(count);
353 m_template = 0;
354 }
355}
356
357void Window::checkXTick()
358{
359 foreach (QChart *chart, m_grid->charts()) {
360 if (qobject_cast<QValueAxis *>(object: chart->axisX())) {
361 QValueAxis *valueAxis = qobject_cast<QValueAxis *>(object: chart->axisX());
362 valueAxis->setGridLineVisible(m_gridCheckBox->isChecked());
363 valueAxis->setTickCount(m_xTickSpinBox->value());
364 }
365 }
366}
367
368void Window::checkYTick()
369{
370 foreach (QChart *chart, m_grid->charts()) {
371 if (qobject_cast<QValueAxis *>(object: chart->axisY())) {
372 QValueAxis *valueAxis = qobject_cast<QValueAxis *>(object: chart->axisY());
373 valueAxis->setGridLineVisible(m_gridCheckBox->isChecked());
374 valueAxis->setTickCount(m_yTickSpinBox->value());
375 }
376 }
377}
378
379void Window::checkMinorXTick()
380{
381 foreach (QChart *chart, m_grid->charts()) {
382 if (qobject_cast<QValueAxis *>(object: chart->axisX())) {
383 QValueAxis *valueAxis = qobject_cast<QValueAxis *>(object: chart->axisX());
384 valueAxis->setMinorGridLineVisible(m_gridCheckBox->isChecked());
385 valueAxis->setGridLineVisible(m_gridCheckBox->isChecked());
386 valueAxis->setMinorTickCount(m_minorXTickSpinBox->value());
387 }
388 }
389}
390
391void Window::checkMinorYTick()
392{
393 foreach (QChart *chart, m_grid->charts()) {
394 if (qobject_cast<QValueAxis *>(object: chart->axisY())) {
395 QValueAxis *valueAxis = qobject_cast<QValueAxis *>(object: chart->axisY());
396 valueAxis->setMinorGridLineVisible(m_gridCheckBox->isChecked());
397 valueAxis->setGridLineVisible(m_gridCheckBox->isChecked());
398 valueAxis->setMinorTickCount(m_minorYTickSpinBox->value());
399 }
400 }
401}
402
403void Window::checkLegend()
404{
405 Qt::Alignment alignment(m_legendComboBox->itemData(index: m_legendComboBox->currentIndex()).toInt());
406
407 if (!alignment) {
408 foreach (QChart *chart, m_grid->charts())
409 chart->legend()->hide();
410 } else {
411 foreach (QChart *chart, m_grid->charts()) {
412 chart->legend()->setAlignment(alignment);
413 chart->legend()->show();
414 }
415 }
416}
417
418void Window::checkOpenGL()
419{
420 bool opengl = m_openGLCheckBox->isChecked();
421 bool isOpengl = qobject_cast<QOpenGLWidget *>(object: m_view->viewport());
422 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
423 m_view->deleteLater();
424 m_view = new View(m_scene, m_form);
425 m_view->setViewport(!opengl ? new QWidget() : new QOpenGLWidget());
426 setCentralWidget(m_view);
427 }
428
429 bool antialias = m_antialiasCheckBox->isChecked();
430
431 m_view->setRenderHint(hint: QPainter::Antialiasing, enabled: antialias);
432}
433
434void Window::checkAnimationOptions()
435{
436 QChart::AnimationOptions options(
437 m_animatedComboBox->itemData(index: m_animatedComboBox->currentIndex()).toInt());
438
439 QList<QChart *> charts = m_grid->charts();
440
441 if (!charts.isEmpty() && charts.at(i: 0)->animationOptions() != options) {
442 foreach (QChart *chart, charts)
443 chart->setAnimationOptions(options);
444 }
445}
446
447void Window::checkState()
448{
449 bool scroll = m_scrollCheckBox->isChecked();
450
451
452 if (m_grid->state() != Grid::ScrollState && scroll) {
453 m_grid->setState(Grid::ScrollState);
454 m_zoomCheckBox->setChecked(false);
455 } else if (!scroll && m_grid->state() == Grid::ScrollState) {
456 m_grid->setState(Grid::NoState);
457 }
458
459 bool zoom = m_zoomCheckBox->isChecked();
460
461 if (m_grid->state() != Grid::ZoomState && zoom) {
462 m_grid->setState(Grid::ZoomState);
463 m_scrollCheckBox->setChecked(false);
464 } else if (!zoom && m_grid->state() == Grid::ZoomState) {
465 m_grid->setState(Grid::NoState);
466 }
467}
468
469void Window::checkTemplate()
470{
471 int index = m_templateComboBox->currentIndex();
472 if (m_template == index || index == 0)
473 return;
474
475 m_template = index;
476 QString category = m_templateComboBox->itemData(index).toString();
477 m_grid->createCharts(category);
478}
479
480void Window::checkTheme()
481{
482 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
483 index: m_themeComboBox->currentIndex()).toInt();
484
485 foreach (QChart *chart, m_grid->charts())
486 chart->setTheme(theme);
487
488 QPalette pal = window()->palette();
489 if (theme == QChart::ChartThemeLight) {
490 pal.setColor(acr: QPalette::Window, acolor: QRgb(0xf0f0f0));
491 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x404044));
492 } else if (theme == QChart::ChartThemeDark) {
493 pal.setColor(acr: QPalette::Window, acolor: QRgb(0x121218));
494 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0xd6d6d6));
495 } else if (theme == QChart::ChartThemeBlueCerulean) {
496 pal.setColor(acr: QPalette::Window, acolor: QRgb(0x40434a));
497 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0xd6d6d6));
498 } else if (theme == QChart::ChartThemeBrownSand) {
499 pal.setColor(acr: QPalette::Window, acolor: QRgb(0x9e8965));
500 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x404044));
501 } else if (theme == QChart::ChartThemeBlueNcs) {
502 pal.setColor(acr: QPalette::Window, acolor: QRgb(0x018bba));
503 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x404044));
504 } else if (theme == QChart::ChartThemeHighContrast) {
505 pal.setColor(acr: QPalette::Window, acolor: QRgb(0xffab03));
506 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x181818));
507 } else if (theme == QChart::ChartThemeBlueIcy) {
508 pal.setColor(acr: QPalette::Window, acolor: QRgb(0xcee7f0));
509 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x404044));
510 } else if (theme == QChart::ChartThemeQt) {
511 pal.setColor(acr: QPalette::Window, acolor: QRgb(0xf0f0f0));
512 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x404044));
513 } else {
514 pal.setColor(acr: QPalette::Window, acolor: QRgb(0xf0f0f0));
515 pal.setColor(acr: QPalette::WindowText, acolor: QRgb(0x404044));
516 }
517 foreach (QGraphicsProxyWidget *widget, m_widgetHash)
518 widget->setPalette(pal);
519 m_view->setBackgroundBrush(pal.color(cr: (QPalette::Window)));
520 m_grid->setRubberPen(pal.color(cr: (QPalette::WindowText)));
521}
522
523void Window::comboBoxFocused(QComboBox *combobox)
524{
525 foreach (QGraphicsProxyWidget *widget , m_widgetHash) {
526 if (widget->widget() == combobox)
527 widget->setZValue(2.0);
528 else
529 widget->setZValue(0.0);
530 }
531}
532
533void Window::handleChartSelected(QChart *qchart)
534{
535 if (m_templateComboBox->currentIndex() != 0)
536 return;
537
538 QAction *chosen = m_menu->exec(pos: QCursor::pos());
539
540 if (chosen) {
541 Chart *chart = (Chart *) chosen->data().value<void *>();
542 m_grid->replaceChart(oldChart: qchart, newChart: chart);
543 updateUI();
544 }
545}
546
547QMenu *Window::createMenu()
548{
549 Charts::ChartList list = Charts::chartList();
550 QMultiMap<QString, Chart *> categoryMap;
551
552 QMenu *result = new QMenu(this);
553
554 foreach (Chart *chart, list)
555 categoryMap.insertMulti(key: chart->category(), value: chart);
556
557 foreach (const QString &category, categoryMap.uniqueKeys()) {
558 QMenu *menu(0);
559 QMultiMap<QString, Chart *> subCategoryMap;
560 if (category.isEmpty()) {
561 menu = result;
562 } else {
563 menu = new QMenu(category, this);
564 result->addMenu(menu);
565 }
566
567 foreach (Chart *chart, categoryMap.values(category))
568 subCategoryMap.insert(akey: chart->subCategory(), avalue: chart);
569
570 foreach (const QString &subCategory, subCategoryMap.uniqueKeys()) {
571 QMenu *subMenu(0);
572 if (subCategory.isEmpty()) {
573 subMenu = menu;
574 } else {
575 subMenu = new QMenu(subCategory, this);
576 menu->addMenu(menu: subMenu);
577 }
578
579 foreach (Chart *chart, subCategoryMap.values(subCategory)) {
580 createMenuAction(menu: subMenu, icon: QIcon(), text: chart->name(),
581 data: QVariant::fromValue(value: static_cast<void *>(chart)));
582 }
583 }
584 }
585 return result;
586}
587
588QAction *Window::createMenuAction(QMenu *menu, const QIcon &icon, const QString &text,
589 const QVariant &data)
590{
591 QAction *action = menu->addAction(icon, text);
592 action->setCheckable(false);
593 action->setData(data);
594 return action;
595}
596
597void Window::handleGeometryChanged()
598{
599 QSizeF size = m_baseLayout->sizeHint(which: Qt::MinimumSize);
600 m_view->scene()->setSceneRect(x: 0, y: 0, w: this->width(), h: this->height());
601 m_view->setMinimumSize(size.toSize());
602}
603

source code of qtcharts/tests/manual/chartviewer/window.cpp