##// END OF EJS Templates
Bugfix for handleMarkerDestroyed(), removing it
Michal Klocek -
r870:80b01df748b7
parent child
Show More
@@ -1,545 +1,529
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 "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qseries.h"
23 #include "qseries.h"
24 #include "legendmarker_p.h"
24 #include "legendmarker_p.h"
25 #include "qxyseries.h"
25 #include "qxyseries.h"
26 #include "qlineseries.h"
26 #include "qlineseries.h"
27 #include "qareaseries.h"
27 #include "qareaseries.h"
28 #include "qscatterseries.h"
28 #include "qscatterseries.h"
29 #include "qsplineseries.h"
29 #include "qsplineseries.h"
30 #include "qbarseries.h"
30 #include "qbarseries.h"
31 #include "qstackedbarseries.h"
31 #include "qstackedbarseries.h"
32 #include "qpercentbarseries.h"
32 #include "qpercentbarseries.h"
33 #include "qbarset.h"
33 #include "qbarset.h"
34 #include "qpieseries.h"
34 #include "qpieseries.h"
35 #include "qpieslice.h"
35 #include "qpieslice.h"
36 #include "chartpresenter_p.h"
36 #include "chartpresenter_p.h"
37 #include <QPainter>
37 #include <QPainter>
38 #include <QPen>
38 #include <QPen>
39 #include <QTimer>
39 #include <QTimer>
40
40
41 #include <QGraphicsSceneEvent>
41 #include <QGraphicsSceneEvent>
42
42
43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
44
44
45 /*!
45 /*!
46 \class QLegend
46 \class QLegend
47 \brief part of QtCommercial chart API.
47 \brief part of QtCommercial chart API.
48
48
49 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
49 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
50 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
50 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
51 handle the drawing manually.
51 handle the drawing manually.
52 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
52 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
53
53
54 \mainclass
54 \mainclass
55
55
56 \sa QChart, QSeries
56 \sa QChart, QSeries
57 */
57 */
58
58
59 /*!
59 /*!
60 \enum QLegend::Layout
60 \enum QLegend::Layout
61
61
62 This enum describes the possible position for legend inside chart.
62 This enum describes the possible position for legend inside chart.
63
63
64 \value LayoutTop
64 \value LayoutTop
65 \value LayoutBottom
65 \value LayoutBottom
66 \value LayoutLeft
66 \value LayoutLeft
67 \value LayoutRight
67 \value LayoutRight
68 */
68 */
69
69
70
70
71 /*!
71 /*!
72 \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button)
72 \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button)
73 \brief Notifies when series has been clicked on legend \a series \a button
73 \brief Notifies when series has been clicked on legend \a series \a button
74 */
74 */
75
75
76 /*!
76 /*!
77 \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button)
77 \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button)
78 \brief Notifies when barset has been clicked on legend \a barset \a button
78 \brief Notifies when barset has been clicked on legend \a barset \a button
79 */
79 */
80
80
81 /*!
81 /*!
82 \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button)
82 \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button)
83 \brief Notifies when pie slice has been clicked on legend \a slice \a button
83 \brief Notifies when pie slice has been clicked on legend \a slice \a button
84 */
84 */
85
85
86 /*!
86 /*!
87 Constructs the legend object and sets the parent to \a parent
87 Constructs the legend object and sets the parent to \a parent
88 */
88 */
89
89
90 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
90 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
91 m_margin(5),
91 m_margin(5),
92 m_offsetX(0),
92 m_offsetX(0),
93 m_offsetY(0),
93 m_offsetY(0),
94 m_brush(Qt::darkGray), // TODO: default should come from theme
94 m_brush(Qt::darkGray), // TODO: default should come from theme
95 m_alignment(QLegend::AlignmentTop),
95 m_alignment(QLegend::AlignmentTop),
96 m_markers(new QGraphicsItemGroup(this)),
96 m_markers(new QGraphicsItemGroup(this)),
97 m_attachedToChart(true),
97 m_attachedToChart(true),
98 m_chart(chart),
98 m_chart(chart),
99 m_minWidth(0),
99 m_minWidth(0),
100 m_minHeight(0),
100 m_minHeight(0),
101 m_width(0),
101 m_width(0),
102 m_height(0),
102 m_height(0),
103 m_visible(false),
103 m_visible(false),
104 m_dirty(false)
104 m_dirty(false)
105 {
105 {
106 setZValue(ChartPresenter::LegendZValue);
106 setZValue(ChartPresenter::LegendZValue);
107 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
107 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
108 }
108 }
109
109
110 /*!
110 /*!
111 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
111 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
112 */
112 */
113
113
114 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
114 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
115 {
115 {
116 Q_UNUSED(option)
116 Q_UNUSED(option)
117 Q_UNUSED(widget)
117 Q_UNUSED(widget)
118 if(!m_visible) return;
118 if(!m_visible) return;
119
119
120 painter->setOpacity(opacity());
120 painter->setOpacity(opacity());
121 painter->setPen(m_pen);
121 painter->setPen(m_pen);
122 painter->setBrush(m_brush);
122 painter->setBrush(m_brush);
123 painter->drawRect(boundingRect());
123 painter->drawRect(boundingRect());
124 }
124 }
125
125
126 /*!
126 /*!
127 Bounding rect of legend.
127 Bounding rect of legend.
128 */
128 */
129
129
130 QRectF QLegend::boundingRect() const
130 QRectF QLegend::boundingRect() const
131 {
131 {
132 return m_rect;
132 return m_rect;
133 }
133 }
134
134
135 /*!
135 /*!
136 Sets the \a brush of legend. Brush affects the background of legend.
136 Sets the \a brush of legend. Brush affects the background of legend.
137 */
137 */
138 void QLegend::setBrush(const QBrush &brush)
138 void QLegend::setBrush(const QBrush &brush)
139 {
139 {
140 if (m_brush != brush) {
140 if (m_brush != brush) {
141 m_brush = brush;
141 m_brush = brush;
142 update();
142 update();
143 }
143 }
144 }
144 }
145
145
146 /*!
146 /*!
147 Returns the brush used by legend.
147 Returns the brush used by legend.
148 */
148 */
149 QBrush QLegend::brush() const
149 QBrush QLegend::brush() const
150 {
150 {
151 return m_brush;
151 return m_brush;
152 }
152 }
153
153
154 /*!
154 /*!
155 Sets the \a pen of legend. Pen affects the legend borders.
155 Sets the \a pen of legend. Pen affects the legend borders.
156 */
156 */
157 void QLegend::setPen(const QPen &pen)
157 void QLegend::setPen(const QPen &pen)
158 {
158 {
159 if (m_pen != pen) {
159 if (m_pen != pen) {
160 m_pen = pen;
160 m_pen = pen;
161 update();
161 update();
162 }
162 }
163 }
163 }
164
164
165 /*!
165 /*!
166 Returns the pen used by legend
166 Returns the pen used by legend
167 */
167 */
168
168
169 QPen QLegend::pen() const
169 QPen QLegend::pen() const
170 {
170 {
171 return m_pen;
171 return m_pen;
172 }
172 }
173
173
174 /*!
174 /*!
175 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
175 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
176 \sa QLegend::Layout
176 \sa QLegend::Layout
177 */
177 */
178 void QLegend::setAlignmnent(QLegend::Alignments alignment)
178 void QLegend::setAlignmnent(QLegend::Alignments alignment)
179 {
179 {
180 if(m_alignment!=alignment && m_attachedToChart) {
180 if(m_alignment!=alignment && m_attachedToChart) {
181 m_alignment = alignment;
181 m_alignment = alignment;
182 updateLayout();
182 updateLayout();
183 }
183 }
184 }
184 }
185
185
186 /*!
186 /*!
187 Returns the preferred layout for legend
187 Returns the preferred layout for legend
188 */
188 */
189 QLegend::Alignments QLegend::alignment() const
189 QLegend::Alignments QLegend::alignment() const
190 {
190 {
191 return m_alignment;
191 return m_alignment;
192 }
192 }
193
193
194 /*!
194 /*!
195 \internal \a series \a domain Should be called when series is added to chart.
195 \internal \a series \a domain Should be called when series is added to chart.
196 */
196 */
197 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
197 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
198 {
198 {
199 Q_UNUSED(domain)
199 Q_UNUSED(domain)
200
200
201 switch (series->type())
201 switch (series->type())
202 {
202 {
203 case QSeries::SeriesTypeLine: {
203 case QSeries::SeriesTypeLine: {
204 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
204 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
205 appendMarkers(lineSeries);
205 appendMarkers(lineSeries);
206 break;
206 break;
207 }
207 }
208 case QSeries::SeriesTypeArea: {
208 case QSeries::SeriesTypeArea: {
209 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
209 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
210 appendMarkers(areaSeries);
210 appendMarkers(areaSeries);
211 break;
211 break;
212 }
212 }
213 case QSeries::SeriesTypeBar: {
213 case QSeries::SeriesTypeBar: {
214 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
214 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
215 appendMarkers(barSeries);
215 appendMarkers(barSeries);
216 break;
216 break;
217 }
217 }
218 case QSeries::SeriesTypeStackedBar: {
218 case QSeries::SeriesTypeStackedBar: {
219 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
219 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
220 appendMarkers(stackedBarSeries);
220 appendMarkers(stackedBarSeries);
221 break;
221 break;
222 }
222 }
223 case QSeries::SeriesTypePercentBar: {
223 case QSeries::SeriesTypePercentBar: {
224 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
224 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
225 appendMarkers(percentBarSeries);
225 appendMarkers(percentBarSeries);
226 break;
226 break;
227 }
227 }
228 case QSeries::SeriesTypeScatter: {
228 case QSeries::SeriesTypeScatter: {
229 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
229 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
230 appendMarkers(scatterSeries);
230 appendMarkers(scatterSeries);
231 break;
231 break;
232 }
232 }
233 case QSeries::SeriesTypePie: {
233 case QSeries::SeriesTypePie: {
234 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
234 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
235 appendMarkers(pieSeries);
235 appendMarkers(pieSeries);
236 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
236 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
237 break;
237 break;
238 }
238 }
239 case QSeries::SeriesTypeSpline: {
239 case QSeries::SeriesTypeSpline: {
240 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
240 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
241 appendMarkers(splineSeries);
241 appendMarkers(splineSeries);
242 break;
242 break;
243 }
243 }
244 default: {
244 default: {
245 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
245 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
246 break;
246 break;
247 }
247 }
248 }
248 }
249
249
250 // wait for all series added
250 // wait for all series added
251 if(!m_dirty){
251 if(!m_dirty){
252 QTimer::singleShot(0,this,SLOT(updateLayout()));
252 QTimer::singleShot(0,this,SLOT(updateLayout()));
253 m_dirty=true;
253 m_dirty=true;
254 }
254 }
255 }
255 }
256
256
257 /*!
257 /*!
258 \internal \a series Should be called when series is removed from chart.
258 \internal \a series Should be called when series is removed from chart.
259 */
259 */
260 void QLegend::handleSeriesRemoved(QSeries *series)
260 void QLegend::handleSeriesRemoved(QSeries *series)
261 {
261 {
262 switch (series->type())
262 switch (series->type())
263 {
263 {
264 case QSeries::SeriesTypeArea: {
264 case QSeries::SeriesTypeArea: {
265 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
265 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
266 deleteMarkers(areaSeries);
266 deleteMarkers(areaSeries);
267 break;
267 break;
268 }
268 }
269 case QSeries::SeriesTypePie: {
269 case QSeries::SeriesTypePie: {
270 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
270 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
271 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
271 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
272 deleteMarkers(series);
272 deleteMarkers(series);
273 break;
273 break;
274 }
274 }
275 default: {
275 default: {
276 // All other types
276 // All other types
277 deleteMarkers(series);
277 deleteMarkers(series);
278 break;
278 break;
279 }
279 }
280 }
280 }
281
281
282 updateLayout();
282 updateLayout();
283 }
283 }
284
284
285 /*!
285 /*!
286 \internal \a slices Should be called when slices are added to pie chart.
286 \internal \a slices Should be called when slices are added to pie chart.
287 */
287 */
288 void QLegend::handleAdded(QList<QPieSlice *> slices)
288 void QLegend::handleAdded(QList<QPieSlice *> slices)
289 {
289 {
290 QPieSeries* series = static_cast<QPieSeries *> (sender());
290 QPieSeries* series = static_cast<QPieSeries *> (sender());
291 foreach(QPieSlice* slice, slices) {
291 foreach(QPieSlice* slice, slices) {
292 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
292 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
293 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
294 m_markers->addToGroup(marker);
293 m_markers->addToGroup(marker);
295 }
294 }
296 updateLayout();
295 updateLayout();
297 }
296 }
298
297
299 /*!
298 /*!
300 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
299 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
301 because removed slices are also deleted and we listen destroyed signal
300 because removed slices are also deleted and we listen destroyed signal
302 */
301 */
303 void QLegend::handleRemoved(QList<QPieSlice *> slices)
302 void QLegend::handleRemoved(QList<QPieSlice *> slices)
304 {
303 {
305 Q_UNUSED(slices)
304 Q_UNUSED(slices)
306 }
305 }
307
306
308
309 /*!
310 \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed
311 */
312 void QLegend::handleMarkerDestroyed()
313 {
314 LegendMarker* m = static_cast<LegendMarker *> (sender());
315 delete m;
316 // updateLayout();
317 }
318
319 /*!
307 /*!
320 Detaches the legend from chart. Chart won't change layout of the legend.
308 Detaches the legend from chart. Chart won't change layout of the legend.
321 */
309 */
322 void QLegend::detachFromChart()
310 void QLegend::detachFromChart()
323 {
311 {
324 m_attachedToChart = false;
312 m_attachedToChart = false;
325 }
313 }
326
314
327 /*!
315 /*!
328 Attaches the legend to chart. Chart may change layout of the legend.
316 Attaches the legend to chart. Chart may change layout of the legend.
329 */
317 */
330 void QLegend::attachToChart()
318 void QLegend::attachToChart()
331 {
319 {
332 m_attachedToChart = true;
320 m_attachedToChart = true;
333 }
321 }
334
322
335 /*!
323 /*!
336 Returns true, if legend is attached to chart.
324 Returns true, if legend is attached to chart.
337 */
325 */
338 bool QLegend::isAttachedToChart()
326 bool QLegend::isAttachedToChart()
339 {
327 {
340 return m_attachedToChart;
328 return m_attachedToChart;
341 }
329 }
342
330
343 /*!
331 /*!
344 \internal Helper function. Appends markers from \a series to legend.
332 \internal Helper function. Appends markers from \a series to legend.
345 */
333 */
346 void QLegend::appendMarkers(QAreaSeries* series)
334 void QLegend::appendMarkers(QAreaSeries* series)
347 {
335 {
348 AreaLegendMarker* marker = new AreaLegendMarker(series,this);
336 AreaLegendMarker* marker = new AreaLegendMarker(series,this);
349 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
350 m_markers->addToGroup(marker);
337 m_markers->addToGroup(marker);
351 }
338 }
352
339
353 /*!
340 /*!
354 \internal Helper function. Appends markers from \a series to legend.
341 \internal Helper function. Appends markers from \a series to legend.
355 */
342 */
356 void QLegend::appendMarkers(QXYSeries* series)
343 void QLegend::appendMarkers(QXYSeries* series)
357 {
344 {
358 XYLegendMarker* marker = new XYLegendMarker(series,this);
345 XYLegendMarker* marker = new XYLegendMarker(series,this);
359 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
360 m_markers->addToGroup(marker);
346 m_markers->addToGroup(marker);
361 }
347 }
362
348
363 /*!
349 /*!
364 \internal Helper function. Appends markers from \a series to legend.
350 \internal Helper function. Appends markers from \a series to legend.
365 */
351 */
366 void QLegend::appendMarkers(QBarSeries *series)
352 void QLegend::appendMarkers(QBarSeries *series)
367 {
353 {
368 foreach(QBarSet* set, series->barSets()) {
354 foreach(QBarSet* set, series->barSets()) {
369 BarLegendMarker* marker = new BarLegendMarker(series,set, this);
355 BarLegendMarker* marker = new BarLegendMarker(series,set, this);
370 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
371 m_markers->addToGroup(marker);
356 m_markers->addToGroup(marker);
372 }
357 }
373 }
358 }
374
359
375 /*!
360 /*!
376 \internal Helper function. Appends markers from \a series to legend.
361 \internal Helper function. Appends markers from \a series to legend.
377 */
362 */
378 void QLegend::appendMarkers(QPieSeries *series)
363 void QLegend::appendMarkers(QPieSeries *series)
379 {
364 {
380 foreach(QPieSlice* slice, series->slices()) {
365 foreach(QPieSlice* slice, series->slices()) {
381 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
366 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
382 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
383 m_markers->addToGroup(marker);
367 m_markers->addToGroup(marker);
384 }
368 }
385 }
369 }
386
370
387 /*!
371 /*!
388 \internal Deletes all markers that are created from \a series
372 \internal Deletes all markers that are created from \a series
389 */
373 */
390 void QLegend::deleteMarkers(QSeries *series)
374 void QLegend::deleteMarkers(QSeries *series)
391 {
375 {
392 // Search all markers that belong to given series and delete them.
376 // Search all markers that belong to given series and delete them.
393
377
394 QList<QGraphicsItem *> items = m_markers->childItems();
378 QList<QGraphicsItem *> items = m_markers->childItems();
395
379
396 foreach (QGraphicsItem *m, items) {
380 foreach (QGraphicsItem *markers, items) {
397 LegendMarker *marker = static_cast<LegendMarker*>(m);
381 LegendMarker *marker = static_cast<LegendMarker*>(markers);
398 if (marker->series() == series) {
382 if (marker->series() == series) {
399 delete marker;
383 delete marker;
400 }
384 }
401 }
385 }
402 }
386 }
403
387
404 /*!
388 /*!
405 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
389 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
406 If items don't fit, sets the visibility of scroll buttons accordingly.
390 If items don't fit, sets the visibility of scroll buttons accordingly.
407 Causes legend to be resized.
391 Causes legend to be resized.
408 */
392 */
409
393
410 void QLegend::setOffset(const QPointF& point)
394 void QLegend::setOffset(const QPointF& point)
411 {
395 {
412
396
413 switch(m_alignment) {
397 switch(m_alignment) {
414
398
415 case AlignmentTop:
399 case AlignmentTop:
416 case AlignmentBottom: {
400 case AlignmentBottom: {
417 if(m_width<=m_rect.width()) return;
401 if(m_width<=m_rect.width()) return;
418
402
419 if (point.x() != m_offsetX) {
403 if (point.x() != m_offsetX) {
420 m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width());
404 m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width());
421 m_markers->setPos(-m_offsetX,m_rect.top());
405 m_markers->setPos(-m_offsetX,m_rect.top());
422 }
406 }
423 break;
407 break;
424 }
408 }
425 case AlignmentLeft:
409 case AlignmentLeft:
426 case AlignmentRight: {
410 case AlignmentRight: {
427
411
428 if(m_height<=m_rect.height()) return;
412 if(m_height<=m_rect.height()) return;
429
413
430 if (point.y() != m_offsetY) {
414 if (point.y() != m_offsetY) {
431 m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height());
415 m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height());
432 m_markers->setPos(m_rect.left(),-m_offsetY);
416 m_markers->setPos(m_rect.left(),-m_offsetY);
433 }
417 }
434 break;
418 break;
435 }
419 }
436 }
420 }
437 }
421 }
438
422
439 QPointF QLegend::offset() const
423 QPointF QLegend::offset() const
440 {
424 {
441 return QPointF(m_offsetX,m_offsetY);
425 return QPointF(m_offsetX,m_offsetY);
442 }
426 }
443
427
444 // this function runs first to set min max values
428 // this function runs first to set min max values
445 void QLegend::updateLayout()
429 void QLegend::updateLayout()
446 {
430 {
447 m_dirty=false;
431 m_dirty=false;
448 m_offsetX=0;
432 m_offsetX=0;
449 QList<QGraphicsItem *> items = m_markers->childItems();
433 QList<QGraphicsItem *> items = m_markers->childItems();
450
434
451 if(items.isEmpty()) return;
435 if(items.isEmpty()) return;
452
436
453 m_minWidth=0;
437 m_minWidth=0;
454 m_minHeight=0;
438 m_minHeight=0;
455
439
456 switch(m_alignment) {
440 switch(m_alignment) {
457
441
458 case AlignmentTop:
442 case AlignmentTop:
459 case AlignmentBottom: {
443 case AlignmentBottom: {
460 QPointF point = m_rect.topLeft();
444 QPointF point = m_rect.topLeft();
461 m_width = 0;
445 m_width = 0;
462 foreach (QGraphicsItem *item, items) {
446 foreach (QGraphicsItem *item, items) {
463 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
447 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
464 const QRectF& rect = item->boundingRect();
448 const QRectF& rect = item->boundingRect();
465 qreal w = rect.width();
449 qreal w = rect.width();
466 m_minWidth=qMax(m_minWidth,w);
450 m_minWidth=qMax(m_minWidth,w);
467 m_minHeight=qMax(m_minHeight,rect.height());
451 m_minHeight=qMax(m_minHeight,rect.height());
468 m_width+=w;
452 m_width+=w;
469 point.setX(point.x() + w);
453 point.setX(point.x() + w);
470 }
454 }
471 if(m_width<m_rect.width()){
455 if(m_width<m_rect.width()){
472 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
456 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
473 }else{
457 }else{
474 m_markers->setPos(m_rect.topLeft());
458 m_markers->setPos(m_rect.topLeft());
475 }
459 }
476 m_height=m_minHeight;
460 m_height=m_minHeight;
477 }
461 }
478 break;
462 break;
479 case AlignmentLeft:
463 case AlignmentLeft:
480 case AlignmentRight:{
464 case AlignmentRight:{
481 QPointF point = m_rect.topLeft();
465 QPointF point = m_rect.topLeft();
482 m_height = 0;
466 m_height = 0;
483 foreach (QGraphicsItem *item, items) {
467 foreach (QGraphicsItem *item, items) {
484 item->setPos(point);
468 item->setPos(point);
485 const QRectF& rect = item->boundingRect();
469 const QRectF& rect = item->boundingRect();
486 qreal h = rect.height();
470 qreal h = rect.height();
487 m_minWidth=qMax(m_minWidth,rect.width());
471 m_minWidth=qMax(m_minWidth,rect.width());
488 m_minHeight=qMax(m_minHeight,h);
472 m_minHeight=qMax(m_minHeight,h);
489 m_height+=h;
473 m_height+=h;
490 point.setY(point.y() + h);
474 point.setY(point.y() + h);
491 }
475 }
492 if(m_height<m_rect.height()){
476 if(m_height<m_rect.height()){
493 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
477 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
494 }else{
478 }else{
495 m_markers->setPos(m_rect.topLeft());
479 m_markers->setPos(m_rect.topLeft());
496 }
480 }
497 m_width=m_minWidth;
481 m_width=m_minWidth;
498 }
482 }
499 break;
483 break;
500 }
484 }
501
485
502 m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme;
486 m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme;
503 }
487 }
504
488
505 void QLegend::setBackgroundVisible(bool visible)
489 void QLegend::setBackgroundVisible(bool visible)
506 {
490 {
507 if(m_visible!=visible)
491 if(m_visible!=visible)
508 {
492 {
509 m_visible=visible;
493 m_visible=visible;
510 update();
494 update();
511 }
495 }
512 }
496 }
513
497
514 bool QLegend::isBackgroundVisible() const
498 bool QLegend::isBackgroundVisible() const
515 {
499 {
516 return m_visible;
500 return m_visible;
517 }
501 }
518
502
519 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
503 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
520 {
504 {
521 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
505 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
522 QGraphicsWidget::resizeEvent(event);
506 QGraphicsWidget::resizeEvent(event);
523 if(m_rect != rect){
507 if(m_rect != rect){
524 m_rect = rect;
508 m_rect = rect;
525 updateLayout();
509 updateLayout();
526 }
510 }
527 }
511 }
528
512
529 void QLegend::hideEvent(QHideEvent *event)
513 void QLegend::hideEvent(QHideEvent *event)
530 {
514 {
531 QGraphicsWidget::hideEvent(event);
515 QGraphicsWidget::hideEvent(event);
532 setEnabled(false);
516 setEnabled(false);
533 updateLayout();
517 updateLayout();
534 }
518 }
535
519
536 void QLegend::showEvent(QShowEvent *event)
520 void QLegend::showEvent(QShowEvent *event)
537 {
521 {
538 QGraphicsWidget::showEvent(event);
522 QGraphicsWidget::showEvent(event);
539 setEnabled(true);
523 setEnabled(true);
540 updateLayout();
524 updateLayout();
541 }
525 }
542
526
543 #include "moc_qlegend.cpp"
527 #include "moc_qlegend.cpp"
544
528
545 QTCOMMERCIALCHART_END_NAMESPACE
529 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,179 +1,177
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 #ifndef QLEGEND_H
21 #ifndef QLEGEND_H
22 #define QLEGEND_H
22 #define QLEGEND_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QGraphicsWidget>
25 #include <QGraphicsWidget>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28 #include "private/scroller_p.h" //TODO fixme
28 #include "private/scroller_p.h" //TODO fixme
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class Domain;
32 class Domain;
33 class LegendMarker;
33 class LegendMarker;
34 class QPieSlice;
34 class QPieSlice;
35 class QXYSeries;
35 class QXYSeries;
36 class QBarSet;
36 class QBarSet;
37 class QBarSeries;
37 class QBarSeries;
38 class QPieSeries;
38 class QPieSeries;
39 class QAreaSeries;
39 class QAreaSeries;
40 class LegendScrollButton;
40 class LegendScrollButton;
41 class QSeries;
41 class QSeries;
42 class QChart;
42 class QChart;
43
43
44 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48
48
49 // We only support these alignments (for now)
49 // We only support these alignments (for now)
50 enum Alignment {
50 enum Alignment {
51 AlignmentTop = Qt::AlignTop,
51 AlignmentTop = Qt::AlignTop,
52 AlignmentBottom = Qt::AlignBottom,
52 AlignmentBottom = Qt::AlignBottom,
53 AlignmentLeft = Qt::AlignLeft,
53 AlignmentLeft = Qt::AlignLeft,
54 AlignmentRight = Qt::AlignRight
54 AlignmentRight = Qt::AlignRight
55 };
55 };
56
56
57 Q_DECLARE_FLAGS(Alignments, Alignment)
57 Q_DECLARE_FLAGS(Alignments, Alignment)
58
58
59 private:
59 private:
60 explicit QLegend(QChart *chart);
60 explicit QLegend(QChart *chart);
61
61
62 public:
62 public:
63 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
63 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
64 QRectF boundingRect() const;
64 QRectF boundingRect() const;
65
65
66 void setBrush(const QBrush &brush);
66 void setBrush(const QBrush &brush);
67 QBrush brush() const;
67 QBrush brush() const;
68
68
69 void setPen(const QPen &pen);
69 void setPen(const QPen &pen);
70 QPen pen() const;
70 QPen pen() const;
71
71
72 void setAlignmnent(QLegend::Alignments alignment);
72 void setAlignmnent(QLegend::Alignments alignment);
73 QLegend::Alignments alignment() const;
73 QLegend::Alignments alignment() const;
74
74
75
75
76 void detachFromChart();
76 void detachFromChart();
77 void attachToChart();
77 void attachToChart();
78 bool isAttachedToChart();
78 bool isAttachedToChart();
79
79
80 qreal minWidht() const { return m_minWidth;}
80 qreal minWidht() const { return m_minWidth;}
81 qreal minHeight() const { return m_minHeight;}
81 qreal minHeight() const { return m_minHeight;}
82
82
83 void setBackgroundVisible(bool visible);
83 void setBackgroundVisible(bool visible);
84 bool isBackgroundVisible() const;
84 bool isBackgroundVisible() const;
85
85
86 void setOffset(const QPointF& point);
86 void setOffset(const QPointF& point);
87 QPointF offset() const;
87 QPointF offset() const;
88
88
89 protected:
89 protected:
90 void resizeEvent(QGraphicsSceneResizeEvent *event);
90 void resizeEvent(QGraphicsSceneResizeEvent *event);
91 void hideEvent(QHideEvent *event);
91 void hideEvent(QHideEvent *event);
92 void showEvent(QShowEvent *event);
92 void showEvent(QShowEvent *event);
93
93
94 public Q_SLOTS:
94 public Q_SLOTS:
95 // PIMPL --->
95 // PIMPL --->
96 void handleSeriesAdded(QSeries *series, Domain *domain);
96 void handleSeriesAdded(QSeries *series, Domain *domain);
97 void handleSeriesRemoved(QSeries *series);
97 void handleSeriesRemoved(QSeries *series);
98 void handleAdded(QList<QPieSlice *> slices);
98 void handleAdded(QList<QPieSlice *> slices);
99 void handleRemoved(QList<QPieSlice *> slices);
99 void handleRemoved(QList<QPieSlice *> slices);
100 void handleMarkerDestroyed();
101
102 // PIMPL <---
100 // PIMPL <---
103
101
104 private:
102 private:
105 // PIMPL --->
103 // PIMPL --->
106 void appendMarkers(QAreaSeries *series);
104 void appendMarkers(QAreaSeries *series);
107 void appendMarkers(QXYSeries *series);
105 void appendMarkers(QXYSeries *series);
108 void appendMarkers(QBarSeries *series);
106 void appendMarkers(QBarSeries *series);
109 void appendMarkers(QPieSeries *series);
107 void appendMarkers(QPieSeries *series);
110 void deleteMarkers(QSeries *series);
108 void deleteMarkers(QSeries *series);
111
109
112
110
113
111
114
112
115 private Q_SLOTS:
113 private Q_SLOTS:
116 void updateLayout();
114 void updateLayout();
117
115
118 private:
116 private:
119 qreal m_margin;
117 qreal m_margin;
120
118
121 QRectF m_rect;
119 QRectF m_rect;
122 qreal m_offsetX;
120 qreal m_offsetX;
123 qreal m_offsetY;
121 qreal m_offsetY;
124
122
125 //QList<LegendMarker *> m_markers;
123 //QList<LegendMarker *> m_markers;
126
124
127 QBrush m_brush;
125 QBrush m_brush;
128 QPen m_pen;
126 QPen m_pen;
129 QLegend::Alignments m_alignment;
127 QLegend::Alignments m_alignment;
130 QGraphicsItemGroup* m_markers;
128 QGraphicsItemGroup* m_markers;
131
129
132
130
133 bool m_attachedToChart;
131 bool m_attachedToChart;
134
132
135 QChart *m_chart;
133 QChart *m_chart;
136 qreal m_minWidth;
134 qreal m_minWidth;
137 qreal m_minHeight;
135 qreal m_minHeight;
138 qreal m_width;
136 qreal m_width;
139 qreal m_height;
137 qreal m_height;
140 bool m_visible;
138 bool m_visible;
141 bool m_dirty;
139 bool m_dirty;
142 friend class ScrolledQLegend;
140 friend class ScrolledQLegend;
143 // <--- PIMPL
141 // <--- PIMPL
144 };
142 };
145
143
146 class ScrolledQLegend: public QLegend, public Scroller
144 class ScrolledQLegend: public QLegend, public Scroller
147 {
145 {
148
146
149 public:
147 public:
150 ScrolledQLegend(QChart *chart):QLegend(chart)
148 ScrolledQLegend(QChart *chart):QLegend(chart)
151 {
149 {
152 }
150 }
153
151
154 void setOffset(const QPointF& point)
152 void setOffset(const QPointF& point)
155 {
153 {
156 QLegend::setOffset(point);
154 QLegend::setOffset(point);
157 }
155 }
158 QPointF offset() const
156 QPointF offset() const
159 {
157 {
160 return QLegend::offset();
158 return QLegend::offset();
161 }
159 }
162
160
163 void mousePressEvent(QGraphicsSceneMouseEvent* event){
161 void mousePressEvent(QGraphicsSceneMouseEvent* event){
164 Scroller::mousePressEvent(event);
162 Scroller::mousePressEvent(event);
165 //QLegend::mousePressEvent(event);
163 //QLegend::mousePressEvent(event);
166 }
164 }
167 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
165 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
168 Scroller::mouseMoveEvent(event);
166 Scroller::mouseMoveEvent(event);
169 //QLegend::mouseMoveEvent(event);
167 //QLegend::mouseMoveEvent(event);
170 }
168 }
171 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
169 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
172 Scroller::mouseReleaseEvent(event);
170 Scroller::mouseReleaseEvent(event);
173 //QLegend::mouseReleaseEvent(event);
171 //QLegend::mouseReleaseEvent(event);
174 }
172 }
175 };
173 };
176
174
177 QTCOMMERCIALCHART_END_NAMESPACE
175 QTCOMMERCIALCHART_END_NAMESPACE
178
176
179 #endif // QLEGEND_H
177 #endif // QLEGEND_H
General Comments 0
You need to be logged in to leave comments. Login now