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