##// END OF EJS Templates
refactoring
sauimone -
r2167:39840e10ebaa
parent child
Show More
@@ -1,253 +1,175
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 "legendmarkeritem_p.h"
21 #include "legendmarkeritem_p.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
24 #include "qlegend.h"
25 #include "qabstractbarseries.h"
26 #include "qpieseries.h"
27 #include "qpieslice.h"
28 #include "qbarset.h"
29 #include "qbarset_p.h"
30 #include "qareaseries.h"
31 #include "qareaseries_p.h"
32 #include <QPainter>
22 #include <QPainter>
33 #include <QGraphicsSceneEvent>
23 #include <QGraphicsSceneEvent>
34 #include <QGraphicsSimpleTextItem>
24 #include <QGraphicsSimpleTextItem>
35 #include <QDebug>
25 #include <QDebug>
36
26
27 #include "qlegendmarker_p.h"
28
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
30
39 LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) :
31 //LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) :
32 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
40 QGraphicsObject(parent),
33 QGraphicsObject(parent),
41 m_series(series),
34 m_marker(marker),
42 m_markerRect(0,0,10.0,10.0),
35 m_markerRect(0,0,10.0,10.0),
43 m_boundingRect(0,0,0,0),
36 m_boundingRect(0,0,0,0),
44 m_textItem(new QGraphicsSimpleTextItem(this)),
37 m_textItem(new QGraphicsSimpleTextItem(this)),
45 m_rectItem(new QGraphicsRectItem(this)),
38 m_rectItem(new QGraphicsRectItem(this)),
46 m_margin(4),
39 m_margin(4),
47 m_space(4)
40 m_space(4)
48 {
41 {
49 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
42 qDebug() << "LegendMarkerItem created for marker:" << m_marker;
43 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
50 m_rectItem->setRect(m_markerRect);
44 m_rectItem->setRect(m_markerRect);
51 }
45 }
52
46
53 void LegendMarkerItem::setPen(const QPen &pen)
47 void LegendMarkerItem::setPen(const QPen &pen)
54 {
48 {
55 m_rectItem->setPen(pen);
49 m_rectItem->setPen(pen);
56 }
50 }
57
51
58 QPen LegendMarkerItem::pen() const
52 QPen LegendMarkerItem::pen() const
59 {
53 {
60 return m_rectItem->pen();
54 return m_rectItem->pen();
61 }
55 }
62
56
63 void LegendMarkerItem::setBrush(const QBrush &brush)
57 void LegendMarkerItem::setBrush(const QBrush &brush)
64 {
58 {
65 m_rectItem->setBrush(brush);
59 m_rectItem->setBrush(brush);
66 }
60 }
67
61
68 QBrush LegendMarkerItem::brush() const
62 QBrush LegendMarkerItem::brush() const
69 {
63 {
70 return m_rectItem->brush();
64 return m_rectItem->brush();
71 }
65 }
72
66
73 void LegendMarkerItem::setFont(const QFont &font)
67 void LegendMarkerItem::setFont(const QFont &font)
74 {
68 {
75 m_textItem->setFont(font);
69 m_textItem->setFont(font);
76 QFontMetrics fn(font);
70 QFontMetrics fn(font);
77 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
71 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
78 updateGeometry();
72 updateGeometry();
79 }
73 }
80
74
81 QFont LegendMarkerItem::font() const
75 QFont LegendMarkerItem::font() const
82 {
76 {
83 return m_textItem->font();
77 return m_textItem->font();
84 }
78 }
85
79
86 void LegendMarkerItem::setLabel(const QString label)
80 void LegendMarkerItem::setLabel(const QString label)
87 {
81 {
88 m_text = label;
82 m_text = label;
89 updateGeometry();
83 updateGeometry();
90 }
84 }
91
85
92 QString LegendMarkerItem::label() const
86 QString LegendMarkerItem::label() const
93 {
87 {
94 return m_text;
88 return m_text;
95 }
89 }
96
90
97 QRectF LegendMarkerItem::boundingRect() const
91 QRectF LegendMarkerItem::boundingRect() const
98 {
92 {
99 return m_boundingRect;
93 return m_boundingRect;
100 }
94 }
101
95
102 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
96 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
103 {
97 {
104 m_textItem->setBrush(brush);
98 m_textItem->setBrush(brush);
105 }
99 }
106
100
107 QBrush LegendMarkerItem::labelBrush() const
101 QBrush LegendMarkerItem::labelBrush() const
108 {
102 {
109 return m_textItem->brush();
103 return m_textItem->brush();
110 }
104 }
111
105
112
106
113 void LegendMarkerItem::setGeometry(const QRectF& rect)
107 void LegendMarkerItem::setGeometry(const QRectF& rect)
114 {
108 {
115 QFontMetrics fn (font());
109 QFontMetrics fn (font());
116
110
117 int width = rect.width();
111 int width = rect.width();
118 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
112 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
119 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
113 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
120
114
121 if (fn.boundingRect(m_text).width() + x > width)
115 if (fn.boundingRect(m_text).width() + x > width)
122 {
116 {
123 QString string = m_text + "...";
117 QString string = m_text + "...";
124 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
118 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
125 string.remove(string.length() - 4, 1);
119 string.remove(string.length() - 4, 1);
126 m_textItem->setText(string);
120 m_textItem->setText(string);
127 }
121 }
128 else
122 else
129 m_textItem->setText(m_text);
123 m_textItem->setText(m_text);
130
124
131 const QRectF& textRect = m_textItem->boundingRect();
125 const QRectF& textRect = m_textItem->boundingRect();
132
126
133
127
134 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
128 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
135 m_rectItem->setRect(m_markerRect);
129 m_rectItem->setRect(m_markerRect);
136 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
130 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
137
131
138 prepareGeometryChange();
132 prepareGeometryChange();
139 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
133 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
140 }
134 }
141
135
142 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
136 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
143 {
137 {
144 Q_UNUSED(option)
138 Q_UNUSED(option)
145 Q_UNUSED(widget)
139 Q_UNUSED(widget)
146 Q_UNUSED(painter)
140 Q_UNUSED(painter)
147 }
141 }
148
142
149 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
143 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
150 {
144 {
151 Q_UNUSED(constraint)
145 Q_UNUSED(constraint)
152
146
153 QFontMetrics fn(m_textItem->font());
147 QFontMetrics fn(m_textItem->font());
154 QSizeF sh;
148 QSizeF sh;
155
149
156 switch (which) {
150 switch (which) {
157 case Qt::MinimumSize:
151 case Qt::MinimumSize:
158 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
152 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
159 break;
153 break;
160 case Qt::PreferredSize:
154 case Qt::PreferredSize:
161 sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
155 sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
162 break;
156 break;
163 default:
157 default:
164 break;
158 break;
165 }
159 }
166
160
167 return sh;
161 return sh;
168 }
162 }
169
163
170 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
164 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
171 {
165 {
172 qDebug() << "LegendMarkerItem::mousePressEvent";
166 qDebug() << "LegendMarkerItem::mousePressEvent";
173 QGraphicsObject::mousePressEvent(event);
167 // QGraphicsObject::mousePressEvent(event);
174 //TODO: selected signal removed for now
168 //TODO: selected signal removed for now
175 }
169 emit m_marker->handleMousePressEvent(event);
176
170 QGraphicsItem::mousePressEvent(event);
177 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178
179 AreaLegendMarkerItem::AreaLegendMarkerItem(QAreaSeries *series,QLegend *legend) : LegendMarkerItem(series,legend),
180 m_series(series)
181 {
182 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
183 // QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
184 // QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
185 updated();
186 }
187
188 void AreaLegendMarkerItem::updated()
189 {
190 setBrush(m_series->brush());
191 setLabel(m_series->name());
192 }
193
194 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
195
196 BarLegendMarkerItem::BarLegendMarkerItem(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarkerItem(barseries,legend),
197 m_barset(barset)
198 {
199 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
200 // QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
201 updated();
202 }
203
204 void BarLegendMarkerItem::updated()
205 {
206 setBrush(m_barset->brush());
207 setLabel(m_barset->label());
208 }
209
210 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211
212 PieLegendMarkerItem::PieLegendMarkerItem(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarkerItem(series,legend),
213 m_pieslice(pieslice)
214 {
215 // QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
216 // QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
217 updated();
218 }
219
220 void PieLegendMarkerItem::updated()
221 {
222 setBrush(m_pieslice->brush());
223 setLabel(m_pieslice->label());
224 }
225
226 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227
228 XYLegendMarkerItem::XYLegendMarkerItem(QXYSeries *series, QLegend *legend) : LegendMarkerItem(series,legend),
229 m_series(series)
230 {
231 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
232 // QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
233 // QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
234 updated();
235 }
236
237 void XYLegendMarkerItem::updated()
238 {
239 setLabel(m_series->name());
240
241 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
242 {
243 setBrush(m_series->brush());
244
245 }
246 else {
247 setBrush(QBrush(m_series->pen().color()));
248 }
249 }
171 }
250
172
251 #include "moc_legendmarkeritem_p.cpp"
173 #include "moc_legendmarkeritem_p.cpp"
252
174
253 QTCOMMERCIALCHART_END_NAMESPACE
175 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,146 +1,93
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef LEGENDMARKERITEM_P_H
30 #ifndef LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QBrush>
35 #include <QBrush>
36 #include <QPen>
36 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
37 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsLayoutItem>
38 #include <QGraphicsLayoutItem>
39
39
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41
41
42 class QAbstractSeries;
42 class QLegendMarkerPrivate;
43 class QAreaSeries;
44 class QXYSeries;
45 class QBarSet;
46 class QAbstractBarSeries;
47 class QPieSlice;
48 class QLegend;
49 class QPieSeries;
50
43
51 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
44 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
52 {
45 {
53 Q_OBJECT
46 Q_OBJECT
54 Q_INTERFACES(QGraphicsLayoutItem)
47 Q_INTERFACES(QGraphicsLayoutItem)
55 public:
48 public:
56 explicit LegendMarkerItem(QAbstractSeries *m_series, QGraphicsObject *parent = 0);
49 // explicit LegendMarkerItem(QAbstractSeries *m_series, QGraphicsObject *parent = 0);
50 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
57
51
58 void setPen(const QPen &pen);
52 void setPen(const QPen &pen);
59 QPen pen() const;
53 QPen pen() const;
60
54
61 void setBrush(const QBrush &brush);
55 void setBrush(const QBrush &brush);
62 QBrush brush() const;
56 QBrush brush() const;
63
57
64 void setFont(const QFont &font);
58 void setFont(const QFont &font);
65 QFont font() const;
59 QFont font() const;
66
60
67 void setLabel(const QString label);
61 void setLabel(const QString label);
68 QString label() const;
62 QString label() const;
69
63
70 void setLabelBrush(const QBrush &brush);
64 void setLabelBrush(const QBrush &brush);
71 QBrush labelBrush() const;
65 QBrush labelBrush() const;
72
66
73 QAbstractSeries *series() const { return m_series;}
74
75 void setGeometry(const QRectF& rect);
67 void setGeometry(const QRectF& rect);
76
68
77 QRectF boundingRect() const;
69 QRectF boundingRect() const;
78
70
79 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
71 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
80
72
81 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
73 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
82
74
83 protected:
75 protected:
84 // From QGraphicsObject
76 // From QGraphicsObject
85 void mousePressEvent(QGraphicsSceneMouseEvent *event);
77 void mousePressEvent(QGraphicsSceneMouseEvent *event);
86
78
87 //public Q_SLOTS:
88 //virtual void updated() = 0;
89
90 protected:
79 protected:
91 QAbstractSeries *m_series;
80 QLegendMarkerPrivate *m_marker;
92 QRectF m_markerRect;
81 QRectF m_markerRect;
93 QRectF m_boundingRect;
82 QRectF m_boundingRect;
94 // QLegend* m_legend;
95 QGraphicsSimpleTextItem *m_textItem;
83 QGraphicsSimpleTextItem *m_textItem;
96 QGraphicsRectItem *m_rectItem;
84 QGraphicsRectItem *m_rectItem;
97 qreal m_margin;
85 qreal m_margin;
98 qreal m_space;
86 qreal m_space;
99 QString m_text;
87 QString m_text;
100
88
101 };
89 };
102
90
103 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 class XYLegendMarkerItem : public LegendMarkerItem
105 {
106 public:
107 XYLegendMarkerItem(QXYSeries *series, QLegend *legend);
108 protected:
109 void updated();
110 private:
111 QXYSeries *m_series;
112 };
113 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
114 class AreaLegendMarkerItem : public LegendMarkerItem
115 {
116 public:
117 AreaLegendMarkerItem(QAreaSeries *series, QLegend *legend);
118 protected:
119 void updated();
120 private:
121 QAreaSeries *m_series;
122 };
123 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124 class BarLegendMarkerItem : public LegendMarkerItem
125 {
126 public:
127 BarLegendMarkerItem(QAbstractBarSeries *barseries, QBarSet *barset,QLegend *legend);
128 protected:
129 void updated();
130 private:
131 QBarSet *m_barset;
132 };
133 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
134 class PieLegendMarkerItem : public LegendMarkerItem
135 {
136 public:
137 PieLegendMarkerItem(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
138 protected:
139 void updated();
140 private:
141 QPieSlice *m_pieslice;
142 };
143
144 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
145
92
146 #endif // LEGENDMARKERITEM_P_H
93 #endif // LEGENDMARKERITEM_P_H
@@ -1,564 +1,599
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26 #include "legendlayout_p.h"
26 #include "legendlayout_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h" // TODO: deprecated
28 #include "qxyseries.h"
28 #include "qxyseries.h"
29 #include "qlineseries.h"
29 #include "qlineseries.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qscatterseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
32 #include "qsplineseries.h"
33 #include "qabstractbarseries.h"
33 #include "qabstractbarseries.h"
34 #include "qstackedbarseries.h"
34 #include "qstackedbarseries.h"
35 #include "qpercentbarseries.h"
35 #include "qpercentbarseries.h"
36 #include "qbarset.h"
36 #include "qbarset.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieseries_p.h"
38 #include "qpieseries_p.h"
39 #include "qpieslice.h"
39 #include "qpieslice.h"
40 #include "chartpresenter_p.h"
40 #include "chartpresenter_p.h"
41 #include "chartlayout_p.h"
41 #include "chartlayout_p.h"
42 #include <QPainter>
42 #include <QPainter>
43 #include <QPen>
43 #include <QPen>
44 #include <QTimer>
44 #include <QTimer>
45 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
46
46
47 #include <QLegendMarker>
47 #include <QLegendMarker>
48 #include "qlegendmarker_p.h"
49 #include "legendmarkeritem_p.h"
48
50
49 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
50
52
51 /*!
53 /*!
52 \class QLegend
54 \class QLegend
53 \brief Legend object
55 \brief Legend object
54 \mainclass
56 \mainclass
55
57
56 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
57 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
58 handle the drawing manually.
60 handle the drawing manually.
59 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
60
62
61 \image examples_percentbarchart_legend.png
63 \image examples_percentbarchart_legend.png
62
64
63 \sa QChart
65 \sa QChart
64 */
66 */
65 /*!
67 /*!
66 \qmlclass Legend QLegend
68 \qmlclass Legend QLegend
67 \brief Legend is part of QtCommercial Chart QML API.
69 \brief Legend is part of QtCommercial Chart QML API.
68
70
69 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
70 series have been changed. Legend is used via ChartView class. For example:
72 series have been changed. Legend is used via ChartView class. For example:
71 \code
73 \code
72 ChartView {
74 ChartView {
73 legend.visible: true
75 legend.visible: true
74 legend.alignment: Qt.AlignBottom
76 legend.alignment: Qt.AlignBottom
75 // Add a few series...
77 // Add a few series...
76 }
78 }
77 \endcode
79 \endcode
78
80
79 \image examples_percentbarchart_legend.png
81 \image examples_percentbarchart_legend.png
80 */
82 */
81
83
82 /*!
84 /*!
83 \property QLegend::alignment
85 \property QLegend::alignment
84 \brief The alignment of the legend.
86 \brief The alignment of the legend.
85
87
86 Legend paints on the defined position in the chart. The following alignments are supported:
88 Legend paints on the defined position in the chart. The following alignments are supported:
87 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
88 */
90 */
89 /*!
91 /*!
90 \qmlproperty Qt.Alignment Legend::alignment
92 \qmlproperty Qt.Alignment Legend::alignment
91 \brief The alignment of the legend.
93 \brief The alignment of the legend.
92
94
93 Legend paints on the defined position in the chart. The following alignments are supported:
95 Legend paints on the defined position in the chart. The following alignments are supported:
94 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
95 */
97 */
96
98
97 /*!
99 /*!
98 \property QLegend::backgroundVisible
100 \property QLegend::backgroundVisible
99 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
100 */
102 */
101 /*!
103 /*!
102 \qmlproperty bool Legend::backgroundVisible
104 \qmlproperty bool Legend::backgroundVisible
103 Whether the legend background is visible or not.
105 Whether the legend background is visible or not.
104 */
106 */
105
107
106 /*!
108 /*!
107 \property QLegend::color
109 \property QLegend::color
108 The color of the legend, i.e. the background (brush) color. Note that if you change the color
110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
109 of the legend, the style of the legend brush is set to Qt::SolidPattern.
111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
110 */
112 */
111 /*!
113 /*!
112 \qmlproperty color Legend::color
114 \qmlproperty color Legend::color
113 The color of the legend, i.e. the background (brush) color.
115 The color of the legend, i.e. the background (brush) color.
114 */
116 */
115
117
116 /*!
118 /*!
117 \property QLegend::borderColor
119 \property QLegend::borderColor
118 The border color of the legend, i.e. the line color.
120 The border color of the legend, i.e. the line color.
119 */
121 */
120 /*!
122 /*!
121 \qmlproperty color Legend::borderColor
123 \qmlproperty color Legend::borderColor
122 The border color of the legend, i.e. the line color.
124 The border color of the legend, i.e. the line color.
123 */
125 */
124
126
125 /*!
127 /*!
126 \property QLegend::font
128 \property QLegend::font
127 The font of markers used by legend
129 The font of markers used by legend
128 */
130 */
129 /*!
131 /*!
130 \qmlproperty Font Legend::font
132 \qmlproperty Font Legend::font
131 The font of markers used by legend
133 The font of markers used by legend
132 */
134 */
133
135
134 /*!
136 /*!
135 \property QLegend::labelColor
137 \property QLegend::labelColor
136 The color of brush used to draw labels.
138 The color of brush used to draw labels.
137 */
139 */
138 /*!
140 /*!
139 \qmlproperty color QLegend::labelColor
141 \qmlproperty color QLegend::labelColor
140 The color of brush used to draw labels.
142 The color of brush used to draw labels.
141 */
143 */
142
144
143 /*!
145 /*!
144 \fn void QLegend::backgroundVisibleChanged(bool)
146 \fn void QLegend::backgroundVisibleChanged(bool)
145 The visibility of the legend background changed to \a visible.
147 The visibility of the legend background changed to \a visible.
146 */
148 */
147
149
148 /*!
150 /*!
149 \fn void QLegend::colorChanged(QColor)
151 \fn void QLegend::colorChanged(QColor)
150 The color of the legend background changed to \a color.
152 The color of the legend background changed to \a color.
151 */
153 */
152
154
153 /*!
155 /*!
154 \fn void QLegend::borderColorChanged(QColor)
156 \fn void QLegend::borderColorChanged(QColor)
155 The border color of the legend background changed to \a color.
157 The border color of the legend background changed to \a color.
156 */
158 */
157
159
158 /*!
160 /*!
159 \fn void QLegend::fontChanged(QFont)
161 \fn void QLegend::fontChanged(QFont)
160 The font of markers of the legend changed to \a font.
162 The font of markers of the legend changed to \a font.
161 */
163 */
162
164
163 /*!
165 /*!
164 \fn void QLegend::labelColorChanged(QColor color)
166 \fn void QLegend::labelColorChanged(QColor color)
165 This signal is emitted when the color of brush used to draw labels has changed to \a color.
167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
166 */
168 */
167
169
168 /*!
170 /*!
169 Constructs the legend object and sets the parent to \a parent
171 Constructs the legend object and sets the parent to \a parent
170 */
172 */
171
173
172 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
173 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
174 {
176 {
175 setZValue(ChartPresenter::LegendZValue);
177 setZValue(ChartPresenter::LegendZValue);
176 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
177 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
178 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
179 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
180 setLayout(d_ptr->m_layout);
182 setLayout(d_ptr->m_layout);
181 }
183 }
182
184
183 /*!
185 /*!
184 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
185 */
187 */
186 QLegend::~QLegend()
188 QLegend::~QLegend()
187 {
189 {
188 }
190 }
189
191
190 /*!
192 /*!
191 \internal
193 \internal
192 */
194 */
193 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
194 {
196 {
195 Q_UNUSED(option)
197 Q_UNUSED(option)
196 Q_UNUSED(widget)
198 Q_UNUSED(widget)
197
199
198 if (!d_ptr->m_backgroundVisible)
200 if (!d_ptr->m_backgroundVisible)
199 return;
201 return;
200
202
201 painter->setOpacity(opacity());
203 painter->setOpacity(opacity());
202 painter->setPen(d_ptr->m_pen);
204 painter->setPen(d_ptr->m_pen);
203 painter->setBrush(d_ptr->m_brush);
205 painter->setBrush(d_ptr->m_brush);
204 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
205 }
207 }
206
208
207
209
208 /*!
210 /*!
209 Sets the \a brush of legend. Brush affects the background of legend.
211 Sets the \a brush of legend. Brush affects the background of legend.
210 */
212 */
211 void QLegend::setBrush(const QBrush &brush)
213 void QLegend::setBrush(const QBrush &brush)
212 {
214 {
213 if (d_ptr->m_brush != brush) {
215 if (d_ptr->m_brush != brush) {
214 d_ptr->m_brush = brush;
216 d_ptr->m_brush = brush;
215 update();
217 update();
216 emit colorChanged(brush.color());
218 emit colorChanged(brush.color());
217 }
219 }
218 }
220 }
219
221
220 /*!
222 /*!
221 Returns the brush used by legend.
223 Returns the brush used by legend.
222 */
224 */
223 QBrush QLegend::brush() const
225 QBrush QLegend::brush() const
224 {
226 {
225 return d_ptr->m_brush;
227 return d_ptr->m_brush;
226 }
228 }
227
229
228 void QLegend::setColor(QColor color)
230 void QLegend::setColor(QColor color)
229 {
231 {
230 QBrush b = d_ptr->m_brush;
232 QBrush b = d_ptr->m_brush;
231 if (b.style() != Qt::SolidPattern || b.color() != color) {
233 if (b.style() != Qt::SolidPattern || b.color() != color) {
232 b.setStyle(Qt::SolidPattern);
234 b.setStyle(Qt::SolidPattern);
233 b.setColor(color);
235 b.setColor(color);
234 setBrush(b);
236 setBrush(b);
235 }
237 }
236 }
238 }
237
239
238 QColor QLegend::color()
240 QColor QLegend::color()
239 {
241 {
240 return d_ptr->m_brush.color();
242 return d_ptr->m_brush.color();
241 }
243 }
242
244
243 /*!
245 /*!
244 Sets the \a pen of legend. Pen affects the legend borders.
246 Sets the \a pen of legend. Pen affects the legend borders.
245 */
247 */
246 void QLegend::setPen(const QPen &pen)
248 void QLegend::setPen(const QPen &pen)
247 {
249 {
248 if (d_ptr->m_pen != pen) {
250 if (d_ptr->m_pen != pen) {
249 d_ptr->m_pen = pen;
251 d_ptr->m_pen = pen;
250 update();
252 update();
251 emit borderColorChanged(pen.color());
253 emit borderColorChanged(pen.color());
252 }
254 }
253 }
255 }
254
256
255 /*!
257 /*!
256 Returns the pen used by legend
258 Returns the pen used by legend
257 */
259 */
258
260
259 QPen QLegend::pen() const
261 QPen QLegend::pen() const
260 {
262 {
261 return d_ptr->m_pen;
263 return d_ptr->m_pen;
262 }
264 }
263
265
264 void QLegend::setFont(const QFont &font)
266 void QLegend::setFont(const QFont &font)
265 {
267 {
266 if (d_ptr->m_font != font) {
268 if (d_ptr->m_font != font) {
267 d_ptr->m_font = font;
269 d_ptr->m_font = font;
268 foreach (LegendMarker *marker, d_ptr->markers())
270 foreach (LegendMarker *marker, d_ptr->markers())
269 marker->setFont(d_ptr->m_font);
271 marker->setFont(d_ptr->m_font);
270 layout()->invalidate();
272 layout()->invalidate();
271 emit fontChanged(font);
273 emit fontChanged(font);
272 }
274 }
273 }
275 }
274
276
275 QFont QLegend::font() const
277 QFont QLegend::font() const
276 {
278 {
277 return d_ptr->m_font;
279 return d_ptr->m_font;
278 }
280 }
279
281
280 void QLegend::setBorderColor(QColor color)
282 void QLegend::setBorderColor(QColor color)
281 {
283 {
282 QPen p = d_ptr->m_pen;
284 QPen p = d_ptr->m_pen;
283 if (p.color() != color) {
285 if (p.color() != color) {
284 p.setColor(color);
286 p.setColor(color);
285 setPen(p);
287 setPen(p);
286 }
288 }
287 }
289 }
288
290
289 QColor QLegend::borderColor()
291 QColor QLegend::borderColor()
290 {
292 {
291 return d_ptr->m_pen.color();
293 return d_ptr->m_pen.color();
292 }
294 }
293
295
294 /*!
296 /*!
295 Set brush used to draw labels to \a brush.
297 Set brush used to draw labels to \a brush.
296 */
298 */
297 void QLegend::setLabelBrush(const QBrush &brush)
299 void QLegend::setLabelBrush(const QBrush &brush)
298 {
300 {
299 if (d_ptr->m_labelBrush != brush) {
301 if (d_ptr->m_labelBrush != brush) {
300 d_ptr->m_labelBrush = brush;
302 d_ptr->m_labelBrush = brush;
301 foreach (LegendMarker *marker, d_ptr->markers()) {
303 foreach (LegendMarker *marker, d_ptr->markers()) {
302 marker->setLabelBrush(d_ptr->m_labelBrush);
304 marker->setLabelBrush(d_ptr->m_labelBrush);
303 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
305 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
304 // instead of mapping it from label brush color
306 // instead of mapping it from label brush color
305 marker->setPen(brush.color());
307 marker->setPen(brush.color());
306 }
308 }
307 emit labelColorChanged(brush.color());
309 emit labelColorChanged(brush.color());
308 }
310 }
309 }
311 }
310
312
311 /*!
313 /*!
312 Brush used to draw labels.
314 Brush used to draw labels.
313 */
315 */
314 QBrush QLegend::labelBrush() const
316 QBrush QLegend::labelBrush() const
315 {
317 {
316 return d_ptr->m_labelBrush;
318 return d_ptr->m_labelBrush;
317 }
319 }
318
320
319 void QLegend::setLabelColor(QColor color)
321 void QLegend::setLabelColor(QColor color)
320 {
322 {
321 QBrush b = d_ptr->m_labelBrush;
323 QBrush b = d_ptr->m_labelBrush;
322 if (b.style() != Qt::SolidPattern || b.color() != color) {
324 if (b.style() != Qt::SolidPattern || b.color() != color) {
323 b.setStyle(Qt::SolidPattern);
325 b.setStyle(Qt::SolidPattern);
324 b.setColor(color);
326 b.setColor(color);
325 setLabelBrush(b);
327 setLabelBrush(b);
326 }
328 }
327 }
329 }
328
330
329 QColor QLegend::labelColor() const
331 QColor QLegend::labelColor() const
330 {
332 {
331 return d_ptr->m_labelBrush.color();
333 return d_ptr->m_labelBrush.color();
332 }
334 }
333
335
334
336
335 void QLegend::setAlignment(Qt::Alignment alignment)
337 void QLegend::setAlignment(Qt::Alignment alignment)
336 {
338 {
337 if (d_ptr->m_alignment != alignment) {
339 if (d_ptr->m_alignment != alignment) {
338 d_ptr->m_alignment = alignment;
340 d_ptr->m_alignment = alignment;
339 layout()->invalidate();
341 layout()->invalidate();
340 }
342 }
341 }
343 }
342
344
343 Qt::Alignment QLegend::alignment() const
345 Qt::Alignment QLegend::alignment() const
344 {
346 {
345 return d_ptr->m_alignment;
347 return d_ptr->m_alignment;
346 }
348 }
347
349
348 /*!
350 /*!
349 Detaches the legend from chart. Chart won't change layout of the legend.
351 Detaches the legend from chart. Chart won't change layout of the legend.
350 */
352 */
351 void QLegend::detachFromChart()
353 void QLegend::detachFromChart()
352 {
354 {
353 d_ptr->m_attachedToChart = false;
355 d_ptr->m_attachedToChart = false;
354 layout()->invalidate();
356 layout()->invalidate();
355 setParent(0);
357 setParent(0);
356
358
357 }
359 }
358
360
359 /*!
361 /*!
360 Attaches the legend to chart. Chart may change layout of the legend.
362 Attaches the legend to chart. Chart may change layout of the legend.
361 */
363 */
362 void QLegend::attachToChart()
364 void QLegend::attachToChart()
363 {
365 {
364 d_ptr->m_attachedToChart = true;
366 d_ptr->m_attachedToChart = true;
365 layout()->invalidate();
367 layout()->invalidate();
366 setParent(d_ptr->m_chart);
368 setParent(d_ptr->m_chart);
367 }
369 }
368
370
369 /*!
371 /*!
370 Returns true, if legend is attached to chart.
372 Returns true, if legend is attached to chart.
371 */
373 */
372 bool QLegend::isAttachedToChart()
374 bool QLegend::isAttachedToChart()
373 {
375 {
374 return d_ptr->m_attachedToChart;
376 return d_ptr->m_attachedToChart;
375 }
377 }
376
378
377 /*!
379 /*!
378 Sets the visibility of legend background to \a visible
380 Sets the visibility of legend background to \a visible
379 */
381 */
380 void QLegend::setBackgroundVisible(bool visible)
382 void QLegend::setBackgroundVisible(bool visible)
381 {
383 {
382 if (d_ptr->m_backgroundVisible != visible) {
384 if (d_ptr->m_backgroundVisible != visible) {
383 d_ptr->m_backgroundVisible = visible;
385 d_ptr->m_backgroundVisible = visible;
384 update();
386 update();
385 emit backgroundVisibleChanged(visible);
387 emit backgroundVisibleChanged(visible);
386 }
388 }
387 }
389 }
388
390
389 /*!
391 /*!
390 Returns the visibility of legend background
392 Returns the visibility of legend background
391 */
393 */
392 bool QLegend::isBackgroundVisible() const
394 bool QLegend::isBackgroundVisible() const
393 {
395 {
394 return d_ptr->m_backgroundVisible;
396 return d_ptr->m_backgroundVisible;
395 }
397 }
396
398
397
399
398 QList<QLegendMarker*> QLegend::markers() const
400 QList<QLegendMarker*> QLegend::markers() const
399 {
401 {
400 // TODO: name of PIMPL method will change.
402 // TODO: name of PIMPL method will change.
401 return d_ptr->legendMarkers();
403 return d_ptr->legendMarkers();
402 }
404 }
403
405
406 void QLegend::appendSeries(QAbstractSeries* series)
407 {
408 d_ptr->appendSeries(series);
409 }
410
411 void QLegend::removeSeries(QAbstractSeries* series)
412 {
413 d_ptr->removeSeries(series);
414 }
404
415
405 /*!
416 /*!
406 \internal \a event see QGraphicsWidget for details
417 \internal \a event see QGraphicsWidget for details
407 */
418 */
408 void QLegend::hideEvent(QHideEvent *event)
419 void QLegend::hideEvent(QHideEvent *event)
409 {
420 {
410 if (isAttachedToChart())
421 if (isAttachedToChart())
411 d_ptr->m_presenter->layout()->invalidate();
422 d_ptr->m_presenter->layout()->invalidate();
412 QGraphicsWidget::hideEvent(event);
423 QGraphicsWidget::hideEvent(event);
413 }
424 }
414 /*!
425 /*!
415 \internal \a event see QGraphicsWidget for details
426 \internal \a event see QGraphicsWidget for details
416 */
427 */
417 void QLegend::showEvent(QShowEvent *event)
428 void QLegend::showEvent(QShowEvent *event)
418 {
429 {
419 if (isAttachedToChart()) {
430 if (isAttachedToChart()) {
420 d_ptr->items()->setVisible(false);
431 d_ptr->items()->setVisible(false);
421 layout()->invalidate();
432 layout()->invalidate();
422 }
433 }
423 QGraphicsWidget::showEvent(event);
434 QGraphicsWidget::showEvent(event);
424 //layout activation will show the items
435 //layout activation will show the items
425 }
436 }
426
437
427 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
438 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
428
439
429 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
440 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
430 : q_ptr(q),
441 : q_ptr(q),
431 m_presenter(presenter),
442 m_presenter(presenter),
432 m_layout(new LegendLayout(q)),
443 m_layout(new LegendLayout(q)),
433 m_chart(chart),
444 m_chart(chart),
434 m_items(new QGraphicsItemGroup(q)),
445 m_items(new QGraphicsItemGroup(q)),
435 m_alignment(Qt::AlignTop),
446 m_alignment(Qt::AlignTop),
436 m_brush(QBrush()),
447 m_brush(QBrush()),
437 m_pen(QPen()),
448 m_pen(QPen()),
438 m_labelBrush(QBrush()),
449 m_labelBrush(QBrush()),
439 m_diameter(5),
450 m_diameter(5),
440 m_attachedToChart(true),
451 m_attachedToChart(true),
441 m_backgroundVisible(false)
452 m_backgroundVisible(false)
442 {
453 {
443
454
444 }
455 }
445
456
446 QLegendPrivate::~QLegendPrivate()
457 QLegendPrivate::~QLegendPrivate()
447 {
458 {
448
459
449 }
460 }
450
461
451 void QLegendPrivate::setOffset(qreal x, qreal y)
462 void QLegendPrivate::setOffset(qreal x, qreal y)
452 {
463 {
453 m_layout->setOffset(x, y);
464 m_layout->setOffset(x, y);
454 }
465 }
455
466
456 QPointF QLegendPrivate::offset() const
467 QPointF QLegendPrivate::offset() const
457 {
468 {
458 return m_layout->offset();
469 return m_layout->offset();
459 }
470 }
460
471
461 int QLegendPrivate::roundness(qreal size)
472 int QLegendPrivate::roundness(qreal size)
462 {
473 {
463 return 100 * m_diameter / int(size);
474 return 100 * m_diameter / int(size);
464 }
475 }
465
476
477 void QLegendPrivate::appendSeries(QAbstractSeries* series)
478 {
479 Q_UNUSED(series);
480 // TODO:
481 /*
482 if (!m_series.contains(series)) {
483 m_series.append(series);
484 handleSeriesAdded(series,0); // Dummy domain
485 }
486 */
487 }
488
489 void QLegendPrivate::removeSeries(QAbstractSeries* series)
490 {
491 Q_UNUSED(series);
492 // TODO:
493 /*
494 if (m_series.contains(series)) {
495 m_series.removeOne(series);
496 handleSeriesRemoved(series);
497 }
498 */
499 }
500
466 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
501 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
467 {
502 {
468 Q_UNUSED(domain)
503 Q_UNUSED(domain)
469
504
470 qDebug() << "QLegendPrivate::handleSeriesAdded";
505 qDebug() << "QLegendPrivate::handleSeriesAdded";
471
506
472 // New markers --->
507 // New markers --->
473 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
508 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
474 foreach (QLegendMarker* marker, newMarkers) {
509 foreach (QLegendMarker* marker, newMarkers) {
475 marker->setFont(m_font);
510 marker->setFont(m_font);
476 marker->setLabelBrush(m_labelBrush);
511 marker->setLabelBrush(m_labelBrush);
477 marker->setVisible(series->isVisible());
512 marker->setVisible(series->isVisible());
478 // TODO: QLegendMarker has QGraphicsItem vs QLegendMarker is QGraphicsItem
479 // TODO: possible hazard. What if marker is deleted and group still has pointer?
513 // TODO: possible hazard. What if marker is deleted and group still has pointer?
480 // m_items->addToGroup(marker->d_ptr.data());
514 m_items->addToGroup(marker->d_ptr.data()->item());
515 // qDebug() << "item:" << marker->d_ptr.data()->item();
481 m_legendMarkers << marker;
516 m_legendMarkers << marker;
482 }
517 }
483
518
484 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
519 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
485 // <--- New markers
520 // <--- New markers
486
521
487 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
522 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
488
523
489 foreach (LegendMarker *marker, markers) {
524 foreach (LegendMarker *marker, markers) {
490 marker->setFont(m_font);
525 marker->setFont(m_font);
491 marker->setLabelBrush(m_labelBrush);
526 marker->setLabelBrush(m_labelBrush);
492 marker->setVisible(series->isVisible());
527 marker->setVisible(series->isVisible());
493 m_items->addToGroup(marker);
528 m_items->addToGroup(marker);
494 m_markers << marker;
529 m_markers << marker;
495 }
530 }
496
531
497 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
532 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
498 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
533 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
499
534
500 m_items->setVisible(false);
535 m_items->setVisible(false);
501 m_layout->invalidate();
536 m_layout->invalidate();
502 }
537 }
503
538
504 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
539 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
505 {
540 {
506 // New markers --->
541 // New markers --->
507 foreach (QLegendMarker *marker, m_legendMarkers) {
542 foreach (QLegendMarker *marker, m_legendMarkers) {
508 if (marker->series() == series) {
543 if (marker->series() == series) {
509 delete marker;
544 delete marker;
510 m_legendMarkers.removeAll(marker);
545 m_legendMarkers.removeAll(marker);
511 }
546 }
512 }
547 }
513
548
514 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
549 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
515 // <--- New markers
550 // <--- New markers
516
551
517 foreach (LegendMarker *marker, m_markers) {
552 foreach (LegendMarker *marker, m_markers) {
518 if (marker->series() == series) {
553 if (marker->series() == series) {
519 delete marker;
554 delete marker;
520 m_markers.removeAll(marker);
555 m_markers.removeAll(marker);
521 }
556 }
522 }
557 }
523
558
524 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
559 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
525 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
560 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
526 m_layout->invalidate();
561 m_layout->invalidate();
527 }
562 }
528
563
529 void QLegendPrivate::handleSeriesVisibleChanged()
564 void QLegendPrivate::handleSeriesVisibleChanged()
530 {
565 {
531 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
566 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
532 Q_ASSERT(series);
567 Q_ASSERT(series);
533
568
534 // New markers --->
569 // New markers --->
535 foreach (QLegendMarker* marker, m_legendMarkers) {
570 foreach (QLegendMarker* marker, m_legendMarkers) {
536 if (marker->series() == series) {
571 if (marker->series() == series) {
537 marker->setVisible(series->isVisible());
572 marker->setVisible(series->isVisible());
538 }
573 }
539 }
574 }
540
575
541 // <--- New markers
576 // <--- New markers
542
577
543 foreach (LegendMarker* marker, m_markers) {
578 foreach (LegendMarker* marker, m_markers) {
544 if (marker->series() == series) {
579 if (marker->series() == series) {
545 marker->setVisible(series->isVisible());
580 marker->setVisible(series->isVisible());
546 }
581 }
547 m_layout->invalidate();
582 m_layout->invalidate();
548 }
583 }
549
584
550 void QLegendPrivate::handleCountChanged()
585 void QLegendPrivate::handleCountChanged()
551 {
586 {
552 // With new markers, the series shoud notify markers directly?
587 // With new markers, the series shoud notify markers directly?
553
588
554 // Handle new or removed markers
589 // Handle new or removed markers
555 // Handle changes of marker pen/brush/label. every property that legend is interested
590 // Handle changes of marker pen/brush/label. every property that legend is interested
556 handleSeriesRemoved(series);
591 handleSeriesRemoved(series);
557 Domain domain;
592 Domain domain;
558 handleSeriesAdded(series, &domain);
593 handleSeriesAdded(series, &domain);
559 }
594 }
560
595
561 #include "moc_qlegend.cpp"
596 #include "moc_qlegend.cpp"
562 #include "moc_qlegend_p.cpp"
597 #include "moc_qlegend_p.cpp"
563
598
564 QTCOMMERCIALCHART_END_NAMESPACE
599 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,111 +1,108
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.h>
24 #include <qchartglobal.h>
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 QPieSlice;
32 class QXYSeries;
33 class QBarSet;
34 class QAbstractBarSeries;
35 class QPieSeries;
36 class QAreaSeries;
37 class QChart;
31 class QChart;
38 class QLegendPrivate;
32 class QLegendPrivate;
39 class QLegendMarker;
33 class QLegendMarker;
34 class QAbstractSeries;
40
35
41 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
36 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
42 {
37 {
43 Q_OBJECT
38 Q_OBJECT
44 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
39 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
45 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
40 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
46 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
41 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
47 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
42 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
48 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
43 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
49 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
44 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
50
45
51 private:
46 private:
52 explicit QLegend(QChart *chart);
47 explicit QLegend(QChart *chart);
53
48
54 public:
49 public:
55 ~QLegend();
50 ~QLegend();
56
51
57 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
52 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
58
53
59 void setBrush(const QBrush &brush);
54 void setBrush(const QBrush &brush);
60 QBrush brush() const;
55 QBrush brush() const;
61 void setColor(QColor color);
56 void setColor(QColor color);
62 QColor color();
57 QColor color();
63
58
64 void setPen(const QPen &pen);
59 void setPen(const QPen &pen);
65 QPen pen() const;
60 QPen pen() const;
66 void setBorderColor(QColor color);
61 void setBorderColor(QColor color);
67 QColor borderColor();
62 QColor borderColor();
68
63
69 void setFont(const QFont &font);
64 void setFont(const QFont &font);
70 QFont font() const;
65 QFont font() const;
71 void setLabelBrush(const QBrush &brush);
66 void setLabelBrush(const QBrush &brush);
72 QBrush labelBrush() const;
67 QBrush labelBrush() const;
73
68
74 void setLabelColor(QColor color);
69 void setLabelColor(QColor color);
75 QColor labelColor() const;
70 QColor labelColor() const;
76
71
77 void setAlignment(Qt::Alignment alignment);
72 void setAlignment(Qt::Alignment alignment);
78 Qt::Alignment alignment() const;
73 Qt::Alignment alignment() const;
79
74
80 void detachFromChart();
75 void detachFromChart();
81 void attachToChart();
76 void attachToChart();
82 bool isAttachedToChart();
77 bool isAttachedToChart();
83
78
84 void setBackgroundVisible(bool visible = true);
79 void setBackgroundVisible(bool visible = true);
85 bool isBackgroundVisible() const;
80 bool isBackgroundVisible() const;
86
81
87 // New stuff:
82 // New stuff:
88 QList <QLegendMarker*> markers() const;
83 QList <QLegendMarker*> markers() const;
84 void appendSeries(QAbstractSeries* series);
85 void removeSeries(QAbstractSeries* series);
89
86
90 protected:
87 protected:
91 void hideEvent(QHideEvent *event);
88 void hideEvent(QHideEvent *event);
92 void showEvent(QShowEvent *event);
89 void showEvent(QShowEvent *event);
93
90
94 Q_SIGNALS:
91 Q_SIGNALS:
95 void backgroundVisibleChanged(bool visible);
92 void backgroundVisibleChanged(bool visible);
96 void colorChanged(QColor color);
93 void colorChanged(QColor color);
97 void borderColorChanged(QColor color);
94 void borderColorChanged(QColor color);
98 void fontChanged(QFont font);
95 void fontChanged(QFont font);
99 void labelColorChanged(QColor color);
96 void labelColorChanged(QColor color);
100
97
101 private:
98 private:
102 QScopedPointer<QLegendPrivate> d_ptr;
99 QScopedPointer<QLegendPrivate> d_ptr;
103 Q_DISABLE_COPY(QLegend)
100 Q_DISABLE_COPY(QLegend)
104 friend class LegendScroller;
101 friend class LegendScroller;
105 friend class LegendLayout;
102 friend class LegendLayout;
106 friend class ChartLayout;
103 friend class ChartLayout;
107 };
104 };
108
105
109 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
110
107
111 #endif // QLEGEND_H
108 #endif // QLEGEND_H
@@ -1,93 +1,96
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGEND_P_H
30 #ifndef QLEGEND_P_H
31 #define QLEGEND_P_H
31 #define QLEGEND_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QChart;
37 class QChart;
38 class ChartPresenter;
38 class ChartPresenter;
39 class QAbstractSeries;
39 class QAbstractSeries;
40 class LegendLayout;
40 class LegendLayout;
41 class LegendMarker;
41 class LegendMarker;
42 class Domain;
42 class Domain;
43 class QLegendMarker;
43 class QLegendMarker;
44
44
45 class QLegendPrivate : public QObject
45 class QLegendPrivate : public QObject
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 public:
48 public:
49 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
49 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
50 ~QLegendPrivate();
50 ~QLegendPrivate();
51
51
52 void setOffset(qreal x, qreal y);
52 void setOffset(qreal x, qreal y);
53 QPointF offset() const;
53 QPointF offset() const;
54 int roundness(qreal size);
54 int roundness(qreal size);
55
55
56 QList<LegendMarker*> markers() { return m_markers; } // TODO: this will be removed
56 QList<LegendMarker*> markers() { return m_markers; } // TODO: this will be removed
57 QGraphicsItemGroup* items() { return m_items; }
57 QGraphicsItemGroup* items() { return m_items; }
58
58
59 // New stuff:
59 // New stuff:
60 QList<QLegendMarker*> legendMarkers() { return m_legendMarkers; } // TODO: function name will change
60 QList<QLegendMarker*> legendMarkers() { return m_legendMarkers; } // TODO: function name will change
61 void appendSeries(QAbstractSeries* series);
62 void removeSeries(QAbstractSeries* series);
61
63
62 public Q_SLOTS:
64 public Q_SLOTS:
63 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
65 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
64 void handleSeriesRemoved(QAbstractSeries *series);
66 void handleSeriesRemoved(QAbstractSeries *series);
65 void handleSeriesVisibleChanged();
67 void handleSeriesVisibleChanged();
66 void handleCountChanged();
68 void handleCountChanged();
67
69
68 private:
70 private:
69 QLegend *q_ptr;
71 QLegend *q_ptr;
70 ChartPresenter *m_presenter;
72 ChartPresenter *m_presenter;
71 LegendLayout *m_layout;
73 LegendLayout *m_layout;
72 QChart* m_chart;
74 QChart* m_chart;
73 QGraphicsItemGroup* m_items;
75 QGraphicsItemGroup* m_items;
74 QList<LegendMarker*> m_markers; // TODO: this will be removed
76 QList<LegendMarker*> m_markers; // TODO: this will be removed
75 Qt::Alignment m_alignment;
77 Qt::Alignment m_alignment;
76 QBrush m_brush;
78 QBrush m_brush;
77 QPen m_pen;
79 QPen m_pen;
78 QFont m_font;
80 QFont m_font;
79 QBrush m_labelBrush;
81 QBrush m_labelBrush;
80
82
81 qreal m_diameter;
83 qreal m_diameter;
82 bool m_attachedToChart;
84 bool m_attachedToChart;
83 bool m_backgroundVisible;
85 bool m_backgroundVisible;
84
86
85 friend class QLegend;
87 friend class QLegend;
86 friend class LegendLayout;
88 friend class LegendLayout;
87 QList<QLegendMarker*> m_legendMarkers; // TODO: rename to m_markers eventually.
89 QList<QLegendMarker*> m_legendMarkers; // TODO: rename to m_markers eventually.
90 QList<QAbstractSeries*> m_series;
88
91
89 };
92 };
90
93
91 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
92
95
93 #endif
96 #endif
@@ -1,121 +1,137
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 "qlegendmarker.h"
21 #include "qlegendmarker.h"
22 #include "qlegendmarker_p.h"
22 #include "qlegendmarker_p.h"
23 #include "legendmarkeritem_p.h"
23 #include "legendmarkeritem_p.h"
24 #include <QDebug>
24 #include <QDebug>
25 #include <QFontMetrics>
25 #include <QFontMetrics>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28 /*
29 QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) :
29 QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) :
30 QObject(parent),
30 QObject(parent),
31 d_ptr(new QLegendMarkerPrivate(series, this))
31 d_ptr(new QLegendMarkerPrivate(series, this))
32 {
32 {
33 }
33 }
34 */
35 QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) :
36 QObject(parent),
37 d_ptr(&d)
38 {
39 }
34
40
35 QLegendMarker::~QLegendMarker()
41 QLegendMarker::~QLegendMarker()
36 {
42 {
37 }
43 }
38
44
39 QString QLegendMarker::label() const
45 QString QLegendMarker::label() const
40 {
46 {
41 return d_ptr->m_label;
47 return d_ptr->m_label;
42 }
48 }
43
49
44 void QLegendMarker::setLabel(const QString &label)
50 void QLegendMarker::setLabel(const QString &label)
45 {
51 {
46 d_ptr->m_label = label;
52 d_ptr->m_label = label;
47 }
53 }
48
54
49 QBrush QLegendMarker::labelBrush() const
55 QBrush QLegendMarker::labelBrush() const
50 {
56 {
51 return d_ptr->m_labelBrush;
57 return d_ptr->m_labelBrush;
52 }
58 }
53
59
54 void QLegendMarker::setLabelBrush(const QBrush &brush)
60 void QLegendMarker::setLabelBrush(const QBrush &brush)
55 {
61 {
56 d_ptr->m_labelBrush = brush;
62 d_ptr->m_labelBrush = brush;
57 }
63 }
58
64
59 QFont QLegendMarker::font() const
65 QFont QLegendMarker::font() const
60 {
66 {
61 return d_ptr->m_font;
67 return d_ptr->m_font;
62 }
68 }
63
69
64 void QLegendMarker::setFont(const QFont &font)
70 void QLegendMarker::setFont(const QFont &font)
65 {
71 {
66 d_ptr->m_font = font;
72 d_ptr->m_font = font;
67 }
73 }
68
74
69 QPen QLegendMarker::pen() const
75 QPen QLegendMarker::pen() const
70 {
76 {
71 return d_ptr->m_pen;
77 return d_ptr->m_pen;
72 }
78 }
73
79
74 void QLegendMarker::setPen(const QPen &pen)
80 void QLegendMarker::setPen(const QPen &pen)
75 {
81 {
76 d_ptr->m_pen = pen;
82 d_ptr->m_pen = pen;
77 }
83 }
78
84
79 QBrush QLegendMarker::brush() const
85 QBrush QLegendMarker::brush() const
80 {
86 {
81 return d_ptr->m_brush;
87 return d_ptr->m_brush;
82 }
88 }
83
89
84 void QLegendMarker::setBrush(const QBrush &brush)
90 void QLegendMarker::setBrush(const QBrush &brush)
85 {
91 {
86 d_ptr->m_brush = brush;
92 d_ptr->m_brush = brush;
87 }
93 }
88
94
89 bool QLegendMarker::isVisible() const
95 bool QLegendMarker::isVisible() const
90 {
96 {
91 return d_ptr->m_visible;
97 return d_ptr->m_visible;
92 }
98 }
93
99
94 void QLegendMarker::setVisible(bool visible)
100 void QLegendMarker::setVisible(bool visible)
95 {
101 {
96 d_ptr->m_visible = visible;
102 d_ptr->m_visible = visible;
97 }
103 }
98
104
99 QAbstractSeries* QLegendMarker::series()
100 {
101 return d_ptr->m_series;
102 }
103
104 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105
106 /*
106 QLegendMarkerPrivate::QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q) :
107 QLegendMarkerPrivate::QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q) :
107 q_ptr(q),
108 q_ptr(q),
108 m_series(series)
109 m_series(series)
109 {
110 {
110 m_item = new LegendMarkerItem(m_series);
111 m_item = new LegendMarkerItem(m_series);
111 }
112 }
113 */
114 QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q) :
115 q_ptr(q)
116 {
117 qDebug() << "QLegendMarkerPrivate created";
118 m_item = new LegendMarkerItem(this);
119 }
112
120
113 QLegendMarkerPrivate::~QLegendMarkerPrivate()
121 QLegendMarkerPrivate::~QLegendMarkerPrivate()
114 {
122 {
115 }
123 }
116
124
125 void QLegendMarkerPrivate::handleMousePressEvent(QGraphicsSceneEvent *event)
126 {
127 // Just emit clicked signal for now
128 Q_UNUSED(event);
129 Q_Q(QLegendMarker);
130 emit q->clicked();
131 }
132
117
133
118 #include "moc_qlegendmarker.cpp"
134 #include "moc_qlegendmarker.cpp"
119 #include "moc_qlegendmarker_p.cpp"
135 #include "moc_qlegendmarker_p.cpp"
120
136
121 QTCOMMERCIALCHART_END_NAMESPACE
137 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,86 +1,90
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 QLEGENDMARKER_H
21 #ifndef QLEGENDMARKER_H
22 #define QLEGENDMARKER_H
22 #define QLEGENDMARKER_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QObject>
25 #include <QObject>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28 #include <QFont>
28 #include <QFont>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QLegendMarkerPrivate;
32 class QLegendMarkerPrivate;
33 class QAbstractSeries;
33 class QAbstractSeries;
34
34
35 // TODO: should this be QAbstractLegendMarker?
35 // TODO: should this be QAbstractLegendMarker?
36 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
36 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
37 {
37 {
38 Q_OBJECT
38 Q_OBJECT
39
39
40 // TODO: need for these?
40 // TODO: need for these?
41 // Q_PROPERTY(QString label READ label WRITE setlabel NOTIFY labelChanged);
41 // Q_PROPERTY(QString label READ label WRITE setlabel NOTIFY labelChanged);
42 // Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged);
42 // Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged);
43 // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged);
43 // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged);
44
44
45 public:
45 public:
46 explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0);
46 // explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0);
47 virtual ~QLegendMarker();
47 virtual ~QLegendMarker();
48
48
49 QString label() const;
49 QString label() const;
50 void setLabel(const QString &label);
50 void setLabel(const QString &label);
51
51
52 QBrush labelBrush() const;
52 QBrush labelBrush() const;
53 void setLabelBrush(const QBrush &brush);
53 void setLabelBrush(const QBrush &brush);
54
54
55 QFont font() const;
55 QFont font() const;
56 void setFont(const QFont &font);
56 void setFont(const QFont &font);
57
57
58 QPen pen() const;
58 QPen pen() const;
59 void setPen(const QPen &pen);
59 void setPen(const QPen &pen);
60
60
61 QBrush brush() const;
61 QBrush brush() const;
62 void setBrush(const QBrush &brush);
62 void setBrush(const QBrush &brush);
63
63
64 bool isVisible() const;
64 bool isVisible() const;
65 void setVisible(bool visible);
65 void setVisible(bool visible);
66
66
67 // virtual QAbstractSeries::SeriesType type() = 0;
67 // virtual QAbstractSeries::SeriesType type() = 0; // TODO?
68 virtual QAbstractSeries* series();
68 virtual QAbstractSeries* series() = 0;
69 virtual QObject* peerObject() = 0;
69 virtual QObject* peerObject() = 0;
70
70
71 protected:
72 explicit QLegendMarker(QLegendMarkerPrivate &d, QObject *parent = 0);
73
71 Q_SIGNALS:
74 Q_SIGNALS:
72 void clicked();
75 void clicked();
73 void hovered(bool status);
76 void hovered(bool status);
74
77
75 public Q_SLOTS:
78 public Q_SLOTS:
76 virtual void updated() = 0; // TODO: private. Idea is that series signals, when some property has changed
79 virtual void updated() = 0; // TODO: private. Idea is that series signals, when some property has changed
77
80
78 public:
81 protected:
79 QScopedPointer<QLegendMarkerPrivate> d_ptr;
82 QScopedPointer<QLegendMarkerPrivate> d_ptr;
80 Q_DISABLE_COPY(QLegendMarker)
83 Q_DISABLE_COPY(QLegendMarker)
81
84 friend class QLegendPrivate;
85 friend class QLegendMarkerPrivate;
82 };
86 };
83
87
84 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
85
89
86 #endif // QLEGENDMARKER_H
90 #endif // QLEGENDMARKER_H
@@ -1,113 +1,111
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGENDMARKERPRIVATE_H
30 #ifndef QLEGENDMARKERPRIVATE_H
31 #define QLEGENDMARKERPRIVATE_H
31 #define QLEGENDMARKERPRIVATE_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QBrush>
35 #include <QBrush>
36 #include <QPen>
36 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
37 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsLayoutItem>
38 #include <QGraphicsLayoutItem>
39 #include <QDebug>
39
40
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41
42
42 // TODO: check these
43 // TODO: check these
43 class QAbstractSeries;
44 class QAbstractSeries;
44 class QAreaSeries;
45 class QAreaSeries;
45 class QXYSeries;
46 class QXYSeries;
46 class QBarSet;
47 class QBarSet;
47 class QAbstractBarSeries;
48 class QAbstractBarSeries;
48 class QPieSlice;
49 class QPieSlice;
49 class QLegend;
50 class QLegend;
50 class QPieSeries;
51 class QPieSeries;
51
52
52 class QLegendMarker;
53 class QLegendMarker;
53 class LegendMarkerItem;
54 class LegendMarkerItem;
54
55
55 class QLegendMarkerPrivate : public QObject
56 class QLegendMarkerPrivate : public QObject
56 {
57 {
57 Q_OBJECT
58 Q_OBJECT
58 public:
59 public:
59 explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q);
60 // explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q);
61 explicit QLegendMarkerPrivate(QLegendMarker *q);
60 virtual ~QLegendMarkerPrivate();
62 virtual ~QLegendMarkerPrivate();
61 /*
63 /*
62 void setPen(const QPen &pen);
64 void setPen(const QPen &pen);
63 QPen pen() const;
65 QPen pen() const;
64
66
65 void setBrush(const QBrush &brush);
67 void setBrush(const QBrush &brush);
66 QBrush brush() const;
68 QBrush brush() const;
67
69
68 void setFont(const QFont &font);
70 void setFont(const QFont &font);
69 QFont font() const;
71 QFont font() const;
70
72
71 void setLabel(const QString label);
73 void setLabel(const QString label);
72 QString label() const;
74 QString label() const;
73
75
74 void setLabelBrush(const QBrush &brush);
76 void setLabelBrush(const QBrush &brush);
75 QBrush labelBrush() const;
77 QBrush labelBrush() const;
76 */
78 */
79 // Helper for now.
80 LegendMarkerItem* item() const { return m_item; }
81
82 // Item gets the event, logic for event is here
83 void handleMousePressEvent(QGraphicsSceneEvent *event);
77
84
78 public Q_SLOTS:
85 public Q_SLOTS:
79 virtual void updated() {};
86 virtual void updated() {};
80
87
81 private:
88 protected:
82 QLegendMarker *q_ptr;
83
84 LegendMarkerItem *m_item;
89 LegendMarkerItem *m_item;
85
90
91 private:
92 QLegendMarker *q_ptr;
86 /*
93 /*
87 QLegend* m_legend;
94 QLegend* m_legend;
88 */
95 */
89
96
90 // New legend marker properties
97 // New legend marker properties
91 QAbstractSeries* m_series;
92 QString m_label;
98 QString m_label;
93 QBrush m_labelBrush;
99 QBrush m_labelBrush;
94 QFont m_font;
100 QFont m_font;
95 QPen m_pen;
101 QPen m_pen;
96 QBrush m_brush;
102 QBrush m_brush;
97 bool m_visible;
103 bool m_visible;
98
104
99 // Implementation details of new marker
100 QRectF m_markerRect;
101 QRectF m_boundingRect;
102 QGraphicsSimpleTextItem *m_textItem;
103 QGraphicsRectItem *m_rectItem;
104 qreal m_margin;
105 qreal m_space;
106
107 friend class QLegendPrivate; // TODO: Is this needed?
105 friend class QLegendPrivate; // TODO: Is this needed?
108 Q_DECLARE_PUBLIC(QLegendMarker)
106 Q_DECLARE_PUBLIC(QLegendMarker)
109 };
107 };
110
108
111 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
112
110
113 #endif // QLEGENDMARKERPRIVATE_H
111 #endif // QLEGENDMARKERPRIVATE_H
@@ -1,62 +1,91
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 "qpielegendmarker.h"
21 #include "qpielegendmarker.h"
22 #include "qpielegendmarker_p.h"
22 #include "qpielegendmarker_p.h"
23 #include <QPieSeries>
23 #include <QPieSeries>
24 #include <QDebug>
24
25
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
27 /*
27 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
28 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
28 QLegendMarker(series, parent),
29 QLegendMarker(series, parent),
29 m_slice(slice)
30 d_ptr(new QPieLegendMarkerPrivate(series,slice,this))
30 {
31 {
31 QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated()));
32 QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated()));
32 QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated()));
33 QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated()));
33 updated();
34 updated();
34 }
35 }
35
36
36 QPieSlice* QPieLegendMarker::peerObject()
37 */
38 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
39 QLegendMarker(*new QPieLegendMarkerPrivate(series,slice,this), parent)
40 {
41 QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated()));
42 QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated()));
43 // updated();
44 }
45
46 /*!
47 \internal
48 */
49 QPieLegendMarker::QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent) :
50 QLegendMarker(d, parent)
37 {
51 {
38 return m_slice;
39 }
52 }
40
53
41 void QPieLegendMarker::updated()
54 QAbstractSeries* QPieLegendMarker::series()
42 {
55 {
43 // TODO: to PIMPL.
56 Q_D(QPieLegendMarker);
44 setBrush(m_slice->brush());
57 return d->m_series;
45 setLabel(m_slice->label());
58 }
59
60 QPieSlice* QPieLegendMarker::peerObject()
61 {
62 Q_D(QPieLegendMarker);
63 return d->m_slice;
46 }
64 }
47
65
48 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49
67
50 QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q) :
68 //QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q) :
51 QLegendMarkerPrivate(series, q)
69 QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QPieSeries *series, QPieSlice *slice, QPieLegendMarker *q) :
70 QLegendMarkerPrivate(q),
71 m_series(series),
72 m_slice(slice)
52 {
73 {
74 qDebug() << "QPieLegendMarkerPrivate created";
75 updated();
53 }
76 }
54
77
55 QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate()
78 QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate()
56 {
79 {
57 }
80 }
58
81
82 void QPieLegendMarkerPrivate::updated()
83 {
84 m_item->setBrush(m_slice->brush());
85 m_item->setLabel(m_slice->label());
86 }
87
59 #include "moc_qpielegendmarker.cpp"
88 #include "moc_qpielegendmarker.cpp"
60 #include "moc_qpielegendmarker_p.cpp"
89 #include "moc_qpielegendmarker_p.cpp"
61
90
62 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,57 +1,55
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 QPIELEGENDMARKER_H
21 #ifndef QPIELEGENDMARKER_H
22 #define QPIELEGENDMARKER_H
22 #define QPIELEGENDMARKER_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QLegendMarker>
25 #include <QLegendMarker>
26 #include <QPieSlice>
26 #include <QPieSlice>
27 #include "qpielegendmarker_p.h"
27 #include "qpielegendmarker_p.h"
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker
31 class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public:
35 public:
36 explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0);
36 explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0);
37
37
38 virtual QAbstractSeries* series();
38 virtual QPieSlice* peerObject();
39 virtual QPieSlice* peerObject();
39
40
40 // TODO: to pimpl.
41 protected:
41 void updated();
42 QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent = 0);
42
43
43 //Q_SIGNALS:
44 //Q_SIGNALS:
44
45
45 //public Q_SLOTS:
46 //public Q_SLOTS:
46
47
47 private:
48 private:
48 QScopedPointer<QPieLegendMarkerPrivate> d_ptr;
49 Q_DECLARE_PRIVATE(QPieLegendMarker)
49 Q_DISABLE_COPY(QPieLegendMarker)
50 Q_DISABLE_COPY(QPieLegendMarker)
50
51
51 // TODO: PIMPL
52 QPieSlice* m_slice;
53
54 };
52 };
55
53
56 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
57 #endif // QPIELEGENDMARKER_H
55 #endif // QPIELEGENDMARKER_H
@@ -1,61 +1,68
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QPIELEGENDMARKER_P_H
30 #ifndef QPIELEGENDMARKER_P_H
31 #define QPIELEGENDMARKER_P_H
31 #define QPIELEGENDMARKER_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "qlegendmarker_p.h"
34 #include "qlegendmarker_p.h"
35 #include "legendmarkeritem_p.h"
35 #include "legendmarkeritem_p.h"
36 #include <QPieSeries>
37 #include <QPieSlice>
38
39 #include <QDebug>
36
40
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
42
39 class QPieLegendMarker;
43 class QPieLegendMarker;
40
44
41 class QPieLegendMarkerPrivate : public QLegendMarkerPrivate
45 class QPieLegendMarkerPrivate : public QLegendMarkerPrivate
42 {
46 {
43 Q_OBJECT
47 Q_OBJECT
44 public:
48 public:
45 explicit QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q);
49 // explicit QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q);
50 explicit QPieLegendMarkerPrivate(QPieSeries *series, QPieSlice *slice, QPieLegendMarker *q);
46 virtual ~QPieLegendMarkerPrivate();
51 virtual ~QPieLegendMarkerPrivate();
47
52
48 public Q_SLOTS:
53 public Q_SLOTS:
49 virtual void updated() {};
54 virtual void updated();
50
55
51 private:
56 private:
52 QPieLegendMarker *q_ptr;
57 QPieLegendMarker *q_ptr;
53 PieLegendMarkerItem *m_item;
58
59 QPieSeries* m_series;
60 QPieSlice* m_slice;
54
61
55 friend class QLegendPrivate; // TODO: Is this needed?
62 friend class QLegendPrivate; // TODO: Is this needed?
56 Q_DECLARE_PUBLIC(QPieLegendMarker)
63 Q_DECLARE_PUBLIC(QPieLegendMarker)
57 };
64 };
58
65
59 QTCOMMERCIALCHART_END_NAMESPACE
66 QTCOMMERCIALCHART_END_NAMESPACE
60
67
61 #endif // QPIELEGENDMARKER_P_H
68 #endif // QPIELEGENDMARKER_P_H
@@ -1,892 +1,892
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 "qpieseries.h"
21 #include "qpieseries.h"
22 #include "qpieseries_p.h"
22 #include "qpieseries_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieslice_p.h"
24 #include "qpieslice_p.h"
25 #include "pieslicedata_p.h"
25 #include "pieslicedata_p.h"
26 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
27 #include "charttheme_p.h"
27 #include "charttheme_p.h"
28 #include "legendmarker_p.h"
28 #include "legendmarker_p.h"
29 #include "qabstractaxis.h"
29 #include "qabstractaxis.h"
30 #include "pieanimation_p.h"
30 #include "pieanimation_p.h"
31
31
32 #include "qpielegendmarker.h"
32 #include "qpielegendmarker.h"
33
33
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
35
36 /*!
36 /*!
37 \class QPieSeries
37 \class QPieSeries
38 \brief Pie series API for QtCommercial Charts
38 \brief Pie series API for QtCommercial Charts
39
39
40 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
40 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
41 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
41 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
42 The actual slice size is determined by that relative value.
42 The actual slice size is determined by that relative value.
43
43
44 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
44 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
45 These relate to the actual chart rectangle.
45 These relate to the actual chart rectangle.
46
46
47 By default the pie is defined as a full pie but it can also be a partial pie.
47 By default the pie is defined as a full pie but it can also be a partial pie.
48 This can be done by setting a starting angle and angle span to the series.
48 This can be done by setting a starting angle and angle span to the series.
49 Full pie is 360 degrees where 0 is at 12 a'clock.
49 Full pie is 360 degrees where 0 is at 12 a'clock.
50
50
51 See the \l {PieChart Example} {pie chart example} or \l {DonutChart Example} {donut chart example} to learn how to use QPieSeries.
51 See the \l {PieChart Example} {pie chart example} or \l {DonutChart Example} {donut chart example} to learn how to use QPieSeries.
52 \table 100%
52 \table 100%
53 \row
53 \row
54 \o \image examples_piechart.png
54 \o \image examples_piechart.png
55 \o \image examples_donut.png
55 \o \image examples_donut.png
56 \endtable
56 \endtable
57 */
57 */
58 /*!
58 /*!
59 \qmlclass PieSeries QPieSeries
59 \qmlclass PieSeries QPieSeries
60 \inherits AbstractSeries
60 \inherits AbstractSeries
61
61
62 The following QML shows how to create a simple pie chart.
62 The following QML shows how to create a simple pie chart.
63
63
64 \snippet ../demos/qmlchart/qml/qmlchart/View1.qml 1
64 \snippet ../demos/qmlchart/qml/qmlchart/View1.qml 1
65
65
66 \beginfloatleft
66 \beginfloatleft
67 \image demos_qmlchart1.png
67 \image demos_qmlchart1.png
68 \endfloat
68 \endfloat
69 \clearfloat
69 \clearfloat
70 */
70 */
71
71
72 /*!
72 /*!
73 \property QPieSeries::horizontalPosition
73 \property QPieSeries::horizontalPosition
74 \brief Defines the horizontal position of the pie.
74 \brief Defines the horizontal position of the pie.
75
75
76 The value is a relative value to the chart rectangle where:
76 The value is a relative value to the chart rectangle where:
77
77
78 \list
78 \list
79 \o 0.0 is the absolute left.
79 \o 0.0 is the absolute left.
80 \o 1.0 is the absolute right.
80 \o 1.0 is the absolute right.
81 \endlist
81 \endlist
82 Default value is 0.5 (center).
82 Default value is 0.5 (center).
83 \sa verticalPosition
83 \sa verticalPosition
84 */
84 */
85
85
86 /*!
86 /*!
87 \qmlproperty real PieSeries::horizontalPosition
87 \qmlproperty real PieSeries::horizontalPosition
88
88
89 Defines the horizontal position of the pie.
89 Defines the horizontal position of the pie.
90
90
91 The value is a relative value to the chart rectangle where:
91 The value is a relative value to the chart rectangle where:
92
92
93 \list
93 \list
94 \o 0.0 is the absolute left.
94 \o 0.0 is the absolute left.
95 \o 1.0 is the absolute right.
95 \o 1.0 is the absolute right.
96 \endlist
96 \endlist
97 Default value is 0.5 (center).
97 Default value is 0.5 (center).
98 \sa verticalPosition
98 \sa verticalPosition
99 */
99 */
100
100
101 /*!
101 /*!
102 \property QPieSeries::verticalPosition
102 \property QPieSeries::verticalPosition
103 \brief Defines the vertical position of the pie.
103 \brief Defines the vertical position of the pie.
104
104
105 The value is a relative value to the chart rectangle where:
105 The value is a relative value to the chart rectangle where:
106
106
107 \list
107 \list
108 \o 0.0 is the absolute top.
108 \o 0.0 is the absolute top.
109 \o 1.0 is the absolute bottom.
109 \o 1.0 is the absolute bottom.
110 \endlist
110 \endlist
111 Default value is 0.5 (center).
111 Default value is 0.5 (center).
112 \sa horizontalPosition
112 \sa horizontalPosition
113 */
113 */
114
114
115 /*!
115 /*!
116 \qmlproperty real PieSeries::verticalPosition
116 \qmlproperty real PieSeries::verticalPosition
117
117
118 Defines the vertical position of the pie.
118 Defines the vertical position of the pie.
119
119
120 The value is a relative value to the chart rectangle where:
120 The value is a relative value to the chart rectangle where:
121
121
122 \list
122 \list
123 \o 0.0 is the absolute top.
123 \o 0.0 is the absolute top.
124 \o 1.0 is the absolute bottom.
124 \o 1.0 is the absolute bottom.
125 \endlist
125 \endlist
126 Default value is 0.5 (center).
126 Default value is 0.5 (center).
127 \sa horizontalPosition
127 \sa horizontalPosition
128 */
128 */
129
129
130 /*!
130 /*!
131 \property QPieSeries::size
131 \property QPieSeries::size
132 \brief Defines the pie size.
132 \brief Defines the pie size.
133
133
134 The value is a relative value to the chart rectangle where:
134 The value is a relative value to the chart rectangle where:
135
135
136 \list
136 \list
137 \o 0.0 is the minimum size (pie not drawn).
137 \o 0.0 is the minimum size (pie not drawn).
138 \o 1.0 is the maximum size that can fit the chart.
138 \o 1.0 is the maximum size that can fit the chart.
139 \endlist
139 \endlist
140
140
141 When setting this property the holeSize property is adjusted if necessary, to ensure that the hole size is not greater than the outer size.
141 When setting this property the holeSize property is adjusted if necessary, to ensure that the hole size is not greater than the outer size.
142
142
143 Default value is 0.7.
143 Default value is 0.7.
144 */
144 */
145
145
146 /*!
146 /*!
147 \qmlproperty real PieSeries::size
147 \qmlproperty real PieSeries::size
148
148
149 Defines the pie size.
149 Defines the pie size.
150
150
151 The value is a relative value to the chart rectangle where:
151 The value is a relative value to the chart rectangle where:
152
152
153 \list
153 \list
154 \o 0.0 is the minimum size (pie not drawn).
154 \o 0.0 is the minimum size (pie not drawn).
155 \o 1.0 is the maximum size that can fit the chart.
155 \o 1.0 is the maximum size that can fit the chart.
156 \endlist
156 \endlist
157
157
158 Default value is 0.7.
158 Default value is 0.7.
159 */
159 */
160
160
161 /*!
161 /*!
162 \property QPieSeries::holeSize
162 \property QPieSeries::holeSize
163 \brief Defines the donut hole size.
163 \brief Defines the donut hole size.
164
164
165 The value is a relative value to the chart rectangle where:
165 The value is a relative value to the chart rectangle where:
166
166
167 \list
167 \list
168 \o 0.0 is the minimum size (full pie drawn, without any hole inside).
168 \o 0.0 is the minimum size (full pie drawn, without any hole inside).
169 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
169 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
170 \endlist
170 \endlist
171
171
172 The value is never greater then size property.
172 The value is never greater then size property.
173 Default value is 0.0.
173 Default value is 0.0.
174 */
174 */
175
175
176 /*!
176 /*!
177 \qmlproperty real PieSeries::holeSize
177 \qmlproperty real PieSeries::holeSize
178
178
179 Defines the donut hole size.
179 Defines the donut hole size.
180
180
181 The value is a relative value to the chart rectangle where:
181 The value is a relative value to the chart rectangle where:
182
182
183 \list
183 \list
184 \o 0.0 is the minimum size (full pie drawn, without any hole inside).
184 \o 0.0 is the minimum size (full pie drawn, without any hole inside).
185 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
185 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
186 \endlist
186 \endlist
187
187
188 When setting this property the size property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
188 When setting this property the size property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
189
189
190 Default value is 0.0.
190 Default value is 0.0.
191 */
191 */
192
192
193 /*!
193 /*!
194 \property QPieSeries::startAngle
194 \property QPieSeries::startAngle
195 \brief Defines the starting angle of the pie.
195 \brief Defines the starting angle of the pie.
196
196
197 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
197 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
198
198
199 Default is value is 0.
199 Default is value is 0.
200 */
200 */
201
201
202 /*!
202 /*!
203 \qmlproperty real PieSeries::startAngle
203 \qmlproperty real PieSeries::startAngle
204
204
205 Defines the starting angle of the pie.
205 Defines the starting angle of the pie.
206
206
207 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
207 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
208
208
209 Default is value is 0.
209 Default is value is 0.
210 */
210 */
211
211
212 /*!
212 /*!
213 \property QPieSeries::endAngle
213 \property QPieSeries::endAngle
214 \brief Defines the ending angle of the pie.
214 \brief Defines the ending angle of the pie.
215
215
216 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
216 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
217
217
218 Default is value is 360.
218 Default is value is 360.
219 */
219 */
220
220
221 /*!
221 /*!
222 \qmlproperty real PieSeries::endAngle
222 \qmlproperty real PieSeries::endAngle
223
223
224 Defines the ending angle of the pie.
224 Defines the ending angle of the pie.
225
225
226 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
226 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
227
227
228 Default is value is 360.
228 Default is value is 360.
229 */
229 */
230
230
231 /*!
231 /*!
232 \property QPieSeries::count
232 \property QPieSeries::count
233
233
234 Number of slices in the series.
234 Number of slices in the series.
235 */
235 */
236
236
237 /*!
237 /*!
238 \qmlproperty int PieSeries::count
238 \qmlproperty int PieSeries::count
239
239
240 Number of slices in the series.
240 Number of slices in the series.
241 */
241 */
242
242
243 /*!
243 /*!
244 \fn void QPieSeries::countChanged()
244 \fn void QPieSeries::countChanged()
245 Emitted when the slice count has changed.
245 Emitted when the slice count has changed.
246 \sa count
246 \sa count
247 */
247 */
248 /*!
248 /*!
249 \qmlsignal PieSeries::onCountChanged()
249 \qmlsignal PieSeries::onCountChanged()
250 Emitted when the slice count has changed.
250 Emitted when the slice count has changed.
251 */
251 */
252
252
253 /*!
253 /*!
254 \property QPieSeries::sum
254 \property QPieSeries::sum
255
255
256 Sum of all slices.
256 Sum of all slices.
257
257
258 The series keeps track of the sum of all slices it holds.
258 The series keeps track of the sum of all slices it holds.
259 */
259 */
260
260
261 /*!
261 /*!
262 \qmlproperty real PieSeries::sum
262 \qmlproperty real PieSeries::sum
263
263
264 Sum of all slices.
264 Sum of all slices.
265
265
266 The series keeps track of the sum of all slices it holds.
266 The series keeps track of the sum of all slices it holds.
267 */
267 */
268
268
269 /*!
269 /*!
270 \fn void QPieSeries::sumChanged()
270 \fn void QPieSeries::sumChanged()
271 Emitted when the sum of all slices has changed.
271 Emitted when the sum of all slices has changed.
272 \sa sum
272 \sa sum
273 */
273 */
274 /*!
274 /*!
275 \qmlsignal PieSeries::onSumChanged()
275 \qmlsignal PieSeries::onSumChanged()
276 Emitted when the sum of all slices has changed. This may happen for example if you add or remove slices, or if you
276 Emitted when the sum of all slices has changed. This may happen for example if you add or remove slices, or if you
277 change value of a slice.
277 change value of a slice.
278 */
278 */
279
279
280 /*!
280 /*!
281 \fn void QPieSeries::added(QList<QPieSlice*> slices)
281 \fn void QPieSeries::added(QList<QPieSlice*> slices)
282
282
283 This signal is emitted when \a slices have been added to the series.
283 This signal is emitted when \a slices have been added to the series.
284
284
285 \sa append(), insert()
285 \sa append(), insert()
286 */
286 */
287 /*!
287 /*!
288 \qmlsignal PieSeries::onAdded(PieSlice slice)
288 \qmlsignal PieSeries::onAdded(PieSlice slice)
289 Emitted when \a slice has been added to the series.
289 Emitted when \a slice has been added to the series.
290 */
290 */
291
291
292 /*!
292 /*!
293 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
293 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
294 This signal is emitted when \a slices have been removed from the series.
294 This signal is emitted when \a slices have been removed from the series.
295 \sa remove()
295 \sa remove()
296 */
296 */
297 /*!
297 /*!
298 \qmlsignal PieSeries::onRemoved(PieSlice slice)
298 \qmlsignal PieSeries::onRemoved(PieSlice slice)
299 Emitted when \a slice has been removed from the series.
299 Emitted when \a slice has been removed from the series.
300 */
300 */
301
301
302 /*!
302 /*!
303 \fn void QPieSeries::clicked(QPieSlice* slice)
303 \fn void QPieSeries::clicked(QPieSlice* slice)
304 This signal is emitted when a \a slice has been clicked.
304 This signal is emitted when a \a slice has been clicked.
305 \sa QPieSlice::clicked()
305 \sa QPieSlice::clicked()
306 */
306 */
307 /*!
307 /*!
308 \qmlsignal PieSeries::onClicked(PieSlice slice)
308 \qmlsignal PieSeries::onClicked(PieSlice slice)
309 This signal is emitted when a \a slice has been clicked.
309 This signal is emitted when a \a slice has been clicked.
310 */
310 */
311
311
312 /*!
312 /*!
313 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
313 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
314 This signal is emitted when user has hovered over or away from the \a slice.
314 This signal is emitted when user has hovered over or away from the \a slice.
315 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
315 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
316 \sa QPieSlice::hovered()
316 \sa QPieSlice::hovered()
317 */
317 */
318 /*!
318 /*!
319 \qmlsignal PieSeries::onHovered(PieSlice slice, bool state)
319 \qmlsignal PieSeries::onHovered(PieSlice slice, bool state)
320 This signal is emitted when user has hovered over or away from the \a slice. \a state is true when user has hovered
320 This signal is emitted when user has hovered over or away from the \a slice. \a state is true when user has hovered
321 over the slice and false when hover has moved away from the slice.
321 over the slice and false when hover has moved away from the slice.
322 */
322 */
323
323
324 /*!
324 /*!
325 \qmlmethod PieSlice PieSeries::at(int index)
325 \qmlmethod PieSlice PieSeries::at(int index)
326 Returns slice at \a index. Returns null if the index is not valid.
326 Returns slice at \a index. Returns null if the index is not valid.
327 */
327 */
328
328
329 /*!
329 /*!
330 \qmlmethod PieSlice PieSeries::find(string label)
330 \qmlmethod PieSlice PieSeries::find(string label)
331 Returns the first slice with \a label. Returns null if the index is not valid.
331 Returns the first slice with \a label. Returns null if the index is not valid.
332 */
332 */
333
333
334 /*!
334 /*!
335 \qmlmethod PieSlice PieSeries::append(string label, real value)
335 \qmlmethod PieSlice PieSeries::append(string label, real value)
336 Adds a new slice with \a label and \a value to the pie.
336 Adds a new slice with \a label and \a value to the pie.
337 */
337 */
338
338
339 /*!
339 /*!
340 \qmlmethod bool PieSeries::remove(PieSlice slice)
340 \qmlmethod bool PieSeries::remove(PieSlice slice)
341 Removes the \a slice from the pie. Returns true if the removal was successful, false otherwise.
341 Removes the \a slice from the pie. Returns true if the removal was successful, false otherwise.
342 */
342 */
343
343
344 /*!
344 /*!
345 \qmlmethod PieSeries::clear()
345 \qmlmethod PieSeries::clear()
346 Removes all slices from the pie.
346 Removes all slices from the pie.
347 */
347 */
348
348
349 /*!
349 /*!
350 Constructs a series object which is a child of \a parent.
350 Constructs a series object which is a child of \a parent.
351 */
351 */
352 QPieSeries::QPieSeries(QObject *parent)
352 QPieSeries::QPieSeries(QObject *parent)
353 : QAbstractSeries(*new QPieSeriesPrivate(this), parent)
353 : QAbstractSeries(*new QPieSeriesPrivate(this), parent)
354 {
354 {
355 Q_D(QPieSeries);
355 Q_D(QPieSeries);
356 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
356 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
357 }
357 }
358
358
359 /*!
359 /*!
360 Destroys the series and its slices.
360 Destroys the series and its slices.
361 */
361 */
362 QPieSeries::~QPieSeries()
362 QPieSeries::~QPieSeries()
363 {
363 {
364 // NOTE: d_prt destroyed by QObject
364 // NOTE: d_prt destroyed by QObject
365 }
365 }
366
366
367 /*!
367 /*!
368 Returns QChartSeries::SeriesTypePie.
368 Returns QChartSeries::SeriesTypePie.
369 */
369 */
370 QAbstractSeries::SeriesType QPieSeries::type() const
370 QAbstractSeries::SeriesType QPieSeries::type() const
371 {
371 {
372 return QAbstractSeries::SeriesTypePie;
372 return QAbstractSeries::SeriesTypePie;
373 }
373 }
374
374
375 /*!
375 /*!
376 Appends a single \a slice to the series.
376 Appends a single \a slice to the series.
377 Slice ownership is passed to the series.
377 Slice ownership is passed to the series.
378
378
379 Returns true if append was succesfull.
379 Returns true if append was succesfull.
380 */
380 */
381 bool QPieSeries::append(QPieSlice *slice)
381 bool QPieSeries::append(QPieSlice *slice)
382 {
382 {
383 return append(QList<QPieSlice *>() << slice);
383 return append(QList<QPieSlice *>() << slice);
384 }
384 }
385
385
386 /*!
386 /*!
387 Appends an array of \a slices to the series.
387 Appends an array of \a slices to the series.
388 Slice ownership is passed to the series.
388 Slice ownership is passed to the series.
389
389
390 Returns true if append was successful.
390 Returns true if append was successful.
391 */
391 */
392 bool QPieSeries::append(QList<QPieSlice *> slices)
392 bool QPieSeries::append(QList<QPieSlice *> slices)
393 {
393 {
394 Q_D(QPieSeries);
394 Q_D(QPieSeries);
395
395
396 if (slices.count() == 0)
396 if (slices.count() == 0)
397 return false;
397 return false;
398
398
399 foreach (QPieSlice *s, slices) {
399 foreach (QPieSlice *s, slices) {
400 if (!s || d->m_slices.contains(s))
400 if (!s || d->m_slices.contains(s))
401 return false;
401 return false;
402 if (s->series()) // already added to some series
402 if (s->series()) // already added to some series
403 return false;
403 return false;
404 }
404 }
405
405
406 foreach (QPieSlice *s, slices) {
406 foreach (QPieSlice *s, slices) {
407 s->setParent(this);
407 s->setParent(this);
408 QPieSlicePrivate::fromSlice(s)->m_series = this;
408 QPieSlicePrivate::fromSlice(s)->m_series = this;
409 d->m_slices << s;
409 d->m_slices << s;
410 }
410 }
411
411
412 d->updateDerivativeData();
412 d->updateDerivativeData();
413
413
414 foreach(QPieSlice * s, slices) {
414 foreach(QPieSlice * s, slices) {
415 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
415 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
416 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
416 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
417 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
417 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
418 }
418 }
419
419
420 emit added(slices);
420 emit added(slices);
421 emit countChanged();
421 emit countChanged();
422
422
423 return true;
423 return true;
424 }
424 }
425
425
426 /*!
426 /*!
427 Appends a single \a slice to the series and returns a reference to the series.
427 Appends a single \a slice to the series and returns a reference to the series.
428 Slice ownership is passed to the series.
428 Slice ownership is passed to the series.
429 */
429 */
430 QPieSeries &QPieSeries::operator << (QPieSlice *slice)
430 QPieSeries &QPieSeries::operator << (QPieSlice *slice)
431 {
431 {
432 append(slice);
432 append(slice);
433 return *this;
433 return *this;
434 }
434 }
435
435
436
436
437 /*!
437 /*!
438 Appends a single slice to the series with give \a value and \a label.
438 Appends a single slice to the series with give \a value and \a label.
439 Slice ownership is passed to the series.
439 Slice ownership is passed to the series.
440 */
440 */
441 QPieSlice *QPieSeries::append(QString label, qreal value)
441 QPieSlice *QPieSeries::append(QString label, qreal value)
442 {
442 {
443 QPieSlice *slice = new QPieSlice(label, value);
443 QPieSlice *slice = new QPieSlice(label, value);
444 append(slice);
444 append(slice);
445 return slice;
445 return slice;
446 }
446 }
447
447
448 /*!
448 /*!
449 Inserts a single \a slice to the series before the slice at \a index position.
449 Inserts a single \a slice to the series before the slice at \a index position.
450 Slice ownership is passed to the series.
450 Slice ownership is passed to the series.
451
451
452 Returns true if insert was successful.
452 Returns true if insert was successful.
453 */
453 */
454 bool QPieSeries::insert(int index, QPieSlice *slice)
454 bool QPieSeries::insert(int index, QPieSlice *slice)
455 {
455 {
456 Q_D(QPieSeries);
456 Q_D(QPieSeries);
457
457
458 if (index < 0 || index > d->m_slices.count())
458 if (index < 0 || index > d->m_slices.count())
459 return false;
459 return false;
460
460
461 if (!slice || d->m_slices.contains(slice))
461 if (!slice || d->m_slices.contains(slice))
462 return false;
462 return false;
463
463
464 if (slice->series()) // already added to some series
464 if (slice->series()) // already added to some series
465 return false;
465 return false;
466
466
467 slice->setParent(this);
467 slice->setParent(this);
468 QPieSlicePrivate::fromSlice(slice)->m_series = this;
468 QPieSlicePrivate::fromSlice(slice)->m_series = this;
469 d->m_slices.insert(index, slice);
469 d->m_slices.insert(index, slice);
470
470
471 d->updateDerivativeData();
471 d->updateDerivativeData();
472
472
473 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
473 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
474 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
474 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
475 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
475 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
476
476
477
477
478 emit added(QList<QPieSlice *>() << slice);
478 emit added(QList<QPieSlice *>() << slice);
479 emit countChanged();
479 emit countChanged();
480
480
481 return true;
481 return true;
482 }
482 }
483
483
484 /*!
484 /*!
485 Removes a single \a slice from the series and deletes the slice.
485 Removes a single \a slice from the series and deletes the slice.
486
486
487 Do not reference the pointer after this call.
487 Do not reference the pointer after this call.
488
488
489 Returns true if remove was successful.
489 Returns true if remove was successful.
490 */
490 */
491 bool QPieSeries::remove(QPieSlice *slice)
491 bool QPieSeries::remove(QPieSlice *slice)
492 {
492 {
493 Q_D(QPieSeries);
493 Q_D(QPieSeries);
494
494
495 if (!d->m_slices.removeOne(slice))
495 if (!d->m_slices.removeOne(slice))
496 return false;
496 return false;
497
497
498 d->updateDerivativeData();
498 d->updateDerivativeData();
499
499
500 emit removed(QList<QPieSlice *>() << slice);
500 emit removed(QList<QPieSlice *>() << slice);
501 emit countChanged();
501 emit countChanged();
502
502
503 delete slice;
503 delete slice;
504 slice = 0;
504 slice = 0;
505
505
506 return true;
506 return true;
507 }
507 }
508
508
509 /*!
509 /*!
510 Takes a single \a slice from the series. Does not destroy the slice object.
510 Takes a single \a slice from the series. Does not destroy the slice object.
511
511
512 NOTE: The series remains as the slice's parent object. You must set the
512 NOTE: The series remains as the slice's parent object. You must set the
513 parent object to take full ownership.
513 parent object to take full ownership.
514
514
515 Returns true if take was successful.
515 Returns true if take was successful.
516 */
516 */
517 bool QPieSeries::take(QPieSlice *slice)
517 bool QPieSeries::take(QPieSlice *slice)
518 {
518 {
519 Q_D(QPieSeries);
519 Q_D(QPieSeries);
520
520
521 if (!d->m_slices.removeOne(slice))
521 if (!d->m_slices.removeOne(slice))
522 return false;
522 return false;
523
523
524 QPieSlicePrivate::fromSlice(slice)->m_series = 0;
524 QPieSlicePrivate::fromSlice(slice)->m_series = 0;
525 slice->disconnect(d);
525 slice->disconnect(d);
526
526
527 d->updateDerivativeData();
527 d->updateDerivativeData();
528
528
529 emit removed(QList<QPieSlice *>() << slice);
529 emit removed(QList<QPieSlice *>() << slice);
530 emit countChanged();
530 emit countChanged();
531
531
532 return true;
532 return true;
533 }
533 }
534
534
535 /*!
535 /*!
536 Clears all slices from the series.
536 Clears all slices from the series.
537 */
537 */
538 void QPieSeries::clear()
538 void QPieSeries::clear()
539 {
539 {
540 Q_D(QPieSeries);
540 Q_D(QPieSeries);
541 if (d->m_slices.count() == 0)
541 if (d->m_slices.count() == 0)
542 return;
542 return;
543
543
544 QList<QPieSlice *> slices = d->m_slices;
544 QList<QPieSlice *> slices = d->m_slices;
545 foreach (QPieSlice *s, d->m_slices)
545 foreach (QPieSlice *s, d->m_slices)
546 d->m_slices.removeOne(s);
546 d->m_slices.removeOne(s);
547
547
548 d->updateDerivativeData();
548 d->updateDerivativeData();
549
549
550 emit removed(slices);
550 emit removed(slices);
551 emit countChanged();
551 emit countChanged();
552
552
553 foreach (QPieSlice *s, slices)
553 foreach (QPieSlice *s, slices)
554 delete s;
554 delete s;
555 }
555 }
556
556
557 /*!
557 /*!
558 Returns a list of slices that belong to this series.
558 Returns a list of slices that belong to this series.
559 */
559 */
560 QList<QPieSlice *> QPieSeries::slices() const
560 QList<QPieSlice *> QPieSeries::slices() const
561 {
561 {
562 Q_D(const QPieSeries);
562 Q_D(const QPieSeries);
563 return d->m_slices;
563 return d->m_slices;
564 }
564 }
565
565
566 /*!
566 /*!
567 returns the number of the slices in this series.
567 returns the number of the slices in this series.
568 */
568 */
569 int QPieSeries::count() const
569 int QPieSeries::count() const
570 {
570 {
571 Q_D(const QPieSeries);
571 Q_D(const QPieSeries);
572 return d->m_slices.count();
572 return d->m_slices.count();
573 }
573 }
574
574
575 /*!
575 /*!
576 Returns true is the series is empty.
576 Returns true is the series is empty.
577 */
577 */
578 bool QPieSeries::isEmpty() const
578 bool QPieSeries::isEmpty() const
579 {
579 {
580 Q_D(const QPieSeries);
580 Q_D(const QPieSeries);
581 return d->m_slices.isEmpty();
581 return d->m_slices.isEmpty();
582 }
582 }
583
583
584 /*!
584 /*!
585 Returns the sum of all slice values in this series.
585 Returns the sum of all slice values in this series.
586
586
587 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
587 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
588 */
588 */
589 qreal QPieSeries::sum() const
589 qreal QPieSeries::sum() const
590 {
590 {
591 Q_D(const QPieSeries);
591 Q_D(const QPieSeries);
592 return d->m_sum;
592 return d->m_sum;
593 }
593 }
594
594
595 void QPieSeries::setHoleSize(qreal holeSize)
595 void QPieSeries::setHoleSize(qreal holeSize)
596 {
596 {
597 Q_D(QPieSeries);
597 Q_D(QPieSeries);
598 holeSize = qBound((qreal)0.0, holeSize, (qreal)1.0);
598 holeSize = qBound((qreal)0.0, holeSize, (qreal)1.0);
599 d->setSizes(holeSize, qMax(d->m_pieRelativeSize, holeSize));
599 d->setSizes(holeSize, qMax(d->m_pieRelativeSize, holeSize));
600 }
600 }
601
601
602 qreal QPieSeries::holeSize() const
602 qreal QPieSeries::holeSize() const
603 {
603 {
604 Q_D(const QPieSeries);
604 Q_D(const QPieSeries);
605 return d->m_holeRelativeSize;
605 return d->m_holeRelativeSize;
606 }
606 }
607
607
608 void QPieSeries::setHorizontalPosition(qreal relativePosition)
608 void QPieSeries::setHorizontalPosition(qreal relativePosition)
609 {
609 {
610 Q_D(QPieSeries);
610 Q_D(QPieSeries);
611
611
612 if (relativePosition < 0.0)
612 if (relativePosition < 0.0)
613 relativePosition = 0.0;
613 relativePosition = 0.0;
614 if (relativePosition > 1.0)
614 if (relativePosition > 1.0)
615 relativePosition = 1.0;
615 relativePosition = 1.0;
616
616
617 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativePosition)) {
617 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativePosition)) {
618 d->m_pieRelativeHorPos = relativePosition;
618 d->m_pieRelativeHorPos = relativePosition;
619 emit d->horizontalPositionChanged();
619 emit d->horizontalPositionChanged();
620 }
620 }
621 }
621 }
622
622
623 qreal QPieSeries::horizontalPosition() const
623 qreal QPieSeries::horizontalPosition() const
624 {
624 {
625 Q_D(const QPieSeries);
625 Q_D(const QPieSeries);
626 return d->m_pieRelativeHorPos;
626 return d->m_pieRelativeHorPos;
627 }
627 }
628
628
629 void QPieSeries::setVerticalPosition(qreal relativePosition)
629 void QPieSeries::setVerticalPosition(qreal relativePosition)
630 {
630 {
631 Q_D(QPieSeries);
631 Q_D(QPieSeries);
632
632
633 if (relativePosition < 0.0)
633 if (relativePosition < 0.0)
634 relativePosition = 0.0;
634 relativePosition = 0.0;
635 if (relativePosition > 1.0)
635 if (relativePosition > 1.0)
636 relativePosition = 1.0;
636 relativePosition = 1.0;
637
637
638 if (!qFuzzyIsNull(d->m_pieRelativeVerPos - relativePosition)) {
638 if (!qFuzzyIsNull(d->m_pieRelativeVerPos - relativePosition)) {
639 d->m_pieRelativeVerPos = relativePosition;
639 d->m_pieRelativeVerPos = relativePosition;
640 emit d->verticalPositionChanged();
640 emit d->verticalPositionChanged();
641 }
641 }
642 }
642 }
643
643
644 qreal QPieSeries::verticalPosition() const
644 qreal QPieSeries::verticalPosition() const
645 {
645 {
646 Q_D(const QPieSeries);
646 Q_D(const QPieSeries);
647 return d->m_pieRelativeVerPos;
647 return d->m_pieRelativeVerPos;
648 }
648 }
649
649
650 void QPieSeries::setPieSize(qreal relativeSize)
650 void QPieSeries::setPieSize(qreal relativeSize)
651 {
651 {
652 Q_D(QPieSeries);
652 Q_D(QPieSeries);
653 relativeSize = qBound((qreal)0.0, relativeSize, (qreal)1.0);
653 relativeSize = qBound((qreal)0.0, relativeSize, (qreal)1.0);
654 d->setSizes(qMin(d->m_holeRelativeSize, relativeSize), relativeSize);
654 d->setSizes(qMin(d->m_holeRelativeSize, relativeSize), relativeSize);
655
655
656 }
656 }
657
657
658 qreal QPieSeries::pieSize() const
658 qreal QPieSeries::pieSize() const
659 {
659 {
660 Q_D(const QPieSeries);
660 Q_D(const QPieSeries);
661 return d->m_pieRelativeSize;
661 return d->m_pieRelativeSize;
662 }
662 }
663
663
664
664
665 void QPieSeries::setPieStartAngle(qreal angle)
665 void QPieSeries::setPieStartAngle(qreal angle)
666 {
666 {
667 Q_D(QPieSeries);
667 Q_D(QPieSeries);
668 if (qFuzzyIsNull(d->m_pieStartAngle - angle))
668 if (qFuzzyIsNull(d->m_pieStartAngle - angle))
669 return;
669 return;
670 d->m_pieStartAngle = angle;
670 d->m_pieStartAngle = angle;
671 d->updateDerivativeData();
671 d->updateDerivativeData();
672 emit d->pieStartAngleChanged();
672 emit d->pieStartAngleChanged();
673 }
673 }
674
674
675 qreal QPieSeries::pieStartAngle() const
675 qreal QPieSeries::pieStartAngle() const
676 {
676 {
677 Q_D(const QPieSeries);
677 Q_D(const QPieSeries);
678 return d->m_pieStartAngle;
678 return d->m_pieStartAngle;
679 }
679 }
680
680
681 /*!
681 /*!
682 Sets the end angle of the pie.
682 Sets the end angle of the pie.
683
683
684 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
684 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
685
685
686 \a angle must be greater than start angle.
686 \a angle must be greater than start angle.
687
687
688 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
688 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
689 */
689 */
690 void QPieSeries::setPieEndAngle(qreal angle)
690 void QPieSeries::setPieEndAngle(qreal angle)
691 {
691 {
692 Q_D(QPieSeries);
692 Q_D(QPieSeries);
693 if (qFuzzyIsNull(d->m_pieEndAngle - angle))
693 if (qFuzzyIsNull(d->m_pieEndAngle - angle))
694 return;
694 return;
695 d->m_pieEndAngle = angle;
695 d->m_pieEndAngle = angle;
696 d->updateDerivativeData();
696 d->updateDerivativeData();
697 emit d->pieEndAngleChanged();
697 emit d->pieEndAngleChanged();
698 }
698 }
699
699
700 /*!
700 /*!
701 Returns the end angle of the pie.
701 Returns the end angle of the pie.
702
702
703 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
703 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
704
704
705 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
705 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
706 */
706 */
707 qreal QPieSeries::pieEndAngle() const
707 qreal QPieSeries::pieEndAngle() const
708 {
708 {
709 Q_D(const QPieSeries);
709 Q_D(const QPieSeries);
710 return d->m_pieEndAngle;
710 return d->m_pieEndAngle;
711 }
711 }
712
712
713 /*!
713 /*!
714 Sets the all the slice labels \a visible or invisible.
714 Sets the all the slice labels \a visible or invisible.
715
715
716 Note that this affects only the current slices in the series.
716 Note that this affects only the current slices in the series.
717 If user adds a new slice the default label visibility is false.
717 If user adds a new slice the default label visibility is false.
718
718
719 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
719 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
720 */
720 */
721 void QPieSeries::setLabelsVisible(bool visible)
721 void QPieSeries::setLabelsVisible(bool visible)
722 {
722 {
723 Q_D(QPieSeries);
723 Q_D(QPieSeries);
724 foreach (QPieSlice *s, d->m_slices)
724 foreach (QPieSlice *s, d->m_slices)
725 s->setLabelVisible(visible);
725 s->setLabelVisible(visible);
726 }
726 }
727
727
728 /*!
728 /*!
729 Sets the all the slice labels \a position
729 Sets the all the slice labels \a position
730
730
731 Note that this affects only the current slices in the series.
731 Note that this affects only the current slices in the series.
732 If user adds a new slice the default label position is LabelOutside
732 If user adds a new slice the default label position is LabelOutside
733
733
734 \sa QPieSlice::labelPosition(), QPieSlice::setLabelPosition()
734 \sa QPieSlice::labelPosition(), QPieSlice::setLabelPosition()
735 */
735 */
736 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
736 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
737 {
737 {
738 Q_D(QPieSeries);
738 Q_D(QPieSeries);
739 foreach (QPieSlice *s, d->m_slices)
739 foreach (QPieSlice *s, d->m_slices)
740 s->setLabelPosition(position);
740 s->setLabelPosition(position);
741 }
741 }
742
742
743 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
743 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
744
744
745
745
746 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
746 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
747 QAbstractSeriesPrivate(parent),
747 QAbstractSeriesPrivate(parent),
748 m_pieRelativeHorPos(0.5),
748 m_pieRelativeHorPos(0.5),
749 m_pieRelativeVerPos(0.5),
749 m_pieRelativeVerPos(0.5),
750 m_pieRelativeSize(0.7),
750 m_pieRelativeSize(0.7),
751 m_pieStartAngle(0),
751 m_pieStartAngle(0),
752 m_pieEndAngle(360),
752 m_pieEndAngle(360),
753 m_sum(0),
753 m_sum(0),
754 m_holeRelativeSize(0.0)
754 m_holeRelativeSize(0.0)
755 {
755 {
756 }
756 }
757
757
758 QPieSeriesPrivate::~QPieSeriesPrivate()
758 QPieSeriesPrivate::~QPieSeriesPrivate()
759 {
759 {
760 }
760 }
761
761
762 void QPieSeriesPrivate::updateDerivativeData()
762 void QPieSeriesPrivate::updateDerivativeData()
763 {
763 {
764 // calculate sum of all slices
764 // calculate sum of all slices
765 qreal sum = 0;
765 qreal sum = 0;
766 foreach (QPieSlice *s, m_slices)
766 foreach (QPieSlice *s, m_slices)
767 sum += s->value();
767 sum += s->value();
768
768
769 if (!qFuzzyIsNull(m_sum - sum)) {
769 if (!qFuzzyIsNull(m_sum - sum)) {
770 m_sum = sum;
770 m_sum = sum;
771 emit q_func()->sumChanged();
771 emit q_func()->sumChanged();
772 }
772 }
773
773
774 // nothing to show..
774 // nothing to show..
775 if (qFuzzyIsNull(m_sum))
775 if (qFuzzyIsNull(m_sum))
776 return;
776 return;
777
777
778 // update slice attributes
778 // update slice attributes
779 qreal sliceAngle = m_pieStartAngle;
779 qreal sliceAngle = m_pieStartAngle;
780 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
780 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
781 QVector<QPieSlice *> changed;
781 QVector<QPieSlice *> changed;
782 foreach (QPieSlice *s, m_slices) {
782 foreach (QPieSlice *s, m_slices) {
783 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
783 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
784 d->setPercentage(s->value() / m_sum);
784 d->setPercentage(s->value() / m_sum);
785 d->setStartAngle(sliceAngle);
785 d->setStartAngle(sliceAngle);
786 d->setAngleSpan(pieSpan * s->percentage());
786 d->setAngleSpan(pieSpan * s->percentage());
787 sliceAngle += s->angleSpan();
787 sliceAngle += s->angleSpan();
788 }
788 }
789
789
790
790
791 emit calculatedDataChanged();
791 emit calculatedDataChanged();
792 }
792 }
793
793
794 void QPieSeriesPrivate::setSizes(qreal innerSize, qreal outerSize)
794 void QPieSeriesPrivate::setSizes(qreal innerSize, qreal outerSize)
795 {
795 {
796 bool changed = false;
796 bool changed = false;
797
797
798 if (!qFuzzyIsNull(m_holeRelativeSize - innerSize)) {
798 if (!qFuzzyIsNull(m_holeRelativeSize - innerSize)) {
799 m_holeRelativeSize = innerSize;
799 m_holeRelativeSize = innerSize;
800 changed = true;
800 changed = true;
801 }
801 }
802
802
803 if (!qFuzzyIsNull(m_pieRelativeSize - outerSize)) {
803 if (!qFuzzyIsNull(m_pieRelativeSize - outerSize)) {
804 m_pieRelativeSize = outerSize;
804 m_pieRelativeSize = outerSize;
805 changed = true;
805 changed = true;
806 }
806 }
807
807
808 if (changed)
808 if (changed)
809 emit pieSizeChanged();
809 emit pieSizeChanged();
810 }
810 }
811
811
812 QPieSeriesPrivate *QPieSeriesPrivate::fromSeries(QPieSeries *series)
812 QPieSeriesPrivate *QPieSeriesPrivate::fromSeries(QPieSeries *series)
813 {
813 {
814 return series->d_func();
814 return series->d_func();
815 }
815 }
816
816
817 void QPieSeriesPrivate::sliceValueChanged()
817 void QPieSeriesPrivate::sliceValueChanged()
818 {
818 {
819 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
819 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
820 updateDerivativeData();
820 updateDerivativeData();
821 }
821 }
822
822
823 void QPieSeriesPrivate::sliceClicked()
823 void QPieSeriesPrivate::sliceClicked()
824 {
824 {
825 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
825 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
826 Q_ASSERT(m_slices.contains(slice));
826 Q_ASSERT(m_slices.contains(slice));
827 Q_Q(QPieSeries);
827 Q_Q(QPieSeries);
828 emit q->clicked(slice);
828 emit q->clicked(slice);
829 }
829 }
830
830
831 void QPieSeriesPrivate::sliceHovered(bool state)
831 void QPieSeriesPrivate::sliceHovered(bool state)
832 {
832 {
833 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
833 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
834 Q_ASSERT(m_slices.contains(slice));
834 Q_ASSERT(m_slices.contains(slice));
835 Q_Q(QPieSeries);
835 Q_Q(QPieSeries);
836 emit q->hovered(slice, state);
836 emit q->hovered(slice, state);
837 }
837 }
838
838
839 void QPieSeriesPrivate::scaleDomain(Domain &domain)
839 void QPieSeriesPrivate::scaleDomain(Domain &domain)
840 {
840 {
841 Q_UNUSED(domain);
841 Q_UNUSED(domain);
842 // does not apply to pie
842 // does not apply to pie
843 }
843 }
844
844
845 ChartElement *QPieSeriesPrivate::createGraphics(ChartPresenter *presenter)
845 ChartElement *QPieSeriesPrivate::createGraphics(ChartPresenter *presenter)
846 {
846 {
847 Q_Q(QPieSeries);
847 Q_Q(QPieSeries);
848 PieChartItem *pie = new PieChartItem(q, presenter);
848 PieChartItem *pie = new PieChartItem(q, presenter);
849 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
849 if (presenter->animationOptions().testFlag(QChart::SeriesAnimations))
850 pie->setAnimation(new PieAnimation(pie));
850 pie->setAnimation(new PieAnimation(pie));
851 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
851 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
852 return pie;
852 return pie;
853 }
853 }
854
854
855 QList<LegendMarker *> QPieSeriesPrivate::createLegendMarker(QLegend *legend)
855 QList<LegendMarker *> QPieSeriesPrivate::createLegendMarker(QLegend *legend)
856 {
856 {
857 Q_Q(QPieSeries);
857 Q_Q(QPieSeries);
858 QList<LegendMarker *> markers;
858 QList<LegendMarker *> markers;
859 foreach (QPieSlice *slice, q->slices()) {
859 foreach (QPieSlice *slice, q->slices()) {
860 PieLegendMarker* marker = new PieLegendMarker(q, slice, legend);
860 PieLegendMarker* marker = new PieLegendMarker(q, slice, legend);
861 markers << marker;
861 markers << marker;
862 }
862 }
863 return markers;
863 return markers;
864 }
864 }
865
865
866 QList<QLegendMarker*> QPieSeriesPrivate::createLegendMarkers(QLegend* legend)
866 QList<QLegendMarker*> QPieSeriesPrivate::createLegendMarkers(QLegend* legend)
867 {
867 {
868 Q_UNUSED(legend);
868 // Q_UNUSED(legend);
869 Q_Q(QPieSeries);
869 Q_Q(QPieSeries);
870 QList<QLegendMarker*> markers;
870 QList<QLegendMarker*> markers;
871 foreach(QPieSlice* slice, q->slices()) {
871 foreach(QPieSlice* slice, q->slices()) {
872 QPieLegendMarker* marker = new QPieLegendMarker(q,slice);
872 QPieLegendMarker* marker = new QPieLegendMarker(q,slice,legend);
873 markers << marker;
873 markers << marker;
874 }
874 }
875 return markers;
875 return markers;
876 }
876 }
877
877
878 void QPieSeriesPrivate::initializeAxis(QAbstractAxis *axis)
878 void QPieSeriesPrivate::initializeAxis(QAbstractAxis *axis)
879 {
879 {
880 Q_UNUSED(axis);
880 Q_UNUSED(axis);
881 }
881 }
882
882
883 QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
883 QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
884 {
884 {
885 Q_UNUSED(orientation);
885 Q_UNUSED(orientation);
886 return QAbstractAxis::AxisTypeNoAxis;
886 return QAbstractAxis::AxisTypeNoAxis;
887 }
887 }
888
888
889 #include "moc_qpieseries.cpp"
889 #include "moc_qpieseries.cpp"
890 #include "moc_qpieseries_p.cpp"
890 #include "moc_qpieseries_p.cpp"
891
891
892 QTCOMMERCIALCHART_END_NAMESPACE
892 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now