1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QLEGEND_H
5#define QLEGEND_H
6
7#include <QtCharts/QChartGlobal>
8#include <QtWidgets/QGraphicsWidget>
9#include <QtGui/QPen>
10#include <QtGui/QBrush>
11
12QT_BEGIN_NAMESPACE
13
14class QChart;
15class QLegendPrivate;
16class QLegendMarker;
17class QAbstractSeries;
18
19class Q_CHARTS_EXPORT QLegend : public QGraphicsWidget
20{
21 Q_OBJECT
22 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
23 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
24 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
25 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
26 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
27 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
28 Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged)
29 Q_PROPERTY(bool showToolTips READ showToolTips WRITE setShowToolTips NOTIFY showToolTipsChanged)
30 Q_PROPERTY(MarkerShape markerShape READ markerShape WRITE setMarkerShape NOTIFY markerShapeChanged)
31
32private:
33 explicit QLegend(QChart *chart);
34
35public:
36 enum MarkerShape {
37 MarkerShapeDefault,
38 MarkerShapeRectangle,
39 MarkerShapeCircle,
40 MarkerShapeFromSeries,
41 MarkerShapeRotatedRectangle,
42 MarkerShapeTriangle,
43 MarkerShapeStar,
44 MarkerShapePentagon
45 };
46 Q_ENUMS(MarkerShape)
47
48 ~QLegend();
49
50 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
51
52 void setBrush(const QBrush &brush);
53 QBrush brush() const;
54 void setColor(QColor color);
55 QColor color();
56
57 void setPen(const QPen &pen);
58 QPen pen() const;
59 void setBorderColor(QColor color);
60 QColor borderColor();
61
62 void setFont(const QFont &font);
63 QFont font() const;
64 void setLabelBrush(const QBrush &brush);
65 QBrush labelBrush() const;
66
67 void setLabelColor(QColor color);
68 QColor labelColor() const;
69
70 void setAlignment(Qt::Alignment alignment);
71 Qt::Alignment alignment() const;
72
73 void detachFromChart();
74 void attachToChart();
75 bool isAttachedToChart();
76
77 void setBackgroundVisible(bool visible = true);
78 bool isBackgroundVisible() const;
79
80 QList <QLegendMarker*> markers(QAbstractSeries *series = nullptr) const;
81
82 bool reverseMarkers();
83 void setReverseMarkers(bool reverseMarkers = true);
84
85 bool showToolTips() const;
86 void setShowToolTips(bool show);
87
88 bool isInteractive() const;
89 void setInteractive(bool interactive);
90
91 MarkerShape markerShape() const;
92 void setMarkerShape(MarkerShape shape);
93
94protected:
95 void hideEvent(QHideEvent *event) override;
96 void showEvent(QShowEvent *event) override;
97
98Q_SIGNALS:
99 void backgroundVisibleChanged(bool visible);
100 void colorChanged(QColor color);
101 void borderColorChanged(QColor color);
102 void fontChanged(QFont font);
103 void labelColorChanged(QColor color);
104 void reverseMarkersChanged(bool reverseMarkers);
105 void showToolTipsChanged(bool showToolTips);
106 void markerShapeChanged(MarkerShape shape);
107 Q_REVISION(6, 2) void attachedToChartChanged(bool attachedToChart);
108 void interactiveChanged(bool interactive);
109
110private:
111 QScopedPointer<QLegendPrivate> d_ptr;
112 Q_DISABLE_COPY(QLegend)
113 friend class LegendScroller;
114 friend class LegendLayout;
115 friend class LegendMoveResizeHandler;
116 friend class ChartLayout;
117 friend class LegendMarkerItem;
118 friend class QLegendMarkerPrivate;
119};
120
121QT_END_NAMESPACE
122
123#endif // QLEGEND_H
124

source code of qtcharts/src/charts/legend/qlegend.h