| 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 <QtCharts/QXYLegendMarker> |
| 31 | #include <private/qxylegendmarker_p.h> |
| 32 | #include <private/qxyseries_p.h> |
| 33 | #include <QtCharts/QXYSeries> |
| 34 | #include <QtCharts/QScatterSeries> |
| 35 | |
| 36 | QT_CHARTS_BEGIN_NAMESPACE |
| 37 | |
| 38 | /*! |
| 39 | \class QXYLegendMarker |
| 40 | \inmodule QtCharts |
| 41 | \brief The QXYLegendMarker class is a legend marker for a line, spline, or scatter series. |
| 42 | |
| 43 | An XY legend marker is related to QXYSeries derived classes: QLineSeries, QSplineSeries, |
| 44 | and QScatterSeries. Each marker is related to one series. |
| 45 | |
| 46 | \sa QLegend, QXYSeries, QSplineSeries, QScatterSeries, QLineSeries |
| 47 | */ |
| 48 | |
| 49 | /*! |
| 50 | \fn virtual LegendMarkerType QXYLegendMarker::type() |
| 51 | \reimp |
| 52 | */ |
| 53 | |
| 54 | /*! |
| 55 | \internal |
| 56 | */ |
| 57 | QXYLegendMarker::QXYLegendMarker(QXYSeries *series, QLegend *legend, QObject *parent) : |
| 58 | QLegendMarker(*new QXYLegendMarkerPrivate(this,series,legend), parent) |
| 59 | { |
| 60 | d_ptr->updated(); |
| 61 | } |
| 62 | |
| 63 | /*! |
| 64 | Removes the legend marker for a line, spline, or scatter series. |
| 65 | */ |
| 66 | QXYLegendMarker::~QXYLegendMarker() |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | /*! |
| 71 | \internal |
| 72 | */ |
| 73 | QXYLegendMarker::QXYLegendMarker(QXYLegendMarkerPrivate &d, QObject *parent) : |
| 74 | QLegendMarker(d, parent) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | /*! |
| 79 | \reimp |
| 80 | */ |
| 81 | QXYSeries* QXYLegendMarker::series() |
| 82 | { |
| 83 | Q_D(QXYLegendMarker); |
| 84 | return d->m_series; |
| 85 | } |
| 86 | |
| 87 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 88 | |
| 89 | QXYLegendMarkerPrivate::QXYLegendMarkerPrivate(QXYLegendMarker *q, QXYSeries *series, QLegend *legend) : |
| 90 | QLegendMarkerPrivate(q,legend), |
| 91 | q_ptr(q), |
| 92 | m_series(series) |
| 93 | { |
| 94 | QObject::connect(sender: m_series, SIGNAL(nameChanged()), receiver: this, SLOT(updated())); |
| 95 | QObject::connect(sender: m_series->d_func(), SIGNAL(updated()), receiver: this, SLOT(updated())); |
| 96 | } |
| 97 | |
| 98 | QXYLegendMarkerPrivate::~QXYLegendMarkerPrivate() |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | QAbstractSeries* QXYLegendMarkerPrivate::series() |
| 103 | { |
| 104 | return m_series; |
| 105 | } |
| 106 | |
| 107 | QObject* QXYLegendMarkerPrivate::relatedObject() |
| 108 | { |
| 109 | return m_series; |
| 110 | } |
| 111 | |
| 112 | void QXYLegendMarkerPrivate::updated() |
| 113 | { |
| 114 | bool labelChanged = false; |
| 115 | bool brushChanged = false; |
| 116 | |
| 117 | if (!m_customLabel && (m_item->label() != m_series->name())) { |
| 118 | m_item->setLabel(m_series->name()); |
| 119 | labelChanged = true; |
| 120 | } |
| 121 | |
| 122 | if (m_series->type()== QAbstractSeries::SeriesTypeScatter) { |
| 123 | if (!m_customBrush && (m_item->brush() != m_series->brush())) { |
| 124 | m_item->setBrush(m_series->brush()); |
| 125 | brushChanged = true; |
| 126 | } |
| 127 | if (m_item->effectiveMarkerShape() == QLegend::MarkerShapeFromSeries) { |
| 128 | QScatterSeries *scatter = static_cast<QScatterSeries *>(m_series); |
| 129 | if (scatter) { |
| 130 | const bool shapeChangeNeeded = |
| 131 | (scatter->markerShape() == QScatterSeries::MarkerShapeCircle |
| 132 | && m_item->itemType() != LegendMarkerItem::TypeCircle) |
| 133 | || (scatter->markerShape() == QScatterSeries::MarkerShapeRectangle |
| 134 | && m_item->itemType() != LegendMarkerItem::TypeRect); |
| 135 | if (shapeChangeNeeded || scatter->markerSize() != m_item->markerRect().width()) |
| 136 | m_item->updateMarkerShapeAndSize(); |
| 137 | } |
| 138 | } |
| 139 | } else { |
| 140 | QBrush emptyBrush; |
| 141 | if (!m_customBrush |
| 142 | && (m_item->brush() == emptyBrush |
| 143 | || m_item->brush().color() != m_series->pen().color())) { |
| 144 | m_item->setBrush(QBrush(m_series->pen().color())); |
| 145 | brushChanged = true; |
| 146 | } |
| 147 | } |
| 148 | m_item->setSeriesBrush(m_series->brush()); |
| 149 | m_item->setSeriesPen(m_series->pen()); |
| 150 | |
| 151 | invalidateLegend(); |
| 152 | |
| 153 | if (labelChanged) |
| 154 | emit q_ptr->labelChanged(); |
| 155 | if (brushChanged) |
| 156 | emit q_ptr->brushChanged(); |
| 157 | } |
| 158 | |
| 159 | QT_CHARTS_END_NAMESPACE |
| 160 | |
| 161 | #include "moc_qxylegendmarker.cpp" |
| 162 | #include "moc_qxylegendmarker_p.cpp" |
| 163 | |