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