@@ -1,195 +1,198 | |||||
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 "legendmarker_p.h" |
|
21 | #include "legendmarker_p.h" | |
22 | #include "qxyseries.h" |
|
22 | #include "qxyseries.h" | |
23 | #include "qxyseries_p.h" |
|
23 | #include "qxyseries_p.h" | |
24 | #include "qlegend.h" |
|
24 | #include "qlegend.h" | |
25 | #include "qbarseries.h" |
|
25 | #include "qbarseries.h" | |
26 | #include "qpieseries.h" |
|
26 | #include "qpieseries.h" | |
27 | #include "qpieslice.h" |
|
27 | #include "qpieslice.h" | |
28 | #include "qbarset.h" |
|
28 | #include "qbarset.h" | |
29 | #include "qbarset_p.h" |
|
29 | #include "qbarset_p.h" | |
30 | #include "qareaseries.h" |
|
30 | #include "qareaseries.h" | |
31 | #include "qareaseries_p.h" |
|
31 | #include "qareaseries_p.h" | |
32 | #include <QPainter> |
|
32 | #include <QPainter> | |
33 | #include <QGraphicsSceneEvent> |
|
33 | #include <QGraphicsSceneEvent> | |
34 | #include <QGraphicsSimpleTextItem> |
|
34 | #include <QGraphicsSimpleTextItem> | |
35 | #include <QDebug> |
|
35 | #include <QDebug> | |
36 |
|
36 | |||
37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
38 |
|
38 | |||
39 |
LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : |
|
39 | LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : | |
40 | m_series(series), |
|
40 | QGraphicsObject(legend), | |
41 | m_markerRect(0,0,10.0,10.0), |
|
41 | m_series(series), | |
42 | m_boundingRect(0,0,0,0), |
|
42 | m_markerRect(0,0,10.0,10.0), | |
43 | m_legend(legend), |
|
43 | m_boundingRect(0,0,0,0), | |
44 | m_textItem(new QGraphicsSimpleTextItem(this)), |
|
44 | m_legend(legend), | |
45 |
|
|
45 | m_textItem(new QGraphicsSimpleTextItem(this)), | |
|
46 | m_rectItem(new QGraphicsRectItem(this)) | |||
46 | { |
|
47 | { | |
47 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
48 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | |
48 | m_rectItem->setRect(m_markerRect); |
|
49 | m_rectItem->setRect(m_markerRect); | |
49 | updateLayout(); |
|
50 | updateLayout(); | |
50 | } |
|
51 | } | |
51 |
|
52 | |||
52 | void LegendMarker::setPen(const QPen &pen) |
|
53 | void LegendMarker::setPen(const QPen &pen) | |
53 | { |
|
54 | { | |
54 | m_textItem->setPen(pen); |
|
55 | m_textItem->setPen(pen); | |
55 | updateLayout(); |
|
56 | updateLayout(); | |
56 | } |
|
57 | } | |
57 |
|
58 | |||
58 | QPen LegendMarker::pen() const |
|
59 | QPen LegendMarker::pen() const | |
59 | { |
|
60 | { | |
60 | return m_textItem->pen(); |
|
61 | return m_textItem->pen(); | |
61 | } |
|
62 | } | |
62 |
|
63 | |||
63 | void LegendMarker::setBrush(const QBrush &brush) |
|
64 | void LegendMarker::setBrush(const QBrush &brush) | |
64 | { |
|
65 | { | |
65 | m_rectItem->setBrush(brush); |
|
66 | m_rectItem->setBrush(brush); | |
66 | } |
|
67 | } | |
67 |
|
68 | |||
68 | QBrush LegendMarker::brush() const |
|
69 | QBrush LegendMarker::brush() const | |
69 | { |
|
70 | { | |
70 | return m_rectItem->brush(); |
|
71 | return m_rectItem->brush(); | |
71 | } |
|
72 | } | |
72 |
|
73 | |||
73 | void LegendMarker::setLabel(const QString name) |
|
74 | void LegendMarker::setLabel(const QString name) | |
74 | { |
|
75 | { | |
75 | m_textItem->setText(name); |
|
76 | m_textItem->setText(name); | |
76 | updateLayout(); |
|
77 | updateLayout(); | |
77 | } |
|
78 | } | |
78 |
|
79 | |||
79 | void LegendMarker::setSize(const QSize& size) |
|
80 | void LegendMarker::setSize(const QSize& size) | |
80 | { |
|
81 | { | |
81 | m_markerRect = QRectF(0,0,size.width(),size.height()); |
|
82 | m_markerRect = QRectF(0,0,size.width(),size.height()); | |
82 | } |
|
83 | } | |
83 |
|
84 | |||
84 | QString LegendMarker::label() const |
|
85 | QString LegendMarker::label() const | |
85 | { |
|
86 | { | |
86 | return m_textItem->text(); |
|
87 | return m_textItem->text(); | |
87 | } |
|
88 | } | |
88 |
|
89 | |||
89 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
90 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
90 | { |
|
91 | { | |
91 | Q_UNUSED(option) |
|
92 | Q_UNUSED(option) | |
92 | Q_UNUSED(widget) |
|
93 | Q_UNUSED(widget) | |
93 | Q_UNUSED(painter) |
|
94 | Q_UNUSED(painter) | |
94 | } |
|
95 | } | |
95 |
|
96 | |||
96 | QRectF LegendMarker::boundingRect() const |
|
97 | QRectF LegendMarker::boundingRect() const | |
97 | { |
|
98 | { | |
98 | return m_boundingRect; |
|
99 | return m_boundingRect; | |
99 | } |
|
100 | } | |
100 |
|
101 | |||
101 | void LegendMarker::updateLayout() |
|
102 | void LegendMarker::updateLayout() | |
102 | { |
|
103 | { | |
103 |
|
104 | |||
104 | static const qreal margin = 2; |
|
105 | static const qreal margin = 2; | |
105 | static const qreal space = 4; |
|
106 | static const qreal space = 4; | |
106 |
|
107 | |||
107 | const QRectF& textRect = m_textItem->boundingRect(); |
|
108 | const QRectF& textRect = m_textItem->boundingRect(); | |
108 | prepareGeometryChange(); |
|
109 | prepareGeometryChange(); | |
109 | m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin)); |
|
110 | m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin)); | |
110 | m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2); |
|
111 | m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2); | |
111 | m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2); |
|
112 | m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2); | |
112 |
|
113 | |||
113 | } |
|
114 | } | |
114 |
|
115 | |||
115 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
116 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
116 | { |
|
117 | { | |
117 | QGraphicsObject::mousePressEvent(event); |
|
118 | QGraphicsObject::mousePressEvent(event); | |
118 | qDebug()<<"Not implemented"; //TODO: selected signal removed for now |
|
119 | qDebug()<<"Not implemented"; //TODO: selected signal removed for now | |
119 | } |
|
120 | } | |
120 |
|
121 | |||
121 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
122 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
122 |
|
123 | |||
123 | AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), |
|
124 | AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), | |
124 | m_series(series) |
|
125 | m_series(series) | |
125 | { |
|
126 | { | |
126 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
127 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | |
127 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
128 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | |
|
129 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | |||
128 | updated(); |
|
130 | updated(); | |
129 | } |
|
131 | } | |
130 |
|
132 | |||
131 | void AreaLegendMarker::updated() |
|
133 | void AreaLegendMarker::updated() | |
132 | { |
|
134 | { | |
133 | setBrush(m_series->brush()); |
|
135 | setBrush(m_series->brush()); | |
134 | setLabel(m_series->name()); |
|
136 | setLabel(m_series->name()); | |
135 | } |
|
137 | } | |
136 |
|
138 | |||
137 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
139 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
138 |
|
140 | |||
139 | BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), |
|
141 | BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), | |
140 | m_barset(barset) |
|
142 | m_barset(barset) | |
141 | { |
|
143 | { | |
142 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); |
|
144 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); | |
143 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); |
|
145 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); | |
144 | updated(); |
|
146 | updated(); | |
145 | } |
|
147 | } | |
146 |
|
148 | |||
147 | void BarLegendMarker::updated() |
|
149 | void BarLegendMarker::updated() | |
148 | { |
|
150 | { | |
149 | setBrush(m_barset->brush()); |
|
151 | setBrush(m_barset->brush()); | |
150 | setLabel(m_barset->name()); |
|
152 | setLabel(m_barset->name()); | |
151 | } |
|
153 | } | |
152 |
|
154 | |||
153 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
155 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
154 |
|
156 | |||
155 | PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), |
|
157 | PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), | |
156 | m_pieslice(pieslice) |
|
158 | m_pieslice(pieslice) | |
157 | { |
|
159 | { | |
158 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); |
|
160 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); | |
159 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); |
|
161 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); | |
160 | updated(); |
|
162 | updated(); | |
161 | } |
|
163 | } | |
162 |
|
164 | |||
163 | void PieLegendMarker::updated() |
|
165 | void PieLegendMarker::updated() | |
164 | { |
|
166 | { | |
165 | setBrush(m_pieslice->brush()); |
|
167 | setBrush(m_pieslice->brush()); | |
166 | setLabel(m_pieslice->label()); |
|
168 | setLabel(m_pieslice->label()); | |
167 | } |
|
169 | } | |
168 |
|
170 | |||
169 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
171 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
170 |
|
172 | |||
171 | XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), |
|
173 | XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), | |
172 | m_series(series) |
|
174 | m_series(series) | |
173 | { |
|
175 | { | |
174 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
176 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | |
175 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
177 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | |
|
178 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | |||
176 | updated(); |
|
179 | updated(); | |
177 | } |
|
180 | } | |
178 |
|
181 | |||
179 | void XYLegendMarker::updated() |
|
182 | void XYLegendMarker::updated() | |
180 | { |
|
183 | { | |
181 | setLabel(m_series->name()); |
|
184 | setLabel(m_series->name()); | |
182 |
|
185 | |||
183 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) |
|
186 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) | |
184 | { |
|
187 | { | |
185 | setBrush(m_series->brush()); |
|
188 | setBrush(m_series->brush()); | |
186 |
|
189 | |||
187 | } |
|
190 | } | |
188 | else { |
|
191 | else { | |
189 | setBrush(QBrush(m_series->pen().color())); |
|
192 | setBrush(QBrush(m_series->pen().color())); | |
190 | } |
|
193 | } | |
191 | } |
|
194 | } | |
192 |
|
195 | |||
193 | #include "moc_legendmarker_p.cpp" |
|
196 | #include "moc_legendmarker_p.cpp" | |
194 |
|
197 | |||
195 | QTCOMMERCIALCHART_END_NAMESPACE |
|
198 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,478 +1,476 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "qlegend.h" |
|
21 | #include "qlegend.h" | |
22 | #include "qlegend_p.h" |
|
22 | #include "qlegend_p.h" | |
23 | #include "qabstractseries.h" |
|
23 | #include "qabstractseries.h" | |
24 | #include "qabstractseries_p.h" |
|
24 | #include "qabstractseries_p.h" | |
25 | #include "qchart_p.h" |
|
25 | #include "qchart_p.h" | |
26 |
|
26 | |||
27 | #include "legendmarker_p.h" |
|
27 | #include "legendmarker_p.h" | |
28 | #include "qxyseries.h" |
|
28 | #include "qxyseries.h" | |
29 | #include "qlineseries.h" |
|
29 | #include "qlineseries.h" | |
30 | #include "qareaseries.h" |
|
30 | #include "qareaseries.h" | |
31 | #include "qscatterseries.h" |
|
31 | #include "qscatterseries.h" | |
32 | #include "qsplineseries.h" |
|
32 | #include "qsplineseries.h" | |
33 | #include "qbarseries.h" |
|
33 | #include "qbarseries.h" | |
34 | #include "qstackedbarseries.h" |
|
34 | #include "qstackedbarseries.h" | |
35 | #include "qpercentbarseries.h" |
|
35 | #include "qpercentbarseries.h" | |
36 | #include "qbarset.h" |
|
36 | #include "qbarset.h" | |
37 | #include "qpieseries.h" |
|
37 | #include "qpieseries.h" | |
38 | #include "qpieseries_p.h" |
|
38 | #include "qpieseries_p.h" | |
39 | #include "qpieslice.h" |
|
39 | #include "qpieslice.h" | |
40 | #include "chartpresenter_p.h" |
|
40 | #include "chartpresenter_p.h" | |
41 | #include <QPainter> |
|
41 | #include <QPainter> | |
42 | #include <QPen> |
|
42 | #include <QPen> | |
43 | #include <QTimer> |
|
43 | #include <QTimer> | |
44 |
|
44 | |||
45 | #include <QGraphicsSceneEvent> |
|
45 | #include <QGraphicsSceneEvent> | |
46 |
|
46 | |||
47 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
47 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
48 |
|
48 | |||
49 | /*! |
|
49 | /*! | |
50 | \class QLegend |
|
50 | \class QLegend | |
51 | \brief part of QtCommercial chart API. |
|
51 | \brief part of QtCommercial chart API. | |
52 | \mainclass |
|
52 | \mainclass | |
53 |
|
53 | |||
54 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when |
|
54 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when | |
55 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and |
|
55 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and | |
56 | handle the drawing manually. |
|
56 | handle the drawing manually. | |
57 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. |
|
57 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. | |
58 |
|
58 | |||
59 | \image examples_percentbarchart_legend.png |
|
59 | \image examples_percentbarchart_legend.png | |
60 |
|
60 | |||
61 | \sa QChart |
|
61 | \sa QChart | |
62 | */ |
|
62 | */ | |
63 |
|
63 | |||
64 | /*! |
|
64 | /*! | |
65 | \enum QLegend::Alignment |
|
65 | \enum QLegend::Alignment | |
66 |
|
66 | |||
67 | This enum describes the possible position for legend inside chart. |
|
67 | This enum describes the possible position for legend inside chart. | |
68 |
|
68 | |||
69 | \value AlignmentTop |
|
69 | \value AlignmentTop | |
70 | \value AlignmentBottom |
|
70 | \value AlignmentBottom | |
71 | \value AlignmentLeft |
|
71 | \value AlignmentLeft | |
72 | \value AlignmentRight |
|
72 | \value AlignmentRight | |
73 | */ |
|
73 | */ | |
74 |
|
74 | |||
75 | /*! |
|
75 | /*! | |
76 | \fn qreal QLegend::minWidth() const |
|
76 | \fn qreal QLegend::minWidth() const | |
77 | Returns minimum width of the legend |
|
77 | Returns minimum width of the legend | |
78 | */ |
|
78 | */ | |
79 |
|
79 | |||
80 | /*! |
|
80 | /*! | |
81 | \fn qreal QLegend::minHeight() const |
|
81 | \fn qreal QLegend::minHeight() const | |
82 | Returns minimum height of the legend |
|
82 | Returns minimum height of the legend | |
83 | */ |
|
83 | */ | |
84 |
|
84 | |||
85 | /*! |
|
85 | /*! | |
86 | Constructs the legend object and sets the parent to \a parent |
|
86 | Constructs the legend object and sets the parent to \a parent | |
87 | */ |
|
87 | */ | |
88 |
|
88 | |||
89 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), |
|
89 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), | |
90 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) |
|
90 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) | |
91 | { |
|
91 | { | |
92 | setZValue(ChartPresenter::LegendZValue); |
|
92 | setZValue(ChartPresenter::LegendZValue); | |
93 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
93 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | |
94 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); |
|
94 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); | |
95 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); |
|
95 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); | |
96 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); |
|
96 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | /*! |
|
99 | /*! | |
100 | Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. |
|
100 | Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. | |
101 | */ |
|
101 | */ | |
102 | QLegend::~QLegend() |
|
102 | QLegend::~QLegend() | |
103 | { |
|
103 | { | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | /*! |
|
106 | /*! | |
107 | Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. |
|
107 | Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. | |
108 | */ |
|
108 | */ | |
109 |
|
109 | |||
110 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
110 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
111 | { |
|
111 | { | |
112 | Q_UNUSED(option) |
|
112 | Q_UNUSED(option) | |
113 | Q_UNUSED(widget) |
|
113 | Q_UNUSED(widget) | |
114 | if(!d_ptr->m_backgroundVisible) return; |
|
114 | if(!d_ptr->m_backgroundVisible) return; | |
115 |
|
115 | |||
116 | painter->setOpacity(opacity()); |
|
116 | painter->setOpacity(opacity()); | |
117 | painter->setPen(d_ptr->m_pen); |
|
117 | painter->setPen(d_ptr->m_pen); | |
118 | painter->setBrush(d_ptr->m_brush); |
|
118 | painter->setBrush(d_ptr->m_brush); | |
119 | painter->drawRect(boundingRect()); |
|
119 | painter->drawRect(boundingRect()); | |
120 | } |
|
120 | } | |
121 |
|
121 | |||
122 | /*! |
|
122 | /*! | |
123 | Bounding rect of legend. |
|
123 | Bounding rect of legend. | |
124 | */ |
|
124 | */ | |
125 |
|
125 | |||
126 | QRectF QLegend::boundingRect() const |
|
126 | QRectF QLegend::boundingRect() const | |
127 | { |
|
127 | { | |
128 | return d_ptr->m_rect; |
|
128 | return d_ptr->m_rect; | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | /*! |
|
131 | /*! | |
132 | Sets the \a brush of legend. Brush affects the background of legend. |
|
132 | Sets the \a brush of legend. Brush affects the background of legend. | |
133 | */ |
|
133 | */ | |
134 | void QLegend::setBrush(const QBrush &brush) |
|
134 | void QLegend::setBrush(const QBrush &brush) | |
135 | { |
|
135 | { | |
136 | if (d_ptr->m_brush != brush) { |
|
136 | if (d_ptr->m_brush != brush) { | |
137 | d_ptr->m_brush = brush; |
|
137 | d_ptr->m_brush = brush; | |
138 | update(); |
|
138 | update(); | |
139 | } |
|
139 | } | |
140 | } |
|
140 | } | |
141 |
|
141 | |||
142 | /*! |
|
142 | /*! | |
143 | Returns the brush used by legend. |
|
143 | Returns the brush used by legend. | |
144 | */ |
|
144 | */ | |
145 | QBrush QLegend::brush() const |
|
145 | QBrush QLegend::brush() const | |
146 | { |
|
146 | { | |
147 | return d_ptr->m_brush; |
|
147 | return d_ptr->m_brush; | |
148 | } |
|
148 | } | |
149 |
|
149 | |||
150 | /*! |
|
150 | /*! | |
151 | Sets the \a pen of legend. Pen affects the legend borders. |
|
151 | Sets the \a pen of legend. Pen affects the legend borders. | |
152 | */ |
|
152 | */ | |
153 | void QLegend::setPen(const QPen &pen) |
|
153 | void QLegend::setPen(const QPen &pen) | |
154 | { |
|
154 | { | |
155 | if (d_ptr->m_pen != pen) { |
|
155 | if (d_ptr->m_pen != pen) { | |
156 | d_ptr->m_pen = pen; |
|
156 | d_ptr->m_pen = pen; | |
157 | update(); |
|
157 | update(); | |
158 | } |
|
158 | } | |
159 | } |
|
159 | } | |
160 |
|
160 | |||
161 | /*! |
|
161 | /*! | |
162 | Returns the pen used by legend |
|
162 | Returns the pen used by legend | |
163 | */ |
|
163 | */ | |
164 |
|
164 | |||
165 | QPen QLegend::pen() const |
|
165 | QPen QLegend::pen() const | |
166 | { |
|
166 | { | |
167 | return d_ptr->m_pen; |
|
167 | return d_ptr->m_pen; | |
168 | } |
|
168 | } | |
169 |
|
169 | |||
170 | /*! |
|
170 | /*! | |
171 | Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart. |
|
171 | Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart. | |
172 | \sa QLegend::Alignment |
|
172 | \sa QLegend::Alignment | |
173 | */ |
|
173 | */ | |
174 | void QLegend::setAlignment(QLegend::Alignments alignment) |
|
174 | void QLegend::setAlignment(QLegend::Alignments alignment) | |
175 | { |
|
175 | { | |
176 | if(d_ptr->m_alignment!=alignment) { |
|
176 | if(d_ptr->m_alignment!=alignment) { | |
177 | d_ptr->m_alignment = alignment; |
|
177 | d_ptr->m_alignment = alignment; | |
178 | d_ptr->updateLayout(); |
|
178 | d_ptr->updateLayout(); | |
179 | } |
|
179 | } | |
180 | } |
|
180 | } | |
181 |
|
181 | |||
182 | /*! |
|
182 | /*! | |
183 | Returns the preferred layout for legend |
|
183 | Returns the preferred layout for legend | |
184 | */ |
|
184 | */ | |
185 | QLegend::Alignments QLegend::alignment() const |
|
185 | QLegend::Alignments QLegend::alignment() const | |
186 | { |
|
186 | { | |
187 | return d_ptr->m_alignment; |
|
187 | return d_ptr->m_alignment; | |
188 | } |
|
188 | } | |
189 |
|
189 | |||
190 | /*! |
|
190 | /*! | |
191 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
191 | Detaches the legend from chart. Chart won't change layout of the legend. | |
192 | */ |
|
192 | */ | |
193 | void QLegend::detachFromChart() |
|
193 | void QLegend::detachFromChart() | |
194 | { |
|
194 | { | |
195 | d_ptr->m_attachedToChart = false; |
|
195 | d_ptr->m_attachedToChart = false; | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
198 | /*! |
|
198 | /*! | |
199 | Attaches the legend to chart. Chart may change layout of the legend. |
|
199 | Attaches the legend to chart. Chart may change layout of the legend. | |
200 | */ |
|
200 | */ | |
201 | void QLegend::attachToChart() |
|
201 | void QLegend::attachToChart() | |
202 | { |
|
202 | { | |
203 | d_ptr->attachToChart(); |
|
203 | d_ptr->attachToChart(); | |
204 | } |
|
204 | } | |
205 |
|
205 | |||
206 | /*! |
|
206 | /*! | |
207 | Returns true, if legend is attached to chart. |
|
207 | Returns true, if legend is attached to chart. | |
208 | */ |
|
208 | */ | |
209 | bool QLegend::isAttachedToChart() |
|
209 | bool QLegend::isAttachedToChart() | |
210 | { |
|
210 | { | |
211 | return d_ptr->m_attachedToChart; |
|
211 | return d_ptr->m_attachedToChart; | |
212 | } |
|
212 | } | |
213 |
|
213 | |||
214 | /*! |
|
214 | /*! | |
215 | Sets the legend's scrolling offset to value defined by \a point. |
|
215 | Sets the legend's scrolling offset to value defined by \a point. | |
216 | */ |
|
216 | */ | |
217 | void QLegend::setOffset(const QPointF& point) |
|
217 | void QLegend::setOffset(const QPointF& point) | |
218 | { |
|
218 | { | |
219 | d_ptr->setOffset(point.x(),point.y()); |
|
219 | d_ptr->setOffset(point.x(),point.y()); | |
220 | } |
|
220 | } | |
221 |
|
221 | |||
222 | /*! |
|
222 | /*! | |
223 | Returns the legend's scrolling offset. |
|
223 | Returns the legend's scrolling offset. | |
224 | */ |
|
224 | */ | |
225 | QPointF QLegend::offset() const |
|
225 | QPointF QLegend::offset() const | |
226 | { |
|
226 | { | |
227 | return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY); |
|
227 | return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY); | |
228 | } |
|
228 | } | |
229 |
|
229 | |||
230 | /*! |
|
230 | /*! | |
231 | Sets the visibility of legend background to \a visible |
|
231 | Sets the visibility of legend background to \a visible | |
232 | */ |
|
232 | */ | |
233 | void QLegend::setBackgroundVisible(bool visible) |
|
233 | void QLegend::setBackgroundVisible(bool visible) | |
234 | { |
|
234 | { | |
235 | if(d_ptr->m_backgroundVisible!=visible) |
|
235 | if(d_ptr->m_backgroundVisible!=visible) | |
236 | { |
|
236 | { | |
237 | d_ptr->m_backgroundVisible=visible; |
|
237 | d_ptr->m_backgroundVisible=visible; | |
238 | update(); |
|
238 | update(); | |
239 | } |
|
239 | } | |
240 | } |
|
240 | } | |
241 |
|
241 | |||
242 | /*! |
|
242 | /*! | |
243 | Returns the visibility of legend background |
|
243 | Returns the visibility of legend background | |
244 | */ |
|
244 | */ | |
245 | bool QLegend::isBackgroundVisible() const |
|
245 | bool QLegend::isBackgroundVisible() const | |
246 | { |
|
246 | { | |
247 | return d_ptr->m_backgroundVisible; |
|
247 | return d_ptr->m_backgroundVisible; | |
248 | } |
|
248 | } | |
249 |
|
249 | |||
250 | /*! |
|
250 | /*! | |
251 | \internal \a event see QGraphicsWidget for details |
|
251 | \internal \a event see QGraphicsWidget for details | |
252 | */ |
|
252 | */ | |
253 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
253 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) | |
254 | { |
|
254 | { | |
255 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); |
|
255 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); | |
256 | QGraphicsWidget::resizeEvent(event); |
|
256 | QGraphicsWidget::resizeEvent(event); | |
257 | if(d_ptr->m_rect != rect) { |
|
257 | if(d_ptr->m_rect != rect) { | |
258 | d_ptr->m_rect = rect; |
|
258 | d_ptr->m_rect = rect; | |
259 | d_ptr->updateLayout(); |
|
259 | d_ptr->updateLayout(); | |
260 | } |
|
260 | } | |
261 | } |
|
261 | } | |
262 |
|
262 | |||
263 | /*! |
|
263 | /*! | |
264 | \internal \a event see QGraphicsWidget for details |
|
264 | \internal \a event see QGraphicsWidget for details | |
265 | */ |
|
265 | */ | |
266 | void QLegend::hideEvent(QHideEvent *event) |
|
266 | void QLegend::hideEvent(QHideEvent *event) | |
267 | { |
|
267 | { | |
268 | QGraphicsWidget::hideEvent(event); |
|
268 | QGraphicsWidget::hideEvent(event); | |
269 | setEnabled(false); |
|
269 | setEnabled(false); | |
270 | d_ptr->updateLayout(); |
|
270 | d_ptr->updateLayout(); | |
271 | } |
|
271 | } | |
272 |
|
272 | |||
273 | /*! |
|
273 | /*! | |
274 | \internal \a event see QGraphicsWidget for details |
|
274 | \internal \a event see QGraphicsWidget for details | |
275 | */ |
|
275 | */ | |
276 | void QLegend::showEvent(QShowEvent *event) |
|
276 | void QLegend::showEvent(QShowEvent *event) | |
277 | { |
|
277 | { | |
278 | QGraphicsWidget::showEvent(event); |
|
278 | QGraphicsWidget::showEvent(event); | |
279 | setEnabled(true); |
|
279 | setEnabled(true); | |
280 | d_ptr->updateLayout(); |
|
280 | d_ptr->updateLayout(); | |
281 | } |
|
281 | } | |
282 |
|
282 | |||
283 | qreal QLegend::minWidth() const |
|
283 | qreal QLegend::minWidth() const | |
284 | { |
|
284 | { | |
285 | return d_ptr->m_minWidth; |
|
285 | return d_ptr->m_minWidth; | |
286 | } |
|
286 | } | |
287 |
|
287 | |||
288 | qreal QLegend::minHeight() const |
|
288 | qreal QLegend::minHeight() const | |
289 | { |
|
289 | { | |
290 | return d_ptr->m_minHeight; |
|
290 | return d_ptr->m_minHeight; | |
291 | } |
|
291 | } | |
292 |
|
292 | |||
293 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
293 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
294 |
|
294 | |||
295 | QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): |
|
295 | QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): | |
296 | q_ptr(q), |
|
296 | q_ptr(q), | |
297 | m_presenter(presenter), |
|
297 | m_presenter(presenter), | |
298 | m_chart(chart), |
|
298 | m_chart(chart), | |
299 | m_markers(new QGraphicsItemGroup(q)), |
|
299 | m_markers(new QGraphicsItemGroup(q)), | |
300 | m_alignment(QLegend::AlignmentTop), |
|
300 | m_alignment(QLegend::AlignmentTop), | |
301 | m_offsetX(0), |
|
301 | m_offsetX(0), | |
302 | m_offsetY(0), |
|
302 | m_offsetY(0), | |
303 | m_minWidth(0), |
|
303 | m_minWidth(0), | |
304 | m_minHeight(0), |
|
304 | m_minHeight(0), | |
305 | m_width(0), |
|
305 | m_width(0), | |
306 | m_height(0), |
|
306 | m_height(0), | |
307 | m_attachedToChart(true), |
|
307 | m_attachedToChart(true), | |
308 | m_backgroundVisible(false) |
|
308 | m_backgroundVisible(false) | |
309 | { |
|
309 | { | |
310 |
|
310 | |||
311 | } |
|
311 | } | |
312 |
|
312 | |||
313 | QLegendPrivate::~QLegendPrivate() |
|
313 | QLegendPrivate::~QLegendPrivate() | |
314 | { |
|
314 | { | |
315 |
|
315 | |||
316 | } |
|
316 | } | |
317 |
|
317 | |||
318 | void QLegendPrivate::setOffset(qreal x, qreal y) |
|
318 | void QLegendPrivate::setOffset(qreal x, qreal y) | |
319 | { |
|
319 | { | |
320 |
|
320 | |||
321 | switch(m_alignment) { |
|
321 | switch(m_alignment) { | |
322 |
|
322 | |||
323 | case QLegend::AlignmentTop: |
|
323 | case QLegend::AlignmentTop: | |
324 | case QLegend::AlignmentBottom: { |
|
324 | case QLegend::AlignmentBottom: { | |
325 | if(m_width<=m_rect.width()) return; |
|
325 | if(m_width<=m_rect.width()) return; | |
326 |
|
326 | |||
327 | if (x != m_offsetX) { |
|
327 | if (x != m_offsetX) { | |
328 | m_offsetX = qBound(qreal(0), x, m_width - m_rect.width()); |
|
328 | m_offsetX = qBound(qreal(0), x, m_width - m_rect.width()); | |
329 | m_markers->setPos(-m_offsetX,m_rect.top()); |
|
329 | m_markers->setPos(-m_offsetX,m_rect.top()); | |
330 | } |
|
330 | } | |
331 | break; |
|
331 | break; | |
332 | } |
|
332 | } | |
333 | case QLegend::AlignmentLeft: |
|
333 | case QLegend::AlignmentLeft: | |
334 | case QLegend::AlignmentRight: { |
|
334 | case QLegend::AlignmentRight: { | |
335 |
|
335 | |||
336 | if(m_height<=m_rect.height()) return; |
|
336 | if(m_height<=m_rect.height()) return; | |
337 |
|
337 | |||
338 | if (y != m_offsetY) { |
|
338 | if (y != m_offsetY) { | |
339 | m_offsetY = qBound(qreal(0), y, m_height - m_rect.height()); |
|
339 | m_offsetY = qBound(qreal(0), y, m_height - m_rect.height()); | |
340 | m_markers->setPos(m_rect.left(),-m_offsetY); |
|
340 | m_markers->setPos(m_rect.left(),-m_offsetY); | |
341 | } |
|
341 | } | |
342 | break; |
|
342 | break; | |
343 | } |
|
343 | } | |
344 | } |
|
344 | } | |
345 | } |
|
345 | } | |
346 |
|
346 | |||
347 |
|
347 | |||
348 | void QLegendPrivate::updateLayout() |
|
348 | void QLegendPrivate::updateLayout() | |
349 | { |
|
349 | { | |
350 | m_offsetX=0; |
|
350 | m_offsetX=0; | |
351 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
351 | QList<QGraphicsItem *> items = m_markers->childItems(); | |
352 |
|
352 | |||
353 | if(items.isEmpty()) return; |
|
353 | if(items.isEmpty()) return; | |
354 |
|
354 | |||
355 | m_minWidth=0; |
|
355 | m_minWidth=0; | |
356 | m_minHeight=0; |
|
356 | m_minHeight=0; | |
357 |
|
357 | |||
358 | switch(m_alignment) { |
|
358 | switch(m_alignment) { | |
359 |
|
359 | |||
360 | case QLegend::AlignmentTop: |
|
360 | case QLegend::AlignmentTop: | |
361 | case QLegend::AlignmentBottom: { |
|
361 | case QLegend::AlignmentBottom: { | |
362 | QPointF point = m_rect.topLeft(); |
|
362 | QPointF point = m_rect.topLeft(); | |
363 | m_width = 0; |
|
363 | m_width = 0; | |
364 | foreach (QGraphicsItem *item, items) { |
|
364 | foreach (QGraphicsItem *item, items) { | |
365 | item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); |
|
365 | item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); | |
366 | const QRectF& rect = item->boundingRect(); |
|
366 | const QRectF& rect = item->boundingRect(); | |
367 | qreal w = rect.width(); |
|
367 | qreal w = rect.width(); | |
368 | m_minWidth=qMax(m_minWidth,w); |
|
368 | m_minWidth=qMax(m_minWidth,w); | |
369 | m_minHeight=qMax(m_minHeight,rect.height()); |
|
369 | m_minHeight=qMax(m_minHeight,rect.height()); | |
370 | m_width+=w; |
|
370 | m_width+=w; | |
371 | point.setX(point.x() + w); |
|
371 | point.setX(point.x() + w); | |
372 | } |
|
372 | } | |
373 | if(m_width<m_rect.width()) { |
|
373 | if(m_width<m_rect.width()) { | |
374 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); |
|
374 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); | |
375 | } |
|
375 | } | |
376 | else { |
|
376 | else { | |
377 | m_markers->setPos(m_rect.topLeft()); |
|
377 | m_markers->setPos(m_rect.topLeft()); | |
378 | } |
|
378 | } | |
379 | m_height=m_minHeight; |
|
379 | m_height=m_minHeight; | |
380 | } |
|
380 | } | |
381 | break; |
|
381 | break; | |
382 | case QLegend::AlignmentLeft: |
|
382 | case QLegend::AlignmentLeft: | |
383 | case QLegend::AlignmentRight: { |
|
383 | case QLegend::AlignmentRight: { | |
384 | QPointF point = m_rect.topLeft(); |
|
384 | QPointF point = m_rect.topLeft(); | |
385 | m_height = 0; |
|
385 | m_height = 0; | |
386 | foreach (QGraphicsItem *item, items) { |
|
386 | foreach (QGraphicsItem *item, items) { | |
387 | item->setPos(point); |
|
387 | item->setPos(point); | |
388 | const QRectF& rect = item->boundingRect(); |
|
388 | const QRectF& rect = item->boundingRect(); | |
389 | qreal h = rect.height(); |
|
389 | qreal h = rect.height(); | |
390 | m_minWidth=qMax(m_minWidth,rect.width()); |
|
390 | m_minWidth=qMax(m_minWidth,rect.width()); | |
391 | m_minHeight=qMax(m_minHeight,h); |
|
391 | m_minHeight=qMax(m_minHeight,h); | |
392 | m_height+=h; |
|
392 | m_height+=h; | |
393 | point.setY(point.y() + h); |
|
393 | point.setY(point.y() + h); | |
394 | } |
|
394 | } | |
395 | if(m_height<m_rect.height()) { |
|
395 | if(m_height<m_rect.height()) { | |
396 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); |
|
396 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); | |
397 | } |
|
397 | } | |
398 | else { |
|
398 | else { | |
399 | m_markers->setPos(m_rect.topLeft()); |
|
399 | m_markers->setPos(m_rect.topLeft()); | |
400 | } |
|
400 | } | |
401 | m_width=m_minWidth; |
|
401 | m_width=m_minWidth; | |
402 | } |
|
402 | } | |
403 | break; |
|
403 | break; | |
404 | } |
|
404 | } | |
405 |
|
405 | |||
406 | if (m_attachedToChart) { |
|
406 | if (m_attachedToChart) { | |
407 | m_presenter->updateLayout(); |
|
407 | m_presenter->updateLayout(); | |
408 | } |
|
408 | } | |
409 | } |
|
409 | } | |
410 |
|
410 | |||
411 | void QLegendPrivate::attachToChart() |
|
411 | void QLegendPrivate::attachToChart() | |
412 | { |
|
412 | { | |
413 | m_attachedToChart = true; |
|
413 | m_attachedToChart = true; | |
414 | q_ptr->setParent(m_chart); |
|
414 | q_ptr->setParent(m_chart); | |
415 | } |
|
415 | } | |
416 |
|
416 | |||
417 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) |
|
417 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) | |
418 | { |
|
418 | { | |
419 | Q_UNUSED(domain) |
|
419 | Q_UNUSED(domain) | |
420 |
|
420 | |||
421 | QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); |
|
421 | QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); | |
422 |
foreach(LegendMarker* marker |
|
422 | foreach(LegendMarker* marker, markers) | |
423 | m_markers->addToGroup(marker); |
|
423 | m_markers->addToGroup(marker); | |
424 |
|
424 | |||
425 | if(series->type() == QAbstractSeries::SeriesTypePie) |
|
425 | if(series->type() == QAbstractSeries::SeriesTypePie) { | |
426 | { |
|
|||
427 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
426 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |
428 | QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
427 | QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); | |
429 | QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
428 | QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); | |
430 | } |
|
429 | } | |
431 |
|
430 | |||
432 | updateLayout(); |
|
431 | updateLayout(); | |
433 | } |
|
432 | } | |
434 |
|
433 | |||
435 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) |
|
434 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) | |
436 | { |
|
435 | { | |
437 |
|
||||
438 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
436 | QList<QGraphicsItem *> items = m_markers->childItems(); | |
439 |
|
437 | |||
440 | foreach (QGraphicsItem *markers, items) { |
|
438 | foreach (QGraphicsItem *markers, items) { | |
441 | LegendMarker *marker = static_cast<LegendMarker*>(markers); |
|
439 | LegendMarker *marker = static_cast<LegendMarker*>(markers); | |
442 | if (marker->series() == series) { |
|
440 | if (marker->series() == series) { | |
443 | delete marker; |
|
441 | delete marker; | |
444 | } |
|
442 | } | |
445 | } |
|
443 | } | |
446 |
|
444 | |||
447 | if(series->type() == QAbstractSeries::SeriesTypePie) |
|
445 | if(series->type() == QAbstractSeries::SeriesTypePie) | |
448 | { |
|
446 | { | |
449 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
447 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |
450 | QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
448 | QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); | |
451 | QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
449 | QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); | |
452 | } |
|
450 | } | |
453 |
|
451 | |||
454 | updateLayout(); |
|
452 | updateLayout(); | |
455 | } |
|
453 | } | |
456 |
|
454 | |||
457 | void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series) |
|
455 | void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series) | |
458 | { |
|
456 | { | |
459 | // TODO: find out which markers are are added or removed. Update them |
|
457 | // TODO: find out which markers are are added or removed. Update them | |
460 | // TODO: better implementation |
|
458 | // TODO: better implementation | |
461 | handleSeriesRemoved(series); |
|
459 | handleSeriesRemoved(series); | |
462 | Domain domain; |
|
460 | Domain domain; | |
463 | handleSeriesAdded(series, &domain); |
|
461 | handleSeriesAdded(series, &domain); | |
464 | } |
|
462 | } | |
465 |
|
463 | |||
466 | void QLegendPrivate::handleUpdatePieSeries() |
|
464 | void QLegendPrivate::handleUpdatePieSeries() | |
467 | { |
|
465 | { | |
468 | //TODO: reimplement to be optimal |
|
466 | //TODO: reimplement to be optimal | |
469 | QPieSeries* series = qobject_cast<QPieSeries *> (sender()); |
|
467 | QPieSeries* series = qobject_cast<QPieSeries *> (sender()); | |
470 | Q_ASSERT(series); |
|
468 | Q_ASSERT(series); | |
471 | handleSeriesRemoved(series); |
|
469 | handleSeriesRemoved(series); | |
472 | handleSeriesAdded(series, 0); |
|
470 | handleSeriesAdded(series, 0); | |
473 | } |
|
471 | } | |
474 |
|
472 | |||
475 | #include "moc_qlegend.cpp" |
|
473 | #include "moc_qlegend.cpp" | |
476 | #include "moc_qlegend_p.cpp" |
|
474 | #include "moc_qlegend_p.cpp" | |
477 |
|
475 | |||
478 | QTCOMMERCIALCHART_END_NAMESPACE |
|
476 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,134 +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 "qabstractseries.h" |
|
21 | #include "qabstractseries.h" | |
22 | #include "qabstractseries_p.h" |
|
22 | #include "qabstractseries_p.h" | |
23 | #include "chartdataset_p.h" |
|
23 | #include "chartdataset_p.h" | |
24 |
|
24 | |||
25 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
25 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
26 |
|
26 | |||
27 | /*! |
|
27 | /*! | |
28 | \class QAbstractSeries |
|
28 | \class QAbstractSeries | |
29 | \brief Base class for all QtCommercial Chart series. |
|
29 | \brief Base class for all QtCommercial Chart series. | |
30 | \mainclass |
|
30 | \mainclass | |
31 |
|
31 | |||
32 | Usually you use the series type specific inherited classes instead of the base class. |
|
32 | Usually you use the series type specific inherited classes instead of the base class. | |
33 | \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries, |
|
33 | \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries, | |
34 | QPercentBarSeries, QPieSeries |
|
34 | QPercentBarSeries, QPieSeries | |
35 | */ |
|
35 | */ | |
36 |
|
36 | |||
37 | /*! |
|
37 | /*! | |
38 | \enum QAbstractSeries::SeriesType |
|
38 | \enum QAbstractSeries::SeriesType | |
39 |
|
39 | |||
40 | The type of the series object. |
|
40 | The type of the series object. | |
41 |
|
41 | |||
42 | \value SeriesTypeLine |
|
42 | \value SeriesTypeLine | |
43 | \value SeriesTypeArea |
|
43 | \value SeriesTypeArea | |
44 | \value SeriesTypeBar |
|
44 | \value SeriesTypeBar | |
45 | \value SeriesTypeStackedBar |
|
45 | \value SeriesTypeStackedBar | |
46 | \value SeriesTypePercentBar |
|
46 | \value SeriesTypePercentBar | |
47 | \value SeriesTypeGroupedBar |
|
47 | \value SeriesTypeGroupedBar | |
48 | \value SeriesTypePie |
|
48 | \value SeriesTypePie | |
49 | \value SeriesTypeScatter |
|
49 | \value SeriesTypeScatter | |
50 | \value SeriesTypeSpline |
|
50 | \value SeriesTypeSpline | |
51 | */ |
|
51 | */ | |
52 |
|
52 | |||
53 | /*! |
|
53 | /*! | |
54 | \fn QSeriesType QAbstractSeries::type() const |
|
54 | \fn QSeriesType QAbstractSeries::type() const | |
55 | \brief The type of the series. |
|
55 | \brief The type of the series. | |
56 | */ |
|
56 | */ | |
57 |
|
57 | |||
58 | /*! |
|
58 | /*! | |
59 | \property QAbstractSeries::name |
|
59 | \property QAbstractSeries::name | |
60 | \brief name of the series property |
|
60 | \brief name of the series property | |
61 | */ |
|
61 | */ | |
62 |
|
62 | |||
63 | /*! |
|
63 | /*! | |
64 | \fn void QAbstractSeries::setName(const QString& name) |
|
64 | \fn void QAbstractSeries::setName(const QString& name) | |
65 | \brief Sets a \a name for the series. |
|
65 | \brief Sets a \a name for the series. | |
66 |
|
66 | |||
67 | The name of a series is shown in the legend for QXYSeries. |
|
67 | The name of a series is shown in the legend for QXYSeries. | |
68 | \sa QChart::setTitle() |
|
68 | \sa QChart::setTitle() | |
69 | \sa QPieSlice::setLabel() |
|
69 | \sa QPieSlice::setLabel() | |
70 | \sa QBarSet::setName() |
|
70 | \sa QBarSet::setName() | |
71 | */ |
|
71 | */ | |
72 |
|
72 | |||
73 | /*! |
|
73 | /*! | |
74 | \internal |
|
74 | \internal | |
75 | \brief Constructs ChartSeries object with \a parent. |
|
75 | \brief Constructs ChartSeries object with \a parent. | |
76 | */ |
|
76 | */ | |
77 | QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) : |
|
77 | QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) : | |
78 | QObject(parent), |
|
78 | QObject(parent), | |
79 | d_ptr(&d) |
|
79 | d_ptr(&d) | |
80 | { |
|
80 | { | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | /*! |
|
83 | /*! | |
84 | \brief Virtual destructor for the chart series. |
|
84 | \brief Virtual destructor for the chart series. | |
85 | */ |
|
85 | */ | |
86 | QAbstractSeries::~QAbstractSeries() |
|
86 | QAbstractSeries::~QAbstractSeries() | |
87 | { |
|
87 | { | |
88 | if(d_ptr->m_dataset) qFatal("Still binded series detected !"); |
|
88 | if(d_ptr->m_dataset) qFatal("Still binded series detected !"); | |
89 | } |
|
89 | } | |
90 |
|
90 | |||
91 | void QAbstractSeries::setName(const QString& name) |
|
91 | void QAbstractSeries::setName(const QString& name) | |
92 | { |
|
92 | { | |
93 |
d_ptr->m_name |
|
93 | if (name != d_ptr->m_name) { | |
|
94 | d_ptr->m_name = name; | |||
|
95 | nameChanged(); | |||
|
96 | } | |||
94 | } |
|
97 | } | |
95 |
|
98 | |||
96 | /*! |
|
99 | /*! | |
97 | \brief Returns the name of the series. |
|
100 | \brief Returns the name of the series. | |
98 | \sa setName() |
|
101 | \sa setName() | |
99 | */ |
|
102 | */ | |
100 | QString QAbstractSeries::name() const |
|
103 | QString QAbstractSeries::name() const | |
101 | { |
|
104 | { | |
102 | return d_ptr->m_name; |
|
105 | return d_ptr->m_name; | |
103 | } |
|
106 | } | |
104 |
|
107 | |||
105 | /*! |
|
108 | /*! | |
106 | \brief Returns the chart where series belongs to. |
|
109 | \brief Returns the chart where series belongs to. | |
107 |
|
110 | |||
108 | Set automatically when the series is added to the chart |
|
111 | Set automatically when the series is added to the chart | |
109 | and unset when the series is removed from the chart. |
|
112 | and unset when the series is removed from the chart. | |
110 | */ |
|
113 | */ | |
111 | QChart* QAbstractSeries::chart() const |
|
114 | QChart* QAbstractSeries::chart() const | |
112 | { |
|
115 | { | |
113 | return d_ptr->m_chart; |
|
116 | return d_ptr->m_chart; | |
114 | } |
|
117 | } | |
115 |
|
118 | |||
116 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
119 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
117 |
|
120 | |||
118 | QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q): |
|
121 | QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q): | |
119 | q_ptr(q), |
|
122 | q_ptr(q), | |
120 | m_chart(0), |
|
123 | m_chart(0), | |
121 | m_dataset(0) |
|
124 | m_dataset(0) | |
122 | { |
|
125 | { | |
123 | } |
|
126 | } | |
124 |
|
127 | |||
125 | QAbstractSeriesPrivate::~QAbstractSeriesPrivate() |
|
128 | QAbstractSeriesPrivate::~QAbstractSeriesPrivate() | |
126 | { |
|
129 | { | |
127 | } |
|
130 | } | |
128 |
|
131 | |||
129 | #include "moc_qabstractseries.cpp" |
|
132 | #include "moc_qabstractseries.cpp" | |
130 | #include "moc_qabstractseries_p.cpp" |
|
133 | #include "moc_qabstractseries_p.cpp" | |
131 |
|
134 | |||
132 | QTCOMMERCIALCHART_END_NAMESPACE |
|
135 | QTCOMMERCIALCHART_END_NAMESPACE | |
133 |
|
136 | |||
134 |
|
137 |
@@ -1,71 +1,74 | |||||
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 QABSTRACTSERIES_H |
|
21 | #ifndef QABSTRACTSERIES_H | |
22 | #define QABSTRACTSERIES_H |
|
22 | #define QABSTRACTSERIES_H | |
23 |
|
23 | |||
24 | #include <qchartglobal.h> |
|
24 | #include <qchartglobal.h> | |
25 | #include <QObject> |
|
25 | #include <QObject> | |
26 | #include <QPen> |
|
26 | #include <QPen> | |
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
29 |
|
29 | |||
30 | class QAbstractSeriesPrivate; |
|
30 | class QAbstractSeriesPrivate; | |
31 | class QChart; |
|
31 | class QChart; | |
32 |
|
32 | |||
33 | class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject |
|
33 | class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject | |
34 | { |
|
34 | { | |
35 | Q_OBJECT |
|
35 | Q_OBJECT | |
36 | Q_PROPERTY(QString name READ name WRITE setName) |
|
36 | Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) | |
37 | Q_ENUMS(SeriesType) |
|
37 | Q_ENUMS(SeriesType) | |
38 |
|
38 | |||
39 | public: |
|
39 | public: | |
40 | enum SeriesType { |
|
40 | enum SeriesType { | |
41 | SeriesTypeLine, |
|
41 | SeriesTypeLine, | |
42 | SeriesTypeArea, |
|
42 | SeriesTypeArea, | |
43 | SeriesTypeBar, |
|
43 | SeriesTypeBar, | |
44 | SeriesTypeStackedBar, |
|
44 | SeriesTypeStackedBar, | |
45 | SeriesTypePercentBar, |
|
45 | SeriesTypePercentBar, | |
46 | SeriesTypeGroupedBar, |
|
46 | SeriesTypeGroupedBar, | |
47 | SeriesTypePie, |
|
47 | SeriesTypePie, | |
48 | SeriesTypeScatter, |
|
48 | SeriesTypeScatter, | |
49 | SeriesTypeSpline |
|
49 | SeriesTypeSpline | |
50 | }; |
|
50 | }; | |
51 |
|
51 | |||
52 | protected: |
|
52 | protected: | |
53 | QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0); |
|
53 | QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0); | |
54 |
|
54 | |||
55 | public: |
|
55 | public: | |
56 | ~QAbstractSeries(); |
|
56 | ~QAbstractSeries(); | |
57 | virtual SeriesType type() const = 0; |
|
57 | virtual SeriesType type() const = 0; | |
58 | void setName(const QString& name); |
|
58 | void setName(const QString& name); | |
59 | QString name() const; |
|
59 | QString name() const; | |
60 | QChart* chart() const; |
|
60 | QChart* chart() const; | |
61 |
|
61 | |||
|
62 | Q_SIGNALS: | |||
|
63 | void nameChanged(); | |||
|
64 | ||||
62 | protected: |
|
65 | protected: | |
63 | QScopedPointer<QAbstractSeriesPrivate> d_ptr; |
|
66 | QScopedPointer<QAbstractSeriesPrivate> d_ptr; | |
64 | friend class ChartDataSet; |
|
67 | friend class ChartDataSet; | |
65 | friend class ChartPresenter; |
|
68 | friend class ChartPresenter; | |
66 | friend class QLegendPrivate; |
|
69 | friend class QLegendPrivate; | |
67 | }; |
|
70 | }; | |
68 |
|
71 | |||
69 | QTCOMMERCIALCHART_END_NAMESPACE |
|
72 | QTCOMMERCIALCHART_END_NAMESPACE | |
70 |
|
73 | |||
71 | #endif |
|
74 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now