##// END OF EJS Templates
Adding legend properties
Tero Ahola -
r1452:aa62563de463
parent child
Show More
@@ -1,15 +1,16
1 /*!
1 /*!
2 \page qml.html
2 \page qml.html
3 \title QtCommercial Charts QML API
3 \title QtCommercial Charts QML API
4 \keyword Charts QML API
4 \keyword Charts QML API
5
5
6 Charts QML API is an intuitive and simple way to use QtCommercial Charts in your QML applications.
6 Charts QML API is an intuitive and simple way to use QtCommercial Charts in your QML applications.
7
7
8 \raw HTML
8 \raw HTML
9 <ul>
9 <ul>
10 <li><a href="qml-chartview.html">ChartView</a></li>
10 <li><a href="qml-chartview.html">ChartView</a></li>
11 <li><a href="qml-pieseries.html">PieSeries</a></li>
11 <li><a href="qml-pieseries.html">PieSeries</a></li>
12 <li><a href="qml-axis.html">Axis</a></li>
12 <li><a href="qml-axis.html">Axis</a></li>
13 <li><a href="qml-legend.html">Legend</a></li>
13 </ul>
14 </ul>
14 \endraw
15 \endraw
15 */
16 */
@@ -1,652 +1,740
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
26
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 "qbarseries.h"
33 #include "qbarseries.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
44
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 part of QtCommercial chart API.
51 \brief part of QtCommercial chart API.
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 /*!
64 \qmlclass Legend QLegend
65 \brief Legend is part of QtCommercial Chart QML API.
66
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 referenced via ChartView class. For example:
69 \code
70 ChartView {
71 legend.visible: true
72 legend.alignment: Qt.AlignBottom
73 // Add a few series...
74 }
75 \endcode
76
77 \image examples_percentbarchart_legend.png
78 */
79
80 /*!
81 \property QLegend::alignment
82 \brief The alignment of the legend.
83
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.
86 */
87 /*!
88 \qmlproperty Qt.Alignment Legend::alignment
89 \brief The alignment of the legend.
90
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.
93 */
94
95 /*!
96 \property QLegend::backgroundVisible
97 Whether the legend background is visible or not.
98 */
99 /*!
100 \qmlproperty bool Legend::backgroundVisible
101 Whether the legend background is visible or not.
102 */
103
104 /*!
105 \property QLegend::color
106 The color of the legend, i.e. the background color.
107 */
108 /*!
109 \qmlproperty color Legend::color
110 The color of the legend, i.e. the background color.
111 */
112
113 /*!
114 \property QLegend::borderColor
115 The border color of the legend, i.e. the line color.
116 */
117 /*!
118 \qmlproperty color Legend::borderColor
119 The border color of the legend, i.e. the line color.
120 */
63
121
64 /*!
122 /*!
65 \fn void QLegend::alignmentChanged()
123 \fn void QLegend::alignmentChanged(Qt::Alignment)
66 Emitted when the alignment of the legend changes.
124 Emitted when the \a alignment of the legend changes.
125 */
126
127 /*!
128 \fn void QLegend::backgroundVisibleChanged(bool)
129 The visibility of the legend background changed to \a visible.
130 */
131
132 /*!
133 \fn void QLegend::colorChanged(QColor)
134 The color of the legend background changed to \a color.
135 */
136
137 /*!
138 \fn void QLegend::borderColorChanged(QColor)
139 The border color of the legend background changed to \a color.
67 */
140 */
68
141
69 /*!
142 /*!
70 \fn qreal QLegend::minWidth() const
143 \fn qreal QLegend::minWidth() const
71 Returns minimum width of the legend
144 Returns minimum width of the legend
72 */
145 */
73
146
74 /*!
147 /*!
75 \fn qreal QLegend::minHeight() const
148 \fn qreal QLegend::minHeight() const
76 Returns minimum height of the legend
149 Returns minimum height of the legend
77 */
150 */
78
151
79 /*!
152 /*!
80 Constructs the legend object and sets the parent to \a parent
153 Constructs the legend object and sets the parent to \a parent
81 */
154 */
82
155
83 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
156 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
84 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
157 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
85 {
158 {
86 setZValue(ChartPresenter::LegendZValue);
159 setZValue(ChartPresenter::LegendZValue);
87 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
160 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
88 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
161 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
89 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
162 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
90 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
163 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
91 }
164 }
92
165
93 /*!
166 /*!
94 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
167 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
95 */
168 */
96 QLegend::~QLegend()
169 QLegend::~QLegend()
97 {
170 {
98 }
171 }
99
172
100 /*!
173 /*!
101 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
174 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
102 */
175 */
103
176
104 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
177 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
105 {
178 {
106 Q_UNUSED(option)
179 Q_UNUSED(option)
107 Q_UNUSED(widget)
180 Q_UNUSED(widget)
108 if(!d_ptr->m_backgroundVisible) return;
181 if(!d_ptr->m_backgroundVisible) return;
109
182
110 painter->setOpacity(opacity());
183 painter->setOpacity(opacity());
111 painter->setPen(d_ptr->m_pen);
184 painter->setPen(d_ptr->m_pen);
112 painter->setBrush(d_ptr->m_brush);
185 painter->setBrush(d_ptr->m_brush);
113 painter->drawRect(boundingRect());
186 painter->drawRect(boundingRect());
114 }
187 }
115
188
116 /*!
189 /*!
117 Bounding rect of legend.
190 Bounding rect of legend.
118 */
191 */
119
192
120 QRectF QLegend::boundingRect() const
193 QRectF QLegend::boundingRect() const
121 {
194 {
122 return d_ptr->m_rect;
195 return d_ptr->m_rect;
123 }
196 }
124
197
125 /*!
198 /*!
126 Sets the \a brush of legend. Brush affects the background of legend.
199 Sets the \a brush of legend. Brush affects the background of legend.
127 */
200 */
128 void QLegend::setBrush(const QBrush &brush)
201 void QLegend::setBrush(const QBrush &brush)
129 {
202 {
130 if (d_ptr->m_brush != brush) {
203 if (d_ptr->m_brush != brush) {
131 d_ptr->m_brush = brush;
204 d_ptr->m_brush = brush;
132 update();
205 update();
133 }
206 }
134 }
207 }
135
208
136 /*!
209 /*!
137 Returns the brush used by legend.
210 Returns the brush used by legend.
138 */
211 */
139 QBrush QLegend::brush() const
212 QBrush QLegend::brush() const
140 {
213 {
141 return d_ptr->m_brush;
214 return d_ptr->m_brush;
142 }
215 }
143
216
217 void QLegend::setColor(QColor color)
218 {
219 QBrush b = d_ptr->m_brush;
220 if (b.color() != color) {
221 b.setColor(color);
222 setBrush(b);
223 emit colorChanged(color);
224 }
225 }
226
227 QColor QLegend::color()
228 {
229 return d_ptr->m_brush.color();
230 }
231
144 /*!
232 /*!
145 Sets the \a pen of legend. Pen affects the legend borders.
233 Sets the \a pen of legend. Pen affects the legend borders.
146 */
234 */
147 void QLegend::setPen(const QPen &pen)
235 void QLegend::setPen(const QPen &pen)
148 {
236 {
149 if (d_ptr->m_pen != pen) {
237 if (d_ptr->m_pen != pen) {
150 d_ptr->m_pen = pen;
238 d_ptr->m_pen = pen;
151 update();
239 update();
152 }
240 }
153 }
241 }
154
242
155 /*!
243 /*!
156 Returns the pen used by legend
244 Returns the pen used by legend
157 */
245 */
158
246
159 QPen QLegend::pen() const
247 QPen QLegend::pen() const
160 {
248 {
161 return d_ptr->m_pen;
249 return d_ptr->m_pen;
162 }
250 }
163
251
164 /*!
252 void QLegend::setBorderColor(QColor color)
165 \property QLegend::alignment
253 {
166 \brief The alignment of the legend.
254 QPen p = d_ptr->m_pen;
167 */
255 if (p.color() != color) {
256 p.setColor(color);
257 setPen(p);
258 emit borderColorChanged(color);
259 }
260 }
168
261
169 /*!
262 QColor QLegend::borderColor()
170 Sets the \a alignment for legend. Legend paints on the defined position in chart. The following alignments are
263 {
171 supported: Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result
264 return d_ptr->m_pen.color();
172 is undefined.
265 }
173
266
174 \sa QLegend::Alignment
175 */
176 void QLegend::setAlignment(Qt::Alignment alignment)
267 void QLegend::setAlignment(Qt::Alignment alignment)
177 {
268 {
178 if(d_ptr->m_alignment!=alignment) {
269 if(d_ptr->m_alignment!=alignment) {
179 d_ptr->m_alignment = alignment;
270 d_ptr->m_alignment = alignment;
180 d_ptr->updateLayout();
271 d_ptr->updateLayout();
181 alignmentChanged();
272 alignmentChanged(alignment);
182 }
273 }
183 }
274 }
184
275
185 /*!
186 Returns the preferred layout for legend
187 */
188 Qt::Alignment QLegend::alignment() const
276 Qt::Alignment QLegend::alignment() const
189 {
277 {
190 return d_ptr->m_alignment;
278 return d_ptr->m_alignment;
191 }
279 }
192
280
193 /*!
281 /*!
194 Detaches the legend from chart. Chart won't change layout of the legend.
282 Detaches the legend from chart. Chart won't change layout of the legend.
195 */
283 */
196 void QLegend::detachFromChart()
284 void QLegend::detachFromChart()
197 {
285 {
198 d_ptr->m_attachedToChart = false;
286 d_ptr->m_attachedToChart = false;
199 }
287 }
200
288
201 /*!
289 /*!
202 Attaches the legend to chart. Chart may change layout of the legend.
290 Attaches the legend to chart. Chart may change layout of the legend.
203 */
291 */
204 void QLegend::attachToChart()
292 void QLegend::attachToChart()
205 {
293 {
206 d_ptr->attachToChart();
294 d_ptr->attachToChart();
207 }
295 }
208
296
209 /*!
297 /*!
210 Returns true, if legend is attached to chart.
298 Returns true, if legend is attached to chart.
211 */
299 */
212 bool QLegend::isAttachedToChart()
300 bool QLegend::isAttachedToChart()
213 {
301 {
214 return d_ptr->m_attachedToChart;
302 return d_ptr->m_attachedToChart;
215 }
303 }
216
304
217 /*!
305 /*!
218 Sets the legend's scrolling offset to value defined by \a point.
306 Sets the legend's scrolling offset to value defined by \a point.
219 */
307 */
220 void QLegend::setOffset(const QPointF& point)
308 void QLegend::setOffset(const QPointF& point)
221 {
309 {
222 d_ptr->setOffset(point.x(),point.y());
310 d_ptr->setOffset(point.x(),point.y());
223 }
311 }
224
312
225 /*!
313 /*!
226 Returns the legend's scrolling offset.
314 Returns the legend's scrolling offset.
227 */
315 */
228 QPointF QLegend::offset() const
316 QPointF QLegend::offset() const
229 {
317 {
230 return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY);
318 return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY);
231 }
319 }
232
320
233 /*!
321 /*!
234 Sets the visibility of legend background to \a visible
322 Sets the visibility of legend background to \a visible
235 */
323 */
236 void QLegend::setBackgroundVisible(bool visible)
324 void QLegend::setBackgroundVisible(bool visible)
237 {
325 {
238 if(d_ptr->m_backgroundVisible!=visible)
326 if(d_ptr->m_backgroundVisible != visible) {
239 {
327 d_ptr->m_backgroundVisible = visible;
240 d_ptr->m_backgroundVisible=visible;
241 update();
328 update();
329 emit backgroundVisibleChanged(visible);
242 }
330 }
243 }
331 }
244
332
245 /*!
333 /*!
246 Returns the visibility of legend background
334 Returns the visibility of legend background
247 */
335 */
248 bool QLegend::isBackgroundVisible() const
336 bool QLegend::isBackgroundVisible() const
249 {
337 {
250 return d_ptr->m_backgroundVisible;
338 return d_ptr->m_backgroundVisible;
251 }
339 }
252
340
253 /*!
341 /*!
254 \internal \a event see QGraphicsWidget for details
342 \internal \a event see QGraphicsWidget for details
255 */
343 */
256 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
344 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
257 {
345 {
258 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
346 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
259 QGraphicsWidget::resizeEvent(event);
347 QGraphicsWidget::resizeEvent(event);
260 if(d_ptr->m_rect != rect) {
348 if(d_ptr->m_rect != rect) {
261 d_ptr->m_rect = rect;
349 d_ptr->m_rect = rect;
262 d_ptr->updateLayout();
350 d_ptr->updateLayout();
263 }
351 }
264 }
352 }
265
353
266 /*!
354 /*!
267 \internal \a event see QGraphicsWidget for details
355 \internal \a event see QGraphicsWidget for details
268 */
356 */
269 void QLegend::hideEvent(QHideEvent *event)
357 void QLegend::hideEvent(QHideEvent *event)
270 {
358 {
271 QGraphicsWidget::hideEvent(event);
359 QGraphicsWidget::hideEvent(event);
272 setEnabled(false);
360 setEnabled(false);
273 d_ptr->updateLayout();
361 d_ptr->updateLayout();
274 }
362 }
275
363
276 /*!
364 /*!
277 \internal \a event see QGraphicsWidget for details
365 \internal \a event see QGraphicsWidget for details
278 */
366 */
279 void QLegend::showEvent(QShowEvent *event)
367 void QLegend::showEvent(QShowEvent *event)
280 {
368 {
281 QGraphicsWidget::showEvent(event);
369 QGraphicsWidget::showEvent(event);
282 setEnabled(true);
370 setEnabled(true);
283 d_ptr->updateLayout();
371 d_ptr->updateLayout();
284 }
372 }
285
373
286 qreal QLegend::minWidth() const
374 qreal QLegend::minWidth() const
287 {
375 {
288 return d_ptr->m_minWidth;
376 return d_ptr->m_minWidth;
289 }
377 }
290
378
291 qreal QLegend::minHeight() const
379 qreal QLegend::minHeight() const
292 {
380 {
293 return d_ptr->m_minHeight;
381 return d_ptr->m_minHeight;
294 }
382 }
295
383
296 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
384 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
297
385
298 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
386 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
299 q_ptr(q),
387 q_ptr(q),
300 m_presenter(presenter),
388 m_presenter(presenter),
301 m_chart(chart),
389 m_chart(chart),
302 m_markers(new QGraphicsItemGroup(q)),
390 m_markers(new QGraphicsItemGroup(q)),
303 m_alignment(Qt::AlignTop),
391 m_alignment(Qt::AlignTop),
304 m_offsetX(0),
392 m_offsetX(0),
305 m_offsetY(0),
393 m_offsetY(0),
306 m_minWidth(0),
394 m_minWidth(0),
307 m_minHeight(0),
395 m_minHeight(0),
308 m_width(0),
396 m_width(0),
309 m_height(0),
397 m_height(0),
310 m_attachedToChart(true),
398 m_attachedToChart(true),
311 m_backgroundVisible(false)
399 m_backgroundVisible(false)
312 {
400 {
313
401
314 }
402 }
315
403
316 QLegendPrivate::~QLegendPrivate()
404 QLegendPrivate::~QLegendPrivate()
317 {
405 {
318
406
319 }
407 }
320
408
321 void QLegendPrivate::setOffset(qreal x, qreal y)
409 void QLegendPrivate::setOffset(qreal x, qreal y)
322 {
410 {
323 bool scrollHorizontal = true;
411 bool scrollHorizontal = true;
324 switch(m_alignment) {
412 switch(m_alignment) {
325 case Qt::AlignTop:
413 case Qt::AlignTop:
326 case Qt::AlignBottom: {
414 case Qt::AlignBottom: {
327 scrollHorizontal = true;
415 scrollHorizontal = true;
328 break;
416 break;
329 }
417 }
330 case Qt::AlignLeft:
418 case Qt::AlignLeft:
331 case Qt::AlignRight: {
419 case Qt::AlignRight: {
332 scrollHorizontal = false;
420 scrollHorizontal = false;
333 break;
421 break;
334 }
422 }
335 }
423 }
336
424
337 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
425 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
338 if (!m_attachedToChart) {
426 if (!m_attachedToChart) {
339 scrollHorizontal = !scrollHorizontal;
427 scrollHorizontal = !scrollHorizontal;
340 }
428 }
341
429
342 // Limit offset between m_minOffset and m_maxOffset
430 // Limit offset between m_minOffset and m_maxOffset
343 if (scrollHorizontal) {
431 if (scrollHorizontal) {
344 if(m_width<=m_rect.width()) return;
432 if(m_width<=m_rect.width()) return;
345
433
346 if (x != m_offsetX) {
434 if (x != m_offsetX) {
347 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
435 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
348 m_markers->setPos(-m_offsetX,m_rect.top());
436 m_markers->setPos(-m_offsetX,m_rect.top());
349 }
437 }
350 } else {
438 } else {
351 if(m_height<=m_rect.height()) return;
439 if(m_height<=m_rect.height()) return;
352
440
353 if (y != m_offsetY) {
441 if (y != m_offsetY) {
354 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
442 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
355 m_markers->setPos(m_rect.left(),-m_offsetY);
443 m_markers->setPos(m_rect.left(),-m_offsetY);
356 }
444 }
357 }
445 }
358 }
446 }
359
447
360
448
361 void QLegendPrivate::updateLayout()
449 void QLegendPrivate::updateLayout()
362 {
450 {
363 if (!m_attachedToChart) {
451 if (!m_attachedToChart) {
364 updateDetachedLayout();
452 updateDetachedLayout();
365 return;
453 return;
366 }
454 }
367
455
368 m_offsetX=0;
456 m_offsetX=0;
369 QList<QGraphicsItem *> items = m_markers->childItems();
457 QList<QGraphicsItem *> items = m_markers->childItems();
370
458
371 if(items.isEmpty()) return;
459 if(items.isEmpty()) return;
372
460
373 m_minWidth=0;
461 m_minWidth=0;
374 m_minHeight=0;
462 m_minHeight=0;
375
463
376 switch(m_alignment) {
464 switch(m_alignment) {
377
465
378 case Qt::AlignTop:
466 case Qt::AlignTop:
379 case Qt::AlignBottom: {
467 case Qt::AlignBottom: {
380 QPointF point = m_rect.topLeft();
468 QPointF point = m_rect.topLeft();
381 m_width = 0;
469 m_width = 0;
382 foreach (QGraphicsItem *item, items) {
470 foreach (QGraphicsItem *item, items) {
383 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
471 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
384 const QRectF& rect = item->boundingRect();
472 const QRectF& rect = item->boundingRect();
385 qreal w = rect.width();
473 qreal w = rect.width();
386 m_minWidth=qMax(m_minWidth,w);
474 m_minWidth=qMax(m_minWidth,w);
387 m_minHeight=qMax(m_minHeight,rect.height());
475 m_minHeight=qMax(m_minHeight,rect.height());
388 m_width+=w;
476 m_width+=w;
389 point.setX(point.x() + w);
477 point.setX(point.x() + w);
390 }
478 }
391 if(m_width<m_rect.width()) {
479 if(m_width<m_rect.width()) {
392 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
480 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
393 }
481 }
394 else {
482 else {
395 m_markers->setPos(m_rect.topLeft());
483 m_markers->setPos(m_rect.topLeft());
396 }
484 }
397 m_height=m_minHeight;
485 m_height=m_minHeight;
398 }
486 }
399 break;
487 break;
400 case Qt::AlignLeft:
488 case Qt::AlignLeft:
401 case Qt::AlignRight: {
489 case Qt::AlignRight: {
402 QPointF point = m_rect.topLeft();
490 QPointF point = m_rect.topLeft();
403 m_height = 0;
491 m_height = 0;
404 foreach (QGraphicsItem *item, items) {
492 foreach (QGraphicsItem *item, items) {
405 item->setPos(point);
493 item->setPos(point);
406 const QRectF& rect = item->boundingRect();
494 const QRectF& rect = item->boundingRect();
407 qreal h = rect.height();
495 qreal h = rect.height();
408 m_minWidth=qMax(m_minWidth,rect.width());
496 m_minWidth=qMax(m_minWidth,rect.width());
409 m_minHeight=qMax(m_minHeight,h);
497 m_minHeight=qMax(m_minHeight,h);
410 m_height+=h;
498 m_height+=h;
411 point.setY(point.y() + h);
499 point.setY(point.y() + h);
412 }
500 }
413 if(m_height<m_rect.height()) {
501 if(m_height<m_rect.height()) {
414 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
502 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
415 }
503 }
416 else {
504 else {
417 m_markers->setPos(m_rect.topLeft());
505 m_markers->setPos(m_rect.topLeft());
418 }
506 }
419 m_width=m_minWidth;
507 m_width=m_minWidth;
420 }
508 }
421 break;
509 break;
422 }
510 }
423
511
424 m_minOffsetX = 0;
512 m_minOffsetX = 0;
425 m_minOffsetY = 0;
513 m_minOffsetY = 0;
426 m_maxOffsetX = m_width - m_rect.width();
514 m_maxOffsetX = m_width - m_rect.width();
427 m_maxOffsetY = m_height - m_rect.height();
515 m_maxOffsetY = m_height - m_rect.height();
428
516
429 m_presenter->updateLayout();
517 m_presenter->updateLayout();
430 }
518 }
431
519
432 void QLegendPrivate::updateDetachedLayout()
520 void QLegendPrivate::updateDetachedLayout()
433 {
521 {
434 // Detached layout is different.
522 // Detached layout is different.
435 // In detached mode legend may have multiple rows and columns, so layout calculations
523 // In detached mode legend may have multiple rows and columns, so layout calculations
436 // differ a log from attached mode.
524 // differ a log from attached mode.
437 // Also the scrolling logic is bit different.
525 // Also the scrolling logic is bit different.
438 m_offsetX=0;
526 m_offsetX=0;
439 m_offsetY=0;
527 m_offsetY=0;
440 QList<QGraphicsItem *> items = m_markers->childItems();
528 QList<QGraphicsItem *> items = m_markers->childItems();
441
529
442 if(items.isEmpty()) return;
530 if(items.isEmpty()) return;
443
531
444 m_minWidth = 0;
532 m_minWidth = 0;
445 m_minHeight = 0;
533 m_minHeight = 0;
446
534
447 switch (m_alignment) {
535 switch (m_alignment) {
448 case Qt::AlignTop: {
536 case Qt::AlignTop: {
449 QPointF point = m_rect.topLeft();
537 QPointF point = m_rect.topLeft();
450 m_width = 0;
538 m_width = 0;
451 m_height = 0;
539 m_height = 0;
452 for (int i=0; i<items.count(); i++) {
540 for (int i=0; i<items.count(); i++) {
453 QGraphicsItem *item = items.at(i);
541 QGraphicsItem *item = items.at(i);
454 const QRectF& rect = item->boundingRect();
542 const QRectF& rect = item->boundingRect();
455 qreal w = rect.width();
543 qreal w = rect.width();
456 qreal h = rect.height();
544 qreal h = rect.height();
457 m_minWidth = qMax(m_minWidth,w);
545 m_minWidth = qMax(m_minWidth,w);
458 m_minHeight = qMax(m_minHeight,rect.height());
546 m_minHeight = qMax(m_minHeight,rect.height());
459 m_height = qMax(m_height,h);
547 m_height = qMax(m_height,h);
460 item->setPos(point.x(),point.y());
548 item->setPos(point.x(),point.y());
461 point.setX(point.x() + w);
549 point.setX(point.x() + w);
462 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
550 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
463 // Next item would go off rect.
551 // Next item would go off rect.
464 point.setX(m_rect.topLeft().x());
552 point.setX(m_rect.topLeft().x());
465 point.setY(point.y() + h);
553 point.setY(point.y() + h);
466 if (i+1 < items.count()) {
554 if (i+1 < items.count()) {
467 m_height += h;
555 m_height += h;
468 }
556 }
469 }
557 }
470 }
558 }
471 m_markers->setPos(m_rect.topLeft());
559 m_markers->setPos(m_rect.topLeft());
472 m_width = m_minWidth;
560 m_width = m_minWidth;
473
561
474 m_minOffsetX = 0;
562 m_minOffsetX = 0;
475 m_minOffsetY = 0;
563 m_minOffsetY = 0;
476 m_maxOffsetX = m_width - m_rect.width();
564 m_maxOffsetX = m_width - m_rect.width();
477 m_maxOffsetY = m_height - m_rect.height();
565 m_maxOffsetY = m_height - m_rect.height();
478 }
566 }
479 break;
567 break;
480 case Qt::AlignBottom: {
568 case Qt::AlignBottom: {
481 QPointF point = m_rect.bottomLeft();
569 QPointF point = m_rect.bottomLeft();
482 m_width = 0;
570 m_width = 0;
483 m_height = 0;
571 m_height = 0;
484 for (int i=0; i<items.count(); i++) {
572 for (int i=0; i<items.count(); i++) {
485 QGraphicsItem *item = items.at(i);
573 QGraphicsItem *item = items.at(i);
486 const QRectF& rect = item->boundingRect();
574 const QRectF& rect = item->boundingRect();
487 qreal w = rect.width();
575 qreal w = rect.width();
488 qreal h = rect.height();
576 qreal h = rect.height();
489 m_minWidth = qMax(m_minWidth,w);
577 m_minWidth = qMax(m_minWidth,w);
490 m_minHeight = qMax(m_minHeight,rect.height());
578 m_minHeight = qMax(m_minHeight,rect.height());
491 m_height = qMax(m_height,h);
579 m_height = qMax(m_height,h);
492 item->setPos(point.x(),point.y() - h);
580 item->setPos(point.x(),point.y() - h);
493 point.setX(point.x() + w);
581 point.setX(point.x() + w);
494 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
582 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
495 // Next item would go off rect.
583 // Next item would go off rect.
496 point.setX(m_rect.bottomLeft().x());
584 point.setX(m_rect.bottomLeft().x());
497 point.setY(point.y() - h);
585 point.setY(point.y() - h);
498 if (i+1 < items.count()) {
586 if (i+1 < items.count()) {
499 m_height += h;
587 m_height += h;
500 }
588 }
501 }
589 }
502 }
590 }
503 m_markers->setPos(m_rect.topLeft());
591 m_markers->setPos(m_rect.topLeft());
504 m_width = m_minWidth;
592 m_width = m_minWidth;
505
593
506 m_minOffsetX = 0;
594 m_minOffsetX = 0;
507 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
595 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
508 m_maxOffsetX = m_width - m_rect.width();
596 m_maxOffsetX = m_width - m_rect.width();
509 m_maxOffsetY = 0;
597 m_maxOffsetY = 0;
510 }
598 }
511 break;
599 break;
512 case Qt::AlignLeft: {
600 case Qt::AlignLeft: {
513 QPointF point = m_rect.topLeft();
601 QPointF point = m_rect.topLeft();
514 m_width = 0;
602 m_width = 0;
515 m_height = 0;
603 m_height = 0;
516 qreal maxWidth = 0;
604 qreal maxWidth = 0;
517 for (int i=0; i<items.count(); i++) {
605 for (int i=0; i<items.count(); i++) {
518 QGraphicsItem *item = items.at(i);
606 QGraphicsItem *item = items.at(i);
519 const QRectF& rect = item->boundingRect();
607 const QRectF& rect = item->boundingRect();
520 qreal w = rect.width();
608 qreal w = rect.width();
521 qreal h = rect.height();
609 qreal h = rect.height();
522 m_minWidth = qMax(m_minWidth,rect.width());
610 m_minWidth = qMax(m_minWidth,rect.width());
523 m_minHeight = qMax(m_minHeight,h);
611 m_minHeight = qMax(m_minHeight,h);
524 maxWidth = qMax(maxWidth,w);
612 maxWidth = qMax(maxWidth,w);
525 item->setPos(point.x(),point.y());
613 item->setPos(point.x(),point.y());
526 point.setY(point.y() + h);
614 point.setY(point.y() + h);
527 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
615 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
528 // Next item would go off rect.
616 // Next item would go off rect.
529 point.setX(point.x() + maxWidth);
617 point.setX(point.x() + maxWidth);
530 point.setY(m_rect.topLeft().y());
618 point.setY(m_rect.topLeft().y());
531 if (i+1 < items.count()) {
619 if (i+1 < items.count()) {
532 m_width += maxWidth;
620 m_width += maxWidth;
533 maxWidth = 0;
621 maxWidth = 0;
534 }
622 }
535 }
623 }
536 }
624 }
537 m_width += maxWidth;
625 m_width += maxWidth;
538 m_markers->setPos(m_rect.topLeft());
626 m_markers->setPos(m_rect.topLeft());
539 m_height = m_minHeight;
627 m_height = m_minHeight;
540
628
541 m_minOffsetX = 0;
629 m_minOffsetX = 0;
542 m_minOffsetY = 0;
630 m_minOffsetY = 0;
543 m_maxOffsetX = m_width - m_rect.width();
631 m_maxOffsetX = m_width - m_rect.width();
544 m_maxOffsetY = m_height - m_rect.height();
632 m_maxOffsetY = m_height - m_rect.height();
545 }
633 }
546 break;
634 break;
547 case Qt::AlignRight: {
635 case Qt::AlignRight: {
548 QPointF point = m_rect.topRight();
636 QPointF point = m_rect.topRight();
549 m_width = 0;
637 m_width = 0;
550 m_height = 0;
638 m_height = 0;
551 qreal maxWidth = 0;
639 qreal maxWidth = 0;
552 for (int i=0; i<items.count(); i++) {
640 for (int i=0; i<items.count(); i++) {
553 QGraphicsItem *item = items.at(i);
641 QGraphicsItem *item = items.at(i);
554 const QRectF& rect = item->boundingRect();
642 const QRectF& rect = item->boundingRect();
555 qreal w = rect.width();
643 qreal w = rect.width();
556 qreal h = rect.height();
644 qreal h = rect.height();
557 m_minWidth = qMax(m_minWidth,rect.width());
645 m_minWidth = qMax(m_minWidth,rect.width());
558 m_minHeight = qMax(m_minHeight,h);
646 m_minHeight = qMax(m_minHeight,h);
559 maxWidth = qMax(maxWidth,w);
647 maxWidth = qMax(maxWidth,w);
560 item->setPos(point.x() - w,point.y());
648 item->setPos(point.x() - w,point.y());
561 point.setY(point.y() + h);
649 point.setY(point.y() + h);
562 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
650 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
563 // Next item would go off rect.
651 // Next item would go off rect.
564 point.setX(point.x() - maxWidth);
652 point.setX(point.x() - maxWidth);
565 point.setY(m_rect.topLeft().y());
653 point.setY(m_rect.topLeft().y());
566 if (i+1 < items.count()) {
654 if (i+1 < items.count()) {
567 m_width += maxWidth;
655 m_width += maxWidth;
568 maxWidth = 0;
656 maxWidth = 0;
569 }
657 }
570 }
658 }
571 }
659 }
572 m_width += maxWidth;
660 m_width += maxWidth;
573 m_markers->setPos(m_rect.topLeft());
661 m_markers->setPos(m_rect.topLeft());
574 m_height = m_minHeight;
662 m_height = m_minHeight;
575
663
576 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
664 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
577 m_minOffsetY = 0;
665 m_minOffsetY = 0;
578 m_maxOffsetX = 0;
666 m_maxOffsetX = 0;
579 m_maxOffsetY = m_height - m_rect.height();
667 m_maxOffsetY = m_height - m_rect.height();
580 }
668 }
581 break;
669 break;
582 default:
670 default:
583 break;
671 break;
584 }
672 }
585 }
673 }
586
674
587 void QLegendPrivate::attachToChart()
675 void QLegendPrivate::attachToChart()
588 {
676 {
589 m_attachedToChart = true;
677 m_attachedToChart = true;
590 q_ptr->setParent(m_chart);
678 q_ptr->setParent(m_chart);
591 }
679 }
592
680
593 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
681 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
594 {
682 {
595 Q_UNUSED(domain)
683 Q_UNUSED(domain)
596
684
597 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
685 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
598 foreach(LegendMarker* marker, markers)
686 foreach(LegendMarker* marker, markers)
599 m_markers->addToGroup(marker);
687 m_markers->addToGroup(marker);
600
688
601 if(series->type() == QAbstractSeries::SeriesTypePie) {
689 if(series->type() == QAbstractSeries::SeriesTypePie) {
602 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
690 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
603 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
691 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
604 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
692 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
605 }
693 }
606
694
607 updateLayout();
695 updateLayout();
608 }
696 }
609
697
610 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
698 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
611 {
699 {
612 QList<QGraphicsItem *> items = m_markers->childItems();
700 QList<QGraphicsItem *> items = m_markers->childItems();
613
701
614 foreach (QGraphicsItem *markers, items) {
702 foreach (QGraphicsItem *markers, items) {
615 LegendMarker *marker = static_cast<LegendMarker*>(markers);
703 LegendMarker *marker = static_cast<LegendMarker*>(markers);
616 if (marker->series() == series) {
704 if (marker->series() == series) {
617 delete marker;
705 delete marker;
618 }
706 }
619 }
707 }
620
708
621 if(series->type() == QAbstractSeries::SeriesTypePie)
709 if(series->type() == QAbstractSeries::SeriesTypePie)
622 {
710 {
623 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
711 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
624 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
712 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
625 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
713 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
626 }
714 }
627
715
628 updateLayout();
716 updateLayout();
629 }
717 }
630
718
631 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
719 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
632 {
720 {
633 // TODO: find out which markers are are added or removed. Update them
721 // TODO: find out which markers are are added or removed. Update them
634 // TODO: better implementation
722 // TODO: better implementation
635 handleSeriesRemoved(series);
723 handleSeriesRemoved(series);
636 Domain domain;
724 Domain domain;
637 handleSeriesAdded(series, &domain);
725 handleSeriesAdded(series, &domain);
638 }
726 }
639
727
640 void QLegendPrivate::handleUpdatePieSeries()
728 void QLegendPrivate::handleUpdatePieSeries()
641 {
729 {
642 //TODO: reimplement to be optimal
730 //TODO: reimplement to be optimal
643 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
731 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
644 Q_ASSERT(series);
732 Q_ASSERT(series);
645 handleSeriesRemoved(series);
733 handleSeriesRemoved(series);
646 handleSeriesAdded(series, 0);
734 handleSeriesAdded(series, 0);
647 }
735 }
648
736
649 #include "moc_qlegend.cpp"
737 #include "moc_qlegend.cpp"
650 #include "moc_qlegend_p.cpp"
738 #include "moc_qlegend_p.cpp"
651
739
652 QTCOMMERCIALCHART_END_NAMESPACE
740 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,95 +1,105
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
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Domain;
31 class Domain;
32 class LegendMarker;
32 class LegendMarker;
33 class QPieSlice;
33 class QPieSlice;
34 class QXYSeries;
34 class QXYSeries;
35 class QBarSet;
35 class QBarSet;
36 class QBarSeries;
36 class QBarSeries;
37 class QPieSeries;
37 class QPieSeries;
38 class QAreaSeries;
38 class QAreaSeries;
39 class LegendScrollButton;
39 class LegendScrollButton;
40 class QChart;
40 class QChart;
41 class QLegendPrivate;
41 class QLegendPrivate;
42
42
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
47
50
48 private:
51 private:
49 explicit QLegend(QChart *chart);
52 explicit QLegend(QChart *chart);
50
53
51 public:
54 public:
52 ~QLegend();
55 ~QLegend();
53
56
54 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
57 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
55 QRectF boundingRect() const;
58 QRectF boundingRect() const;
56
59
57 void setBrush(const QBrush &brush);
60 void setBrush(const QBrush &brush);
58 QBrush brush() const;
61 QBrush brush() const;
62 void setColor(QColor color);
63 QColor color();
59
64
60 void setPen(const QPen &pen);
65 void setPen(const QPen &pen);
61 QPen pen() const;
66 QPen pen() const;
67 void setBorderColor(QColor color);
68 QColor borderColor();
62
69
63 void setAlignment(Qt::Alignment alignment);
70 void setAlignment(Qt::Alignment alignment);
64 Qt::Alignment alignment() const;
71 Qt::Alignment alignment() const;
65
72
66 void detachFromChart();
73 void detachFromChart();
67 void attachToChart();
74 void attachToChart();
68 bool isAttachedToChart();
75 bool isAttachedToChart();
69
76
70 qreal minWidth() const;
77 qreal minWidth() const;
71 qreal minHeight() const;
78 qreal minHeight() const;
72
79
73 void setBackgroundVisible(bool visible = true);
80 void setBackgroundVisible(bool visible = true);
74 bool isBackgroundVisible() const;
81 bool isBackgroundVisible() const;
75
82
76 void setOffset(const QPointF& point);
83 void setOffset(const QPointF& point);
77 QPointF offset() const;
84 QPointF offset() const;
78
85
79 protected:
86 protected:
80 void resizeEvent(QGraphicsSceneResizeEvent *event);
87 void resizeEvent(QGraphicsSceneResizeEvent *event);
81 void hideEvent(QHideEvent *event);
88 void hideEvent(QHideEvent *event);
82 void showEvent(QShowEvent *event);
89 void showEvent(QShowEvent *event);
83
90
84 Q_SIGNALS:
91 Q_SIGNALS:
85 void alignmentChanged();
92 void alignmentChanged(Qt::Alignment alignment);
93 void backgroundVisibleChanged(bool visible);
94 void colorChanged(QColor color);
95 void borderColorChanged(QColor color);
86
96
87 private:
97 private:
88 QScopedPointer<QLegendPrivate> d_ptr;
98 QScopedPointer<QLegendPrivate> d_ptr;
89 Q_DISABLE_COPY(QLegend);
99 Q_DISABLE_COPY(QLegend)
90 friend class LegendScroller;
100 friend class LegendScroller;
91 };
101 };
92
102
93 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
94
104
95 #endif // QLEGEND_H
105 #endif // QLEGEND_H
@@ -1,239 +1,254
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 import QtQuick 1.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23
23
24
24
25 Flow {
25 Flow {
26 id: flow
26 id: flow
27 spacing: 5
27 spacing: 5
28 flow: Flow.TopToBottom
28 flow: Flow.TopToBottom
29 property variant series // TODO: rename to chart
29 property variant series // TODO: rename to chart
30
30
31 onSeriesChanged: {
31 onSeriesChanged: {
32 legendConnections.target = series.legend;
32 legendConnections.target = series.legend;
33 axisXConnections.target = series.axisX;
33 axisXConnections.target = series.axisX;
34 axisYConnections.target = series.axisY;
34 axisYConnections.target = series.axisY;
35 }
35 }
36
36
37 Connections {
37 Connections {
38 target: series
38 target: series
39 ignoreUnknownSignals: true
39 ignoreUnknownSignals: true
40 onVisibleChanged: console.log("chart.onVisibleChanged: " + series.visible);
40 onVisibleChanged: console.log("chart.onVisibleChanged: " + series.visible);
41 onThemeChanged: console.log("chart.onThemeChanged: " + series.theme);
41 onThemeChanged: console.log("chart.onThemeChanged: " + series.theme);
42 onLegendChanged: console.log("chart.onLegendChanged: " + series.legend);
42 onLegendChanged: console.log("chart.onLegendChanged: " + series.legend);
43 onAnimationOptionsChanged: console.log("chart.onAnimationOptionsChanged: " + series.animationOptions);
43 onAnimationOptionsChanged: console.log("chart.onAnimationOptionsChanged: " + series.animationOptions);
44 onTitleColorChanged: console.log("chart.onTitleColorChanged: " + series.titleColor);
44 onTitleColorChanged: console.log("chart.onTitleColorChanged: " + series.titleColor);
45 onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor);
45 onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor);
46 }
46 }
47
47
48 Connections {
48 Connections {
49 id: legendConnections
49 id: legendConnections
50 ignoreUnknownSignals: true
50 ignoreUnknownSignals: true
51 onAlignmentChanged: console.log("legend.onAlignmentChanged: " + series.legend.alignment);
51 onAlignmentChanged: console.log("legend.onAlignmentChanged: " + alignment);
52 onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible);
52 onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible);
53 onBackgroundVisibleChanged: console.log("legend.onBackgroundVisibleChanged: " + visible);
54 onColorChanged: console.log("legend.onColorChanged: " + color);
55 onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color);
53 }
56 }
54
57
55 Connections {
58 Connections {
56 id: axisXConnections
59 id: axisXConnections
57 ignoreUnknownSignals: true
60 ignoreUnknownSignals: true
58 onColorChanged: console.log("axisX.onColorChanged: " + color);
61 onColorChanged: console.log("axisX.onColorChanged: " + color);
59 onLabelsVisibleChanged: console.log("axisX.onLabelsVisibleChanged: " + visible);
62 onLabelsVisibleChanged: console.log("axisX.onLabelsVisibleChanged: " + visible);
60 onLabelsColorChanged: console.log("axisX.onLabelsColorChanged: " + color);
63 onLabelsColorChanged: console.log("axisX.onLabelsColorChanged: " + color);
61 onLabelsAngleChanged: console.log("axisX.onLabelsAngleChanged: " + angle);
64 onLabelsAngleChanged: console.log("axisX.onLabelsAngleChanged: " + angle);
62 onVisibleChanged: console.log("axisX.onVisibleChanged: " + visible);
65 onVisibleChanged: console.log("axisX.onVisibleChanged: " + visible);
63 onGridVisibleChanged: console.log("axisX.onGridVisibleChanged: " + visible);
66 onGridVisibleChanged: console.log("axisX.onGridVisibleChanged: " + visible);
64 onShadesVisibleChanged: console.log("axisX.onShadesVisibleChanged: " + visible);
67 onShadesVisibleChanged: console.log("axisX.onShadesVisibleChanged: " + visible);
65 onShadesColorChanged: console.log("axisX.onShadesColorChanged: " + color);
68 onShadesColorChanged: console.log("axisX.onShadesColorChanged: " + color);
66 onShadesBorderColorChanged: console.log("axisX.onShadesBorderColorChanged: " + color);
69 onShadesBorderColorChanged: console.log("axisX.onShadesBorderColorChanged: " + color);
67 onNiceNumbersEnabledChanged: console.log("axisX.onNiceNumbersEnabledChanged: " + enabled);
70 onNiceNumbersEnabledChanged: console.log("axisX.onNiceNumbersEnabledChanged: " + enabled);
68 onTicksCountChanged: console.log("axisX.onTicksCountChanged: " + count);
71 onTicksCountChanged: console.log("axisX.onTicksCountChanged: " + count);
69 }
72 }
70
73
71 Connections {
74 Connections {
72 id: axisYConnections
75 id: axisYConnections
73 ignoreUnknownSignals: true
76 ignoreUnknownSignals: true
74 onColorChanged: console.log("axisY.onColorChanged: " + color);
77 onColorChanged: console.log("axisY.onColorChanged: " + color);
75 onLabelsVisibleChanged: console.log("axisY.onLabelsVisibleChanged: " + visible);
78 onLabelsVisibleChanged: console.log("axisY.onLabelsVisibleChanged: " + visible);
76 onLabelsColorChanged: console.log("axisY.onLabelsColorChanged: " + color);
79 onLabelsColorChanged: console.log("axisY.onLabelsColorChanged: " + color);
77 onLabelsAngleChanged: console.log("axisY.onLabelsAngleChanged: " + angle);
80 onLabelsAngleChanged: console.log("axisY.onLabelsAngleChanged: " + angle);
78 onVisibleChanged: console.log("axisY.onVisibleChanged: " + visible);
81 onVisibleChanged: console.log("axisY.onVisibleChanged: " + visible);
79 onGridVisibleChanged: console.log("axisY.onGridVisibleChanged: " + visible);
82 onGridVisibleChanged: console.log("axisY.onGridVisibleChanged: " + visible);
80 onShadesVisibleChanged: console.log("axisY.onShadesVisibleChanged: " + visible);
83 onShadesVisibleChanged: console.log("axisY.onShadesVisibleChanged: " + visible);
81 onShadesColorChanged: console.log("axisY.onShadesColorChanged: " + color);
84 onShadesColorChanged: console.log("axisY.onShadesColorChanged: " + color);
82 onShadesBorderColorChanged: console.log("axisY.onShadesBorderColorChanged: " + color);
85 onShadesBorderColorChanged: console.log("axisY.onShadesBorderColorChanged: " + color);
83 onNiceNumbersEnabledChanged: console.log("axisY.onNiceNumbersEnabledChanged: " + enabled);
86 onNiceNumbersEnabledChanged: console.log("axisY.onNiceNumbersEnabledChanged: " + enabled);
84 onTicksCountChanged: console.log("axisY.onTicksCountChanged: " + count);
87 onTicksCountChanged: console.log("axisY.onTicksCountChanged: " + count);
85 }
88 }
86
89
87 Button {
90 Button {
88 text: "visible"
91 text: "visible"
89 onClicked: series.visible = !series.visible;
92 onClicked: series.visible = !series.visible;
90 }
93 }
91 Button {
94 Button {
92 text: "theme +"
95 text: "theme +"
93 onClicked: series.theme++;
96 onClicked: series.theme++;
94 }
97 }
95 Button {
98 Button {
96 text: "theme -"
99 text: "theme -"
97 onClicked: series.theme--;
100 onClicked: series.theme--;
98 }
101 }
99 Button {
102 Button {
100 text: "animation opt +"
103 text: "animation opt +"
101 onClicked: series.animationOptions++;
104 onClicked: series.animationOptions++;
102 }
105 }
103 Button {
106 Button {
104 text: "animation opt -"
107 text: "animation opt -"
105 onClicked: series.animationOptions--;
108 onClicked: series.animationOptions--;
106 }
109 }
107 Button {
110 Button {
108 text: "title color"
111 text: "title color"
109 onClicked: series.titleColor = main.nextColor();
112 onClicked: series.titleColor = main.nextColor();
110 }
113 }
111 Button {
114 Button {
112 text: "background color"
115 text: "background color"
113 onClicked: series.backgroundColor = main.nextColor();
116 onClicked: series.backgroundColor = main.nextColor();
114 }
117 }
115 Button {
118 Button {
119 text: "legend visible"
120 onClicked: series.legend.visible = !series.legend.visible;
121 }
122 Button {
123 text: "legend bckgrd visible"
124 onClicked: series.legend.backgroundVisible = !series.legend.backgroundVisible;
125 }
126 Button {
127 text: "legend color"
128 onClicked: series.legend.color = main.nextColor();
129 }
130 Button {
131 text: "legend border color"
132 onClicked: series.legend.borderColor = main.nextColor();
133 }
134 Button {
116 text: "legend top"
135 text: "legend top"
117 onClicked: series.legend.alignment ^= Qt.AlignTop;
136 onClicked: series.legend.alignment ^= Qt.AlignTop;
118 }
137 }
119 Button {
138 Button {
120 text: "legend bottom"
139 text: "legend bottom"
121 onClicked: series.legend.alignment ^= Qt.AlignBottom;
140 onClicked: series.legend.alignment ^= Qt.AlignBottom;
122 }
141 }
123 Button {
142 Button {
124 text: "legend left"
143 text: "legend left"
125 onClicked: series.legend.alignment ^= Qt.AlignLeft;
144 onClicked: series.legend.alignment ^= Qt.AlignLeft;
126 }
145 }
127 Button {
146 Button {
128 text: "legend right"
147 text: "legend right"
129 onClicked: series.legend.alignment ^= Qt.AlignRight;
148 onClicked: series.legend.alignment ^= Qt.AlignRight;
130 }
149 }
131 Button {
150 Button {
132 text: "legend visible"
133 onClicked: series.legend.visible = !series.legend.visible;
134 }
135 Button {
136 text: "axis X nice nmb"
151 text: "axis X nice nmb"
137 onClicked: series.axisX.niceNumbersEnabled = !series.axisX.niceNumbersEnabled;
152 onClicked: series.axisX.niceNumbersEnabled = !series.axisX.niceNumbersEnabled;
138 }
153 }
139 Button {
154 Button {
140 text: "axis X visible"
155 text: "axis X visible"
141 onClicked: series.axisX.visible = !series.axisX.visible;
156 onClicked: series.axisX.visible = !series.axisX.visible;
142 }
157 }
143 Button {
158 Button {
144 text: "axis X grid visible"
159 text: "axis X grid visible"
145 onClicked: series.axisX.gridVisible = !series.axisX.gridVisible;
160 onClicked: series.axisX.gridVisible = !series.axisX.gridVisible;
146 }
161 }
147 Button {
162 Button {
148 text: "axis X labels visible"
163 text: "axis X labels visible"
149 onClicked: series.axisX.labelsVisible = !series.axisX.labelsVisible;
164 onClicked: series.axisX.labelsVisible = !series.axisX.labelsVisible;
150 }
165 }
151 Button {
166 Button {
152 text: "axis X color"
167 text: "axis X color"
153 onClicked: series.axisX.color = main.nextColor();
168 onClicked: series.axisX.color = main.nextColor();
154 }
169 }
155 Button {
170 Button {
156 text: "axis X labels color"
171 text: "axis X labels color"
157 onClicked: series.axisX.labelsColor = main.nextColor();
172 onClicked: series.axisX.labelsColor = main.nextColor();
158 }
173 }
159 Button {
174 Button {
160 text: "axis X labels angle +"
175 text: "axis X labels angle +"
161 onClicked: series.axisX.labelsAngle += 5;
176 onClicked: series.axisX.labelsAngle += 5;
162 }
177 }
163 Button {
178 Button {
164 text: "axis X labels angle -"
179 text: "axis X labels angle -"
165 onClicked: series.axisX.labelsAngle -= 5;
180 onClicked: series.axisX.labelsAngle -= 5;
166 }
181 }
167 Button {
182 Button {
168 text: "axis X shades visible"
183 text: "axis X shades visible"
169 onClicked: series.axisX.shadesVisible = !series.axisX.shadesVisible;
184 onClicked: series.axisX.shadesVisible = !series.axisX.shadesVisible;
170 }
185 }
171 Button {
186 Button {
172 text: "axis X shades color"
187 text: "axis X shades color"
173 onClicked: series.axisX.shadesColor = main.nextColor();
188 onClicked: series.axisX.shadesColor = main.nextColor();
174 }
189 }
175 Button {
190 Button {
176 text: "axis X shades bcolor"
191 text: "axis X shades bcolor"
177 onClicked: series.axisX.shadesBorderColor = main.nextColor();
192 onClicked: series.axisX.shadesBorderColor = main.nextColor();
178 }
193 }
179 Button {
194 Button {
180 text: "axis X ticks count +"
195 text: "axis X ticks count +"
181 onClicked: series.axisX.ticksCount++;
196 onClicked: series.axisX.ticksCount++;
182 }
197 }
183 Button {
198 Button {
184 text: "axis X ticks count -"
199 text: "axis X ticks count -"
185 onClicked: series.axisX.ticksCount--;
200 onClicked: series.axisX.ticksCount--;
186 }
201 }
187 Button {
202 Button {
188 text: "axis Y nice nmb"
203 text: "axis Y nice nmb"
189 onClicked: series.axisY.niceNumbersEnabled = !series.axisY.niceNumbersEnabled;
204 onClicked: series.axisY.niceNumbersEnabled = !series.axisY.niceNumbersEnabled;
190 }
205 }
191 Button {
206 Button {
192 text: "axis Y visible"
207 text: "axis Y visible"
193 onClicked: series.axisY.visible = !series.axisY.visible;
208 onClicked: series.axisY.visible = !series.axisY.visible;
194 }
209 }
195 Button {
210 Button {
196 text: "axis Y grid visible"
211 text: "axis Y grid visible"
197 onClicked: series.axisY.gridVisible = !series.axisY.gridVisible;
212 onClicked: series.axisY.gridVisible = !series.axisY.gridVisible;
198 }
213 }
199 Button {
214 Button {
200 text: "axis Y labels visible"
215 text: "axis Y labels visible"
201 onClicked: series.axisY.labelsVisible = !series.axisY.labelsVisible;
216 onClicked: series.axisY.labelsVisible = !series.axisY.labelsVisible;
202 }
217 }
203 Button {
218 Button {
204 text: "axis Y color"
219 text: "axis Y color"
205 onClicked: series.axisY.color = main.nextColor();
220 onClicked: series.axisY.color = main.nextColor();
206 }
221 }
207 Button {
222 Button {
208 text: "axis Y labels color"
223 text: "axis Y labels color"
209 onClicked: series.axisY.labelsColor = main.nextColor();
224 onClicked: series.axisY.labelsColor = main.nextColor();
210 }
225 }
211 Button {
226 Button {
212 text: "axis Y labels angle +"
227 text: "axis Y labels angle +"
213 onClicked: series.axisY.labelsAngle += 5;
228 onClicked: series.axisY.labelsAngle += 5;
214 }
229 }
215 Button {
230 Button {
216 text: "axis Y labels angle -"
231 text: "axis Y labels angle -"
217 onClicked: series.axisY.labelsAngle -= 5;
232 onClicked: series.axisY.labelsAngle -= 5;
218 }
233 }
219 Button {
234 Button {
220 text: "axis Y shades visible"
235 text: "axis Y shades visible"
221 onClicked: series.axisY.shadesVisible = !series.axisY.shadesVisible;
236 onClicked: series.axisY.shadesVisible = !series.axisY.shadesVisible;
222 }
237 }
223 Button {
238 Button {
224 text: "axis Y shades color"
239 text: "axis Y shades color"
225 onClicked: series.axisY.shadesColor = main.nextColor();
240 onClicked: series.axisY.shadesColor = main.nextColor();
226 }
241 }
227 Button {
242 Button {
228 text: "axis Y shades bcolor"
243 text: "axis Y shades bcolor"
229 onClicked: series.axisY.shadesBorderColor = main.nextColor();
244 onClicked: series.axisY.shadesBorderColor = main.nextColor();
230 }
245 }
231 Button {
246 Button {
232 text: "axis Y ticks count +"
247 text: "axis Y ticks count +"
233 onClicked: series.axisY.ticksCount++;
248 onClicked: series.axisY.ticksCount++;
234 }
249 }
235 Button {
250 Button {
236 text: "axis Y ticks count -"
251 text: "axis Y ticks count -"
237 onClicked: series.axisY.ticksCount--;
252 onClicked: series.axisY.ticksCount--;
238 }
253 }
239 }
254 }
General Comments 0
You need to be logged in to leave comments. Login now