1 | // Copyright (C) 2023 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include <QtGraphs/qlineseries.h> |
5 | #include <private/qgraphsview_p.h> |
6 | #include <private/qlineseries_p.h> |
7 | #include <private/qxypoint_p.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \class QLineSeries |
13 | \inmodule QtGraphs |
14 | \ingroup graphs_2D |
15 | \brief The QLineSeries class presents data in line graphs. |
16 | |
17 | A line graph is used to show information as a series of data points |
18 | connected by straight lines. |
19 | */ |
20 | /*! |
21 | \qmltype LineSeries |
22 | \nativetype QLineSeries |
23 | \inqmlmodule QtGraphs |
24 | \ingroup graphs_qml_2D |
25 | \inherits XYSeries |
26 | |
27 | \brief Presents data in line graphs. |
28 | |
29 | A line graph is used to show information as a series of data points |
30 | connected by straight lines. |
31 | |
32 | \image graphs2d-line.png |
33 | |
34 | LineSeries uses mostly the same API as ScatterSeries so see ScatterSeries |
35 | documentation for further usage examples. |
36 | |
37 | \sa ScatterSeries |
38 | */ |
39 | |
40 | /*! |
41 | \qmlproperty real LineSeries::width |
42 | The width of the line. By default, the width is 2.0. Widths lower than 0 |
43 | are invalid and are automatically set to 0. |
44 | */ |
45 | |
46 | /*! |
47 | \qmlproperty Qt::PenCapStyle LineSeries::capStyle |
48 | Controls the cap style of the line. Set to one of \l{Qt::FlatCap}{Qt.FlatCap}, |
49 | \l{Qt::SquareCap}{Qt.SquareCap} or \l{Qt::RoundCap}{Qt.RoundCap}. By |
50 | default the cap style is Qt.SquareCap. Invalid values are automatically set |
51 | to the default value. |
52 | |
53 | \sa Qt::PenCapStyle |
54 | */ |
55 | |
56 | /*! |
57 | \qmlproperty Component LineSeries::pointDelegate |
58 | Marks the point with the given QML component. |
59 | |
60 | \code |
61 | pointDelegate: Image { |
62 | source: "images/happy_box.png" |
63 | } |
64 | \endcode |
65 | */ |
66 | |
67 | /*! |
68 | \qmlsignal LineSeries::widthChanged() |
69 | This signal is emitted when the line series width changes. |
70 | */ |
71 | |
72 | /*! |
73 | \qmlsignal LineSeries::capStyleChanged() |
74 | This signal is emitted when the line series cap style changes. |
75 | */ |
76 | |
77 | QLineSeries::QLineSeries(QObject *parent) |
78 | : QXYSeries(*(new QLineSeriesPrivate()), parent) |
79 | {} |
80 | |
81 | QLineSeries::~QLineSeries() {} |
82 | |
83 | QLineSeries::QLineSeries(QLineSeriesPrivate &dd, QObject *parent) |
84 | : QXYSeries(dd, parent) |
85 | {} |
86 | |
87 | void QLineSeries::componentComplete() |
88 | { |
89 | Q_D(QLineSeries); |
90 | |
91 | for (auto *child : children()) { |
92 | if (auto point = qobject_cast<QXYPoint *>(object: child)) |
93 | append(x: point->x(), y: point->y()); |
94 | } |
95 | |
96 | if (d->m_graphTransition) |
97 | d->m_graphTransition->initialize(); |
98 | |
99 | QAbstractSeries::componentComplete(); |
100 | } |
101 | |
102 | QAbstractSeries::SeriesType QLineSeries::type() const |
103 | { |
104 | return QAbstractSeries::SeriesType::Line; |
105 | } |
106 | |
107 | QLineSeriesPrivate::QLineSeriesPrivate() {} |
108 | |
109 | qreal QLineSeries::width() const |
110 | { |
111 | Q_D(const QLineSeries); |
112 | return d->m_width; |
113 | } |
114 | |
115 | void QLineSeries::setWidth(qreal newWidth) |
116 | { |
117 | Q_D(QLineSeries); |
118 | if (newWidth < 0.0) |
119 | newWidth = 0.0; |
120 | if (qFuzzyCompare(p1: d->m_width, p2: newWidth)) |
121 | return; |
122 | d->m_width = newWidth; |
123 | emit widthChanged(); |
124 | emit update(); |
125 | } |
126 | |
127 | Qt::PenCapStyle QLineSeries::capStyle() const |
128 | { |
129 | Q_D(const QLineSeries); |
130 | return d->m_capStyle; |
131 | } |
132 | |
133 | void QLineSeries::setCapStyle(Qt::PenCapStyle newCapStyle) |
134 | { |
135 | Q_D(QLineSeries); |
136 | Qt::PenCapStyle validCapStyle = newCapStyle; |
137 | if (validCapStyle != Qt::PenCapStyle::FlatCap && validCapStyle != Qt::PenCapStyle::SquareCap |
138 | && validCapStyle != Qt::PenCapStyle::RoundCap |
139 | && validCapStyle != Qt::PenCapStyle::MPenCapStyle) { |
140 | validCapStyle = Qt::PenCapStyle::SquareCap; |
141 | } |
142 | if (d->m_capStyle == validCapStyle) |
143 | return; |
144 | d->m_capStyle = validCapStyle; |
145 | emit capStyleChanged(); |
146 | emit update(); |
147 | } |
148 | |
149 | QT_END_NAMESPACE |
150 | |