##// END OF EJS Templates
Enabled legend by default....
Tero Ahola -
r2071:b22984824da1
parent child
Show More
@@ -1,541 +1,540
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 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 Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt 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 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 "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "qxyseries.h"
28 #include "qxyseries.h"
29 #include "qlineseries.h"
29 #include "qlineseries.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qscatterseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
32 #include "qsplineseries.h"
33 #include "qabstractbarseries.h"
33 #include "qabstractbarseries.h"
34 #include "qstackedbarseries.h"
34 #include "qstackedbarseries.h"
35 #include "qpercentbarseries.h"
35 #include "qpercentbarseries.h"
36 #include "qbarset.h"
36 #include "qbarset.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieseries_p.h"
38 #include "qpieseries_p.h"
39 #include "qpieslice.h"
39 #include "qpieslice.h"
40 #include "chartpresenter_p.h"
40 #include "chartpresenter_p.h"
41 #include <QPainter>
41 #include <QPainter>
42 #include <QPen>
42 #include <QPen>
43 #include <QTimer>
43 #include <QTimer>
44 #include <QGraphicsLayout>
44 #include <QGraphicsLayout>
45 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
46
46
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48
48
49 /*!
49 /*!
50 \class QLegend
50 \class QLegend
51 \brief Legend object
51 \brief Legend object
52 \mainclass
52 \mainclass
53
53
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
54 QLegend is a graphical object, whics displays 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 \qmlclass Legend QLegend
64 \qmlclass Legend QLegend
65 \brief Legend is part of QtCommercial Chart QML API.
65 \brief Legend is part of QtCommercial Chart QML API.
66
66
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
68 series have been changed. Legend is used via ChartView class. For example:
68 series have been changed. Legend is used via ChartView class. For example:
69 \code
69 \code
70 ChartView {
70 ChartView {
71 legend.visible: true
71 legend.visible: true
72 legend.alignment: Qt.AlignBottom
72 legend.alignment: Qt.AlignBottom
73 // Add a few series...
73 // Add a few series...
74 }
74 }
75 \endcode
75 \endcode
76
76
77 \image examples_percentbarchart_legend.png
77 \image examples_percentbarchart_legend.png
78 */
78 */
79
79
80 /*!
80 /*!
81 \property QLegend::alignment
81 \property QLegend::alignment
82 \brief The alignment of the legend.
82 \brief The alignment of the legend.
83
83
84 Legend paints on the defined position in the chart. The following alignments are supported:
84 Legend paints on the defined position in the chart. The following alignments are supported:
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
86 */
86 */
87 /*!
87 /*!
88 \qmlproperty Qt.Alignment Legend::alignment
88 \qmlproperty Qt.Alignment Legend::alignment
89 \brief The alignment of the legend.
89 \brief The alignment of the legend.
90
90
91 Legend paints on the defined position in the chart. The following alignments are supported:
91 Legend paints on the defined position in the chart. The following alignments are supported:
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
93 */
93 */
94
94
95 /*!
95 /*!
96 \property QLegend::backgroundVisible
96 \property QLegend::backgroundVisible
97 Whether the legend background is visible or not.
97 Whether the legend background is visible or not.
98 */
98 */
99 /*!
99 /*!
100 \qmlproperty bool Legend::backgroundVisible
100 \qmlproperty bool Legend::backgroundVisible
101 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
102 */
102 */
103
103
104 /*!
104 /*!
105 \property QLegend::color
105 \property QLegend::color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
108 */
108 */
109 /*!
109 /*!
110 \qmlproperty color Legend::color
110 \qmlproperty color Legend::color
111 The color of the legend, i.e. the background (brush) color.
111 The color of the legend, i.e. the background (brush) color.
112 */
112 */
113
113
114 /*!
114 /*!
115 \property QLegend::borderColor
115 \property QLegend::borderColor
116 The border color of the legend, i.e. the line color.
116 The border color of the legend, i.e. the line color.
117 */
117 */
118 /*!
118 /*!
119 \qmlproperty color Legend::borderColor
119 \qmlproperty color Legend::borderColor
120 The border color of the legend, i.e. the line color.
120 The border color of the legend, i.e. the line color.
121 */
121 */
122
122
123 /*!
123 /*!
124 \property QLegend::font
124 \property QLegend::font
125 The font of markers used by legend
125 The font of markers used by legend
126 */
126 */
127 /*!
127 /*!
128 \qmlproperty color Legend::font
128 \qmlproperty color Legend::font
129 The font of markers used by legend
129 The font of markers used by legend
130 */
130 */
131
131
132 /*!
132 /*!
133 \property QLegend::labelColor
133 \property QLegend::labelColor
134 The color of brush used to draw labels.
134 The color of brush used to draw labels.
135 */
135 */
136 /*!
136 /*!
137 \qmlproperty color QLegend::labelColor
137 \qmlproperty color QLegend::labelColor
138 The color of brush used to draw labels.
138 The color of brush used to draw labels.
139 */
139 */
140
140
141 /*!
141 /*!
142 \fn void QLegend::backgroundVisibleChanged(bool)
142 \fn void QLegend::backgroundVisibleChanged(bool)
143 The visibility of the legend background changed to \a visible.
143 The visibility of the legend background changed to \a visible.
144 */
144 */
145
145
146 /*!
146 /*!
147 \fn void QLegend::colorChanged(QColor)
147 \fn void QLegend::colorChanged(QColor)
148 The color of the legend background changed to \a color.
148 The color of the legend background changed to \a color.
149 */
149 */
150
150
151 /*!
151 /*!
152 \fn void QLegend::borderColorChanged(QColor)
152 \fn void QLegend::borderColorChanged(QColor)
153 The border color of the legend background changed to \a color.
153 The border color of the legend background changed to \a color.
154 */
154 */
155
155
156 /*!
156 /*!
157 \fn void QLegend::fontChanged(QFont)
157 \fn void QLegend::fontChanged(QFont)
158 The font of markers of the legend changed to \a font.
158 The font of markers of the legend changed to \a font.
159 */
159 */
160
160
161 /*!
161 /*!
162 \fn void QLegend::labelColorChanged(QColor color)
162 \fn void QLegend::labelColorChanged(QColor color)
163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
164 */
164 */
165
165
166 /*!
166 /*!
167 Constructs the legend object and sets the parent to \a parent
167 Constructs the legend object and sets the parent to \a parent
168 */
168 */
169
169
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
172 {
172 {
173 setZValue(ChartPresenter::LegendZValue);
173 setZValue(ChartPresenter::LegendZValue);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
177 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
177 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
178 setLayout(d_ptr->m_layout);
178 setLayout(d_ptr->m_layout);
179 setVisible(false);
180 }
179 }
181
180
182 /*!
181 /*!
183 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
182 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
184 */
183 */
185 QLegend::~QLegend()
184 QLegend::~QLegend()
186 {
185 {
187 }
186 }
188
187
189 /*!
188 /*!
190 \internal
189 \internal
191 */
190 */
192 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
191 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
193 {
192 {
194 Q_UNUSED(option)
193 Q_UNUSED(option)
195 Q_UNUSED(widget)
194 Q_UNUSED(widget)
196
195
197 if(!d_ptr->m_backgroundVisible) return;
196 if(!d_ptr->m_backgroundVisible) return;
198
197
199 painter->setOpacity(opacity());
198 painter->setOpacity(opacity());
200 painter->setPen(d_ptr->m_pen);
199 painter->setPen(d_ptr->m_pen);
201 painter->setBrush(d_ptr->m_brush);
200 painter->setBrush(d_ptr->m_brush);
202 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
201 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
203
202
204 }
203 }
205
204
206
205
207 /*!
206 /*!
208 Sets the \a brush of legend. Brush affects the background of legend.
207 Sets the \a brush of legend. Brush affects the background of legend.
209 */
208 */
210 void QLegend::setBrush(const QBrush &brush)
209 void QLegend::setBrush(const QBrush &brush)
211 {
210 {
212 if (d_ptr->m_brush != brush) {
211 if (d_ptr->m_brush != brush) {
213 d_ptr->m_brush = brush;
212 d_ptr->m_brush = brush;
214 update();
213 update();
215 emit colorChanged(brush.color());
214 emit colorChanged(brush.color());
216 }
215 }
217 }
216 }
218
217
219 /*!
218 /*!
220 Returns the brush used by legend.
219 Returns the brush used by legend.
221 */
220 */
222 QBrush QLegend::brush() const
221 QBrush QLegend::brush() const
223 {
222 {
224 return d_ptr->m_brush;
223 return d_ptr->m_brush;
225 }
224 }
226
225
227 void QLegend::setColor(QColor color)
226 void QLegend::setColor(QColor color)
228 {
227 {
229 QBrush b = d_ptr->m_brush;
228 QBrush b = d_ptr->m_brush;
230 if (b.style() != Qt::SolidPattern || b.color() != color) {
229 if (b.style() != Qt::SolidPattern || b.color() != color) {
231 b.setStyle(Qt::SolidPattern);
230 b.setStyle(Qt::SolidPattern);
232 b.setColor(color);
231 b.setColor(color);
233 setBrush(b);
232 setBrush(b);
234 }
233 }
235 }
234 }
236
235
237 QColor QLegend::color()
236 QColor QLegend::color()
238 {
237 {
239 return d_ptr->m_brush.color();
238 return d_ptr->m_brush.color();
240 }
239 }
241
240
242 /*!
241 /*!
243 Sets the \a pen of legend. Pen affects the legend borders.
242 Sets the \a pen of legend. Pen affects the legend borders.
244 */
243 */
245 void QLegend::setPen(const QPen &pen)
244 void QLegend::setPen(const QPen &pen)
246 {
245 {
247 if (d_ptr->m_pen != pen) {
246 if (d_ptr->m_pen != pen) {
248 d_ptr->m_pen = pen;
247 d_ptr->m_pen = pen;
249 update();
248 update();
250 emit borderColorChanged(pen.color());
249 emit borderColorChanged(pen.color());
251 }
250 }
252 }
251 }
253
252
254 /*!
253 /*!
255 Returns the pen used by legend
254 Returns the pen used by legend
256 */
255 */
257
256
258 QPen QLegend::pen() const
257 QPen QLegend::pen() const
259 {
258 {
260 return d_ptr->m_pen;
259 return d_ptr->m_pen;
261 }
260 }
262
261
263 void QLegend::setFont(const QFont &font)
262 void QLegend::setFont(const QFont &font)
264 {
263 {
265 if (d_ptr->m_font != font) {
264 if (d_ptr->m_font != font) {
266 d_ptr->m_font = font;
265 d_ptr->m_font = font;
267
266
268 foreach (LegendMarker *marker, d_ptr->markers()) {
267 foreach (LegendMarker *marker, d_ptr->markers()) {
269 marker->setFont(d_ptr->m_font);
268 marker->setFont(d_ptr->m_font);
270 }
269 }
271 layout()->invalidate();
270 layout()->invalidate();
272 emit fontChanged(font);
271 emit fontChanged(font);
273 }
272 }
274 }
273 }
275
274
276 QFont QLegend::font() const
275 QFont QLegend::font() const
277 {
276 {
278 return d_ptr->m_font;
277 return d_ptr->m_font;
279 }
278 }
280
279
281 void QLegend::setBorderColor(QColor color)
280 void QLegend::setBorderColor(QColor color)
282 {
281 {
283 QPen p = d_ptr->m_pen;
282 QPen p = d_ptr->m_pen;
284 if (p.color() != color) {
283 if (p.color() != color) {
285 p.setColor(color);
284 p.setColor(color);
286 setPen(p);
285 setPen(p);
287 }
286 }
288 }
287 }
289
288
290 QColor QLegend::borderColor()
289 QColor QLegend::borderColor()
291 {
290 {
292 return d_ptr->m_pen.color();
291 return d_ptr->m_pen.color();
293 }
292 }
294
293
295 /*!
294 /*!
296 Set brush used to draw labels to \a brush.
295 Set brush used to draw labels to \a brush.
297 */
296 */
298 void QLegend::setLabelBrush(const QBrush &brush)
297 void QLegend::setLabelBrush(const QBrush &brush)
299 {
298 {
300 if (d_ptr->m_labelBrush != brush) {
299 if (d_ptr->m_labelBrush != brush) {
301 d_ptr->m_labelBrush = brush;
300 d_ptr->m_labelBrush = brush;
302 foreach (LegendMarker *marker, d_ptr->markers()) {
301 foreach (LegendMarker *marker, d_ptr->markers()) {
303 marker->setLabelBrush(d_ptr->m_labelBrush);
302 marker->setLabelBrush(d_ptr->m_labelBrush);
304 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
303 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
305 // instead of mapping it from label brush color
304 // instead of mapping it from label brush color
306 marker->setPen(brush.color());
305 marker->setPen(brush.color());
307 }
306 }
308 emit labelColorChanged(brush.color());
307 emit labelColorChanged(brush.color());
309 }
308 }
310 }
309 }
311
310
312 /*!
311 /*!
313 Brush used to draw labels.
312 Brush used to draw labels.
314 */
313 */
315 QBrush QLegend::labelBrush() const
314 QBrush QLegend::labelBrush() const
316 {
315 {
317 return d_ptr->m_labelBrush;
316 return d_ptr->m_labelBrush;
318 }
317 }
319
318
320 void QLegend::setLabelColor(QColor color)
319 void QLegend::setLabelColor(QColor color)
321 {
320 {
322 QBrush b = d_ptr->m_labelBrush;
321 QBrush b = d_ptr->m_labelBrush;
323 if (b.style() != Qt::SolidPattern || b.color() != color) {
322 if (b.style() != Qt::SolidPattern || b.color() != color) {
324 b.setStyle(Qt::SolidPattern);
323 b.setStyle(Qt::SolidPattern);
325 b.setColor(color);
324 b.setColor(color);
326 setLabelBrush(b);
325 setLabelBrush(b);
327 }
326 }
328 }
327 }
329
328
330 QColor QLegend::labelColor() const
329 QColor QLegend::labelColor() const
331 {
330 {
332 return d_ptr->m_labelBrush.color();
331 return d_ptr->m_labelBrush.color();
333 }
332 }
334
333
335
334
336 void QLegend::setAlignment(Qt::Alignment alignment)
335 void QLegend::setAlignment(Qt::Alignment alignment)
337 {
336 {
338 if(d_ptr->m_alignment!=alignment) {
337 if(d_ptr->m_alignment!=alignment) {
339 d_ptr->m_alignment = alignment;
338 d_ptr->m_alignment = alignment;
340 updateGeometry();
339 updateGeometry();
341 if(isAttachedToChart()){
340 if(isAttachedToChart()){
342 d_ptr->m_presenter->layout()->invalidate();
341 d_ptr->m_presenter->layout()->invalidate();
343 }else{
342 }else{
344 layout()->invalidate();
343 layout()->invalidate();
345 }
344 }
346 }
345 }
347 }
346 }
348
347
349 Qt::Alignment QLegend::alignment() const
348 Qt::Alignment QLegend::alignment() const
350 {
349 {
351 return d_ptr->m_alignment;
350 return d_ptr->m_alignment;
352 }
351 }
353
352
354 /*!
353 /*!
355 Detaches the legend from chart. Chart won't change layout of the legend.
354 Detaches the legend from chart. Chart won't change layout of the legend.
356 */
355 */
357 void QLegend::detachFromChart()
356 void QLegend::detachFromChart()
358 {
357 {
359 d_ptr->m_attachedToChart = false;
358 d_ptr->m_attachedToChart = false;
360 d_ptr->m_layout->invalidate();
359 d_ptr->m_layout->invalidate();
361 setParent(0);
360 setParent(0);
362
361
363 }
362 }
364
363
365 /*!
364 /*!
366 Attaches the legend to chart. Chart may change layout of the legend.
365 Attaches the legend to chart. Chart may change layout of the legend.
367 */
366 */
368 void QLegend::attachToChart()
367 void QLegend::attachToChart()
369 {
368 {
370 d_ptr->m_attachedToChart = true;
369 d_ptr->m_attachedToChart = true;
371 d_ptr->m_presenter->layout()->invalidate();
370 d_ptr->m_presenter->layout()->invalidate();
372 setParent(d_ptr->m_chart);
371 setParent(d_ptr->m_chart);
373 }
372 }
374
373
375 /*!
374 /*!
376 Returns true, if legend is attached to chart.
375 Returns true, if legend is attached to chart.
377 */
376 */
378 bool QLegend::isAttachedToChart()
377 bool QLegend::isAttachedToChart()
379 {
378 {
380 return d_ptr->m_attachedToChart;
379 return d_ptr->m_attachedToChart;
381 }
380 }
382
381
383 /*!
382 /*!
384 Sets the visibility of legend background to \a visible
383 Sets the visibility of legend background to \a visible
385 */
384 */
386 void QLegend::setBackgroundVisible(bool visible)
385 void QLegend::setBackgroundVisible(bool visible)
387 {
386 {
388 if(d_ptr->m_backgroundVisible != visible) {
387 if(d_ptr->m_backgroundVisible != visible) {
389 d_ptr->m_backgroundVisible = visible;
388 d_ptr->m_backgroundVisible = visible;
390 update();
389 update();
391 emit backgroundVisibleChanged(visible);
390 emit backgroundVisibleChanged(visible);
392 }
391 }
393 }
392 }
394
393
395 /*!
394 /*!
396 Returns the visibility of legend background
395 Returns the visibility of legend background
397 */
396 */
398 bool QLegend::isBackgroundVisible() const
397 bool QLegend::isBackgroundVisible() const
399 {
398 {
400 return d_ptr->m_backgroundVisible;
399 return d_ptr->m_backgroundVisible;
401 }
400 }
402
401
403 /*!
402 /*!
404 \internal \a event see QGraphicsWidget for details
403 \internal \a event see QGraphicsWidget for details
405 */
404 */
406 void QLegend::hideEvent(QHideEvent *event)
405 void QLegend::hideEvent(QHideEvent *event)
407 {
406 {
408 d_ptr->m_presenter->layout()->invalidate();
407 d_ptr->m_presenter->layout()->invalidate();
409 QGraphicsWidget::hideEvent(event);
408 QGraphicsWidget::hideEvent(event);
410 }
409 }
411
410
412 /*!
411 /*!
413 \internal \a event see QGraphicsWidget for details
412 \internal \a event see QGraphicsWidget for details
414 */
413 */
415 void QLegend::showEvent(QShowEvent *event)
414 void QLegend::showEvent(QShowEvent *event)
416 {
415 {
417 QGraphicsWidget::showEvent(event);
416 QGraphicsWidget::showEvent(event);
418 d_ptr->m_presenter->layout()->invalidate();
417 d_ptr->m_presenter->layout()->invalidate();
419 d_ptr->items()->setVisible(false);
418 d_ptr->items()->setVisible(false);
420 //layout activation will show the items
419 //layout activation will show the items
421 }
420 }
422
421
423 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
422 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
424
423
425 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
424 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
426 q_ptr(q),
425 q_ptr(q),
427 m_presenter(presenter),
426 m_presenter(presenter),
428 m_layout(new LegendLayout(q)),
427 m_layout(new LegendLayout(q)),
429 m_chart(chart),
428 m_chart(chart),
430 m_items(new QGraphicsItemGroup(q)),
429 m_items(new QGraphicsItemGroup(q)),
431 m_alignment(Qt::AlignTop),
430 m_alignment(Qt::AlignTop),
432 m_brush(QBrush()),
431 m_brush(QBrush()),
433 m_pen(QPen()),
432 m_pen(QPen()),
434 m_labelBrush(QBrush()),
433 m_labelBrush(QBrush()),
435 m_diameter(5),
434 m_diameter(5),
436 m_attachedToChart(true),
435 m_attachedToChart(true),
437 m_backgroundVisible(false)
436 m_backgroundVisible(false)
438 {
437 {
439
438
440 }
439 }
441
440
442 QLegendPrivate::~QLegendPrivate()
441 QLegendPrivate::~QLegendPrivate()
443 {
442 {
444
443
445 }
444 }
446
445
447 void QLegendPrivate::setOffset(qreal x, qreal y)
446 void QLegendPrivate::setOffset(qreal x, qreal y)
448 {
447 {
449 m_layout->setOffset(x,y);
448 m_layout->setOffset(x,y);
450 }
449 }
451
450
452 QPointF QLegendPrivate::offset() const
451 QPointF QLegendPrivate::offset() const
453 {
452 {
454 return m_layout->offset();
453 return m_layout->offset();
455 }
454 }
456
455
457 int QLegendPrivate::roundness(qreal size)
456 int QLegendPrivate::roundness(qreal size)
458 {
457 {
459 return 100*m_diameter/int(size);
458 return 100*m_diameter/int(size);
460 }
459 }
461
460
462 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
461 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
463 {
462 {
464 Q_UNUSED(domain)
463 Q_UNUSED(domain)
465
464
466 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
465 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
467
466
468 foreach(LegendMarker* marker, markers) {
467 foreach(LegendMarker* marker, markers) {
469 marker->setFont(m_font);
468 marker->setFont(m_font);
470 marker->setLabelBrush(m_labelBrush);
469 marker->setLabelBrush(m_labelBrush);
471 marker->setVisible(series->isVisible());
470 marker->setVisible(series->isVisible());
472 m_items->addToGroup(marker);
471 m_items->addToGroup(marker);
473 m_markers<<marker;
472 m_markers<<marker;
474 }
473 }
475
474
476 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
475 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
477
476
478 if(series->type() == QAbstractSeries::SeriesTypePie) {
477 if(series->type() == QAbstractSeries::SeriesTypePie) {
479 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
478 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
480 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
479 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
481 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
480 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
482 }
481 }
483
482
484 q_ptr->layout()->invalidate();
483 q_ptr->layout()->invalidate();
485 q_ptr->layout()->activate();
484 q_ptr->layout()->activate();
486 }
485 }
487
486
488 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
487 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
489 {
488 {
490 foreach (LegendMarker *marker, m_markers) {
489 foreach (LegendMarker *marker, m_markers) {
491 if (marker->series() == series) {
490 if (marker->series() == series) {
492 delete marker;
491 delete marker;
493 m_markers.removeAll(marker);
492 m_markers.removeAll(marker);
494 }
493 }
495 }
494 }
496
495
497 if(series->type() == QAbstractSeries::SeriesTypePie)
496 if(series->type() == QAbstractSeries::SeriesTypePie)
498 {
497 {
499 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
498 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
500 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
499 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
501 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
500 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
502 }
501 }
503
502
504 q_ptr->layout()->invalidate();
503 q_ptr->layout()->invalidate();
505 }
504 }
506
505
507 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
506 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
508 {
507 {
509 // TODO: find out which markers are are added or removed. Update them
508 // TODO: find out which markers are are added or removed. Update them
510 // TODO: better implementation
509 // TODO: better implementation
511 handleSeriesRemoved(series);
510 handleSeriesRemoved(series);
512 Domain domain;
511 Domain domain;
513 handleSeriesAdded(series, &domain);
512 handleSeriesAdded(series, &domain);
514 }
513 }
515
514
516 void QLegendPrivate::handleUpdatePieSeries()
515 void QLegendPrivate::handleUpdatePieSeries()
517 {
516 {
518 //TODO: reimplement to be optimal
517 //TODO: reimplement to be optimal
519 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
518 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
520 Q_ASSERT(series);
519 Q_ASSERT(series);
521 handleSeriesRemoved(series);
520 handleSeriesRemoved(series);
522 handleSeriesAdded(series, 0);
521 handleSeriesAdded(series, 0);
523 }
522 }
524
523
525 void QLegendPrivate::handleSeriesVisibleChanged()
524 void QLegendPrivate::handleSeriesVisibleChanged()
526 {
525 {
527 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
526 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
528
527
529 foreach (LegendMarker* marker, m_markers) {
528 foreach (LegendMarker* marker, m_markers) {
530 if (marker->series() == series) {
529 if (marker->series() == series) {
531 marker->setVisible(series->isVisible());
530 marker->setVisible(series->isVisible());
532 }
531 }
533 }
532 }
534
533
535 q_ptr->layout()->invalidate();
534 q_ptr->layout()->invalidate();
536 }
535 }
537
536
538 #include "moc_qlegend.cpp"
537 #include "moc_qlegend.cpp"
539 #include "moc_qlegend_p.cpp"
538 #include "moc_qlegend_p.cpp"
540
539
541 QTCOMMERCIALCHART_END_NAMESPACE
540 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now