##// END OF EJS Templates
changed legend default visibility
sauimone -
r882:703758f6d2cf
parent child
Show More
@@ -1,523 +1,524
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_backgroundVisible(false)
104 {
104 {
105 setZValue(ChartPresenter::LegendZValue);
105 setZValue(ChartPresenter::LegendZValue);
106 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
106 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
107 setVisible(false); // By default legend is invisible
107 }
108 }
108
109
109 /*!
110 /*!
110 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.
111 */
112 */
112
113
113 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
114 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
114 {
115 {
115 Q_UNUSED(option)
116 Q_UNUSED(option)
116 Q_UNUSED(widget)
117 Q_UNUSED(widget)
117 if(!m_visible) return;
118 if(!m_backgroundVisible) return;
118
119
119 painter->setOpacity(opacity());
120 painter->setOpacity(opacity());
120 painter->setPen(m_pen);
121 painter->setPen(m_pen);
121 painter->setBrush(m_brush);
122 painter->setBrush(m_brush);
122 painter->drawRect(boundingRect());
123 painter->drawRect(boundingRect());
123 }
124 }
124
125
125 /*!
126 /*!
126 Bounding rect of legend.
127 Bounding rect of legend.
127 */
128 */
128
129
129 QRectF QLegend::boundingRect() const
130 QRectF QLegend::boundingRect() const
130 {
131 {
131 return m_rect;
132 return m_rect;
132 }
133 }
133
134
134 /*!
135 /*!
135 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.
136 */
137 */
137 void QLegend::setBrush(const QBrush &brush)
138 void QLegend::setBrush(const QBrush &brush)
138 {
139 {
139 if (m_brush != brush) {
140 if (m_brush != brush) {
140 m_brush = brush;
141 m_brush = brush;
141 update();
142 update();
142 }
143 }
143 }
144 }
144
145
145 /*!
146 /*!
146 Returns the brush used by legend.
147 Returns the brush used by legend.
147 */
148 */
148 QBrush QLegend::brush() const
149 QBrush QLegend::brush() const
149 {
150 {
150 return m_brush;
151 return m_brush;
151 }
152 }
152
153
153 /*!
154 /*!
154 Sets the \a pen of legend. Pen affects the legend borders.
155 Sets the \a pen of legend. Pen affects the legend borders.
155 */
156 */
156 void QLegend::setPen(const QPen &pen)
157 void QLegend::setPen(const QPen &pen)
157 {
158 {
158 if (m_pen != pen) {
159 if (m_pen != pen) {
159 m_pen = pen;
160 m_pen = pen;
160 update();
161 update();
161 }
162 }
162 }
163 }
163
164
164 /*!
165 /*!
165 Returns the pen used by legend
166 Returns the pen used by legend
166 */
167 */
167
168
168 QPen QLegend::pen() const
169 QPen QLegend::pen() const
169 {
170 {
170 return m_pen;
171 return m_pen;
171 }
172 }
172
173
173 /*!
174 /*!
174 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.
175 \sa QLegend::Layout
176 \sa QLegend::Layout
176 */
177 */
177 void QLegend::setAlignmnent(QLegend::Alignments alignment)
178 void QLegend::setAlignmnent(QLegend::Alignments alignment)
178 {
179 {
179 if(m_alignment!=alignment && m_attachedToChart) {
180 if(m_alignment!=alignment && m_attachedToChart) {
180 m_alignment = alignment;
181 m_alignment = alignment;
181 updateLayout();
182 updateLayout();
182 }
183 }
183 }
184 }
184
185
185 /*!
186 /*!
186 Returns the preferred layout for legend
187 Returns the preferred layout for legend
187 */
188 */
188 QLegend::Alignments QLegend::alignment() const
189 QLegend::Alignments QLegend::alignment() const
189 {
190 {
190 return m_alignment;
191 return m_alignment;
191 }
192 }
192
193
193 /*!
194 /*!
194 \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.
195 */
196 */
196 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
197 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
197 {
198 {
198 Q_UNUSED(domain)
199 Q_UNUSED(domain)
199
200
200 switch (series->type())
201 switch (series->type())
201 {
202 {
202 case QSeries::SeriesTypeLine: {
203 case QSeries::SeriesTypeLine: {
203 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
204 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
204 appendMarkers(lineSeries);
205 appendMarkers(lineSeries);
205 break;
206 break;
206 }
207 }
207 case QSeries::SeriesTypeArea: {
208 case QSeries::SeriesTypeArea: {
208 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
209 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
209 appendMarkers(areaSeries);
210 appendMarkers(areaSeries);
210 break;
211 break;
211 }
212 }
212 case QSeries::SeriesTypeBar: {
213 case QSeries::SeriesTypeBar: {
213 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
214 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
214 appendMarkers(barSeries);
215 appendMarkers(barSeries);
215 break;
216 break;
216 }
217 }
217 case QSeries::SeriesTypeStackedBar: {
218 case QSeries::SeriesTypeStackedBar: {
218 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
219 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
219 appendMarkers(stackedBarSeries);
220 appendMarkers(stackedBarSeries);
220 break;
221 break;
221 }
222 }
222 case QSeries::SeriesTypePercentBar: {
223 case QSeries::SeriesTypePercentBar: {
223 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
224 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
224 appendMarkers(percentBarSeries);
225 appendMarkers(percentBarSeries);
225 break;
226 break;
226 }
227 }
227 case QSeries::SeriesTypeScatter: {
228 case QSeries::SeriesTypeScatter: {
228 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
229 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
229 appendMarkers(scatterSeries);
230 appendMarkers(scatterSeries);
230 break;
231 break;
231 }
232 }
232 case QSeries::SeriesTypePie: {
233 case QSeries::SeriesTypePie: {
233 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
234 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
234 appendMarkers(pieSeries);
235 appendMarkers(pieSeries);
235 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
236 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
236 break;
237 break;
237 }
238 }
238 case QSeries::SeriesTypeSpline: {
239 case QSeries::SeriesTypeSpline: {
239 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
240 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
240 appendMarkers(splineSeries);
241 appendMarkers(splineSeries);
241 break;
242 break;
242 }
243 }
243 default: {
244 default: {
244 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
245 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
245 break;
246 break;
246 }
247 }
247 }
248 }
248
249
249 updateLayout();
250 updateLayout();
250 }
251 }
251
252
252 /*!
253 /*!
253 \internal \a series Should be called when series is removed from chart.
254 \internal \a series Should be called when series is removed from chart.
254 */
255 */
255 void QLegend::handleSeriesRemoved(QSeries *series)
256 void QLegend::handleSeriesRemoved(QSeries *series)
256 {
257 {
257 switch (series->type())
258 switch (series->type())
258 {
259 {
259 case QSeries::SeriesTypeArea: {
260 case QSeries::SeriesTypeArea: {
260 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
261 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
261 deleteMarkers(areaSeries);
262 deleteMarkers(areaSeries);
262 break;
263 break;
263 }
264 }
264 case QSeries::SeriesTypePie: {
265 case QSeries::SeriesTypePie: {
265 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
266 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
266 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
267 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
267 deleteMarkers(series);
268 deleteMarkers(series);
268 break;
269 break;
269 }
270 }
270 default: {
271 default: {
271 // All other types
272 // All other types
272 deleteMarkers(series);
273 deleteMarkers(series);
273 break;
274 break;
274 }
275 }
275 }
276 }
276
277
277 updateLayout();
278 updateLayout();
278 }
279 }
279
280
280 /*!
281 /*!
281 \internal \a slices Should be called when slices are added to pie chart.
282 \internal \a slices Should be called when slices are added to pie chart.
282 */
283 */
283 void QLegend::handleAdded(QList<QPieSlice *> slices)
284 void QLegend::handleAdded(QList<QPieSlice *> slices)
284 {
285 {
285 QPieSeries* series = static_cast<QPieSeries *> (sender());
286 QPieSeries* series = static_cast<QPieSeries *> (sender());
286 foreach(QPieSlice* slice, slices) {
287 foreach(QPieSlice* slice, slices) {
287 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
288 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
288 m_markers->addToGroup(marker);
289 m_markers->addToGroup(marker);
289 }
290 }
290 updateLayout();
291 updateLayout();
291 }
292 }
292
293
293 /*!
294 /*!
294 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
295 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
295 because removed slices are also deleted and we listen destroyed signal
296 because removed slices are also deleted and we listen destroyed signal
296 */
297 */
297 void QLegend::handleRemoved(QList<QPieSlice *> slices)
298 void QLegend::handleRemoved(QList<QPieSlice *> slices)
298 {
299 {
299 Q_UNUSED(slices)
300 Q_UNUSED(slices)
300 }
301 }
301
302
302 /*!
303 /*!
303 Detaches the legend from chart. Chart won't change layout of the legend.
304 Detaches the legend from chart. Chart won't change layout of the legend.
304 */
305 */
305 void QLegend::detachFromChart()
306 void QLegend::detachFromChart()
306 {
307 {
307 m_attachedToChart = false;
308 m_attachedToChart = false;
308 }
309 }
309
310
310 /*!
311 /*!
311 Attaches the legend to chart. Chart may change layout of the legend.
312 Attaches the legend to chart. Chart may change layout of the legend.
312 */
313 */
313 void QLegend::attachToChart()
314 void QLegend::attachToChart()
314 {
315 {
315 m_attachedToChart = true;
316 m_attachedToChart = true;
316 }
317 }
317
318
318 /*!
319 /*!
319 Returns true, if legend is attached to chart.
320 Returns true, if legend is attached to chart.
320 */
321 */
321 bool QLegend::isAttachedToChart()
322 bool QLegend::isAttachedToChart()
322 {
323 {
323 return m_attachedToChart;
324 return m_attachedToChart;
324 }
325 }
325
326
326 /*!
327 /*!
327 \internal Helper function. Appends markers from \a series to legend.
328 \internal Helper function. Appends markers from \a series to legend.
328 */
329 */
329 void QLegend::appendMarkers(QAreaSeries* series)
330 void QLegend::appendMarkers(QAreaSeries* series)
330 {
331 {
331 AreaLegendMarker* marker = new AreaLegendMarker(series,this);
332 AreaLegendMarker* marker = new AreaLegendMarker(series,this);
332 m_markers->addToGroup(marker);
333 m_markers->addToGroup(marker);
333 }
334 }
334
335
335 /*!
336 /*!
336 \internal Helper function. Appends markers from \a series to legend.
337 \internal Helper function. Appends markers from \a series to legend.
337 */
338 */
338 void QLegend::appendMarkers(QXYSeries* series)
339 void QLegend::appendMarkers(QXYSeries* series)
339 {
340 {
340 XYLegendMarker* marker = new XYLegendMarker(series,this);
341 XYLegendMarker* marker = new XYLegendMarker(series,this);
341 m_markers->addToGroup(marker);
342 m_markers->addToGroup(marker);
342 }
343 }
343
344
344 /*!
345 /*!
345 \internal Helper function. Appends markers from \a series to legend.
346 \internal Helper function. Appends markers from \a series to legend.
346 */
347 */
347 void QLegend::appendMarkers(QBarSeries *series)
348 void QLegend::appendMarkers(QBarSeries *series)
348 {
349 {
349 foreach(QBarSet* set, series->barSets()) {
350 foreach(QBarSet* set, series->barSets()) {
350 BarLegendMarker* marker = new BarLegendMarker(series,set, this);
351 BarLegendMarker* marker = new BarLegendMarker(series,set, this);
351 m_markers->addToGroup(marker);
352 m_markers->addToGroup(marker);
352 }
353 }
353 }
354 }
354
355
355 /*!
356 /*!
356 \internal Helper function. Appends markers from \a series to legend.
357 \internal Helper function. Appends markers from \a series to legend.
357 */
358 */
358 void QLegend::appendMarkers(QPieSeries *series)
359 void QLegend::appendMarkers(QPieSeries *series)
359 {
360 {
360 foreach(QPieSlice* slice, series->slices()) {
361 foreach(QPieSlice* slice, series->slices()) {
361 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
362 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
362 m_markers->addToGroup(marker);
363 m_markers->addToGroup(marker);
363 }
364 }
364 }
365 }
365
366
366 /*!
367 /*!
367 \internal Deletes all markers that are created from \a series
368 \internal Deletes all markers that are created from \a series
368 */
369 */
369 void QLegend::deleteMarkers(QSeries *series)
370 void QLegend::deleteMarkers(QSeries *series)
370 {
371 {
371 // Search all markers that belong to given series and delete them.
372 // Search all markers that belong to given series and delete them.
372
373
373 QList<QGraphicsItem *> items = m_markers->childItems();
374 QList<QGraphicsItem *> items = m_markers->childItems();
374
375
375 foreach (QGraphicsItem *markers, items) {
376 foreach (QGraphicsItem *markers, items) {
376 LegendMarker *marker = static_cast<LegendMarker*>(markers);
377 LegendMarker *marker = static_cast<LegendMarker*>(markers);
377 if (marker->series() == series) {
378 if (marker->series() == series) {
378 delete marker;
379 delete marker;
379 }
380 }
380 }
381 }
381 }
382 }
382
383
383 /*!
384 /*!
384 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
385 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
385 If items don't fit, sets the visibility of scroll buttons accordingly.
386 If items don't fit, sets the visibility of scroll buttons accordingly.
386 Causes legend to be resized.
387 Causes legend to be resized.
387 */
388 */
388
389
389 void QLegend::setOffset(const QPointF& point)
390 void QLegend::setOffset(const QPointF& point)
390 {
391 {
391
392
392 switch(m_alignment) {
393 switch(m_alignment) {
393
394
394 case AlignmentTop:
395 case AlignmentTop:
395 case AlignmentBottom: {
396 case AlignmentBottom: {
396 if(m_width<=m_rect.width()) return;
397 if(m_width<=m_rect.width()) return;
397
398
398 if (point.x() != m_offsetX) {
399 if (point.x() != m_offsetX) {
399 m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width());
400 m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width());
400 m_markers->setPos(-m_offsetX,m_rect.top());
401 m_markers->setPos(-m_offsetX,m_rect.top());
401 }
402 }
402 break;
403 break;
403 }
404 }
404 case AlignmentLeft:
405 case AlignmentLeft:
405 case AlignmentRight: {
406 case AlignmentRight: {
406
407
407 if(m_height<=m_rect.height()) return;
408 if(m_height<=m_rect.height()) return;
408
409
409 if (point.y() != m_offsetY) {
410 if (point.y() != m_offsetY) {
410 m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height());
411 m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height());
411 m_markers->setPos(m_rect.left(),-m_offsetY);
412 m_markers->setPos(m_rect.left(),-m_offsetY);
412 }
413 }
413 break;
414 break;
414 }
415 }
415 }
416 }
416 }
417 }
417
418
418 QPointF QLegend::offset() const
419 QPointF QLegend::offset() const
419 {
420 {
420 return QPointF(m_offsetX,m_offsetY);
421 return QPointF(m_offsetX,m_offsetY);
421 }
422 }
422
423
423 // this function runs first to set min max values
424 // this function runs first to set min max values
424 void QLegend::updateLayout()
425 void QLegend::updateLayout()
425 {
426 {
426 m_offsetX=0;
427 m_offsetX=0;
427 QList<QGraphicsItem *> items = m_markers->childItems();
428 QList<QGraphicsItem *> items = m_markers->childItems();
428
429
429 if(items.isEmpty()) return;
430 if(items.isEmpty()) return;
430
431
431 m_minWidth=0;
432 m_minWidth=0;
432 m_minHeight=0;
433 m_minHeight=0;
433
434
434 switch(m_alignment) {
435 switch(m_alignment) {
435
436
436 case AlignmentTop:
437 case AlignmentTop:
437 case AlignmentBottom: {
438 case AlignmentBottom: {
438 QPointF point = m_rect.topLeft();
439 QPointF point = m_rect.topLeft();
439 m_width = 0;
440 m_width = 0;
440 foreach (QGraphicsItem *item, items) {
441 foreach (QGraphicsItem *item, items) {
441 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
442 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
442 const QRectF& rect = item->boundingRect();
443 const QRectF& rect = item->boundingRect();
443 qreal w = rect.width();
444 qreal w = rect.width();
444 m_minWidth=qMax(m_minWidth,w);
445 m_minWidth=qMax(m_minWidth,w);
445 m_minHeight=qMax(m_minHeight,rect.height());
446 m_minHeight=qMax(m_minHeight,rect.height());
446 m_width+=w;
447 m_width+=w;
447 point.setX(point.x() + w);
448 point.setX(point.x() + w);
448 }
449 }
449 if(m_width<m_rect.width()){
450 if(m_width<m_rect.width()){
450 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
451 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
451 }else{
452 }else{
452 m_markers->setPos(m_rect.topLeft());
453 m_markers->setPos(m_rect.topLeft());
453 }
454 }
454 m_height=m_minHeight;
455 m_height=m_minHeight;
455 }
456 }
456 break;
457 break;
457 case AlignmentLeft:
458 case AlignmentLeft:
458 case AlignmentRight:{
459 case AlignmentRight:{
459 QPointF point = m_rect.topLeft();
460 QPointF point = m_rect.topLeft();
460 m_height = 0;
461 m_height = 0;
461 foreach (QGraphicsItem *item, items) {
462 foreach (QGraphicsItem *item, items) {
462 item->setPos(point);
463 item->setPos(point);
463 const QRectF& rect = item->boundingRect();
464 const QRectF& rect = item->boundingRect();
464 qreal h = rect.height();
465 qreal h = rect.height();
465 m_minWidth=qMax(m_minWidth,rect.width());
466 m_minWidth=qMax(m_minWidth,rect.width());
466 m_minHeight=qMax(m_minHeight,h);
467 m_minHeight=qMax(m_minHeight,h);
467 m_height+=h;
468 m_height+=h;
468 point.setY(point.y() + h);
469 point.setY(point.y() + h);
469 }
470 }
470 if(m_height<m_rect.height()){
471 if(m_height<m_rect.height()){
471 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
472 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
472 }else{
473 }else{
473 m_markers->setPos(m_rect.topLeft());
474 m_markers->setPos(m_rect.topLeft());
474 }
475 }
475 m_width=m_minWidth;
476 m_width=m_minWidth;
476 }
477 }
477 break;
478 break;
478 }
479 }
479
480
480 m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme;
481 m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme;
481 }
482 }
482
483
483 void QLegend::setBackgroundVisible(bool visible)
484 void QLegend::setBackgroundVisible(bool visible)
484 {
485 {
485 if(m_visible!=visible)
486 if(m_backgroundVisible!=visible)
486 {
487 {
487 m_visible=visible;
488 m_backgroundVisible=visible;
488 update();
489 update();
489 }
490 }
490 }
491 }
491
492
492 bool QLegend::isBackgroundVisible() const
493 bool QLegend::isBackgroundVisible() const
493 {
494 {
494 return m_visible;
495 return m_backgroundVisible;
495 }
496 }
496
497
497 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
498 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
498 {
499 {
499 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
500 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
500 QGraphicsWidget::resizeEvent(event);
501 QGraphicsWidget::resizeEvent(event);
501 if(m_rect != rect){
502 if(m_rect != rect){
502 m_rect = rect;
503 m_rect = rect;
503 updateLayout();
504 updateLayout();
504 }
505 }
505 }
506 }
506
507
507 void QLegend::hideEvent(QHideEvent *event)
508 void QLegend::hideEvent(QHideEvent *event)
508 {
509 {
509 QGraphicsWidget::hideEvent(event);
510 QGraphicsWidget::hideEvent(event);
510 setEnabled(false);
511 setEnabled(false);
511 updateLayout();
512 updateLayout();
512 }
513 }
513
514
514 void QLegend::showEvent(QShowEvent *event)
515 void QLegend::showEvent(QShowEvent *event)
515 {
516 {
516 QGraphicsWidget::showEvent(event);
517 QGraphicsWidget::showEvent(event);
517 setEnabled(true);
518 setEnabled(true);
518 updateLayout();
519 updateLayout();
519 }
520 }
520
521
521 #include "moc_qlegend.cpp"
522 #include "moc_qlegend.cpp"
522
523
523 QTCOMMERCIALCHART_END_NAMESPACE
524 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,171 +1,171
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 = true);
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 // PIMPL <---
100 // PIMPL <---
101
101
102 private:
102 private:
103 // PIMPL --->
103 // PIMPL --->
104 void appendMarkers(QAreaSeries *series);
104 void appendMarkers(QAreaSeries *series);
105 void appendMarkers(QXYSeries *series);
105 void appendMarkers(QXYSeries *series);
106 void appendMarkers(QBarSeries *series);
106 void appendMarkers(QBarSeries *series);
107 void appendMarkers(QPieSeries *series);
107 void appendMarkers(QPieSeries *series);
108 void deleteMarkers(QSeries *series);
108 void deleteMarkers(QSeries *series);
109 void updateLayout();
109 void updateLayout();
110
110
111 private:
111 private:
112 qreal m_margin;
112 qreal m_margin;
113
113
114 QRectF m_rect;
114 QRectF m_rect;
115 qreal m_offsetX;
115 qreal m_offsetX;
116 qreal m_offsetY;
116 qreal m_offsetY;
117
117
118 //QList<LegendMarker *> m_markers;
118 //QList<LegendMarker *> m_markers;
119
119
120 QBrush m_brush;
120 QBrush m_brush;
121 QPen m_pen;
121 QPen m_pen;
122 QLegend::Alignments m_alignment;
122 QLegend::Alignments m_alignment;
123 QGraphicsItemGroup* m_markers;
123 QGraphicsItemGroup* m_markers;
124
124
125
125
126 bool m_attachedToChart;
126 bool m_attachedToChart;
127
127
128 QChart *m_chart;
128 QChart *m_chart;
129 qreal m_minWidth;
129 qreal m_minWidth;
130 qreal m_minHeight;
130 qreal m_minHeight;
131 qreal m_width;
131 qreal m_width;
132 qreal m_height;
132 qreal m_height;
133 bool m_visible;
133 bool m_backgroundVisible;
134 friend class ScrolledQLegend;
134 friend class ScrolledQLegend;
135 // <--- PIMPL
135 // <--- PIMPL
136 };
136 };
137
137
138 class ScrolledQLegend: public QLegend, public Scroller
138 class ScrolledQLegend: public QLegend, public Scroller
139 {
139 {
140
140
141 public:
141 public:
142 ScrolledQLegend(QChart *chart):QLegend(chart)
142 ScrolledQLegend(QChart *chart):QLegend(chart)
143 {
143 {
144 }
144 }
145
145
146 void setOffset(const QPointF& point)
146 void setOffset(const QPointF& point)
147 {
147 {
148 QLegend::setOffset(point);
148 QLegend::setOffset(point);
149 }
149 }
150 QPointF offset() const
150 QPointF offset() const
151 {
151 {
152 return QLegend::offset();
152 return QLegend::offset();
153 }
153 }
154
154
155 void mousePressEvent(QGraphicsSceneMouseEvent* event){
155 void mousePressEvent(QGraphicsSceneMouseEvent* event){
156 Scroller::mousePressEvent(event);
156 Scroller::mousePressEvent(event);
157 //QLegend::mousePressEvent(event);
157 //QLegend::mousePressEvent(event);
158 }
158 }
159 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
159 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
160 Scroller::mouseMoveEvent(event);
160 Scroller::mouseMoveEvent(event);
161 //QLegend::mouseMoveEvent(event);
161 //QLegend::mouseMoveEvent(event);
162 }
162 }
163 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
163 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
164 Scroller::mouseReleaseEvent(event);
164 Scroller::mouseReleaseEvent(event);
165 //QLegend::mouseReleaseEvent(event);
165 //QLegend::mouseReleaseEvent(event);
166 }
166 }
167 };
167 };
168
168
169 QTCOMMERCIALCHART_END_NAMESPACE
169 QTCOMMERCIALCHART_END_NAMESPACE
170
170
171 #endif // QLEGEND_H
171 #endif // QLEGEND_H
General Comments 0
You need to be logged in to leave comments. Login now