##// END OF EJS Templates
disable legend by default
sauimone -
r596:9551377d12c7
parent child
Show More
@@ -1,216 +1,217
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 ,mMinimumSize(50,20) // TODO: magic numbers
28 28 {
29 setVisible(false);
29 30 }
30 31
31 32 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 33 {
33 34 painter->setBrush(mBackgroundBrush);
34 35 painter->drawRect(mBoundingRect);
35 36 }
36 37
37 38 QRectF QLegend::boundingRect() const
38 39 {
39 40 return mBoundingRect;
40 41 }
41 42
42 43 void QLegend::setBackgroundBrush(const QBrush& brush)
43 44 {
44 45 mBackgroundBrush = brush;
45 46 }
46 47
47 48 QBrush QLegend::backgroundBrush() const
48 49 {
49 50 return mBackgroundBrush;
50 51 }
51 52
52 53 QSizeF QLegend::minimumSize() const
53 54 {
54 55 return mMinimumSize;
55 56 }
56 57
57 58 void QLegend::setMinimumSize(const QSizeF size)
58 59 {
59 60 mMinimumSize = size;
60 61 }
61 62
62 63 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
63 64 {
64 65 mSeriesList.append(series);
65 66 createMarkers(series);
66 67 layoutChanged();
67 68 }
68 69
69 70 void QLegend::handleSeriesRemoved(QSeries* series)
70 71 {
71 72 if (series->type() == QSeries::SeriesTypeArea)
72 73 {
73 74 // This is special case. Area series has upper and lower series, which each have markers
74 75 QAreaSeries* s = static_cast<QAreaSeries*> (series);
75 76 deleteMarkers(s->upperSeries());
76 77 deleteMarkers(s->lowerSeries());
77 78 } else {
78 79 deleteMarkers(series);
79 80 }
80 81
81 82 mSeriesList.removeOne(series);
82 83 layoutChanged();
83 84 }
84 85
85 86 void QLegend::handleGeometryChanged(const QRectF& size)
86 87 {
87 88 mBoundingRect = size;
88 89 layoutChanged();
89 90 }
90 91
91 92 void QLegend::createMarkers(QSeries *series)
92 93 {
93 94 switch (series->type())
94 95 {
95 96 case QSeries::SeriesTypeLine: {
96 97 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
97 98 appendMarkers(lineSeries);
98 99 break;
99 100 }
100 101 case QSeries::SeriesTypeArea: {
101 102 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
102 103 appendMarkers(areaSeries->upperSeries());
103 104 if(areaSeries->lowerSeries())
104 105 appendMarkers(areaSeries->lowerSeries());
105 106 break;
106 107 }
107 108
108 109 case QSeries::SeriesTypeBar: {
109 110 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
110 111 appendMarkers(barSeries);
111 112 break;
112 113 }
113 114
114 115 case QSeries::SeriesTypeStackedBar: {
115 116 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
116 117 appendMarkers(stackedBarSeries);
117 118 break;
118 119 }
119 120
120 121 case QSeries::SeriesTypePercentBar: {
121 122 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
122 123 appendMarkers(percentBarSeries);
123 124 break;
124 125 }
125 126
126 127 case QSeries::SeriesTypeScatter: {
127 128 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
128 129 appendMarkers(scatterSeries);
129 130 break;
130 131 }
131 132
132 133 case QSeries::SeriesTypePie: {
133 134 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
134 135 appendMarkers(pieSeries);
135 136 break;
136 137 }
137 138
138 139 case QSeries::SeriesTypeSpline: {
139 140 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
140 141 appendMarkers(splineSeries);
141 142 break;
142 143 }
143 144 default: {
144 145 qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented.";
145 146 break;
146 147 }
147 148 }
148 149 }
149 150
150 151 void QLegend::appendMarkers(QXYSeries* series)
151 152 {
152 153 LegendMarker* marker = new LegendMarker(series,this);
153 154 marker->setName(series->name());
154 155 marker->setBrush(series->brush());
155 156 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
156 157 mMarkers.append(marker);
157 158 childItems().append(marker);
158 159 }
159 160
160 161 void QLegend::appendMarkers(QBarSeries *series)
161 162 {
162 163 foreach(QBarSet* s, series->barSets()) {
163 164 LegendMarker* marker = new LegendMarker(series,s,this);
164 165 marker->setName(s->name());
165 166 marker->setBrush(s->brush());
166 167 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
167 168 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
168 169 mMarkers.append(marker);
169 170 childItems().append(marker);
170 171 }
171 172 }
172 173
173 174 void QLegend::appendMarkers(QPieSeries *series)
174 175 {
175 176 foreach(QPieSlice* s, series->slices()) {
176 177 LegendMarker* marker = new LegendMarker(series,s,this);
177 178 marker->setName(s->label());
178 179 marker->setBrush(s->sliceBrush());
179 180 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
180 181 connect(s,SIGNAL(changed()),marker,SLOT(changed()));
181 182 mMarkers.append(marker);
182 183 childItems().append(marker);
183 184 }
184 185 }
185 186
186 187 void QLegend::deleteMarkers(QSeries *series)
187 188 {
188 189 // Search all markers that belong to given series and delete them.
189 190 foreach (LegendMarker *m, mMarkers) {
190 191 if (m->series() == series) {
191 192 mMarkers.removeOne(m);
192 193 delete m;
193 194 }
194 195 }
195 196 }
196 197
197 198 void QLegend::layoutChanged()
198 199 {
199 200 // Calculate layout for markers and text
200 201 if (mMarkers.count() <= 0) {
201 202 // Nothing to do
202 203 return;
203 204 }
204 205
205 206 qreal steps = mMarkers.count();
206 207 qreal xStep = mBoundingRect.width() / steps;
207 208 qreal x=mBoundingRect.x();
208 209 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
209 210 foreach (LegendMarker* m, mMarkers) {
210 211 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
211 212 x += xStep;
212 213 }
213 214 }
214 215
215 216 #include "moc_qlegend.cpp"
216 217 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now