##// END OF EJS Templates
solid brush for all legend markers
sauimone -
r800:32248fc3cb26
parent child
Show More
@@ -1,210 +1,214
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 "qchartglobal.h"
21 #include "qchartglobal.h"
22 #include "legendmarker_p.h"
22 #include "legendmarker_p.h"
23 #include <qpieslice.h>
23 #include <qpieslice.h>
24 #include <qbarset.h>
24 #include <qbarset.h>
25 #include <qxyseries.h>
25 #include <qxyseries.h>
26 #include <qareaseries.h>
26 #include <qareaseries.h>
27 #include <QPainter>
27 #include <QPainter>
28 #include <QGraphicsSceneEvent>
28 #include <QGraphicsSceneEvent>
29 #include <QGraphicsSimpleTextItem>
29 #include <QGraphicsSimpleTextItem>
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 LegendMarker::LegendMarker(QSeries *series, QGraphicsItem *parent) : QGraphicsObject(parent),
33 LegendMarker::LegendMarker(QSeries *series, QGraphicsItem *parent) : QGraphicsObject(parent),
34 m_pos(0,0),
34 m_pos(0,0),
35 m_size(0,0),
35 m_size(0,0),
36 m_boundingRect(0,0,0,0),
36 m_boundingRect(0,0,0,0),
37 m_markerBoundingRect(0,0,0,0),
37 m_markerBoundingRect(0,0,0,0),
38 m_series(series),
38 m_series(series),
39 m_barset(0),
39 m_barset(0),
40 m_pieslice(0),
40 m_pieslice(0),
41 m_textItem(new QGraphicsSimpleTextItem(this))
41 m_textItem(new QGraphicsSimpleTextItem(this))
42 {
42 {
43 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
43 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
44 }
44 }
45
45
46 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent) : QGraphicsObject(parent),
46 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent) : QGraphicsObject(parent),
47 m_pos(0,0),
47 m_pos(0,0),
48 m_size(0,0),
48 m_size(0,0),
49 m_boundingRect(0,0,0,0),
49 m_boundingRect(0,0,0,0),
50 m_markerBoundingRect(0,0,0,0),
50 m_markerBoundingRect(0,0,0,0),
51 m_series(series),
51 m_series(series),
52 m_barset(barset),
52 m_barset(barset),
53 m_pieslice(0),
53 m_pieslice(0),
54 m_textItem(new QGraphicsSimpleTextItem(this))
54 m_textItem(new QGraphicsSimpleTextItem(this))
55 {
55 {
56 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
56 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
57 }
57 }
58
58
59 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent) : QGraphicsObject(parent),
59 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent) : QGraphicsObject(parent),
60 m_pos(0,0),
60 m_pos(0,0),
61 m_size(0,0),
61 m_size(0,0),
62 m_boundingRect(0,0,0,0),
62 m_boundingRect(0,0,0,0),
63 m_markerBoundingRect(0,0,0,0),
63 m_markerBoundingRect(0,0,0,0),
64 m_series(series),
64 m_series(series),
65 m_barset(0),
65 m_barset(0),
66 m_pieslice(pieslice),
66 m_pieslice(pieslice),
67 m_textItem(new QGraphicsSimpleTextItem(this))
67 m_textItem(new QGraphicsSimpleTextItem(this))
68 {
68 {
69 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
69 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
70 }
70 }
71
71
72 void LegendMarker::setPos(qreal x, qreal y)
72 void LegendMarker::setPos(qreal x, qreal y)
73 {
73 {
74 m_pos = QPointF(x,y);
74 m_pos = QPointF(x,y);
75 layoutChanged();
75 layoutChanged();
76 }
76 }
77
77
78 void LegendMarker::setPen(const QPen &pen)
78 void LegendMarker::setPen(const QPen &pen)
79 {
79 {
80 m_pen = pen;
80 m_pen = pen;
81 }
81 }
82
82
83 QPen LegendMarker::pen() const
83 QPen LegendMarker::pen() const
84 {
84 {
85 return m_pen;
85 return m_pen;
86 }
86 }
87
87
88 void LegendMarker::setBrush(const QBrush &brush)
88 void LegendMarker::setBrush(const QBrush &brush)
89 {
89 {
90 m_brush = brush;
90 m_brush = brush;
91 }
91 }
92
92
93 QBrush LegendMarker::brush() const
93 QBrush LegendMarker::brush() const
94 {
94 {
95 return m_brush;
95 return m_brush;
96 }
96 }
97
97
98 void LegendMarker::setName(const QString name)
98 void LegendMarker::setName(const QString name)
99 {
99 {
100 m_textItem->setText(name);
100 m_textItem->setText(name);
101 layoutChanged();
101 layoutChanged();
102 }
102 }
103
103
104 QString LegendMarker::name() const
104 QString LegendMarker::name() const
105 {
105 {
106 return m_textItem->text();
106 return m_textItem->text();
107 }
107 }
108
108
109 QSeries* LegendMarker::series() const
109 QSeries* LegendMarker::series() const
110 {
110 {
111 return m_series;
111 return m_series;
112 }
112 }
113
113
114 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
114 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
115 {
115 {
116 Q_UNUSED(option)
116 Q_UNUSED(option)
117 Q_UNUSED(widget)
117 Q_UNUSED(widget)
118
118
119 painter->setPen(m_pen);
119 painter->setPen(m_pen);
120 painter->setBrush(m_brush);
120 painter->setBrush(m_brush);
121 painter->drawRect(m_markerBoundingRect);
121 painter->drawRect(m_markerBoundingRect);
122 }
122 }
123
123
124 QRectF LegendMarker::boundingRect() const
124 QRectF LegendMarker::boundingRect() const
125 {
125 {
126 return m_boundingRect;
126 return m_boundingRect;
127 }
127 }
128
128
129 void LegendMarker::layoutChanged()
129 void LegendMarker::layoutChanged()
130 {
130 {
131 QSizeF markerSize(10,10);
131 QSizeF markerSize(10,10);
132 qreal margin = 2;
132 qreal margin = 2;
133
133
134 m_size.setHeight(markerSize.height() + 2 * margin);
134 m_size.setHeight(markerSize.height() + 2 * margin);
135 m_size.setWidth(m_textItem->boundingRect().width() + markerSize.width() + 3 * margin);
135 m_size.setWidth(m_textItem->boundingRect().width() + markerSize.width() + 3 * margin);
136
136
137 m_boundingRect = QRectF(m_pos.x(),m_pos.y(),m_size.width(),m_size.height());
137 m_boundingRect = QRectF(m_pos.x(),m_pos.y(),m_size.width(),m_size.height());
138
138
139 m_markerBoundingRect = QRectF(m_pos.x() + margin, m_pos.y() + margin, markerSize.width(),markerSize.height());
139 m_markerBoundingRect = QRectF(m_pos.x() + margin, m_pos.y() + margin, markerSize.width(),markerSize.height());
140
140
141 m_textItem->setPos(m_pos.x() + markerSize.width() + 2 * margin, m_pos.y() + margin);
141 m_textItem->setPos(m_pos.x() + markerSize.width() + 2 * margin, m_pos.y() + margin);
142 }
142 }
143
143
144 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
144 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
145 {
145 {
146 switch (m_series->type()) {
146 switch (m_series->type()) {
147 case QSeries::SeriesTypeLine:
147 case QSeries::SeriesTypeLine:
148 case QSeries::SeriesTypeArea:
148 case QSeries::SeriesTypeArea:
149 case QSeries::SeriesTypeScatter:
149 case QSeries::SeriesTypeScatter:
150 case QSeries::SeriesTypeSpline: {
150 case QSeries::SeriesTypeSpline: {
151 emit clicked(m_series,event->button());
151 emit clicked(m_series,event->button());
152 break;
152 break;
153 }
153 }
154 case QSeries::SeriesTypeBar:
154 case QSeries::SeriesTypeBar:
155 case QSeries::SeriesTypeStackedBar:
155 case QSeries::SeriesTypeStackedBar:
156 case QSeries::SeriesTypePercentBar: {
156 case QSeries::SeriesTypePercentBar: {
157 emit clicked(m_barset,event->button());
157 emit clicked(m_barset,event->button());
158 break;
158 break;
159 }
159 }
160 case QSeries::SeriesTypePie: {
160 case QSeries::SeriesTypePie: {
161 emit clicked(m_pieslice,event->button());
161 emit clicked(m_pieslice,event->button());
162 break;
162 break;
163 }
163 }
164 default: {
164 default: {
165 break;
165 break;
166 }
166 }
167 }
167 }
168 }
168 }
169
169
170 void LegendMarker::changed()
170 void LegendMarker::changed()
171 {
171 {
172 setPen(Qt::NoPen);
172 switch (m_series->type()) {
173 switch (m_series->type()) {
173 case QSeries::SeriesTypeArea: {
174 case QSeries::SeriesTypeArea: {
174 QAreaSeries* s = static_cast<QAreaSeries*> (m_series);
175 QAreaSeries* s = static_cast<QAreaSeries*> (m_series);
175 setPen(s->pen());
176 setBrush(s->brush());
176 setBrush(s->brush());
177 setName(s->name());
177 setName(s->name());
178 break;
178 break;
179 }
179 }
180 case QSeries::SeriesTypeLine:
180 case QSeries::SeriesTypeLine:
181 case QSeries::SeriesTypeScatter:
182 case QSeries::SeriesTypeSpline: {
181 case QSeries::SeriesTypeSpline: {
183 QXYSeries* s = static_cast<QXYSeries*> (m_series);
182 QXYSeries* s = static_cast<QXYSeries*> (m_series);
184 setPen(s->pen());
183 setBrush(QBrush(s->pen().color(),Qt::SolidPattern));
185 setBrush(s->brush());
186 setName(s->name());
184 setName(s->name());
187 break;
185 break;
188 }
186 }
187 case QSeries::SeriesTypeScatter: {
188 QXYSeries* s = static_cast<QXYSeries*> (m_series);
189 setBrush(s->brush());
190 setName(s->name());
191 break;
192 }
189 case QSeries::SeriesTypeBar:
193 case QSeries::SeriesTypeBar:
190 case QSeries::SeriesTypeStackedBar:
194 case QSeries::SeriesTypeStackedBar:
191 case QSeries::SeriesTypePercentBar: {
195 case QSeries::SeriesTypePercentBar: {
192 setPen(m_barset->pen());
193 setBrush(m_barset->brush());
196 setBrush(m_barset->brush());
194 setName(m_barset->name());
197 setName(m_barset->name());
195 break;
198 break;
196 }
199 }
197 case QSeries::SeriesTypePie: {
200 case QSeries::SeriesTypePie: {
198 setBrush(m_pieslice->brush());
201 setBrush(m_pieslice->brush());
199 setName(m_pieslice->label());
202 setName(m_pieslice->label());
200 break;
203 break;
201 }
204 }
202 default: {
205 default: {
206 setBrush(Qt::NoBrush);
203 break;
207 break;
204 }
208 }
205 }
209 }
206 }
210 }
207
211
208 #include "moc_legendmarker_p.cpp"
212 #include "moc_legendmarker_p.cpp"
209
213
210 QTCOMMERCIALCHART_END_NAMESPACE
214 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,441 +1,441
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 "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include <QGraphicsScene>
23 #include <QGraphicsScene>
24 #include <QGraphicsSceneResizeEvent>
24 #include <QGraphicsSceneResizeEvent>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 /*!
28 /*!
29 \enum QChart::ChartTheme
29 \enum QChart::ChartTheme
30
30
31 This enum describes the theme used by the chart.
31 This enum describes the theme used by the chart.
32
32
33 \value ChartThemeDefault Follows the GUI style of the Operating System
33 \value ChartThemeDefault Follows the GUI style of the Operating System
34 \value ChartThemeLight
34 \value ChartThemeLight
35 \value ChartThemeBlueCerulean
35 \value ChartThemeBlueCerulean
36 \value ChartThemeDark
36 \value ChartThemeDark
37 \value ChartThemeBrownSand
37 \value ChartThemeBrownSand
38 \value ChartThemeBlueNcs
38 \value ChartThemeBlueNcs
39 \value ChartThemeHighContrast
39 \value ChartThemeHighContrast
40 \value ChartThemeBlueIcy
40 \value ChartThemeBlueIcy
41 \value ChartThemeCount Not really a theme; the total count of themes.
41 \value ChartThemeCount Not really a theme; the total count of themes.
42 */
42 */
43
43
44 /*!
44 /*!
45 \enum QChart::AnimationOption
45 \enum QChart::AnimationOption
46
46
47 For enabling/disabling animations. Defaults to NoAnimation.
47 For enabling/disabling animations. Defaults to NoAnimation.
48
48
49 \value NoAnimation
49 \value NoAnimation
50 \value GridAxisAnimations
50 \value GridAxisAnimations
51 \value SeriesAnimations
51 \value SeriesAnimations
52 \value AllAnimations
52 \value AllAnimations
53 */
53 */
54
54
55 /*!
55 /*!
56 \class QChart
56 \class QChart
57 \brief QtCommercial chart API.
57 \brief QtCommercial chart API.
58
58
59 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
59 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
60 representation of different types of QChartSeries and other chart related objects like
60 representation of different types of QChartSeries and other chart related objects like
61 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
61 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
62 convenience class QChartView instead of QChart.
62 convenience class QChartView instead of QChart.
63 \sa QChartView
63 \sa QChartView
64 */
64 */
65
65
66 /*!
66 /*!
67 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
67 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
68 */
68 */
69 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
69 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
70 d_ptr(new QChartPrivate(this))
70 d_ptr(new QChartPrivate(this))
71 {
71 {
72 //setMinimumSize(200,200);
72 //setMinimumSize(200,200);
73 d_ptr->m_legend = new QLegend(this);
73 d_ptr->m_legend = new QLegend(this);
74 d_ptr->m_dataset = new ChartDataSet(this);
74 d_ptr->m_dataset = new ChartDataSet(this);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
76 int padding = d_ptr->m_presenter->padding();
76 int padding = d_ptr->m_presenter->padding();
77 setMinimumSize(3*padding,3*padding);
77 setMinimumSize(3*padding,3*padding);
78 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
78 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
79 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
79 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
80 }
80 }
81
81
82 /*!
82 /*!
83 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
83 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
84 */
84 */
85 QChart::~QChart()
85 QChart::~QChart()
86 {
86 {
87 //delete first presenter , since this is a root of all the graphical items
87 //delete first presenter , since this is a root of all the graphical items
88 delete d_ptr->m_presenter;
88 delete d_ptr->m_presenter;
89 d_ptr->m_presenter=0;
89 d_ptr->m_presenter=0;
90 }
90 }
91
91
92 /*!
92 /*!
93 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
93 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
94 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
94 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
95 the y axis).
95 the y axis).
96 */
96 */
97 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
97 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
98 {
98 {
99 d_ptr->m_dataset->addSeries(series, axisY);
99 d_ptr->m_dataset->addSeries(series, axisY);
100 }
100 }
101
101
102 /*!
102 /*!
103 Removes the \a series specified in a perameter from the QChartView.
103 Removes the \a series specified in a perameter from the QChartView.
104 It releses its ownership of the specified QChartSeries object.
104 It releses its ownership of the specified QChartSeries object.
105 It does not delete the pointed QChartSeries data object
105 It does not delete the pointed QChartSeries data object
106 \sa addSeries(), removeAllSeries()
106 \sa addSeries(), removeAllSeries()
107 */
107 */
108 void QChart::removeSeries(QSeries* series)
108 void QChart::removeSeries(QSeries* series)
109 {
109 {
110 d_ptr->m_dataset->removeSeries(series);
110 d_ptr->m_dataset->removeSeries(series);
111 }
111 }
112
112
113 /*!
113 /*!
114 Removes all the QChartSeries that have been added to the QChartView
114 Removes all the QChartSeries that have been added to the QChartView
115 It also deletes the pointed QChartSeries data objects
115 It also deletes the pointed QChartSeries data objects
116 \sa addSeries(), removeSeries()
116 \sa addSeries(), removeSeries()
117 */
117 */
118 void QChart::removeAllSeries()
118 void QChart::removeAllSeries()
119 {
119 {
120 d_ptr->m_dataset->removeAllSeries();
120 d_ptr->m_dataset->removeAllSeries();
121 }
121 }
122
122
123 /*!
123 /*!
124 Sets the \a brush that is used for painting the background of the chart area.
124 Sets the \a brush that is used for painting the background of the chart area.
125 */
125 */
126 void QChart::setBackgroundBrush(const QBrush& brush)
126 void QChart::setBackgroundBrush(const QBrush& brush)
127 {
127 {
128 d_ptr->createChartBackgroundItem();
128 d_ptr->createChartBackgroundItem();
129 d_ptr->m_backgroundItem->setBrush(brush);
129 d_ptr->m_backgroundItem->setBrush(brush);
130 d_ptr->m_backgroundItem->update();
130 d_ptr->m_backgroundItem->update();
131 }
131 }
132
132
133 QBrush QChart::backgroundBrush() const
133 QBrush QChart::backgroundBrush() const
134 {
134 {
135 if (!d_ptr->m_backgroundItem) return QBrush();
135 if (!d_ptr->m_backgroundItem) return QBrush();
136 return (d_ptr->m_backgroundItem)->brush();
136 return (d_ptr->m_backgroundItem)->brush();
137 }
137 }
138
138
139 /*!
139 /*!
140 Sets the \a pen that is used for painting the background of the chart area.
140 Sets the \a pen that is used for painting the background of the chart area.
141 */
141 */
142 void QChart::setBackgroundPen(const QPen& pen)
142 void QChart::setBackgroundPen(const QPen& pen)
143 {
143 {
144 d_ptr->createChartBackgroundItem();
144 d_ptr->createChartBackgroundItem();
145 d_ptr->m_backgroundItem->setPen(pen);
145 d_ptr->m_backgroundItem->setPen(pen);
146 d_ptr->m_backgroundItem->update();
146 d_ptr->m_backgroundItem->update();
147 }
147 }
148
148
149 QPen QChart::backgroundPen() const
149 QPen QChart::backgroundPen() const
150 {
150 {
151 if (!d_ptr->m_backgroundItem) return QPen();
151 if (!d_ptr->m_backgroundItem) return QPen();
152 return d_ptr->m_backgroundItem->pen();
152 return d_ptr->m_backgroundItem->pen();
153 }
153 }
154
154
155 /*!
155 /*!
156 Sets the chart \a title. The description text that is drawn above the chart.
156 Sets the chart \a title. The description text that is drawn above the chart.
157 */
157 */
158 void QChart::setTitle(const QString& title)
158 void QChart::setTitle(const QString& title)
159 {
159 {
160 d_ptr->createChartTitleItem();
160 d_ptr->createChartTitleItem();
161 d_ptr->m_titleItem->setText(title);
161 d_ptr->m_titleItem->setText(title);
162 d_ptr->updateLayout();
162 d_ptr->updateLayout();
163 }
163 }
164
164
165 /*!
165 /*!
166 Returns the chart title. The description text that is drawn above the chart.
166 Returns the chart title. The description text that is drawn above the chart.
167 */
167 */
168 QString QChart::title() const
168 QString QChart::title() const
169 {
169 {
170 if (d_ptr->m_titleItem)
170 if (d_ptr->m_titleItem)
171 return d_ptr->m_titleItem->text();
171 return d_ptr->m_titleItem->text();
172 else
172 else
173 return QString();
173 return QString();
174 }
174 }
175
175
176 /*!
176 /*!
177 Sets the \a font that is used for rendering the description text that is rendered above the chart.
177 Sets the \a font that is used for rendering the description text that is rendered above the chart.
178 */
178 */
179 void QChart::setTitleFont(const QFont& font)
179 void QChart::setTitleFont(const QFont& font)
180 {
180 {
181 d_ptr->createChartTitleItem();
181 d_ptr->createChartTitleItem();
182 d_ptr->m_titleItem->setFont(font);
182 d_ptr->m_titleItem->setFont(font);
183 d_ptr->updateLayout();
183 d_ptr->updateLayout();
184 }
184 }
185
185
186 /*!
186 /*!
187 Sets the \a brush used for rendering the title text.
187 Sets the \a brush used for rendering the title text.
188 */
188 */
189 void QChart::setTitleBrush(const QBrush &brush)
189 void QChart::setTitleBrush(const QBrush &brush)
190 {
190 {
191 d_ptr->createChartTitleItem();
191 d_ptr->createChartTitleItem();
192 d_ptr->m_titleItem->setBrush(brush);
192 d_ptr->m_titleItem->setBrush(brush);
193 d_ptr->updateLayout();
193 d_ptr->updateLayout();
194 }
194 }
195
195
196 /*!
196 /*!
197 Returns the brush used for rendering the title text.
197 Returns the brush used for rendering the title text.
198 */
198 */
199 QBrush QChart::titleBrush() const
199 QBrush QChart::titleBrush() const
200 {
200 {
201 if (!d_ptr->m_titleItem) return QBrush();
201 if (!d_ptr->m_titleItem) return QBrush();
202 return d_ptr->m_titleItem->brush();
202 return d_ptr->m_titleItem->brush();
203 }
203 }
204
204
205 /*!
205 /*!
206 Sets the \a theme used by the chart for rendering the graphical representation of the data
206 Sets the \a theme used by the chart for rendering the graphical representation of the data
207 \sa ChartTheme, chartTheme()
207 \sa ChartTheme, chartTheme()
208 */
208 */
209 void QChart::setTheme(QChart::ChartTheme theme)
209 void QChart::setTheme(QChart::ChartTheme theme)
210 {
210 {
211 d_ptr->m_presenter->setTheme(theme);
211 d_ptr->m_presenter->setTheme(theme);
212 }
212 }
213
213
214 /*!
214 /*!
215 Returns the theme enum used by the chart.
215 Returns the theme enum used by the chart.
216 \sa ChartTheme, setChartTheme()
216 \sa ChartTheme, setChartTheme()
217 */
217 */
218 QChart::ChartTheme QChart::theme() const
218 QChart::ChartTheme QChart::theme() const
219 {
219 {
220 return d_ptr->m_presenter->theme();
220 return d_ptr->m_presenter->theme();
221 }
221 }
222
222
223 /*!
223 /*!
224 Zooms in the view by a factor of 2
224 Zooms in the view by a factor of 2
225 */
225 */
226 void QChart::zoomIn()
226 void QChart::zoomIn()
227 {
227 {
228 d_ptr->m_presenter->zoomIn();
228 d_ptr->m_presenter->zoomIn();
229 }
229 }
230
230
231 /*!
231 /*!
232 Zooms in the view to a maximum level at which \a rect is still fully visible.
232 Zooms in the view to a maximum level at which \a rect is still fully visible.
233 */
233 */
234 void QChart::zoomIn(const QRectF& rect)
234 void QChart::zoomIn(const QRectF& rect)
235 {
235 {
236 if (!rect.isValid()) return;
236 if (!rect.isValid()) return;
237 d_ptr->m_presenter->zoomIn(rect);
237 d_ptr->m_presenter->zoomIn(rect);
238 }
238 }
239
239
240 /*!
240 /*!
241 Restores the view zoom level to the previous one.
241 Restores the view zoom level to the previous one.
242 */
242 */
243 void QChart::zoomOut()
243 void QChart::zoomOut()
244 {
244 {
245 d_ptr->m_presenter->zoomOut();
245 d_ptr->m_presenter->zoomOut();
246 }
246 }
247
247
248 /*!
248 /*!
249 Returns the pointer to the x axis object of the chart
249 Returns the pointer to the x axis object of the chart
250 */
250 */
251 QChartAxis* QChart::axisX() const
251 QChartAxis* QChart::axisX() const
252 {
252 {
253 return d_ptr->m_dataset->axisX();
253 return d_ptr->m_dataset->axisX();
254 }
254 }
255
255
256 /*!
256 /*!
257 Returns the pointer to the y axis object of the chart
257 Returns the pointer to the y axis object of the chart
258 */
258 */
259 QChartAxis* QChart::axisY() const
259 QChartAxis* QChart::axisY() const
260 {
260 {
261 return d_ptr->m_dataset->axisY();
261 return d_ptr->m_dataset->axisY();
262 }
262 }
263
263
264 /*!
264 /*!
265 Returns the legend object of the chart. Ownership stays in chart.
265 Returns the legend object of the chart. Ownership stays in chart.
266 */
266 */
267 QLegend* QChart::legend() const
267 QLegend* QChart::legend() const
268 {
268 {
269 return d_ptr->m_legend;
269 return d_ptr->m_legend;
270 }
270 }
271
271
272 /*!
272 /*!
273 Resizes and updates the chart area using the \a event data
273 Resizes and updates the chart area using the \a event data
274 */
274 */
275 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
275 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
276 {
276 {
277 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
277 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
278 d_ptr->updateLayout();
278 d_ptr->updateLayout();
279 QGraphicsWidget::resizeEvent(event);
279 QGraphicsWidget::resizeEvent(event);
280 update();
280 update();
281 }
281 }
282
282
283 /*!
283 /*!
284 Sets animation \a options for the chart
284 Sets animation \a options for the chart
285 */
285 */
286 void QChart::setAnimationOptions(AnimationOptions options)
286 void QChart::setAnimationOptions(AnimationOptions options)
287 {
287 {
288 d_ptr->m_presenter->setAnimationOptions(options);
288 d_ptr->m_presenter->setAnimationOptions(options);
289 }
289 }
290
290
291 /*!
291 /*!
292 Returns animation options for the chart
292 Returns animation options for the chart
293 */
293 */
294 QChart::AnimationOptions QChart::animationOptions() const
294 QChart::AnimationOptions QChart::animationOptions() const
295 {
295 {
296 return d_ptr->m_presenter->animationOptions();
296 return d_ptr->m_presenter->animationOptions();
297 }
297 }
298
298
299 void QChart::scrollLeft()
299 void QChart::scrollLeft()
300 {
300 {
301 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
301 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
302 }
302 }
303
303
304 void QChart::scrollRight()
304 void QChart::scrollRight()
305 {
305 {
306 d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
306 d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
307 }
307 }
308
308
309 void QChart::scrollUp()
309 void QChart::scrollUp()
310 {
310 {
311 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
311 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
312 }
312 }
313
313
314 void QChart::scrollDown()
314 void QChart::scrollDown()
315 {
315 {
316 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
316 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
317 }
317 }
318
318
319 void QChart::setBackgroundVisible(bool visible)
319 void QChart::setBackgroundVisible(bool visible)
320 {
320 {
321 d_ptr->createChartBackgroundItem();
321 d_ptr->createChartBackgroundItem();
322 d_ptr->m_backgroundItem->setVisible(visible);
322 d_ptr->m_backgroundItem->setVisible(visible);
323 }
323 }
324
324
325 bool QChart::isBackgroundVisible() const
325 bool QChart::isBackgroundVisible() const
326 {
326 {
327 if (!d_ptr->m_backgroundItem) return false;
327 if (!d_ptr->m_backgroundItem) return false;
328 return d_ptr->m_backgroundItem->isVisible();
328 return d_ptr->m_backgroundItem->isVisible();
329 }
329 }
330
330
331 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
331 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
332
332
333 QChartPrivate::QChartPrivate(QChart *parent):
333 QChartPrivate::QChartPrivate(QChart *parent):
334 q_ptr(parent),
334 q_ptr(parent),
335 m_backgroundItem(0),
335 m_backgroundItem(0),
336 m_titleItem(0),
336 m_titleItem(0),
337 m_legend(0),
337 m_legend(0),
338 m_dataset(0),
338 m_dataset(0),
339 m_presenter(0)
339 m_presenter(0)
340 {
340 {
341
341
342 }
342 }
343
343
344 QChartPrivate::~QChartPrivate()
344 QChartPrivate::~QChartPrivate()
345 {
345 {
346
346
347 }
347 }
348
348
349 void QChartPrivate::createChartBackgroundItem()
349 void QChartPrivate::createChartBackgroundItem()
350 {
350 {
351 if (!m_backgroundItem) {
351 if (!m_backgroundItem) {
352 m_backgroundItem = new ChartBackground(q_ptr);
352 m_backgroundItem = new ChartBackground(q_ptr);
353 m_backgroundItem->setPen(Qt::NoPen);
353 m_backgroundItem->setPen(Qt::NoPen);
354 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
354 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
355 }
355 }
356 }
356 }
357
357
358 void QChartPrivate::createChartTitleItem()
358 void QChartPrivate::createChartTitleItem()
359 {
359 {
360 if (!m_titleItem) {
360 if (!m_titleItem) {
361 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
361 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
362 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
362 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
363 }
363 }
364 }
364 }
365
365
366 void QChartPrivate::updateLegendLayout()
366 void QChartPrivate::updateLegendLayout()
367 {
367 {
368 int padding = m_presenter->padding();
368 int padding = m_presenter->padding();
369 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
369 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
370 QRectF legendRect;
370 QRectF legendRect;
371
371
372 switch (m_legend->alignment())
372 switch (m_legend->alignment())
373 {
373 {
374 case QLegend::AlignmentTop: {
374 case QLegend::AlignmentTop: {
375 legendRect = m_rect.adjusted(0,0,0,-padding - plotRect.height());
375 legendRect = m_rect.adjusted(0,padding,0,-padding - plotRect.height());
376 break;
376 break;
377 }
377 }
378 case QLegend::AlignmentBottom: {
378 case QLegend::AlignmentBottom: {
379 legendRect = m_rect.adjusted(padding,padding + plotRect.height(),-padding,0);
379 legendRect = m_rect.adjusted(padding,padding + plotRect.height(),-padding,0);
380 break;
380 break;
381 }
381 }
382 case QLegend::AlignmentLeft: {
382 case QLegend::AlignmentLeft: {
383 legendRect = m_rect.adjusted(0,padding,-padding - plotRect.width(),-padding);
383 legendRect = m_rect.adjusted(0,padding,-padding - plotRect.width(),-padding);
384 break;
384 break;
385 }
385 }
386 case QLegend::AlignmentRight: {
386 case QLegend::AlignmentRight: {
387 legendRect = m_rect.adjusted(padding + plotRect.width(),padding,0,-padding);
387 legendRect = m_rect.adjusted(padding + plotRect.width(),padding,0,-padding);
388 break;
388 break;
389 }
389 }
390 default: {
390 default: {
391 legendRect = plotRect;
391 legendRect = plotRect;
392 break;
392 break;
393 }
393 }
394 }
394 }
395
395
396 m_legend->setMaximumSize(legendRect.size());
396 m_legend->setMaximumSize(legendRect.size());
397
397
398 qreal width = legendRect.width() - m_legend->size().width();
398 qreal width = legendRect.width() - m_legend->size().width();
399 qreal height = legendRect.height() - m_legend->size().height();
399 qreal height = legendRect.height() - m_legend->size().height();
400
400
401 QPointF pos = legendRect.topLeft();
401 QPointF pos = legendRect.topLeft();
402 if (width > 0) {
402 if (width > 0) {
403 pos.setX(pos.x() + width/2);
403 pos.setX(pos.x() + width/2);
404 }
404 }
405 if (height > 0) {
405 if (height > 0) {
406 pos.setY(pos.y() + height/2);
406 pos.setY(pos.y() + height/2);
407 }
407 }
408
408
409 qDebug() << "lenged topleft:" << pos;
409 qDebug() << "lenged topleft:" << pos;
410 m_legend->setPos(pos);
410 m_legend->setPos(pos);
411 }
411 }
412
412
413 void QChartPrivate::updateLayout()
413 void QChartPrivate::updateLayout()
414 {
414 {
415 if (!m_rect.isValid()) return;
415 if (!m_rect.isValid()) return;
416
416
417 int padding = m_presenter->padding();
417 int padding = m_presenter->padding();
418 int backgroundPadding = m_presenter->backgroundPadding();
418 int backgroundPadding = m_presenter->backgroundPadding();
419
419
420 // recalculate title position
420 // recalculate title position
421 if (m_titleItem) {
421 if (m_titleItem) {
422 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
422 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
423 m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2);
423 m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2);
424 }
424 }
425
425
426 //recalculate background gradient
426 //recalculate background gradient
427 if (m_backgroundItem) {
427 if (m_backgroundItem) {
428 m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding));
428 m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding));
429 }
429 }
430
430
431 // recalculate legend position
431 // recalculate legend position
432 if (m_legend) {
432 if (m_legend) {
433 if ((m_legend->attachedToChart()) && (m_legend->parentObject() == q_ptr)) {
433 if ((m_legend->attachedToChart()) && (m_legend->parentObject() == q_ptr)) {
434 updateLegendLayout();
434 updateLegendLayout();
435 }
435 }
436 }
436 }
437 }
437 }
438
438
439 #include "moc_qchart.cpp"
439 #include "moc_qchart.cpp"
440
440
441 QTCOMMERCIALCHART_END_NAMESPACE
441 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,728 +1,720
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 "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qseries.h"
23 #include "qseries.h"
24 #include "legendmarker_p.h"
24 #include "legendmarker_p.h"
25 #include "legendscrollbutton_p.h"
25 #include "legendscrollbutton_p.h"
26 #include "qxyseries.h"
26 #include "qxyseries.h"
27 #include "qlineseries.h"
27 #include "qlineseries.h"
28 #include "qareaseries.h"
28 #include "qareaseries.h"
29 #include "qscatterseries.h"
29 #include "qscatterseries.h"
30 #include "qsplineseries.h"
30 #include "qsplineseries.h"
31 #include "qbarseries.h"
31 #include "qbarseries.h"
32 #include "qstackedbarseries.h"
32 #include "qstackedbarseries.h"
33 #include "qpercentbarseries.h"
33 #include "qpercentbarseries.h"
34 #include "qbarset.h"
34 #include "qbarset.h"
35 #include "qpieseries.h"
35 #include "qpieseries.h"
36 #include "qpieslice.h"
36 #include "qpieslice.h"
37 #include "chartpresenter_p.h"
37 #include "chartpresenter_p.h"
38 #include <QPainter>
38 #include <QPainter>
39 #include <QPen>
39 #include <QPen>
40
40
41 #include <QGraphicsSceneEvent>
41 #include <QGraphicsSceneEvent>
42
42
43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
44
44
45 /*!
45 /*!
46 \class QLegend
46 \class QLegend
47 \brief part of QtCommercial chart API.
47 \brief part of QtCommercial chart API.
48
48
49 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
49 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
50 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
50 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
51 handle the drawing manually.
51 handle the drawing manually.
52 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
52 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
53
53
54 \mainclass
54 \mainclass
55
55
56 \sa QChart, QSeries
56 \sa QChart, QSeries
57 */
57 */
58
58
59 /*!
59 /*!
60 \enum QLegend::Layout
60 \enum QLegend::Layout
61
61
62 This enum describes the possible position for legend inside chart.
62 This enum describes the possible position for legend inside chart.
63
63
64 \value LayoutTop
64 \value LayoutTop
65 \value LayoutBottom
65 \value LayoutBottom
66 \value LayoutLeft
66 \value LayoutLeft
67 \value LayoutRight
67 \value LayoutRight
68 */
68 */
69
69
70
70
71 /*!
71 /*!
72 \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button)
72 \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button)
73 \brief Notifies when series has been clicked on legend \a series \a button
73 \brief Notifies when series has been clicked on legend \a series \a button
74 */
74 */
75
75
76 /*!
76 /*!
77 \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button)
77 \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button)
78 \brief Notifies when barset has been clicked on legend \a barset \a button
78 \brief Notifies when barset has been clicked on legend \a barset \a button
79 */
79 */
80
80
81 /*!
81 /*!
82 \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button)
82 \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button)
83 \brief Notifies when pie slice has been clicked on legend \a slice \a button
83 \brief Notifies when pie slice has been clicked on legend \a slice \a button
84 */
84 */
85
85
86 /*!
86 /*!
87 Constructs the legend object and sets the parent to \a parent
87 Constructs the legend object and sets the parent to \a parent
88 */
88 */
89 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
89 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
90 m_margin(5),
90 m_margin(5),
91 m_pos(0,0),
91 m_pos(0,0),
92 m_minimumSize(50,20), // TODO: magic numbers
92 m_minimumSize(50,20), // TODO: magic numbers
93 m_maximumSize(150,100),
93 m_maximumSize(150,100),
94 m_size(m_minimumSize),
94 m_size(m_minimumSize),
95 m_brush(Qt::darkGray), // TODO: default should come from theme
95 m_brush(Qt::darkGray), // TODO: default should come from theme
96 m_alignment(QLegend::AlignmentRight),
96 m_alignment(QLegend::AlignmentTop),
97 mFirstMarker(0),
97 mFirstMarker(0),
98 m_attachedToChart(true)
98 m_attachedToChart(true)
99 {
99 {
100 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
100 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
101 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
101 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
102 m_scrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this);
102 m_scrollButtonUp = new LegendScrollButton(LegendScrollButton::ScrollButtonIdUp, this);
103 m_scrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this);
103 m_scrollButtonDown = new LegendScrollButton(LegendScrollButton::ScrollButtonIdDown, this);
104 setZValue(ChartPresenter::LegendZValue);
104 setZValue(ChartPresenter::LegendZValue);
105 }
105 }
106
106
107 /*!
107 /*!
108 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
108 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
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
114
115 painter->setOpacity(opacity());
115 painter->setOpacity(opacity());
116 painter->setPen(m_pen);
116 painter->setPen(m_pen);
117 painter->setBrush(m_brush);
117 painter->setBrush(m_brush);
118 // painter->drawRect(boundingRect());
118 // painter->drawRect(boundingRect());
119 }
119 }
120
120
121 /*!
121 /*!
122 Bounding rect of legend.
122 Bounding rect of legend.
123 */
123 */
124 QRectF QLegend::boundingRect() const
124 QRectF QLegend::boundingRect() const
125 {
125 {
126 return QRectF(m_pos,m_size);
126 return QRectF(m_pos,m_size);
127 }
127 }
128
128
129 /*!
129 /*!
130 Sets the \a brush of legend. Brush affects the background of legend.
130 Sets the \a brush of legend. Brush affects the background of legend.
131 */
131 */
132 void QLegend::setBrush(const QBrush &brush)
132 void QLegend::setBrush(const QBrush &brush)
133 {
133 {
134 if (m_brush != brush) {
134 if (m_brush != brush) {
135 m_brush = brush;
135 m_brush = brush;
136 update();
136 update();
137 }
137 }
138 }
138 }
139
139
140 /*!
140 /*!
141 Returns the brush used by legend.
141 Returns the brush used by legend.
142 */
142 */
143 QBrush QLegend::brush() const
143 QBrush QLegend::brush() const
144 {
144 {
145 return m_brush;
145 return m_brush;
146 }
146 }
147
147
148 /*!
148 /*!
149 Sets the \a pen of legend. Pen affects the legend borders.
149 Sets the \a pen of legend. Pen affects the legend borders.
150 */
150 */
151 void QLegend::setPen(const QPen &pen)
151 void QLegend::setPen(const QPen &pen)
152 {
152 {
153 if (m_pen != pen) {
153 if (m_pen != pen) {
154 m_pen = pen;
154 m_pen = pen;
155 update();
155 update();
156 }
156 }
157 }
157 }
158
158
159 /*!
159 /*!
160 Returns the pen used by legend
160 Returns the pen used by legend
161 */
161 */
162
162
163 QPen QLegend::pen() const
163 QPen QLegend::pen() const
164 {
164 {
165 return m_pen;
165 return m_pen;
166 }
166 }
167
167
168 /*!
168 /*!
169 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
169 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
170 \sa QLegend::Layout
170 \sa QLegend::Layout
171 */
171 */
172 void QLegend::setAlignmnent(QLegend::Alignment alignment)
172 void QLegend::setAlignmnent(QLegend::Alignment alignment)
173 {
173 {
174 m_alignment = alignment;
174 m_alignment = alignment;
175 updateLayout();
175 updateLayout();
176 }
176 }
177
177
178 /*!
178 /*!
179 Returns the preferred layout for legend
179 Returns the preferred layout for legend
180 */
180 */
181 QLegend::Alignment QLegend::alignment() const
181 QLegend::Alignment QLegend::alignment() const
182 {
182 {
183 return m_alignment;
183 return m_alignment;
184 }
184 }
185
185
186 /*!
186 /*!
187 Returns the maximum size of legend.
187 Returns the maximum size of legend.
188 */
188 */
189 QSizeF QLegend::maximumSize() const
189 QSizeF QLegend::maximumSize() const
190 {
190 {
191 return m_maximumSize;
191 return m_maximumSize;
192 }
192 }
193
193
194 /*!
194 /*!
195 Sets the maximum \a size for legend. The legend can't grow bigger than this size. If there are
195 Sets the maximum \a size for legend. The legend can't grow bigger than this size. If there are
196 more series than legend can fit to this size, scroll buttons are displayed.
196 more series than legend can fit to this size, scroll buttons are displayed.
197 */
197 */
198 void QLegend::setMaximumSize(const QSizeF size)
198 void QLegend::setMaximumSize(const QSizeF size)
199 {
199 {
200 m_maximumSize = size;
200 m_maximumSize = size;
201 updateLayout();
201 updateLayout();
202 }
202 }
203
203
204 /*!
204 /*!
205 Returns the current size of legend.
205 Returns the current size of legend.
206 */
206 */
207 QSizeF QLegend::size() const
207 QSizeF QLegend::size() const
208 {
208 {
209 return m_size;
209 return m_size;
210 }
210 }
211
211
212 /*!
212 /*!
213 Sets the \a size of legend. If size is bigger than maximum size of legend, the legend is resized to the maximum size.
213 Sets the \a size of legend. If size is bigger than maximum size of legend, the legend is resized to the maximum size.
214 \sa setMmaximumSize()
214 \sa setMmaximumSize()
215 */
215 */
216 void QLegend::setSize(const QSizeF size)
216 void QLegend::setSize(const QSizeF size)
217 {
217 {
218 m_size = size;
218 m_size = size;
219 if (m_size.width() > m_maximumSize.width()) {
219 if (m_size.width() > m_maximumSize.width()) {
220 m_size.setWidth(m_maximumSize.width());
220 m_size.setWidth(m_maximumSize.width());
221 }
221 }
222 if (m_size.height() > m_maximumSize.height()) {
222 if (m_size.height() > m_maximumSize.height()) {
223 m_size.setHeight(m_maximumSize.height());
223 m_size.setHeight(m_maximumSize.height());
224 }
224 }
225 }
225 }
226
226
227 /*!
227 /*!
228 Sets position of legend to \a pos
228 Sets position of legend to \a pos
229 */
229 */
230 void QLegend::setPos(const QPointF &pos)
230 void QLegend::setPos(const QPointF &pos)
231 {
231 {
232 m_pos = pos;
232 m_pos = pos;
233 updateLayout();
233 updateLayout();
234 }
234 }
235
235
236 /*!
236 /*!
237 \internal \a series \a domain Should be called when series is added to chart.
237 \internal \a series \a domain Should be called when series is added to chart.
238 */
238 */
239 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
239 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
240 {
240 {
241 Q_UNUSED(domain)
241 Q_UNUSED(domain)
242
242
243 switch (series->type())
243 switch (series->type())
244 {
244 {
245 case QSeries::SeriesTypeLine: {
245 case QSeries::SeriesTypeLine: {
246 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
246 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
247 appendMarkers(lineSeries);
247 appendMarkers(lineSeries);
248 break;
248 break;
249 }
249 }
250 case QSeries::SeriesTypeArea: {
250 case QSeries::SeriesTypeArea: {
251 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
251 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
252 appendMarkers(areaSeries);
252 appendMarkers(areaSeries);
253 break;
253 break;
254 }
254 }
255 case QSeries::SeriesTypeBar: {
255 case QSeries::SeriesTypeBar: {
256 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
256 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
257 appendMarkers(barSeries);
257 appendMarkers(barSeries);
258 break;
258 break;
259 }
259 }
260 case QSeries::SeriesTypeStackedBar: {
260 case QSeries::SeriesTypeStackedBar: {
261 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
261 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
262 appendMarkers(stackedBarSeries);
262 appendMarkers(stackedBarSeries);
263 break;
263 break;
264 }
264 }
265 case QSeries::SeriesTypePercentBar: {
265 case QSeries::SeriesTypePercentBar: {
266 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
266 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
267 appendMarkers(percentBarSeries);
267 appendMarkers(percentBarSeries);
268 break;
268 break;
269 }
269 }
270 case QSeries::SeriesTypeScatter: {
270 case QSeries::SeriesTypeScatter: {
271 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
271 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
272 appendMarkers(scatterSeries);
272 appendMarkers(scatterSeries);
273 break;
273 break;
274 }
274 }
275 case QSeries::SeriesTypePie: {
275 case QSeries::SeriesTypePie: {
276 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
276 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
277 appendMarkers(pieSeries);
277 appendMarkers(pieSeries);
278 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
278 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
279 break;
279 break;
280 }
280 }
281 case QSeries::SeriesTypeSpline: {
281 case QSeries::SeriesTypeSpline: {
282 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
282 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
283 appendMarkers(splineSeries);
283 appendMarkers(splineSeries);
284 break;
284 break;
285 }
285 }
286 default: {
286 default: {
287 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
287 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
288 break;
288 break;
289 }
289 }
290 }
290 }
291
291
292 updateLayout();
292 updateLayout();
293 }
293 }
294
294
295 /*!
295 /*!
296 \internal \a series Should be called when series is removed from chart.
296 \internal \a series Should be called when series is removed from chart.
297 */
297 */
298 void QLegend::handleSeriesRemoved(QSeries *series)
298 void QLegend::handleSeriesRemoved(QSeries *series)
299 {
299 {
300 switch (series->type())
300 switch (series->type())
301 {
301 {
302 case QSeries::SeriesTypeArea: {
302 case QSeries::SeriesTypeArea: {
303 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
303 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
304 deleteMarkers(areaSeries);
304 deleteMarkers(areaSeries);
305 break;
305 break;
306 }
306 }
307 case QSeries::SeriesTypePie: {
307 case QSeries::SeriesTypePie: {
308 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
308 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
309 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
309 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
310 deleteMarkers(series);
310 deleteMarkers(series);
311 break;
311 break;
312 }
312 }
313 default: {
313 default: {
314 // All other types
314 // All other types
315 deleteMarkers(series);
315 deleteMarkers(series);
316 break;
316 break;
317 }
317 }
318 }
318 }
319
319
320 updateLayout();
320 updateLayout();
321 }
321 }
322
322
323 /*!
323 /*!
324 \internal \a slices Should be called when slices are added to pie chart.
324 \internal \a slices Should be called when slices are added to pie chart.
325 */
325 */
326 void QLegend::handleAdded(QList<QPieSlice *> slices)
326 void QLegend::handleAdded(QList<QPieSlice *> slices)
327 {
327 {
328 QPieSeries* series = static_cast<QPieSeries *> (sender());
328 QPieSeries* series = static_cast<QPieSeries *> (sender());
329 foreach(QPieSlice* s, slices) {
329 foreach(QPieSlice* s, slices) {
330 LegendMarker* marker = new LegendMarker(series, s, this);
330 LegendMarker* marker = new LegendMarker(series, s, this);
331 marker->setName(s->label());
331 marker->setName(s->label());
332 marker->setBrush(s->brush());
332 marker->setBrush(s->brush());
333 connect(marker, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),
333 connect(marker, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),
334 this, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
334 this, SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
335 connect(s, SIGNAL(changed()), marker, SLOT(changed()));
335 connect(s, SIGNAL(changed()), marker, SLOT(changed()));
336 connect(s, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
336 connect(s, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
337 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
337 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
338 m_markers.append(marker);
338 m_markers.append(marker);
339 childItems().append(marker);
339 childItems().append(marker);
340 }
340 }
341 updateLayout();
341 updateLayout();
342 }
342 }
343
343
344 /*!
344 /*!
345 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
345 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
346 because removed slices are also deleted and we listen destroyed signal
346 because removed slices are also deleted and we listen destroyed signal
347 */
347 */
348 void QLegend::handleRemoved(QList<QPieSlice *> slices)
348 void QLegend::handleRemoved(QList<QPieSlice *> slices)
349 {
349 {
350 Q_UNUSED(slices)
350 Q_UNUSED(slices)
351 }
351 }
352
352
353
353
354 /*!
354 /*!
355 \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed
355 \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed
356 */
356 */
357 void QLegend::handleMarkerDestroyed()
357 void QLegend::handleMarkerDestroyed()
358 {
358 {
359 LegendMarker* m = static_cast<LegendMarker *> (sender());
359 LegendMarker* m = static_cast<LegendMarker *> (sender());
360 m_markers.removeOne(m);
360 m_markers.removeOne(m);
361 updateLayout();
361 updateLayout();
362 }
362 }
363
363
364 /*!
364 /*!
365 \internal \a event Handles clicked signals from scroll buttons
365 \internal \a event Handles clicked signals from scroll buttons
366 */
366 */
367 void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton)
367 void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton)
368 {
368 {
369 Q_ASSERT(scrollButton);
369 Q_ASSERT(scrollButton);
370
370
371 switch (scrollButton->id()) {
371 switch (scrollButton->id()) {
372 case LegendScrollButton::ScrollButtonIdLeft:
372 case LegendScrollButton::ScrollButtonIdLeft:
373 case LegendScrollButton::ScrollButtonIdUp: {
373 case LegendScrollButton::ScrollButtonIdUp: {
374 // Lower limit is same in these cases
374 // Lower limit is same in these cases
375 mFirstMarker--;
375 mFirstMarker--;
376 checkFirstMarkerBounds();
376 checkFirstMarkerBounds();
377 break;
377 break;
378 }
378 }
379 case LegendScrollButton::ScrollButtonIdRight:
379 case LegendScrollButton::ScrollButtonIdRight:
380 case LegendScrollButton::ScrollButtonIdDown: {
380 case LegendScrollButton::ScrollButtonIdDown: {
381 mFirstMarker++;
381 mFirstMarker++;
382 checkFirstMarkerBounds();
382 checkFirstMarkerBounds();
383 break;
383 break;
384 }
384 }
385 default: {
385 default: {
386 break;
386 break;
387 }
387 }
388 }
388 }
389 updateLayout();
389 updateLayout();
390 }
390 }
391
391
392 /*!
392 /*!
393 Detaches the legend from chart. Chart won't change layout of the legend.
393 Detaches the legend from chart. Chart won't change layout of the legend.
394 */
394 */
395 void QLegend::detachFromChart()
395 void QLegend::detachFromChart()
396 {
396 {
397 m_attachedToChart = false;
397 m_attachedToChart = false;
398 }
398 }
399
399
400 /*!
400 /*!
401 Attaches the legend to chart. Chart may change layout of the legend.
401 Attaches the legend to chart. Chart may change layout of the legend.
402 */
402 */
403 void QLegend::attachToChart()
403 void QLegend::attachToChart()
404 {
404 {
405 m_attachedToChart = true;
405 m_attachedToChart = true;
406 }
406 }
407
407
408 /*!
408 /*!
409 Returns true, if legend is attached to chart.
409 Returns true, if legend is attached to chart.
410 */
410 */
411 bool QLegend::attachedToChart()
411 bool QLegend::attachedToChart()
412 {
412 {
413 return m_attachedToChart;
413 return m_attachedToChart;
414 }
414 }
415
415
416 /*!
416 /*!
417 \internal Helper function. Appends markers from \a series to legend.
417 \internal Helper function. Appends markers from \a series to legend.
418 */
418 */
419 void QLegend::appendMarkers(QAreaSeries* series)
419 void QLegend::appendMarkers(QAreaSeries* series)
420 {
420 {
421 LegendMarker* marker = new LegendMarker(series,this);
421 LegendMarker* marker = new LegendMarker(series,this);
422 marker->setName(series->name());
423 marker->setPen(series->pen());
424 marker->setBrush(series->brush());
425 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
422 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
426 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
423 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
427 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
424 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
425 marker->changed();
428 m_markers.append(marker);
426 m_markers.append(marker);
429 childItems().append(marker);
427 childItems().append(marker);
430 }
428 }
431
429
432 /*!
430 /*!
433 \internal Helper function. Appends markers from \a series to legend.
431 \internal Helper function. Appends markers from \a series to legend.
434 */
432 */
435 void QLegend::appendMarkers(QXYSeries* series)
433 void QLegend::appendMarkers(QXYSeries* series)
436 {
434 {
437 LegendMarker* marker = new LegendMarker(series,this);
435 LegendMarker* marker = new LegendMarker(series,this);
438 marker->setName(series->name());
439 marker->setPen(series->pen());
440 marker->setBrush(series->brush());
441 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
436 connect(marker, SIGNAL(clicked(QSeries *, Qt::MouseButton)), this, SIGNAL(clicked(QSeries *, Qt::MouseButton)));
442 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
437 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
443 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
438 connect(series,SIGNAL(updated()),marker,SLOT(changed()));
439 marker->changed();
444 m_markers.append(marker);
440 m_markers.append(marker);
445 childItems().append(marker);
441 childItems().append(marker);
446 }
442 }
447
443
448 /*!
444 /*!
449 \internal Helper function. Appends markers from \a series to legend.
445 \internal Helper function. Appends markers from \a series to legend.
450 */
446 */
451 void QLegend::appendMarkers(QBarSeries *series)
447 void QLegend::appendMarkers(QBarSeries *series)
452 {
448 {
453 foreach(QBarSet* set, series->barSets()) {
449 foreach(QBarSet* set, series->barSets()) {
454 LegendMarker* marker = new LegendMarker(series, set, this);
450 LegendMarker* marker = new LegendMarker(series, set, this);
455 marker->setName(set->name());
456 marker->setPen(set->pen());
457 marker->setBrush(set->brush());
458 connect(marker, SIGNAL(clicked(QBarSet *, Qt::MouseButton)),
451 connect(marker, SIGNAL(clicked(QBarSet *, Qt::MouseButton)),
459 this, SIGNAL(clicked(QBarSet *, Qt::MouseButton)));
452 this, SIGNAL(clicked(QBarSet *, Qt::MouseButton)));
460 connect(set, SIGNAL(valueChanged()), marker, SLOT(changed()));
453 connect(set, SIGNAL(valueChanged()), marker, SLOT(changed()));
461 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
454 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
455 marker->changed();
462 m_markers.append(marker);
456 m_markers.append(marker);
463 childItems().append(marker);
457 childItems().append(marker);
464 }
458 }
465 }
459 }
466
460
467 /*!
461 /*!
468 \internal Helper function. Appends markers from \a series to legend.
462 \internal Helper function. Appends markers from \a series to legend.
469 */
463 */
470 void QLegend::appendMarkers(QPieSeries *series)
464 void QLegend::appendMarkers(QPieSeries *series)
471 {
465 {
472 foreach(QPieSlice* slice, series->slices()) {
466 foreach(QPieSlice* slice, series->slices()) {
473 LegendMarker* marker = new LegendMarker(series, slice, this);
467 LegendMarker* marker = new LegendMarker(series, slice, this);
474 marker->setName(slice->label());
475 marker->setPen(slice->pen());
476 marker->setBrush(slice->brush());
477 connect(marker, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)),
468 connect(marker, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)),
478 this, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)));
469 this, SIGNAL(clicked(QPieSlice *, Qt::MouseButton)));
479 connect(slice, SIGNAL(changed()), marker, SLOT(changed()));
470 connect(slice, SIGNAL(changed()), marker, SLOT(changed()));
480 connect(slice, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
471 connect(slice, SIGNAL(destroyed()), marker, SLOT(deleteLater()));
481 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
472 connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed()));
473 marker->changed();
482 m_markers.append(marker);
474 m_markers.append(marker);
483 childItems().append(marker);
475 childItems().append(marker);
484 }
476 }
485 }
477 }
486
478
487 /*!
479 /*!
488 \internal Deletes all markers that are created from \a series
480 \internal Deletes all markers that are created from \a series
489 */
481 */
490 void QLegend::deleteMarkers(QSeries *series)
482 void QLegend::deleteMarkers(QSeries *series)
491 {
483 {
492 // Search all markers that belong to given series and delete them.
484 // Search all markers that belong to given series and delete them.
493 foreach (LegendMarker *m, m_markers) {
485 foreach (LegendMarker *m, m_markers) {
494 if (m->series() == series) {
486 if (m->series() == series) {
495 m_markers.removeOne(m);
487 m_markers.removeOne(m);
496 delete m;
488 delete m;
497 }
489 }
498 }
490 }
499 }
491 }
500
492
501 /*!
493 /*!
502 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
494 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
503 If items don't fit, sets the visibility of scroll buttons accordingly.
495 If items don't fit, sets the visibility of scroll buttons accordingly.
504 Causes legend to be resized.
496 Causes legend to be resized.
505 */
497 */
506 void QLegend::updateLayout()
498 void QLegend::updateLayout()
507 {
499 {
508 // Calculate layout for markers and text
500 // Calculate layout for markers and text
509 if (m_markers.count() <= 0) {
501 if (m_markers.count() <= 0) {
510 // Nothing to do
502 // Nothing to do
511 return;
503 return;
512 }
504 }
513
505
514 // Find out widest item.
506 // Find out widest item.
515 QSizeF markerMaxSize = maximumMarkerSize();
507 QSizeF markerMaxSize = maximumMarkerSize();
516 checkFirstMarkerBounds();
508 checkFirstMarkerBounds();
517
509
518 // Use max height as scroll button size
510 // Use max height as scroll button size
519 rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height()));
511 rescaleScrollButtons(QSize(markerMaxSize.height() ,markerMaxSize.height()));
520
512
521 qreal totalWidth = 0;
513 qreal totalWidth = 0;
522 qreal totalHeight = 0;
514 qreal totalHeight = 0;
523 switch (m_alignment)
515 switch (m_alignment)
524 {
516 {
525 // Both cases organise items horizontally
517 // Both cases organise items horizontally
526 case QLegend::AlignmentBottom:
518 case QLegend::AlignmentBottom:
527 case QLegend::AlignmentTop: {
519 case QLegend::AlignmentTop: {
528
520
529 qreal xStep = markerMaxSize.width();
521 qreal xStep = markerMaxSize.width();
530 qreal x = m_pos.x() + m_margin;
522 qreal x = m_pos.x() + m_margin;
531 qreal y = m_pos.y() + m_margin;
523 qreal y = m_pos.y() + m_margin;
532 int column = 0;
524 int column = 0;
533 int maxColumns = 1;
525 int maxColumns = 1;
534 qreal scrollButtonWidth = 0;
526 qreal scrollButtonWidth = 0;
535
527
536 // Set correct visibility for scroll scrollbuttons
528 // Set correct visibility for scroll scrollbuttons
537 if (scrollButtonsVisible()) {
529 if (scrollButtonsVisible()) {
538 m_scrollButtonLeft->setVisible(true);
530 m_scrollButtonLeft->setVisible(true);
539 m_scrollButtonRight->setVisible(true);
531 m_scrollButtonRight->setVisible(true);
540 // scrollbuttons visible, so add their width to total width
532 // scrollbuttons visible, so add their width to total width
541 totalWidth += (m_scrollButtonLeft->boundingRect().width() + m_margin) * 2;
533 totalWidth += (m_scrollButtonLeft->boundingRect().width() + m_margin) * 2;
542 scrollButtonWidth = m_scrollButtonLeft->boundingRect().width() + m_margin;
534 scrollButtonWidth = m_scrollButtonLeft->boundingRect().width() + m_margin;
543 // start position changes by scrollbutton width
535 // start position changes by scrollbutton width
544 x += scrollButtonWidth;
536 x += scrollButtonWidth;
545 } else {
537 } else {
546 m_scrollButtonLeft->setVisible(false);
538 m_scrollButtonLeft->setVisible(false);
547 m_scrollButtonRight->setVisible(false);
539 m_scrollButtonRight->setVisible(false);
548 }
540 }
549 m_scrollButtonUp->setVisible(false);
541 m_scrollButtonUp->setVisible(false);
550 m_scrollButtonDown->setVisible(false);
542 m_scrollButtonDown->setVisible(false);
551
543
552 for (int i=0; i < m_markers.count(); i++) {
544 for (int i=0; i < m_markers.count(); i++) {
553 LegendMarker *m = m_markers.at(i);
545 LegendMarker *m = m_markers.at(i);
554 if (i < mFirstMarker) {
546 if (i < mFirstMarker) {
555 // Markers before first are not visible.
547 // Markers before first are not visible.
556 m->setVisible(false);
548 m->setVisible(false);
557 } else {
549 } else {
558 if ((x + xStep + scrollButtonWidth + m_margin) > (m_pos.x() + m_maximumSize.width())) {
550 if ((x + xStep + scrollButtonWidth + m_margin) > (m_pos.x() + m_maximumSize.width())) {
559 // This marker would go outside legend rect.
551 // This marker would go outside legend rect.
560 m->setVisible(false);
552 m->setVisible(false);
561 } else {
553 } else {
562 // This marker is ok
554 // This marker is ok
563 m->setVisible(true);
555 m->setVisible(true);
564 m->setPos(x, y);
556 m->setPos(x, y);
565 x += xStep;
557 x += xStep;
566 column++;
558 column++;
567 }
559 }
568 }
560 }
569 maxColumns = column;
561 maxColumns = column;
570 }
562 }
571
563
572 m_scrollButtonLeft->setPos(m_pos.x() + m_margin, y);
564 m_scrollButtonLeft->setPos(m_pos.x() + m_margin, y);
573 m_scrollButtonRight->setPos(x + m_margin, y);
565 m_scrollButtonRight->setPos(x + m_margin, y);
574
566
575 totalWidth += maxColumns * markerMaxSize.width() + m_margin * 2;
567 totalWidth += maxColumns * markerMaxSize.width() + m_margin * 2;
576 totalHeight = markerMaxSize.height() + m_margin * 2;
568 totalHeight = markerMaxSize.height() + m_margin * 2;
577
569
578 break;
570 break;
579 }
571 }
580 // Both cases organize items vertically
572 // Both cases organize items vertically
581 case QLegend::AlignmentLeft:
573 case QLegend::AlignmentLeft:
582 case QLegend::AlignmentRight: {
574 case QLegend::AlignmentRight: {
583 qreal yStep = markerMaxSize.height();
575 qreal yStep = markerMaxSize.height();
584 qreal x = m_pos.x() + m_margin;
576 qreal x = m_pos.x() + m_margin;
585 qreal y = m_pos.y() + m_margin;
577 qreal y = m_pos.y() + m_margin;
586 int row = 1;
578 int row = 1;
587 int maxRows = 1;
579 int maxRows = 1;
588 qreal scrollButtonHeight = 0;
580 qreal scrollButtonHeight = 0;
589
581
590 // Set correct visibility for scroll scrollbuttons
582 // Set correct visibility for scroll scrollbuttons
591 if (scrollButtonsVisible()) {
583 if (scrollButtonsVisible()) {
592 m_scrollButtonUp->setVisible(true);
584 m_scrollButtonUp->setVisible(true);
593 m_scrollButtonDown->setVisible(true);
585 m_scrollButtonDown->setVisible(true);
594 totalHeight += (m_scrollButtonUp->boundingRect().height() + m_margin) * 2; // scrollbuttons visible, so add their height to total height
586 totalHeight += (m_scrollButtonUp->boundingRect().height() + m_margin) * 2; // scrollbuttons visible, so add their height to total height
595 scrollButtonHeight = m_scrollButtonUp->boundingRect().height();
587 scrollButtonHeight = m_scrollButtonUp->boundingRect().height();
596 y += scrollButtonHeight + m_margin; // start position changes by scrollbutton height
588 y += scrollButtonHeight + m_margin; // start position changes by scrollbutton height
597 } else {
589 } else {
598 m_scrollButtonUp->setVisible(false);
590 m_scrollButtonUp->setVisible(false);
599 m_scrollButtonDown->setVisible(false);
591 m_scrollButtonDown->setVisible(false);
600 }
592 }
601 m_scrollButtonLeft->setVisible(false);
593 m_scrollButtonLeft->setVisible(false);
602 m_scrollButtonRight->setVisible(false);
594 m_scrollButtonRight->setVisible(false);
603
595
604 for (int i=0; i < m_markers.count(); i++) {
596 for (int i=0; i < m_markers.count(); i++) {
605 LegendMarker* m = m_markers.at(i);
597 LegendMarker* m = m_markers.at(i);
606 if (i < mFirstMarker) {
598 if (i < mFirstMarker) {
607 // Markers before first are not visible.
599 // Markers before first are not visible.
608 m->setVisible(false);
600 m->setVisible(false);
609 } else {
601 } else {
610 if ((y + yStep + scrollButtonHeight) > (m_pos.y() + m_maximumSize.height())) {
602 if ((y + yStep + scrollButtonHeight) > (m_pos.y() + m_maximumSize.height())) {
611 // This marker would go outside legend rect.
603 // This marker would go outside legend rect.
612 m->setVisible(false);
604 m->setVisible(false);
613 } else {
605 } else {
614 // This marker is ok
606 // This marker is ok
615 m->setVisible(true);
607 m->setVisible(true);
616 m->setPos(x, y);
608 m->setPos(x, y);
617 y += yStep;
609 y += yStep;
618 row++;
610 row++;
619 }
611 }
620 }
612 }
621 maxRows = row;
613 maxRows = row;
622 }
614 }
623
615
624 m_scrollButtonUp->setPos(m_pos.x() + m_margin, m_pos.y() + m_margin);
616 m_scrollButtonUp->setPos(m_pos.x() + m_margin, m_pos.y() + m_margin);
625 m_scrollButtonDown->setPos(m_pos.x() + m_margin, y + m_margin);
617 m_scrollButtonDown->setPos(m_pos.x() + m_margin, y + m_margin);
626
618
627 totalWidth += markerMaxSize.width() + m_margin * 2;
619 totalWidth += markerMaxSize.width() + m_margin * 2;
628 totalHeight = maxRows * markerMaxSize.height() + m_margin * 4 + scrollButtonHeight; // TODO: check this
620 totalHeight = maxRows * markerMaxSize.height() + m_margin * 4 + scrollButtonHeight; // TODO: check this
629 break;
621 break;
630 }
622 }
631 default: {
623 default: {
632 break;
624 break;
633 }
625 }
634 }
626 }
635
627
636 m_size.setWidth(totalWidth);
628 m_size.setWidth(totalWidth);
637 m_size.setHeight(totalHeight);
629 m_size.setHeight(totalHeight);
638
630
639 update();
631 update();
640 }
632 }
641
633
642 /*!
634 /*!
643 \internal Sets the size of scroll buttons to \a size
635 \internal Sets the size of scroll buttons to \a size
644 */
636 */
645 void QLegend::rescaleScrollButtons(const QSize &size)
637 void QLegend::rescaleScrollButtons(const QSize &size)
646 {
638 {
647 QPolygonF left;
639 QPolygonF left;
648 left << QPointF(size.width(), 0) << QPointF(0, size.height() / 2) << QPointF(size.width(), size.height());
640 left << QPointF(size.width(), 0) << QPointF(0, size.height() / 2) << QPointF(size.width(), size.height());
649 QPolygonF right;
641 QPolygonF right;
650 right << QPointF(0, 0) << QPointF(size.width(), size.height() / 2) << QPointF(0, size.height());
642 right << QPointF(0, 0) << QPointF(size.width(), size.height() / 2) << QPointF(0, size.height());
651 QPolygonF up;
643 QPolygonF up;
652 up << QPointF(0, size.height()) << QPointF(size.width() / 2,0) << QPointF(size.width(), size.height());
644 up << QPointF(0, size.height()) << QPointF(size.width() / 2,0) << QPointF(size.width(), size.height());
653 QPolygonF down;
645 QPolygonF down;
654 down << QPointF(0, 0) << QPointF(size.width() / 2, size.height()) << QPointF(size.width(), 0);
646 down << QPointF(0, 0) << QPointF(size.width() / 2, size.height()) << QPointF(size.width(), 0);
655
647
656 m_scrollButtonLeft->setPolygon(left);
648 m_scrollButtonLeft->setPolygon(left);
657 m_scrollButtonRight->setPolygon(right);
649 m_scrollButtonRight->setPolygon(right);
658 m_scrollButtonUp->setPolygon(up);
650 m_scrollButtonUp->setPolygon(up);
659 m_scrollButtonDown->setPolygon(down);
651 m_scrollButtonDown->setPolygon(down);
660 }
652 }
661
653
662 /*!
654 /*!
663 \internal Finds out maximum size of single marker. Marker sizes depend on series names.
655 \internal Finds out maximum size of single marker. Marker sizes depend on series names.
664 */
656 */
665 QSizeF QLegend::maximumMarkerSize()
657 QSizeF QLegend::maximumMarkerSize()
666 {
658 {
667 QSizeF max(0,0);
659 QSizeF max(0,0);
668 foreach (LegendMarker* m, m_markers) {
660 foreach (LegendMarker* m, m_markers) {
669 if (m->boundingRect().width() > max.width())
661 if (m->boundingRect().width() > max.width())
670 max.setWidth(m->boundingRect().width());
662 max.setWidth(m->boundingRect().width());
671 if (m->boundingRect().height() > max.height())
663 if (m->boundingRect().height() > max.height())
672 max.setHeight(m->boundingRect().height());
664 max.setHeight(m->boundingRect().height());
673 }
665 }
674 return max;
666 return max;
675 }
667 }
676
668
677 /*!
669 /*!
678 \internal Checks that first marker is in acceptable bounds. Bounds range from 0 to (maximum number of markers - visible markers)
670 \internal Checks that first marker is in acceptable bounds. Bounds range from 0 to (maximum number of markers - visible markers)
679 If scrollbuttons are visible, they affect the number of visible markers.
671 If scrollbuttons are visible, they affect the number of visible markers.
680 */
672 */
681 void QLegend::checkFirstMarkerBounds()
673 void QLegend::checkFirstMarkerBounds()
682 {
674 {
683 if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) {
675 if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) {
684 // Bounds limited by height.
676 // Bounds limited by height.
685 int max;
677 int max;
686 if (scrollButtonsVisible()) {
678 if (scrollButtonsVisible()) {
687 max = (m_maximumSize.height() - m_scrollButtonLeft->boundingRect().height() * 2 - m_margin * 4) / maximumMarkerSize().height();
679 max = (m_maximumSize.height() - m_scrollButtonLeft->boundingRect().height() * 2 - m_margin * 4) / maximumMarkerSize().height();
688 } else {
680 } else {
689 max = m_maximumSize.height() / maximumMarkerSize().height();
681 max = m_maximumSize.height() / maximumMarkerSize().height();
690 }
682 }
691
683
692 if (mFirstMarker > m_markers.count() - max)
684 if (mFirstMarker > m_markers.count() - max)
693 mFirstMarker = m_markers.count() - max;
685 mFirstMarker = m_markers.count() - max;
694 } else {
686 } else {
695 // Bounds limited by width
687 // Bounds limited by width
696 int max;
688 int max;
697 if (scrollButtonsVisible()) {
689 if (scrollButtonsVisible()) {
698 max = (m_maximumSize.width() - m_scrollButtonLeft->boundingRect().width() * 2 - m_margin*4) / maximumMarkerSize().width();
690 max = (m_maximumSize.width() - m_scrollButtonLeft->boundingRect().width() * 2 - m_margin*4) / maximumMarkerSize().width();
699 } else {
691 } else {
700 max = m_maximumSize.width() / maximumMarkerSize().width();
692 max = m_maximumSize.width() / maximumMarkerSize().width();
701 }
693 }
702
694
703 if (mFirstMarker > m_markers.count() - max)
695 if (mFirstMarker > m_markers.count() - max)
704 mFirstMarker = m_markers.count() - max;
696 mFirstMarker = m_markers.count() - max;
705 }
697 }
706
698
707 if (mFirstMarker < 0)
699 if (mFirstMarker < 0)
708 mFirstMarker = 0;
700 mFirstMarker = 0;
709 }
701 }
710
702
711 /*!
703 /*!
712 \internal Helper function. Visibility of scroll buttons isn't quite obvious, so helper function clarifies the logic.
704 \internal Helper function. Visibility of scroll buttons isn't quite obvious, so helper function clarifies the logic.
713 */
705 */
714 bool QLegend::scrollButtonsVisible()
706 bool QLegend::scrollButtonsVisible()
715 {
707 {
716 // Just a helper to clarify, what the magic below means :)
708 // Just a helper to clarify, what the magic below means :)
717 if ((m_alignment == QLegend::AlignmentTop) || (m_alignment == QLegend::AlignmentBottom)) {
709 if ((m_alignment == QLegend::AlignmentTop) || (m_alignment == QLegend::AlignmentBottom)) {
718 return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width());
710 return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width());
719 } else if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) {
711 } else if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) {
720 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
712 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
721 }
713 }
722
714
723 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
715 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
724 }
716 }
725
717
726 #include "moc_qlegend.cpp"
718 #include "moc_qlegend.cpp"
727
719
728 QTCOMMERCIALCHART_END_NAMESPACE
720 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now