##// 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 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 21 : QGraphicsObject(parent)
22 22 ,mBoundingRect(0,0,1,1)
23 23 ,mName("")
24 ,mSeries(0)
24 ,mSeries(series)
25 25 ,mBarset(barset)
26 26 ,mPieslice(0)
27 27 ,mType(LegendMarkerTypeBarset)
@@ -29,11 +29,11 LegendMarker::LegendMarker(QBarSet* barset, QGraphicsItem *parent)
29 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 33 : QGraphicsObject(parent)
34 34 ,mBoundingRect(0,0,1,1)
35 35 ,mName("")
36 ,mSeries(0)
36 ,mSeries(series)
37 37 ,mBarset(0)
38 38 ,mPieslice(pieslice)
39 39 ,mType(LegendMarkerTypePieslice)
@@ -66,6 +66,11 QString LegendMarker::name() const
66 66 return mName;
67 67 }
68 68
69 QSeries* LegendMarker::series() const
70 {
71 return mSeries;
72 }
73
69 74 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70 75 {
71 76 painter->setBrush(mBrush);
@@ -82,17 +87,14 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
82 87 switch (mType)
83 88 {
84 89 case LegendMarkerTypeSeries: {
85 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypeSeries" << event;
86 90 emit clicked(mSeries,event->button());
87 91 break;
88 92 }
89 93 case LegendMarkerTypeBarset: {
90 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypeBarset" << event;
91 94 emit clicked(mBarset,event->button());
92 95 break;
93 96 }
94 97 case LegendMarkerTypePieslice: {
95 qDebug() << "LegendMarker::mousePressEvent LegendMarkerTypePieslice" << event;
96 98 emit clicked(mPieslice,event->button());
97 99 break;
98 100 }
@@ -24,8 +24,8 class LegendMarker : public QGraphicsObject
24 24
25 25 public:
26 26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
27 LegendMarker(QBarSet* barset, QGraphicsItem *parent = 0);
28 LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent = 0);
27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
28 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
29 29 void setBoundingRect(const QRectF rect);
30 30
31 31 void setBrush(const QBrush brush);
@@ -34,6 +34,8 public:
34 34 void setName(const QString name);
35 35 QString name() const;
36 36
37 QSeries* series() const;
38
37 39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
38 40
39 41 QRectF boundingRect() const;
@@ -113,6 +113,7 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
113 113 case QSeries::SeriesTypeSpline: {
114 114
115 115 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
116 createMarker(splineSeries);
116 117 break;
117 118 }
118 119 default: {
@@ -126,7 +127,16 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
126 127
127 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 140 mSeriesList.removeOne(series);
131 141 layoutChanged();
132 142 }
@@ -137,6 +147,17 void QLegend::handleGeometryChanged(const QRectF& size)
137 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 161 void QLegend::createMarker(QXYSeries* series)
141 162 {
142 163 LegendMarker* marker = new LegendMarker(series,this);
@@ -150,7 +171,7 void QLegend::createMarker(QXYSeries* series)
150 171 void QLegend::createMarkers(QBarSeries *series)
151 172 {
152 173 foreach(QBarSet* s, series->barSets()) {
153 LegendMarker* marker = new LegendMarker(s,this);
174 LegendMarker* marker = new LegendMarker(series,s,this);
154 175 marker->setName(s->name());
155 176 marker->setBrush(s->brush());
156 177 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
@@ -162,7 +183,7 void QLegend::createMarkers(QBarSeries *series)
162 183 void QLegend::createMarkers(QPieSeries *series)
163 184 {
164 185 foreach(QPieSlice* s, series->slices()) {
165 LegendMarker* marker = new LegendMarker(s,this);
186 LegendMarker* marker = new LegendMarker(series,s,this);
166 187 marker->setName(s->label());
167 188 marker->setBrush(s->sliceBrush());
168 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 220 #include "moc_qlegend.cpp"
202 221 QTCOMMERCIALCHART_END_NAMESPACE
@@ -44,6 +44,7 private:
44 44 void createMarker(QXYSeries* series);
45 45 void createMarkers(QBarSeries* series);
46 46 void createMarkers(QPieSeries* series);
47 void deleteMarkers(QSeries* series);
47 48 void layoutChanged();
48 49 // <--- PIMPL
49 50
General Comments 0
You need to be logged in to leave comments. Login now