##// END OF EJS Templates
Legend can handle removing of series
sauimone -
r576:47e29632d9f9
parent child
Show More
@@ -17,11 +17,11 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
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(QSeries *series, 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(series)
25 ,mBarset(barset)
25 ,mBarset(barset)
26 ,mPieslice(0)
26 ,mPieslice(0)
27 ,mType(LegendMarkerTypeBarset)
27 ,mType(LegendMarkerTypeBarset)
@@ -29,11 +29,11 LegendMarker::LegendMarker(QBarSet* barset, QGraphicsItem *parent)
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(QSeries *series, 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(series)
37 ,mBarset(0)
37 ,mBarset(0)
38 ,mPieslice(pieslice)
38 ,mPieslice(pieslice)
39 ,mType(LegendMarkerTypePieslice)
39 ,mType(LegendMarkerTypePieslice)
@@ -66,6 +66,11 QString LegendMarker::name() const
66 return mName;
66 return mName;
67 }
67 }
68
68
69 QSeries* LegendMarker::series() const
70 {
71 return mSeries;
72 }
73
69 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
74 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70 {
75 {
71 painter->setBrush(mBrush);
76 painter->setBrush(mBrush);
@@ -82,17 +87,14 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
82 switch (mType)
87 switch (mType)
83 {
88 {
84 case LegendMarkerTypeSeries: {
89 case LegendMarkerTypeSeries: {
85 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypeSeries" << event;
86 emit clicked(mSeries,event->button());
90 emit clicked(mSeries,event->button());
87 break;
91 break;
88 }
92 }
89 case LegendMarkerTypeBarset: {
93 case LegendMarkerTypeBarset: {
90 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypeBarset" << event;
91 emit clicked(mBarset,event->button());
94 emit clicked(mBarset,event->button());
92 break;
95 break;
93 }
96 }
94 case LegendMarkerTypePieslice: {
97 case LegendMarkerTypePieslice: {
95 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypePieslice" << event;
96 emit clicked(mPieslice,event->button());
98 emit clicked(mPieslice,event->button());
97 break;
99 break;
98 }
100 }
@@ -24,8 +24,8 class LegendMarker : public QGraphicsObject
24
24
25 public:
25 public:
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
27 LegendMarker(QBarSet* barset, QGraphicsItem *parent = 0);
27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
28 LegendMarker(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);
@@ -34,6 +34,8 public:
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;
38
37 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
38
40
39 QRectF boundingRect() const;
41 QRectF boundingRect() const;
@@ -113,6 +113,7 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
113 case QSeries::SeriesTypeSpline: {
113 case QSeries::SeriesTypeSpline: {
114
114
115 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
115 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
116 createMarker(splineSeries);
116 break;
117 break;
117 }
118 }
118 default: {
119 default: {
@@ -126,7 +127,16 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
126
127
127 void QLegend::handleSeriesRemoved(QSeries* series)
128 void QLegend::handleSeriesRemoved(QSeries* series)
128 {
129 {
129 // TODO: delete markers, disconnect.
130 if (series->type() == QSeries::SeriesTypeArea)
131 {
132 // This is special case. Area series has upper and lower series, which each have markers
133 QAreaSeries* s = static_cast<QAreaSeries*> (series);
134 deleteMarkers(s->upperSeries());
135 deleteMarkers(s->lowerSeries());
136 } else {
137 deleteMarkers(series);
138 }
139
130 mSeriesList.removeOne(series);
140 mSeriesList.removeOne(series);
131 layoutChanged();
141 layoutChanged();
132 }
142 }
@@ -137,6 +147,17 void QLegend::handleGeometryChanged(const QRectF& size)
137 layoutChanged();
147 layoutChanged();
138 }
148 }
139
149
150 void QLegend::deleteMarkers(QSeries *series)
151 {
152 // Search all markers that belong to given series and delete them.
153 foreach (LegendMarker *m, mMarkers) {
154 if (m->series() == series) {
155 mMarkers.removeOne(m);
156 delete m;
157 }
158 }
159 }
160
140 void QLegend::createMarker(QXYSeries* series)
161 void QLegend::createMarker(QXYSeries* series)
141 {
162 {
142 LegendMarker* marker = new LegendMarker(series,this);
163 LegendMarker* marker = new LegendMarker(series,this);
@@ -150,7 +171,7 void QLegend::createMarker(QXYSeries* series)
150 void QLegend::createMarkers(QBarSeries *series)
171 void QLegend::createMarkers(QBarSeries *series)
151 {
172 {
152 foreach(QBarSet* s, series->barSets()) {
173 foreach(QBarSet* s, series->barSets()) {
153 LegendMarker* marker = new LegendMarker(s,this);
174 LegendMarker* marker = new LegendMarker(series,s,this);
154 marker->setName(s->name());
175 marker->setName(s->name());
155 marker->setBrush(s->brush());
176 marker->setBrush(s->brush());
156 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
177 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
@@ -162,7 +183,7 void QLegend::createMarkers(QBarSeries *series)
162 void QLegend::createMarkers(QPieSeries *series)
183 void QLegend::createMarkers(QPieSeries *series)
163 {
184 {
164 foreach(QPieSlice* s, series->slices()) {
185 foreach(QPieSlice* s, series->slices()) {
165 LegendMarker* marker = new LegendMarker(s,this);
186 LegendMarker* marker = new LegendMarker(series,s,this);
166 marker->setName(s->label());
187 marker->setName(s->label());
167 marker->setBrush(s->sliceBrush());
188 marker->setBrush(s->sliceBrush());
168 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
189 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
@@ -196,7 +217,5 void QLegend::layoutChanged()
196 }
217 }
197 }
218 }
198
219
199
200
201 #include "moc_qlegend.cpp"
220 #include "moc_qlegend.cpp"
202 QTCOMMERCIALCHART_END_NAMESPACE
221 QTCOMMERCIALCHART_END_NAMESPACE
@@ -44,6 +44,7 private:
44 void createMarker(QXYSeries* series);
44 void createMarker(QXYSeries* series);
45 void createMarkers(QBarSeries* series);
45 void createMarkers(QBarSeries* series);
46 void createMarkers(QPieSeries* series);
46 void createMarkers(QPieSeries* series);
47 void deleteMarkers(QSeries* series);
47 void layoutChanged();
48 void layoutChanged();
48 // <--- PIMPL
49 // <--- PIMPL
49
50
General Comments 0
You need to be logged in to leave comments. Login now