1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include <QtCharts/QXYLegendMarker> |
5 | #include <private/qxylegendmarker_p.h> |
6 | #include <private/qxyseries_p.h> |
7 | #include <QtCharts/QXYSeries> |
8 | #if QT_CONFIG(charts_scatter_chart) |
9 | #include <QtCharts/QScatterSeries> |
10 | #endif |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | /*! |
15 | \class QXYLegendMarker |
16 | \inmodule QtCharts |
17 | \brief The QXYLegendMarker class is a legend marker for a line, spline, or scatter series. |
18 | |
19 | An XY legend marker is related to QXYSeries derived classes: QLineSeries, QSplineSeries, |
20 | and QScatterSeries. Each marker is related to one series. |
21 | |
22 | \sa QLegend, QXYSeries, QSplineSeries, QScatterSeries, QLineSeries |
23 | */ |
24 | |
25 | /*! |
26 | \fn virtual LegendMarkerType QXYLegendMarker::type() |
27 | \reimp |
28 | */ |
29 | |
30 | /*! |
31 | \internal |
32 | */ |
33 | QXYLegendMarker::QXYLegendMarker(QXYSeries *series, QLegend *legend, QObject *parent) : |
34 | QLegendMarker(*new QXYLegendMarkerPrivate(this,series,legend), parent) |
35 | { |
36 | d_ptr->updated(); |
37 | } |
38 | |
39 | /*! |
40 | Removes the legend marker for a line, spline, or scatter series. |
41 | */ |
42 | QXYLegendMarker::~QXYLegendMarker() |
43 | { |
44 | } |
45 | |
46 | /*! |
47 | \internal |
48 | */ |
49 | QXYLegendMarker::QXYLegendMarker(QXYLegendMarkerPrivate &d, QObject *parent) : |
50 | QLegendMarker(d, parent) |
51 | { |
52 | } |
53 | |
54 | /*! |
55 | \reimp |
56 | */ |
57 | QXYSeries* QXYLegendMarker::series() |
58 | { |
59 | Q_D(QXYLegendMarker); |
60 | return d->m_series; |
61 | } |
62 | |
63 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
64 | |
65 | QXYLegendMarkerPrivate::QXYLegendMarkerPrivate(QXYLegendMarker *q, QXYSeries *series, QLegend *legend) : |
66 | QLegendMarkerPrivate(q,legend), |
67 | q_ptr(q), |
68 | m_series(series) |
69 | { |
70 | connect(sender: m_series->d_func(), signal: &QXYSeriesPrivate::seriesUpdated, |
71 | context: this, slot: &QXYLegendMarkerPrivate::updated); |
72 | connect(sender: m_series, signal: &QXYSeries::nameChanged, context: this, slot: &QXYLegendMarkerPrivate::updated); |
73 | } |
74 | |
75 | QXYLegendMarkerPrivate::~QXYLegendMarkerPrivate() |
76 | { |
77 | } |
78 | |
79 | QAbstractSeries* QXYLegendMarkerPrivate::series() |
80 | { |
81 | return m_series; |
82 | } |
83 | |
84 | QObject* QXYLegendMarkerPrivate::relatedObject() |
85 | { |
86 | return m_series; |
87 | } |
88 | |
89 | void QXYLegendMarkerPrivate::updated() |
90 | { |
91 | bool labelChanged = false; |
92 | bool brushChanged = false; |
93 | |
94 | if (!m_customLabel && (m_item->label() != m_series->name())) { |
95 | m_item->setLabel(m_series->name()); |
96 | labelChanged = true; |
97 | } |
98 | #if QT_CONFIG(charts_scatter_chart) |
99 | if (m_series->type()== QAbstractSeries::SeriesTypeScatter) { |
100 | if (!m_customBrush && (m_item->brush() != m_series->brush())) { |
101 | m_item->setBrush(m_series->brush()); |
102 | brushChanged = true; |
103 | } |
104 | if (m_item->effectiveMarkerShape() == QLegend::MarkerShapeFromSeries) { |
105 | QScatterSeries *scatter = static_cast<QScatterSeries *>(m_series); |
106 | if (scatter) { |
107 | const bool shapeChangeNeeded = |
108 | (scatter->markerShape() == QScatterSeries::MarkerShapeCircle |
109 | && m_item->itemType() != LegendMarkerItem::TypeCircle) |
110 | || (scatter->markerShape() == QScatterSeries::MarkerShapeRectangle |
111 | && m_item->itemType() != LegendMarkerItem::TypeRect); |
112 | if (shapeChangeNeeded || scatter->markerSize() != m_item->markerRect().width()) |
113 | m_item->updateMarkerShapeAndSize(); |
114 | } |
115 | } |
116 | } else |
117 | #endif |
118 | { |
119 | QBrush emptyBrush; |
120 | if (!m_customBrush |
121 | && (m_item->brush() == emptyBrush |
122 | || m_item->brush().color() != m_series->pen().color())) { |
123 | m_item->setBrush(QBrush(m_series->pen().color())); |
124 | brushChanged = true; |
125 | } |
126 | |
127 | if (m_item->effectiveMarkerShape() == QLegend::MarkerShapeFromSeries |
128 | && m_series->markerSize() != m_item->markerRect().width()) { |
129 | m_item->updateMarkerShapeAndSize(); |
130 | } |
131 | } |
132 | m_item->setSeriesBrush(m_series->brush()); |
133 | m_item->setSeriesPen(m_series->pen()); |
134 | |
135 | if (m_item->effectiveMarkerShape() == QLegend::MarkerShapeFromSeries) |
136 | m_item->setSeriesLightMarker(m_series->lightMarker()); |
137 | |
138 | invalidateLegend(); |
139 | |
140 | if (labelChanged) |
141 | emit q_ptr->labelChanged(); |
142 | if (brushChanged) |
143 | emit q_ptr->brushChanged(); |
144 | } |
145 | |
146 | QT_END_NAMESPACE |
147 | |
148 | #include "moc_qxylegendmarker.cpp" |
149 | #include "moc_qxylegendmarker_p.cpp" |
150 | |