##// END OF EJS Templates
Add support for showing tooltips when the legend text is truncated...
Andy Shaw -
r2864:47032ec192b3
parent child
Show More
@@ -1,201 +1,215
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
4 ** Contact: https://www.qt.io/licensing/
5 **
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
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
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.
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
16 **
17 ** GNU General Public License Usage
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
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
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
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
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
25 **
26 ** $QT_END_LICENSE$
26 ** $QT_END_LICENSE$
27 **
27 **
28 ****************************************************************************/
28 ****************************************************************************/
29
29
30 #include <QtGui/QPainter>
30 #include <QtGui/QPainter>
31 #include <QtWidgets/QGraphicsSceneEvent>
31 #include <QtWidgets/QGraphicsSceneEvent>
32 #include <QtWidgets/QGraphicsTextItem>
32 #include <QtWidgets/QGraphicsTextItem>
33 #include <QtGui/QTextDocument>
33 #include <QtGui/QTextDocument>
34
34
35 #include <QtCharts/QLegend>
35 #include <QtCharts/QLegend>
36 #include <private/qlegend_p.h>
36 #include <private/qlegend_p.h>
37 #include <QtCharts/QLegendMarker>
37 #include <QtCharts/QLegendMarker>
38 #include <private/qlegendmarker_p.h>
38 #include <private/qlegendmarker_p.h>
39 #include <private/legendmarkeritem_p.h>
39 #include <private/legendmarkeritem_p.h>
40 #include <private/chartpresenter_p.h>
40 #include <private/chartpresenter_p.h>
41
41
42 QT_CHARTS_BEGIN_NAMESPACE
42 QT_CHARTS_BEGIN_NAMESPACE
43
43
44 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
44 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
45 QGraphicsObject(parent),
45 QGraphicsObject(parent),
46 m_marker(marker),
46 m_marker(marker),
47 m_markerRect(0,0,10.0,10.0),
47 m_markerRect(0,0,10.0,10.0),
48 m_boundingRect(0,0,0,0),
48 m_boundingRect(0,0,0,0),
49 m_textItem(new QGraphicsTextItem(this)),
49 m_textItem(new QGraphicsTextItem(this)),
50 m_rectItem(new QGraphicsRectItem(this)),
50 m_rectItem(new QGraphicsRectItem(this)),
51 m_margin(3),
51 m_margin(3),
52 m_space(4),
52 m_space(4),
53 m_hovering(false),
53 m_hovering(false),
54 m_pressPos(0, 0)
54 m_pressPos(0, 0)
55 {
55 {
56 m_rectItem->setRect(m_markerRect);
56 m_rectItem->setRect(m_markerRect);
57 m_textItem->document()->setDocumentMargin(ChartPresenter::textMargin());
57 m_textItem->document()->setDocumentMargin(ChartPresenter::textMargin());
58 setAcceptHoverEvents(true);
58 setAcceptHoverEvents(true);
59 }
59 }
60
60
61 LegendMarkerItem::~LegendMarkerItem()
61 LegendMarkerItem::~LegendMarkerItem()
62 {
62 {
63 if (m_hovering) {
63 if (m_hovering) {
64 emit m_marker->q_ptr->hovered(false);
64 emit m_marker->q_ptr->hovered(false);
65 }
65 }
66 }
66 }
67
67
68 void LegendMarkerItem::setPen(const QPen &pen)
68 void LegendMarkerItem::setPen(const QPen &pen)
69 {
69 {
70 m_rectItem->setPen(pen);
70 m_rectItem->setPen(pen);
71 }
71 }
72
72
73 QPen LegendMarkerItem::pen() const
73 QPen LegendMarkerItem::pen() const
74 {
74 {
75 return m_rectItem->pen();
75 return m_rectItem->pen();
76 }
76 }
77
77
78 void LegendMarkerItem::setBrush(const QBrush &brush)
78 void LegendMarkerItem::setBrush(const QBrush &brush)
79 {
79 {
80 m_rectItem->setBrush(brush);
80 m_rectItem->setBrush(brush);
81 }
81 }
82
82
83 QBrush LegendMarkerItem::brush() const
83 QBrush LegendMarkerItem::brush() const
84 {
84 {
85 return m_rectItem->brush();
85 return m_rectItem->brush();
86 }
86 }
87
87
88 void LegendMarkerItem::setFont(const QFont &font)
88 void LegendMarkerItem::setFont(const QFont &font)
89 {
89 {
90 m_textItem->setFont(font);
90 m_textItem->setFont(font);
91 QFontMetrics fn(font);
91 QFontMetrics fn(font);
92 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
92 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
93 updateGeometry();
93 updateGeometry();
94 }
94 }
95
95
96 QFont LegendMarkerItem::font() const
96 QFont LegendMarkerItem::font() const
97 {
97 {
98 return m_textItem->font();
98 return m_textItem->font();
99 }
99 }
100
100
101 void LegendMarkerItem::setLabel(const QString label)
101 void LegendMarkerItem::setLabel(const QString label)
102 {
102 {
103 m_label = label;
103 m_label = label;
104 updateGeometry();
104 updateGeometry();
105 }
105 }
106
106
107 QString LegendMarkerItem::label() const
107 QString LegendMarkerItem::label() const
108 {
108 {
109 return m_label;
109 return m_label;
110 }
110 }
111
111
112 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
112 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
113 {
113 {
114 m_textItem->setDefaultTextColor(brush.color());
114 m_textItem->setDefaultTextColor(brush.color());
115 }
115 }
116
116
117 QBrush LegendMarkerItem::labelBrush() const
117 QBrush LegendMarkerItem::labelBrush() const
118 {
118 {
119 return QBrush(m_textItem->defaultTextColor());
119 return QBrush(m_textItem->defaultTextColor());
120 }
120 }
121
121
122 void LegendMarkerItem::setGeometry(const QRectF &rect)
122 void LegendMarkerItem::setGeometry(const QRectF &rect)
123 {
123 {
124 qreal width = rect.width();
124 qreal width = rect.width();
125 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
125 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
126 QRectF truncatedRect;
126 QRectF truncatedRect;
127 const QString html = ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0),
128 width - x, rect.height(), truncatedRect);
129 m_textItem->setHtml(html);
130 if (m_marker->m_legend->showToolTips() && html != m_label)
131 m_textItem->setToolTip(m_label);
132 else
133 m_textItem->setToolTip(QString());
127
134
128 m_textItem->setHtml(ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0),
129 width - x, rect.height(), truncatedRect));
130 m_textItem->setTextWidth(truncatedRect.width());
135 m_textItem->setTextWidth(truncatedRect.width());
131
136
132 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
137 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
133
138
134 const QRectF &textRect = m_textItem->boundingRect();
139 const QRectF &textRect = m_textItem->boundingRect();
135
140
136 m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2);
141 m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2);
137 m_rectItem->setRect(m_markerRect);
142 m_rectItem->setRect(m_markerRect);
138 // The textMargin adjustments to position are done to make default case rects less blurry with anti-aliasing
143 // The textMargin adjustments to position are done to make default case rects less blurry with anti-aliasing
139 m_rectItem->setPos(m_margin - ChartPresenter::textMargin(), y / 2.0 - m_markerRect.height() / 2.0 + ChartPresenter::textMargin());
144 m_rectItem->setPos(m_margin - ChartPresenter::textMargin(), y / 2.0 - m_markerRect.height() / 2.0 + ChartPresenter::textMargin());
140
145
141 prepareGeometryChange();
146 prepareGeometryChange();
142 m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y);
147 m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y);
143 }
148 }
144
149
145 QRectF LegendMarkerItem::boundingRect() const
150 QRectF LegendMarkerItem::boundingRect() const
146 {
151 {
147 return m_boundingRect;
152 return m_boundingRect;
148 }
153 }
149
154
150 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
155 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
151 {
156 {
152 Q_UNUSED(option)
157 Q_UNUSED(option)
153 Q_UNUSED(widget)
158 Q_UNUSED(widget)
154 Q_UNUSED(painter)
159 Q_UNUSED(painter)
155 }
160 }
156
161
157 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
162 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
158 {
163 {
159 Q_UNUSED(constraint)
164 Q_UNUSED(constraint)
160
165
161 QSizeF sh;
166 QSizeF sh;
162
167
163 switch (which) {
168 switch (which) {
164 case Qt::MinimumSize: {
169 case Qt::MinimumSize: {
165 QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(),
170 QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(),
166 QStringLiteral("..."));
171 QStringLiteral("..."));
167 sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(),
172 sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(),
168 qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin));
173 qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin));
169 break;
174 break;
170 }
175 }
171 case Qt::PreferredSize: {
176 case Qt::PreferredSize: {
172 QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(), m_label);
177 QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(), m_label);
173 sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(),
178 sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(),
174 qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin));
179 qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin));
175 break;
180 break;
176 }
181 }
177 default:
182 default:
178 break;
183 break;
179 }
184 }
180
185
181 return sh;
186 return sh;
182 }
187 }
183
188
184 void LegendMarkerItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
189 void LegendMarkerItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
185 {
190 {
186 Q_UNUSED(event)
191 Q_UNUSED(event)
187 m_hovering = true;
192 m_hovering = true;
188 emit m_marker->q_ptr->hovered(true);
193 emit m_marker->q_ptr->hovered(true);
189 }
194 }
190
195
191 void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
196 void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
192 {
197 {
193 Q_UNUSED(event)
198 Q_UNUSED(event)
194 m_hovering = false;
199 m_hovering = false;
195 emit m_marker->q_ptr->hovered(false);
200 emit m_marker->q_ptr->hovered(false);
196 }
201 }
197
202
203 QString LegendMarkerItem::displayedLabel() const
204 {
205 return m_textItem->toHtml();
206 }
207
208 void LegendMarkerItem::setToolTip(const QString &tip)
209 {
210 m_textItem->setToolTip(tip);
211 }
198
212
199 #include "moc_legendmarkeritem_p.cpp"
213 #include "moc_legendmarkeritem_p.cpp"
200
214
201 QT_CHARTS_END_NAMESPACE
215 QT_CHARTS_END_NAMESPACE
@@ -1,110 +1,112
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
4 ** Contact: https://www.qt.io/licensing/
5 **
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
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
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.
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
16 **
17 ** GNU General Public License Usage
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
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
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
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
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
25 **
26 ** $QT_END_LICENSE$
26 ** $QT_END_LICENSE$
27 **
27 **
28 ****************************************************************************/
28 ****************************************************************************/
29
29
30 // W A R N I N G
30 // W A R N I N G
31 // -------------
31 // -------------
32 //
32 //
33 // This file is not part of the Qt Chart API. It exists purely as an
33 // This file is not part of the Qt Chart API. It exists purely as an
34 // implementation detail. This header file may change from version to
34 // implementation detail. This header file may change from version to
35 // version without notice, or even be removed.
35 // version without notice, or even be removed.
36 //
36 //
37 // We mean it.
37 // We mean it.
38
38
39 #ifndef LEGENDMARKERITEM_P_H
39 #ifndef LEGENDMARKERITEM_P_H
40 #define LEGENDMARKERITEM_P_H
40 #define LEGENDMARKERITEM_P_H
41
41
42 #include <QtCharts/QChartGlobal>
42 #include <QtCharts/QChartGlobal>
43 #include <QGraphicsObject>
43 #include <QGraphicsObject>
44 #include <QtGui/QFont>
44 #include <QtGui/QFont>
45 #include <QtGui/QBrush>
45 #include <QtGui/QBrush>
46 #include <QtGui/QPen>
46 #include <QtGui/QPen>
47 #include <QtWidgets/QGraphicsTextItem>
47 #include <QtWidgets/QGraphicsTextItem>
48 #include <QtWidgets/QGraphicsLayoutItem>
48 #include <QtWidgets/QGraphicsLayoutItem>
49
49
50 QT_CHARTS_BEGIN_NAMESPACE
50 QT_CHARTS_BEGIN_NAMESPACE
51
51
52 class QLegendMarkerPrivate;
52 class QLegendMarkerPrivate;
53
53
54 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
54 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
55 {
55 {
56 Q_OBJECT
56 Q_OBJECT
57 Q_INTERFACES(QGraphicsLayoutItem)
57 Q_INTERFACES(QGraphicsLayoutItem)
58 public:
58 public:
59 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
59 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
60 ~LegendMarkerItem();
60 ~LegendMarkerItem();
61
61
62 void setPen(const QPen &pen);
62 void setPen(const QPen &pen);
63 QPen pen() const;
63 QPen pen() const;
64
64
65 void setBrush(const QBrush &brush);
65 void setBrush(const QBrush &brush);
66 QBrush brush() const;
66 QBrush brush() const;
67
67
68 void setFont(const QFont &font);
68 void setFont(const QFont &font);
69 QFont font() const;
69 QFont font() const;
70
70
71 void setLabel(const QString label);
71 void setLabel(const QString label);
72 QString label() const;
72 QString label() const;
73
73
74 void setLabelBrush(const QBrush &brush);
74 void setLabelBrush(const QBrush &brush);
75 QBrush labelBrush() const;
75 QBrush labelBrush() const;
76
76
77 void setGeometry(const QRectF &rect);
77 void setGeometry(const QRectF &rect);
78 QRectF boundingRect() const;
78 QRectF boundingRect() const;
79
79
80 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
80 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
81 QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint) const;
81 QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint) const;
82
82
83 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
83 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
84 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
84 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
85
85
86 QString displayedLabel() const;
87 void setToolTip(const QString &tooltip);
86 protected:
88 protected:
87 QLegendMarkerPrivate *m_marker; // Knows
89 QLegendMarkerPrivate *m_marker; // Knows
88 QRectF m_markerRect;
90 QRectF m_markerRect;
89 QRectF m_boundingRect;
91 QRectF m_boundingRect;
90 QGraphicsTextItem *m_textItem;
92 QGraphicsTextItem *m_textItem;
91 QGraphicsRectItem *m_rectItem;
93 QGraphicsRectItem *m_rectItem;
92 qreal m_margin;
94 qreal m_margin;
93 qreal m_space;
95 qreal m_space;
94 QString m_label;
96 QString m_label;
95
97
96 QBrush m_labelBrush;
98 QBrush m_labelBrush;
97 QPen m_pen;
99 QPen m_pen;
98 QBrush m_brush;
100 QBrush m_brush;
99 bool m_hovering;
101 bool m_hovering;
100
102
101 QPointF m_pressPos;
103 QPointF m_pressPos;
102
104
103 friend class QLegendMarker;
105 friend class QLegendMarker;
104 friend class QLegendMarkerPrivate;
106 friend class QLegendMarkerPrivate;
105 friend class LegendLayout;
107 friend class LegendLayout;
106 };
108 };
107
109
108 QT_CHARTS_END_NAMESPACE
110 QT_CHARTS_END_NAMESPACE
109
111
110 #endif // LEGENDMARKERITEM_P_H
112 #endif // LEGENDMARKERITEM_P_H
@@ -1,658 +1,710
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
4 ** Contact: https://www.qt.io/licensing/
5 **
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
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
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.
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
16 **
17 ** GNU General Public License Usage
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
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
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
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
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
25 **
26 ** $QT_END_LICENSE$
26 ** $QT_END_LICENSE$
27 **
27 **
28 ****************************************************************************/
28 ****************************************************************************/
29
29
30 #include <QtCharts/QLegend>
30 #include <QtCharts/QLegend>
31 #include <private/qlegend_p.h>
31 #include <private/qlegend_p.h>
32 #include <QtCharts/QAbstractSeries>
32 #include <QtCharts/QAbstractSeries>
33 #include <private/qabstractseries_p.h>
33 #include <private/qabstractseries_p.h>
34 #include <private/qchart_p.h>
34 #include <private/qchart_p.h>
35 #include <private/legendlayout_p.h>
35 #include <private/legendlayout_p.h>
36 #include <private/chartpresenter_p.h>
36 #include <private/chartpresenter_p.h>
37 #include <private/abstractchartlayout_p.h>
37 #include <private/abstractchartlayout_p.h>
38 #include <QtCharts/QLegendMarker>
38 #include <QtCharts/QLegendMarker>
39 #include <private/qlegendmarker_p.h>
39 #include <private/qlegendmarker_p.h>
40 #include <private/legendmarkeritem_p.h>
40 #include <private/legendmarkeritem_p.h>
41 #include <private/chartdataset_p.h>
41 #include <private/chartdataset_p.h>
42 #include <QtGui/QPainter>
42 #include <QtGui/QPainter>
43 #include <QtGui/QPen>
43 #include <QtGui/QPen>
44 #include <QtWidgets/QGraphicsItemGroup>
44 #include <QtWidgets/QGraphicsItemGroup>
45
45
46 QT_CHARTS_BEGIN_NAMESPACE
46 QT_CHARTS_BEGIN_NAMESPACE
47
47
48 /*!
48 /*!
49 \class QLegend
49 \class QLegend
50 \inmodule Qt Charts
50 \inmodule Qt Charts
51 \inherits QGraphicsWidget
51 \inherits QGraphicsWidget
52 \brief Legend object.
52 \brief Legend object.
53
53
54 QLegend is a graphical object for displaying the legend of the chart. Legend state is updated by QChart, when
54 QLegend is a graphical object for displaying the legend of the chart. Legend state is updated by QChart, when
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
56 handle the drawing manually.
56 handle the drawing manually.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
58
58
59 \image examples_percentbarchart_legend.png
59 \image examples_percentbarchart_legend.png
60
60
61 \sa QChart
61 \sa QChart
62 */
62 */
63 /*!
63 /*!
64 \qmltype Legend
64 \qmltype Legend
65 \instantiates QLegend
65 \instantiates QLegend
66 \inqmlmodule QtCharts
66 \inqmlmodule QtCharts
67
67
68 \brief Legend is part of Qt Chart QML API.
68 \brief Legend is part of Qt Chart QML API.
69
69
70 Legend is a graphical object, which displays legend of the chart. Legend state is updated by
70 Legend is a graphical object, which displays legend of the chart. Legend state is updated by
71 ChartView, when series have been changed. Legend is used via ChartView class. For example:
71 ChartView, when series have been changed. Legend is used via ChartView class. For example:
72 \code
72 \code
73 ChartView {
73 ChartView {
74 legend.visible: true
74 legend.visible: true
75 legend.alignment: Qt.AlignBottom
75 legend.alignment: Qt.AlignBottom
76 // Add a few series...
76 // Add a few series...
77 }
77 }
78 \endcode
78 \endcode
79
79
80 \image examples_percentbarchart_legend.png
80 \image examples_percentbarchart_legend.png
81
81
82 Please note that there is no QML API available for modifying legend markers, unlike in the Qt
82 Please note that there is no QML API available for modifying legend markers, unlike in the Qt
83 API of Charts. The use case of modifying markers can be implemented for example by creating your
83 API of Charts. The use case of modifying markers can be implemented for example by creating your
84 own custom legend. For an example on how to do this,
84 own custom legend. For an example on how to do this,
85 see \l {qmlcustomlegend}{Qml Custom Example} application.
85 see \l {qmlcustomlegend}{Qml Custom Example} application.
86 */
86 */
87
87
88 /*!
88 /*!
89 \property QLegend::alignment
89 \property QLegend::alignment
90 \brief The alignment of the legend.
90 \brief The alignment of the legend.
91
91
92 Legend paints on the defined position in the chart. The following alignments are supported:
92 Legend paints on the defined position in the chart. The following alignments are supported:
93 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
93 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
94 */
94 */
95 /*!
95 /*!
96 \qmlproperty Qt.Alignment Legend::alignment
96 \qmlproperty Qt.Alignment Legend::alignment
97 \brief The alignment of the legend.
97 \brief The alignment of the legend.
98
98
99 Legend paints on the defined position in the chart. The following alignments are supported:
99 Legend paints on the defined position in the chart. The following alignments are supported:
100 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
100 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
101 */
101 */
102
102
103 /*!
103 /*!
104 \property QLegend::backgroundVisible
104 \property QLegend::backgroundVisible
105 Whether the legend background is visible or not.
105 Whether the legend background is visible or not.
106 */
106 */
107 /*!
107 /*!
108 \qmlproperty bool Legend::backgroundVisible
108 \qmlproperty bool Legend::backgroundVisible
109 Whether the legend background is visible or not.
109 Whether the legend background is visible or not.
110 */
110 */
111
111
112 /*!
112 /*!
113 \qmlproperty bool Legend::visible
113 \qmlproperty bool Legend::visible
114 \brief Whether the legend is visible or not.
114 \brief Whether the legend is visible or not.
115
115
116 By default, this property is \c true.
116 By default, this property is \c true.
117 \sa QGraphicsObject::visible
117 \sa QGraphicsObject::visible
118 */
118 */
119
119
120 /*!
120 /*!
121 \property QLegend::color
121 \property QLegend::color
122 The color of the legend, i.e. the background (brush) color. Note that if you change the color
122 The color of the legend, i.e. the background (brush) color. Note that if you change the color
123 of the legend, the style of the legend brush is set to Qt::SolidPattern.
123 of the legend, the style of the legend brush is set to Qt::SolidPattern.
124 */
124 */
125 /*!
125 /*!
126 \qmlproperty color Legend::color
126 \qmlproperty color Legend::color
127 The color of the legend, i.e. the background (brush) color.
127 The color of the legend, i.e. the background (brush) color.
128 */
128 */
129
129
130 /*!
130 /*!
131 \property QLegend::borderColor
131 \property QLegend::borderColor
132 The border color of the legend, i.e. the line color.
132 The border color of the legend, i.e. the line color.
133 */
133 */
134 /*!
134 /*!
135 \qmlproperty color Legend::borderColor
135 \qmlproperty color Legend::borderColor
136 The border color of the legend, i.e. the line color.
136 The border color of the legend, i.e. the line color.
137 */
137 */
138
138
139 /*!
139 /*!
140 \property QLegend::font
140 \property QLegend::font
141 The font of markers used by legend.
141 The font of markers used by legend.
142 */
142 */
143 /*!
143 /*!
144 \qmlproperty Font Legend::font
144 \qmlproperty Font Legend::font
145 The font of markers used by legend.
145 The font of markers used by legend.
146 */
146 */
147
147
148 /*!
148 /*!
149 \property QLegend::labelColor
149 \property QLegend::labelColor
150 The color of brush used to draw labels.
150 The color of brush used to draw labels.
151 */
151 */
152 /*!
152 /*!
153 \qmlproperty color Legend::labelColor
153 \qmlproperty color Legend::labelColor
154 The color of brush used to draw labels.
154 The color of brush used to draw labels.
155 */
155 */
156
156
157 /*!
157 /*!
158 \property QLegend::reverseMarkers
158 \property QLegend::reverseMarkers
159 Whether reverse order is used for the markers in legend or not. False by default.
159 Whether reverse order is used for the markers in legend or not. False by default.
160 */
160 */
161 /*!
161 /*!
162 \qmlproperty bool Legend::reverseMarkers
162 \qmlproperty bool Legend::reverseMarkers
163 Whether reverse order is used for the markers in legend or not. False by default.
163 Whether reverse order is used for the markers in legend or not. False by default.
164 */
164 */
165
165
166 /*!
166 /*!
167 \property QLegend::showToolTips
168 Whether tooltips are shown when the text is truncated. This is false by default.
169
170 This will not have any effect when used in QML.
171 */
172
173 /*!
174 \qmlproperty bool Legend::showToolTips
175 Whether tooltips are shown when the text is truncated. This is false by default.
176 This currently has no effect as there is no support for tooltips in QML.
177 */
178
179 /*!
167 \fn void QLegend::backgroundVisibleChanged(bool)
180 \fn void QLegend::backgroundVisibleChanged(bool)
168 The visibility of the legend background changed to \a visible.
181 The visibility of the legend background changed to \a visible.
169 */
182 */
170
183
171 /*!
184 /*!
172 \fn void QLegend::colorChanged(QColor)
185 \fn void QLegend::colorChanged(QColor)
173 The color of the legend background changed to \a color.
186 The color of the legend background changed to \a color.
174 */
187 */
175
188
176 /*!
189 /*!
177 \fn void QLegend::borderColorChanged(QColor)
190 \fn void QLegend::borderColorChanged(QColor)
178 The border color of the legend background changed to \a color.
191 The border color of the legend background changed to \a color.
179 */
192 */
180
193
181 /*!
194 /*!
182 \fn void QLegend::fontChanged(QFont)
195 \fn void QLegend::fontChanged(QFont)
183 The font of markers of the legend changed to \a font.
196 The font of markers of the legend changed to \a font.
184 */
197 */
185
198
186 /*!
199 /*!
187 \fn void QLegend::labelColorChanged(QColor color)
200 \fn void QLegend::labelColorChanged(QColor color)
188 This signal is emitted when the color of brush used to draw labels has changed to \a color.
201 This signal is emitted when the color of brush used to draw labels has changed to \a color.
189 */
202 */
190
203
191 /*!
204 /*!
192 \fn void QLegend::reverseMarkersChanged(bool)
205 \fn void QLegend::reverseMarkersChanged(bool)
193 The use of reverse order for the markers in legend is changed to \a reverseMarkers.
206 The use of reverse order for the markers in legend is changed to \a reverseMarkers.
194 */
207 */
195
208
209 /*!
210 \fn void QLegend::showToolTipsChanged(bool showToolTips)
211 This signal is emitted when the visibility of tooltips is changed to \a showToolTips.
212 */
213
196 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
214 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
197 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
215 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
198 {
216 {
199 setZValue(ChartPresenter::LegendZValue);
217 setZValue(ChartPresenter::LegendZValue);
200 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
218 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
201 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
219 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
202 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
220 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
203 setLayout(d_ptr->m_layout);
221 setLayout(d_ptr->m_layout);
204 }
222 }
205
223
206 /*!
224 /*!
207 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
225 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
208 */
226 */
209 QLegend::~QLegend()
227 QLegend::~QLegend()
210 {
228 {
211 }
229 }
212
230
213 /*!
231 /*!
214 \internal
232 \internal
215 */
233 */
216 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
234 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
217 {
235 {
218 Q_UNUSED(option)
236 Q_UNUSED(option)
219 Q_UNUSED(widget)
237 Q_UNUSED(widget)
220
238
221 if (!d_ptr->m_backgroundVisible)
239 if (!d_ptr->m_backgroundVisible)
222 return;
240 return;
223
241
224 painter->setOpacity(opacity());
242 painter->setOpacity(opacity());
225 painter->setPen(d_ptr->m_pen);
243 painter->setPen(d_ptr->m_pen);
226 painter->setBrush(d_ptr->m_brush);
244 painter->setBrush(d_ptr->m_brush);
227 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
245 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
228 }
246 }
229
247
230
248
231 /*!
249 /*!
232 Sets the \a brush of legend. Brush affects the background of legend.
250 Sets the \a brush of legend. Brush affects the background of legend.
233 */
251 */
234 void QLegend::setBrush(const QBrush &brush)
252 void QLegend::setBrush(const QBrush &brush)
235 {
253 {
236 if (d_ptr->m_brush != brush) {
254 if (d_ptr->m_brush != brush) {
237 d_ptr->m_brush = brush;
255 d_ptr->m_brush = brush;
238 update();
256 update();
239 emit colorChanged(brush.color());
257 emit colorChanged(brush.color());
240 }
258 }
241 }
259 }
242
260
243 /*!
261 /*!
244 Returns the brush used by legend.
262 Returns the brush used by legend.
245 */
263 */
246 QBrush QLegend::brush() const
264 QBrush QLegend::brush() const
247 {
265 {
248 if (d_ptr->m_brush == QChartPrivate::defaultBrush())
266 if (d_ptr->m_brush == QChartPrivate::defaultBrush())
249 return QBrush();
267 return QBrush();
250 else
268 else
251 return d_ptr->m_brush;
269 return d_ptr->m_brush;
252 }
270 }
253
271
254 void QLegend::setColor(QColor color)
272 void QLegend::setColor(QColor color)
255 {
273 {
256 QBrush b = brush();
274 QBrush b = brush();
257 if (b.style() != Qt::SolidPattern || b.color() != color) {
275 if (b.style() != Qt::SolidPattern || b.color() != color) {
258 b.setStyle(Qt::SolidPattern);
276 b.setStyle(Qt::SolidPattern);
259 b.setColor(color);
277 b.setColor(color);
260 setBrush(b);
278 setBrush(b);
261 }
279 }
262 }
280 }
263
281
264 QColor QLegend::color()
282 QColor QLegend::color()
265 {
283 {
266 return d_ptr->m_brush.color();
284 return d_ptr->m_brush.color();
267 }
285 }
268
286
269 /*!
287 /*!
270 Sets the \a pen of legend. Pen affects the legend borders.
288 Sets the \a pen of legend. Pen affects the legend borders.
271 */
289 */
272 void QLegend::setPen(const QPen &pen)
290 void QLegend::setPen(const QPen &pen)
273 {
291 {
274 if (d_ptr->m_pen != pen) {
292 if (d_ptr->m_pen != pen) {
275 d_ptr->m_pen = pen;
293 d_ptr->m_pen = pen;
276 update();
294 update();
277 emit borderColorChanged(pen.color());
295 emit borderColorChanged(pen.color());
278 }
296 }
279 }
297 }
280
298
281 /*!
299 /*!
282 Returns the pen used by legend.
300 Returns the pen used by legend.
283 */
301 */
284
302
285 QPen QLegend::pen() const
303 QPen QLegend::pen() const
286 {
304 {
287 if (d_ptr->m_pen == QChartPrivate::defaultPen())
305 if (d_ptr->m_pen == QChartPrivate::defaultPen())
288 return QPen();
306 return QPen();
289 else
307 else
290 return d_ptr->m_pen;
308 return d_ptr->m_pen;
291 }
309 }
292
310
293 void QLegend::setFont(const QFont &font)
311 void QLegend::setFont(const QFont &font)
294 {
312 {
295 if (d_ptr->m_font != font) {
313 if (d_ptr->m_font != font) {
296 // Hide items to avoid flickering
314 // Hide items to avoid flickering
297 d_ptr->items()->setVisible(false);
315 d_ptr->items()->setVisible(false);
298 d_ptr->m_font = font;
316 d_ptr->m_font = font;
299 foreach (QLegendMarker *marker, d_ptr->markers()) {
317 foreach (QLegendMarker *marker, d_ptr->markers()) {
300 marker->setFont(d_ptr->m_font);
318 marker->setFont(d_ptr->m_font);
301 }
319 }
302 layout()->invalidate();
320 layout()->invalidate();
303 emit fontChanged(font);
321 emit fontChanged(font);
304 }
322 }
305 }
323 }
306
324
307 QFont QLegend::font() const
325 QFont QLegend::font() const
308 {
326 {
309 return d_ptr->m_font;
327 return d_ptr->m_font;
310 }
328 }
311
329
312 void QLegend::setBorderColor(QColor color)
330 void QLegend::setBorderColor(QColor color)
313 {
331 {
314 QPen p = pen();
332 QPen p = pen();
315 if (p.color() != color) {
333 if (p.color() != color) {
316 p.setColor(color);
334 p.setColor(color);
317 setPen(p);
335 setPen(p);
318 }
336 }
319 }
337 }
320
338
321 QColor QLegend::borderColor()
339 QColor QLegend::borderColor()
322 {
340 {
323 return d_ptr->m_pen.color();
341 return d_ptr->m_pen.color();
324 }
342 }
325
343
326 /*!
344 /*!
327 Set brush used to draw labels to \a brush.
345 Set brush used to draw labels to \a brush.
328 */
346 */
329 void QLegend::setLabelBrush(const QBrush &brush)
347 void QLegend::setLabelBrush(const QBrush &brush)
330 {
348 {
331 if (d_ptr->m_labelBrush != brush) {
349 if (d_ptr->m_labelBrush != brush) {
332 d_ptr->m_labelBrush = brush;
350 d_ptr->m_labelBrush = brush;
333 foreach (QLegendMarker *marker, d_ptr->markers()) {
351 foreach (QLegendMarker *marker, d_ptr->markers()) {
334 marker->setLabelBrush(d_ptr->m_labelBrush);
352 marker->setLabelBrush(d_ptr->m_labelBrush);
335 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
353 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
336 // instead of mapping it from label brush color
354 // instead of mapping it from label brush color
337 marker->setPen(brush.color());
355 marker->setPen(brush.color());
338 }
356 }
339 emit labelColorChanged(brush.color());
357 emit labelColorChanged(brush.color());
340 }
358 }
341 }
359 }
342
360
343 /*!
361 /*!
344 Brush used to draw labels.
362 Brush used to draw labels.
345 */
363 */
346 QBrush QLegend::labelBrush() const
364 QBrush QLegend::labelBrush() const
347 {
365 {
348 if (d_ptr->m_labelBrush == QChartPrivate::defaultBrush())
366 if (d_ptr->m_labelBrush == QChartPrivate::defaultBrush())
349 return QBrush();
367 return QBrush();
350 else
368 else
351 return d_ptr->m_labelBrush;
369 return d_ptr->m_labelBrush;
352 }
370 }
353
371
354 void QLegend::setLabelColor(QColor color)
372 void QLegend::setLabelColor(QColor color)
355 {
373 {
356 QBrush b = labelBrush();
374 QBrush b = labelBrush();
357 if (b.style() != Qt::SolidPattern || b.color() != color) {
375 if (b.style() != Qt::SolidPattern || b.color() != color) {
358 b.setStyle(Qt::SolidPattern);
376 b.setStyle(Qt::SolidPattern);
359 b.setColor(color);
377 b.setColor(color);
360 setLabelBrush(b);
378 setLabelBrush(b);
361 }
379 }
362 }
380 }
363
381
364 QColor QLegend::labelColor() const
382 QColor QLegend::labelColor() const
365 {
383 {
366 return d_ptr->m_labelBrush.color();
384 return d_ptr->m_labelBrush.color();
367 }
385 }
368
386
369
387
370 void QLegend::setAlignment(Qt::Alignment alignment)
388 void QLegend::setAlignment(Qt::Alignment alignment)
371 {
389 {
372 if (d_ptr->m_alignment != alignment) {
390 if (d_ptr->m_alignment != alignment) {
373 d_ptr->m_alignment = alignment;
391 d_ptr->m_alignment = alignment;
374 layout()->invalidate();
392 layout()->invalidate();
375 }
393 }
376 }
394 }
377
395
378 Qt::Alignment QLegend::alignment() const
396 Qt::Alignment QLegend::alignment() const
379 {
397 {
380 return d_ptr->m_alignment;
398 return d_ptr->m_alignment;
381 }
399 }
382
400
383 /*!
401 /*!
384 Detaches the legend from chart. Chart won't change layout of the legend.
402 Detaches the legend from chart. Chart won't change layout of the legend.
385 */
403 */
386 void QLegend::detachFromChart()
404 void QLegend::detachFromChart()
387 {
405 {
388 d_ptr->m_attachedToChart = false;
406 d_ptr->m_attachedToChart = false;
389 // layout()->invalidate();
407 // layout()->invalidate();
390 d_ptr->m_chart->layout()->invalidate();
408 d_ptr->m_chart->layout()->invalidate();
391 setParent(0);
409 setParent(0);
392
410
393 }
411 }
394
412
395 /*!
413 /*!
396 Attaches the legend to chart. Chart may change layout of the legend.
414 Attaches the legend to chart. Chart may change layout of the legend.
397 */
415 */
398 void QLegend::attachToChart()
416 void QLegend::attachToChart()
399 {
417 {
400 d_ptr->m_attachedToChart = true;
418 d_ptr->m_attachedToChart = true;
401 // layout()->invalidate();
419 // layout()->invalidate();
402 d_ptr->m_chart->layout()->invalidate();
420 d_ptr->m_chart->layout()->invalidate();
403 setParent(d_ptr->m_chart);
421 setParent(d_ptr->m_chart);
404 }
422 }
405
423
406 /*!
424 /*!
407 Returns true, if legend is attached to chart.
425 Returns true, if legend is attached to chart.
408 */
426 */
409 bool QLegend::isAttachedToChart()
427 bool QLegend::isAttachedToChart()
410 {
428 {
411 return d_ptr->m_attachedToChart;
429 return d_ptr->m_attachedToChart;
412 }
430 }
413
431
414 /*!
432 /*!
415 Sets the visibility of legend background to \a visible
433 Sets the visibility of legend background to \a visible
416 */
434 */
417 void QLegend::setBackgroundVisible(bool visible)
435 void QLegend::setBackgroundVisible(bool visible)
418 {
436 {
419 if (d_ptr->m_backgroundVisible != visible) {
437 if (d_ptr->m_backgroundVisible != visible) {
420 d_ptr->m_backgroundVisible = visible;
438 d_ptr->m_backgroundVisible = visible;
421 update();
439 update();
422 emit backgroundVisibleChanged(visible);
440 emit backgroundVisibleChanged(visible);
423 }
441 }
424 }
442 }
425
443
426 /*!
444 /*!
427 Returns the visibility of legend background
445 Returns the visibility of legend background
428 */
446 */
429 bool QLegend::isBackgroundVisible() const
447 bool QLegend::isBackgroundVisible() const
430 {
448 {
431 return d_ptr->m_backgroundVisible;
449 return d_ptr->m_backgroundVisible;
432 }
450 }
433
451
434 /*!
452 /*!
435 Returns the list of markers in legend. The list can be filtered with \a series parameter.
453 Returns the list of markers in legend. The list can be filtered with \a series parameter.
436 If \a series is given, only markers related to that series are returned.
454 If \a series is given, only markers related to that series are returned.
437 */
455 */
438 QList<QLegendMarker*> QLegend::markers(QAbstractSeries *series) const
456 QList<QLegendMarker*> QLegend::markers(QAbstractSeries *series) const
439 {
457 {
440 return d_ptr->markers(series);
458 return d_ptr->markers(series);
441 }
459 }
442
460
443 bool QLegend::reverseMarkers()
461 bool QLegend::reverseMarkers()
444 {
462 {
445 return d_ptr->m_reverseMarkers;
463 return d_ptr->m_reverseMarkers;
446 }
464 }
447
465
448 void QLegend::setReverseMarkers(bool reverseMarkers)
466 void QLegend::setReverseMarkers(bool reverseMarkers)
449 {
467 {
450 if (d_ptr->m_reverseMarkers != reverseMarkers) {
468 if (d_ptr->m_reverseMarkers != reverseMarkers) {
451 d_ptr->m_reverseMarkers = reverseMarkers;
469 d_ptr->m_reverseMarkers = reverseMarkers;
452 layout()->invalidate();
470 layout()->invalidate();
453 emit reverseMarkersChanged(reverseMarkers);
471 emit reverseMarkersChanged(reverseMarkers);
454 }
472 }
455 }
473 }
456
474
457 /*!
475 /*!
476 Returns whether the tooltips are shown or not for the legend labels
477 when they are elided.
478 */
479
480 bool QLegend::showToolTips() const
481 {
482 return d_ptr->m_showToolTips;
483 }
484
485 /*!
486 When \a show is true, the legend labels will show a tooltip when
487 the mouse hovers over them if the label itself is shown elided.
488 This is false by default.
489 */
490
491 void QLegend::setShowToolTips(bool show)
492 {
493 if (d_ptr->m_showToolTips != show) {
494 d_ptr->m_showToolTips = show;
495 d_ptr->updateToolTips();
496 emit showToolTipsChanged(show);
497 }
498 }
499
500 /*!
458 \internal \a event see QGraphicsWidget for details
501 \internal \a event see QGraphicsWidget for details
459 */
502 */
460 void QLegend::hideEvent(QHideEvent *event)
503 void QLegend::hideEvent(QHideEvent *event)
461 {
504 {
462 if (isAttachedToChart())
505 if (isAttachedToChart())
463 d_ptr->m_presenter->layout()->invalidate();
506 d_ptr->m_presenter->layout()->invalidate();
464 QGraphicsWidget::hideEvent(event);
507 QGraphicsWidget::hideEvent(event);
465 }
508 }
466 /*!
509 /*!
467 \internal \a event see QGraphicsWidget for details
510 \internal \a event see QGraphicsWidget for details
468 */
511 */
469 void QLegend::showEvent(QShowEvent *event)
512 void QLegend::showEvent(QShowEvent *event)
470 {
513 {
471 if (isAttachedToChart())
514 if (isAttachedToChart())
472 layout()->invalidate();
515 layout()->invalidate();
473 QGraphicsWidget::showEvent(event);
516 QGraphicsWidget::showEvent(event);
474 //layout activation will show the items
517 //layout activation will show the items
475 }
518 }
476
519
477 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
520 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
478
521
479 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
522 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
480 : q_ptr(q),
523 : q_ptr(q),
481 m_presenter(presenter),
524 m_presenter(presenter),
482 m_layout(new LegendLayout(q)),
525 m_layout(new LegendLayout(q)),
483 m_chart(chart),
526 m_chart(chart),
484 m_items(new QGraphicsItemGroup(q)),
527 m_items(new QGraphicsItemGroup(q)),
485 m_alignment(Qt::AlignTop),
528 m_alignment(Qt::AlignTop),
486 m_brush(QChartPrivate::defaultBrush()),
529 m_brush(QChartPrivate::defaultBrush()),
487 m_pen(QChartPrivate::defaultPen()),
530 m_pen(QChartPrivate::defaultPen()),
488 m_labelBrush(QChartPrivate::defaultBrush()),
531 m_labelBrush(QChartPrivate::defaultBrush()),
489 m_diameter(5),
532 m_diameter(5),
490 m_attachedToChart(true),
533 m_attachedToChart(true),
491 m_backgroundVisible(false),
534 m_backgroundVisible(false),
492 m_reverseMarkers(false)
535 m_reverseMarkers(false),
536 m_showToolTips(false)
493 {
537 {
494 m_items->setHandlesChildEvents(false);
538 m_items->setHandlesChildEvents(false);
495 }
539 }
496
540
497 QLegendPrivate::~QLegendPrivate()
541 QLegendPrivate::~QLegendPrivate()
498 {
542 {
499
543
500 }
544 }
501
545
502 void QLegendPrivate::setOffset(const QPointF &offset)
546 void QLegendPrivate::setOffset(const QPointF &offset)
503 {
547 {
504 m_layout->setOffset(offset.x(), offset.y());
548 m_layout->setOffset(offset.x(), offset.y());
505 }
549 }
506
550
507 QPointF QLegendPrivate::offset() const
551 QPointF QLegendPrivate::offset() const
508 {
552 {
509 return m_layout->offset();
553 return m_layout->offset();
510 }
554 }
511
555
512 int QLegendPrivate::roundness(qreal size)
556 int QLegendPrivate::roundness(qreal size)
513 {
557 {
514 return 100 * m_diameter / int(size);
558 return 100 * m_diameter / int(size);
515 }
559 }
516
560
517 QList<QLegendMarker*> QLegendPrivate::markers(QAbstractSeries *series)
561 QList<QLegendMarker*> QLegendPrivate::markers(QAbstractSeries *series)
518 {
562 {
519 // Return all markers
563 // Return all markers
520 if (!series) {
564 if (!series) {
521 return m_markers;
565 return m_markers;
522 }
566 }
523
567
524 // Create filtered list
568 // Create filtered list
525 QList<QLegendMarker *> markers;
569 QList<QLegendMarker *> markers;
526 foreach (QLegendMarker *marker, m_markers) {
570 foreach (QLegendMarker *marker, m_markers) {
527 if (marker->series() == series) {
571 if (marker->series() == series) {
528 markers.append(marker);
572 markers.append(marker);
529 }
573 }
530 }
574 }
531 return markers;
575 return markers;
532 }
576 }
533
577
534 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
578 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
535 {
579 {
536 if (m_series.contains(series)) {
580 if (m_series.contains(series)) {
537 return;
581 return;
538 }
582 }
539
583
540 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
584 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
541 decorateMarkers(newMarkers);
585 decorateMarkers(newMarkers);
542 addMarkers(newMarkers);
586 addMarkers(newMarkers);
543
587
544 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
588 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
545 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
589 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
546
590
547 m_series.append(series);
591 m_series.append(series);
548 m_items->setVisible(false);
592 m_items->setVisible(false);
549 m_layout->invalidate();
593 m_layout->invalidate();
550 }
594 }
551
595
552 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
596 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
553 {
597 {
554 if (m_series.contains(series)) {
598 if (m_series.contains(series)) {
555 m_series.removeOne(series);
599 m_series.removeOne(series);
556 }
600 }
557
601
558 // Find out, which markers to remove
602 // Find out, which markers to remove
559 QList<QLegendMarker *> removed;
603 QList<QLegendMarker *> removed;
560 foreach (QLegendMarker *m, m_markers) {
604 foreach (QLegendMarker *m, m_markers) {
561 if (m->series() == series) {
605 if (m->series() == series) {
562 removed << m;
606 removed << m;
563 }
607 }
564 }
608 }
565 removeMarkers(removed);
609 removeMarkers(removed);
566
610
567 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
611 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
568 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
612 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
569
613
570 m_layout->invalidate();
614 m_layout->invalidate();
571 }
615 }
572
616
573 void QLegendPrivate::handleSeriesVisibleChanged()
617 void QLegendPrivate::handleSeriesVisibleChanged()
574 {
618 {
575 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
619 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
576 Q_ASSERT(series);
620 Q_ASSERT(series);
577
621
578 foreach (QLegendMarker *marker, m_markers) {
622 foreach (QLegendMarker *marker, m_markers) {
579 if (marker->series() == series) {
623 if (marker->series() == series) {
580 marker->setVisible(series->isVisible());
624 marker->setVisible(series->isVisible());
581 }
625 }
582 }
626 }
583
627
584 if (m_chart->isVisible())
628 if (m_chart->isVisible())
585 m_layout->invalidate();
629 m_layout->invalidate();
586 }
630 }
587
631
588 void QLegendPrivate::handleCountChanged()
632 void QLegendPrivate::handleCountChanged()
589 {
633 {
590 // Here we handle the changes in marker count.
634 // Here we handle the changes in marker count.
591 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
635 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
592
636
593 QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender());
637 QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender());
594 QList<QLegendMarker *> createdMarkers = series->createLegendMarkers(q_ptr);
638 QList<QLegendMarker *> createdMarkers = series->createLegendMarkers(q_ptr);
595
639
596 // Find out removed markers and created markers
640 // Find out removed markers and created markers
597 QList<QLegendMarker *> removedMarkers;
641 QList<QLegendMarker *> removedMarkers;
598 foreach (QLegendMarker *oldMarker, m_markers) {
642 foreach (QLegendMarker *oldMarker, m_markers) {
599 // we have marker, which is related to sender.
643 // we have marker, which is related to sender.
600 if (oldMarker->series() == series->q_ptr) {
644 if (oldMarker->series() == series->q_ptr) {
601 bool found = false;
645 bool found = false;
602 foreach(QLegendMarker *newMarker, createdMarkers) {
646 foreach(QLegendMarker *newMarker, createdMarkers) {
603 // New marker considered existing if:
647 // New marker considered existing if:
604 // - d_ptr->relatedObject() is same for both markers.
648 // - d_ptr->relatedObject() is same for both markers.
605 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
649 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
606 // Delete the new marker, since we already have existing marker, that might be connected on user side.
650 // Delete the new marker, since we already have existing marker, that might be connected on user side.
607 found = true;
651 found = true;
608 createdMarkers.removeOne(newMarker);
652 createdMarkers.removeOne(newMarker);
609 delete newMarker;
653 delete newMarker;
610 }
654 }
611 }
655 }
612 if (!found) {
656 if (!found) {
613 // No related object found for marker, add to removedMarkers list
657 // No related object found for marker, add to removedMarkers list
614 removedMarkers << oldMarker;
658 removedMarkers << oldMarker;
615 }
659 }
616 }
660 }
617 }
661 }
618
662
619 removeMarkers(removedMarkers);
663 removeMarkers(removedMarkers);
620 decorateMarkers(createdMarkers);
664 decorateMarkers(createdMarkers);
621 addMarkers(createdMarkers);
665 addMarkers(createdMarkers);
622
666
623 q_ptr->layout()->invalidate();
667 q_ptr->layout()->invalidate();
624 }
668 }
625
669
626 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
670 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
627 {
671 {
628 foreach (QLegendMarker *marker, markers) {
672 foreach (QLegendMarker *marker, markers) {
629 m_items->addToGroup(marker->d_ptr.data()->item());
673 m_items->addToGroup(marker->d_ptr.data()->item());
630 m_markers << marker;
674 m_markers << marker;
631 m_markerHash.insert(marker->d_ptr->item(), marker);
675 m_markerHash.insert(marker->d_ptr->item(), marker);
632 }
676 }
633 }
677 }
634
678
635 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
679 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
636 {
680 {
637 foreach (QLegendMarker *marker, markers) {
681 foreach (QLegendMarker *marker, markers) {
638 marker->d_ptr->item()->setVisible(false);
682 marker->d_ptr->item()->setVisible(false);
639 m_items->removeFromGroup(marker->d_ptr->item());
683 m_items->removeFromGroup(marker->d_ptr->item());
640 m_markers.removeOne(marker);
684 m_markers.removeOne(marker);
641 m_markerHash.remove(marker->d_ptr->item());
685 m_markerHash.remove(marker->d_ptr->item());
642 delete marker;
686 delete marker;
643 }
687 }
644 }
688 }
645
689
646 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
690 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
647 {
691 {
648 foreach (QLegendMarker *marker, markers) {
692 foreach (QLegendMarker *marker, markers) {
649 marker->setFont(m_font);
693 marker->setFont(m_font);
650 marker->setLabelBrush(m_labelBrush);
694 marker->setLabelBrush(m_labelBrush);
651 }
695 }
652 }
696 }
653
697
654
698 void QLegendPrivate::updateToolTips()
699 {
700 foreach (QLegendMarker *m, m_markers) {
701 if (m->d_ptr->m_item->displayedLabel() != m->label())
702 m->d_ptr->m_item->setToolTip(m->label());
703 else
704 m->d_ptr->m_item->setToolTip(QString());
705 }
706 }
655 #include "moc_qlegend.cpp"
707 #include "moc_qlegend.cpp"
656 #include "moc_qlegend_p.cpp"
708 #include "moc_qlegend_p.cpp"
657
709
658 QT_CHARTS_END_NAMESPACE
710 QT_CHARTS_END_NAMESPACE
@@ -1,121 +1,125
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
4 ** Contact: https://www.qt.io/licensing/
5 **
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
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
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.
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
16 **
17 ** GNU General Public License Usage
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
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
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
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
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
25 **
26 ** $QT_END_LICENSE$
26 ** $QT_END_LICENSE$
27 **
27 **
28 ****************************************************************************/
28 ****************************************************************************/
29
29
30 #ifndef QLEGEND_H
30 #ifndef QLEGEND_H
31 #define QLEGEND_H
31 #define QLEGEND_H
32
32
33 #include <QtCharts/QChartGlobal>
33 #include <QtCharts/QChartGlobal>
34 #include <QtWidgets/QGraphicsWidget>
34 #include <QtWidgets/QGraphicsWidget>
35 #include <QtGui/QPen>
35 #include <QtGui/QPen>
36 #include <QtGui/QBrush>
36 #include <QtGui/QBrush>
37
37
38 QT_CHARTS_BEGIN_NAMESPACE
38 QT_CHARTS_BEGIN_NAMESPACE
39
39
40 class QChart;
40 class QChart;
41 class QLegendPrivate;
41 class QLegendPrivate;
42 class QLegendMarker;
42 class QLegendMarker;
43 class QAbstractSeries;
43 class QAbstractSeries;
44
44
45 class QT_CHARTS_EXPORT QLegend : public QGraphicsWidget
45 class QT_CHARTS_EXPORT QLegend : public QGraphicsWidget
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
48 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
49 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
49 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
50 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
50 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
51 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
51 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
52 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
52 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
53 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
53 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
54 Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged)
54 Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged)
55 Q_PROPERTY(bool showToolTips READ showToolTips WRITE setShowToolTips NOTIFY showToolTipsChanged)
55
56
56 private:
57 private:
57 explicit QLegend(QChart *chart);
58 explicit QLegend(QChart *chart);
58
59
59 public:
60 public:
60 ~QLegend();
61 ~QLegend();
61
62
62 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
63 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
63
64
64 void setBrush(const QBrush &brush);
65 void setBrush(const QBrush &brush);
65 QBrush brush() const;
66 QBrush brush() const;
66 void setColor(QColor color);
67 void setColor(QColor color);
67 QColor color();
68 QColor color();
68
69
69 void setPen(const QPen &pen);
70 void setPen(const QPen &pen);
70 QPen pen() const;
71 QPen pen() const;
71 void setBorderColor(QColor color);
72 void setBorderColor(QColor color);
72 QColor borderColor();
73 QColor borderColor();
73
74
74 void setFont(const QFont &font);
75 void setFont(const QFont &font);
75 QFont font() const;
76 QFont font() const;
76 void setLabelBrush(const QBrush &brush);
77 void setLabelBrush(const QBrush &brush);
77 QBrush labelBrush() const;
78 QBrush labelBrush() const;
78
79
79 void setLabelColor(QColor color);
80 void setLabelColor(QColor color);
80 QColor labelColor() const;
81 QColor labelColor() const;
81
82
82 void setAlignment(Qt::Alignment alignment);
83 void setAlignment(Qt::Alignment alignment);
83 Qt::Alignment alignment() const;
84 Qt::Alignment alignment() const;
84
85
85 void detachFromChart();
86 void detachFromChart();
86 void attachToChart();
87 void attachToChart();
87 bool isAttachedToChart();
88 bool isAttachedToChart();
88
89
89 void setBackgroundVisible(bool visible = true);
90 void setBackgroundVisible(bool visible = true);
90 bool isBackgroundVisible() const;
91 bool isBackgroundVisible() const;
91
92
92 QList <QLegendMarker*> markers(QAbstractSeries *series = 0) const;
93 QList <QLegendMarker*> markers(QAbstractSeries *series = 0) const;
93
94
94 bool reverseMarkers();
95 bool reverseMarkers();
95 void setReverseMarkers(bool reverseMarkers = true);
96 void setReverseMarkers(bool reverseMarkers = true);
96
97
98 bool showToolTips() const;
99 void setShowToolTips(bool show);
97 protected:
100 protected:
98 void hideEvent(QHideEvent *event);
101 void hideEvent(QHideEvent *event);
99 void showEvent(QShowEvent *event);
102 void showEvent(QShowEvent *event);
100
103
101 Q_SIGNALS:
104 Q_SIGNALS:
102 void backgroundVisibleChanged(bool visible);
105 void backgroundVisibleChanged(bool visible);
103 void colorChanged(QColor color);
106 void colorChanged(QColor color);
104 void borderColorChanged(QColor color);
107 void borderColorChanged(QColor color);
105 void fontChanged(QFont font);
108 void fontChanged(QFont font);
106 void labelColorChanged(QColor color);
109 void labelColorChanged(QColor color);
107 void reverseMarkersChanged(bool reverseMarkers);
110 void reverseMarkersChanged(bool reverseMarkers);
111 void showToolTipsChanged(bool showToolTips);
108
112
109 private:
113 private:
110 QScopedPointer<QLegendPrivate> d_ptr;
114 QScopedPointer<QLegendPrivate> d_ptr;
111 Q_DISABLE_COPY(QLegend)
115 Q_DISABLE_COPY(QLegend)
112 friend class LegendScroller;
116 friend class LegendScroller;
113 friend class LegendLayout;
117 friend class LegendLayout;
114 friend class ChartLayout;
118 friend class ChartLayout;
115 friend class LegendMarkerItem;
119 friend class LegendMarkerItem;
116 friend class QLegendMarkerPrivate;
120 friend class QLegendMarkerPrivate;
117 };
121 };
118
122
119 QT_CHARTS_END_NAMESPACE
123 QT_CHARTS_END_NAMESPACE
120
124
121 #endif // QLEGEND_H
125 #endif // QLEGEND_H
@@ -1,110 +1,112
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
4 ** Contact: https://www.qt.io/licensing/
5 **
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
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
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.
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
16 **
17 ** GNU General Public License Usage
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
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
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
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
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
25 **
26 ** $QT_END_LICENSE$
26 ** $QT_END_LICENSE$
27 **
27 **
28 ****************************************************************************/
28 ****************************************************************************/
29
29
30 // W A R N I N G
30 // W A R N I N G
31 // -------------
31 // -------------
32 //
32 //
33 // This file is not part of the Qt Chart API. It exists purely as an
33 // This file is not part of the Qt Chart API. It exists purely as an
34 // implementation detail. This header file may change from version to
34 // implementation detail. This header file may change from version to
35 // version without notice, or even be removed.
35 // version without notice, or even be removed.
36 //
36 //
37 // We mean it.
37 // We mean it.
38
38
39 #ifndef QLEGEND_P_H
39 #ifndef QLEGEND_P_H
40 #define QLEGEND_P_H
40 #define QLEGEND_P_H
41
41
42 #include <QtCharts/QLegend>
42 #include <QtCharts/QLegend>
43
43
44 QT_CHARTS_BEGIN_NAMESPACE
44 QT_CHARTS_BEGIN_NAMESPACE
45
45
46 class QChart;
46 class QChart;
47 class ChartPresenter;
47 class ChartPresenter;
48 class QAbstractSeries;
48 class QAbstractSeries;
49 class LegendLayout;
49 class LegendLayout;
50 class QLegendMarker;
50 class QLegendMarker;
51
51
52 class QLegendPrivate : public QObject
52 class QLegendPrivate : public QObject
53 {
53 {
54 Q_OBJECT
54 Q_OBJECT
55 public:
55 public:
56 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
56 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
57 ~QLegendPrivate();
57 ~QLegendPrivate();
58
58
59 void setOffset(const QPointF &offset);
59 void setOffset(const QPointF &offset);
60 QPointF offset() const;
60 QPointF offset() const;
61 int roundness(qreal size);
61 int roundness(qreal size);
62
62
63 QGraphicsItemGroup* items() { return m_items; }
63 QGraphicsItemGroup* items() { return m_items; }
64
64
65 QList<QLegendMarker*> markers(QAbstractSeries *series = 0);
65 QList<QLegendMarker*> markers(QAbstractSeries *series = 0);
66
66
67 public Q_SLOTS:
67 public Q_SLOTS:
68 void handleSeriesAdded(QAbstractSeries *series);
68 void handleSeriesAdded(QAbstractSeries *series);
69 void handleSeriesRemoved(QAbstractSeries *series);
69 void handleSeriesRemoved(QAbstractSeries *series);
70 void handleSeriesVisibleChanged();
70 void handleSeriesVisibleChanged();
71 void handleCountChanged();
71 void handleCountChanged();
72
72
73 private:
73 private:
74 // Internal helpers
74 // Internal helpers
75 void addMarkers(QList<QLegendMarker *> markers);
75 void addMarkers(QList<QLegendMarker *> markers);
76 void removeMarkers(QList<QLegendMarker *> markers);
76 void removeMarkers(QList<QLegendMarker *> markers);
77 void decorateMarkers(QList<QLegendMarker *> markers);
77 void decorateMarkers(QList<QLegendMarker *> markers);
78 void updateToolTips();
78
79
79 private:
80 private:
80 QLegend *q_ptr;
81 QLegend *q_ptr;
81 ChartPresenter *m_presenter;
82 ChartPresenter *m_presenter;
82 LegendLayout *m_layout;
83 LegendLayout *m_layout;
83 QChart *m_chart;
84 QChart *m_chart;
84 QGraphicsItemGroup *m_items;
85 QGraphicsItemGroup *m_items;
85 Qt::Alignment m_alignment;
86 Qt::Alignment m_alignment;
86 QBrush m_brush;
87 QBrush m_brush;
87 QPen m_pen;
88 QPen m_pen;
88 QFont m_font;
89 QFont m_font;
89 QBrush m_labelBrush;
90 QBrush m_labelBrush;
90
91
91 qreal m_diameter;
92 qreal m_diameter;
92 bool m_attachedToChart;
93 bool m_attachedToChart;
93 bool m_backgroundVisible;
94 bool m_backgroundVisible;
94 bool m_reverseMarkers;
95 bool m_reverseMarkers;
96 bool m_showToolTips;
95
97
96 QList<QLegendMarker *> m_markers;
98 QList<QLegendMarker *> m_markers;
97 QList<QAbstractSeries *> m_series;
99 QList<QAbstractSeries *> m_series;
98
100
99 QHash<QGraphicsItem *, QLegendMarker *> m_markerHash;
101 QHash<QGraphicsItem *, QLegendMarker *> m_markerHash;
100
102
101 friend class QLegend;
103 friend class QLegend;
102 friend class LegendMarkerItem;
104 friend class LegendMarkerItem;
103 friend class LegendLayout;
105 friend class LegendLayout;
104 friend class QLegendMarkerPrivate;
106 friend class QLegendMarkerPrivate;
105 friend class LegendScroller;
107 friend class LegendScroller;
106 };
108 };
107
109
108 QT_CHARTS_END_NAMESPACE
110 QT_CHARTS_END_NAMESPACE
109
111
110 #endif
112 #endif
General Comments 0
You need to be logged in to leave comments. Login now