##// END OF EJS Templates
Fix missing legend...
Titta Heikkala -
r2602:2e0922f74ba5
parent child
Show More
@@ -1,596 +1,594
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise 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 Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26 #include "legendlayout_p.h"
26 #include "legendlayout_p.h"
27 #include "chartpresenter_p.h"
27 #include "chartpresenter_p.h"
28 #include "abstractchartlayout_p.h"
28 #include "abstractchartlayout_p.h"
29 #include "qlegendmarker.h"
29 #include "qlegendmarker.h"
30 #include "qlegendmarker_p.h"
30 #include "qlegendmarker_p.h"
31 #include "legendmarkeritem_p.h"
31 #include "legendmarkeritem_p.h"
32 #include "chartdataset_p.h"
32 #include "chartdataset_p.h"
33 #include <QPainter>
33 #include <QPainter>
34 #include <QPen>
34 #include <QPen>
35 #include <QGraphicsItemGroup>
35 #include <QGraphicsItemGroup>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 /*!
39 /*!
40 \class QLegend
40 \class QLegend
41 \brief Legend object.
41 \brief Legend object.
42 \mainclass
42 \mainclass
43
43
44 QLegend is a graphical object for displaying the legend of the chart. Legend state is updated by QChart, when
44 QLegend is a graphical object for displaying the legend of the chart. Legend state is updated by QChart, when
45 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
45 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
46 handle the drawing manually.
46 handle the drawing manually.
47 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
47 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
48
48
49 \image examples_percentbarchart_legend.png
49 \image examples_percentbarchart_legend.png
50
50
51 \sa QChart
51 \sa QChart
52 */
52 */
53 /*!
53 /*!
54 \qmlclass Legend QLegend
54 \qmlclass Legend QLegend
55 \brief Legend is part of Qt Chart QML API.
55 \brief Legend is part of Qt Chart QML API.
56
56
57 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
57 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
58 series have been changed. Legend is used via ChartView class. For example:
58 series have been changed. Legend is used via ChartView class. For example:
59 \code
59 \code
60 ChartView {
60 ChartView {
61 legend.visible: true
61 legend.visible: true
62 legend.alignment: Qt.AlignBottom
62 legend.alignment: Qt.AlignBottom
63 // Add a few series...
63 // Add a few series...
64 }
64 }
65 \endcode
65 \endcode
66
66
67 \image examples_percentbarchart_legend.png
67 \image examples_percentbarchart_legend.png
68
68
69 Please note that there is no QML API available for modifying legend markers, unlike in the Qt API of Charts.
69 Please note that there is no QML API available for modifying legend markers, unlike in the Qt API of Charts.
70 The use case of modifying markers can be implemented for example by creating your own custom legend. For an example
70 The use case of modifying markers can be implemented for example by creating your own custom legend. For an example
71 on how to do this, see \l {demos/qmlcustomlegend}{Qml Custom Demo} application.
71 on how to do this, see \l {demos/qmlcustomlegend}{Qml Custom Demo} application.
72 */
72 */
73
73
74 /*!
74 /*!
75 \property QLegend::alignment
75 \property QLegend::alignment
76 \brief The alignment of the legend.
76 \brief The alignment of the legend.
77
77
78 Legend paints on the defined position in the chart. The following alignments are supported:
78 Legend paints on the defined position in the chart. The following alignments are supported:
79 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
79 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
80 */
80 */
81 /*!
81 /*!
82 \qmlproperty Qt.Alignment Legend::alignment
82 \qmlproperty Qt.Alignment Legend::alignment
83 \brief The alignment of the legend.
83 \brief The alignment of the legend.
84
84
85 Legend paints on the defined position in the chart. The following alignments are supported:
85 Legend paints on the defined position in the chart. The following alignments are supported:
86 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
86 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
87 */
87 */
88
88
89 /*!
89 /*!
90 \property QLegend::backgroundVisible
90 \property QLegend::backgroundVisible
91 Whether the legend background is visible or not.
91 Whether the legend background is visible or not.
92 */
92 */
93 /*!
93 /*!
94 \qmlproperty bool Legend::backgroundVisible
94 \qmlproperty bool Legend::backgroundVisible
95 Whether the legend background is visible or not.
95 Whether the legend background is visible or not.
96 */
96 */
97
97
98 /*!
98 /*!
99 \property QLegend::color
99 \property QLegend::color
100 The color of the legend, i.e. the background (brush) color. Note that if you change the color
100 The color of the legend, i.e. the background (brush) color. Note that if you change the color
101 of the legend, the style of the legend brush is set to Qt::SolidPattern.
101 of the legend, the style of the legend brush is set to Qt::SolidPattern.
102 */
102 */
103 /*!
103 /*!
104 \qmlproperty color Legend::color
104 \qmlproperty color Legend::color
105 The color of the legend, i.e. the background (brush) color.
105 The color of the legend, i.e. the background (brush) color.
106 */
106 */
107
107
108 /*!
108 /*!
109 \property QLegend::borderColor
109 \property QLegend::borderColor
110 The border color of the legend, i.e. the line color.
110 The border color of the legend, i.e. the line color.
111 */
111 */
112 /*!
112 /*!
113 \qmlproperty color Legend::borderColor
113 \qmlproperty color Legend::borderColor
114 The border color of the legend, i.e. the line color.
114 The border color of the legend, i.e. the line color.
115 */
115 */
116
116
117 /*!
117 /*!
118 \property QLegend::font
118 \property QLegend::font
119 The font of markers used by legend
119 The font of markers used by legend
120 */
120 */
121 /*!
121 /*!
122 \qmlproperty Font Legend::font
122 \qmlproperty Font Legend::font
123 The font of markers used by legend
123 The font of markers used by legend
124 */
124 */
125
125
126 /*!
126 /*!
127 \property QLegend::labelColor
127 \property QLegend::labelColor
128 The color of brush used to draw labels.
128 The color of brush used to draw labels.
129 */
129 */
130 /*!
130 /*!
131 \qmlproperty color QLegend::labelColor
131 \qmlproperty color QLegend::labelColor
132 The color of brush used to draw labels.
132 The color of brush used to draw labels.
133 */
133 */
134
134
135 /*!
135 /*!
136 \fn void QLegend::backgroundVisibleChanged(bool)
136 \fn void QLegend::backgroundVisibleChanged(bool)
137 The visibility of the legend background changed to \a visible.
137 The visibility of the legend background changed to \a visible.
138 */
138 */
139
139
140 /*!
140 /*!
141 \fn void QLegend::colorChanged(QColor)
141 \fn void QLegend::colorChanged(QColor)
142 The color of the legend background changed to \a color.
142 The color of the legend background changed to \a color.
143 */
143 */
144
144
145 /*!
145 /*!
146 \fn void QLegend::borderColorChanged(QColor)
146 \fn void QLegend::borderColorChanged(QColor)
147 The border color of the legend background changed to \a color.
147 The border color of the legend background changed to \a color.
148 */
148 */
149
149
150 /*!
150 /*!
151 \fn void QLegend::fontChanged(QFont)
151 \fn void QLegend::fontChanged(QFont)
152 The font of markers of the legend changed to \a font.
152 The font of markers of the legend changed to \a font.
153 */
153 */
154
154
155 /*!
155 /*!
156 \fn void QLegend::labelColorChanged(QColor color)
156 \fn void QLegend::labelColorChanged(QColor color)
157 This signal is emitted when the color of brush used to draw labels has changed to \a color.
157 This signal is emitted when the color of brush used to draw labels has changed to \a color.
158 */
158 */
159
159
160 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
160 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
161 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
161 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
162 {
162 {
163 setZValue(ChartPresenter::LegendZValue);
163 setZValue(ChartPresenter::LegendZValue);
164 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
164 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
165 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
165 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
166 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
166 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
167 setLayout(d_ptr->m_layout);
167 setLayout(d_ptr->m_layout);
168 }
168 }
169
169
170 /*!
170 /*!
171 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
171 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
172 */
172 */
173 QLegend::~QLegend()
173 QLegend::~QLegend()
174 {
174 {
175 }
175 }
176
176
177 /*!
177 /*!
178 \internal
178 \internal
179 */
179 */
180 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
180 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
181 {
181 {
182 Q_UNUSED(option)
182 Q_UNUSED(option)
183 Q_UNUSED(widget)
183 Q_UNUSED(widget)
184
184
185 if (!d_ptr->m_backgroundVisible)
185 if (!d_ptr->m_backgroundVisible)
186 return;
186 return;
187
187
188 painter->setOpacity(opacity());
188 painter->setOpacity(opacity());
189 painter->setPen(d_ptr->m_pen);
189 painter->setPen(d_ptr->m_pen);
190 painter->setBrush(d_ptr->m_brush);
190 painter->setBrush(d_ptr->m_brush);
191 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
191 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
192 }
192 }
193
193
194
194
195 /*!
195 /*!
196 Sets the \a brush of legend. Brush affects the background of legend.
196 Sets the \a brush of legend. Brush affects the background of legend.
197 */
197 */
198 void QLegend::setBrush(const QBrush &brush)
198 void QLegend::setBrush(const QBrush &brush)
199 {
199 {
200 if (d_ptr->m_brush != brush) {
200 if (d_ptr->m_brush != brush) {
201 d_ptr->m_brush = brush;
201 d_ptr->m_brush = brush;
202 update();
202 update();
203 emit colorChanged(brush.color());
203 emit colorChanged(brush.color());
204 }
204 }
205 }
205 }
206
206
207 /*!
207 /*!
208 Returns the brush used by legend.
208 Returns the brush used by legend.
209 */
209 */
210 QBrush QLegend::brush() const
210 QBrush QLegend::brush() const
211 {
211 {
212 return d_ptr->m_brush;
212 return d_ptr->m_brush;
213 }
213 }
214
214
215 void QLegend::setColor(QColor color)
215 void QLegend::setColor(QColor color)
216 {
216 {
217 QBrush b = d_ptr->m_brush;
217 QBrush b = d_ptr->m_brush;
218 if (b.style() != Qt::SolidPattern || b.color() != color) {
218 if (b.style() != Qt::SolidPattern || b.color() != color) {
219 b.setStyle(Qt::SolidPattern);
219 b.setStyle(Qt::SolidPattern);
220 b.setColor(color);
220 b.setColor(color);
221 setBrush(b);
221 setBrush(b);
222 }
222 }
223 }
223 }
224
224
225 QColor QLegend::color()
225 QColor QLegend::color()
226 {
226 {
227 return d_ptr->m_brush.color();
227 return d_ptr->m_brush.color();
228 }
228 }
229
229
230 /*!
230 /*!
231 Sets the \a pen of legend. Pen affects the legend borders.
231 Sets the \a pen of legend. Pen affects the legend borders.
232 */
232 */
233 void QLegend::setPen(const QPen &pen)
233 void QLegend::setPen(const QPen &pen)
234 {
234 {
235 if (d_ptr->m_pen != pen) {
235 if (d_ptr->m_pen != pen) {
236 d_ptr->m_pen = pen;
236 d_ptr->m_pen = pen;
237 update();
237 update();
238 emit borderColorChanged(pen.color());
238 emit borderColorChanged(pen.color());
239 }
239 }
240 }
240 }
241
241
242 /*!
242 /*!
243 Returns the pen used by legend
243 Returns the pen used by legend
244 */
244 */
245
245
246 QPen QLegend::pen() const
246 QPen QLegend::pen() const
247 {
247 {
248 return d_ptr->m_pen;
248 return d_ptr->m_pen;
249 }
249 }
250
250
251 void QLegend::setFont(const QFont &font)
251 void QLegend::setFont(const QFont &font)
252 {
252 {
253 if (d_ptr->m_font != font) {
253 if (d_ptr->m_font != font) {
254 d_ptr->m_font = font;
254 d_ptr->m_font = font;
255 foreach (QLegendMarker *marker, d_ptr->markers()) {
255 foreach (QLegendMarker *marker, d_ptr->markers()) {
256 marker->setFont(d_ptr->m_font);
256 marker->setFont(d_ptr->m_font);
257 }
257 }
258 layout()->invalidate();
258 layout()->invalidate();
259 emit fontChanged(font);
259 emit fontChanged(font);
260 }
260 }
261 }
261 }
262
262
263 QFont QLegend::font() const
263 QFont QLegend::font() const
264 {
264 {
265 return d_ptr->m_font;
265 return d_ptr->m_font;
266 }
266 }
267
267
268 void QLegend::setBorderColor(QColor color)
268 void QLegend::setBorderColor(QColor color)
269 {
269 {
270 QPen p = d_ptr->m_pen;
270 QPen p = d_ptr->m_pen;
271 if (p.color() != color) {
271 if (p.color() != color) {
272 p.setColor(color);
272 p.setColor(color);
273 setPen(p);
273 setPen(p);
274 }
274 }
275 }
275 }
276
276
277 QColor QLegend::borderColor()
277 QColor QLegend::borderColor()
278 {
278 {
279 return d_ptr->m_pen.color();
279 return d_ptr->m_pen.color();
280 }
280 }
281
281
282 /*!
282 /*!
283 Set brush used to draw labels to \a brush.
283 Set brush used to draw labels to \a brush.
284 */
284 */
285 void QLegend::setLabelBrush(const QBrush &brush)
285 void QLegend::setLabelBrush(const QBrush &brush)
286 {
286 {
287 if (d_ptr->m_labelBrush != brush) {
287 if (d_ptr->m_labelBrush != brush) {
288 d_ptr->m_labelBrush = brush;
288 d_ptr->m_labelBrush = brush;
289 foreach (QLegendMarker *marker, d_ptr->markers()) {
289 foreach (QLegendMarker *marker, d_ptr->markers()) {
290 marker->setLabelBrush(d_ptr->m_labelBrush);
290 marker->setLabelBrush(d_ptr->m_labelBrush);
291 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
291 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
292 // instead of mapping it from label brush color
292 // instead of mapping it from label brush color
293 marker->setPen(brush.color());
293 marker->setPen(brush.color());
294 }
294 }
295 emit labelColorChanged(brush.color());
295 emit labelColorChanged(brush.color());
296 }
296 }
297 }
297 }
298
298
299 /*!
299 /*!
300 Brush used to draw labels.
300 Brush used to draw labels.
301 */
301 */
302 QBrush QLegend::labelBrush() const
302 QBrush QLegend::labelBrush() const
303 {
303 {
304 return d_ptr->m_labelBrush;
304 return d_ptr->m_labelBrush;
305 }
305 }
306
306
307 void QLegend::setLabelColor(QColor color)
307 void QLegend::setLabelColor(QColor color)
308 {
308 {
309 QBrush b = d_ptr->m_labelBrush;
309 QBrush b = d_ptr->m_labelBrush;
310 if (b.style() != Qt::SolidPattern || b.color() != color) {
310 if (b.style() != Qt::SolidPattern || b.color() != color) {
311 b.setStyle(Qt::SolidPattern);
311 b.setStyle(Qt::SolidPattern);
312 b.setColor(color);
312 b.setColor(color);
313 setLabelBrush(b);
313 setLabelBrush(b);
314 }
314 }
315 }
315 }
316
316
317 QColor QLegend::labelColor() const
317 QColor QLegend::labelColor() const
318 {
318 {
319 return d_ptr->m_labelBrush.color();
319 return d_ptr->m_labelBrush.color();
320 }
320 }
321
321
322
322
323 void QLegend::setAlignment(Qt::Alignment alignment)
323 void QLegend::setAlignment(Qt::Alignment alignment)
324 {
324 {
325 if (d_ptr->m_alignment != alignment) {
325 if (d_ptr->m_alignment != alignment) {
326 d_ptr->m_alignment = alignment;
326 d_ptr->m_alignment = alignment;
327 layout()->invalidate();
327 layout()->invalidate();
328 }
328 }
329 }
329 }
330
330
331 Qt::Alignment QLegend::alignment() const
331 Qt::Alignment QLegend::alignment() const
332 {
332 {
333 return d_ptr->m_alignment;
333 return d_ptr->m_alignment;
334 }
334 }
335
335
336 /*!
336 /*!
337 Detaches the legend from chart. Chart won't change layout of the legend.
337 Detaches the legend from chart. Chart won't change layout of the legend.
338 */
338 */
339 void QLegend::detachFromChart()
339 void QLegend::detachFromChart()
340 {
340 {
341 d_ptr->m_attachedToChart = false;
341 d_ptr->m_attachedToChart = false;
342 // layout()->invalidate();
342 // layout()->invalidate();
343 d_ptr->m_chart->layout()->invalidate();
343 d_ptr->m_chart->layout()->invalidate();
344 setParent(0);
344 setParent(0);
345
345
346 }
346 }
347
347
348 /*!
348 /*!
349 Attaches the legend to chart. Chart may change layout of the legend.
349 Attaches the legend to chart. Chart may change layout of the legend.
350 */
350 */
351 void QLegend::attachToChart()
351 void QLegend::attachToChart()
352 {
352 {
353 d_ptr->m_attachedToChart = true;
353 d_ptr->m_attachedToChart = true;
354 // layout()->invalidate();
354 // layout()->invalidate();
355 d_ptr->m_chart->layout()->invalidate();
355 d_ptr->m_chart->layout()->invalidate();
356 setParent(d_ptr->m_chart);
356 setParent(d_ptr->m_chart);
357 }
357 }
358
358
359 /*!
359 /*!
360 Returns true, if legend is attached to chart.
360 Returns true, if legend is attached to chart.
361 */
361 */
362 bool QLegend::isAttachedToChart()
362 bool QLegend::isAttachedToChart()
363 {
363 {
364 return d_ptr->m_attachedToChart;
364 return d_ptr->m_attachedToChart;
365 }
365 }
366
366
367 /*!
367 /*!
368 Sets the visibility of legend background to \a visible
368 Sets the visibility of legend background to \a visible
369 */
369 */
370 void QLegend::setBackgroundVisible(bool visible)
370 void QLegend::setBackgroundVisible(bool visible)
371 {
371 {
372 if (d_ptr->m_backgroundVisible != visible) {
372 if (d_ptr->m_backgroundVisible != visible) {
373 d_ptr->m_backgroundVisible = visible;
373 d_ptr->m_backgroundVisible = visible;
374 update();
374 update();
375 emit backgroundVisibleChanged(visible);
375 emit backgroundVisibleChanged(visible);
376 }
376 }
377 }
377 }
378
378
379 /*!
379 /*!
380 Returns the visibility of legend background
380 Returns the visibility of legend background
381 */
381 */
382 bool QLegend::isBackgroundVisible() const
382 bool QLegend::isBackgroundVisible() const
383 {
383 {
384 return d_ptr->m_backgroundVisible;
384 return d_ptr->m_backgroundVisible;
385 }
385 }
386
386
387 /*!
387 /*!
388 Returns the list of markers in legend. The list can be filtered with \a series parameter.
388 Returns the list of markers in legend. The list can be filtered with \a series parameter.
389 If \a series is given, only markers related to that series are returned.
389 If \a series is given, only markers related to that series are returned.
390 */
390 */
391 QList<QLegendMarker*> QLegend::markers(QAbstractSeries *series) const
391 QList<QLegendMarker*> QLegend::markers(QAbstractSeries *series) const
392 {
392 {
393 return d_ptr->markers(series);
393 return d_ptr->markers(series);
394 }
394 }
395
395
396 /*!
396 /*!
397 \internal \a event see QGraphicsWidget for details
397 \internal \a event see QGraphicsWidget for details
398 */
398 */
399 void QLegend::hideEvent(QHideEvent *event)
399 void QLegend::hideEvent(QHideEvent *event)
400 {
400 {
401 if (isAttachedToChart())
401 if (isAttachedToChart())
402 d_ptr->m_presenter->layout()->invalidate();
402 d_ptr->m_presenter->layout()->invalidate();
403 QGraphicsWidget::hideEvent(event);
403 QGraphicsWidget::hideEvent(event);
404 }
404 }
405 /*!
405 /*!
406 \internal \a event see QGraphicsWidget for details
406 \internal \a event see QGraphicsWidget for details
407 */
407 */
408 void QLegend::showEvent(QShowEvent *event)
408 void QLegend::showEvent(QShowEvent *event)
409 {
409 {
410 if (isAttachedToChart()) {
410 if (isAttachedToChart())
411 d_ptr->items()->setVisible(false);
412 layout()->invalidate();
411 layout()->invalidate();
413 }
414 QGraphicsWidget::showEvent(event);
412 QGraphicsWidget::showEvent(event);
415 //layout activation will show the items
413 //layout activation will show the items
416 }
414 }
417
415
418 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
416 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
419
417
420 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
418 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
421 : q_ptr(q),
419 : q_ptr(q),
422 m_presenter(presenter),
420 m_presenter(presenter),
423 m_layout(new LegendLayout(q)),
421 m_layout(new LegendLayout(q)),
424 m_chart(chart),
422 m_chart(chart),
425 m_items(new QGraphicsItemGroup(q)),
423 m_items(new QGraphicsItemGroup(q)),
426 m_alignment(Qt::AlignTop),
424 m_alignment(Qt::AlignTop),
427 m_brush(QBrush()),
425 m_brush(QBrush()),
428 m_pen(QPen()),
426 m_pen(QPen()),
429 m_labelBrush(QBrush()),
427 m_labelBrush(QBrush()),
430 m_diameter(5),
428 m_diameter(5),
431 m_attachedToChart(true),
429 m_attachedToChart(true),
432 m_backgroundVisible(false)
430 m_backgroundVisible(false)
433 {
431 {
434 m_items->setHandlesChildEvents(false);
432 m_items->setHandlesChildEvents(false);
435 }
433 }
436
434
437 QLegendPrivate::~QLegendPrivate()
435 QLegendPrivate::~QLegendPrivate()
438 {
436 {
439
437
440 }
438 }
441
439
442 void QLegendPrivate::setOffset(const QPointF &offset)
440 void QLegendPrivate::setOffset(const QPointF &offset)
443 {
441 {
444 m_layout->setOffset(offset.x(), offset.y());
442 m_layout->setOffset(offset.x(), offset.y());
445 }
443 }
446
444
447 QPointF QLegendPrivate::offset() const
445 QPointF QLegendPrivate::offset() const
448 {
446 {
449 return m_layout->offset();
447 return m_layout->offset();
450 }
448 }
451
449
452 int QLegendPrivate::roundness(qreal size)
450 int QLegendPrivate::roundness(qreal size)
453 {
451 {
454 return 100 * m_diameter / int(size);
452 return 100 * m_diameter / int(size);
455 }
453 }
456
454
457 QList<QLegendMarker*> QLegendPrivate::markers(QAbstractSeries *series)
455 QList<QLegendMarker*> QLegendPrivate::markers(QAbstractSeries *series)
458 {
456 {
459 // Return all markers
457 // Return all markers
460 if (!series) {
458 if (!series) {
461 return m_markers;
459 return m_markers;
462 }
460 }
463
461
464 // Create filtered list
462 // Create filtered list
465 QList<QLegendMarker *> markers;
463 QList<QLegendMarker *> markers;
466 foreach (QLegendMarker *marker, m_markers) {
464 foreach (QLegendMarker *marker, m_markers) {
467 if (marker->series() == series) {
465 if (marker->series() == series) {
468 markers.append(marker);
466 markers.append(marker);
469 }
467 }
470 }
468 }
471 return markers;
469 return markers;
472 }
470 }
473
471
474 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
472 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
475 {
473 {
476 if (m_series.contains(series)) {
474 if (m_series.contains(series)) {
477 return;
475 return;
478 }
476 }
479
477
480 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
478 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
481 decorateMarkers(newMarkers);
479 decorateMarkers(newMarkers);
482 addMarkers(newMarkers);
480 addMarkers(newMarkers);
483
481
484 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
482 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
485 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
483 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
486
484
487 m_series.append(series);
485 m_series.append(series);
488 m_items->setVisible(false);
486 m_items->setVisible(false);
489 m_layout->invalidate();
487 m_layout->invalidate();
490 }
488 }
491
489
492 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
490 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
493 {
491 {
494 if (m_series.contains(series)) {
492 if (m_series.contains(series)) {
495 m_series.removeOne(series);
493 m_series.removeOne(series);
496 }
494 }
497
495
498 // Find out, which markers to remove
496 // Find out, which markers to remove
499 QList<QLegendMarker *> removed;
497 QList<QLegendMarker *> removed;
500 foreach (QLegendMarker *m, m_markers) {
498 foreach (QLegendMarker *m, m_markers) {
501 if (m->series() == series) {
499 if (m->series() == series) {
502 removed << m;
500 removed << m;
503 }
501 }
504 }
502 }
505 removeMarkers(removed);
503 removeMarkers(removed);
506
504
507 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
505 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
508 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
506 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
509
507
510 m_layout->invalidate();
508 m_layout->invalidate();
511 }
509 }
512
510
513 void QLegendPrivate::handleSeriesVisibleChanged()
511 void QLegendPrivate::handleSeriesVisibleChanged()
514 {
512 {
515 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
513 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
516 Q_ASSERT(series);
514 Q_ASSERT(series);
517
515
518 foreach (QLegendMarker *marker, m_markers) {
516 foreach (QLegendMarker *marker, m_markers) {
519 if (marker->series() == series) {
517 if (marker->series() == series) {
520 marker->setVisible(series->isVisible());
518 marker->setVisible(series->isVisible());
521 }
519 }
522 }
520 }
523 m_layout->invalidate();
521 m_layout->invalidate();
524 }
522 }
525
523
526 void QLegendPrivate::handleCountChanged()
524 void QLegendPrivate::handleCountChanged()
527 {
525 {
528 // Here we handle the changes in marker count.
526 // Here we handle the changes in marker count.
529 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
527 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
530
528
531 QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender());
529 QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender());
532 QList<QLegendMarker *> createdMarkers = series->createLegendMarkers(q_ptr);
530 QList<QLegendMarker *> createdMarkers = series->createLegendMarkers(q_ptr);
533
531
534 // Find out removed markers and created markers
532 // Find out removed markers and created markers
535 QList<QLegendMarker *> removedMarkers;
533 QList<QLegendMarker *> removedMarkers;
536 foreach (QLegendMarker *oldMarker, m_markers) {
534 foreach (QLegendMarker *oldMarker, m_markers) {
537 // we have marker, which is related to sender.
535 // we have marker, which is related to sender.
538 if (oldMarker->series() == series->q_ptr) {
536 if (oldMarker->series() == series->q_ptr) {
539 bool found = false;
537 bool found = false;
540 foreach(QLegendMarker *newMarker, createdMarkers) {
538 foreach(QLegendMarker *newMarker, createdMarkers) {
541 // New marker considered existing if:
539 // New marker considered existing if:
542 // - d_ptr->relatedObject() is same for both markers.
540 // - d_ptr->relatedObject() is same for both markers.
543 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
541 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
544 // Delete the new marker, since we already have existing marker, that might be connected on user side.
542 // Delete the new marker, since we already have existing marker, that might be connected on user side.
545 found = true;
543 found = true;
546 createdMarkers.removeOne(newMarker);
544 createdMarkers.removeOne(newMarker);
547 delete newMarker;
545 delete newMarker;
548 }
546 }
549 }
547 }
550 if (!found) {
548 if (!found) {
551 // No related object found for marker, add to removedMarkers list
549 // No related object found for marker, add to removedMarkers list
552 removedMarkers << oldMarker;
550 removedMarkers << oldMarker;
553 }
551 }
554 }
552 }
555 }
553 }
556
554
557 removeMarkers(removedMarkers);
555 removeMarkers(removedMarkers);
558 decorateMarkers(createdMarkers);
556 decorateMarkers(createdMarkers);
559 addMarkers(createdMarkers);
557 addMarkers(createdMarkers);
560
558
561 q_ptr->layout()->invalidate();
559 q_ptr->layout()->invalidate();
562 }
560 }
563
561
564 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
562 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
565 {
563 {
566 foreach (QLegendMarker *marker, markers) {
564 foreach (QLegendMarker *marker, markers) {
567 m_items->addToGroup(marker->d_ptr.data()->item());
565 m_items->addToGroup(marker->d_ptr.data()->item());
568 m_markers << marker;
566 m_markers << marker;
569 m_markerHash.insert(marker->d_ptr->item(), marker);
567 m_markerHash.insert(marker->d_ptr->item(), marker);
570 }
568 }
571 }
569 }
572
570
573 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
571 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
574 {
572 {
575 foreach (QLegendMarker *marker, markers) {
573 foreach (QLegendMarker *marker, markers) {
576 marker->d_ptr->item()->setVisible(false);
574 marker->d_ptr->item()->setVisible(false);
577 m_items->removeFromGroup(marker->d_ptr->item());
575 m_items->removeFromGroup(marker->d_ptr->item());
578 m_markers.removeOne(marker);
576 m_markers.removeOne(marker);
579 m_markerHash.remove(marker->d_ptr->item());
577 m_markerHash.remove(marker->d_ptr->item());
580 delete marker;
578 delete marker;
581 }
579 }
582 }
580 }
583
581
584 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
582 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
585 {
583 {
586 foreach (QLegendMarker *marker, markers) {
584 foreach (QLegendMarker *marker, markers) {
587 marker->setFont(m_font);
585 marker->setFont(m_font);
588 marker->setLabelBrush(m_labelBrush);
586 marker->setLabelBrush(m_labelBrush);
589 }
587 }
590 }
588 }
591
589
592
590
593 #include "moc_qlegend.cpp"
591 #include "moc_qlegend.cpp"
594 #include "moc_qlegend_p.cpp"
592 #include "moc_qlegend_p.cpp"
595
593
596 QTCOMMERCIALCHART_END_NAMESPACE
594 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now