summaryrefslogtreecommitdiffstats
path: root/examples/charts/pointsselectionandmarkers/main.cpp
blob: 376d49f9a30a3bec474b6b069111043ed8e954a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "utilities.h"

#include <QApplication>
#include <QChart>
#include <QChartView>
#include <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMainWindow>
#include <QSplineSeries>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow mainWindow;
    mainWindow.setWindowTitle("Chart");

    //![1]
    constexpr qreal marker_size = 20.;

    QSplineSeries *series = new QSplineSeries();
    series->append({QPointF(0., 0.),
                    QPointF(0.5, 2.27),
                    QPointF(1.5, 2.2),
                    QPointF(3.3, 1.7),
                    QPointF(4.23, 3.1),
                    QPointF(5.3, 2.3),
                    QPointF(6.47, 4.1)
                   });
    series->setMarkerSize(marker_size);
    series->setLightMarker(Utilities::redRectangle(marker_size));
    series->setSelectedLightMarker(Utilities::blueTriangle(marker_size));
    QObject::connect(series, &QXYSeries::clicked, series, [&](const QPointF &point) {
       int index = series->points().indexOf(point);
       if (index != -1)
           series->toggleSelection({index});
    });
    //![1]

    //![2]
    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->createDefaultAxes();
    chart->legend()->setVisible(false);

    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    QWidget *controlWidget = new QWidget(&mainWindow);
    QGridLayout *controlLayout = new QGridLayout(controlWidget);
    //![2]

    //![3]
    QComboBox *charPointCombobox = new QComboBox();
    QComboBox *charPointSelectedCombobox = new QComboBox();
    QComboBox *lineColorCombobox = new QComboBox();
    QCheckBox *showUnselectedPointsCheckbox = new QCheckBox();
    //![3]

    //![4]
    QLabel *charPoint = new QLabel(QCoreApplication::tr("Char point: "));
    charPointCombobox->addItems({QCoreApplication::tr("Red rectangle"),
                                 QCoreApplication::tr("Green triangle"),
                                 QCoreApplication::tr("Orange circle")
                                });
    QObject::connect(charPointCombobox, &QComboBox::currentIndexChanged, series, [&](const int index) {
        if (showUnselectedPointsCheckbox->isChecked())
            series->setLightMarker(Utilities::getPointRepresentation(Utilities::PointType(index), marker_size));
    });
    //![4]

    //![5]
    QLabel *charPointSelected = new QLabel(QCoreApplication::tr("Char point selected: "));
    charPointSelectedCombobox->addItems({QCoreApplication::tr("Blue triangle"),
                                         QCoreApplication::tr("Yellow rectangle"),
                                         QCoreApplication::tr("Lavender circle")
                                        });
    QObject::connect(charPointSelectedCombobox, &QComboBox::currentIndexChanged, series, [&](const int index) {
        series->setSelectedLightMarker(Utilities::getSelectedPointRepresentation(Utilities::SelectedPointType(index), marker_size));
    });

    QLabel *lineColorLabel = new QLabel(QCoreApplication::tr("Line color: "));
    lineColorCombobox->addItems({QCoreApplication::tr("Blue"),
                                 QCoreApplication::tr("Black"),
                                 QCoreApplication::tr("Mint")
                                });
    QObject::connect(lineColorCombobox, &QComboBox::currentIndexChanged, series, [&](const int index) {
        series->setColor(Utilities::makeLineColor(Utilities::LineColor(index)));
    });
    //![5]

    //![6]
    QLabel *showUnselectedPointsLabel = new QLabel(QCoreApplication::tr("Display unselected points: "));
    showUnselectedPointsCheckbox->setChecked(true);
    QObject::connect(showUnselectedPointsCheckbox, &QCheckBox::stateChanged, series, [&](const int state) {
        if (state) {
            series->setLightMarker(Utilities::getPointRepresentation(Utilities::PointType(charPointCombobox->currentIndex()), marker_size));
        } else {
            series->setLightMarker(QImage());
        }
    });
    //![6]

    //![7]
    controlLayout->addWidget(charPoint, 0, 0);
    controlLayout->addWidget(charPointCombobox, 0, 1);

    controlLayout->addWidget(charPointSelected, 1, 0);
    controlLayout->addWidget(charPointSelectedCombobox, 1, 1);

    controlLayout->addWidget(lineColorLabel, 2, 0);
    controlLayout->addWidget(lineColorCombobox, 2, 1);

    controlLayout->addWidget(showUnselectedPointsLabel, 3, 0);
    controlLayout->addWidget(showUnselectedPointsCheckbox, 3, 1, 1, 2);

    QWidget *mainWidget = new QWidget(&mainWindow);
    QHBoxLayout *mainLayout = new QHBoxLayout(mainWidget);
    mainLayout->addWidget(chartView);
    mainLayout->addWidget(controlWidget);

    mainWindow.setCentralWidget(mainWidget);
    mainWindow.resize(1080, 720);
    mainWindow.show();
    return a.exec();
    //![7]
}