##// END OF EJS Templates
legend theme fix
sauimone -
r1527:14e346f15b06
parent child
Show More
@@ -1,383 +1,391
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "charttheme_p.h"
21 #include "charttheme_p.h"
22 #include "qchart.h"
22 #include "qchart.h"
23 #include "qchart_p.h"
23 #include "qchart_p.h"
24 #include "qchartview.h"
24 #include "qchartview.h"
25 #include "qlegend.h"
25 #include "qlegend.h"
26 #include "qaxis.h"
26 #include "qaxis.h"
27 #include <QTime>
27 #include <QTime>
28
28
29 //series
29 //series
30 #include "qbarset.h"
30 #include "qbarset.h"
31 #include "qbarseries.h"
31 #include "qbarseries.h"
32 #include "qstackedbarseries.h"
32 #include "qstackedbarseries.h"
33 #include "qpercentbarseries.h"
33 #include "qpercentbarseries.h"
34 #include "qlineseries.h"
34 #include "qlineseries.h"
35 #include "qareaseries.h"
35 #include "qareaseries.h"
36 #include "qscatterseries.h"
36 #include "qscatterseries.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieslice.h"
38 #include "qpieslice.h"
39 #include "qpieslice_p.h"
39 #include "qpieslice_p.h"
40 #include "qsplineseries.h"
40 #include "qsplineseries.h"
41
41
42 //items
42 //items
43 #include "chartaxis_p.h"
43 #include "chartaxis_p.h"
44 #include "barchartitem_p.h"
44 #include "barchartitem_p.h"
45 #include "stackedbarchartitem_p.h"
45 #include "stackedbarchartitem_p.h"
46 #include "percentbarchartitem_p.h"
46 #include "percentbarchartitem_p.h"
47 #include "linechartitem_p.h"
47 #include "linechartitem_p.h"
48 #include "areachartitem_p.h"
48 #include "areachartitem_p.h"
49 #include "scatterchartitem_p.h"
49 #include "scatterchartitem_p.h"
50 #include "piechartitem_p.h"
50 #include "piechartitem_p.h"
51 #include "splinechartitem_p.h"
51 #include "splinechartitem_p.h"
52
52
53 //themes
53 //themes
54 #include "chartthemesystem_p.h"
54 #include "chartthemesystem_p.h"
55 #include "chartthemelight_p.h"
55 #include "chartthemelight_p.h"
56 #include "chartthemebluecerulean_p.h"
56 #include "chartthemebluecerulean_p.h"
57 #include "chartthemedark_p.h"
57 #include "chartthemedark_p.h"
58 #include "chartthemebrownsand_p.h"
58 #include "chartthemebrownsand_p.h"
59 #include "chartthemebluencs_p.h"
59 #include "chartthemebluencs_p.h"
60 #include "chartthemehighcontrast_p.h"
60 #include "chartthemehighcontrast_p.h"
61 #include "chartthemeblueicy_p.h"
61 #include "chartthemeblueicy_p.h"
62
62
63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
64
64
65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
66 m_masterFont(QFont("arial", 14)),
66 m_masterFont(QFont("arial", 14)),
67 m_labelFont(QFont("arial", 10)),
67 m_labelFont(QFont("arial", 10)),
68 m_titleBrush(QColor(QRgb(0x000000))),
68 m_titleBrush(QColor(QRgb(0x000000))),
69 m_axisLinePen(QPen(QRgb(0x000000))),
69 m_axisLinePen(QPen(QRgb(0x000000))),
70 m_axisLabelBrush(QColor(QRgb(0x000000))),
70 m_axisLabelBrush(QColor(QRgb(0x000000))),
71 m_backgroundShadesPen(Qt::NoPen),
71 m_backgroundShadesPen(Qt::NoPen),
72 m_backgroundShadesBrush(Qt::NoBrush),
72 m_backgroundShadesBrush(Qt::NoBrush),
73 m_backgroundShades(BackgroundShadesNone),
73 m_backgroundShades(BackgroundShadesNone),
74 m_backgroundDropShadowEnabled(false),
74 m_backgroundDropShadowEnabled(false),
75 m_gridLinePen(QPen(QRgb(0x000000))),
75 m_gridLinePen(QPen(QRgb(0x000000))),
76 m_force(false)
76 m_force(false)
77 {
77 {
78 m_id = id;
78 m_id = id;
79 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
79 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
80 }
80 }
81
81
82
82
83 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
83 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
84 {
84 {
85 switch(theme) {
85 switch(theme) {
86 case QChart::ChartThemeLight:
86 case QChart::ChartThemeLight:
87 return new ChartThemeLight();
87 return new ChartThemeLight();
88 case QChart::ChartThemeBlueCerulean:
88 case QChart::ChartThemeBlueCerulean:
89 return new ChartThemeBlueCerulean();
89 return new ChartThemeBlueCerulean();
90 case QChart::ChartThemeDark:
90 case QChart::ChartThemeDark:
91 return new ChartThemeDark();
91 return new ChartThemeDark();
92 case QChart::ChartThemeBrownSand:
92 case QChart::ChartThemeBrownSand:
93 return new ChartThemeBrownSand();
93 return new ChartThemeBrownSand();
94 case QChart::ChartThemeBlueNcs:
94 case QChart::ChartThemeBlueNcs:
95 return new ChartThemeBlueNcs();
95 return new ChartThemeBlueNcs();
96 case QChart::ChartThemeHighContrast:
96 case QChart::ChartThemeHighContrast:
97 return new ChartThemeHighContrast();
97 return new ChartThemeHighContrast();
98 case QChart::ChartThemeBlueIcy:
98 case QChart::ChartThemeBlueIcy:
99 return new ChartThemeBlueIcy();
99 return new ChartThemeBlueIcy();
100 default:
100 default:
101 return new ChartThemeSystem();
101 return new ChartThemeSystem();
102 }
102 }
103 }
103 }
104
104
105 void ChartTheme::decorate(QChart *chart)
105 void ChartTheme::decorate(QChart *chart)
106 {
106 {
107 QBrush brush;
107 QBrush brush;
108
108
109 if(brush == chart->backgroundBrush() || m_force)
109 if(brush == chart->backgroundBrush() || m_force)
110 chart->setBackgroundBrush(m_chartBackgroundGradient);
110 chart->setBackgroundBrush(m_chartBackgroundGradient);
111 chart->setTitleFont(m_masterFont);
111 chart->setTitleFont(m_masterFont);
112 chart->setTitleBrush(m_titleBrush);
112 chart->setTitleBrush(m_titleBrush);
113 chart->setDropShadowEnabled(m_backgroundDropShadowEnabled);
113 chart->setDropShadowEnabled(m_backgroundDropShadowEnabled);
114 }
114 }
115
115
116 void ChartTheme::decorate(QLegend *legend)
116 void ChartTheme::decorate(QLegend *legend)
117 {
117 {
118 QPen pen;
118 QPen pen;
119 QBrush brush;
119 QBrush brush;
120 QFont font;
120
121
121 if (pen == legend->pen() || m_force)
122 if (pen == legend->pen() || m_force)
122 legend->setPen(m_axisLinePen);
123 legend->setPen(m_axisLinePen);
123
124
124 if (brush == legend->brush() || m_force)
125 if (brush == legend->brush() || m_force)
125 legend->setBrush(m_chartBackgroundGradient);
126 legend->setBrush(m_chartBackgroundGradient);
127
128 // TODO: should legend have own brush & font defined by theme?
129 if (font == legend->font() || m_force)
130 legend->setFont(m_labelFont);
131
132 if (brush == legend->labelBrush() || m_force)
133 legend->setLabelBrush(m_axisLabelBrush);
126 }
134 }
127
135
128 void ChartTheme::decorate(QAreaSeries *series, int index)
136 void ChartTheme::decorate(QAreaSeries *series, int index)
129 {
137 {
130 QPen pen;
138 QPen pen;
131 QBrush brush;
139 QBrush brush;
132
140
133 if (pen == series->pen() || m_force){
141 if (pen == series->pen() || m_force){
134 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
142 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
135 pen.setWidthF(2);
143 pen.setWidthF(2);
136 series->setPen(pen);
144 series->setPen(pen);
137 }
145 }
138
146
139 if (brush == series->brush() || m_force) {
147 if (brush == series->brush() || m_force) {
140 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
148 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
141 series->setBrush(brush);
149 series->setBrush(brush);
142 }
150 }
143 }
151 }
144
152
145
153
146 void ChartTheme::decorate(QLineSeries *series,int index)
154 void ChartTheme::decorate(QLineSeries *series,int index)
147 {
155 {
148 QPen pen;
156 QPen pen;
149 if(pen == series->pen() || m_force ){
157 if(pen == series->pen() || m_force ){
150 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
158 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
151 pen.setWidthF(2);
159 pen.setWidthF(2);
152 series->setPen(pen);
160 series->setPen(pen);
153 }
161 }
154 }
162 }
155
163
156 void ChartTheme::decorate(QBarSeries *series, int index)
164 void ChartTheme::decorate(QBarSeries *series, int index)
157 {
165 {
158 QBrush brush;
166 QBrush brush;
159 QPen pen;
167 QPen pen;
160 QList<QBarSet *> sets = series->barSets();
168 QList<QBarSet *> sets = series->barSets();
161
169
162 qreal takeAtPos = 0.5;
170 qreal takeAtPos = 0.5;
163 qreal step = 0.2;
171 qreal step = 0.2;
164 if (sets.count() > 1 ) {
172 if (sets.count() > 1 ) {
165 step = 1.0 / (qreal) sets.count();
173 step = 1.0 / (qreal) sets.count();
166 if (sets.count() % m_seriesGradients.count())
174 if (sets.count() % m_seriesGradients.count())
167 step *= m_seriesGradients.count();
175 step *= m_seriesGradients.count();
168 else
176 else
169 step *= (m_seriesGradients.count() - 1);
177 step *= (m_seriesGradients.count() - 1);
170 }
178 }
171
179
172 for (int i(0); i < sets.count(); i++) {
180 for (int i(0); i < sets.count(); i++) {
173 int colorIndex = (index + i) % m_seriesGradients.count();
181 int colorIndex = (index + i) % m_seriesGradients.count();
174 if (i > 0 && i % m_seriesGradients.count() == 0) {
182 if (i > 0 && i % m_seriesGradients.count() == 0) {
175 // There is no dedicated base color for each sets, generate more colors
183 // There is no dedicated base color for each sets, generate more colors
176 takeAtPos += step;
184 takeAtPos += step;
177 if (takeAtPos == 1.0)
185 if (takeAtPos == 1.0)
178 takeAtPos += step;
186 takeAtPos += step;
179 takeAtPos -= (int) takeAtPos;
187 takeAtPos -= (int) takeAtPos;
180 }
188 }
181 if (brush == sets.at(i)->brush() || m_force )
189 if (brush == sets.at(i)->brush() || m_force )
182 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
190 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
183
191
184 // Pick label color from the opposite end of the gradient.
192 // Pick label color from the opposite end of the gradient.
185 // 0.3 as a boundary seems to work well.
193 // 0.3 as a boundary seems to work well.
186 if (takeAtPos < 0.3)
194 if (takeAtPos < 0.3)
187 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
195 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
188 else
196 else
189 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
197 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
190
198
191 if (pen == sets.at(i)->pen() || m_force) {
199 if (pen == sets.at(i)->pen() || m_force) {
192 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
200 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
193 sets.at(i)->setPen(c);
201 sets.at(i)->setPen(c);
194 }
202 }
195 }
203 }
196 }
204 }
197
205
198 void ChartTheme::decorate(QScatterSeries *series, int index)
206 void ChartTheme::decorate(QScatterSeries *series, int index)
199 {
207 {
200 QPen pen;
208 QPen pen;
201 QBrush brush;
209 QBrush brush;
202
210
203 if (pen == series->pen() || m_force) {
211 if (pen == series->pen() || m_force) {
204 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
212 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
205 pen.setWidthF(2);
213 pen.setWidthF(2);
206 series->setPen(pen);
214 series->setPen(pen);
207 }
215 }
208
216
209 if (brush == series->brush() || m_force) {
217 if (brush == series->brush() || m_force) {
210 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
218 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
211 series->setBrush(brush);
219 series->setBrush(brush);
212 }
220 }
213 }
221 }
214
222
215 void ChartTheme::decorate(QPieSeries *series, int index)
223 void ChartTheme::decorate(QPieSeries *series, int index)
216 {
224 {
217
225
218 for (int i(0); i < series->slices().count(); i++) {
226 for (int i(0); i < series->slices().count(); i++) {
219
227
220 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
228 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
221
229
222 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
230 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
223 qreal pos = (qreal) (i + 1) / (qreal) series->count();
231 qreal pos = (qreal) (i + 1) / (qreal) series->count();
224 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
232 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
225
233
226 QPieSlice *s = series->slices().at(i);
234 QPieSlice *s = series->slices().at(i);
227 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
235 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
228
236
229 if (d->m_data.m_slicePen.isThemed() || m_force)
237 if (d->m_data.m_slicePen.isThemed() || m_force)
230 d->setPen(penColor, true);
238 d->setPen(penColor, true);
231
239
232 if (d->m_data.m_sliceBrush.isThemed() || m_force)
240 if (d->m_data.m_sliceBrush.isThemed() || m_force)
233 d->setBrush(brushColor, true);
241 d->setBrush(brushColor, true);
234
242
235 if (d->m_data.m_labelBrush.isThemed() || m_force)
243 if (d->m_data.m_labelBrush.isThemed() || m_force)
236 d->setLabelBrush(QBrush(m_titleBrush.color()), true);
244 d->setLabelBrush(QBrush(m_titleBrush.color()), true);
237
245
238 if (d->m_data.m_labelFont.isThemed() || m_force)
246 if (d->m_data.m_labelFont.isThemed() || m_force)
239 d->setLabelFont(m_labelFont, true);
247 d->setLabelFont(m_labelFont, true);
240 }
248 }
241 }
249 }
242
250
243 void ChartTheme::decorate(QSplineSeries *series, int index)
251 void ChartTheme::decorate(QSplineSeries *series, int index)
244 {
252 {
245 QPen pen;
253 QPen pen;
246 if(pen == series->pen() || m_force){
254 if(pen == series->pen() || m_force){
247 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
255 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
248 pen.setWidthF(2);
256 pen.setWidthF(2);
249 series->setPen(pen);
257 series->setPen(pen);
250 }
258 }
251 }
259 }
252
260
253 void ChartTheme::decorate(QAxis *axis,bool axisX)
261 void ChartTheme::decorate(QAxis *axis,bool axisX)
254 {
262 {
255 QPen pen;
263 QPen pen;
256 QBrush brush;
264 QBrush brush;
257 QFont font;
265 QFont font;
258
266
259 if (axis->isAxisVisible()) {
267 if (axis->isAxisVisible()) {
260
268
261 if(brush == axis->labelsBrush() || m_force){
269 if(brush == axis->labelsBrush() || m_force){
262 axis->setLabelsBrush(m_axisLabelBrush);
270 axis->setLabelsBrush(m_axisLabelBrush);
263 }
271 }
264 if(pen == axis->labelsPen() || m_force){
272 if(pen == axis->labelsPen() || m_force){
265 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
273 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
266 }
274 }
267
275
268
276
269 if (axis->shadesVisible() || m_force) {
277 if (axis->shadesVisible() || m_force) {
270
278
271 if(brush == axis->shadesBrush() || m_force){
279 if(brush == axis->shadesBrush() || m_force){
272 axis->setShadesBrush(m_backgroundShadesBrush);
280 axis->setShadesBrush(m_backgroundShadesBrush);
273 }
281 }
274
282
275 if(pen == axis->shadesPen() || m_force){
283 if(pen == axis->shadesPen() || m_force){
276 axis->setShadesPen(m_backgroundShadesPen);
284 axis->setShadesPen(m_backgroundShadesPen);
277 }
285 }
278
286
279 if( m_force && (m_backgroundShades == BackgroundShadesBoth
287 if( m_force && (m_backgroundShades == BackgroundShadesBoth
280 || (m_backgroundShades == BackgroundShadesVertical && axisX)
288 || (m_backgroundShades == BackgroundShadesVertical && axisX)
281 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
289 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
282 axis->setShadesVisible(true);
290 axis->setShadesVisible(true);
283
291
284 }
292 }
285 }
293 }
286
294
287 if(pen == axis->axisPen() || m_force){
295 if(pen == axis->axisPen() || m_force){
288 axis->setAxisPen(m_axisLinePen);
296 axis->setAxisPen(m_axisLinePen);
289 }
297 }
290
298
291 if(pen == axis->gridLinePen() || m_force){
299 if(pen == axis->gridLinePen() || m_force){
292 axis->setGridLinePen(m_gridLinePen);
300 axis->setGridLinePen(m_gridLinePen);
293 }
301 }
294
302
295 if(font == axis->labelsFont() || m_force){
303 if(font == axis->labelsFont() || m_force){
296 axis->setLabelsFont(m_labelFont);
304 axis->setLabelsFont(m_labelFont);
297 }
305 }
298 }
306 }
299 }
307 }
300
308
301 void ChartTheme::generateSeriesGradients()
309 void ChartTheme::generateSeriesGradients()
302 {
310 {
303 // Generate gradients in HSV color space
311 // Generate gradients in HSV color space
304 foreach (const QColor& color, m_seriesColors) {
312 foreach (const QColor& color, m_seriesColors) {
305 QLinearGradient g;
313 QLinearGradient g;
306 qreal h = color.hsvHueF();
314 qreal h = color.hsvHueF();
307 qreal s = color.hsvSaturationF();
315 qreal s = color.hsvSaturationF();
308
316
309 // TODO: tune the algorithm to give nice results with most base colors defined in
317 // TODO: tune the algorithm to give nice results with most base colors defined in
310 // most themes. The rest of the gradients we can define manually in theme specific
318 // most themes. The rest of the gradients we can define manually in theme specific
311 // implementation.
319 // implementation.
312 QColor start = color;
320 QColor start = color;
313 start.setHsvF(h, 0.0, 1.0);
321 start.setHsvF(h, 0.0, 1.0);
314 g.setColorAt(0.0, start);
322 g.setColorAt(0.0, start);
315
323
316 g.setColorAt(0.5, color);
324 g.setColorAt(0.5, color);
317
325
318 QColor end = color;
326 QColor end = color;
319 end.setHsvF(h, s, 0.25);
327 end.setHsvF(h, s, 0.25);
320 g.setColorAt(1.0, end);
328 g.setColorAt(1.0, end);
321
329
322 m_seriesGradients << g;
330 m_seriesGradients << g;
323 }
331 }
324 }
332 }
325
333
326
334
327 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
335 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
328 {
336 {
329 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
337 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
330 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
338 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
331 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
339 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
332 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
340 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
333 QColor c;
341 QColor c;
334 c.setRgbF(r, g, b);
342 c.setRgbF(r, g, b);
335 return c;
343 return c;
336 }
344 }
337
345
338 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
346 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
339 {
347 {
340 Q_ASSERT(pos >= 0 && pos <= 1.0);
348 Q_ASSERT(pos >= 0 && pos <= 1.0);
341
349
342 QGradientStops stops = gradient.stops();
350 QGradientStops stops = gradient.stops();
343 int count = stops.count();
351 int count = stops.count();
344
352
345 // find previous stop relative to position
353 // find previous stop relative to position
346 QGradientStop prev = stops.first();
354 QGradientStop prev = stops.first();
347 for (int i = 0; i < count; i++) {
355 for (int i = 0; i < count; i++) {
348 QGradientStop stop = stops.at(i);
356 QGradientStop stop = stops.at(i);
349 if (pos > stop.first)
357 if (pos > stop.first)
350 prev = stop;
358 prev = stop;
351
359
352 // given position is actually a stop position?
360 // given position is actually a stop position?
353 if (pos == stop.first) {
361 if (pos == stop.first) {
354 //qDebug() << "stop color" << pos;
362 //qDebug() << "stop color" << pos;
355 return stop.second;
363 return stop.second;
356 }
364 }
357 }
365 }
358
366
359 // find next stop relative to position
367 // find next stop relative to position
360 QGradientStop next = stops.last();
368 QGradientStop next = stops.last();
361 for (int i = count - 1; i >= 0; i--) {
369 for (int i = count - 1; i >= 0; i--) {
362 QGradientStop stop = stops.at(i);
370 QGradientStop stop = stops.at(i);
363 if (pos < stop.first)
371 if (pos < stop.first)
364 next = stop;
372 next = stop;
365 }
373 }
366
374
367 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
375 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
368
376
369 qreal range = next.first - prev.first;
377 qreal range = next.first - prev.first;
370 qreal posDelta = pos - prev.first;
378 qreal posDelta = pos - prev.first;
371 qreal relativePos = posDelta / range;
379 qreal relativePos = posDelta / range;
372
380
373 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
381 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
374
382
375 return colorAt(prev.second, next.second, relativePos);
383 return colorAt(prev.second, next.second, relativePos);
376 }
384 }
377
385
378 void ChartTheme::setForced(bool enabled)
386 void ChartTheme::setForced(bool enabled)
379 {
387 {
380 m_force=enabled;
388 m_force=enabled;
381 }
389 }
382
390
383 QTCOMMERCIALCHART_END_NAMESPACE
391 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,209 +1,231
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "legendmarker_p.h"
21 #include "legendmarker_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "qlegend.h"
24 #include "qlegend.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include "qpieslice.h"
27 #include "qpieslice.h"
28 #include "qbarset.h"
28 #include "qbarset.h"
29 #include "qbarset_p.h"
29 #include "qbarset_p.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qareaseries_p.h"
31 #include "qareaseries_p.h"
32 #include <QPainter>
32 #include <QPainter>
33 #include <QGraphicsSceneEvent>
33 #include <QGraphicsSceneEvent>
34 #include <QGraphicsSimpleTextItem>
34 #include <QGraphicsSimpleTextItem>
35 #include <QDebug>
35 #include <QDebug>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
40 QGraphicsObject(legend),
40 QGraphicsObject(legend),
41 m_series(series),
41 m_series(series),
42 m_markerRect(0,0,10.0,10.0),
42 m_markerRect(0,0,10.0,10.0),
43 m_boundingRect(0,0,0,0),
43 m_boundingRect(0,0,0,0),
44 m_legend(legend),
44 m_legend(legend),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
46 m_rectItem(new QGraphicsRectItem(this))
46 m_rectItem(new QGraphicsRectItem(this))
47 {
47 {
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
49 m_rectItem->setRect(m_markerRect);
49 m_rectItem->setRect(m_markerRect);
50 updateLayout();
50 updateLayout();
51 }
51 }
52
52
53 void LegendMarker::setPen(const QPen &pen)
53 void LegendMarker::setPen(const QPen &pen)
54 {
54 {
55 m_textItem->setPen(pen);
55 m_textItem->setPen(pen);
56 updateLayout();
56 updateLayout();
57 }
57 }
58
58
59 QPen LegendMarker::pen() const
59 QPen LegendMarker::pen() const
60 {
60 {
61 return m_textItem->pen();
61 return m_textItem->pen();
62 }
62 }
63
63
64 void LegendMarker::setBrush(const QBrush &brush)
64 void LegendMarker::setBrush(const QBrush &brush)
65 {
65 {
66 m_rectItem->setBrush(brush);
66 m_rectItem->setBrush(brush);
67 }
67 }
68
68
69 QBrush LegendMarker::brush() const
69 QBrush LegendMarker::brush() const
70 {
70 {
71 return m_rectItem->brush();
71 return m_rectItem->brush();
72 }
72 }
73
73
74 void LegendMarker::setFont(const QFont &font)
74 void LegendMarker::setFont(const QFont &font)
75 {
75 {
76 m_textItem->setFont(font);
76 m_textItem->setFont(font);
77 updateLayout();
77 updateLayout();
78 }
78 }
79
79
80 QFont LegendMarker::font() const
80 QFont LegendMarker::font() const
81 {
81 {
82 return m_textItem->font();
82 return m_textItem->font();
83 }
83 }
84
84
85 void LegendMarker::setLabel(const QString label)
85 void LegendMarker::setLabel(const QString label)
86 {
86 {
87 m_textItem->setText(label);
87 m_textItem->setText(label);
88 updateLayout();
88 updateLayout();
89 }
89 }
90
90
91 void LegendMarker::setSize(const QSize& size)
91 void LegendMarker::setSize(const QSize& size)
92 {
92 {
93 m_markerRect = QRectF(0,0,size.width(),size.height());
93 m_markerRect = QRectF(0,0,size.width(),size.height());
94 }
94 }
95
95
96 QString LegendMarker::label() const
96 QString LegendMarker::label() const
97 {
97 {
98 return m_textItem->text();
98 return m_textItem->text();
99 }
99 }
100
100
101 void LegendMarker::setLabelBrush(const QBrush &brush)
102 {
103 m_textItem->setBrush(brush);
104 updateLayout();
105 }
106
107 QBrush LegendMarker::labelBrush() const
108 {
109 return m_textItem->brush();
110 }
111
112 void LegendMarker::setLabelPen(const QPen &pen)
113 {
114 m_textItem->setPen(pen);
115 updateLayout();
116 }
117
118 QPen LegendMarker::labelPen() const
119 {
120 return m_textItem->pen();
121 }
122
101 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
123 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
102 {
124 {
103 Q_UNUSED(option)
125 Q_UNUSED(option)
104 Q_UNUSED(widget)
126 Q_UNUSED(widget)
105 Q_UNUSED(painter)
127 Q_UNUSED(painter)
106 }
128 }
107
129
108 QRectF LegendMarker::boundingRect() const
130 QRectF LegendMarker::boundingRect() const
109 {
131 {
110 return m_boundingRect;
132 return m_boundingRect;
111 }
133 }
112
134
113 void LegendMarker::updateLayout()
135 void LegendMarker::updateLayout()
114 {
136 {
115
137
116 static const qreal margin = 2;
138 static const qreal margin = 2;
117 static const qreal space = 4;
139 static const qreal space = 4;
118
140
119 const QRectF& textRect = m_textItem->boundingRect();
141 const QRectF& textRect = m_textItem->boundingRect();
120 prepareGeometryChange();
142 prepareGeometryChange();
121 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
143 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
122 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
144 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
123 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
145 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
124
146
125 }
147 }
126
148
127 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
149 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
128 {
150 {
129 QGraphicsObject::mousePressEvent(event);
151 QGraphicsObject::mousePressEvent(event);
130 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
152 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
131 }
153 }
132
154
133 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
155 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
134
156
135 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
157 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
136 m_series(series)
158 m_series(series)
137 {
159 {
138 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
160 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
139 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
161 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
140 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
162 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
141 updated();
163 updated();
142 }
164 }
143
165
144 void AreaLegendMarker::updated()
166 void AreaLegendMarker::updated()
145 {
167 {
146 setBrush(m_series->brush());
168 setBrush(m_series->brush());
147 setLabel(m_series->name());
169 setLabel(m_series->name());
148 }
170 }
149
171
150 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
172 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151
173
152 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
174 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
153 m_barset(barset)
175 m_barset(barset)
154 {
176 {
155 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
177 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
156 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
178 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
157 updated();
179 updated();
158 }
180 }
159
181
160 void BarLegendMarker::updated()
182 void BarLegendMarker::updated()
161 {
183 {
162 setBrush(m_barset->brush());
184 setBrush(m_barset->brush());
163 setLabel(m_barset->label());
185 setLabel(m_barset->label());
164 }
186 }
165
187
166 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
167
189
168 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
190 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
169 m_pieslice(pieslice)
191 m_pieslice(pieslice)
170 {
192 {
171 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
193 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
172 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
194 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
173 updated();
195 updated();
174 }
196 }
175
197
176 void PieLegendMarker::updated()
198 void PieLegendMarker::updated()
177 {
199 {
178 setBrush(m_pieslice->brush());
200 setBrush(m_pieslice->brush());
179 setLabel(m_pieslice->label());
201 setLabel(m_pieslice->label());
180 }
202 }
181
203
182 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
204 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
183
205
184 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
206 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
185 m_series(series)
207 m_series(series)
186 {
208 {
187 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
209 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
188 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
210 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
189 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
211 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
190 updated();
212 updated();
191 }
213 }
192
214
193 void XYLegendMarker::updated()
215 void XYLegendMarker::updated()
194 {
216 {
195 setLabel(m_series->name());
217 setLabel(m_series->name());
196
218
197 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
219 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
198 {
220 {
199 setBrush(m_series->brush());
221 setBrush(m_series->brush());
200
222
201 }
223 }
202 else {
224 else {
203 setBrush(QBrush(m_series->pen().color()));
225 setBrush(QBrush(m_series->pen().color()));
204 }
226 }
205 }
227 }
206
228
207 #include "moc_legendmarker_p.cpp"
229 #include "moc_legendmarker_p.cpp"
208
230
209 QTCOMMERCIALCHART_END_NAMESPACE
231 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,137 +1,141
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef LEGENDMARKER_P_H
30 #ifndef LEGENDMARKER_P_H
31 #define LEGENDMARKER_P_H
31 #define LEGENDMARKER_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QBrush>
35 #include <QBrush>
36 #include <QPen>
36 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
37 #include <QGraphicsSimpleTextItem>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QAbstractSeries;
41 class QAbstractSeries;
42 class QAreaSeries;
42 class QAreaSeries;
43 class QXYSeries;
43 class QXYSeries;
44 class QBarSet;
44 class QBarSet;
45 class QBarSeries;
45 class QBarSeries;
46 class QPieSlice;
46 class QPieSlice;
47 class QLegend;
47 class QLegend;
48 class QPieSeries;
48 class QPieSeries;
49
49
50 class LegendMarker : public QGraphicsObject
50 class LegendMarker : public QGraphicsObject
51 {
51 {
52 Q_OBJECT
52 Q_OBJECT
53
53
54 public:
54 public:
55 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
55 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
56
56
57 void setPen(const QPen &pen);
57 void setPen(const QPen &pen);
58 QPen pen() const;
58 QPen pen() const;
59 void setBrush(const QBrush &brush);
59 void setBrush(const QBrush &brush);
60 QBrush brush() const;
60 QBrush brush() const;
61
61
62 void setFont(const QFont &font);
62 void setFont(const QFont &font);
63 QFont font() const;
63 QFont font() const;
64
64
65 void setSize(const QSize& size);
65 void setSize(const QSize& size);
66
66
67 void setLabel(const QString label);
67 void setLabel(const QString label);
68 QString label() const;
68 QString label() const;
69 void setLabelBrush(const QBrush &brush);
70 QBrush labelBrush() const;
71 void setLabelPen(const QPen &pen);
72 QPen labelPen() const;
69
73
70 QAbstractSeries *series() const { return m_series;}
74 QAbstractSeries *series() const { return m_series;}
71
75
72 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
76 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
73
77
74 QRectF boundingRect() const;
78 QRectF boundingRect() const;
75
79
76 void updateLayout();
80 void updateLayout();
77
81
78 protected:
82 protected:
79 // From QGraphicsObject
83 // From QGraphicsObject
80 void mousePressEvent(QGraphicsSceneMouseEvent *event);
84 void mousePressEvent(QGraphicsSceneMouseEvent *event);
81
85
82 public Q_SLOTS:
86 public Q_SLOTS:
83 virtual void updated() = 0;
87 virtual void updated() = 0;
84
88
85 protected:
89 protected:
86 QAbstractSeries *m_series;
90 QAbstractSeries *m_series;
87 QRectF m_markerRect;
91 QRectF m_markerRect;
88 QRectF m_boundingRect;
92 QRectF m_boundingRect;
89 QLegend* m_legend;
93 QLegend* m_legend;
90 QGraphicsSimpleTextItem *m_textItem;
94 QGraphicsSimpleTextItem *m_textItem;
91 QGraphicsRectItem *m_rectItem;
95 QGraphicsRectItem *m_rectItem;
92
96
93 };
97 };
94
98
95 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96 class XYLegendMarker : public LegendMarker
100 class XYLegendMarker : public LegendMarker
97 {
101 {
98 public:
102 public:
99 XYLegendMarker(QXYSeries *series, QLegend *legend);
103 XYLegendMarker(QXYSeries *series, QLegend *legend);
100 protected:
104 protected:
101 void updated();
105 void updated();
102 private:
106 private:
103 QXYSeries *m_series;
107 QXYSeries *m_series;
104 };
108 };
105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106 class AreaLegendMarker : public LegendMarker
110 class AreaLegendMarker : public LegendMarker
107 {
111 {
108 public:
112 public:
109 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
113 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
110 protected:
114 protected:
111 void updated();
115 void updated();
112 private:
116 private:
113 QAreaSeries *m_series;
117 QAreaSeries *m_series;
114 };
118 };
115 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
119 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116 class BarLegendMarker : public LegendMarker
120 class BarLegendMarker : public LegendMarker
117 {
121 {
118 public:
122 public:
119 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
123 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
120 protected:
124 protected:
121 void updated();
125 void updated();
122 private:
126 private:
123 QBarSet *m_barset;
127 QBarSet *m_barset;
124 };
128 };
125 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126 class PieLegendMarker : public LegendMarker
130 class PieLegendMarker : public LegendMarker
127 {
131 {
128 public:
132 public:
129 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
133 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
130 protected:
134 protected:
131 void updated();
135 void updated();
132 private:
136 private:
133 QPieSlice *m_pieslice;
137 QPieSlice *m_pieslice;
134 };
138 };
135
139
136 QTCOMMERCIALCHART_END_NAMESPACE
140 QTCOMMERCIALCHART_END_NAMESPACE
137 #endif // LEGENDMARKER_P_H
141 #endif // LEGENDMARKER_P_H
@@ -1,801 +1,873
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26
26
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "qxyseries.h"
28 #include "qxyseries.h"
29 #include "qlineseries.h"
29 #include "qlineseries.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qscatterseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
32 #include "qsplineseries.h"
33 #include "qbarseries.h"
33 #include "qbarseries.h"
34 #include "qstackedbarseries.h"
34 #include "qstackedbarseries.h"
35 #include "qpercentbarseries.h"
35 #include "qpercentbarseries.h"
36 #include "qbarset.h"
36 #include "qbarset.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieseries_p.h"
38 #include "qpieseries_p.h"
39 #include "qpieslice.h"
39 #include "qpieslice.h"
40 #include "chartpresenter_p.h"
40 #include "chartpresenter_p.h"
41 #include <QPainter>
41 #include <QPainter>
42 #include <QPen>
42 #include <QPen>
43 #include <QTimer>
43 #include <QTimer>
44
44
45 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
46
46
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48
48
49 /*!
49 /*!
50 \class QLegend
50 \class QLegend
51 \brief Legend object
51 \brief Legend object
52 \mainclass
52 \mainclass
53
53
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
56 handle the drawing manually.
56 handle the drawing manually.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
58
58
59 \image examples_percentbarchart_legend.png
59 \image examples_percentbarchart_legend.png
60
60
61 \sa QChart
61 \sa QChart
62 */
62 */
63 /*!
63 /*!
64 \qmlclass Legend QLegend
64 \qmlclass Legend QLegend
65 \brief Legend is part of QtCommercial Chart QML API.
65 \brief Legend is part of QtCommercial Chart QML API.
66
66
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
68 series have been changed. Legend is used via ChartView class. For example:
68 series have been changed. Legend is used via ChartView class. For example:
69 \code
69 \code
70 ChartView {
70 ChartView {
71 legend.visible: true
71 legend.visible: true
72 legend.alignment: Qt.AlignBottom
72 legend.alignment: Qt.AlignBottom
73 // Add a few series...
73 // Add a few series...
74 }
74 }
75 \endcode
75 \endcode
76
76
77 \image examples_percentbarchart_legend.png
77 \image examples_percentbarchart_legend.png
78 */
78 */
79
79
80 /*!
80 /*!
81 \property QLegend::alignment
81 \property QLegend::alignment
82 \brief The alignment of the legend.
82 \brief The alignment of the legend.
83
83
84 Legend paints on the defined position in the chart. The following alignments are supported:
84 Legend paints on the defined position in the chart. The following alignments are supported:
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
86 */
86 */
87 /*!
87 /*!
88 \qmlproperty Qt.Alignment Legend::alignment
88 \qmlproperty Qt.Alignment Legend::alignment
89 \brief The alignment of the legend.
89 \brief The alignment of the legend.
90
90
91 Legend paints on the defined position in the chart. The following alignments are supported:
91 Legend paints on the defined position in the chart. The following alignments are supported:
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
93 */
93 */
94
94
95 /*!
95 /*!
96 \property QLegend::backgroundVisible
96 \property QLegend::backgroundVisible
97 Whether the legend background is visible or not.
97 Whether the legend background is visible or not.
98 */
98 */
99 /*!
99 /*!
100 \qmlproperty bool Legend::backgroundVisible
100 \qmlproperty bool Legend::backgroundVisible
101 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
102 */
102 */
103
103
104 /*!
104 /*!
105 \property QLegend::color
105 \property QLegend::color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
108 */
108 */
109 /*!
109 /*!
110 \qmlproperty color Legend::color
110 \qmlproperty color Legend::color
111 The color of the legend, i.e. the background (brush) color.
111 The color of the legend, i.e. the background (brush) color.
112 */
112 */
113
113
114 /*!
114 /*!
115 \property QLegend::borderColor
115 \property QLegend::borderColor
116 The border color of the legend, i.e. the line color.
116 The border color of the legend, i.e. the line color.
117 */
117 */
118 /*!
118 /*!
119 \qmlproperty color Legend::borderColor
119 \qmlproperty color Legend::borderColor
120 The border color of the legend, i.e. the line color.
120 The border color of the legend, i.e. the line color.
121 */
121 */
122
122
123 /*!
123 /*!
124 \property QLegend::font
124 \property QLegend::font
125 The font of markers used by legend
125 The font of markers used by legend
126 */
126 */
127 /*!
127 /*!
128 \qmlproperty color Legend::font
128 \qmlproperty color Legend::font
129 The font of markers used by legend
129 The font of markers used by legend
130 */
130 */
131
131
132 /*!
132 /*!
133 \fn void QLegend::backgroundVisibleChanged(bool)
133 \fn void QLegend::backgroundVisibleChanged(bool)
134 The visibility of the legend background changed to \a visible.
134 The visibility of the legend background changed to \a visible.
135 */
135 */
136
136
137 /*!
137 /*!
138 \fn void QLegend::colorChanged(QColor)
138 \fn void QLegend::colorChanged(QColor)
139 The color of the legend background changed to \a color.
139 The color of the legend background changed to \a color.
140 */
140 */
141
141
142 /*!
142 /*!
143 \fn void QLegend::borderColorChanged(QColor)
143 \fn void QLegend::borderColorChanged(QColor)
144 The border color of the legend background changed to \a color.
144 The border color of the legend background changed to \a color.
145 */
145 */
146
146
147 /*!
147 /*!
148 \fn void QLegend::fontChanged(QFont)
148 \fn void QLegend::fontChanged(QFont)
149 The font of markers of the legend changed to \a font.
149 The font of markers of the legend changed to \a font.
150 */
150 */
151
151
152 /*!
152 /*!
153 \fn qreal QLegend::minWidth() const
153 \fn qreal QLegend::minWidth() const
154 Returns minimum width of the legend
154 Returns minimum width of the legend
155 */
155 */
156
156
157 /*!
157 /*!
158 \fn qreal QLegend::minHeight() const
158 \fn qreal QLegend::minHeight() const
159 Returns minimum height of the legend
159 Returns minimum height of the legend
160 */
160 */
161
161
162 /*!
162 /*!
163 Constructs the legend object and sets the parent to \a parent
163 Constructs the legend object and sets the parent to \a parent
164 */
164 */
165
165
166 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
166 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
167 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
167 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
168 {
168 {
169 setZValue(ChartPresenter::LegendZValue);
169 setZValue(ChartPresenter::LegendZValue);
170 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
170 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
171 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
171 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
172 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
172 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
173 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
173 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
174 }
174 }
175
175
176 /*!
176 /*!
177 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
177 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
178 */
178 */
179 QLegend::~QLegend()
179 QLegend::~QLegend()
180 {
180 {
181 }
181 }
182
182
183 /*!
183 /*!
184 \internal
184 \internal
185 */
185 */
186 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
186 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
187 {
187 {
188 Q_UNUSED(option)
188 Q_UNUSED(option)
189 Q_UNUSED(widget)
189 Q_UNUSED(widget)
190 if(!d_ptr->m_backgroundVisible) return;
190 if(!d_ptr->m_backgroundVisible) return;
191
191
192 painter->setOpacity(opacity());
192 painter->setOpacity(opacity());
193 painter->setPen(d_ptr->m_pen);
193 painter->setPen(d_ptr->m_pen);
194 painter->setBrush(d_ptr->m_brush);
194 painter->setBrush(d_ptr->m_brush);
195 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
195 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
196 }
196 }
197
197
198 /*!
198 /*!
199 \internal
199 \internal
200 */
200 */
201 QRectF QLegend::boundingRect() const
201 QRectF QLegend::boundingRect() const
202 {
202 {
203 return d_ptr->m_rect;
203 return d_ptr->m_rect;
204 }
204 }
205
205
206 /*!
206 /*!
207 Sets the \a brush of legend. Brush affects the background of legend.
207 Sets the \a brush of legend. Brush affects the background of legend.
208 */
208 */
209 void QLegend::setBrush(const QBrush &brush)
209 void QLegend::setBrush(const QBrush &brush)
210 {
210 {
211 if (d_ptr->m_brush != brush) {
211 if (d_ptr->m_brush != brush) {
212 d_ptr->m_brush = brush;
212 d_ptr->m_brush = brush;
213 update();
213 update();
214 }
214 }
215 }
215 }
216
216
217 /*!
217 /*!
218 Returns the brush used by legend.
218 Returns the brush used by legend.
219 */
219 */
220 QBrush QLegend::brush() const
220 QBrush QLegend::brush() const
221 {
221 {
222 return d_ptr->m_brush;
222 return d_ptr->m_brush;
223 }
223 }
224
224
225 void QLegend::setColor(QColor color)
225 void QLegend::setColor(QColor color)
226 {
226 {
227 QBrush b = d_ptr->m_brush;
227 QBrush b = d_ptr->m_brush;
228 if (b.style() != Qt::SolidPattern || b.color() != color) {
228 if (b.style() != Qt::SolidPattern || b.color() != color) {
229 b.setStyle(Qt::SolidPattern);
229 b.setStyle(Qt::SolidPattern);
230 b.setColor(color);
230 b.setColor(color);
231 setBrush(b);
231 setBrush(b);
232 emit colorChanged(color);
232 emit colorChanged(color);
233 }
233 }
234 }
234 }
235
235
236 QColor QLegend::color()
236 QColor QLegend::color()
237 {
237 {
238 return d_ptr->m_brush.color();
238 return d_ptr->m_brush.color();
239 }
239 }
240
240
241 /*!
241 /*!
242 Sets the \a pen of legend. Pen affects the legend borders.
242 Sets the \a pen of legend. Pen affects the legend borders.
243 */
243 */
244 void QLegend::setPen(const QPen &pen)
244 void QLegend::setPen(const QPen &pen)
245 {
245 {
246 if (d_ptr->m_pen != pen) {
246 if (d_ptr->m_pen != pen) {
247 d_ptr->m_pen = pen;
247 d_ptr->m_pen = pen;
248 update();
248 update();
249 }
249 }
250 }
250 }
251
251
252 /*!
252 /*!
253 Returns the pen used by legend
253 Returns the pen used by legend
254 */
254 */
255
255
256 QPen QLegend::pen() const
256 QPen QLegend::pen() const
257 {
257 {
258 return d_ptr->m_pen;
258 return d_ptr->m_pen;
259 }
259 }
260
260
261 void QLegend::setBorderColor(QColor color)
261 void QLegend::setBorderColor(QColor color)
262 {
262 {
263 QPen p = d_ptr->m_pen;
263 QPen p = d_ptr->m_pen;
264 if (p.color() != color) {
264 if (p.color() != color) {
265 p.setColor(color);
265 p.setColor(color);
266 setPen(p);
266 setPen(p);
267 emit borderColorChanged(color);
267 emit borderColorChanged(color);
268 }
268 }
269 }
269 }
270
270
271 QColor QLegend::borderColor()
271 QColor QLegend::borderColor()
272 {
272 {
273 return d_ptr->m_pen.color();
273 return d_ptr->m_pen.color();
274 }
274 }
275
275
276 void QLegend::setFont(const QFont &font)
276 void QLegend::setFont(const QFont &font)
277 {
277 {
278 if (d_ptr->m_font != font) {
278 if (d_ptr->m_font != font) {
279 d_ptr->setFont(font);
279 d_ptr->setFont(font);
280 emit fontChanged(font);
280 emit fontChanged(font);
281 }
281 }
282 }
282 }
283
283
284 QFont QLegend::font() const
284 QFont QLegend::font() const
285 {
285 {
286 return d_ptr->m_font;
286 return d_ptr->m_font;
287 }
287 }
288
288
289 void QLegend::setLabelPen(const QPen &pen)
290 {
291 if (d_ptr->m_labelPen != pen) {
292 d_ptr->setLabelPen(pen);
293 emit labelPenChanged(pen);
294 }
295 }
296
297 QPen QLegend::labelPen() const
298 {
299 return d_ptr->m_labelPen;
300 }
301
302 void QLegend::setLabelBrush(const QBrush &brush)
303 {
304 qDebug() << "setting legend brush 1";
305 if (d_ptr->m_labelBrush != brush) {
306 d_ptr->setLabelBrush(brush);
307 emit labelBrushChanged(brush);
308 qDebug() << "setting legend brush 2";
309 }
310 }
311
312 QBrush QLegend::labelBrush() const
313 {
314 return d_ptr->m_labelBrush;
315 }
316
317 void QLegend::setLabelColor(QColor color)
318 {
319 QBrush b = d_ptr->m_labelBrush;
320 if (b.style() != Qt::SolidPattern || b.color() != color) {
321 b.setStyle(Qt::SolidPattern);
322 b.setColor(color);
323 setLabelBrush(b);
324 emit labelColorChanged(color);
325 }
326 }
327
328 QColor QLegend::labelColor() const
329 {
330 return d_ptr->m_labelBrush.color();
331 }
332
289 void QLegend::setAlignment(Qt::Alignment alignment)
333 void QLegend::setAlignment(Qt::Alignment alignment)
290 {
334 {
291 if(d_ptr->m_alignment!=alignment) {
335 if(d_ptr->m_alignment!=alignment) {
292 d_ptr->m_alignment = alignment;
336 d_ptr->m_alignment = alignment;
293 d_ptr->updateLayout();
337 d_ptr->updateLayout();
294 }
338 }
295 }
339 }
296
340
297 Qt::Alignment QLegend::alignment() const
341 Qt::Alignment QLegend::alignment() const
298 {
342 {
299 return d_ptr->m_alignment;
343 return d_ptr->m_alignment;
300 }
344 }
301
345
302 /*!
346 /*!
303 Detaches the legend from chart. Chart won't change layout of the legend.
347 Detaches the legend from chart. Chart won't change layout of the legend.
304 */
348 */
305 void QLegend::detachFromChart()
349 void QLegend::detachFromChart()
306 {
350 {
307 d_ptr->m_attachedToChart = false;
351 d_ptr->m_attachedToChart = false;
308 }
352 }
309
353
310 /*!
354 /*!
311 Attaches the legend to chart. Chart may change layout of the legend.
355 Attaches the legend to chart. Chart may change layout of the legend.
312 */
356 */
313 void QLegend::attachToChart()
357 void QLegend::attachToChart()
314 {
358 {
315 d_ptr->attachToChart();
359 d_ptr->attachToChart();
316 }
360 }
317
361
318 /*!
362 /*!
319 Returns true, if legend is attached to chart.
363 Returns true, if legend is attached to chart.
320 */
364 */
321 bool QLegend::isAttachedToChart()
365 bool QLegend::isAttachedToChart()
322 {
366 {
323 return d_ptr->m_attachedToChart;
367 return d_ptr->m_attachedToChart;
324 }
368 }
325
369
326 /*!
370 /*!
327 Sets the visibility of legend background to \a visible
371 Sets the visibility of legend background to \a visible
328 */
372 */
329 void QLegend::setBackgroundVisible(bool visible)
373 void QLegend::setBackgroundVisible(bool visible)
330 {
374 {
331 if(d_ptr->m_backgroundVisible != visible) {
375 if(d_ptr->m_backgroundVisible != visible) {
332 d_ptr->m_backgroundVisible = visible;
376 d_ptr->m_backgroundVisible = visible;
333 update();
377 update();
334 emit backgroundVisibleChanged(visible);
378 emit backgroundVisibleChanged(visible);
335 }
379 }
336 }
380 }
337
381
338 /*!
382 /*!
339 Returns the visibility of legend background
383 Returns the visibility of legend background
340 */
384 */
341 bool QLegend::isBackgroundVisible() const
385 bool QLegend::isBackgroundVisible() const
342 {
386 {
343 return d_ptr->m_backgroundVisible;
387 return d_ptr->m_backgroundVisible;
344 }
388 }
345
389
346 /*!
390 /*!
347 \internal \a event see QGraphicsWidget for details
391 \internal \a event see QGraphicsWidget for details
348 */
392 */
349 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
393 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
350 {
394 {
351 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
395 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
352 QGraphicsWidget::resizeEvent(event);
396 QGraphicsWidget::resizeEvent(event);
353 if(d_ptr->m_rect != rect) {
397 if(d_ptr->m_rect != rect) {
354 d_ptr->m_rect = rect;
398 d_ptr->m_rect = rect;
355 d_ptr->updateLayout();
399 d_ptr->updateLayout();
356 }
400 }
357 }
401 }
358
402
359 /*!
403 /*!
360 \internal \a event see QGraphicsWidget for details
404 \internal \a event see QGraphicsWidget for details
361 */
405 */
362 void QLegend::hideEvent(QHideEvent *event)
406 void QLegend::hideEvent(QHideEvent *event)
363 {
407 {
364 QGraphicsWidget::hideEvent(event);
408 QGraphicsWidget::hideEvent(event);
365 setEnabled(false);
409 setEnabled(false);
366 d_ptr->updateLayout();
410 d_ptr->updateLayout();
367 }
411 }
368
412
369 /*!
413 /*!
370 \internal \a event see QGraphicsWidget for details
414 \internal \a event see QGraphicsWidget for details
371 */
415 */
372 void QLegend::showEvent(QShowEvent *event)
416 void QLegend::showEvent(QShowEvent *event)
373 {
417 {
374 QGraphicsWidget::showEvent(event);
418 QGraphicsWidget::showEvent(event);
375 setEnabled(true);
419 setEnabled(true);
376 d_ptr->updateLayout();
420 d_ptr->updateLayout();
377 }
421 }
378
422
379 qreal QLegend::minWidth() const
423 qreal QLegend::minWidth() const
380 {
424 {
381 return d_ptr->m_minWidth;
425 return d_ptr->m_minWidth;
382 }
426 }
383
427
384 qreal QLegend::minHeight() const
428 qreal QLegend::minHeight() const
385 {
429 {
386 return d_ptr->m_minHeight;
430 return d_ptr->m_minHeight;
387 }
431 }
388
432
389 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
433 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390
434
391 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
435 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
392 q_ptr(q),
436 q_ptr(q),
393 m_presenter(presenter),
437 m_presenter(presenter),
394 m_chart(chart),
438 m_chart(chart),
395 m_markers(new QGraphicsItemGroup(q)),
439 m_markers(new QGraphicsItemGroup(q)),
396 m_alignment(Qt::AlignTop),
440 m_alignment(Qt::AlignTop),
397 m_brush(QBrush()),
441 m_brush(QBrush()),
398 m_pen(QPen()),
442 m_pen(QPen()),
443 m_labelPen(QPen(Qt::NoPen)),
444 m_labelBrush(QBrush()),
399 m_offsetX(0),
445 m_offsetX(0),
400 m_offsetY(0),
446 m_offsetY(0),
401 m_minWidth(0),
447 m_minWidth(0),
402 m_minHeight(0),
448 m_minHeight(0),
403 m_width(0),
449 m_width(0),
404 m_height(0),
450 m_height(0),
405 m_diameter(5),
451 m_diameter(5),
406 m_attachedToChart(true),
452 m_attachedToChart(true),
407 m_backgroundVisible(false)
453 m_backgroundVisible(false)
408 {
454 {
409
455
410 }
456 }
411
457
412 QLegendPrivate::~QLegendPrivate()
458 QLegendPrivate::~QLegendPrivate()
413 {
459 {
414
460
415 }
461 }
416
462
417 void QLegendPrivate::setOffset(qreal x, qreal y)
463 void QLegendPrivate::setOffset(qreal x, qreal y)
418 {
464 {
419 bool scrollHorizontal = true;
465 bool scrollHorizontal = true;
420 switch(m_alignment) {
466 switch(m_alignment) {
421 case Qt::AlignTop:
467 case Qt::AlignTop:
422 case Qt::AlignBottom: {
468 case Qt::AlignBottom: {
423 scrollHorizontal = true;
469 scrollHorizontal = true;
424 break;
470 break;
425 }
471 }
426 case Qt::AlignLeft:
472 case Qt::AlignLeft:
427 case Qt::AlignRight: {
473 case Qt::AlignRight: {
428 scrollHorizontal = false;
474 scrollHorizontal = false;
429 break;
475 break;
430 }
476 }
431 }
477 }
432
478
433 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
479 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
434 if (!m_attachedToChart) {
480 if (!m_attachedToChart) {
435 scrollHorizontal = !scrollHorizontal;
481 scrollHorizontal = !scrollHorizontal;
436 }
482 }
437
483
438 // Limit offset between m_minOffset and m_maxOffset
484 // Limit offset between m_minOffset and m_maxOffset
439 if (scrollHorizontal) {
485 if (scrollHorizontal) {
440 if(m_width<=m_rect.width()) return;
486 if(m_width<=m_rect.width()) return;
441
487
442 if (x != m_offsetX) {
488 if (x != m_offsetX) {
443 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
489 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
444 m_markers->setPos(-m_offsetX,m_rect.top());
490 m_markers->setPos(-m_offsetX,m_rect.top());
445 }
491 }
446 } else {
492 } else {
447 if(m_height<=m_rect.height()) return;
493 if(m_height<=m_rect.height()) return;
448
494
449 if (y != m_offsetY) {
495 if (y != m_offsetY) {
450 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
496 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
451 m_markers->setPos(m_rect.left(),-m_offsetY);
497 m_markers->setPos(m_rect.left(),-m_offsetY);
452 }
498 }
453 }
499 }
454 }
500 }
455
501
456 QPointF QLegendPrivate::offset() const
502 QPointF QLegendPrivate::offset() const
457 {
503 {
458 return QPointF(m_offsetX,m_offsetY);
504 return QPointF(m_offsetX,m_offsetY);
459 }
505 }
460
506
461 void QLegendPrivate::updateLayout()
507 void QLegendPrivate::updateLayout()
462 {
508 {
463 if (!m_attachedToChart) {
509 if (!m_attachedToChart) {
464 updateDetachedLayout();
510 updateDetachedLayout();
465 return;
511 return;
466 }
512 }
467
513
468 m_offsetX=0;
514 m_offsetX=0;
469 QList<QGraphicsItem *> items = m_markers->childItems();
515 QList<QGraphicsItem *> items = m_markers->childItems();
470
516
471 if(items.isEmpty()) return;
517 if(items.isEmpty()) return;
472
518
473 m_minWidth=0;
519 m_minWidth=0;
474 m_minHeight=0;
520 m_minHeight=0;
475
521
476 switch(m_alignment) {
522 switch(m_alignment) {
477
523
478 case Qt::AlignTop:
524 case Qt::AlignTop:
479 case Qt::AlignBottom: {
525 case Qt::AlignBottom: {
480 QPointF point = m_rect.topLeft();
526 QPointF point = m_rect.topLeft();
481 m_width = 0;
527 m_width = 0;
482 foreach (QGraphicsItem *item, items) {
528 foreach (QGraphicsItem *item, items) {
483 if (item->isVisible()) {
529 if (item->isVisible()) {
484 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
530 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
485 const QRectF& rect = item->boundingRect();
531 const QRectF& rect = item->boundingRect();
486 qreal w = rect.width();
532 qreal w = rect.width();
487 m_minWidth=qMax(m_minWidth,w);
533 m_minWidth=qMax(m_minWidth,w);
488 m_minHeight=qMax(m_minHeight,rect.height());
534 m_minHeight=qMax(m_minHeight,rect.height());
489 m_width+=w;
535 m_width+=w;
490 point.setX(point.x() + w);
536 point.setX(point.x() + w);
491 }
537 }
492 }
538 }
493 if(m_width<m_rect.width()) {
539 if(m_width<m_rect.width()) {
494 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
540 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
495 }
541 }
496 else {
542 else {
497 m_markers->setPos(m_rect.topLeft());
543 m_markers->setPos(m_rect.topLeft());
498 }
544 }
499 m_height=m_minHeight;
545 m_height=m_minHeight;
500 }
546 }
501 break;
547 break;
502 case Qt::AlignLeft:
548 case Qt::AlignLeft:
503 case Qt::AlignRight: {
549 case Qt::AlignRight: {
504 QPointF point = m_rect.topLeft();
550 QPointF point = m_rect.topLeft();
505 m_height = 0;
551 m_height = 0;
506 foreach (QGraphicsItem *item, items) {
552 foreach (QGraphicsItem *item, items) {
507 if (item->isVisible()) {
553 if (item->isVisible()) {
508 item->setPos(point);
554 item->setPos(point);
509 const QRectF& rect = item->boundingRect();
555 const QRectF& rect = item->boundingRect();
510 qreal h = rect.height();
556 qreal h = rect.height();
511 m_minWidth=qMax(m_minWidth,rect.width());
557 m_minWidth=qMax(m_minWidth,rect.width());
512 m_minHeight=qMax(m_minHeight,h);
558 m_minHeight=qMax(m_minHeight,h);
513 m_height+=h;
559 m_height+=h;
514 point.setY(point.y() + h);
560 point.setY(point.y() + h);
515 }
561 }
516 }
562 }
517 if(m_height<m_rect.height()) {
563 if(m_height<m_rect.height()) {
518 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
564 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
519 }
565 }
520 else {
566 else {
521 m_markers->setPos(m_rect.topLeft());
567 m_markers->setPos(m_rect.topLeft());
522 }
568 }
523 m_width=m_minWidth;
569 m_width=m_minWidth;
524 }
570 }
525 break;
571 break;
526 }
572 }
527
573
528 m_minOffsetX = 0;
574 m_minOffsetX = 0;
529 m_minOffsetY = 0;
575 m_minOffsetY = 0;
530 m_maxOffsetX = m_width - m_rect.width();
576 m_maxOffsetX = m_width - m_rect.width();
531 m_maxOffsetY = m_height - m_rect.height();
577 m_maxOffsetY = m_height - m_rect.height();
532
578
533 m_presenter->updateLayout();
579 m_presenter->updateLayout();
534 }
580 }
535
581
536 void QLegendPrivate::updateDetachedLayout()
582 void QLegendPrivate::updateDetachedLayout()
537 {
583 {
538 // Detached layout is different.
584 // Detached layout is different.
539 // In detached mode legend may have multiple rows and columns, so layout calculations
585 // In detached mode legend may have multiple rows and columns, so layout calculations
540 // differ a log from attached mode.
586 // differ a log from attached mode.
541 // Also the scrolling logic is bit different.
587 // Also the scrolling logic is bit different.
542 m_offsetX=0;
588 m_offsetX=0;
543 m_offsetY=0;
589 m_offsetY=0;
544 QList<QGraphicsItem *> items = m_markers->childItems();
590 QList<QGraphicsItem *> items = m_markers->childItems();
545
591
546 if(items.isEmpty()) return;
592 if(items.isEmpty()) return;
547
593
548 m_minWidth = 0;
594 m_minWidth = 0;
549 m_minHeight = 0;
595 m_minHeight = 0;
550
596
551 switch (m_alignment) {
597 switch (m_alignment) {
552 case Qt::AlignTop: {
598 case Qt::AlignTop: {
553 QPointF point = m_rect.topLeft();
599 QPointF point = m_rect.topLeft();
554 m_width = 0;
600 m_width = 0;
555 m_height = 0;
601 m_height = 0;
556 for (int i=0; i<items.count(); i++) {
602 for (int i=0; i<items.count(); i++) {
557 QGraphicsItem *item = items.at(i);
603 QGraphicsItem *item = items.at(i);
558 if (item->isVisible()) {
604 if (item->isVisible()) {
559 const QRectF& rect = item->boundingRect();
605 const QRectF& rect = item->boundingRect();
560 qreal w = rect.width();
606 qreal w = rect.width();
561 qreal h = rect.height();
607 qreal h = rect.height();
562 m_minWidth = qMax(m_minWidth,w);
608 m_minWidth = qMax(m_minWidth,w);
563 m_minHeight = qMax(m_minHeight,rect.height());
609 m_minHeight = qMax(m_minHeight,rect.height());
564 m_height = qMax(m_height,h);
610 m_height = qMax(m_height,h);
565 item->setPos(point.x(),point.y());
611 item->setPos(point.x(),point.y());
566 point.setX(point.x() + w);
612 point.setX(point.x() + w);
567 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
613 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
568 // Next item would go off rect.
614 // Next item would go off rect.
569 point.setX(m_rect.topLeft().x());
615 point.setX(m_rect.topLeft().x());
570 point.setY(point.y() + h);
616 point.setY(point.y() + h);
571 if (i+1 < items.count()) {
617 if (i+1 < items.count()) {
572 m_height += h;
618 m_height += h;
573 }
619 }
574 }
620 }
575 }
621 }
576 }
622 }
577 m_markers->setPos(m_rect.topLeft());
623 m_markers->setPos(m_rect.topLeft());
578 m_width = m_minWidth;
624 m_width = m_minWidth;
579
625
580 m_minOffsetX = 0;
626 m_minOffsetX = 0;
581 m_minOffsetY = 0;
627 m_minOffsetY = 0;
582 m_maxOffsetX = m_width - m_rect.width();
628 m_maxOffsetX = m_width - m_rect.width();
583 m_maxOffsetY = m_height - m_rect.height();
629 m_maxOffsetY = m_height - m_rect.height();
584 }
630 }
585 break;
631 break;
586 case Qt::AlignBottom: {
632 case Qt::AlignBottom: {
587 QPointF point = m_rect.bottomLeft();
633 QPointF point = m_rect.bottomLeft();
588 m_width = 0;
634 m_width = 0;
589 m_height = 0;
635 m_height = 0;
590 for (int i=0; i<items.count(); i++) {
636 for (int i=0; i<items.count(); i++) {
591 QGraphicsItem *item = items.at(i);
637 QGraphicsItem *item = items.at(i);
592 if (item->isVisible()) {
638 if (item->isVisible()) {
593 const QRectF& rect = item->boundingRect();
639 const QRectF& rect = item->boundingRect();
594 qreal w = rect.width();
640 qreal w = rect.width();
595 qreal h = rect.height();
641 qreal h = rect.height();
596 m_minWidth = qMax(m_minWidth,w);
642 m_minWidth = qMax(m_minWidth,w);
597 m_minHeight = qMax(m_minHeight,rect.height());
643 m_minHeight = qMax(m_minHeight,rect.height());
598 m_height = qMax(m_height,h);
644 m_height = qMax(m_height,h);
599 item->setPos(point.x(),point.y() - h);
645 item->setPos(point.x(),point.y() - h);
600 point.setX(point.x() + w);
646 point.setX(point.x() + w);
601 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
647 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
602 // Next item would go off rect.
648 // Next item would go off rect.
603 point.setX(m_rect.bottomLeft().x());
649 point.setX(m_rect.bottomLeft().x());
604 point.setY(point.y() - h);
650 point.setY(point.y() - h);
605 if (i+1 < items.count()) {
651 if (i+1 < items.count()) {
606 m_height += h;
652 m_height += h;
607 }
653 }
608 }
654 }
609 }
655 }
610 }
656 }
611 m_markers->setPos(m_rect.topLeft());
657 m_markers->setPos(m_rect.topLeft());
612 m_width = m_minWidth;
658 m_width = m_minWidth;
613
659
614 m_minOffsetX = 0;
660 m_minOffsetX = 0;
615 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
661 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
616 m_maxOffsetX = m_width - m_rect.width();
662 m_maxOffsetX = m_width - m_rect.width();
617 m_maxOffsetY = 0;
663 m_maxOffsetY = 0;
618 }
664 }
619 break;
665 break;
620 case Qt::AlignLeft: {
666 case Qt::AlignLeft: {
621 QPointF point = m_rect.topLeft();
667 QPointF point = m_rect.topLeft();
622 m_width = 0;
668 m_width = 0;
623 m_height = 0;
669 m_height = 0;
624 qreal maxWidth = 0;
670 qreal maxWidth = 0;
625 for (int i=0; i<items.count(); i++) {
671 for (int i=0; i<items.count(); i++) {
626 QGraphicsItem *item = items.at(i);
672 QGraphicsItem *item = items.at(i);
627 if (item->isVisible()) {
673 if (item->isVisible()) {
628 const QRectF& rect = item->boundingRect();
674 const QRectF& rect = item->boundingRect();
629 qreal w = rect.width();
675 qreal w = rect.width();
630 qreal h = rect.height();
676 qreal h = rect.height();
631 m_minWidth = qMax(m_minWidth,rect.width());
677 m_minWidth = qMax(m_minWidth,rect.width());
632 m_minHeight = qMax(m_minHeight,h);
678 m_minHeight = qMax(m_minHeight,h);
633 maxWidth = qMax(maxWidth,w);
679 maxWidth = qMax(maxWidth,w);
634 item->setPos(point.x(),point.y());
680 item->setPos(point.x(),point.y());
635 point.setY(point.y() + h);
681 point.setY(point.y() + h);
636 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
682 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
637 // Next item would go off rect.
683 // Next item would go off rect.
638 point.setX(point.x() + maxWidth);
684 point.setX(point.x() + maxWidth);
639 point.setY(m_rect.topLeft().y());
685 point.setY(m_rect.topLeft().y());
640 if (i+1 < items.count()) {
686 if (i+1 < items.count()) {
641 m_width += maxWidth;
687 m_width += maxWidth;
642 maxWidth = 0;
688 maxWidth = 0;
643 }
689 }
644 }
690 }
645 }
691 }
646 }
692 }
647 m_width += maxWidth;
693 m_width += maxWidth;
648 m_markers->setPos(m_rect.topLeft());
694 m_markers->setPos(m_rect.topLeft());
649 m_height = m_minHeight;
695 m_height = m_minHeight;
650
696
651 m_minOffsetX = 0;
697 m_minOffsetX = 0;
652 m_minOffsetY = 0;
698 m_minOffsetY = 0;
653 m_maxOffsetX = m_width - m_rect.width();
699 m_maxOffsetX = m_width - m_rect.width();
654 m_maxOffsetY = m_height - m_rect.height();
700 m_maxOffsetY = m_height - m_rect.height();
655 }
701 }
656 break;
702 break;
657 case Qt::AlignRight: {
703 case Qt::AlignRight: {
658 QPointF point = m_rect.topRight();
704 QPointF point = m_rect.topRight();
659 m_width = 0;
705 m_width = 0;
660 m_height = 0;
706 m_height = 0;
661 qreal maxWidth = 0;
707 qreal maxWidth = 0;
662 for (int i=0; i<items.count(); i++) {
708 for (int i=0; i<items.count(); i++) {
663 QGraphicsItem *item = items.at(i);
709 QGraphicsItem *item = items.at(i);
664 if (item->isVisible()) {
710 if (item->isVisible()) {
665 const QRectF& rect = item->boundingRect();
711 const QRectF& rect = item->boundingRect();
666 qreal w = rect.width();
712 qreal w = rect.width();
667 qreal h = rect.height();
713 qreal h = rect.height();
668 m_minWidth = qMax(m_minWidth,rect.width());
714 m_minWidth = qMax(m_minWidth,rect.width());
669 m_minHeight = qMax(m_minHeight,h);
715 m_minHeight = qMax(m_minHeight,h);
670 maxWidth = qMax(maxWidth,w);
716 maxWidth = qMax(maxWidth,w);
671 item->setPos(point.x() - w,point.y());
717 item->setPos(point.x() - w,point.y());
672 point.setY(point.y() + h);
718 point.setY(point.y() + h);
673 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
719 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
674 // Next item would go off rect.
720 // Next item would go off rect.
675 point.setX(point.x() - maxWidth);
721 point.setX(point.x() - maxWidth);
676 point.setY(m_rect.topLeft().y());
722 point.setY(m_rect.topLeft().y());
677 if (i+1 < items.count()) {
723 if (i+1 < items.count()) {
678 m_width += maxWidth;
724 m_width += maxWidth;
679 maxWidth = 0;
725 maxWidth = 0;
680 }
726 }
681 }
727 }
682 }
728 }
683 }
729 }
684 m_width += maxWidth;
730 m_width += maxWidth;
685 m_markers->setPos(m_rect.topLeft());
731 m_markers->setPos(m_rect.topLeft());
686 m_height = m_minHeight;
732 m_height = m_minHeight;
687
733
688 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
734 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
689 m_minOffsetY = 0;
735 m_minOffsetY = 0;
690 m_maxOffsetX = 0;
736 m_maxOffsetX = 0;
691 m_maxOffsetY = m_height - m_rect.height();
737 m_maxOffsetY = m_height - m_rect.height();
692 }
738 }
693 break;
739 break;
694 default:
740 default:
695 break;
741 break;
696 }
742 }
697 }
743 }
698
744
699 void QLegendPrivate::attachToChart()
745 void QLegendPrivate::attachToChart()
700 {
746 {
701 m_attachedToChart = true;
747 m_attachedToChart = true;
702 q_ptr->setParent(m_chart);
748 q_ptr->setParent(m_chart);
703 }
749 }
704
750
705 int QLegendPrivate::roundness(qreal size)
751 int QLegendPrivate::roundness(qreal size)
706 {
752 {
707 return 100*m_diameter/int(size);
753 return 100*m_diameter/int(size);
708 }
754 }
709
755
710 void QLegendPrivate::setFont(const QFont &font)
756 void QLegendPrivate::setFont(const QFont &font)
711 {
757 {
712 m_font = font;
758 m_font = font;
713 QList<QGraphicsItem *> items = m_markers->childItems();
759 QList<QGraphicsItem *> items = m_markers->childItems();
714
760
715 foreach (QGraphicsItem *markers, items) {
761 foreach (QGraphicsItem *markers, items) {
716 LegendMarker *marker = static_cast<LegendMarker*>(markers);
762 LegendMarker *marker = static_cast<LegendMarker*>(markers);
717 marker->setFont(m_font);
763 marker->setFont(m_font);
718 }
764 }
719 updateLayout();
765 updateLayout();
720 }
766 }
721
767
768 void QLegendPrivate::setLabelPen(const QPen &pen)
769 {
770 m_labelPen = pen;
771 QList<QGraphicsItem *> items = m_markers->childItems();
772
773 foreach (QGraphicsItem *markers, items) {
774 LegendMarker *marker = static_cast<LegendMarker*>(markers);
775 marker->setPen(m_labelPen);
776 }
777 updateLayout();
778 }
779
780 void QLegendPrivate::setLabelBrush(const QBrush &brush)
781 {
782 m_labelBrush = brush;
783 QList<QGraphicsItem *> items = m_markers->childItems();
784
785 foreach (QGraphicsItem *markers, items) {
786 LegendMarker *marker = static_cast<LegendMarker*>(markers);
787 marker->setLabelBrush(m_labelBrush);
788 }
789 updateLayout();
790 }
791
722 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
792 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
723 {
793 {
724 Q_UNUSED(domain)
794 Q_UNUSED(domain)
725
795
726 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
796 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
727 foreach(LegendMarker* marker, markers) {
797 foreach(LegendMarker* marker, markers) {
728 marker->setFont(m_font);
798 marker->setFont(m_font);
799 marker->setLabelPen(m_labelPen);
800 marker->setLabelBrush(m_labelBrush);
729 m_markers->addToGroup(marker);
801 m_markers->addToGroup(marker);
730 }
802 }
731
803
732 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
804 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
733
805
734 if(series->type() == QAbstractSeries::SeriesTypePie) {
806 if(series->type() == QAbstractSeries::SeriesTypePie) {
735 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
807 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
736 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
808 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
737 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
809 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
738 }
810 }
739
811
740 updateLayout();
812 updateLayout();
741 }
813 }
742
814
743 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
815 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
744 {
816 {
745 QList<QGraphicsItem *> items = m_markers->childItems();
817 QList<QGraphicsItem *> items = m_markers->childItems();
746
818
747 foreach (QGraphicsItem *markers, items) {
819 foreach (QGraphicsItem *markers, items) {
748 LegendMarker *marker = static_cast<LegendMarker*>(markers);
820 LegendMarker *marker = static_cast<LegendMarker*>(markers);
749 if (marker->series() == series) {
821 if (marker->series() == series) {
750 delete marker;
822 delete marker;
751 }
823 }
752 }
824 }
753
825
754 if(series->type() == QAbstractSeries::SeriesTypePie)
826 if(series->type() == QAbstractSeries::SeriesTypePie)
755 {
827 {
756 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
828 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
757 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
829 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
758 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
830 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
759 }
831 }
760
832
761 updateLayout();
833 updateLayout();
762 }
834 }
763
835
764 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
836 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
765 {
837 {
766 // TODO: find out which markers are are added or removed. Update them
838 // TODO: find out which markers are are added or removed. Update them
767 // TODO: better implementation
839 // TODO: better implementation
768 handleSeriesRemoved(series);
840 handleSeriesRemoved(series);
769 Domain domain;
841 Domain domain;
770 handleSeriesAdded(series, &domain);
842 handleSeriesAdded(series, &domain);
771 }
843 }
772
844
773 void QLegendPrivate::handleUpdatePieSeries()
845 void QLegendPrivate::handleUpdatePieSeries()
774 {
846 {
775 //TODO: reimplement to be optimal
847 //TODO: reimplement to be optimal
776 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
848 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
777 Q_ASSERT(series);
849 Q_ASSERT(series);
778 handleSeriesRemoved(series);
850 handleSeriesRemoved(series);
779 handleSeriesAdded(series, 0);
851 handleSeriesAdded(series, 0);
780 }
852 }
781
853
782 void QLegendPrivate::handleSeriesVisibleChanged()
854 void QLegendPrivate::handleSeriesVisibleChanged()
783 {
855 {
784 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
856 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
785 QList<QGraphicsItem *> items = m_markers->childItems();
857 QList<QGraphicsItem *> items = m_markers->childItems();
786
858
787 foreach (QGraphicsItem *markers, items) {
859 foreach (QGraphicsItem *markers, items) {
788 LegendMarker *marker = static_cast<LegendMarker*>(markers);
860 LegendMarker *marker = static_cast<LegendMarker*>(markers);
789 if (marker->series() == series) {
861 if (marker->series() == series) {
790 marker->setVisible(!marker->isVisible());
862 marker->setVisible(!marker->isVisible());
791 }
863 }
792 }
864 }
793
865
794 updateLayout();
866 updateLayout();
795 }
867 }
796
868
797
869
798 #include "moc_qlegend.cpp"
870 #include "moc_qlegend.cpp"
799 #include "moc_qlegend_p.cpp"
871 #include "moc_qlegend_p.cpp"
800
872
801 QTCOMMERCIALCHART_END_NAMESPACE
873 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,121
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QLEGEND_H
21 #ifndef QLEGEND_H
22 #define QLEGEND_H
22 #define QLEGEND_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QGraphicsWidget>
25 #include <QGraphicsWidget>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Domain;
31 class Domain;
32 class LegendMarker;
32 class LegendMarker;
33 class QPieSlice;
33 class QPieSlice;
34 class QXYSeries;
34 class QXYSeries;
35 class QBarSet;
35 class QBarSet;
36 class QBarSeries;
36 class QBarSeries;
37 class QPieSeries;
37 class QPieSeries;
38 class QAreaSeries;
38 class QAreaSeries;
39 class LegendScrollButton;
39 class LegendScrollButton;
40 class QChart;
40 class QChart;
41 class QLegendPrivate;
41 class QLegendPrivate;
42
42
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
51 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
52 // TODO?
53 // Q_PROPERTY(QColor labelBorderColor READ labelBorderColor WRITE setLabelBorderColor NOTIFY labelBorderColorChanged)
51
54
52 private:
55 private:
53 explicit QLegend(QChart *chart);
56 explicit QLegend(QChart *chart);
54
57
55 public:
58 public:
56 ~QLegend();
59 ~QLegend();
57
60
58 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
61 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
59 QRectF boundingRect() const;
62 QRectF boundingRect() const;
60
63
61 void setBrush(const QBrush &brush);
64 void setBrush(const QBrush &brush);
62 QBrush brush() const;
65 QBrush brush() const;
63 void setColor(QColor color);
66 void setColor(QColor color);
64 QColor color();
67 QColor color();
65
68
66 void setPen(const QPen &pen);
69 void setPen(const QPen &pen);
67 QPen pen() const;
70 QPen pen() const;
68 void setBorderColor(QColor color);
71 void setBorderColor(QColor color);
69 QColor borderColor();
72 QColor borderColor();
70
73
71 void setFont(const QFont &font);
74 void setFont(const QFont &font);
72 QFont font() const;
75 QFont font() const;
76 void setLabelPen(const QPen &pen);
77 QPen labelPen() const;
78 void setLabelBrush(const QBrush &brush);
79 QBrush labelBrush() const;
80
81 void setLabelColor(QColor color);
82 QColor labelColor() const;
73
83
74 void setAlignment(Qt::Alignment alignment);
84 void setAlignment(Qt::Alignment alignment);
75 Qt::Alignment alignment() const;
85 Qt::Alignment alignment() const;
76
86
77 void detachFromChart();
87 void detachFromChart();
78 void attachToChart();
88 void attachToChart();
79 bool isAttachedToChart();
89 bool isAttachedToChart();
80
90
81 qreal minWidth() const;
91 qreal minWidth() const;
82 qreal minHeight() const;
92 qreal minHeight() const;
83
93
84 void setBackgroundVisible(bool visible = true);
94 void setBackgroundVisible(bool visible = true);
85 bool isBackgroundVisible() const;
95 bool isBackgroundVisible() const;
86
96
87 protected:
97 protected:
88 void resizeEvent(QGraphicsSceneResizeEvent *event);
98 void resizeEvent(QGraphicsSceneResizeEvent *event);
89 void hideEvent(QHideEvent *event);
99 void hideEvent(QHideEvent *event);
90 void showEvent(QShowEvent *event);
100 void showEvent(QShowEvent *event);
91
101
92 Q_SIGNALS:
102 Q_SIGNALS:
93 void backgroundVisibleChanged(bool visible);
103 void backgroundVisibleChanged(bool visible);
94 void colorChanged(QColor color);
104 void colorChanged(QColor color);
95 void borderColorChanged(QColor color);
105 void borderColorChanged(QColor color);
96 void fontChanged(QFont font);
106 void fontChanged(QFont font);
107 void labelPenChanged(QPen pen);
108 void labelBrushChanged(QBrush brush);
109 void labelColorChanged(QColor color);
110 // TODO?
111 // void labelBorderColorChanged(QColor color);
97
112
98 private:
113 private:
99 QScopedPointer<QLegendPrivate> d_ptr;
114 QScopedPointer<QLegendPrivate> d_ptr;
100 Q_DISABLE_COPY(QLegend)
115 Q_DISABLE_COPY(QLegend)
101 friend class LegendScroller;
116 friend class LegendScroller;
102 };
117 };
103
118
104 QTCOMMERCIALCHART_END_NAMESPACE
119 QTCOMMERCIALCHART_END_NAMESPACE
105
120
106 #endif // QLEGEND_H
121 #endif // QLEGEND_H
@@ -1,93 +1,97
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGEND_P_H
30 #ifndef QLEGEND_P_H
31 #define QLEGEND_P_H
31 #define QLEGEND_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QChart;
37 class QChart;
38 class ChartPresenter;
38 class ChartPresenter;
39 class QAbstractSeries;
39 class QAbstractSeries;
40
40
41 class QLegendPrivate : public QObject
41 class QLegendPrivate : public QObject
42 {
42 {
43 Q_OBJECT
43 Q_OBJECT
44 public:
44 public:
45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
46 ~QLegendPrivate();
46 ~QLegendPrivate();
47
47
48 void setOffset(qreal x, qreal y);
48 void setOffset(qreal x, qreal y);
49 QPointF offset() const;
49 QPointF offset() const;
50 void updateLayout();
50 void updateLayout();
51 void updateDetachedLayout();
51 void updateDetachedLayout();
52 void attachToChart();
52 void attachToChart();
53 int roundness(qreal size);
53 int roundness(qreal size);
54 void setFont(const QFont &font);
54 void setFont(const QFont &font);
55 void setLabelPen(const QPen &pen);
56 void setLabelBrush(const QBrush &brush);
55
57
56 public Q_SLOTS:
58 public Q_SLOTS:
57 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
59 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
58 void handleSeriesRemoved(QAbstractSeries *series);
60 void handleSeriesRemoved(QAbstractSeries *series);
59 void handleSeriesUpdated(QAbstractSeries *series);
61 void handleSeriesUpdated(QAbstractSeries *series);
60 void handleUpdatePieSeries(); //TODO remove this function
62 void handleUpdatePieSeries(); //TODO remove this function
61 void handleSeriesVisibleChanged();
63 void handleSeriesVisibleChanged();
62
64
63 private:
65 private:
64 QLegend *q_ptr;
66 QLegend *q_ptr;
65 ChartPresenter *m_presenter;
67 ChartPresenter *m_presenter;
66 QChart* m_chart;
68 QChart* m_chart;
67 QGraphicsItemGroup* m_markers;
69 QGraphicsItemGroup* m_markers;
68 Qt::Alignment m_alignment;
70 Qt::Alignment m_alignment;
69 QBrush m_brush;
71 QBrush m_brush;
70 QPen m_pen;
72 QPen m_pen;
71 QFont m_font;
73 QFont m_font;
74 QPen m_labelPen;
75 QBrush m_labelBrush;
72 QRectF m_rect;
76 QRectF m_rect;
73 qreal m_offsetX;
77 qreal m_offsetX;
74 qreal m_offsetY;
78 qreal m_offsetY;
75 qreal m_minOffsetX;
79 qreal m_minOffsetX;
76 qreal m_minOffsetY;
80 qreal m_minOffsetY;
77 qreal m_maxOffsetX;
81 qreal m_maxOffsetX;
78 qreal m_maxOffsetY;
82 qreal m_maxOffsetY;
79 qreal m_minWidth;
83 qreal m_minWidth;
80 qreal m_minHeight;
84 qreal m_minHeight;
81 qreal m_width;
85 qreal m_width;
82 qreal m_height;
86 qreal m_height;
83 qreal m_diameter;
87 qreal m_diameter;
84 bool m_attachedToChart;
88 bool m_attachedToChart;
85 bool m_backgroundVisible;
89 bool m_backgroundVisible;
86
90
87 friend class QLegend;
91 friend class QLegend;
88
92
89 };
93 };
90
94
91 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
92
96
93 #endif
97 #endif
General Comments 0
You need to be logged in to leave comments. Login now