summaryrefslogtreecommitdiffstats
path: root/src/charts/legend/qlegendmarker.cpp
blob: 6768a765fe40ac3fb6e72c405e93f40fc47b6ffb (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/****************************************************************************
**
** Copyright (C) 2016 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 <QtCharts/QLegendMarker>
#include <private/qlegendmarker_p.h>
#include <private/legendmarkeritem_p.h>
#include <QtCharts/QLegend>
#include <private/qlegend_p.h>
#include <private/legendlayout_p.h>
#include <QtGui/QFontMetrics>
#include <QtWidgets/QGraphicsSceneEvent>
#include <QtCharts/QAbstractSeries>

QT_CHARTS_BEGIN_NAMESPACE

/*!
    \class QLegendMarker
    \inmodule QtCharts
    \brief The QLegendMarker class is an abstract object that can be used to access
    markers within a legend.

    A legend marker consists of an icon and a label. The icon color corresponds to the color
    used to draw a series and the label displays the name of the series (or the label of the
    slice for a pie series or bar set for a bar series). A legend marker is always related to
    one series, slice, or bar set.

    \image examples_percentbarchart_legend.png

    \sa QLegend
*/
/*!
    \enum QLegendMarker::LegendMarkerType
    \since 5.8

    The type of the legend marker object.

    \value LegendMarkerTypeArea
           A legend marker for an area series.
    \value LegendMarkerTypeBar
           A legend marker for a bar set.
    \value LegendMarkerTypePie
           A legend marker for a pie slice.
    \value LegendMarkerTypeXY
           A legend marker for a line, spline, or scatter series.
    \value LegendMarkerTypeBoxPlot
           A legend marker for a box plot series.
    \value LegendMarkerTypeCandlestick
           A legend marker for a candlestick series.
*/

/*!
    \fn virtual LegendMarkerType QLegendMarker::type() = 0;
    Returns the type of the legend marker for the related series, pie slice, or bar set.

    \sa LegendMarkerType
*/

/*!
    \fn virtual QAbstractSeries* QLegendMarker::series() = 0;
    Returns a pointer to the series that is related to this legend marker. A legend marker
    is always related to a series.
*/

/*!
  \fn void QLegendMarker::clicked();
  This signal is emitted when the legend marker is clicked.
*/

/*!
  \fn void QLegendMarker::hovered(bool status);
  This signal is emitted when a mouse is hovered over the legend marker.
  When the mouse moves over the marker, \a status turns \c true, and when
  the mouse moves away again, it turns \c false.
*/

/*!
    \fn void QLegendMarker::labelChanged()
    This signal is emitted when the label of the legend marker has changed.
*/

/*!
    \fn void QLegendMarker::labelBrushChanged()
    This signal is emitted when the label brush of the legend marker has changed.
*/

/*!
    \fn void QLegendMarker::fontChanged()
    This signal is emitted when the (label) font of the legend marker has changed.
*/

/*!
    \fn void QLegendMarker::penChanged()
    This signal is emitted when the pen of the legend marker has changed.
*/

/*!
    \fn void QLegendMarker::brushChanged()
    This signal is emitted when the brush of the legend marker has changed.
*/

/*!
    \fn void QLegendMarker::visibleChanged()
    This signal is emitted when the visibility of the legend marker has changed.
*/

/*!
    \property QLegendMarker::label
    \brief The text shown in the legend for a legend marker.
*/

/*!
    \property QLegendMarker::labelBrush
    \brief The brush of the label.
*/

/*!
    \property QLegendMarker::font
    \brief The font of the label.
*/

/*!
    \property QLegendMarker::pen
    \brief The pen used to draw the outline of the icon.
*/

/*!
    \property QLegendMarker::brush
    \brief The brush used to fill the icon.
*/

/*!
    \property QLegendMarker::visible
    \brief The visibility of the legend marker.

    The visibility affects both the legend marker label and the icon.
*/

/*!
    \property QLegendMarker::shape

    The shape of the legend marker. Defaults to QLegend::MarkerShapeDefault, which indicates
    the shape is determined by QLegend::markerShape property.
*/

/*!
    \internal
 */
QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) :
    QObject(parent),
    d_ptr(&d)
{
    d_ptr->m_item->setVisible(d_ptr->series()->isVisible());
}

/*!
    Removes the legend marker.
*/
QLegendMarker::~QLegendMarker()
{
}

/*!
  Returns the label of the marker.
*/
QString QLegendMarker::label() const
{
    return d_ptr->m_item->label();
}

/*!
    Sets the label of the marker to \a label.

    \note Changing the name of a series also changes the label of its marker.
*/
void QLegendMarker::setLabel(const QString &label)
{
    if (label.isEmpty()) {
        d_ptr->m_customLabel = false;
    } else {
        d_ptr->m_customLabel = true;
        d_ptr->m_item->setLabel(label);
    }
}
/*!
    Returns the brush that is used to draw the label.
*/
QBrush QLegendMarker::labelBrush() const
{
    return d_ptr->m_item->labelBrush();
}

/*!
    Sets the the brush used to draw to label to \a brush.
*/
void QLegendMarker::setLabelBrush(const QBrush &brush)
{
    d_ptr->m_item->setLabelBrush(brush);
}

/*!
    Retuns the font of the label.
*/
QFont QLegendMarker::font() const
{
    return d_ptr->m_item->font();
}

/*!
    Sets the font of the label to \a font.
*/
void QLegendMarker::setFont(const QFont &font)
{
    d_ptr->m_item->setFont(font);
}

/*!
    Returns the pen used to draw the outline of the icon.
*/
QPen QLegendMarker::pen() const
{
    return d_ptr->m_item->pen();
}

/*!
    Sets the \a pen used to draw the outline of the icon to \a pen.
*/
void QLegendMarker::setPen(const QPen &pen)
{
    if (pen == QPen(Qt::NoPen)) {
        d_ptr->m_customPen = false;
    } else {
        d_ptr->m_customPen = true;
        d_ptr->m_item->setPen(pen);
    }
}

/*!
    Returns the brush used to fill the icon.
*/
QBrush QLegendMarker::brush() const
{
    return d_ptr->m_item->brush();
}

/*!
    Sets the brush used to fill the icon to \a brush.

    \note Changing the color of the series also changes the color of the icon.
*/
void QLegendMarker::setBrush(const QBrush &brush)
{
    if (brush == QBrush(Qt::NoBrush)) {
        d_ptr->m_customBrush = false;
    } else {
        d_ptr->m_customBrush = true;
        d_ptr->m_item->setBrush(brush);
    }
}

/*!
    Returns the visibility of the marker.
*/
bool QLegendMarker::isVisible() const
{
    return d_ptr->m_item->isVisible();
}

/*!
    Sets the marker's visibility to \a visible.
*/
void QLegendMarker::setVisible(bool visible)
{
    d_ptr->m_item->setVisible(visible);
}

QLegend::MarkerShape QLegendMarker::shape() const
{
    return d_ptr->m_item->markerShape();
}

void QLegendMarker::setShape(QLegend::MarkerShape shape)
{
    if (shape != d_ptr->m_item->markerShape()) {
        d_ptr->m_item->setMarkerShape(shape);
        d_ptr->handleShapeChange();
        emit shapeChanged();
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q, QLegend *legend) :
    m_legend(legend),
    m_customLabel(false),
    m_customBrush(false),
    m_customPen(false),
    q_ptr(q)
{
    m_item = new LegendMarkerItem(this);

    connect(legend, &QLegend::markerShapeChanged, this,
            &QLegendMarkerPrivate::handleShapeChange);
}

QLegendMarkerPrivate::~QLegendMarkerPrivate()
{
    delete m_item;
}

void QLegendMarkerPrivate::invalidateLegend()
{
    m_item->updateGeometry();
    m_legend->d_ptr->m_layout->invalidate();
}

void QLegendMarkerPrivate::invalidateAllItems()
{
    QList<QLegendMarker *> markers = m_legend->markers();
    for (int i = 0; i < markers.size(); i++)
        markers.at(i)->d_ptr->m_item->updateGeometry();
    m_legend->d_ptr->m_layout->invalidate();
}

void QLegendMarkerPrivate::handleShapeChange()
{
    m_item->updateMarkerShapeAndSize();
    m_legend->d_ptr->m_layout->invalidate();
}

QT_CHARTS_END_NAMESPACE

#include "moc_qlegendmarker.cpp"
#include "moc_qlegendmarker_p.cpp"