##// END OF EJS Templates
fixed bug in legend name drawing
sauimone -
r585:b761105be1ea
parent child
Show More
@@ -1,109 +1,121
1 #include "qchartglobal.h"
1 #include "qchartglobal.h"
2 #include "legendmarker_p.h"
2 #include "legendmarker_p.h"
3 #include <QPainter>
3 #include <QPainter>
4 #include <QGraphicsSceneEvent>
4 #include <QGraphicsSceneEvent>
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
9 : QGraphicsObject(parent)
9 : QGraphicsObject(parent)
10 ,mBoundingRect(0,0,1,1)
10 ,mBoundingRect(0,0,1,1)
11 ,mName("")
11 ,mMarkerBoundingRect(0,0,1,1)
12 ,mSeries(series)
12 ,mSeries(series)
13 ,mBarset(0)
13 ,mBarset(0)
14 ,mPieslice(0)
14 ,mPieslice(0)
15 ,mType(LegendMarkerTypeSeries)
15 ,mType(LegendMarkerTypeSeries)
16 ,mTextItem(new QGraphicsSimpleTextItem(this))
16 {
17 {
17 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 }
19 }
19
20
20 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
21 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
21 : QGraphicsObject(parent)
22 : QGraphicsObject(parent)
22 ,mBoundingRect(0,0,1,1)
23 ,mBoundingRect(0,0,1,1)
23 ,mName("")
24 ,mMarkerBoundingRect(0,0,1,1)
24 ,mSeries(series)
25 ,mSeries(series)
25 ,mBarset(barset)
26 ,mBarset(barset)
26 ,mPieslice(0)
27 ,mPieslice(0)
27 ,mType(LegendMarkerTypeBarset)
28 ,mType(LegendMarkerTypeBarset)
29 ,mTextItem(new QGraphicsSimpleTextItem(this))
28 {
30 {
29 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
31 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
30 }
32 }
31
33
32 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
34 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
33 : QGraphicsObject(parent)
35 : QGraphicsObject(parent)
34 ,mBoundingRect(0,0,1,1)
36 ,mBoundingRect(0,0,1,1)
35 ,mName("")
37 ,mMarkerBoundingRect(0,0,1,1)
36 ,mSeries(series)
38 ,mSeries(series)
37 ,mBarset(0)
39 ,mBarset(0)
38 ,mPieslice(pieslice)
40 ,mPieslice(pieslice)
39 ,mType(LegendMarkerTypePieslice)
41 ,mType(LegendMarkerTypePieslice)
42 ,mTextItem(new QGraphicsSimpleTextItem(this))
40 {
43 {
41 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
44 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
42 }
45 }
43
46
44 void LegendMarker::setBoundingRect(const QRectF rect)
47 void LegendMarker::setBoundingRect(const QRectF rect)
45 {
48 {
46 mBoundingRect = rect;
49 mBoundingRect = rect;
50 // Calculate Marker pos
51
52 // TODO: remove hard coding. 5 is marigin around marker
53 QSizeF markerSize(10,10);
54 qreal x = mBoundingRect.x() + 5;
55 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
56 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
57
58 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
47 }
59 }
48
60
49 void LegendMarker::setBrush(const QBrush brush)
61 void LegendMarker::setBrush(const QBrush brush)
50 {
62 {
51 mBrush = brush;
63 mBrush = brush;
52 }
64 }
53
65
54 QBrush LegendMarker::brush() const
66 QBrush LegendMarker::brush() const
55 {
67 {
56 return mBrush;
68 return mBrush;
57 }
69 }
58
70
59 void LegendMarker::setName(const QString name)
71 void LegendMarker::setName(const QString name)
60 {
72 {
61 mName = name;
73 mTextItem.setText(name);
62 }
74 }
63
75
64 QString LegendMarker::name() const
76 QString LegendMarker::name() const
65 {
77 {
66 return mName;
78 return mTextItem.text();
67 }
79 }
68
80
69 QSeries* LegendMarker::series() const
81 QSeries* LegendMarker::series() const
70 {
82 {
71 return mSeries;
83 return mSeries;
72 }
84 }
73
85
74 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
86 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
75 {
87 {
76 painter->setBrush(mBrush);
88 painter->setBrush(mBrush);
77 painter->drawRect(mBoundingRect);
89 painter->drawRect(mMarkerBoundingRect);
78 }
90 }
79
91
80 QRectF LegendMarker::boundingRect() const
92 QRectF LegendMarker::boundingRect() const
81 {
93 {
82 return mBoundingRect;
94 return mBoundingRect;
83 }
95 }
84
96
85 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
97 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
86 {
98 {
87 switch (mType)
99 switch (mType)
88 {
100 {
89 case LegendMarkerTypeSeries: {
101 case LegendMarkerTypeSeries: {
90 emit clicked(mSeries,event->button());
102 emit clicked(mSeries,event->button());
91 break;
103 break;
92 }
104 }
93 case LegendMarkerTypeBarset: {
105 case LegendMarkerTypeBarset: {
94 emit clicked(mBarset,event->button());
106 emit clicked(mBarset,event->button());
95 break;
107 break;
96 }
108 }
97 case LegendMarkerTypePieslice: {
109 case LegendMarkerTypePieslice: {
98 emit clicked(mPieslice,event->button());
110 emit clicked(mPieslice,event->button());
99 break;
111 break;
100 }
112 }
101 default: {
113 default: {
102 break;
114 break;
103 }
115 }
104 }
116 }
105 }
117 }
106
118
107 #include "moc_legendmarker_p.cpp"
119 #include "moc_legendmarker_p.cpp"
108
120
109 QTCOMMERCIALCHART_END_NAMESPACE
121 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,66
1 #ifndef LEGENDMARKER_P_H
1 #ifndef LEGENDMARKER_P_H
2 #define LEGENDMARKER_P_H
2 #define LEGENDMARKER_P_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
5 #include <QGraphicsObject>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QSeries;
10 class QSeries;
11 class QBarSet;
11 class QBarSet;
12 class QPieSlice;
12 class QPieSlice;
13
13
14 // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse...
14 // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse...
15 class LegendMarker : public QGraphicsObject
15 class LegendMarker : public QGraphicsObject
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18
18
19 enum LegendMarkerType {
19 enum LegendMarkerType {
20 LegendMarkerTypeSeries,
20 LegendMarkerTypeSeries,
21 LegendMarkerTypeBarset,
21 LegendMarkerTypeBarset,
22 LegendMarkerTypePieslice
22 LegendMarkerTypePieslice
23 };
23 };
24
24
25 public:
25 public:
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
28 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
28 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
29 void setBoundingRect(const QRectF rect);
29 void setBoundingRect(const QRectF rect);
30
30
31 void setBrush(const QBrush brush);
31 void setBrush(const QBrush brush);
32 QBrush brush() const;
32 QBrush brush() const;
33
33
34 void setName(const QString name);
34 void setName(const QString name);
35 QString name() const;
35 QString name() const;
36
36
37 QSeries* series() const;
37 QSeries* series() const;
38
38
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
40
40
41 QRectF boundingRect() const;
41 QRectF boundingRect() const;
42
42
43 public:
43 public:
44 // From QGraphicsObject
44 // From QGraphicsObject
45 void mousePressEvent(QGraphicsSceneMouseEvent *event);
45 void mousePressEvent(QGraphicsSceneMouseEvent *event);
46
46
47 Q_SIGNALS:
47 Q_SIGNALS:
48 void clicked(QSeries* series, Qt::MouseButton button);
48 void clicked(QSeries* series, Qt::MouseButton button);
49 void clicked(QBarSet* barset, Qt::MouseButton button);
49 void clicked(QBarSet* barset, Qt::MouseButton button);
50 void clicked(QPieSlice* pieslice, Qt::MouseButton button);
50 void clicked(QPieSlice* pieslice, Qt::MouseButton button);
51
51
52 private:
52 private:
53 QRectF mBoundingRect;
53 QRectF mBoundingRect;
54 QRectF mMarkerBoundingRect;
54 QBrush mBrush;
55 QBrush mBrush;
55 QString mName;
56 QSeries* mSeries;
56 QSeries* mSeries;
57 QBarSet* mBarset;
57 QBarSet* mBarset;
58 QPieSlice* mPieslice;
58 QPieSlice* mPieslice;
59
59
60 LegendMarkerType mType;
60 LegendMarkerType mType;
61 QGraphicsSimpleTextItem mTextItem;
62
61 };
63 };
62
64
63 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
64 #endif // LEGENDMARKER_P_H
66 #endif // LEGENDMARKER_P_H
@@ -1,356 +1,355
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "chartpresenter_p.h"
4 #include "chartpresenter_p.h"
5 #include "chartdataset_p.h"
5 #include "chartdataset_p.h"
6 #include <QGraphicsScene>
6 #include <QGraphicsScene>
7 #include <QGraphicsSceneResizeEvent>
7 #include <QGraphicsSceneResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 /*!
12 /*!
13 \enum QChart::ChartTheme
13 \enum QChart::ChartTheme
14
14
15 This enum describes the theme used by the chart.
15 This enum describes the theme used by the chart.
16
16
17 \value ChartThemeDefault Follows the GUI style of the Operating System
17 \value ChartThemeDefault Follows the GUI style of the Operating System
18 \value ChartThemeVanilla
18 \value ChartThemeVanilla
19 \value ChartThemeIcy
19 \value ChartThemeIcy
20 \value ChartThemeGrayscale
20 \value ChartThemeGrayscale
21 \value ChartThemeScientific
21 \value ChartThemeScientific
22 \value ChartThemeBlueCerulean
22 \value ChartThemeBlueCerulean
23 \value ChartThemeLight
23 \value ChartThemeLight
24 \value ChartThemeCount Not really a theme; the total count of themes.
24 \value ChartThemeCount Not really a theme; the total count of themes.
25 */
25 */
26
26
27 /*!
27 /*!
28 \enum QChart::AnimationOption
28 \enum QChart::AnimationOption
29
29
30 For enabling/disabling animations. Defaults to NoAnimation.
30 For enabling/disabling animations. Defaults to NoAnimation.
31
31
32 \value NoAnimation
32 \value NoAnimation
33 \value GridAxisAnimations
33 \value GridAxisAnimations
34 \value SeriesAnimations
34 \value SeriesAnimations
35 \value AllAnimations
35 \value AllAnimations
36 */
36 */
37
37
38 /*!
38 /*!
39 \class QChart
39 \class QChart
40 \brief QtCommercial chart API.
40 \brief QtCommercial chart API.
41
41
42 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
42 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
43 representation of different types of QChartSeries and other chart related objects like
43 representation of different types of QChartSeries and other chart related objects like
44 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
44 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
45 convenience class QChartView instead of QChart.
45 convenience class QChartView instead of QChart.
46 \sa QChartView
46 \sa QChartView
47 */
47 */
48
48
49 /*!
49 /*!
50 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
50 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
51 */
51 */
52 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
52 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
53 m_backgroundItem(0),
53 m_backgroundItem(0),
54 m_titleItem(0),
54 m_titleItem(0),
55 m_legend(new QLegend(this)),
55 m_legend(new QLegend(this)),
56 m_dataset(new ChartDataSet(this)),
56 m_dataset(new ChartDataSet(this)),
57 m_presenter(new ChartPresenter(this,m_dataset))
57 m_presenter(new ChartPresenter(this,m_dataset))
58 {
58 {
59 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
59 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
60 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
60 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
61 }
61 }
62
62
63 /*!
63 /*!
64 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
64 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
65 */
65 */
66 QChart::~QChart()
66 QChart::~QChart()
67 {
67 {
68 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
68 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
69 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
69 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
70 }
70 }
71
71
72 /*!
72 /*!
73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 the y axis).
75 the y axis).
76 */
76 */
77 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
77 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
78 {
78 {
79 m_dataset->addSeries(series, axisY);
79 m_dataset->addSeries(series, axisY);
80 }
80 }
81
81
82 /*!
82 /*!
83 Removes the \a series specified in a perameter from the QChartView.
83 Removes the \a series specified in a perameter from the QChartView.
84 It releses its ownership of the specified QChartSeries object.
84 It releses its ownership of the specified QChartSeries object.
85 It does not delete the pointed QChartSeries data object
85 It does not delete the pointed QChartSeries data object
86 \sa addSeries(), removeAllSeries()
86 \sa addSeries(), removeAllSeries()
87 */
87 */
88 void QChart::removeSeries(QSeries* series)
88 void QChart::removeSeries(QSeries* series)
89 {
89 {
90 m_dataset->removeSeries(series);
90 m_dataset->removeSeries(series);
91 }
91 }
92
92
93 /*!
93 /*!
94 Removes all the QChartSeries that have been added to the QChartView
94 Removes all the QChartSeries that have been added to the QChartView
95 It also deletes the pointed QChartSeries data objects
95 It also deletes the pointed QChartSeries data objects
96 \sa addSeries(), removeSeries()
96 \sa addSeries(), removeSeries()
97 */
97 */
98 void QChart::removeAllSeries()
98 void QChart::removeAllSeries()
99 {
99 {
100 m_dataset->removeAllSeries();
100 m_dataset->removeAllSeries();
101 }
101 }
102
102
103 /*!
103 /*!
104 Sets the \a brush that is used for painting the background of the chart area.
104 Sets the \a brush that is used for painting the background of the chart area.
105 */
105 */
106 void QChart::setChartBackgroundBrush(const QBrush& brush)
106 void QChart::setChartBackgroundBrush(const QBrush& brush)
107 {
107 {
108 createChartBackgroundItem();
108 createChartBackgroundItem();
109 m_backgroundItem->setBrush(brush);
109 m_backgroundItem->setBrush(brush);
110 m_backgroundItem->update();
110 m_backgroundItem->update();
111 }
111 }
112
112
113 /*!
113 /*!
114 Sets the \a pen that is used for painting the background of the chart area.
114 Sets the \a pen that is used for painting the background of the chart area.
115 */
115 */
116 void QChart::setChartBackgroundPen(const QPen& pen)
116 void QChart::setChartBackgroundPen(const QPen& pen)
117 {
117 {
118 createChartBackgroundItem();
118 createChartBackgroundItem();
119 m_backgroundItem->setPen(pen);
119 m_backgroundItem->setPen(pen);
120 m_backgroundItem->update();
120 m_backgroundItem->update();
121 }
121 }
122
122
123 /*!
123 /*!
124 Sets the chart \a title. The description text that is drawn above the chart.
124 Sets the chart \a title. The description text that is drawn above the chart.
125 */
125 */
126 void QChart::setChartTitle(const QString& title)
126 void QChart::setChartTitle(const QString& title)
127 {
127 {
128 createChartTitleItem();
128 createChartTitleItem();
129 m_titleItem->setText(title);
129 m_titleItem->setText(title);
130 updateLayout();
130 updateLayout();
131 }
131 }
132
132
133 /*!
133 /*!
134 Returns the chart title. The description text that is drawn above the chart.
134 Returns the chart title. The description text that is drawn above the chart.
135 */
135 */
136 QString QChart::chartTitle() const
136 QString QChart::chartTitle() const
137 {
137 {
138 if(m_titleItem)
138 if(m_titleItem)
139 return m_titleItem->text();
139 return m_titleItem->text();
140 else
140 else
141 return QString();
141 return QString();
142 }
142 }
143
143
144 /*!
144 /*!
145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
146 */
146 */
147 void QChart::setChartTitleFont(const QFont& font)
147 void QChart::setChartTitleFont(const QFont& font)
148 {
148 {
149 createChartTitleItem();
149 createChartTitleItem();
150 m_titleItem->setFont(font);
150 m_titleItem->setFont(font);
151 updateLayout();
151 updateLayout();
152 }
152 }
153
153
154 /*!
154 /*!
155 Sets the \a brush used for rendering the title text.
155 Sets the \a brush used for rendering the title text.
156 */
156 */
157 void QChart::setChartTitleBrush(const QBrush &brush)
157 void QChart::setChartTitleBrush(const QBrush &brush)
158 {
158 {
159 createChartTitleItem();
159 createChartTitleItem();
160 m_titleItem->setBrush(brush);
160 m_titleItem->setBrush(brush);
161 updateLayout();
161 updateLayout();
162 }
162 }
163
163
164 /*!
164 /*!
165 Returns the brush used for rendering the title text.
165 Returns the brush used for rendering the title text.
166 */
166 */
167 QBrush QChart::chartTitleBrush()
167 QBrush QChart::chartTitleBrush()
168 {
168 {
169 createChartTitleItem();
169 createChartTitleItem();
170 return m_titleItem->brush();
170 return m_titleItem->brush();
171 }
171 }
172
172
173 void QChart::createChartBackgroundItem()
173 void QChart::createChartBackgroundItem()
174 {
174 {
175 if(!m_backgroundItem) {
175 if(!m_backgroundItem) {
176 m_backgroundItem = new QGraphicsRectItem(this);
176 m_backgroundItem = new QGraphicsRectItem(this);
177 m_backgroundItem->setPen(Qt::NoPen);
177 m_backgroundItem->setPen(Qt::NoPen);
178 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
178 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
179 }
179 }
180 }
180 }
181
181
182 void QChart::createChartTitleItem()
182 void QChart::createChartTitleItem()
183 {
183 {
184 if(!m_titleItem) {
184 if(!m_titleItem) {
185 m_titleItem = new QGraphicsSimpleTextItem(this);
185 m_titleItem = new QGraphicsSimpleTextItem(this);
186 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
186 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
187 }
187 }
188 }
188 }
189
189
190 /*!
190 /*!
191 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
191 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
192 \sa setMargin()
192 \sa setMargin()
193 */
193 */
194 int QChart::margin() const
194 int QChart::margin() const
195 {
195 {
196 return m_presenter->margin();
196 return m_presenter->margin();
197 }
197 }
198
198
199 /*!
199 /*!
200 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
200 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
201 \sa margin()
201 \sa margin()
202 */
202 */
203 void QChart::setMargin(int margin)
203 void QChart::setMargin(int margin)
204 {
204 {
205 m_presenter->setMargin(margin);
205 m_presenter->setMargin(margin);
206 updateLayout();
206 updateLayout();
207 }
207 }
208
208
209 /*!
209 /*!
210 Sets the \a theme used by the chart for rendering the graphical representation of the data
210 Sets the \a theme used by the chart for rendering the graphical representation of the data
211 \sa ChartTheme, chartTheme()
211 \sa ChartTheme, chartTheme()
212 */
212 */
213 void QChart::setChartTheme(QChart::ChartTheme theme)
213 void QChart::setChartTheme(QChart::ChartTheme theme)
214 {
214 {
215 m_presenter->setChartTheme(theme);
215 m_presenter->setChartTheme(theme);
216 }
216 }
217
217
218 /*!
218 /*!
219 Returns the theme enum used by the chart.
219 Returns the theme enum used by the chart.
220 \sa ChartTheme, setChartTheme()
220 \sa ChartTheme, setChartTheme()
221 */
221 */
222 QChart::ChartTheme QChart::chartTheme() const
222 QChart::ChartTheme QChart::chartTheme() const
223 {
223 {
224 return m_presenter->chartTheme();
224 return m_presenter->chartTheme();
225 }
225 }
226
226
227 /*!
227 /*!
228 Zooms in the view by a factor of 2
228 Zooms in the view by a factor of 2
229 */
229 */
230 void QChart::zoomIn()
230 void QChart::zoomIn()
231 {
231 {
232 m_presenter->zoomIn();
232 m_presenter->zoomIn();
233 }
233 }
234
234
235 /*!
235 /*!
236 Zooms in the view to a maximum level at which \a rect is still fully visible.
236 Zooms in the view to a maximum level at which \a rect is still fully visible.
237 */
237 */
238 void QChart::zoomIn(const QRectF& rect)
238 void QChart::zoomIn(const QRectF& rect)
239 {
239 {
240
240
241 if(!rect.isValid()) return;
241 if(!rect.isValid()) return;
242 m_presenter->zoomIn(rect);
242 m_presenter->zoomIn(rect);
243 }
243 }
244
244
245 /*!
245 /*!
246 Restores the view zoom level to the previous one.
246 Restores the view zoom level to the previous one.
247 */
247 */
248 void QChart::zoomOut()
248 void QChart::zoomOut()
249 {
249 {
250 m_presenter->zoomOut();
250 m_presenter->zoomOut();
251 }
251 }
252
252
253 /*!
253 /*!
254 Resets to the default view.
254 Resets to the default view.
255 */
255 */
256 void QChart::zoomReset()
256 void QChart::zoomReset()
257 {
257 {
258 m_presenter->zoomReset();
258 m_presenter->zoomReset();
259 }
259 }
260
260
261 /*!
261 /*!
262 Returns the pointer to the x axis object of the chart
262 Returns the pointer to the x axis object of the chart
263 */
263 */
264 QChartAxis* QChart::axisX() const
264 QChartAxis* QChart::axisX() const
265 {
265 {
266 return m_dataset->axisX();
266 return m_dataset->axisX();
267 }
267 }
268
268
269 /*!
269 /*!
270 Returns the pointer to the y axis object of the chart
270 Returns the pointer to the y axis object of the chart
271 */
271 */
272 QChartAxis* QChart::axisY() const
272 QChartAxis* QChart::axisY() const
273 {
273 {
274 return m_dataset->axisY();
274 return m_dataset->axisY();
275 }
275 }
276
276
277 /*!
277 /*!
278 Returns the legend object of the chart
278 Returns the legend object of the chart
279 */
279 */
280 QLegend* QChart::legend()
280 QLegend* QChart::legend()
281 {
281 {
282 return m_legend;
282 return m_legend;
283 }
283 }
284
284
285 /*!
285 /*!
286 Resizes and updates the chart area using the \a event data
286 Resizes and updates the chart area using the \a event data
287 */
287 */
288 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
288 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
289 {
289 {
290
290
291 m_rect = QRectF(QPoint(0,0),event->newSize());
291 m_rect = QRectF(QPoint(0,0),event->newSize());
292 updateLayout();
292 updateLayout();
293 QGraphicsWidget::resizeEvent(event);
293 QGraphicsWidget::resizeEvent(event);
294 update();
294 update();
295 }
295 }
296
296
297 /*!
297 /*!
298 Sets animation \a options for the chart
298 Sets animation \a options for the chart
299 */
299 */
300 void QChart::setAnimationOptions(AnimationOptions options)
300 void QChart::setAnimationOptions(AnimationOptions options)
301 {
301 {
302 m_presenter->setAnimationOptions(options);
302 m_presenter->setAnimationOptions(options);
303 }
303 }
304
304
305 /*!
305 /*!
306 Returns animation options for the chart
306 Returns animation options for the chart
307 */
307 */
308 QChart::AnimationOptions QChart::animationOptions() const
308 QChart::AnimationOptions QChart::animationOptions() const
309 {
309 {
310 return m_presenter->animationOptions();
310 return m_presenter->animationOptions();
311 }
311 }
312
312
313 void QChart::scroll(int dx,int dy)
313 void QChart::scroll(int dx,int dy)
314 {
314 {
315 //temporary
315 //temporary
316 if(dx>0)
316 if(dx>0)
317 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
317 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
318 if(dx<0)
318 if(dx<0)
319 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
319 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
320 if(dy>0)
320 if(dy>0)
321 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
321 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
322 if(dy<0)
322 if(dy<0)
323 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
323 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
324 }
324 }
325
325
326 void QChart::updateLayout()
326 void QChart::updateLayout()
327 {
327 {
328 if(!m_rect.isValid()) return;
328 if(!m_rect.isValid()) return;
329
329
330 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
330 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
331
331
332 // recalculate title position
332 // recalculate title position
333 if (m_titleItem) {
333 if (m_titleItem) {
334 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
334 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
335 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
335 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
336 }
336 }
337
337
338 //recalculate background gradient
338 //recalculate background gradient
339 if (m_backgroundItem) {
339 if (m_backgroundItem) {
340 m_backgroundItem->setRect(rect);
340 m_backgroundItem->setRect(rect);
341 }
341 }
342
342
343 // recalculate legend position
343 // recalculate legend position
344 // TODO: better layout
344 // TODO: better layout
345 if (m_legend) {
345 if (m_legend) {
346 QRectF boundingRect(m_rect.adjusted(margin(),
346 QRectF boundingRect(m_rect.adjusted(margin(),
347 rect.height() + margin() + margin()/2 - m_legend->minimumSize().height()/2,
347 rect.height() + margin() + margin()/2,
348 -margin(),
348 -margin(),
349 -margin()/2 + m_legend->minimumSize().height()/2));
349 -margin()/2 + m_legend->minimumSize().height()));
350 m_legend->handleGeometryChanged(boundingRect);
350 m_legend->handleGeometryChanged(boundingRect);
351 qDebug() << "legend rect:" << m_legend->boundingRect();
352 }
351 }
353 }
352 }
354 #include "moc_qchart.cpp"
353 #include "moc_qchart.cpp"
355
354
356 QTCOMMERCIALCHART_END_NAMESPACE
355 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,232 +1,219
1 #include "qchartglobal.h"
1 #include "qchartglobal.h"
2 #include "qlegend.h"
2 #include "qlegend.h"
3 #include "qseries.h"
3 #include "qseries.h"
4 #include "legendmarker_p.h"
4 #include "legendmarker_p.h"
5 #include "qxyseries.h"
5 #include "qxyseries.h"
6 #include "qlineseries.h"
6 #include "qlineseries.h"
7 #include "qareaseries.h"
7 #include "qareaseries.h"
8 #include "qscatterseries.h"
8 #include "qscatterseries.h"
9 #include "qsplineseries.h"
9 #include "qsplineseries.h"
10 #include "qbarseries.h"
10 #include "qbarseries.h"
11 #include "qstackedbarseries.h"
11 #include "qstackedbarseries.h"
12 #include "qpercentbarseries.h"
12 #include "qpercentbarseries.h"
13 #include "qbarset.h"
13 #include "qbarset.h"
14 #include "qpieseries.h"
14 #include "qpieseries.h"
15 #include "qpieslice.h"
15 #include "qpieslice.h"
16 #include <QPainter>
16 #include <QPainter>
17 #include <QPen>
17 #include <QPen>
18
18
19 #include <QGraphicsSceneEvent>
19 #include <QGraphicsSceneEvent>
20
20
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22
22
23 QLegend::QLegend(QGraphicsItem *parent)
23 QLegend::QLegend(QGraphicsItem *parent)
24 : QGraphicsObject(parent)
24 : QGraphicsObject(parent)
25 ,mBoundingRect(0,0,1,1)
25 ,mBoundingRect(0,0,1,1)
26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
27 ,mMinimumSize(50,20) // TODO: magic numbers
27 ,mMinimumSize(50,20) // TODO: magic numbers
28 {
28 {
29 }
29 }
30
30
31 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
31 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 {
32 {
33 painter->setBrush(mBackgroundBrush);
33 painter->setBrush(mBackgroundBrush);
34 painter->drawRect(mBoundingRect);
34 painter->drawRect(mBoundingRect);
35
36 foreach(LegendMarker* m, mMarkers) {
37 QRectF r = m->boundingRect();
38 painter->setBrush(m->brush());
39 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
40 }
41 }
35 }
42
36
43 QRectF QLegend::boundingRect() const
37 QRectF QLegend::boundingRect() const
44 {
38 {
45 return mBoundingRect;
39 return mBoundingRect;
46 }
40 }
47
41
48 void QLegend::setBackgroundBrush(const QBrush& brush)
42 void QLegend::setBackgroundBrush(const QBrush& brush)
49 {
43 {
50 mBackgroundBrush = brush;
44 mBackgroundBrush = brush;
51 }
45 }
52
46
53 QBrush QLegend::backgroundBrush() const
47 QBrush QLegend::backgroundBrush() const
54 {
48 {
55 return mBackgroundBrush;
49 return mBackgroundBrush;
56 }
50 }
57
51
58 QSizeF QLegend::minimumSize() const
52 QSizeF QLegend::minimumSize() const
59 {
53 {
60 return mMinimumSize;
54 return mMinimumSize;
61 }
55 }
62
56
63 void QLegend::setMinimumSize(const QSizeF size)
57 void QLegend::setMinimumSize(const QSizeF size)
64 {
58 {
65 mMinimumSize = size;
59 mMinimumSize = size;
66 }
60 }
67
61
68 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
62 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
69 {
63 {
70 mSeriesList.append(series);
64 mSeriesList.append(series);
71
65
72 switch (series->type())
66 switch (series->type())
73 {
67 {
74 case QSeries::SeriesTypeLine: {
68 case QSeries::SeriesTypeLine: {
75
69
76 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
70 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
77 createMarker(lineSeries);
71 createMarker(lineSeries);
78 break;
72 break;
79 }
73 }
80 case QSeries::SeriesTypeArea: {
74 case QSeries::SeriesTypeArea: {
81
75
82 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
76 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
83 createMarker(areaSeries->upperSeries());
77 createMarker(areaSeries->upperSeries());
84 if(areaSeries->lowerSeries())
78 if(areaSeries->lowerSeries())
85 createMarker(areaSeries->lowerSeries());
79 createMarker(areaSeries->lowerSeries());
86 break;
80 break;
87 }
81 }
88
82
89 case QSeries::SeriesTypeBar: {
83 case QSeries::SeriesTypeBar: {
90
84
91 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
85 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
92 createMarkers(barSeries);
86 createMarkers(barSeries);
93 break;
87 break;
94 }
88 }
95
89
96 case QSeries::SeriesTypeStackedBar: {
90 case QSeries::SeriesTypeStackedBar: {
97
91
98 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
92 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
99 createMarkers(stackedBarSeries);
93 createMarkers(stackedBarSeries);
100 break;
94 break;
101 }
95 }
102
96
103 case QSeries::SeriesTypePercentBar: {
97 case QSeries::SeriesTypePercentBar: {
104
98
105 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
106 createMarkers(percentBarSeries);
100 createMarkers(percentBarSeries);
107 break;
101 break;
108 }
102 }
109
103
110 case QSeries::SeriesTypeScatter: {
104 case QSeries::SeriesTypeScatter: {
111
105
112 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
106 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
113 createMarker(scatterSeries);
107 createMarker(scatterSeries);
114 break;
108 break;
115 }
109 }
116
110
117 case QSeries::SeriesTypePie: {
111 case QSeries::SeriesTypePie: {
118
112
119 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
113 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
120 createMarkers(pieSeries);
114 createMarkers(pieSeries);
121 break;
115 break;
122 }
116 }
123
117
124 case QSeries::SeriesTypeSpline: {
118 case QSeries::SeriesTypeSpline: {
125
119
126 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
120 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
127 createMarker(splineSeries);
121 createMarker(splineSeries);
128 break;
122 break;
129 }
123 }
130 default: {
124 default: {
131 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
125 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
132 break;
126 break;
133 }
127 }
134 }
128 }
135
129
136 layoutChanged();
130 layoutChanged();
137 }
131 }
138
132
139 void QLegend::handleSeriesRemoved(QSeries* series)
133 void QLegend::handleSeriesRemoved(QSeries* series)
140 {
134 {
141 if (series->type() == QSeries::SeriesTypeArea)
135 if (series->type() == QSeries::SeriesTypeArea)
142 {
136 {
143 // This is special case. Area series has upper and lower series, which each have markers
137 // This is special case. Area series has upper and lower series, which each have markers
144 QAreaSeries* s = static_cast<QAreaSeries*> (series);
138 QAreaSeries* s = static_cast<QAreaSeries*> (series);
145 deleteMarkers(s->upperSeries());
139 deleteMarkers(s->upperSeries());
146 deleteMarkers(s->lowerSeries());
140 deleteMarkers(s->lowerSeries());
147 } else {
141 } else {
148 deleteMarkers(series);
142 deleteMarkers(series);
149 }
143 }
150
144
151 mSeriesList.removeOne(series);
145 mSeriesList.removeOne(series);
152 layoutChanged();
146 layoutChanged();
153 }
147 }
154
148
155 void QLegend::handleGeometryChanged(const QRectF& size)
149 void QLegend::handleGeometryChanged(const QRectF& size)
156 {
150 {
157 mBoundingRect = size;
151 mBoundingRect = size;
158 layoutChanged();
152 layoutChanged();
159 }
153 }
160
154
161 void QLegend::deleteMarkers(QSeries *series)
155 void QLegend::deleteMarkers(QSeries *series)
162 {
156 {
163 // Search all markers that belong to given series and delete them.
157 // Search all markers that belong to given series and delete them.
164 foreach (LegendMarker *m, mMarkers) {
158 foreach (LegendMarker *m, mMarkers) {
165 if (m->series() == series) {
159 if (m->series() == series) {
166 mMarkers.removeOne(m);
160 mMarkers.removeOne(m);
167 delete m;
161 delete m;
168 }
162 }
169 }
163 }
170 }
164 }
171
165
172 void QLegend::createMarker(QXYSeries* series)
166 void QLegend::createMarker(QXYSeries* series)
173 {
167 {
174 LegendMarker* marker = new LegendMarker(series,this);
168 LegendMarker* marker = new LegendMarker(series,this);
175 marker->setName(series->name());
169 marker->setName(series->name());
176 marker->setBrush(series->brush());
170 marker->setBrush(series->brush());
177 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
171 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
178 mMarkers.append(marker);
172 mMarkers.append(marker);
179 childItems().append(marker);
173 childItems().append(marker);
180 }
174 }
181
175
182 void QLegend::createMarkers(QBarSeries *series)
176 void QLegend::createMarkers(QBarSeries *series)
183 {
177 {
184 foreach(QBarSet* s, series->barSets()) {
178 foreach(QBarSet* s, series->barSets()) {
185 LegendMarker* marker = new LegendMarker(series,s,this);
179 LegendMarker* marker = new LegendMarker(series,s,this);
186 marker->setName(s->name());
180 marker->setName(s->name());
187 marker->setBrush(s->brush());
181 marker->setBrush(s->brush());
188 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
182 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
189 mMarkers.append(marker);
183 mMarkers.append(marker);
190 childItems().append(marker);
184 childItems().append(marker);
191 }
185 }
192 }
186 }
193
187
194 void QLegend::createMarkers(QPieSeries *series)
188 void QLegend::createMarkers(QPieSeries *series)
195 {
189 {
196 foreach(QPieSlice* s, series->slices()) {
190 foreach(QPieSlice* s, series->slices()) {
197 LegendMarker* marker = new LegendMarker(series,s,this);
191 LegendMarker* marker = new LegendMarker(series,s,this);
198 marker->setName(s->label());
192 marker->setName(s->label());
199 marker->setBrush(s->sliceBrush());
193 marker->setBrush(s->sliceBrush());
200 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
194 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
201 mMarkers.append(marker);
195 mMarkers.append(marker);
202 childItems().append(marker);
196 childItems().append(marker);
203 }
197 }
204 }
198 }
205
199
206 void QLegend::layoutChanged()
200 void QLegend::layoutChanged()
207 {
201 {
208 // Calculate layout for markers and text
202 // Calculate layout for markers and text
209 if (mMarkers.count() <= 0) {
203 if (mMarkers.count() <= 0) {
210 // Nothing to do
204 // Nothing to do
211 return;
205 return;
212 }
206 }
213
207
214 // TODO: marker defined by series.
215 QSizeF markerSize(10,10);
216
217 // TODO: better layout, this is just concept.
218 // Leave some space around markers like this: | x x x x |
219 qreal steps = mMarkers.count();
208 qreal steps = mMarkers.count();
220
221 qreal xStep = mBoundingRect.width() / steps;
209 qreal xStep = mBoundingRect.width() / steps;
222 qreal yStep = mBoundingRect.height() / steps;
210 qreal x=mBoundingRect.x();
223 qreal x = mBoundingRect.x() + 5;
211 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
224 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
225 foreach (LegendMarker* m, mMarkers) {
212 foreach (LegendMarker* m, mMarkers) {
226 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
213 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
227 x += xStep;
214 x += xStep;
228 }
215 }
229 }
216 }
230
217
231 #include "moc_qlegend.cpp"
218 #include "moc_qlegend.cpp"
232 QTCOMMERCIALCHART_END_NAMESPACE
219 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now