summaryrefslogtreecommitdiffstats
path: root/examples/charts/gallery/lightmarkerswidget.cpp
blob: ddb9b759eba9ed6a2a2fb536531340a5c3e53424 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "lightmarkerswidget.h"

#include <QBrush>
#include <QChart>
#include <QChartView>
#include <QCheckBox>
#include <QComboBox>
#include <QGraphicsLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QList>
#include <QPainter>
#include <QPainterPath>
#include <QSplineSeries>

LightMarkersWidget::LightMarkersWidget(QWidget *parent)
    : ContentWidget(parent)
{
    //![1]
    constexpr qreal marker_size = 20.;

    auto 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(rectangle(marker_size, Qt::red));
    series->setSelectedLightMarker(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]
    auto chart = new QChart;
    chart->addSeries(series);
    chart->createDefaultAxes();
    chart->legend()->setVisible(false);
    chart->layout()->setContentsMargins(0, 0, 0, 0);
    chart->setTitle("Select points with mouse click");

    auto chartView = new QChartView(chart, this);
    chartView->setRenderHint(QPainter::Antialiasing);

    auto controlWidget = new QWidget(this);
    auto controlLayout = new QGridLayout(controlWidget);
    //![2]

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

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

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

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

    //![6]
    auto showUnselectedPointsLabel = new QLabel(tr("Display unselected points: "), controlWidget);
    showUnselectedPointsCheckbox->setChecked(true);
    QObject::connect(showUnselectedPointsCheckbox, &QCheckBox::stateChanged, series, [=](const int state) {
        if (state)
            series->setLightMarker(getPointRepresentation(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);

    m_mainWidget = new QWidget(this);
    auto mainLayout = new QHBoxLayout(m_mainWidget);
    mainLayout->addWidget(chartView);
    mainLayout->addWidget(controlWidget);
    //![7]
}

void LightMarkersWidget::resizeEvent(QResizeEvent *)
{
    m_mainWidget->resize(size());
}

QImage LightMarkersWidget::rectangle(qreal imageSize, const QColor &color)
{
    QImage image(imageSize, imageSize, QImage::Format_RGB32);
    QPainter painter;
    painter.begin(&image);
    painter.fillRect(0, 0, imageSize, imageSize, color);
    painter.end();
    return image;
}

QImage LightMarkersWidget::circle(qreal imageSize, const QColor &color)
{
    QImage image(imageSize, imageSize, QImage::Format_ARGB32);
    image.fill(QColor(0, 0, 0, 0));
    QPainter paint;
    paint.begin(&image);
    paint.setBrush(color);
    QPen pen = paint.pen();
    pen.setWidth(0);
    paint.setPen(pen);
    paint.drawEllipse(0, 0, imageSize * 0.9, imageSize * 0.9);
    paint.end();
    return image;
}

QImage LightMarkersWidget::blueTriangle(qreal imageSize)
{
    return QImage(":/blue_triangle").scaled(imageSize, imageSize);
}

QImage LightMarkersWidget::greenTriangle(qreal imageSize)
{
    return QImage(":/green_triangle").scaled(imageSize, imageSize);
}

QImage LightMarkersWidget::getPointRepresentation(PointType pointType, int imageSize)
{
    switch (pointType) {
    case PointType::RedRectangle:
        return rectangle(imageSize, Qt::red);
    case PointType::GreenTriangle:
        return greenTriangle(imageSize);
    case PointType::OrangeCircle:
        return circle(imageSize, QColor(255, 127, 80));
    default:
        return rectangle(imageSize, Qt::red);
    }
}

QImage LightMarkersWidget::getSelectedPointRepresentation(SelectedPointType pointType, int imageSize)
{
    switch (pointType) {
    case SelectedPointType::BlueTriangle:
        return blueTriangle(imageSize);
    case SelectedPointType::YellowRectangle:
        return rectangle(imageSize, Qt::yellow);
    case SelectedPointType::LavenderCircle:
        return circle(imageSize, QColor(147, 112, 219));
    default:
        return blueTriangle(imageSize);
    }
}

QColor LightMarkersWidget::makeLineColor(LineColor lineColor)
{
    switch (lineColor) {
    case LineColor::Blue:
        return QColor(65, 105, 225);
    case LineColor::Black:
        return QColor(0, 0, 0);
    case LineColor::Mint:
        return QColor(70, 203, 155);
    default:
        return QColor(0, 0, 0);
    }
}