##// END OF EJS Templates
fixes legend not vibile after showEvent
Michal Klocek -
r2082:786b61dc6f56
parent child
Show More
@@ -1,389 +1,388
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 "legendlayout_p.h"
21 #include "legendlayout_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "legendmarker_p.h"
23 #include "legendmarker_p.h"
24 #include "qlegend_p.h"
24 #include "qlegend_p.h"
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 LegendLayout::LegendLayout(QLegend* legend):
29 LegendLayout::LegendLayout(QLegend* legend):
30 m_legend(legend)
30 m_legend(legend)
31 {
31 {
32
32
33 }
33 }
34
34
35 LegendLayout::~LegendLayout()
35 LegendLayout::~LegendLayout()
36 {
36 {
37
37
38 }
38 }
39
39
40 void LegendLayout::setOffset(qreal x, qreal y)
40 void LegendLayout::setOffset(qreal x, qreal y)
41 {
41 {
42 bool scrollHorizontal = true;
42 bool scrollHorizontal = true;
43 switch(m_legend->alignment()) {
43 switch(m_legend->alignment()) {
44 case Qt::AlignTop:
44 case Qt::AlignTop:
45 case Qt::AlignBottom: {
45 case Qt::AlignBottom: {
46 scrollHorizontal = true;
46 scrollHorizontal = true;
47 break;
47 break;
48 }
48 }
49 case Qt::AlignLeft:
49 case Qt::AlignLeft:
50 case Qt::AlignRight: {
50 case Qt::AlignRight: {
51 scrollHorizontal = false;
51 scrollHorizontal = false;
52 break;
52 break;
53 }
53 }
54 }
54 }
55
55
56 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
56 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
57 if (!m_legend->isAttachedToChart()) {
57 if (!m_legend->isAttachedToChart()) {
58 scrollHorizontal = !scrollHorizontal;
58 scrollHorizontal = !scrollHorizontal;
59 }
59 }
60
60
61 QRectF boundingRect = geometry();
61 QRectF boundingRect = geometry();
62 qreal left, top, right, bottom;
62 qreal left, top, right, bottom;
63 getContentsMargins(&left, &top, &right, &bottom);
63 getContentsMargins(&left, &top, &right, &bottom);
64 boundingRect.adjust(left,top,-right,-bottom);
64 boundingRect.adjust(left,top,-right,-bottom);
65
65
66 // Limit offset between m_minOffset and m_maxOffset
66 // Limit offset between m_minOffset and m_maxOffset
67 if (scrollHorizontal) {
67 if (scrollHorizontal) {
68 if(m_width<=boundingRect.width()) return;
68 if(m_width<=boundingRect.width()) return;
69
69
70 if (x != m_offsetX) {
70 if (x != m_offsetX) {
71 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
71 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
72 m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top());
72 m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top());
73 }
73 }
74 }
74 }
75 else {
75 else {
76 if(m_height<=boundingRect.height()) return;
76 if(m_height<=boundingRect.height()) return;
77
77
78 if (y != m_offsetY) {
78 if (y != m_offsetY) {
79 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
79 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
80 m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY);
80 m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY);
81 }
81 }
82 }
82 }
83 }
83 }
84
84
85 QPointF LegendLayout::offset() const
85 QPointF LegendLayout::offset() const
86 {
86 {
87 return QPointF(m_offsetX,m_offsetY);
87 return QPointF(m_offsetX,m_offsetY);
88 }
88 }
89
89
90 void LegendLayout::setGeometry(const QRectF& rect)
90 void LegendLayout::setGeometry(const QRectF& rect)
91 {
91 {
92 m_legend->d_ptr->items()->setVisible(m_legend->isVisible());
92 m_legend->d_ptr->items()->setVisible(m_legend->isVisible());
93
93
94 if(m_legend->isAttachedToChart()) {
94 QGraphicsLayout::setGeometry(rect);
95
95
96 if(m_legend->isAttachedToChart()) {
96 setAttachedGeometry(rect);
97 setAttachedGeometry(rect);
97 }
98 } else {
98 else {
99 setDettachedGeometry(rect);
99 setDettachedGeometry(rect);
100 }
100 }
101
101
102 QGraphicsLayout::setGeometry(rect);
103 }
102 }
104
103
105 void LegendLayout::setAttachedGeometry(const QRectF& rect)
104 void LegendLayout::setAttachedGeometry(const QRectF& rect)
106 {
105 {
107 if (!rect.isValid()) return;
106 if (!rect.isValid()) return;
108
107
109 m_offsetX=0;
108 m_offsetX=0;
110 m_offsetY=0;
109 m_offsetY=0;
111
110
112 QSizeF size(0,0);
111 QSizeF size(0,0);
113
112
114 if( m_legend->d_ptr->markers().isEmpty()) return;
113 if( m_legend->d_ptr->markers().isEmpty()) return;
115
114
116 m_width=0;
115 m_width=0;
117 m_height=0;
116 m_height=0;
118
117
119 qreal left, top, right, bottom;
118 qreal left, top, right, bottom;
120 getContentsMargins(&left, &top, &right, &bottom);
119 getContentsMargins(&left, &top, &right, &bottom);
121
120
122 QRectF geometry = rect.adjusted(left,top,-right,-bottom);
121 QRectF geometry = rect.adjusted(left,top,-right,-bottom);
123
122
124 switch(m_legend->alignment()) {
123 switch(m_legend->alignment()) {
125
124
126 case Qt::AlignTop:
125 case Qt::AlignTop:
127 case Qt::AlignBottom: {
126 case Qt::AlignBottom: {
128 QPointF point(0,0);
127 QPointF point(0,0);
129 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
128 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
130 if (marker->isVisible()) {
129 if (marker->isVisible()) {
131 marker->setGeometry(geometry);
130 marker->setGeometry(geometry);
132 marker->setPos(point.x(),geometry.height()/2 - marker->boundingRect().height()/2);
131 marker->setPos(point.x(),geometry.height()/2 - marker->boundingRect().height()/2);
133 const QRectF& rect = marker->boundingRect();
132 const QRectF& rect = marker->boundingRect();
134 size = size.expandedTo(rect.size());
133 size = size.expandedTo(rect.size());
135 qreal w = rect.width();
134 qreal w = rect.width();
136 m_width+=w;
135 m_width+=w;
137 point.setX(point.x() + w);
136 point.setX(point.x() + w);
138 }
137 }
139 }
138 }
140 if(m_width<geometry.width()) {
139 if(m_width<geometry.width()) {
141 m_legend->d_ptr->items()->setPos(geometry.width()/2-m_width/2,geometry.top());
140 m_legend->d_ptr->items()->setPos(geometry.width()/2-m_width/2,geometry.top());
142
141
143 }
142 }
144 else {
143 else {
145 m_legend->d_ptr->items()->setPos(geometry.topLeft());
144 m_legend->d_ptr->items()->setPos(geometry.topLeft());
146 }
145 }
147 m_height=size.height();
146 m_height=size.height();
148 }
147 }
149 break;
148 break;
150 case Qt::AlignLeft:
149 case Qt::AlignLeft:
151 case Qt::AlignRight: {
150 case Qt::AlignRight: {
152 QPointF point(0,0);
151 QPointF point(0,0);
153 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
152 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
154 if (marker->isVisible()) {
153 if (marker->isVisible()) {
155 marker->setGeometry(geometry);
154 marker->setGeometry(geometry);
156 marker->setPos(point);
155 marker->setPos(point);
157 const QRectF& rect = marker->boundingRect();
156 const QRectF& rect = marker->boundingRect();
158 qreal h = rect.height();
157 qreal h = rect.height();
159 size = size.expandedTo(rect.size());
158 size = size.expandedTo(rect.size());
160 m_height+=h;
159 m_height+=h;
161 point.setY(point.y() + h);
160 point.setY(point.y() + h);
162 }
161 }
163 }
162 }
164 if(m_height<geometry.height()) {
163 if(m_height<geometry.height()) {
165 m_legend->d_ptr->items()->setPos(geometry.left(),geometry.height()/2-m_height/2);
164 m_legend->d_ptr->items()->setPos(geometry.left(),geometry.height()/2-m_height/2);
166 }
165 }
167 else {
166 else {
168 m_legend->d_ptr->items()->setPos(geometry.topLeft());
167 m_legend->d_ptr->items()->setPos(geometry.topLeft());
169 }
168 }
170 m_width=size.width();
169 m_width=size.width();
171 }
170 }
172 break;
171 break;
173 }
172 }
174
173
175
174
176 m_minOffsetX = -left;
175 m_minOffsetX = -left;
177 m_minOffsetY = - top;
176 m_minOffsetY = - top;
178 m_maxOffsetX = m_width - geometry.width() - right;
177 m_maxOffsetX = m_width - geometry.width() - right;
179 m_maxOffsetY = m_height - geometry.height() - bottom;
178 m_maxOffsetY = m_height - geometry.height() - bottom;
180 }
179 }
181
180
182 void LegendLayout::setDettachedGeometry(const QRectF& rect)
181 void LegendLayout::setDettachedGeometry(const QRectF& rect)
183 {
182 {
184 if (!rect.isValid()) return;
183 if (!rect.isValid()) return;
185
184
186 // Detached layout is different.
185 // Detached layout is different.
187 // In detached mode legend may have multiple rows and columns, so layout calculations
186 // In detached mode legend may have multiple rows and columns, so layout calculations
188 // differ a log from attached mode.
187 // differ a log from attached mode.
189 // Also the scrolling logic is bit different.
188 // Also the scrolling logic is bit different.
190
189
191 m_offsetX=0;
190 m_offsetX=0;
192 m_offsetY=0;
191 m_offsetY=0;
193
192
194 qreal left, top, right, bottom;
193 qreal left, top, right, bottom;
195 getContentsMargins(&left, &top, &right, &bottom);
194 getContentsMargins(&left, &top, &right, &bottom);
196 QRectF geometry = rect.adjusted(left,top,-right,-bottom);
195 QRectF geometry = rect.adjusted(left,top,-right,-bottom);
197
196
198 QSizeF size(0,0);
197 QSizeF size(0,0);
199
198
200 QList<LegendMarker *> markers = m_legend->d_ptr->markers();
199 QList<LegendMarker *> markers = m_legend->d_ptr->markers();
201
200
202 if(markers.isEmpty()) return;
201 if(markers.isEmpty()) return;
203
202
204 switch (m_legend->alignment()) {
203 switch (m_legend->alignment()) {
205 case Qt::AlignTop: {
204 case Qt::AlignTop: {
206 QPointF point(0,0);
205 QPointF point(0,0);
207 m_width = 0;
206 m_width = 0;
208 m_height = 0;
207 m_height = 0;
209 for (int i=0; i<markers.count(); i++) {
208 for (int i=0; i<markers.count(); i++) {
210 LegendMarker *marker = markers.at(i);
209 LegendMarker *marker = markers.at(i);
211 if (marker->isVisible()) {
210 if (marker->isVisible()) {
212 marker->setGeometry(geometry);
211 marker->setGeometry(geometry);
213 marker->setPos(point.x(),point.y());
212 marker->setPos(point.x(),point.y());
214 const QRectF& boundingRect = marker->boundingRect();
213 const QRectF& boundingRect = marker->boundingRect();
215 qreal w = boundingRect.width();
214 qreal w = boundingRect.width();
216 qreal h = boundingRect.height();
215 qreal h = boundingRect.height();
217 m_width = qMax(m_width,w);
216 m_width = qMax(m_width,w);
218 m_height = qMax(m_height,h);
217 m_height = qMax(m_height,h);
219 point.setX(point.x() + w);
218 point.setX(point.x() + w);
220 if (point.x() + w > geometry.left() + geometry.width() - right) {
219 if (point.x() + w > geometry.left() + geometry.width() - right) {
221 // Next item would go off rect.
220 // Next item would go off rect.
222 point.setX(0);
221 point.setX(0);
223 point.setY(point.y() + h);
222 point.setY(point.y() + h);
224 if (i+1 < markers.count()) {
223 if (i+1 < markers.count()) {
225 m_height += h;
224 m_height += h;
226 }
225 }
227 }
226 }
228 }
227 }
229 }
228 }
230 m_legend->d_ptr->items()->setPos(geometry.topLeft());
229 m_legend->d_ptr->items()->setPos(geometry.topLeft());
231
230
232 m_minOffsetX = -left;
231 m_minOffsetX = -left;
233 m_minOffsetY = -top;
232 m_minOffsetY = -top;
234 m_maxOffsetX = m_width - geometry.width() - right;
233 m_maxOffsetX = m_width - geometry.width() - right;
235 m_maxOffsetY = m_height - geometry.height() - bottom;
234 m_maxOffsetY = m_height - geometry.height() - bottom;
236 }
235 }
237 break;
236 break;
238 case Qt::AlignBottom: {
237 case Qt::AlignBottom: {
239 QPointF point(0,geometry.height());
238 QPointF point(0,geometry.height());
240 m_width = 0;
239 m_width = 0;
241 m_height = 0;
240 m_height = 0;
242 for (int i=0; i<markers.count(); i++) {
241 for (int i=0; i<markers.count(); i++) {
243 LegendMarker *marker = markers.at(i);
242 LegendMarker *marker = markers.at(i);
244 if (marker->isVisible()) {
243 if (marker->isVisible()) {
245 marker->setGeometry(geometry);
244 marker->setGeometry(geometry);
246 const QRectF& boundingRect = marker->boundingRect();
245 const QRectF& boundingRect = marker->boundingRect();
247 qreal w = boundingRect.width();
246 qreal w = boundingRect.width();
248 qreal h = boundingRect.height();
247 qreal h = boundingRect.height();
249 m_width = qMax(m_width,w);
248 m_width = qMax(m_width,w);
250 m_height = qMax(m_height,h);
249 m_height = qMax(m_height,h);
251 marker->setPos(point.x(),point.y() - h);
250 marker->setPos(point.x(),point.y() - h);
252 point.setX(point.x() + w);
251 point.setX(point.x() + w);
253 if (point.x() + w > geometry.left() + geometry.width() - right) {
252 if (point.x() + w > geometry.left() + geometry.width() - right) {
254 // Next item would go off rect.
253 // Next item would go off rect.
255 point.setX(0);
254 point.setX(0);
256 point.setY(point.y() - h);
255 point.setY(point.y() - h);
257 if (i+1 < markers.count()) {
256 if (i+1 < markers.count()) {
258 m_height += h;
257 m_height += h;
259 }
258 }
260 }
259 }
261 }
260 }
262 }
261 }
263 m_legend->d_ptr->items()->setPos(geometry.topLeft());
262 m_legend->d_ptr->items()->setPos(geometry.topLeft());
264
263
265 m_minOffsetX = -left;
264 m_minOffsetX = -left;
266 m_minOffsetY = -m_height + geometry.height() - top;
265 m_minOffsetY = -m_height + geometry.height() - top;
267 m_maxOffsetX = m_width - geometry.width() - right;
266 m_maxOffsetX = m_width - geometry.width() - right;
268 m_maxOffsetY = -bottom;
267 m_maxOffsetY = -bottom;
269 }
268 }
270 break;
269 break;
271 case Qt::AlignLeft: {
270 case Qt::AlignLeft: {
272 QPointF point(0,0);
271 QPointF point(0,0);
273 m_width = 0;
272 m_width = 0;
274 m_height = 0;
273 m_height = 0;
275 qreal maxWidth = 0;
274 qreal maxWidth = 0;
276 for (int i=0; i<markers.count(); i++) {
275 for (int i=0; i<markers.count(); i++) {
277 LegendMarker *marker = markers.at(i);
276 LegendMarker *marker = markers.at(i);
278 if (marker->isVisible()) {
277 if (marker->isVisible()) {
279 marker->setGeometry(geometry);
278 marker->setGeometry(geometry);
280 const QRectF& boundingRect = marker->boundingRect();
279 const QRectF& boundingRect = marker->boundingRect();
281 qreal w = boundingRect.width();
280 qreal w = boundingRect.width();
282 qreal h = boundingRect.height();
281 qreal h = boundingRect.height();
283 m_height = qMax(m_height,h);
282 m_height = qMax(m_height,h);
284 maxWidth = qMax(maxWidth,w);
283 maxWidth = qMax(maxWidth,w);
285 marker->setPos(point.x(),point.y());
284 marker->setPos(point.x(),point.y());
286 point.setY(point.y() + h);
285 point.setY(point.y() + h);
287 if (point.y() + h > geometry.bottom() - bottom) {
286 if (point.y() + h > geometry.bottom() - bottom) {
288 // Next item would go off rect.
287 // Next item would go off rect.
289 point.setX(point.x() + maxWidth);
288 point.setX(point.x() + maxWidth);
290 point.setY(0);
289 point.setY(0);
291 if (i+1 < markers.count()) {
290 if (i+1 < markers.count()) {
292 m_width += maxWidth;
291 m_width += maxWidth;
293 maxWidth = 0;
292 maxWidth = 0;
294 }
293 }
295 }
294 }
296 }
295 }
297 }
296 }
298 m_width += maxWidth;
297 m_width += maxWidth;
299 m_legend->d_ptr->items()->setPos(geometry.topLeft());
298 m_legend->d_ptr->items()->setPos(geometry.topLeft());
300
299
301 m_minOffsetX = -left;
300 m_minOffsetX = -left;
302 m_minOffsetY = -top;
301 m_minOffsetY = -top;
303 m_maxOffsetX = m_width - geometry.width() - right;
302 m_maxOffsetX = m_width - geometry.width() - right;
304 m_maxOffsetY = m_height - geometry.height() - bottom;
303 m_maxOffsetY = m_height - geometry.height() - bottom;
305 }
304 }
306 break;
305 break;
307 case Qt::AlignRight: {
306 case Qt::AlignRight: {
308 QPointF point(geometry.width(),0);
307 QPointF point(geometry.width(),0);
309 m_width = 0;
308 m_width = 0;
310 m_height = 0;
309 m_height = 0;
311 qreal maxWidth = 0;
310 qreal maxWidth = 0;
312 for (int i=0; i<markers.count(); i++) {
311 for (int i=0; i<markers.count(); i++) {
313 LegendMarker *marker = markers.at(i);
312 LegendMarker *marker = markers.at(i);
314 if (marker->isVisible()) {
313 if (marker->isVisible()) {
315 marker->setGeometry(geometry);
314 marker->setGeometry(geometry);
316 const QRectF& boundingRect = marker->boundingRect();
315 const QRectF& boundingRect = marker->boundingRect();
317 qreal w = boundingRect.width();
316 qreal w = boundingRect.width();
318 qreal h = boundingRect.height();
317 qreal h = boundingRect.height();
319 m_height = qMax(m_height,h);
318 m_height = qMax(m_height,h);
320 maxWidth = qMax(maxWidth,w);
319 maxWidth = qMax(maxWidth,w);
321 marker->setPos(point.x() - w,point.y());
320 marker->setPos(point.x() - w,point.y());
322 point.setY(point.y() + h);
321 point.setY(point.y() + h);
323 if (point.y() + h > geometry.bottom()-bottom) {
322 if (point.y() + h > geometry.bottom()-bottom) {
324 // Next item would go off rect.
323 // Next item would go off rect.
325 point.setX(point.x() - maxWidth);
324 point.setX(point.x() - maxWidth);
326 point.setY(0);
325 point.setY(0);
327 if (i+1 < markers.count()) {
326 if (i+1 < markers.count()) {
328 m_width += maxWidth;
327 m_width += maxWidth;
329 maxWidth = 0;
328 maxWidth = 0;
330 }
329 }
331 }
330 }
332 }
331 }
333 }
332 }
334 m_width += maxWidth;
333 m_width += maxWidth;
335 m_legend->d_ptr->items()->setPos(geometry.topLeft());
334 m_legend->d_ptr->items()->setPos(geometry.topLeft());
336
335
337 m_minOffsetX = - m_width + geometry.width() - left;
336 m_minOffsetX = - m_width + geometry.width() - left;
338 m_minOffsetY = -top;
337 m_minOffsetY = -top;
339 m_maxOffsetX = - right;
338 m_maxOffsetX = - right;
340 m_maxOffsetY = m_height - geometry.height() - bottom;
339 m_maxOffsetY = m_height - geometry.height() - bottom;
341 }
340 }
342 break;
341 break;
343 default:
342 default:
344 break;
343 break;
345 }
344 }
346
345
347 }
346 }
348
347
349 QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
348 QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
350 {
349 {
351 QSizeF size(0, 0);
350 QSizeF size(0, 0);
352 qreal left, top, right, bottom;
351 qreal left, top, right, bottom;
353 getContentsMargins(&left, &top, &right, &bottom);
352 getContentsMargins(&left, &top, &right, &bottom);
354
353
355 if(constraint.isValid()) {
354 if(constraint.isValid()) {
356 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
355 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
357 size = size.expandedTo(marker->effectiveSizeHint(which));
356 size = size.expandedTo(marker->effectiveSizeHint(which));
358 }
357 }
359 size = size.boundedTo(constraint);
358 size = size.boundedTo(constraint);
360 }
359 }
361 else if (constraint.width() >= 0) {
360 else if (constraint.width() >= 0) {
362 qreal width = 0;
361 qreal width = 0;
363 qreal height = 0;
362 qreal height = 0;
364 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
363 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
365 width+=marker->effectiveSizeHint(which).width();
364 width+=marker->effectiveSizeHint(which).width();
366 height=qMax(height,marker->effectiveSizeHint(which).height());
365 height=qMax(height,marker->effectiveSizeHint(which).height());
367 }
366 }
368
367
369 size = QSizeF(qMin(constraint.width(),width), height);
368 size = QSizeF(qMin(constraint.width(),width), height);
370 }
369 }
371 else if (constraint.height() >= 0) {
370 else if (constraint.height() >= 0) {
372 qreal width = 0;
371 qreal width = 0;
373 qreal height = 0;
372 qreal height = 0;
374 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
373 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
375 width=qMax(width,marker->effectiveSizeHint(which).width());
374 width=qMax(width,marker->effectiveSizeHint(which).width());
376 height+=height,marker->effectiveSizeHint(which).height();
375 height+=height,marker->effectiveSizeHint(which).height();
377 }
376 }
378 size = QSizeF(width,qMin(constraint.height(),height));
377 size = QSizeF(width,qMin(constraint.height(),height));
379 }
378 }
380 else {
379 else {
381 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
380 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
382 size = size.expandedTo(marker->effectiveSizeHint(which));
381 size = size.expandedTo(marker->effectiveSizeHint(which));
383 }
382 }
384 }
383 }
385 size += QSize(left + right, top + bottom);
384 size += QSize(left + right, top + bottom);
386 return size;
385 return size;
387 }
386 }
388
387
389 QTCOMMERCIALCHART_END_NAMESPACE
388 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,522 +1,524
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 #include "legendlayout_p.h"
26 #include "legendlayout_p.h"
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 "qabstractbarseries.h"
33 #include "qabstractbarseries.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 #include <QGraphicsLayout>
44 #include <QGraphicsLayout>
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 \property QLegend::labelColor
133 \property QLegend::labelColor
134 The color of brush used to draw labels.
134 The color of brush used to draw labels.
135 */
135 */
136 /*!
136 /*!
137 \qmlproperty color QLegend::labelColor
137 \qmlproperty color QLegend::labelColor
138 The color of brush used to draw labels.
138 The color of brush used to draw labels.
139 */
139 */
140
140
141 /*!
141 /*!
142 \fn void QLegend::backgroundVisibleChanged(bool)
142 \fn void QLegend::backgroundVisibleChanged(bool)
143 The visibility of the legend background changed to \a visible.
143 The visibility of the legend background changed to \a visible.
144 */
144 */
145
145
146 /*!
146 /*!
147 \fn void QLegend::colorChanged(QColor)
147 \fn void QLegend::colorChanged(QColor)
148 The color of the legend background changed to \a color.
148 The color of the legend background changed to \a color.
149 */
149 */
150
150
151 /*!
151 /*!
152 \fn void QLegend::borderColorChanged(QColor)
152 \fn void QLegend::borderColorChanged(QColor)
153 The border color of the legend background changed to \a color.
153 The border color of the legend background changed to \a color.
154 */
154 */
155
155
156 /*!
156 /*!
157 \fn void QLegend::fontChanged(QFont)
157 \fn void QLegend::fontChanged(QFont)
158 The font of markers of the legend changed to \a font.
158 The font of markers of the legend changed to \a font.
159 */
159 */
160
160
161 /*!
161 /*!
162 \fn void QLegend::labelColorChanged(QColor color)
162 \fn void QLegend::labelColorChanged(QColor color)
163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
164 */
164 */
165
165
166 /*!
166 /*!
167 Constructs the legend object and sets the parent to \a parent
167 Constructs the legend object and sets the parent to \a parent
168 */
168 */
169
169
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
170 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
172 {
172 {
173 setZValue(ChartPresenter::LegendZValue);
173 setZValue(ChartPresenter::LegendZValue);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
175 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
176 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
177 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
177 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
178 setLayout(d_ptr->m_layout);
178 setLayout(d_ptr->m_layout);
179 }
179 }
180
180
181 /*!
181 /*!
182 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
182 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
183 */
183 */
184 QLegend::~QLegend()
184 QLegend::~QLegend()
185 {
185 {
186 }
186 }
187
187
188 /*!
188 /*!
189 \internal
189 \internal
190 */
190 */
191 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
191 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
192 {
192 {
193 Q_UNUSED(option)
193 Q_UNUSED(option)
194 Q_UNUSED(widget)
194 Q_UNUSED(widget)
195
195
196 if(!d_ptr->m_backgroundVisible) return;
196 if(!d_ptr->m_backgroundVisible) return;
197
197
198 painter->setOpacity(opacity());
198 painter->setOpacity(opacity());
199 painter->setPen(d_ptr->m_pen);
199 painter->setPen(d_ptr->m_pen);
200 painter->setBrush(d_ptr->m_brush);
200 painter->setBrush(d_ptr->m_brush);
201 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
201 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
202
202
203 }
203 }
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 emit colorChanged(brush.color());
214 emit colorChanged(brush.color());
215 }
215 }
216 }
216 }
217
217
218 /*!
218 /*!
219 Returns the brush used by legend.
219 Returns the brush used by legend.
220 */
220 */
221 QBrush QLegend::brush() const
221 QBrush QLegend::brush() const
222 {
222 {
223 return d_ptr->m_brush;
223 return d_ptr->m_brush;
224 }
224 }
225
225
226 void QLegend::setColor(QColor color)
226 void QLegend::setColor(QColor color)
227 {
227 {
228 QBrush b = d_ptr->m_brush;
228 QBrush b = d_ptr->m_brush;
229 if (b.style() != Qt::SolidPattern || b.color() != color) {
229 if (b.style() != Qt::SolidPattern || b.color() != color) {
230 b.setStyle(Qt::SolidPattern);
230 b.setStyle(Qt::SolidPattern);
231 b.setColor(color);
231 b.setColor(color);
232 setBrush(b);
232 setBrush(b);
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 emit borderColorChanged(pen.color());
249 emit borderColorChanged(pen.color());
250 }
250 }
251 }
251 }
252
252
253 /*!
253 /*!
254 Returns the pen used by legend
254 Returns the pen used by legend
255 */
255 */
256
256
257 QPen QLegend::pen() const
257 QPen QLegend::pen() const
258 {
258 {
259 return d_ptr->m_pen;
259 return d_ptr->m_pen;
260 }
260 }
261
261
262 void QLegend::setFont(const QFont &font)
262 void QLegend::setFont(const QFont &font)
263 {
263 {
264 if (d_ptr->m_font != font) {
264 if (d_ptr->m_font != font) {
265 d_ptr->m_font = font;
265 d_ptr->m_font = font;
266
266
267 foreach (LegendMarker *marker, d_ptr->markers()) {
267 foreach (LegendMarker *marker, d_ptr->markers()) {
268 marker->setFont(d_ptr->m_font);
268 marker->setFont(d_ptr->m_font);
269 }
269 }
270 layout()->invalidate();
270 layout()->invalidate();
271 emit fontChanged(font);
271 emit fontChanged(font);
272 }
272 }
273 }
273 }
274
274
275 QFont QLegend::font() const
275 QFont QLegend::font() const
276 {
276 {
277 return d_ptr->m_font;
277 return d_ptr->m_font;
278 }
278 }
279
279
280 void QLegend::setBorderColor(QColor color)
280 void QLegend::setBorderColor(QColor color)
281 {
281 {
282 QPen p = d_ptr->m_pen;
282 QPen p = d_ptr->m_pen;
283 if (p.color() != color) {
283 if (p.color() != color) {
284 p.setColor(color);
284 p.setColor(color);
285 setPen(p);
285 setPen(p);
286 }
286 }
287 }
287 }
288
288
289 QColor QLegend::borderColor()
289 QColor QLegend::borderColor()
290 {
290 {
291 return d_ptr->m_pen.color();
291 return d_ptr->m_pen.color();
292 }
292 }
293
293
294 /*!
294 /*!
295 Set brush used to draw labels to \a brush.
295 Set brush used to draw labels to \a brush.
296 */
296 */
297 void QLegend::setLabelBrush(const QBrush &brush)
297 void QLegend::setLabelBrush(const QBrush &brush)
298 {
298 {
299 if (d_ptr->m_labelBrush != brush) {
299 if (d_ptr->m_labelBrush != brush) {
300 d_ptr->m_labelBrush = brush;
300 d_ptr->m_labelBrush = brush;
301 foreach (LegendMarker *marker, d_ptr->markers()) {
301 foreach (LegendMarker *marker, d_ptr->markers()) {
302 marker->setLabelBrush(d_ptr->m_labelBrush);
302 marker->setLabelBrush(d_ptr->m_labelBrush);
303 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
303 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
304 // instead of mapping it from label brush color
304 // instead of mapping it from label brush color
305 marker->setPen(brush.color());
305 marker->setPen(brush.color());
306 }
306 }
307 emit labelColorChanged(brush.color());
307 emit labelColorChanged(brush.color());
308 }
308 }
309 }
309 }
310
310
311 /*!
311 /*!
312 Brush used to draw labels.
312 Brush used to draw labels.
313 */
313 */
314 QBrush QLegend::labelBrush() const
314 QBrush QLegend::labelBrush() const
315 {
315 {
316 return d_ptr->m_labelBrush;
316 return d_ptr->m_labelBrush;
317 }
317 }
318
318
319 void QLegend::setLabelColor(QColor color)
319 void QLegend::setLabelColor(QColor color)
320 {
320 {
321 QBrush b = d_ptr->m_labelBrush;
321 QBrush b = d_ptr->m_labelBrush;
322 if (b.style() != Qt::SolidPattern || b.color() != color) {
322 if (b.style() != Qt::SolidPattern || b.color() != color) {
323 b.setStyle(Qt::SolidPattern);
323 b.setStyle(Qt::SolidPattern);
324 b.setColor(color);
324 b.setColor(color);
325 setLabelBrush(b);
325 setLabelBrush(b);
326 }
326 }
327 }
327 }
328
328
329 QColor QLegend::labelColor() const
329 QColor QLegend::labelColor() const
330 {
330 {
331 return d_ptr->m_labelBrush.color();
331 return d_ptr->m_labelBrush.color();
332 }
332 }
333
333
334
334
335 void QLegend::setAlignment(Qt::Alignment alignment)
335 void QLegend::setAlignment(Qt::Alignment alignment)
336 {
336 {
337 if(d_ptr->m_alignment!=alignment) {
337 if(d_ptr->m_alignment!=alignment) {
338 d_ptr->m_alignment = alignment;
338 d_ptr->m_alignment = alignment;
339 updateGeometry();
340 if(isAttachedToChart()){
339 if(isAttachedToChart()){
341 d_ptr->m_presenter->layout()->invalidate();
340 d_ptr->m_presenter->layout()->invalidate();
342 }else{
341 }else{
343 layout()->invalidate();
342 layout()->invalidate();
344 }
343 }
345 }
344 }
346 }
345 }
347
346
348 Qt::Alignment QLegend::alignment() const
347 Qt::Alignment QLegend::alignment() const
349 {
348 {
350 return d_ptr->m_alignment;
349 return d_ptr->m_alignment;
351 }
350 }
352
351
353 /*!
352 /*!
354 Detaches the legend from chart. Chart won't change layout of the legend.
353 Detaches the legend from chart. Chart won't change layout of the legend.
355 */
354 */
356 void QLegend::detachFromChart()
355 void QLegend::detachFromChart()
357 {
356 {
358 d_ptr->m_attachedToChart = false;
357 d_ptr->m_attachedToChart = false;
359 d_ptr->m_layout->invalidate();
358 d_ptr->m_layout->invalidate();
360 setParent(0);
359 setParent(0);
361
360
362 }
361 }
363
362
364 /*!
363 /*!
365 Attaches the legend to chart. Chart may change layout of the legend.
364 Attaches the legend to chart. Chart may change layout of the legend.
366 */
365 */
367 void QLegend::attachToChart()
366 void QLegend::attachToChart()
368 {
367 {
369 d_ptr->m_attachedToChart = true;
368 d_ptr->m_attachedToChart = true;
370 d_ptr->m_presenter->layout()->invalidate();
369 d_ptr->m_presenter->layout()->invalidate();
371 setParent(d_ptr->m_chart);
370 setParent(d_ptr->m_chart);
372 }
371 }
373
372
374 /*!
373 /*!
375 Returns true, if legend is attached to chart.
374 Returns true, if legend is attached to chart.
376 */
375 */
377 bool QLegend::isAttachedToChart()
376 bool QLegend::isAttachedToChart()
378 {
377 {
379 return d_ptr->m_attachedToChart;
378 return d_ptr->m_attachedToChart;
380 }
379 }
381
380
382 /*!
381 /*!
383 Sets the visibility of legend background to \a visible
382 Sets the visibility of legend background to \a visible
384 */
383 */
385 void QLegend::setBackgroundVisible(bool visible)
384 void QLegend::setBackgroundVisible(bool visible)
386 {
385 {
387 if(d_ptr->m_backgroundVisible != visible) {
386 if(d_ptr->m_backgroundVisible != visible) {
388 d_ptr->m_backgroundVisible = visible;
387 d_ptr->m_backgroundVisible = visible;
389 update();
388 update();
390 emit backgroundVisibleChanged(visible);
389 emit backgroundVisibleChanged(visible);
391 }
390 }
392 }
391 }
393
392
394 /*!
393 /*!
395 Returns the visibility of legend background
394 Returns the visibility of legend background
396 */
395 */
397 bool QLegend::isBackgroundVisible() const
396 bool QLegend::isBackgroundVisible() const
398 {
397 {
399 return d_ptr->m_backgroundVisible;
398 return d_ptr->m_backgroundVisible;
400 }
399 }
401
400
402 /*!
401 /*!
403 \internal \a event see QGraphicsWidget for details
402 \internal \a event see QGraphicsWidget for details
404 */
403 */
405 void QLegend::hideEvent(QHideEvent *event)
404 void QLegend::hideEvent(QHideEvent *event)
406 {
405 {
407 d_ptr->m_presenter->layout()->invalidate();
406 if(isAttachedToChart()) d_ptr->m_presenter->layout()->invalidate();
408 QGraphicsWidget::hideEvent(event);
407 QGraphicsWidget::hideEvent(event);
409 }
408 }
410
409
411 /*!
410 /*!
412 \internal \a event see QGraphicsWidget for details
411 \internal \a event see QGraphicsWidget for details
413 */
412 */
414 void QLegend::showEvent(QShowEvent *event)
413 void QLegend::showEvent(QShowEvent *event)
415 {
414 {
415 if(isAttachedToChart()) {
416 d_ptr->m_presenter->layout()->invalidate();
417 d_ptr->items()->setVisible(false);
418 layout()->invalidate();
419 }
416 QGraphicsWidget::showEvent(event);
420 QGraphicsWidget::showEvent(event);
417 d_ptr->m_presenter->layout()->invalidate();
418 d_ptr->items()->setVisible(false);
419 //layout activation will show the items
421 //layout activation will show the items
420 }
422 }
421
423
422 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
424 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
423
425
424 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
426 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
425 q_ptr(q),
427 q_ptr(q),
426 m_presenter(presenter),
428 m_presenter(presenter),
427 m_layout(new LegendLayout(q)),
429 m_layout(new LegendLayout(q)),
428 m_chart(chart),
430 m_chart(chart),
429 m_items(new QGraphicsItemGroup(q)),
431 m_items(new QGraphicsItemGroup(q)),
430 m_alignment(Qt::AlignTop),
432 m_alignment(Qt::AlignTop),
431 m_brush(QBrush()),
433 m_brush(QBrush()),
432 m_pen(QPen()),
434 m_pen(QPen()),
433 m_labelBrush(QBrush()),
435 m_labelBrush(QBrush()),
434 m_diameter(5),
436 m_diameter(5),
435 m_attachedToChart(true),
437 m_attachedToChart(true),
436 m_backgroundVisible(false)
438 m_backgroundVisible(false)
437 {
439 {
438
440
439 }
441 }
440
442
441 QLegendPrivate::~QLegendPrivate()
443 QLegendPrivate::~QLegendPrivate()
442 {
444 {
443
445
444 }
446 }
445
447
446 void QLegendPrivate::setOffset(qreal x, qreal y)
448 void QLegendPrivate::setOffset(qreal x, qreal y)
447 {
449 {
448 m_layout->setOffset(x,y);
450 m_layout->setOffset(x,y);
449 }
451 }
450
452
451 QPointF QLegendPrivate::offset() const
453 QPointF QLegendPrivate::offset() const
452 {
454 {
453 return m_layout->offset();
455 return m_layout->offset();
454 }
456 }
455
457
456 int QLegendPrivate::roundness(qreal size)
458 int QLegendPrivate::roundness(qreal size)
457 {
459 {
458 return 100*m_diameter/int(size);
460 return 100*m_diameter/int(size);
459 }
461 }
460
462
461 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
463 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
462 {
464 {
463 Q_UNUSED(domain)
465 Q_UNUSED(domain)
464
466
465 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
467 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
466
468
467 foreach (LegendMarker* marker, markers) {
469 foreach (LegendMarker* marker, markers) {
468 marker->setFont(m_font);
470 marker->setFont(m_font);
469 marker->setLabelBrush(m_labelBrush);
471 marker->setLabelBrush(m_labelBrush);
470 marker->setVisible(series->isVisible());
472 marker->setVisible(series->isVisible());
471 m_items->addToGroup(marker);
473 m_items->addToGroup(marker);
472 m_markers<<marker;
474 m_markers<<marker;
473 }
475 }
474
476
475 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
477 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
476 QObject::connect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*)));
478 QObject::connect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*)));
477
479
478 q_ptr->layout()->invalidate();
480 q_ptr->layout()->invalidate();
479 q_ptr->layout()->activate();
481 q_ptr->layout()->activate();
480 }
482 }
481
483
482 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
484 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
483 {
485 {
484 foreach (LegendMarker *marker, m_markers) {
486 foreach (LegendMarker *marker, m_markers) {
485 if (marker->series() == series) {
487 if (marker->series() == series) {
486 delete marker;
488 delete marker;
487 m_markers.removeAll(marker);
489 m_markers.removeAll(marker);
488 }
490 }
489 }
491 }
490
492
491 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
493 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
492 QObject::disconnect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*)));
494 QObject::disconnect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*)));
493
495
494 q_ptr->layout()->invalidate();
496 q_ptr->layout()->invalidate();
495 }
497 }
496
498
497 void QLegendPrivate::handleSeriesVisibleChanged()
499 void QLegendPrivate::handleSeriesVisibleChanged()
498 {
500 {
499 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
501 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
500
502
501 foreach (LegendMarker* marker, m_markers) {
503 foreach (LegendMarker* marker, m_markers) {
502 if (marker->series() == series) {
504 if (marker->series() == series) {
503 marker->setVisible(series->isVisible());
505 marker->setVisible(series->isVisible());
504 }
506 }
505 }
507 }
506
508
507 q_ptr->layout()->invalidate();
509 q_ptr->layout()->invalidate();
508 }
510 }
509
511
510 void QLegendPrivate::handleLegendPropertiesUpdated(QAbstractSeries *series)
512 void QLegendPrivate::handleLegendPropertiesUpdated(QAbstractSeries *series)
511 {
513 {
512 // Handle new or removed markers
514 // Handle new or removed markers
513 // Handle changes of marker pen/brush/label. every property that legend is interested
515 // Handle changes of marker pen/brush/label. every property that legend is interested
514 handleSeriesRemoved(series);
516 handleSeriesRemoved(series);
515 Domain domain;
517 Domain domain;
516 handleSeriesAdded(series, &domain);
518 handleSeriesAdded(series, &domain);
517 }
519 }
518
520
519 #include "moc_qlegend.cpp"
521 #include "moc_qlegend.cpp"
520 #include "moc_qlegend_p.cpp"
522 #include "moc_qlegend_p.cpp"
521
523
522 QTCOMMERCIALCHART_END_NAMESPACE
524 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now