@@ -1,342 +1,465 | |||||
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 "chartpresenter_p.h" |
|
16 | #include "chartpresenter_p.h" | |
17 | #include <QPainter> |
|
17 | #include <QPainter> | |
18 | #include <QPen> |
|
18 | #include <QPen> | |
19 |
|
19 | |||
20 | #include <QGraphicsSceneEvent> |
|
20 | #include <QGraphicsSceneEvent> | |
21 |
|
21 | |||
22 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
23 |
|
23 | |||
24 | QLegend::QLegend(QGraphicsItem *parent) |
|
24 | QLegend::QLegend(QGraphicsItem *parent) | |
25 | : QGraphicsObject(parent) |
|
25 | : QGraphicsObject(parent) | |
26 | ,mPos(0,0) |
|
26 | ,mPos(0,0) | |
27 | ,mSize(0,0) |
|
27 | ,mSize(0,0) | |
28 | ,mMinimumSize(50,20) // TODO: magic numbers |
|
28 | ,mMinimumSize(50,20) // TODO: magic numbers | |
29 | ,mMaximumSize(150,100) |
|
29 | ,mMaximumSize(150,100) | |
30 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? |
|
30 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? | |
31 | ,mPreferredLayout(QLegend::PreferredLayoutVertical) |
|
31 | ,mPreferredLayout(QLegend::PreferredLayoutVertical) | |
32 | { |
|
32 | { | |
33 | // setVisible(false); |
|
33 | // setVisible(false); | |
34 | setZValue(ChartPresenter::LegendZValue); |
|
34 | setZValue(ChartPresenter::LegendZValue); | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
37 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
38 | { |
|
38 | { | |
39 | Q_UNUSED(option) |
|
39 | Q_UNUSED(option) | |
40 | Q_UNUSED(widget) |
|
40 | Q_UNUSED(widget) | |
41 |
|
41 | |||
42 | painter->setOpacity(0.5); |
|
42 | painter->setOpacity(0.5); | |
43 | painter->setBrush(mBackgroundBrush); |
|
43 | painter->setBrush(mBackgroundBrush); | |
44 | painter->drawRect(boundingRect()); |
|
44 | painter->drawRect(boundingRect()); | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | QRectF QLegend::boundingRect() const |
|
47 | QRectF QLegend::boundingRect() const | |
48 | { |
|
48 | { | |
49 | return QRectF(mPos,mSize); |
|
49 | return QRectF(mPos,mSize); | |
50 | } |
|
50 | } | |
51 |
|
51 | |||
52 | void QLegend::setBackgroundBrush(const QBrush& brush) |
|
52 | void QLegend::setBackgroundBrush(const QBrush& brush) | |
53 | { |
|
53 | { | |
54 | mBackgroundBrush = brush; |
|
54 | mBackgroundBrush = brush; | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | QBrush QLegend::backgroundBrush() const |
|
57 | QBrush QLegend::backgroundBrush() const | |
58 | { |
|
58 | { | |
59 | return mBackgroundBrush; |
|
59 | return mBackgroundBrush; | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred) |
|
62 | void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred) | |
63 | { |
|
63 | { | |
64 | mPreferredLayout = preferred; |
|
64 | mPreferredLayout = preferred; | |
65 | layoutChanged(); |
|
65 | layoutChanged(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | QSizeF QLegend::maximumSize() const |
|
68 | QSizeF QLegend::maximumSize() const | |
69 | { |
|
69 | { | |
70 | return mMaximumSize; |
|
70 | return mMaximumSize; | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | void QLegend::setMaximumSize(const QSizeF size) |
|
73 | void QLegend::setMaximumSize(const QSizeF size) | |
74 | { |
|
74 | { | |
75 | mMaximumSize = size; |
|
75 | mMaximumSize = size; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | void QLegend::setSize(const QSizeF size) |
|
78 | void QLegend::setSize(const QSizeF size) | |
79 | { |
|
79 | { | |
80 | mSize = size; |
|
80 | mSize = size; | |
81 | if (mSize.width() > mMaximumSize.width()) { |
|
81 | if (mSize.width() > mMaximumSize.width()) { | |
82 | mSize.setWidth(mMaximumSize.width()); |
|
82 | mSize.setWidth(mMaximumSize.width()); | |
83 | } |
|
83 | } | |
84 | if (mSize.height() > mMaximumSize.height()) { |
|
84 | if (mSize.height() > mMaximumSize.height()) { | |
85 | mSize.setHeight(mMaximumSize.height()); |
|
85 | mSize.setHeight(mMaximumSize.height()); | |
86 | } |
|
86 | } | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void QLegend::setPos(const QPointF &pos) |
|
89 | void QLegend::setPos(const QPointF &pos) | |
90 | { |
|
90 | { | |
91 | mPos = pos; |
|
91 | mPos = pos; | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | void QLegend::handleSeriesAdded(QSeries* series, Domain* domain) |
|
94 | void QLegend::handleSeriesAdded(QSeries* series, Domain* domain) | |
95 | { |
|
95 | { | |
96 | Q_UNUSED(domain) |
|
96 | Q_UNUSED(domain) | |
97 |
|
97 | |||
98 | mSeriesList.append(series); |
|
98 | mSeriesList.append(series); | |
99 | createMarkers(series); |
|
99 | createMarkers(series); | |
|
100 | connectSeries(series); | |||
100 | layoutChanged(); |
|
101 | layoutChanged(); | |
101 | } |
|
102 | } | |
102 |
|
103 | |||
103 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
104 | void QLegend::handleSeriesRemoved(QSeries* series) | |
104 | { |
|
105 | { | |
|
106 | disconnectSeries(series); | |||
|
107 | ||||
105 | if (series->type() == QSeries::SeriesTypeArea) |
|
108 | if (series->type() == QSeries::SeriesTypeArea) | |
106 | { |
|
109 | { | |
107 | // This is special case. Area series has upper and lower series, which each have markers |
|
110 | // This is special case. Area series has upper and lower series, which each have markers | |
108 | QAreaSeries* s = static_cast<QAreaSeries*> (series); |
|
111 | QAreaSeries* s = static_cast<QAreaSeries*> (series); | |
109 | deleteMarkers(s->upperSeries()); |
|
112 | deleteMarkers(s->upperSeries()); | |
110 | deleteMarkers(s->lowerSeries()); |
|
113 | deleteMarkers(s->lowerSeries()); | |
111 | } else { |
|
114 | } else { | |
112 | deleteMarkers(series); |
|
115 | deleteMarkers(series); | |
113 | } |
|
116 | } | |
114 |
|
117 | |||
115 | mSeriesList.removeOne(series); |
|
118 | mSeriesList.removeOne(series); | |
116 | layoutChanged(); |
|
119 | layoutChanged(); | |
117 | } |
|
120 | } | |
118 |
|
121 | |||
|
122 | void QLegend::handleAdded(QList<QPieSlice*> slices) | |||
|
123 | { | |||
|
124 | QPieSeries* series = static_cast<QPieSeries*> (sender()); | |||
|
125 | foreach(QPieSlice* s, slices) { | |||
|
126 | LegendMarker* marker = new LegendMarker(series,s,this); | |||
|
127 | marker->setName(s->label()); | |||
|
128 | marker->setBrush(s->sliceBrush()); | |||
|
129 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); | |||
|
130 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | |||
|
131 | connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater())); | |||
|
132 | mMarkers.append(marker); | |||
|
133 | childItems().append(marker); | |||
|
134 | } | |||
|
135 | layoutChanged(); | |||
|
136 | } | |||
|
137 | ||||
|
138 | void QLegend::handleMarkerDestroyed() | |||
|
139 | { | |||
|
140 | // TODO: what if more than one markers are destroyed and we update layout after first one? | |||
|
141 | LegendMarker* m = static_cast<LegendMarker*> (sender()); | |||
|
142 | mMarkers.removeOne(m); | |||
|
143 | layoutChanged(); | |||
|
144 | } | |||
|
145 | ||||
|
146 | void QLegend::connectSeries(QSeries *series) | |||
|
147 | { | |||
|
148 | // Connect relevant signals from series | |||
|
149 | switch (series->type()) | |||
|
150 | { | |||
|
151 | case QSeries::SeriesTypeLine: { | |||
|
152 | // QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | |||
|
153 | break; | |||
|
154 | } | |||
|
155 | case QSeries::SeriesTypeArea: { | |||
|
156 | // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |||
|
157 | break; | |||
|
158 | } | |||
|
159 | case QSeries::SeriesTypeBar: { | |||
|
160 | // QBarSeries* barSeries = static_cast<QBarSeries*>(series); | |||
|
161 | break; | |||
|
162 | } | |||
|
163 | case QSeries::SeriesTypeStackedBar: { | |||
|
164 | // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | |||
|
165 | break; | |||
|
166 | } | |||
|
167 | case QSeries::SeriesTypePercentBar: { | |||
|
168 | // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | |||
|
169 | break; | |||
|
170 | } | |||
|
171 | case QSeries::SeriesTypeScatter: { | |||
|
172 | // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | |||
|
173 | break; | |||
|
174 | } | |||
|
175 | case QSeries::SeriesTypePie: { | |||
|
176 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |||
|
177 | connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); | |||
|
178 | // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>))); | |||
|
179 | break; | |||
|
180 | } | |||
|
181 | case QSeries::SeriesTypeSpline: { | |||
|
182 | // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | |||
|
183 | break; | |||
|
184 | } | |||
|
185 | default: { | |||
|
186 | qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented."; | |||
|
187 | break; | |||
|
188 | } | |||
|
189 | } | |||
|
190 | } | |||
|
191 | ||||
|
192 | void QLegend::disconnectSeries(QSeries *series) | |||
|
193 | { | |||
|
194 | // Connect relevant signals from series | |||
|
195 | switch (series->type()) | |||
|
196 | { | |||
|
197 | case QSeries::SeriesTypeLine: { | |||
|
198 | // QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | |||
|
199 | break; | |||
|
200 | } | |||
|
201 | case QSeries::SeriesTypeArea: { | |||
|
202 | // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |||
|
203 | break; | |||
|
204 | } | |||
|
205 | case QSeries::SeriesTypeBar: { | |||
|
206 | // QBarSeries* barSeries = static_cast<QBarSeries*>(series); | |||
|
207 | break; | |||
|
208 | } | |||
|
209 | case QSeries::SeriesTypeStackedBar: { | |||
|
210 | // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | |||
|
211 | break; | |||
|
212 | } | |||
|
213 | case QSeries::SeriesTypePercentBar: { | |||
|
214 | // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | |||
|
215 | break; | |||
|
216 | } | |||
|
217 | case QSeries::SeriesTypeScatter: { | |||
|
218 | // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | |||
|
219 | break; | |||
|
220 | } | |||
|
221 | case QSeries::SeriesTypePie: { | |||
|
222 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |||
|
223 | disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); | |||
|
224 | // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>))); | |||
|
225 | break; | |||
|
226 | } | |||
|
227 | case QSeries::SeriesTypeSpline: { | |||
|
228 | // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | |||
|
229 | break; | |||
|
230 | } | |||
|
231 | default: { | |||
|
232 | qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented."; | |||
|
233 | break; | |||
|
234 | } | |||
|
235 | } | |||
|
236 | } | |||
|
237 | ||||
119 | void QLegend::createMarkers(QSeries *series) |
|
238 | void QLegend::createMarkers(QSeries *series) | |
120 | { |
|
239 | { | |
121 | switch (series->type()) |
|
240 | switch (series->type()) | |
122 | { |
|
241 | { | |
123 | case QSeries::SeriesTypeLine: { |
|
242 | case QSeries::SeriesTypeLine: { | |
124 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
243 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | |
125 | appendMarkers(lineSeries); |
|
244 | appendMarkers(lineSeries); | |
126 | break; |
|
245 | break; | |
127 | } |
|
246 | } | |
128 | case QSeries::SeriesTypeArea: { |
|
247 | case QSeries::SeriesTypeArea: { | |
129 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
248 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |
130 | appendMarkers(areaSeries->upperSeries()); |
|
249 | appendMarkers(areaSeries->upperSeries()); | |
131 | if(areaSeries->lowerSeries()) |
|
250 | if(areaSeries->lowerSeries()) | |
132 | appendMarkers(areaSeries->lowerSeries()); |
|
251 | appendMarkers(areaSeries->lowerSeries()); | |
133 | break; |
|
252 | break; | |
134 | } |
|
253 | } | |
135 |
|
254 | |||
136 | case QSeries::SeriesTypeBar: { |
|
255 | case QSeries::SeriesTypeBar: { | |
137 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
256 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); | |
138 | appendMarkers(barSeries); |
|
257 | appendMarkers(barSeries); | |
139 | break; |
|
258 | break; | |
140 | } |
|
259 | } | |
141 |
|
260 | |||
142 | case QSeries::SeriesTypeStackedBar: { |
|
261 | case QSeries::SeriesTypeStackedBar: { | |
143 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
262 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | |
144 | appendMarkers(stackedBarSeries); |
|
263 | appendMarkers(stackedBarSeries); | |
145 | break; |
|
264 | break; | |
146 | } |
|
265 | } | |
147 |
|
266 | |||
148 | case QSeries::SeriesTypePercentBar: { |
|
267 | case QSeries::SeriesTypePercentBar: { | |
149 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
268 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | |
150 | appendMarkers(percentBarSeries); |
|
269 | appendMarkers(percentBarSeries); | |
151 | break; |
|
270 | break; | |
152 | } |
|
271 | } | |
153 |
|
272 | |||
154 | case QSeries::SeriesTypeScatter: { |
|
273 | case QSeries::SeriesTypeScatter: { | |
155 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
274 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | |
156 | appendMarkers(scatterSeries); |
|
275 | appendMarkers(scatterSeries); | |
157 | break; |
|
276 | break; | |
158 | } |
|
277 | } | |
159 |
|
278 | |||
160 | case QSeries::SeriesTypePie: { |
|
279 | case QSeries::SeriesTypePie: { | |
161 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
280 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | |
162 | appendMarkers(pieSeries); |
|
281 | appendMarkers(pieSeries); | |
163 | break; |
|
282 | break; | |
164 | } |
|
283 | } | |
165 |
|
284 | |||
166 | case QSeries::SeriesTypeSpline: { |
|
285 | case QSeries::SeriesTypeSpline: { | |
167 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
286 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | |
168 | appendMarkers(splineSeries); |
|
287 | appendMarkers(splineSeries); | |
169 | break; |
|
288 | break; | |
170 | } |
|
289 | } | |
171 | default: { |
|
290 | default: { | |
172 | qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented."; |
|
291 | qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented."; | |
173 | break; |
|
292 | break; | |
174 | } |
|
293 | } | |
175 | } |
|
294 | } | |
176 | } |
|
295 | } | |
177 |
|
296 | |||
178 | void QLegend::appendMarkers(QXYSeries* series) |
|
297 | void QLegend::appendMarkers(QXYSeries* series) | |
179 | { |
|
298 | { | |
180 | LegendMarker* marker = new LegendMarker(series,this); |
|
299 | LegendMarker* marker = new LegendMarker(series,this); | |
181 | marker->setName(series->name()); |
|
300 | marker->setName(series->name()); | |
182 | marker->setBrush(series->brush()); |
|
301 | marker->setBrush(series->brush()); | |
183 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); |
|
302 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); | |
|
303 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); | |||
184 | mMarkers.append(marker); |
|
304 | mMarkers.append(marker); | |
185 | childItems().append(marker); |
|
305 | childItems().append(marker); | |
186 | } |
|
306 | } | |
187 |
|
307 | |||
188 | void QLegend::appendMarkers(QBarSeries *series) |
|
308 | void QLegend::appendMarkers(QBarSeries *series) | |
189 | { |
|
309 | { | |
190 | foreach(QBarSet* s, series->barSets()) { |
|
310 | foreach(QBarSet* s, series->barSets()) { | |
191 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
311 | LegendMarker* marker = new LegendMarker(series,s,this); | |
192 | marker->setName(s->name()); |
|
312 | marker->setName(s->name()); | |
193 | marker->setBrush(s->brush()); |
|
313 | marker->setBrush(s->brush()); | |
194 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); |
|
314 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); | |
195 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); |
|
315 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | |
|
316 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); | |||
196 | mMarkers.append(marker); |
|
317 | mMarkers.append(marker); | |
197 | childItems().append(marker); |
|
318 | childItems().append(marker); | |
198 | } |
|
319 | } | |
199 | } |
|
320 | } | |
200 |
|
321 | |||
201 | void QLegend::appendMarkers(QPieSeries *series) |
|
322 | void QLegend::appendMarkers(QPieSeries *series) | |
202 | { |
|
323 | { | |
203 | foreach(QPieSlice* s, series->slices()) { |
|
324 | foreach(QPieSlice* s, series->slices()) { | |
204 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
325 | LegendMarker* marker = new LegendMarker(series,s,this); | |
205 | marker->setName(s->label()); |
|
326 | marker->setName(s->label()); | |
206 | marker->setBrush(s->sliceBrush()); |
|
327 | marker->setBrush(s->sliceBrush()); | |
207 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); |
|
328 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); | |
208 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); |
|
329 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); | |
|
330 | connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater())); | |||
|
331 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); | |||
209 | mMarkers.append(marker); |
|
332 | mMarkers.append(marker); | |
210 | childItems().append(marker); |
|
333 | childItems().append(marker); | |
211 | } |
|
334 | } | |
212 | } |
|
335 | } | |
213 |
|
336 | |||
214 | void QLegend::deleteMarkers(QSeries *series) |
|
337 | void QLegend::deleteMarkers(QSeries *series) | |
215 | { |
|
338 | { | |
216 | // Search all markers that belong to given series and delete them. |
|
339 | // Search all markers that belong to given series and delete them. | |
217 | foreach (LegendMarker *m, mMarkers) { |
|
340 | foreach (LegendMarker *m, mMarkers) { | |
218 | if (m->series() == series) { |
|
341 | if (m->series() == series) { | |
219 | mMarkers.removeOne(m); |
|
342 | mMarkers.removeOne(m); | |
220 | delete m; |
|
343 | delete m; | |
221 | } |
|
344 | } | |
222 | } |
|
345 | } | |
223 | } |
|
346 | } | |
224 |
|
347 | |||
225 | void QLegend::layoutChanged() |
|
348 | void QLegend::layoutChanged() | |
226 | { |
|
349 | { | |
227 | // Calculate layout for markers and text |
|
350 | // Calculate layout for markers and text | |
228 | if (mMarkers.count() <= 0) { |
|
351 | if (mMarkers.count() <= 0) { | |
229 | // Nothing to do |
|
352 | // Nothing to do | |
230 | return; |
|
353 | return; | |
231 | } |
|
354 | } | |
232 |
|
355 | |||
233 | // Find out widest item. |
|
356 | // Find out widest item. | |
234 | qreal itemMaxWidth = 0; |
|
357 | qreal itemMaxWidth = 0; | |
235 | qreal itemMaxHeight = 0; |
|
358 | qreal itemMaxHeight = 0; | |
236 | foreach (LegendMarker* m, mMarkers) { |
|
359 | foreach (LegendMarker* m, mMarkers) { | |
237 | if (m->boundingRect().width() > itemMaxWidth) { |
|
360 | if (m->boundingRect().width() > itemMaxWidth) { | |
238 | itemMaxWidth = m->boundingRect().width(); |
|
361 | itemMaxWidth = m->boundingRect().width(); | |
239 | } |
|
362 | } | |
240 | if (m->boundingRect().height() > itemMaxHeight) { |
|
363 | if (m->boundingRect().height() > itemMaxHeight) { | |
241 | itemMaxHeight = m->boundingRect().height(); |
|
364 | itemMaxHeight = m->boundingRect().height(); | |
242 | } |
|
365 | } | |
243 | } |
|
366 | } | |
244 |
|
367 | |||
245 | int maxHorizontalItems = boundingRect().width() / itemMaxWidth; |
|
368 | int maxHorizontalItems = boundingRect().width() / itemMaxWidth; | |
246 | int maxVerticalItems = boundingRect().height() / itemMaxHeight; |
|
369 | int maxVerticalItems = boundingRect().height() / itemMaxHeight; | |
247 |
|
370 | |||
248 | if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) { |
|
371 | if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) { | |
249 | // TODO: overlapping layout |
|
372 | // TODO: overlapping layout | |
250 | qDebug() << "Warning. Not enough space to layout all legend items properly."; |
|
373 | qDebug() << "Warning. Not enough space to layout all legend items properly."; | |
251 | } |
|
374 | } | |
252 |
|
375 | |||
253 | qreal margin = 5; |
|
376 | qreal margin = 5; | |
254 | qreal totalWidth = 0; |
|
377 | qreal totalWidth = 0; | |
255 | qreal totalHeight = 0; |
|
378 | qreal totalHeight = 0; | |
256 | switch (mPreferredLayout) |
|
379 | switch (mPreferredLayout) | |
257 | { |
|
380 | { | |
258 | case QLegend::PreferredLayoutHorizontal: { |
|
381 | case QLegend::PreferredLayoutHorizontal: { | |
259 | /* |
|
382 | /* | |
260 | qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); |
|
383 | qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); | |
261 | if (xStep > itemMaxWidth) { |
|
384 | if (xStep > itemMaxWidth) { | |
262 | xStep = itemMaxWidth; |
|
385 | xStep = itemMaxWidth; | |
263 | } |
|
386 | } | |
264 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); |
|
387 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); | |
265 | if (yStep > itemMaxHeight) { |
|
388 | if (yStep > itemMaxHeight) { | |
266 | yStep = itemMaxHeight; |
|
389 | yStep = itemMaxHeight; | |
267 | }*/ |
|
390 | }*/ | |
268 | qreal xStep = itemMaxWidth; |
|
391 | qreal xStep = itemMaxWidth; | |
269 | qreal yStep = itemMaxHeight; |
|
392 | qreal yStep = itemMaxHeight; | |
270 | qreal x = mPos.x() + margin; |
|
393 | qreal x = mPos.x() + margin; | |
271 | qreal y = mPos.y() + margin; |
|
394 | qreal y = mPos.y() + margin; | |
272 | int row = 1; |
|
395 | int row = 1; | |
273 | int column = 0; |
|
396 | int column = 0; | |
274 | int maxRows = 1; |
|
397 | int maxRows = 1; | |
275 | int maxColumns = 1; |
|
398 | int maxColumns = 1; | |
276 | foreach (LegendMarker* m, mMarkers) { |
|
399 | foreach (LegendMarker* m, mMarkers) { | |
277 | maxRows = row; |
|
400 | maxRows = row; | |
278 | m->setPos(x,y); |
|
401 | m->setPos(x,y); | |
279 | x += xStep; |
|
402 | x += xStep; | |
280 | column++; |
|
403 | column++; | |
281 | if (column > maxColumns) { |
|
404 | if (column > maxColumns) { | |
282 | maxColumns = column; |
|
405 | maxColumns = column; | |
283 | } |
|
406 | } | |
284 | if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) { |
|
407 | if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) { | |
285 | x = mPos.x() + margin; |
|
408 | x = mPos.x() + margin; | |
286 | y += yStep; |
|
409 | y += yStep; | |
287 | row++; |
|
410 | row++; | |
288 | column = 0; |
|
411 | column = 0; | |
289 | } |
|
412 | } | |
290 | } |
|
413 | } | |
291 | totalWidth = maxColumns * itemMaxWidth + margin * 2; |
|
414 | totalWidth = maxColumns * itemMaxWidth + margin * 2; | |
292 | totalHeight = maxRows * itemMaxHeight + margin * 2; |
|
415 | totalHeight = maxRows * itemMaxHeight + margin * 2; | |
293 | break; |
|
416 | break; | |
294 | } |
|
417 | } | |
295 | case QLegend::PreferredLayoutVertical: { |
|
418 | case QLegend::PreferredLayoutVertical: { | |
296 | /* |
|
419 | /* | |
297 | qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); |
|
420 | qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); | |
298 | if (xStep > itemMaxWidth) { |
|
421 | if (xStep > itemMaxWidth) { | |
299 | xStep = itemMaxWidth; |
|
422 | xStep = itemMaxWidth; | |
300 | } |
|
423 | } | |
301 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); |
|
424 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); | |
302 | if (yStep > itemMaxHeight) { |
|
425 | if (yStep > itemMaxHeight) { | |
303 | yStep = itemMaxHeight; |
|
426 | yStep = itemMaxHeight; | |
304 | }*/ |
|
427 | }*/ | |
305 | qreal xStep = itemMaxWidth; |
|
428 | qreal xStep = itemMaxWidth; | |
306 | qreal yStep = itemMaxHeight; |
|
429 | qreal yStep = itemMaxHeight; | |
307 | qreal x = mPos.x() + margin; |
|
430 | qreal x = mPos.x() + margin; | |
308 | qreal y = mPos.y() + margin; |
|
431 | qreal y = mPos.y() + margin; | |
309 | int row = 0; |
|
432 | int row = 0; | |
310 | int column = 1; |
|
433 | int column = 1; | |
311 | int maxRows = 1; |
|
434 | int maxRows = 1; | |
312 | int maxColumns = 1; |
|
435 | int maxColumns = 1; | |
313 | foreach (LegendMarker* m, mMarkers) { |
|
436 | foreach (LegendMarker* m, mMarkers) { | |
314 | maxColumns = column; |
|
437 | maxColumns = column; | |
315 | m->setPos(x,y); |
|
438 | m->setPos(x,y); | |
316 | y += yStep; |
|
439 | y += yStep; | |
317 | row++; |
|
440 | row++; | |
318 | if (row > maxRows) { |
|
441 | if (row > maxRows) { | |
319 | maxRows = row; |
|
442 | maxRows = row; | |
320 | } |
|
443 | } | |
321 | if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) { |
|
444 | if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) { | |
322 | y = mPos.y() + margin; |
|
445 | y = mPos.y() + margin; | |
323 | x += xStep; |
|
446 | x += xStep; | |
324 | column++; |
|
447 | column++; | |
325 | row = 0; |
|
448 | row = 0; | |
326 | } |
|
449 | } | |
327 | } |
|
450 | } | |
328 | totalWidth = maxColumns * itemMaxWidth + margin * 2; |
|
451 | totalWidth = maxColumns * itemMaxWidth + margin * 2; | |
329 | totalHeight = maxRows * itemMaxHeight + margin * 2; |
|
452 | totalHeight = maxRows * itemMaxHeight + margin * 2; | |
330 | break; |
|
453 | break; | |
331 | } |
|
454 | } | |
332 | default: { |
|
455 | default: { | |
333 | break; |
|
456 | break; | |
334 | } |
|
457 | } | |
335 | } |
|
458 | } | |
336 |
|
459 | |||
337 | mSize.setWidth(totalWidth); |
|
460 | mSize.setWidth(totalWidth); | |
338 | mSize.setHeight(totalHeight); |
|
461 | mSize.setHeight(totalHeight); | |
339 | } |
|
462 | } | |
340 |
|
463 | |||
341 | #include "moc_qlegend.cpp" |
|
464 | #include "moc_qlegend.cpp" | |
342 | QTCOMMERCIALCHART_END_NAMESPACE |
|
465 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,80 +1,85 | |||||
1 | #ifndef QLEGEND_H |
|
1 | #ifndef QLEGEND_H | |
2 | #define QLEGEND_H |
|
2 | #define QLEGEND_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "qseries.h" |
|
5 | #include "qseries.h" | |
6 | #include <QGraphicsObject> |
|
6 | #include <QGraphicsObject> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class Domain; |
|
10 | class Domain; | |
11 | class LegendMarker; |
|
11 | class LegendMarker; | |
12 | class QPieSlice; |
|
12 | class QPieSlice; | |
13 | class QXYSeries; |
|
13 | class QXYSeries; | |
14 | class QBarSet; |
|
14 | class QBarSet; | |
15 | class QBarSeries; |
|
15 | class QBarSeries; | |
16 | class QPieSeries; |
|
16 | class QPieSeries; | |
17 |
|
17 | |||
18 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject |
|
18 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject | |
19 | { |
|
19 | { | |
20 | Q_OBJECT |
|
20 | Q_OBJECT | |
21 | public: |
|
21 | public: | |
22 |
|
22 | |||
23 | enum PreferredLayout { |
|
23 | enum PreferredLayout { | |
24 | PreferredLayoutHorizontal, |
|
24 | PreferredLayoutHorizontal, | |
25 | PreferredLayoutVertical |
|
25 | PreferredLayoutVertical | |
26 | }; |
|
26 | }; | |
27 |
|
27 | |||
28 | explicit QLegend(QGraphicsItem *parent = 0); |
|
28 | explicit QLegend(QGraphicsItem *parent = 0); | |
29 |
|
29 | |||
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |
31 | QRectF boundingRect() const; |
|
31 | QRectF boundingRect() const; | |
32 |
|
32 | |||
33 | void setBackgroundBrush(const QBrush& brush); |
|
33 | void setBackgroundBrush(const QBrush& brush); | |
34 | QBrush backgroundBrush() const; |
|
34 | QBrush backgroundBrush() const; | |
35 |
|
35 | |||
36 | void setPreferredLayout(QLegend::PreferredLayout preferred); |
|
36 | void setPreferredLayout(QLegend::PreferredLayout preferred); | |
37 |
|
37 | |||
38 | QSizeF maximumSize() const; |
|
38 | QSizeF maximumSize() const; | |
39 | void setMaximumSize(const QSizeF size); |
|
39 | void setMaximumSize(const QSizeF size); | |
40 |
|
40 | |||
41 | void setSize(const QSizeF size); |
|
41 | void setSize(const QSizeF size); | |
42 | void setPos(const QPointF &pos); |
|
42 | void setPos(const QPointF &pos); | |
43 |
|
43 | |||
44 | signals: |
|
44 | signals: | |
45 | // for interactions. |
|
45 | // for interactions. | |
46 | void clicked(QSeries* series, Qt::MouseButton button); |
|
46 | void clicked(QSeries* series, Qt::MouseButton button); | |
47 | void clicked(QBarSet* barset, Qt::MouseButton button); |
|
47 | void clicked(QBarSet* barset, Qt::MouseButton button); | |
48 | void clicked(QPieSlice* slice, Qt::MouseButton button); |
|
48 | void clicked(QPieSlice* slice, Qt::MouseButton button); | |
49 |
|
49 | |||
50 | public slots: |
|
50 | public slots: | |
51 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
51 | void handleSeriesAdded(QSeries* series,Domain* domain); | |
52 | void handleSeriesRemoved(QSeries* series); |
|
52 | void handleSeriesRemoved(QSeries* series); | |
|
53 | void handleAdded(QList<QPieSlice*> slices); | |||
|
54 | // void handleRemoved(QList<QPieSlice*> slices); | |||
|
55 | void handleMarkerDestroyed(); | |||
53 |
|
56 | |||
54 | private: |
|
57 | private: | |
55 | // PIMPL ---> |
|
58 | // PIMPL ---> | |
|
59 | void connectSeries(QSeries* series); | |||
|
60 | void disconnectSeries(QSeries* series); | |||
56 | void createMarkers(QSeries* series); |
|
61 | void createMarkers(QSeries* series); | |
57 | void appendMarkers(QXYSeries* series); // All line series are derived from QXYSeries, so this works for now |
|
62 | void appendMarkers(QXYSeries* series); // All line series are derived from QXYSeries, so this works for now | |
58 | void appendMarkers(QBarSeries* series); |
|
63 | void appendMarkers(QBarSeries* series); | |
59 | void appendMarkers(QPieSeries* series); |
|
64 | void appendMarkers(QPieSeries* series); | |
60 | void deleteMarkers(QSeries* series); |
|
65 | void deleteMarkers(QSeries* series); | |
61 | void layoutChanged(); // TODO: rename this to layoutChanged and remove original layoutChanged, when ready |
|
66 | void layoutChanged(); | |
62 | // <--- PIMPL |
|
67 | // <--- PIMPL | |
63 |
|
68 | |||
64 |
|
69 | |||
65 | // QRectF mBoundingRect; |
|
70 | // QRectF mBoundingRect; | |
66 | QPointF mPos; |
|
71 | QPointF mPos; | |
67 | QSizeF mSize; |
|
72 | QSizeF mSize; | |
68 | QSizeF mMinimumSize; |
|
73 | QSizeF mMinimumSize; | |
69 | QSizeF mMaximumSize; |
|
74 | QSizeF mMaximumSize; | |
70 |
|
75 | |||
71 | QList<QSeries*> mSeriesList; |
|
76 | QList<QSeries*> mSeriesList; | |
72 | QList<LegendMarker*> mMarkers; |
|
77 | QList<LegendMarker*> mMarkers; | |
73 |
|
78 | |||
74 | QBrush mBackgroundBrush; |
|
79 | QBrush mBackgroundBrush; | |
75 | QLegend::PreferredLayout mPreferredLayout; |
|
80 | QLegend::PreferredLayout mPreferredLayout; | |
76 | }; |
|
81 | }; | |
77 |
|
82 | |||
78 | QTCOMMERCIALCHART_END_NAMESPACE |
|
83 | QTCOMMERCIALCHART_END_NAMESPACE | |
79 |
|
84 | |||
80 | #endif // QLEGEND_H |
|
85 | #endif // QLEGEND_H |
General Comments 0
You need to be logged in to leave comments.
Login now