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