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