##// END OF EJS Templates
Areaseries uses new legendmarkers
sauimone -
r2177:3de587f7271d
parent child
Show More
@@ -1,409 +1,408
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qareaseries.h"
22 22 #include "qareaseries_p.h"
23 23 #include "qlineseries.h"
24 24 #include "areachartitem_p.h"
25 25 #include "legendmarker_p.h"
26 26 #include "domain_p.h"
27 27 #include "chartdataset_p.h"
28 28 #include "charttheme_p.h"
29 29 #include "qvalueaxis.h"
30 30
31 #include "qarealegendmarker.h"
32
31 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 34
33 35 /*!
34 36 \class QAreaSeries
35 37 \brief The QAreaSeries class is used for making area charts.
36 38
37 39 \mainclass
38 40
39 41 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
40 42 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
41 43 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
42 44 In that case QAreaSeries should be initiated with two QLineSeries instances. Please note terms "upper" and "lower" boundary can be misleading in cases
43 45 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
44 46
45 47 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
46 48 \image examples_areachart.png
47 49 */
48 50 /*!
49 51 \qmlclass AreaSeries QAreaSeries
50 52
51 53 The following QML shows how to create a simple area chart:
52 54 \snippet ../demos/qmlchart/qml/qmlchart/View4.qml 1
53 55 \beginfloatleft
54 56 \image demos_qmlchart4.png
55 57 \endfloat
56 58 \clearfloat
57 59 */
58 60
59 61 /*!
60 62 \property QAreaSeries::upperSeries
61 63 \brief The upper one of the two line series used to define area series boundaries.
62 64 */
63 65 /*!
64 66 \qmlproperty LineSeries AreaSeries::upperSeries
65 67 The upper one of the two line series used to define area series boundaries.
66 68 */
67 69
68 70 /*!
69 71 \property QAreaSeries::lowerSeries
70 72 The lower one of the two line series used to define are series boundaries. Note if
71 73 QAreaSeries was counstucted wihtout a\ lowerSeries this is null.
72 74 */
73 75 /*!
74 76 \qmlproperty LineSeries AreaSeries::lowerSeries
75 77 The lower one of the two line series used to define are series boundaries. Note if
76 78 AreaSeries was counstucted wihtout a\ lowerSeries this is null.
77 79 */
78 80
79 81 /*!
80 82 \property QAreaSeries::color
81 83 Fill (brush) color of the series. This is a convenience property for modifying the color of brush.
82 84 \sa QAreaSeries::brush()
83 85 */
84 86 /*!
85 87 \qmlproperty color AreaSeries::color
86 88 Fill (brush) color of the series.
87 89 */
88 90
89 91 /*!
90 92 \property QAreaSeries::borderColor
91 93 Line (pen) color of the series. This is a convenience property for modifying the color of pen.
92 94 \sa QAreaSeries::pen()
93 95 */
94 96 /*!
95 97 \qmlproperty color AreaSeries::borderColor
96 98 Line (pen) color of the series.
97 99 */
98 100
99 101 /*!
100 102 \qmlproperty real AreaSeries::borderWidth
101 103 The width of the border line. By default the width is 2.0.
102 104 */
103 105
104 106 /*!
105 107 \fn QPen QAreaSeries::pen() const
106 108 \brief Returns the pen used to draw line for this series.
107 109 \sa setPen()
108 110 */
109 111
110 112 /*!
111 113 \fn QPen QAreaSeries::brush() const
112 114 \brief Returns the brush used to draw line for this series.
113 115 \sa setBrush()
114 116 */
115 117
116 118 /*!
117 119 \fn void QAreaSeries::colorChanged(QColor color)
118 120 \brief Signal is emitted when the fill (brush) color has changed to \a color.
119 121 */
120 122 /*!
121 123 \qmlsignal AreaSeries::onColorChanged(color color)
122 124 Signal is emitted when the fill (brush) color has changed to \a color.
123 125 */
124 126
125 127 /*!
126 128 \fn void QAreaSeries::borderColorChanged(QColor color)
127 129 \brief Signal is emitted when the line (pen) color has changed to \a color.
128 130 */
129 131 /*!
130 132 \qmlsignal AreaSeries::onBorderColorChanged(color color)
131 133 Signal is emitted when the line (pen) color has changed to \a color.
132 134 */
133 135
134 136 /*!
135 137 \fn void QAreaSeries::clicked(const QPointF& point)
136 138 \brief Signal is emitted when user clicks the \a point on area chart.
137 139 */
138 140 /*!
139 141 \qmlsignal AreaSeries::onClicked(QPointF point)
140 142 Signal is emitted when user clicks the \a point on area chart.
141 143 */
142 144
143 145 /*!
144 146 \fn void QAreaSeries::selected()
145 147 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
146 148 implemented by the user of QAreaSeries API.
147 149 */
148 150 /*!
149 151 \qmlsignal AreaSeries::onSelected()
150 152 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
151 153 implemented by the user of AreaSeries API.
152 154 */
153 155
154 156 /*!
155 157 \fn void QAreaSeriesPrivate::updated()
156 158 \brief \internal
157 159 */
158 160
159 161 /*!
160 162 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
161 163 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
162 164 When series object is added to QChartView or QChart instance ownerships is transferred.
163 165 */
164 166 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
165 167 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries, lowerSeries, this), upperSeries)
166 168 {
167 169 }
168 170
169 171 /*!
170 172 Constructs area series object without upper or lower series with \a parent object.
171 173 */
172 174 QAreaSeries::QAreaSeries(QObject *parent)
173 175 : QAbstractSeries(*new QAreaSeriesPrivate(0, 0, this), parent)
174 176 {
175 177 }
176 178
177 179 /*!
178 180 Destroys the object.
179 181 */
180 182 QAreaSeries::~QAreaSeries()
181 183 {
182 184 Q_D(QAreaSeries);
183 185 if (d->m_dataset)
184 186 d->m_dataset->removeSeries(this);
185 187 }
186 188
187 189 /*!
188 190 Returns QChartSeries::SeriesTypeArea.
189 191 */
190 192 QAbstractSeries::SeriesType QAreaSeries::type() const
191 193 {
192 194 return QAbstractSeries::SeriesTypeArea;
193 195 }
194 196
195 197 /*!
196 198 Sets the \a series that is to be used as the area chart upper series.
197 199 */
198 200 void QAreaSeries::setUpperSeries(QLineSeries *series)
199 201 {
200 202 Q_D(QAreaSeries);
201 203 d->m_upperSeries = series;
202 204 }
203 205
204 206 QLineSeries *QAreaSeries::upperSeries() const
205 207 {
206 208 Q_D(const QAreaSeries);
207 209 return d->m_upperSeries;
208 210 }
209 211
210 212 /*!
211 213 Sets the \a series that is to be used as the area chart lower series.
212 214 */
213 215 void QAreaSeries::setLowerSeries(QLineSeries *series)
214 216 {
215 217 Q_D(QAreaSeries);
216 218 d->m_lowerSeries = series;
217 219 }
218 220
219 221 QLineSeries *QAreaSeries::lowerSeries() const
220 222 {
221 223 Q_D(const QAreaSeries);
222 224 return d->m_lowerSeries;
223 225 }
224 226
225 227 /*!
226 228 Sets \a pen used for drawing area outline.
227 229 */
228 230 void QAreaSeries::setPen(const QPen &pen)
229 231 {
230 232 Q_D(QAreaSeries);
231 233 if (d->m_pen != pen) {
232 234 d->m_pen = pen;
233 235 emit d->updated();
234 236 }
235 237 }
236 238
237 239 QPen QAreaSeries::pen() const
238 240 {
239 241 Q_D(const QAreaSeries);
240 242 return d->m_pen;
241 243 }
242 244
243 245 /*!
244 246 Sets \a brush used for filling the area.
245 247 */
246 248 void QAreaSeries::setBrush(const QBrush &brush)
247 249 {
248 250 Q_D(QAreaSeries);
249 251 if (d->m_brush != brush) {
250 252 bool emitColorChanged = brush.color() != d->m_brush.color();
251 253 d->m_brush = brush;
252 254 emit d->updated();
253 255 if (emitColorChanged)
254 256 emit colorChanged(brush.color());
255 257 }
256 258 }
257 259
258 260 QBrush QAreaSeries::brush() const
259 261 {
260 262 Q_D(const QAreaSeries);
261 263 return d->m_brush;
262 264 }
263 265
264 266 void QAreaSeries::setColor(const QColor &color)
265 267 {
266 268 QBrush b = brush();
267 269 if (b == QBrush())
268 270 b.setStyle(Qt::SolidPattern);
269 271 b.setColor(color);
270 272 setBrush(b);
271 273 }
272 274
273 275 QColor QAreaSeries::color() const
274 276 {
275 277 return brush().color();
276 278 }
277 279
278 280 void QAreaSeries::setBorderColor(const QColor &color)
279 281 {
280 282 QPen p = pen();
281 283 if (p.color() != color) {
282 284 p.setColor(color);
283 285 setPen(p);
284 286 emit borderColorChanged(color);
285 287 }
286 288 }
287 289
288 290 QColor QAreaSeries::borderColor() const
289 291 {
290 292 return pen().color();
291 293 }
292 294
293 295 /*!
294 296 Sets if data points are \a visible and should be drawn on line.
295 297 */
296 298 void QAreaSeries::setPointsVisible(bool visible)
297 299 {
298 300 Q_D(QAreaSeries);
299 301 if (d->m_pointsVisible != visible) {
300 302 d->m_pointsVisible = visible;
301 303 emit d->updated();
302 304 }
303 305 }
304 306
305 307 /*!
306 308 Returns if the points are drawn for this series.
307 309 \sa setPointsVisible()
308 310 */
309 311 bool QAreaSeries::pointsVisible() const
310 312 {
311 313 Q_D(const QAreaSeries);
312 314 return d->m_pointsVisible;
313 315 }
314 316
315 317 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
316 318
317 319 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries *q)
318 320 : QAbstractSeriesPrivate(q),
319 321 m_upperSeries(upperSeries),
320 322 m_lowerSeries(lowerSeries),
321 323 m_pointsVisible(false)
322 324 {
323 325 }
324 326
325 327 void QAreaSeriesPrivate::scaleDomain(Domain &domain)
326 328 {
327 329 Q_Q(QAreaSeries);
328 330
329 331 qreal minX(domain.minX());
330 332 qreal minY(domain.minY());
331 333 qreal maxX(domain.maxX());
332 334 qreal maxY(domain.maxY());
333 335
334 336 QLineSeries *upperSeries = q->upperSeries();
335 337 QLineSeries *lowerSeries = q->lowerSeries();
336 338
337 339 const QList<QPointF>& points = upperSeries->points();
338 340
339 341 for (int i = 0; i < points.count(); i++) {
340 342 qreal x = points[i].x();
341 343 qreal y = points[i].y();
342 344 minX = qMin(minX, x);
343 345 minY = qMin(minY, y);
344 346 maxX = qMax(maxX, x);
345 347 maxY = qMax(maxY, y);
346 348 }
347 349 if (lowerSeries) {
348 350
349 351 const QList<QPointF>& points = lowerSeries->points();
350 352
351 353 for (int i = 0; i < points.count(); i++) {
352 354 qreal x = points[i].x();
353 355 qreal y = points[i].y();
354 356 minX = qMin(minX, x);
355 357 minY = qMin(minY, y);
356 358 maxX = qMax(maxX, x);
357 359 maxY = qMax(maxY, y);
358 360 }
359 361 }
360 362
361 363 domain.setRange(minX, maxX, minY, maxY);
362 364 }
363 365
364 366 ChartElement *QAreaSeriesPrivate::createGraphics(ChartPresenter *presenter)
365 367 {
366 368 Q_Q(QAreaSeries);
367 369
368 370 AreaChartItem *area = new AreaChartItem(q, presenter);
369 371 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
370 372 area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem()));
371 373 if (q->lowerSeries())
372 374 area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem()));
373 375 }
374 376 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
375 377 return area;
376 378 }
377 379
378 380 QList<LegendMarker *> QAreaSeriesPrivate::createLegendMarker(QLegend *legend)
379 381 {
380 382 Q_Q(QAreaSeries);
381 383 QList<LegendMarker *> list;
382 384 return list << new AreaLegendMarker(q, legend);
383 385 }
384 386
385 387 QList<QLegendMarker*> QAreaSeriesPrivate::createLegendMarkers(QLegend* legend)
386 388 {
387 Q_UNUSED(legend);
388 // Q_Q(QAreaSeries);
389 Q_Q(QAreaSeries);
389 390 QList<QLegendMarker*> list;
390 // TODO:
391 // return list << new AreaLegendMarker(q,legend);
392 return list;
391 return list << new QAreaLegendMarker(q,legend);
393 392 }
394 393
395 394 void QAreaSeriesPrivate::initializeAxis(QAbstractAxis *axis)
396 395 {
397 396 Q_UNUSED(axis);
398 397 }
399 398
400 399 QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
401 400 {
402 401 Q_UNUSED(orientation);
403 402 return QAbstractAxis::AxisTypeValue;
404 403 }
405 404
406 405 #include "moc_qareaseries.cpp"
407 406 #include "moc_qareaseries_p.cpp"
408 407
409 408 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,85 +1,87
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qarealegendmarker.h"
22 22 #include "qarealegendmarker_p.h"
23 23 #include "qareaseries_p.h"
24 24 #include <QAreaSeries>
25 25 #include <QDebug>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 QAreaLegendMarker::QAreaLegendMarker(QAreaSeries* series, QLegend *legend, QObject *parent) :
30 30 QLegendMarker(*new QAreaLegendMarkerPrivate(this,series,legend), parent)
31 31 {
32 32 }
33 33
34 34 QAreaLegendMarker::~QAreaLegendMarker()
35 35 {
36 36 // qDebug() << "deleting Area marker" << this;
37 37 }
38 38
39 39 /*!
40 40 \internal
41 41 */
42 42 QAreaLegendMarker::QAreaLegendMarker(QAreaLegendMarkerPrivate &d, QObject *parent) :
43 43 QLegendMarker(d, parent)
44 44 {
45 45 }
46 46
47 47 QAreaSeries* QAreaLegendMarker::series()
48 48 {
49 49 Q_D(QAreaLegendMarker);
50 50 return d->m_series;
51 51 }
52 52
53 53 QAreaSeries* QAreaLegendMarker::peerObject()
54 54 {
55 55 Q_D(QAreaLegendMarker);
56 56 return d->m_series;
57 57 }
58 58
59 59 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60 60
61 61 QAreaLegendMarkerPrivate::QAreaLegendMarkerPrivate(QAreaLegendMarker *q, QAreaSeries *series, QLegend *legend) :
62 62 QLegendMarkerPrivate(q,legend),
63 63 m_series(series)
64 64 {
65 qDebug() << "QAreaLegendMarkerPrivate created";
65 66 QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
66 67 QObject::connect(m_series, SIGNAL(nameChanged()), this, SLOT(updated()));
67 68 updated();
68 69 }
69 70
70 71 QAreaLegendMarkerPrivate::~QAreaLegendMarkerPrivate()
71 72 {
72 73 QObject::disconnect(m_series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
73 74 QObject::disconnect(m_series, SIGNAL(nameChanged()), this, SLOT(updated()));
74 75 }
75 76
76 77 void QAreaLegendMarkerPrivate::updated()
77 78 {
79 qDebug() << "QAreaLegendMarkerPrivate::updated";
78 80 m_item->setBrush(m_series->brush());
79 81 m_item->setLabel(m_series->name());
80 82 }
81 83
82 84 #include "moc_qarealegendmarker.cpp"
83 85 #include "moc_qarealegendmarker_p.cpp"
84 86
85 87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,627 +1,626
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qlegend.h"
22 22 #include "qlegend_p.h"
23 23 #include "qabstractseries.h"
24 24 #include "qabstractseries_p.h"
25 25 #include "qchart_p.h"
26 26 #include "legendlayout_p.h"
27 27 #include "legendmarker_p.h" // TODO: deprecated
28 28 #include "qxyseries.h"
29 29 #include "qlineseries.h"
30 30 #include "qareaseries.h"
31 31 #include "qscatterseries.h"
32 32 #include "qsplineseries.h"
33 33 #include "qabstractbarseries.h"
34 34 #include "qstackedbarseries.h"
35 35 #include "qpercentbarseries.h"
36 36 #include "qbarset.h"
37 37 #include "qpieseries.h"
38 38 #include "qpieseries_p.h"
39 39 #include "qpieslice.h"
40 40 #include "chartpresenter_p.h"
41 41 #include "chartlayout_p.h"
42 42 #include <QPainter>
43 43 #include <QPen>
44 44 #include <QTimer>
45 45 #include <QGraphicsSceneEvent>
46 46
47 47 #include <QLegendMarker>
48 48 #include "qlegendmarker_p.h"
49 49 #include "legendmarkeritem_p.h"
50 50
51 51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52 52
53 53 /*!
54 54 \class QLegend
55 55 \brief Legend object
56 56 \mainclass
57 57
58 58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
59 59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
60 60 handle the drawing manually.
61 61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
62 62
63 63 \image examples_percentbarchart_legend.png
64 64
65 65 \sa QChart
66 66 */
67 67 /*!
68 68 \qmlclass Legend QLegend
69 69 \brief Legend is part of QtCommercial Chart QML API.
70 70
71 71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
72 72 series have been changed. Legend is used via ChartView class. For example:
73 73 \code
74 74 ChartView {
75 75 legend.visible: true
76 76 legend.alignment: Qt.AlignBottom
77 77 // Add a few series...
78 78 }
79 79 \endcode
80 80
81 81 \image examples_percentbarchart_legend.png
82 82 */
83 83
84 84 /*!
85 85 \property QLegend::alignment
86 86 \brief The alignment of the legend.
87 87
88 88 Legend paints on the defined position in the chart. The following alignments are supported:
89 89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
90 90 */
91 91 /*!
92 92 \qmlproperty Qt.Alignment Legend::alignment
93 93 \brief The alignment of the legend.
94 94
95 95 Legend paints on the defined position in the chart. The following alignments are supported:
96 96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
97 97 */
98 98
99 99 /*!
100 100 \property QLegend::backgroundVisible
101 101 Whether the legend background is visible or not.
102 102 */
103 103 /*!
104 104 \qmlproperty bool Legend::backgroundVisible
105 105 Whether the legend background is visible or not.
106 106 */
107 107
108 108 /*!
109 109 \property QLegend::color
110 110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
111 111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
112 112 */
113 113 /*!
114 114 \qmlproperty color Legend::color
115 115 The color of the legend, i.e. the background (brush) color.
116 116 */
117 117
118 118 /*!
119 119 \property QLegend::borderColor
120 120 The border color of the legend, i.e. the line color.
121 121 */
122 122 /*!
123 123 \qmlproperty color Legend::borderColor
124 124 The border color of the legend, i.e. the line color.
125 125 */
126 126
127 127 /*!
128 128 \property QLegend::font
129 129 The font of markers used by legend
130 130 */
131 131 /*!
132 132 \qmlproperty Font Legend::font
133 133 The font of markers used by legend
134 134 */
135 135
136 136 /*!
137 137 \property QLegend::labelColor
138 138 The color of brush used to draw labels.
139 139 */
140 140 /*!
141 141 \qmlproperty color QLegend::labelColor
142 142 The color of brush used to draw labels.
143 143 */
144 144
145 145 /*!
146 146 \fn void QLegend::backgroundVisibleChanged(bool)
147 147 The visibility of the legend background changed to \a visible.
148 148 */
149 149
150 150 /*!
151 151 \fn void QLegend::colorChanged(QColor)
152 152 The color of the legend background changed to \a color.
153 153 */
154 154
155 155 /*!
156 156 \fn void QLegend::borderColorChanged(QColor)
157 157 The border color of the legend background changed to \a color.
158 158 */
159 159
160 160 /*!
161 161 \fn void QLegend::fontChanged(QFont)
162 162 The font of markers of the legend changed to \a font.
163 163 */
164 164
165 165 /*!
166 166 \fn void QLegend::labelColorChanged(QColor color)
167 167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
168 168 */
169 169
170 170 /*!
171 171 Constructs the legend object and sets the parent to \a parent
172 172 */
173 173
174 174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
175 175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
176 176 {
177 177 setZValue(ChartPresenter::LegendZValue);
178 178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
179 179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
180 180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
181 181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
182 182 setLayout(d_ptr->m_layout);
183 183 }
184 184
185 185 /*!
186 186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
187 187 */
188 188 QLegend::~QLegend()
189 189 {
190 190 }
191 191
192 192 /*!
193 193 \internal
194 194 */
195 195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
196 196 {
197 197 Q_UNUSED(option)
198 198 Q_UNUSED(widget)
199 199
200 200 if (!d_ptr->m_backgroundVisible)
201 201 return;
202 202
203 203 painter->setOpacity(opacity());
204 204 painter->setPen(d_ptr->m_pen);
205 205 painter->setBrush(d_ptr->m_brush);
206 206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
207 207 }
208 208
209 209
210 210 /*!
211 211 Sets the \a brush of legend. Brush affects the background of legend.
212 212 */
213 213 void QLegend::setBrush(const QBrush &brush)
214 214 {
215 215 if (d_ptr->m_brush != brush) {
216 216 d_ptr->m_brush = brush;
217 217 update();
218 218 emit colorChanged(brush.color());
219 219 }
220 220 }
221 221
222 222 /*!
223 223 Returns the brush used by legend.
224 224 */
225 225 QBrush QLegend::brush() const
226 226 {
227 227 return d_ptr->m_brush;
228 228 }
229 229
230 230 void QLegend::setColor(QColor color)
231 231 {
232 232 QBrush b = d_ptr->m_brush;
233 233 if (b.style() != Qt::SolidPattern || b.color() != color) {
234 234 b.setStyle(Qt::SolidPattern);
235 235 b.setColor(color);
236 236 setBrush(b);
237 237 }
238 238 }
239 239
240 240 QColor QLegend::color()
241 241 {
242 242 return d_ptr->m_brush.color();
243 243 }
244 244
245 245 /*!
246 246 Sets the \a pen of legend. Pen affects the legend borders.
247 247 */
248 248 void QLegend::setPen(const QPen &pen)
249 249 {
250 250 if (d_ptr->m_pen != pen) {
251 251 d_ptr->m_pen = pen;
252 252 update();
253 253 emit borderColorChanged(pen.color());
254 254 }
255 255 }
256 256
257 257 /*!
258 258 Returns the pen used by legend
259 259 */
260 260
261 261 QPen QLegend::pen() const
262 262 {
263 263 return d_ptr->m_pen;
264 264 }
265 265
266 266 void QLegend::setFont(const QFont &font)
267 267 {
268 268 if (d_ptr->m_font != font) {
269 269 d_ptr->m_font = font;
270 270 foreach (LegendMarker *marker, d_ptr->markers())
271 271 marker->setFont(d_ptr->m_font);
272 272 layout()->invalidate();
273 273 emit fontChanged(font);
274 274 }
275 275 }
276 276
277 277 QFont QLegend::font() const
278 278 {
279 279 return d_ptr->m_font;
280 280 }
281 281
282 282 void QLegend::setBorderColor(QColor color)
283 283 {
284 284 QPen p = d_ptr->m_pen;
285 285 if (p.color() != color) {
286 286 p.setColor(color);
287 287 setPen(p);
288 288 }
289 289 }
290 290
291 291 QColor QLegend::borderColor()
292 292 {
293 293 return d_ptr->m_pen.color();
294 294 }
295 295
296 296 /*!
297 297 Set brush used to draw labels to \a brush.
298 298 */
299 299 void QLegend::setLabelBrush(const QBrush &brush)
300 300 {
301 301 if (d_ptr->m_labelBrush != brush) {
302 302 d_ptr->m_labelBrush = brush;
303 303 foreach (LegendMarker *marker, d_ptr->markers()) {
304 304 marker->setLabelBrush(d_ptr->m_labelBrush);
305 305 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
306 306 // instead of mapping it from label brush color
307 307 marker->setPen(brush.color());
308 308 }
309 309 emit labelColorChanged(brush.color());
310 310 }
311 311 }
312 312
313 313 /*!
314 314 Brush used to draw labels.
315 315 */
316 316 QBrush QLegend::labelBrush() const
317 317 {
318 318 return d_ptr->m_labelBrush;
319 319 }
320 320
321 321 void QLegend::setLabelColor(QColor color)
322 322 {
323 323 QBrush b = d_ptr->m_labelBrush;
324 324 if (b.style() != Qt::SolidPattern || b.color() != color) {
325 325 b.setStyle(Qt::SolidPattern);
326 326 b.setColor(color);
327 327 setLabelBrush(b);
328 328 }
329 329 }
330 330
331 331 QColor QLegend::labelColor() const
332 332 {
333 333 return d_ptr->m_labelBrush.color();
334 334 }
335 335
336 336
337 337 void QLegend::setAlignment(Qt::Alignment alignment)
338 338 {
339 339 if (d_ptr->m_alignment != alignment) {
340 340 d_ptr->m_alignment = alignment;
341 341 layout()->invalidate();
342 342 }
343 343 }
344 344
345 345 Qt::Alignment QLegend::alignment() const
346 346 {
347 347 return d_ptr->m_alignment;
348 348 }
349 349
350 350 /*!
351 351 Detaches the legend from chart. Chart won't change layout of the legend.
352 352 */
353 353 void QLegend::detachFromChart()
354 354 {
355 355 d_ptr->m_attachedToChart = false;
356 356 layout()->invalidate();
357 357 setParent(0);
358 358
359 359 }
360 360
361 361 /*!
362 362 Attaches the legend to chart. Chart may change layout of the legend.
363 363 */
364 364 void QLegend::attachToChart()
365 365 {
366 366 d_ptr->m_attachedToChart = true;
367 367 layout()->invalidate();
368 368 setParent(d_ptr->m_chart);
369 369 }
370 370
371 371 /*!
372 372 Returns true, if legend is attached to chart.
373 373 */
374 374 bool QLegend::isAttachedToChart()
375 375 {
376 376 return d_ptr->m_attachedToChart;
377 377 }
378 378
379 379 /*!
380 380 Sets the visibility of legend background to \a visible
381 381 */
382 382 void QLegend::setBackgroundVisible(bool visible)
383 383 {
384 384 if (d_ptr->m_backgroundVisible != visible) {
385 385 d_ptr->m_backgroundVisible = visible;
386 386 update();
387 387 emit backgroundVisibleChanged(visible);
388 388 }
389 389 }
390 390
391 391 /*!
392 392 Returns the visibility of legend background
393 393 */
394 394 bool QLegend::isBackgroundVisible() const
395 395 {
396 396 return d_ptr->m_backgroundVisible;
397 397 }
398 398
399 399
400 400 QList<QLegendMarker*> QLegend::markers() const
401 401 {
402 402 // TODO: name of PIMPL method will change.
403 403 return d_ptr->legendMarkers();
404 404 }
405 405
406 406 void QLegend::appendSeries(QAbstractSeries* series)
407 407 {
408 408 d_ptr->appendSeries(series);
409 409 }
410 410
411 411 void QLegend::removeSeries(QAbstractSeries* series)
412 412 {
413 413 d_ptr->removeSeries(series);
414 414 }
415 415
416 416 /*!
417 417 \internal \a event see QGraphicsWidget for details
418 418 */
419 419 void QLegend::hideEvent(QHideEvent *event)
420 420 {
421 421 if (isAttachedToChart())
422 422 d_ptr->m_presenter->layout()->invalidate();
423 423 QGraphicsWidget::hideEvent(event);
424 424 }
425 425 /*!
426 426 \internal \a event see QGraphicsWidget for details
427 427 */
428 428 void QLegend::showEvent(QShowEvent *event)
429 429 {
430 430 if (isAttachedToChart()) {
431 431 d_ptr->items()->setVisible(false);
432 432 layout()->invalidate();
433 433 }
434 434 QGraphicsWidget::showEvent(event);
435 435 //layout activation will show the items
436 436 }
437 437
438 438 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
439 439
440 440 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
441 441 : q_ptr(q),
442 442 m_presenter(presenter),
443 443 m_layout(new LegendLayout(q)),
444 444 m_chart(chart),
445 445 m_items(new QGraphicsItemGroup(q)),
446 446 m_alignment(Qt::AlignTop),
447 447 m_brush(QBrush()),
448 448 m_pen(QPen()),
449 449 m_labelBrush(QBrush()),
450 450 m_diameter(5),
451 451 m_attachedToChart(true),
452 452 m_backgroundVisible(false)
453 453 {
454 454 m_items->setHandlesChildEvents(false);
455 455 }
456 456
457 457 QLegendPrivate::~QLegendPrivate()
458 458 {
459 459
460 460 }
461 461
462 462 void QLegendPrivate::setOffset(qreal x, qreal y)
463 463 {
464 464 m_layout->setOffset(x, y);
465 465 }
466 466
467 467 QPointF QLegendPrivate::offset() const
468 468 {
469 469 return m_layout->offset();
470 470 }
471 471
472 472 int QLegendPrivate::roundness(qreal size)
473 473 {
474 474 return 100 * m_diameter / int(size);
475 475 }
476 476
477 477 void QLegendPrivate::appendSeries(QAbstractSeries* series)
478 478 {
479 479 // Only allow one instance of series
480 480 if (m_series.contains(series)) {
481 481 qDebug() << "series already added" << series;
482 482 return;
483 483 }
484 484
485 485 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
486 486 foreach (QLegendMarker* marker, newMarkers) {
487 487 marker->setFont(m_font);
488 488 marker->setLabelBrush(m_labelBrush);
489 489 marker->setVisible(series->isVisible());
490 490 m_items->addToGroup(marker->d_ptr.data()->item());
491 491 m_legendMarkers << marker;
492 492 }
493 493
494 494 // TODO: This is the part I don't like. There should be better solution.
495 495 // On the other hand. It is only one switch case for appending and another for removing series
496 496 // If countChanged signal were on QAbstractSeries, there would be no need for switch at all.
497 497 switch (series->type())
498 498 {
499 499 case QAbstractSeries::SeriesTypePie: {
500 500 QPieSeries *s = qobject_cast<QPieSeries *> (series);
501 501 QObject::connect(s, SIGNAL(countChanged()), this, SLOT(handleSeriesUpdated()));
502 502 break;
503 503 }
504 504 case QAbstractSeries::SeriesTypeBar:
505 505 case QAbstractSeries::SeriesTypeStackedBar:
506 506 case QAbstractSeries::SeriesTypePercentBar:
507 507 case QAbstractSeries::SeriesTypeHorizontalBar:
508 508 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
509 509 case QAbstractSeries::SeriesTypeHorizontalPercentBar: {
510 510 QAbstractBarSeries *s = qobject_cast<QAbstractBarSeries *> (series);
511 511 QObject::connect(s, SIGNAL(countChanged()), this, SLOT(handleSeriesUpdated()));
512 512 break;
513 513 }
514 // TODO:
515 514 case QAbstractSeries::SeriesTypeLine:
516 515 case QAbstractSeries::SeriesTypeArea:
517 516 case QAbstractSeries::SeriesTypeScatter:
518 517 case QAbstractSeries::SeriesTypeSpline:
519 518 default: {
520 // No need to connect any series related signals?
521 qDebug() << "Not yet implemented";
519 // No need to connect any series related signals. We have no series level
520 // changes, that would generate or delete markers
522 521 }
523 522 }
524 523
525 524 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
526 525
527 526 m_series.append(series);
528 527 m_items->setVisible(false);
529 528 m_layout->invalidate();
530 529 }
531 530
532 531 void QLegendPrivate::removeSeries(QAbstractSeries* series)
533 532 {
534 533 if (m_series.contains(series)) {
535 534 m_series.removeOne(series);
536 535 }
537 536
538 537 foreach (QLegendMarker *marker, m_legendMarkers) {
539 538 if (marker->series() == series) {
540 539 marker->d_ptr.data()->item()->setVisible(false);
541 540 m_items->removeFromGroup(marker->d_ptr.data()->item());
542 541 delete marker;
543 542 m_legendMarkers.removeAll(marker);
544 543 }
545 544 }
546 545
547 546 switch (series->type())
548 547 {
549 548 case QAbstractSeries::SeriesTypePie: {
550 549 QPieSeries *s = qobject_cast<QPieSeries *> (series);
551 550 QObject::disconnect(s, SIGNAL(countChanged()), this, SLOT(handleSeriesUpdated()));
552 551 break;
553 552 }
554 553 case QAbstractSeries::SeriesTypeBar:
555 554 case QAbstractSeries::SeriesTypeStackedBar:
556 555 case QAbstractSeries::SeriesTypePercentBar:
557 556 case QAbstractSeries::SeriesTypeHorizontalBar:
558 557 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
559 558 case QAbstractSeries::SeriesTypeHorizontalPercentBar: {
560 559 QAbstractBarSeries *s = qobject_cast<QAbstractBarSeries *> (series);
561 560 QObject::disconnect(s, SIGNAL(countChanged()), this, SLOT(handleSeriesUpdated()));
562 561 break;
563 562 }
564 563 // TODO:
565 564 case QAbstractSeries::SeriesTypeLine:
566 565 case QAbstractSeries::SeriesTypeArea:
567 566 case QAbstractSeries::SeriesTypeScatter:
568 567 case QAbstractSeries::SeriesTypeSpline:
569 568 default: {
570 // No need to disconnect any series related signals?
569 // No need to disconnect any series related signals
571 570 break;
572 571 }
573 572 }
574 573
575 574
576 575 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
577 576 // QObject::disconnect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*)));
578 577
579 578 m_layout->invalidate();
580 579 // q_ptr->layout()->activate();
581 580 }
582 581
583 582 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
584 583 {
585 584 // Moved to appendSeries
586 585 // This slot is just to make old code work for now.
587 586 appendSeries(series);
588 587 }
589 588
590 589 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
591 590 {
592 591 // Moved to removeSeries
593 592 // This slot is just to make old code work for now.
594 593 removeSeries(series);
595 594 }
596 595
597 596 void QLegendPrivate::handleSeriesVisibleChanged()
598 597 {
599 598 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
600 599 Q_ASSERT(series);
601 600
602 601 foreach (QLegendMarker* marker, m_legendMarkers) {
603 602 if (marker->series() == series) {
604 603 marker->setVisible(series->isVisible());
605 604 }
606 605 }
607 606 m_layout->invalidate();
608 607 }
609 608
610 609 void QLegendPrivate::handleCountChanged()
611 610 {
612 611 // TODO: With new markers, the series should notify markers directly.
613 // TODO: Better way to handle updates
612 // TODO: Better way to handle updates. Remove/Add series again seems like overkill.
614 613
615 614 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
616 615 qDebug() << "QLegendPrivate::handleSeriesUpdated" << series;
617 616
618 617 // Handle new or removed markers
619 618 // Handle changes of marker pen/brush/label. every property that legend is interested
620 619 handleSeriesRemoved(series);
621 620 handleSeriesAdded(series);
622 621 }
623 622
624 623 #include "moc_qlegend.cpp"
625 624 #include "moc_qlegend_p.cpp"
626 625
627 626 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now