##// END OF EJS Templates
bug fix in legend signals
sauimone -
r569:41841e53b04b
parent child
Show More
@@ -1,105 +1,107
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 ,mName("")
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 {
16 {
17 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
17 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 }
18 }
19
19
20 LegendMarker::LegendMarker(QBarSet* barset, QGraphicsItem *parent)
20 LegendMarker::LegendMarker(QBarSet* barset, QGraphicsItem *parent)
21 : QGraphicsObject(parent)
21 : QGraphicsObject(parent)
22 ,mBoundingRect(0,0,1,1)
22 ,mBoundingRect(0,0,1,1)
23 ,mName("")
23 ,mName("")
24 ,mSeries(0)
24 ,mSeries(0)
25 ,mBarset(barset)
25 ,mBarset(barset)
26 ,mPieslice(0)
26 ,mPieslice(0)
27 ,mType(LegendMarkerTypeBarset)
27 ,mType(LegendMarkerTypeBarset)
28 {
28 {
29 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
29 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
30 }
30 }
31
31
32 LegendMarker::LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent)
32 LegendMarker::LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent)
33 : QGraphicsObject(parent)
33 : QGraphicsObject(parent)
34 ,mBoundingRect(0,0,1,1)
34 ,mBoundingRect(0,0,1,1)
35 ,mName("")
35 ,mName("")
36 ,mSeries(0)
36 ,mSeries(0)
37 ,mBarset(0)
37 ,mBarset(0)
38 ,mPieslice(pieslice)
38 ,mPieslice(pieslice)
39 ,mType(LegendMarkerTypePieslice)
39 ,mType(LegendMarkerTypePieslice)
40 {
40 {
41 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
41 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
42 }
42 }
43
43
44 void LegendMarker::setBoundingRect(const QRectF rect)
44 void LegendMarker::setBoundingRect(const QRectF rect)
45 {
45 {
46 mBoundingRect = rect;
46 mBoundingRect = rect;
47 }
47 }
48
48
49 void LegendMarker::setBrush(const QBrush brush)
49 void LegendMarker::setBrush(const QBrush brush)
50 {
50 {
51 mBrush = brush;
51 mBrush = brush;
52 }
52 }
53
53
54 QBrush LegendMarker::brush() const
54 QBrush LegendMarker::brush() const
55 {
55 {
56 return mBrush;
56 return mBrush;
57 }
57 }
58
58
59 void LegendMarker::setName(const QString name)
59 void LegendMarker::setName(const QString name)
60 {
60 {
61 mName = name;
61 mName = name;
62 }
62 }
63
63
64 QString LegendMarker::name() const
64 QString LegendMarker::name() const
65 {
65 {
66 return mName;
66 return mName;
67 }
67 }
68
68
69 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
69 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70 {
70 {
71 painter->setBrush(mBrush);
71 painter->setBrush(mBrush);
72 painter->drawRect(mBoundingRect);
72 painter->drawRect(mBoundingRect);
73 }
73 }
74
74
75 QRectF LegendMarker::boundingRect() const
75 QRectF LegendMarker::boundingRect() const
76 {
76 {
77 return mBoundingRect;
77 return mBoundingRect;
78 }
78 }
79
79
80 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
80 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
81 {
81 {
82 switch (mType)
82 switch (mType)
83 {
83 {
84 case LegendMarkerTypeSeries: {
84 case LegendMarkerTypeSeries: {
85 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypeSeries" << event;
85 emit clicked(mSeries,event->button());
86 emit clicked(mSeries,event->button());
86 break;
87 break;
87 }
88 }
88 case LegendMarkerTypeBarset: {
89 case LegendMarkerTypeBarset: {
90 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypeBarset" << event;
89 emit clicked(mBarset,event->button());
91 emit clicked(mBarset,event->button());
90 break;
92 break;
91 }
93 }
92 case LegendMarkerTypePieslice: {
94 case LegendMarkerTypePieslice: {
95 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypePieslice" << event;
93 emit clicked(mPieslice,event->button());
96 emit clicked(mPieslice,event->button());
94 break;
97 break;
95 }
98 }
96 default: {
99 default: {
97 break;
100 break;
98 }
101 }
99 }
102 }
100
101 }
103 }
102
104
103 #include "moc_legendmarker_p.cpp"
105 #include "moc_legendmarker_p.cpp"
104
106
105 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,203 +1,203
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 {
27 {
28 }
28 }
29
29
30 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
30 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
31 {
31 {
32 painter->setBrush(mBackgroundBrush);
32 painter->setBrush(mBackgroundBrush);
33 painter->drawRect(mBoundingRect);
33 painter->drawRect(mBoundingRect);
34
34
35 foreach(LegendMarker* m, mMarkers) {
35 foreach(LegendMarker* m, mMarkers) {
36 QRectF r = m->boundingRect();
36 QRectF r = m->boundingRect();
37 painter->setBrush(m->brush());
37 painter->setBrush(m->brush());
38 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
38 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
39 }
39 }
40 }
40 }
41
41
42 QRectF QLegend::boundingRect() const
42 QRectF QLegend::boundingRect() const
43 {
43 {
44 return mBoundingRect;
44 return mBoundingRect;
45 }
45 }
46
46
47 void QLegend::setBackgroundBrush(const QBrush& brush)
47 void QLegend::setBackgroundBrush(const QBrush& brush)
48 {
48 {
49 mBackgroundBrush = brush;
49 mBackgroundBrush = brush;
50 }
50 }
51
51
52 QBrush QLegend::backgroundBrush() const
52 QBrush QLegend::backgroundBrush() const
53 {
53 {
54 return mBackgroundBrush;
54 return mBackgroundBrush;
55 }
55 }
56
56
57 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
57 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
58 {
58 {
59 mSeriesList.append(series);
59 mSeriesList.append(series);
60
60
61 switch (series->type())
61 switch (series->type())
62 {
62 {
63 case QSeries::SeriesTypeLine: {
63 case QSeries::SeriesTypeLine: {
64
64
65 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
65 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
66 createMarker(lineSeries);
66 createMarker(lineSeries);
67 break;
67 break;
68 }
68 }
69 case QSeries::SeriesTypeArea: {
69 case QSeries::SeriesTypeArea: {
70
70
71 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
71 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
72 createMarker(areaSeries->upperSeries());
72 createMarker(areaSeries->upperSeries());
73 createMarker(areaSeries->lowerSeries());
73 createMarker(areaSeries->lowerSeries());
74 break;
74 break;
75 }
75 }
76
76
77 case QSeries::SeriesTypeBar: {
77 case QSeries::SeriesTypeBar: {
78
78
79 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
79 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
80 createMarkers(barSeries);
80 createMarkers(barSeries);
81 break;
81 break;
82 }
82 }
83
83
84 case QSeries::SeriesTypeStackedBar: {
84 case QSeries::SeriesTypeStackedBar: {
85
85
86 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
86 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
87 createMarkers(stackedBarSeries);
87 createMarkers(stackedBarSeries);
88 break;
88 break;
89 }
89 }
90
90
91 case QSeries::SeriesTypePercentBar: {
91 case QSeries::SeriesTypePercentBar: {
92
92
93 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
93 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
94 createMarkers(percentBarSeries);
94 createMarkers(percentBarSeries);
95 break;
95 break;
96 }
96 }
97
97
98 case QSeries::SeriesTypeScatter: {
98 case QSeries::SeriesTypeScatter: {
99
99
100 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
100 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
101 createMarker(scatterSeries);
101 createMarker(scatterSeries);
102 break;
102 break;
103 }
103 }
104
104
105 case QSeries::SeriesTypePie: {
105 case QSeries::SeriesTypePie: {
106
106
107 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
107 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
108 createMarkers(pieSeries);
108 createMarkers(pieSeries);
109 break;
109 break;
110 }
110 }
111
111
112 case QSeries::SeriesTypeSpline: {
112 case QSeries::SeriesTypeSpline: {
113
113
114 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
114 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
115 break;
115 break;
116 }
116 }
117 default: {
117 default: {
118 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
118 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
119 break;
119 break;
120 }
120 }
121 }
121 }
122
122
123 layoutChanged();
123 layoutChanged();
124 }
124 }
125
125
126 void QLegend::handleSeriesRemoved(QSeries* series)
126 void QLegend::handleSeriesRemoved(QSeries* series)
127 {
127 {
128 // TODO: delete markers, disconnect.
128 // TODO: delete markers, disconnect.
129 mSeriesList.removeOne(series);
129 mSeriesList.removeOne(series);
130 layoutChanged();
130 layoutChanged();
131 }
131 }
132
132
133 void QLegend::handleGeometryChanged(const QRectF& size)
133 void QLegend::handleGeometryChanged(const QRectF& size)
134 {
134 {
135 mBoundingRect = size;
135 mBoundingRect = size;
136 layoutChanged();
136 layoutChanged();
137 }
137 }
138
138
139 void QLegend::createMarker(QXYSeries* series)
139 void QLegend::createMarker(QXYSeries* series)
140 {
140 {
141 LegendMarker* marker = new LegendMarker(series,this);
141 LegendMarker* marker = new LegendMarker(series,this);
142 marker->setName(series->name());
142 marker->setName(series->name());
143 marker->setBrush(series->brush());
143 marker->setBrush(series->brush());
144 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
144 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
145 mMarkers.append(marker);
145 mMarkers.append(marker);
146 childItems().append(marker);
146 childItems().append(marker);
147 }
147 }
148
148
149 void QLegend::createMarkers(QBarSeries *series)
149 void QLegend::createMarkers(QBarSeries *series)
150 {
150 {
151 foreach(QBarSet* s, series->barSets()) {
151 foreach(QBarSet* s, series->barSets()) {
152 LegendMarker* marker = new LegendMarker(series,this);
152 LegendMarker* marker = new LegendMarker(s,this);
153 marker->setName(s->name());
153 marker->setName(s->name());
154 marker->setBrush(s->brush());
154 marker->setBrush(s->brush());
155 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
155 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
156 mMarkers.append(marker);
156 mMarkers.append(marker);
157 childItems().append(marker);
157 childItems().append(marker);
158 }
158 }
159 }
159 }
160
160
161 void QLegend::createMarkers(QPieSeries *series)
161 void QLegend::createMarkers(QPieSeries *series)
162 {
162 {
163 foreach(QPieSlice* s, series->slices()) {
163 foreach(QPieSlice* s, series->slices()) {
164 LegendMarker* marker = new LegendMarker(series,this);
164 LegendMarker* marker = new LegendMarker(s,this);
165 marker->setName(s->label());
165 marker->setName(s->label());
166 marker->setBrush(s->sliceBrush());
166 marker->setBrush(s->sliceBrush());
167 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
167 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
168 mMarkers.append(marker);
168 mMarkers.append(marker);
169 childItems().append(marker);
169 childItems().append(marker);
170 }
170 }
171 }
171 }
172
172
173 void QLegend::layoutChanged()
173 void QLegend::layoutChanged()
174 {
174 {
175 // Calculate layout for markers and text
175 // Calculate layout for markers and text
176 if (mMarkers.count() <= 0) {
176 if (mMarkers.count() <= 0) {
177 // Nothing to do
177 // Nothing to do
178 return;
178 return;
179 }
179 }
180
180
181 // TODO: marker defined by series.
181 // TODO: marker defined by series.
182 QSizeF markerSize(10,10);
182 QSizeF markerSize(10,10);
183
183
184 // TODO: better layout, this is just concept.
184 // TODO: better layout, this is just concept.
185 // Leave some space around markers like this: | x x x x |
185 // Leave some space around markers like this: | x x x x |
186 qreal steps = mMarkers.count();
186 qreal steps = mMarkers.count();
187
187
188 qreal xStep = mBoundingRect.width() / steps;
188 qreal xStep = mBoundingRect.width() / steps;
189 qreal yStep = mBoundingRect.height() / steps;
189 qreal yStep = mBoundingRect.height() / steps;
190 qreal x = mBoundingRect.x() + 5;
190 qreal x = mBoundingRect.x() + 5;
191 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
191 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
192 foreach (LegendMarker* m, mMarkers) {
192 foreach (LegendMarker* m, mMarkers) {
193 qDebug() << "marker x:" << x;
193 qDebug() << "marker x:" << x;
194 qDebug() << "marker y:" << y;
194 qDebug() << "marker y:" << y;
195 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
195 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
196 x += xStep;
196 x += xStep;
197 }
197 }
198 }
198 }
199
199
200
200
201
201
202 #include "moc_qlegend.cpp"
202 #include "moc_qlegend.cpp"
203 QTCOMMERCIALCHART_END_NAMESPACE
203 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now