##// END OF EJS Templates
layout work started
sauimone -
r2168:778c4543bf0c
parent child
Show More
@@ -1,384 +1,464
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 "chartlayout_p.h"
25 #include "chartlayout_p.h"
26
26
27 #include "qlegendmarker_p.h"
28 #include "legendmarkeritem_p.h"
29 #include "qlegendmarker.h"
30
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
32
29 LegendLayout::LegendLayout(QLegend *legend)
33 LegendLayout::LegendLayout(QLegend *legend)
30 : m_legend(legend)
34 : m_legend(legend)
31 {
35 {
32
36
33 }
37 }
34
38
35 LegendLayout::~LegendLayout()
39 LegendLayout::~LegendLayout()
36 {
40 {
37
41
38 }
42 }
39
43
40 void LegendLayout::setOffset(qreal x, qreal y)
44 void LegendLayout::setOffset(qreal x, qreal y)
41 {
45 {
42 bool scrollHorizontal = true;
46 bool scrollHorizontal = true;
43 switch (m_legend->alignment()) {
47 switch (m_legend->alignment()) {
44 case Qt::AlignTop:
48 case Qt::AlignTop:
45 case Qt::AlignBottom:
49 case Qt::AlignBottom:
46 scrollHorizontal = true;
50 scrollHorizontal = true;
47 break;
51 break;
48 case Qt::AlignLeft:
52 case Qt::AlignLeft:
49 case Qt::AlignRight:
53 case Qt::AlignRight:
50 scrollHorizontal = false;
54 scrollHorizontal = false;
51 break;
55 break;
52 }
56 }
53
57
54 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
58 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
55 if (!m_legend->isAttachedToChart())
59 if (!m_legend->isAttachedToChart())
56 scrollHorizontal = !scrollHorizontal;
60 scrollHorizontal = !scrollHorizontal;
57
61
58 QRectF boundingRect = geometry();
62 QRectF boundingRect = geometry();
59 qreal left, top, right, bottom;
63 qreal left, top, right, bottom;
60 getContentsMargins(&left, &top, &right, &bottom);
64 getContentsMargins(&left, &top, &right, &bottom);
61 boundingRect.adjust(left, top, -right, -bottom);
65 boundingRect.adjust(left, top, -right, -bottom);
62
66
63 // Limit offset between m_minOffset and m_maxOffset
67 // Limit offset between m_minOffset and m_maxOffset
64 if (scrollHorizontal) {
68 if (scrollHorizontal) {
65 if (m_width <= boundingRect.width())
69 if (m_width <= boundingRect.width())
66 return;
70 return;
67
71
68 if (x != m_offsetX) {
72 if (x != m_offsetX) {
69 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
73 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
70 m_legend->d_ptr->items()->setPos(-m_offsetX, boundingRect.top());
74 m_legend->d_ptr->items()->setPos(-m_offsetX, boundingRect.top());
71 }
75 }
72 } else {
76 } else {
73 if (m_height <= boundingRect.height())
77 if (m_height <= boundingRect.height())
74 return;
78 return;
75
79
76 if (y != m_offsetY) {
80 if (y != m_offsetY) {
77 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
81 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
78 m_legend->d_ptr->items()->setPos(boundingRect.left(), -m_offsetY);
82 m_legend->d_ptr->items()->setPos(boundingRect.left(), -m_offsetY);
79 }
83 }
80 }
84 }
81 }
85 }
82
86
83 QPointF LegendLayout::offset() const
87 QPointF LegendLayout::offset() const
84 {
88 {
85 return QPointF(m_offsetX, m_offsetY);
89 return QPointF(m_offsetX, m_offsetY);
86 }
90 }
87
91
88 void LegendLayout::invalidate()
92 void LegendLayout::invalidate()
89 {
93 {
90 QGraphicsLayout::invalidate();
94 QGraphicsLayout::invalidate();
91 if (m_legend->isAttachedToChart())
95 if (m_legend->isAttachedToChart())
92 m_legend->d_ptr->m_presenter->layout()->invalidate();
96 m_legend->d_ptr->m_presenter->layout()->invalidate();
93 }
97 }
94
98
95 void LegendLayout::setGeometry(const QRectF &rect)
99 void LegendLayout::setGeometry(const QRectF &rect)
96 {
100 {
97 m_legend->d_ptr->items()->setVisible(m_legend->isVisible());
101 m_legend->d_ptr->items()->setVisible(m_legend->isVisible());
98
102
99 QGraphicsLayout::setGeometry(rect);
103 QGraphicsLayout::setGeometry(rect);
100
104
101 if (m_legend->isAttachedToChart())
105 if (m_legend->isAttachedToChart())
102 setAttachedGeometry(rect);
106 setAttachedGeometry(rect);
103 else
107 else
104 setDettachedGeometry(rect);
108 setDettachedGeometry(rect);
105 }
109 }
106
110
107 void LegendLayout::setAttachedGeometry(const QRectF &rect)
111 void LegendLayout::setAttachedGeometry(const QRectF &rect)
108 {
112 {
109 if (!rect.isValid())
113 if (!rect.isValid())
110 return;
114 return;
111
115
112 m_offsetX = 0;
116 m_offsetX = 0;
113 m_offsetY = 0;
117 m_offsetY = 0;
114
118
115 QSizeF size(0, 0);
119 QSizeF size(0, 0);
116
120
117 if (m_legend->d_ptr->markers().isEmpty())
121 if (m_legend->d_ptr->markers().isEmpty())
118 return;
122 return;
119
123
120 m_width = 0;
124 m_width = 0;
121 m_height = 0;
125 m_height = 0;
122
126
123 qreal left, top, right, bottom;
127 qreal left, top, right, bottom;
124 getContentsMargins(&left, &top, &right, &bottom);
128 getContentsMargins(&left, &top, &right, &bottom);
125
129
126 QRectF geometry = rect.adjusted(left, top, -right, -bottom);
130 QRectF geometry = rect.adjusted(left, top, -right, -bottom);
127
131
128 switch (m_legend->alignment()) {
132 switch(m_legend->alignment()) {
129 case Qt::AlignTop:
133 case Qt::AlignTop:
130 case Qt::AlignBottom: {
134 case Qt::AlignBottom: {
131 QPointF point(0, 0);
135 QPointF point(0,0);
132 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
136 /*
133 if (marker->isVisible()) {
137 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
134 marker->setGeometry(geometry);
138 if (marker->isVisible()) {
135 marker->setPos(point.x(), geometry.height() / 2 - marker->boundingRect().height() / 2);
139 marker->setGeometry(geometry);
136 const QRectF &rect = marker->boundingRect();
140 marker->setPos(point.x(),geometry.height()/2 - marker->boundingRect().height()/2);
137 size = size.expandedTo(rect.size());
141 const QRectF& rect = marker->boundingRect();
138 qreal w = rect.width();
142 size = size.expandedTo(rect.size());
139 m_width += w;
143 qreal w = rect.width();
140 point.setX(point.x() + w);
144 m_width+=w;
145 point.setX(point.x() + w);
146 }
147 }
148 */
149 // New markers -->>
150 foreach (QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) {
151 if (marker->isVisible()) {
152 LegendMarkerItem* item = marker->d_ptr.data()->item();
153 item->setGeometry(geometry);
154 item->setPos(point.x(),geometry.height()/2 - item->boundingRect().height()/2);
155 const QRectF& rect = item->boundingRect();
156 size = size.expandedTo(rect.size());
157 qreal w = rect.width();
158 m_width+=w;
159 point.setX(point.x() + w);
160 }
141 }
161 }
162 // <<-- New markers
163 if (m_width < geometry.width())
164 m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top());
165 else
166 m_legend->d_ptr->items()->setPos(geometry.topLeft());
167 m_height = size.height();
142 }
168 }
143 if (m_width < geometry.width())
169 break;
144 m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top());
145 else
146 m_legend->d_ptr->items()->setPos(geometry.topLeft());
147 m_height = size.height();
148 }
149 break;
150 case Qt::AlignLeft:
170 case Qt::AlignLeft:
151 case Qt::AlignRight: {
171 case Qt::AlignRight: {
152 QPointF point(0, 0);
172 QPointF point(0,0);
153 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
173 /*
154 if (marker->isVisible()) {
174 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
155 marker->setGeometry(geometry);
175 if (marker->isVisible()) {
156 marker->setPos(point);
176 marker->setGeometry(geometry);
157 const QRectF &rect = marker->boundingRect();
177 marker->setPos(point);
158 qreal h = rect.height();
178 const QRectF& rect = marker->boundingRect();
159 size = size.expandedTo(rect.size());
179 qreal h = rect.height();
160 m_height += h;
180 size = size.expandedTo(rect.size());
161 point.setY(point.y() + h);
181 m_height+=h;
182 point.setY(point.y() + h);
183 }
184 }
185 */
186 // New markers -->>
187 foreach (QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) {
188 if (marker->isVisible()) {
189 LegendMarkerItem* item = marker->d_ptr.data()->item();
190 item->setGeometry(geometry);
191 item->setPos(point);
192 const QRectF& rect = item->boundingRect();
193 qreal h = rect.height();
194 size = size.expandedTo(rect.size());
195 m_height+=h;
196 point.setY(point.y() + h);
197 }
198 }
199 // <<--- New markers
200
201 if (m_height < geometry.height())
202 m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2);
203 else
204 m_legend->d_ptr->items()->setPos(geometry.topLeft());
205 m_width = size.width();
206 break;
162 }
207 }
163 }
208 }
164
209
165 if (m_height < geometry.height())
166 m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2);
167 else
168 m_legend->d_ptr->items()->setPos(geometry.topLeft());
169 m_width = size.width();
170 break;
171 }
172 }
173
174 m_minOffsetX = -left;
210 m_minOffsetX = -left;
175 m_minOffsetY = - top;
211 m_minOffsetY = - top;
176 m_maxOffsetX = m_width - geometry.width() - right;
212 m_maxOffsetX = m_width - geometry.width() - right;
177 m_maxOffsetY = m_height - geometry.height() - bottom;
213 m_maxOffsetY = m_height - geometry.height() - bottom;
178 }
214 }
179
215
180 void LegendLayout::setDettachedGeometry(const QRectF &rect)
216 void LegendLayout::setDettachedGeometry(const QRectF &rect)
181 {
217 {
182 if (!rect.isValid())
218 if (!rect.isValid())
183 return;
219 return;
184
220
185 // Detached layout is different.
221 // Detached layout is different.
186 // In detached mode legend may have multiple rows and columns, so layout calculations
222 // In detached mode legend may have multiple rows and columns, so layout calculations
187 // differ a log from attached mode.
223 // differ a log from attached mode.
188 // Also the scrolling logic is bit different.
224 // Also the scrolling logic is bit different.
189
225
190 m_offsetX = 0;
226 m_offsetX = 0;
191 m_offsetY = 0;
227 m_offsetY = 0;
192
228
193 qreal left, top, right, bottom;
229 qreal left, top, right, bottom;
194 getContentsMargins(&left, &top, &right, &bottom);
230 getContentsMargins(&left, &top, &right, &bottom);
195 QRectF geometry = rect.adjusted(left, top, -right, -bottom);
231 QRectF geometry = rect.adjusted(left, top, -right, -bottom);
196
232
197 QSizeF size(0, 0);
233 QSizeF size(0, 0);
198
234
199 QList<LegendMarker *> markers = m_legend->d_ptr->markers();
235 QList<LegendMarker *> markers = m_legend->d_ptr->markers();
200
236
201 if (markers.isEmpty())
237 if (markers.isEmpty())
202 return;
238 return;
203
239
204 switch (m_legend->alignment()) {
240 switch (m_legend->alignment()) {
205 case Qt::AlignTop: {
241 case Qt::AlignTop: {
206 QPointF point(0, 0);
242 QPointF point(0, 0);
207 m_width = 0;
243 m_width = 0;
208 m_height = 0;
244 m_height = 0;
209 for (int i = 0; i < markers.count(); i++) {
245 for (int i = 0; i < markers.count(); i++) {
210 LegendMarker *marker = markers.at(i);
246 LegendMarker *marker = markers.at(i);
211 if (marker->isVisible()) {
247 if (marker->isVisible()) {
212 marker->setGeometry(geometry);
248 marker->setGeometry(geometry);
213 marker->setPos(point.x(), point.y());
249 marker->setPos(point.x(), point.y());
214 const QRectF &boundingRect = marker->boundingRect();
250 const QRectF &boundingRect = marker->boundingRect();
215 qreal w = boundingRect.width();
251 qreal w = boundingRect.width();
216 qreal h = boundingRect.height();
252 qreal h = boundingRect.height();
217 m_width = qMax(m_width, w);
253 m_width = qMax(m_width, w);
218 m_height = qMax(m_height, h);
254 m_height = qMax(m_height, h);
219 point.setX(point.x() + w);
255 point.setX(point.x() + w);
220 if (point.x() + w > geometry.left() + geometry.width() - right) {
256 if (point.x() + w > geometry.left() + geometry.width() - right) {
221 // Next item would go off rect.
257 // Next item would go off rect.
222 point.setX(0);
258 point.setX(0);
223 point.setY(point.y() + h);
259 point.setY(point.y() + h);
224 if (i + 1 < markers.count()) {
260 if (i + 1 < markers.count()) {
225 m_height += h;
261 m_height += h;
226 }
262 }
227 }
263 }
228 }
264 }
229 }
265 }
230 m_legend->d_ptr->items()->setPos(geometry.topLeft());
266 m_legend->d_ptr->items()->setPos(geometry.topLeft());
231
267
232 m_minOffsetX = -left;
268 m_minOffsetX = -left;
233 m_minOffsetY = -top;
269 m_minOffsetY = -top;
234 m_maxOffsetX = m_width - geometry.width() - right;
270 m_maxOffsetX = m_width - geometry.width() - right;
235 m_maxOffsetY = m_height - geometry.height() - bottom;
271 m_maxOffsetY = m_height - geometry.height() - bottom;
236 }
272 }
237 break;
273 break;
238 case Qt::AlignBottom: {
274 case Qt::AlignBottom: {
239 QPointF point(0, geometry.height());
275 QPointF point(0, geometry.height());
240 m_width = 0;
276 m_width = 0;
241 m_height = 0;
277 m_height = 0;
242 for (int i = 0; i < markers.count(); i++) {
278 for (int i = 0; i < markers.count(); i++) {
243 LegendMarker *marker = markers.at(i);
279 LegendMarker *marker = markers.at(i);
244 if (marker->isVisible()) {
280 if (marker->isVisible()) {
245 marker->setGeometry(geometry);
281 marker->setGeometry(geometry);
246 const QRectF &boundingRect = marker->boundingRect();
282 const QRectF &boundingRect = marker->boundingRect();
247 qreal w = boundingRect.width();
283 qreal w = boundingRect.width();
248 qreal h = boundingRect.height();
284 qreal h = boundingRect.height();
249 m_width = qMax(m_width, w);
285 m_width = qMax(m_width, w);
250 m_height = qMax(m_height, h);
286 m_height = qMax(m_height, h);
251 marker->setPos(point.x(), point.y() - h);
287 marker->setPos(point.x(), point.y() - h);
252 point.setX(point.x() + w);
288 point.setX(point.x() + w);
253 if (point.x() + w > geometry.left() + geometry.width() - right) {
289 if (point.x() + w > geometry.left() + geometry.width() - right) {
254 // Next item would go off rect.
290 // Next item would go off rect.
255 point.setX(0);
291 point.setX(0);
256 point.setY(point.y() - h);
292 point.setY(point.y() - h);
257 if (i + 1 < markers.count()) {
293 if (i + 1 < markers.count()) {
258 m_height += h;
294 m_height += h;
259 }
295 }
260 }
296 }
261 }
297 }
262 }
298 }
263 m_legend->d_ptr->items()->setPos(geometry.topLeft());
299 m_legend->d_ptr->items()->setPos(geometry.topLeft());
264
300
265 m_minOffsetX = -left;
301 m_minOffsetX = -left;
266 m_minOffsetY = -m_height + geometry.height() - top;
302 m_minOffsetY = -m_height + geometry.height() - top;
267 m_maxOffsetX = m_width - geometry.width() - right;
303 m_maxOffsetX = m_width - geometry.width() - right;
268 m_maxOffsetY = -bottom;
304 m_maxOffsetY = -bottom;
269 }
305 }
270 break;
306 break;
271 case Qt::AlignLeft: {
307 case Qt::AlignLeft: {
272 QPointF point(0, 0);
308 QPointF point(0, 0);
273 m_width = 0;
309 m_width = 0;
274 m_height = 0;
310 m_height = 0;
275 qreal maxWidth = 0;
311 qreal maxWidth = 0;
276 for (int i = 0; i < markers.count(); i++) {
312 for (int i = 0; i < markers.count(); i++) {
277 LegendMarker *marker = markers.at(i);
313 LegendMarker *marker = markers.at(i);
278 if (marker->isVisible()) {
314 if (marker->isVisible()) {
279 marker->setGeometry(geometry);
315 marker->setGeometry(geometry);
280 const QRectF &boundingRect = marker->boundingRect();
316 const QRectF &boundingRect = marker->boundingRect();
281 qreal w = boundingRect.width();
317 qreal w = boundingRect.width();
282 qreal h = boundingRect.height();
318 qreal h = boundingRect.height();
283 m_height = qMax(m_height, h);
319 m_height = qMax(m_height, h);
284 maxWidth = qMax(maxWidth, w);
320 maxWidth = qMax(maxWidth, w);
285 marker->setPos(point.x(), point.y());
321 marker->setPos(point.x(), point.y());
286 point.setY(point.y() + h);
322 point.setY(point.y() + h);
287 if (point.y() + h > geometry.bottom() - bottom) {
323 if (point.y() + h > geometry.bottom() - bottom) {
288 // Next item would go off rect.
324 // Next item would go off rect.
289 point.setX(point.x() + maxWidth);
325 point.setX(point.x() + maxWidth);
290 point.setY(0);
326 point.setY(0);
291 if (i + 1 < markers.count()) {
327 if (i + 1 < markers.count()) {
292 m_width += maxWidth;
328 m_width += maxWidth;
293 maxWidth = 0;
329 maxWidth = 0;
294 }
330 }
295 }
331 }
296 }
332 }
297 }
333 }
298 m_width += maxWidth;
334 m_width += maxWidth;
299 m_legend->d_ptr->items()->setPos(geometry.topLeft());
335 m_legend->d_ptr->items()->setPos(geometry.topLeft());
300
336
301 m_minOffsetX = -left;
337 m_minOffsetX = -left;
302 m_minOffsetY = -top;
338 m_minOffsetY = -top;
303 m_maxOffsetX = m_width - geometry.width() - right;
339 m_maxOffsetX = m_width - geometry.width() - right;
304 m_maxOffsetY = m_height - geometry.height() - bottom;
340 m_maxOffsetY = m_height - geometry.height() - bottom;
305 }
341 }
306 break;
342 break;
307 case Qt::AlignRight: {
343 case Qt::AlignRight: {
308 QPointF point(geometry.width(), 0);
344 QPointF point(geometry.width(), 0);
309 m_width = 0;
345 m_width = 0;
310 m_height = 0;
346 m_height = 0;
311 qreal maxWidth = 0;
347 qreal maxWidth = 0;
312 for (int i = 0; i < markers.count(); i++) {
348 for (int i = 0; i < markers.count(); i++) {
313 LegendMarker *marker = markers.at(i);
349 LegendMarker *marker = markers.at(i);
314 if (marker->isVisible()) {
350 if (marker->isVisible()) {
315 marker->setGeometry(geometry);
351 marker->setGeometry(geometry);
316 const QRectF &boundingRect = marker->boundingRect();
352 const QRectF &boundingRect = marker->boundingRect();
317 qreal w = boundingRect.width();
353 qreal w = boundingRect.width();
318 qreal h = boundingRect.height();
354 qreal h = boundingRect.height();
319 m_height = qMax(m_height, h);
355 m_height = qMax(m_height, h);
320 maxWidth = qMax(maxWidth, w);
356 maxWidth = qMax(maxWidth, w);
321 marker->setPos(point.x() - w, point.y());
357 marker->setPos(point.x() - w, point.y());
322 point.setY(point.y() + h);
358 point.setY(point.y() + h);
323 if (point.y() + h > geometry.bottom() - bottom) {
359 if (point.y() + h > geometry.bottom() - bottom) {
324 // Next item would go off rect.
360 // Next item would go off rect.
325 point.setX(point.x() - maxWidth);
361 point.setX(point.x() - maxWidth);
326 point.setY(0);
362 point.setY(0);
327 if (i + 1 < markers.count()) {
363 if (i + 1 < markers.count()) {
328 m_width += maxWidth;
364 m_width += maxWidth;
329 maxWidth = 0;
365 maxWidth = 0;
330 }
366 }
331 }
367 }
332 }
368 }
333 }
369 }
334 m_width += maxWidth;
370 m_width += maxWidth;
335 m_legend->d_ptr->items()->setPos(geometry.topLeft());
371 m_legend->d_ptr->items()->setPos(geometry.topLeft());
336
372
337 m_minOffsetX = - m_width + geometry.width() - left;
373 m_minOffsetX = - m_width + geometry.width() - left;
338 m_minOffsetY = -top;
374 m_minOffsetY = -top;
339 m_maxOffsetX = - right;
375 m_maxOffsetX = - right;
340 m_maxOffsetY = m_height - geometry.height() - bottom;
376 m_maxOffsetY = m_height - geometry.height() - bottom;
341 }
377 }
342 break;
378 break;
343 default:
379 default:
344 break;
380 break;
345 }
381 }
346
382
347 }
383 }
348
384
349 QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
385 QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
350 {
386 {
387 /*
351 QSizeF size(0, 0);
388 QSizeF size(0, 0);
352 qreal left, top, right, bottom;
389 qreal left, top, right, bottom;
353 getContentsMargins(&left, &top, &right, &bottom);
390 getContentsMargins(&left, &top, &right, &bottom);
354
391
355 if (constraint.isValid()) {
392 if (constraint.isValid()) {
356 foreach (LegendMarker *marker, m_legend->d_ptr->markers())
393 foreach (LegendMarker *marker, m_legend->d_ptr->markers())
357 size = size.expandedTo(marker->effectiveSizeHint(which));
394 size = size.expandedTo(marker->effectiveSizeHint(which));
358 size = size.boundedTo(constraint);
395 size = size.boundedTo(constraint);
359 } else if (constraint.width() >= 0) {
396 } else if (constraint.width() >= 0) {
360 qreal width = 0;
397 qreal width = 0;
361 qreal height = 0;
398 qreal height = 0;
362 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
399 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
363 width += marker->effectiveSizeHint(which).width();
400 width += marker->effectiveSizeHint(which).width();
364 height = qMax(height, marker->effectiveSizeHint(which).height());
401 height = qMax(height, marker->effectiveSizeHint(which).height());
365 }
402 }
366
403
367 size = QSizeF(qMin(constraint.width(), width), height);
404 size = QSizeF(qMin(constraint.width(), width), height);
368 } else if (constraint.height() >= 0) {
405 } else if (constraint.height() >= 0) {
369 qreal width = 0;
406 qreal width = 0;
370 qreal height = 0;
407 qreal height = 0;
371 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
408 foreach (LegendMarker *marker, m_legend->d_ptr->markers()) {
372 width = qMax(width, marker->effectiveSizeHint(which).width());
409 width = qMax(width, marker->effectiveSizeHint(which).width());
373 height += height, marker->effectiveSizeHint(which).height();
410 height += height, marker->effectiveSizeHint(which).height();
374 }
411 }
375 size = QSizeF(width, qMin(constraint.height(), height));
412 size = QSizeF(width, qMin(constraint.height(), height));
376 } else {
413 } else {
377 foreach (LegendMarker *marker, m_legend->d_ptr->markers())
414 foreach (LegendMarker *marker, m_legend->d_ptr->markers())
378 size = size.expandedTo(marker->effectiveSizeHint(which));
415 size = size.expandedTo(marker->effectiveSizeHint(which));
379 }
416 }
380 size += QSize(left + right, top + bottom);
417 size += QSize(left + right, top + bottom);
381 return size;
418 return size;
419 */
420 // New markers -->>
421 QSizeF size(0, 0);
422 qreal left, top, right, bottom;
423 getContentsMargins(&left, &top, &right, &bottom);
424
425 if(constraint.isValid()) {
426 foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) {
427 LegendMarkerItem *item = marker->d_ptr.data()->item();
428 size = size.expandedTo(item->effectiveSizeHint(which));
429 }
430 size = size.boundedTo(constraint);
431 }
432 else if (constraint.width() >= 0) {
433 qreal width = 0;
434 qreal height = 0;
435 foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) {
436 LegendMarkerItem *item = marker->d_ptr.data()->item();
437 width+=item->effectiveSizeHint(which).width();
438 height=qMax(height,item->effectiveSizeHint(which).height());
439 }
440
441 size = QSizeF(qMin(constraint.width(),width), height);
442 }
443 else if (constraint.height() >= 0) {
444 qreal width = 0;
445 qreal height = 0;
446 foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) {
447 LegendMarkerItem *item = marker->d_ptr.data()->item();
448 width=qMax(width,item->effectiveSizeHint(which).width());
449 height+=height,item->effectiveSizeHint(which).height();
450 }
451 size = QSizeF(width,qMin(constraint.height(),height));
452 }
453 else {
454 foreach(QLegendMarker* marker, m_legend->d_ptr->legendMarkers()) {
455 LegendMarkerItem *item = marker->d_ptr.data()->item();
456 size = size.expandedTo(item->effectiveSizeHint(which));
457 }
458 }
459 size += QSize(left + right, top + bottom);
460 return size;
461 // <<-- New markers
382 }
462 }
383
463
384 QTCOMMERCIALCHART_END_NAMESPACE
464 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,175 +1,177
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 "legendmarkeritem_p.h"
21 #include "legendmarkeritem_p.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include <QGraphicsSceneEvent>
23 #include <QGraphicsSceneEvent>
24 #include <QGraphicsSimpleTextItem>
24 #include <QGraphicsSimpleTextItem>
25 #include <QDebug>
25 #include <QDebug>
26
26
27 #include "qlegendmarker_p.h"
27 #include "qlegendmarker_p.h"
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 //LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) :
31 //LegendMarkerItem::LegendMarkerItem(QAbstractSeries *series, QGraphicsObject *parent) :
32 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
32 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
33 QGraphicsObject(parent),
33 QGraphicsObject(parent),
34 m_marker(marker),
34 m_marker(marker),
35 m_markerRect(0,0,10.0,10.0),
35 m_markerRect(0,0,10.0,10.0),
36 m_boundingRect(0,0,0,0),
36 m_boundingRect(0,0,10,10),
37 m_textItem(new QGraphicsSimpleTextItem(this)),
37 m_textItem(new QGraphicsSimpleTextItem(this)),
38 m_rectItem(new QGraphicsRectItem(this)),
38 m_rectItem(new QGraphicsRectItem(this)),
39 m_margin(4),
39 m_margin(4),
40 m_space(4)
40 m_space(4)
41 {
41 {
42 qDebug() << "LegendMarkerItem created for marker:" << m_marker;
42 // qDebug() << "LegendMarkerItem created for marker:" << m_marker;
43 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
43 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
44 m_rectItem->setRect(m_markerRect);
44 m_rectItem->setRect(m_markerRect);
45 // setZValue(zValue() + 20);
46 // qDebug() << "z:" << this->zValue();
45 }
47 }
46
48
47 void LegendMarkerItem::setPen(const QPen &pen)
49 void LegendMarkerItem::setPen(const QPen &pen)
48 {
50 {
49 m_rectItem->setPen(pen);
51 m_rectItem->setPen(pen);
50 }
52 }
51
53
52 QPen LegendMarkerItem::pen() const
54 QPen LegendMarkerItem::pen() const
53 {
55 {
54 return m_rectItem->pen();
56 return m_rectItem->pen();
55 }
57 }
56
58
57 void LegendMarkerItem::setBrush(const QBrush &brush)
59 void LegendMarkerItem::setBrush(const QBrush &brush)
58 {
60 {
59 m_rectItem->setBrush(brush);
61 m_rectItem->setBrush(brush);
60 }
62 }
61
63
62 QBrush LegendMarkerItem::brush() const
64 QBrush LegendMarkerItem::brush() const
63 {
65 {
64 return m_rectItem->brush();
66 return m_rectItem->brush();
65 }
67 }
66
68
67 void LegendMarkerItem::setFont(const QFont &font)
69 void LegendMarkerItem::setFont(const QFont &font)
68 {
70 {
69 m_textItem->setFont(font);
71 m_textItem->setFont(font);
70 QFontMetrics fn(font);
72 QFontMetrics fn(font);
71 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
73 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
72 updateGeometry();
74 updateGeometry();
73 }
75 }
74
76
75 QFont LegendMarkerItem::font() const
77 QFont LegendMarkerItem::font() const
76 {
78 {
77 return m_textItem->font();
79 return m_textItem->font();
78 }
80 }
79
81
80 void LegendMarkerItem::setLabel(const QString label)
82 void LegendMarkerItem::setLabel(const QString label)
81 {
83 {
84 qDebug() << "LegendMarkerItem::setlabel" << label;
82 m_text = label;
85 m_text = label;
83 updateGeometry();
86 updateGeometry();
84 }
87 }
85
88
86 QString LegendMarkerItem::label() const
89 QString LegendMarkerItem::label() const
87 {
90 {
88 return m_text;
91 return m_text;
89 }
92 }
90
93
91 QRectF LegendMarkerItem::boundingRect() const
92 {
93 return m_boundingRect;
94 }
95
96 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
94 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
97 {
95 {
98 m_textItem->setBrush(brush);
96 m_textItem->setBrush(brush);
99 }
97 }
100
98
101 QBrush LegendMarkerItem::labelBrush() const
99 QBrush LegendMarkerItem::labelBrush() const
102 {
100 {
103 return m_textItem->brush();
101 return m_textItem->brush();
104 }
102 }
105
103
106
107 void LegendMarkerItem::setGeometry(const QRectF& rect)
104 void LegendMarkerItem::setGeometry(const QRectF& rect)
108 {
105 {
109 QFontMetrics fn (font());
106 QFontMetrics fn (m_font);
110
107
111 int width = rect.width();
108 int width = rect.width();
112 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
109 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
113 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
110 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
114
111
115 if (fn.boundingRect(m_text).width() + x > width)
112 if (fn.boundingRect(m_text).width() + x > width)
116 {
113 {
117 QString string = m_text + "...";
114 QString string = m_text + "...";
118 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
115 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
119 string.remove(string.length() - 4, 1);
116 string.remove(string.length() - 4, 1);
120 m_textItem->setText(string);
117 m_textItem->setText(string);
121 }
118 }
122 else
119 else
123 m_textItem->setText(m_text);
120 m_textItem->setText(m_text);
124
121
125 const QRectF& textRect = m_textItem->boundingRect();
122 const QRectF& textRect = m_textItem->boundingRect();
126
123
127
124
128 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
125 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
129 m_rectItem->setRect(m_markerRect);
126 m_rectItem->setRect(m_markerRect);
130 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
127 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
131
128
132 prepareGeometryChange();
129 prepareGeometryChange();
133 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
130 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
134 }
131 }
135
132
133 QRectF LegendMarkerItem::boundingRect() const
134 {
135 return m_boundingRect;
136 }
137
136 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
138 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
137 {
139 {
138 Q_UNUSED(option)
140 Q_UNUSED(option)
139 Q_UNUSED(widget)
141 Q_UNUSED(widget)
140 Q_UNUSED(painter)
142 Q_UNUSED(painter)
141 }
143 }
142
144
143 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
145 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
144 {
146 {
145 Q_UNUSED(constraint)
147 Q_UNUSED(constraint)
146
148
147 QFontMetrics fn(m_textItem->font());
149 QFontMetrics fn(m_textItem->font());
148 QSizeF sh;
150 QSizeF sh;
149
151
150 switch (which) {
152 switch (which) {
151 case Qt::MinimumSize:
153 case Qt::MinimumSize:
152 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
154 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
153 break;
155 break;
154 case Qt::PreferredSize:
156 case Qt::PreferredSize:
155 sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
157 sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
156 break;
158 break;
157 default:
159 default:
158 break;
160 break;
159 }
161 }
160
162
161 return sh;
163 return sh;
162 }
164 }
163
165
164 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
166 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
165 {
167 {
166 qDebug() << "LegendMarkerItem::mousePressEvent";
168 qDebug() << "LegendMarkerItem::mousePressEvent";
167 // QGraphicsObject::mousePressEvent(event);
169 // QGraphicsObject::mousePressEvent(event);
168 //TODO: selected signal removed for now
170 //TODO: selected signal removed for now
169 emit m_marker->handleMousePressEvent(event);
171 emit m_marker->handleMousePressEvent(event);
170 QGraphicsItem::mousePressEvent(event);
172 QGraphicsItem::mousePressEvent(event);
171 }
173 }
172
174
173 #include "moc_legendmarkeritem_p.cpp"
175 #include "moc_legendmarkeritem_p.cpp"
174
176
175 QTCOMMERCIALCHART_END_NAMESPACE
177 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,93 +1,102
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 LEGENDMARKERITEM_P_H
30 #ifndef LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QFont>
35 #include <QBrush>
36 #include <QBrush>
36 #include <QPen>
37 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsLayoutItem>
39 #include <QGraphicsLayoutItem>
39
40
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41
42
42 class QLegendMarkerPrivate;
43 class QLegendMarkerPrivate;
43
44
44 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
45 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
45 {
46 {
46 Q_OBJECT
47 Q_OBJECT
47 Q_INTERFACES(QGraphicsLayoutItem)
48 Q_INTERFACES(QGraphicsLayoutItem)
48 public:
49 public:
49 // explicit LegendMarkerItem(QAbstractSeries *m_series, QGraphicsObject *parent = 0);
50 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
50 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
51
51
52 void setPen(const QPen &pen);
52 void setPen(const QPen &pen);
53 QPen pen() const;
53 QPen pen() const;
54
54
55 void setBrush(const QBrush &brush);
55 void setBrush(const QBrush &brush);
56 QBrush brush() const;
56 QBrush brush() const;
57
57
58 void setFont(const QFont &font);
58 void setFont(const QFont &font);
59 QFont font() const;
59 QFont font() const;
60
60
61 void setLabel(const QString label);
61 void setLabel(const QString label);
62 QString label() const;
62 QString label() const;
63
63
64 void setLabelBrush(const QBrush &brush);
64 void setLabelBrush(const QBrush &brush);
65 QBrush labelBrush() const;
65 QBrush labelBrush() const;
66
66
67 void setGeometry(const QRectF& rect);
67 void setGeometry(const QRectF& rect);
68
68
69 QRectF boundingRect() const;
69 QRectF boundingRect() const;
70
70
71 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
71 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
72
72
73 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
73 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
74
74
75 protected:
75 protected:
76 // From QGraphicsObject
76 // From QGraphicsObject
77 void mousePressEvent(QGraphicsSceneMouseEvent *event);
77 void mousePressEvent(QGraphicsSceneMouseEvent *event);
78
78
79 protected:
79 protected:
80 QLegendMarkerPrivate *m_marker;
80 QLegendMarkerPrivate *m_marker; // Knows
81 QRectF m_markerRect;
81 QRectF m_markerRect;
82 QRectF m_boundingRect;
82 QRectF m_boundingRect;
83 QGraphicsSimpleTextItem *m_textItem;
83 QGraphicsSimpleTextItem *m_textItem;
84 QGraphicsRectItem *m_rectItem;
84 QGraphicsRectItem *m_rectItem;
85 qreal m_margin;
85 qreal m_margin;
86 qreal m_space;
86 qreal m_space;
87 QString m_text;
87 QString m_text;
88
88
89 QString m_label;
90 QBrush m_labelBrush;
91 QFont m_font;
92 QPen m_pen;
93 QBrush m_brush;
94 bool m_visible;
95
96 friend class QLegendMarkerPrivate;
97 friend class LegendLayout;
89 };
98 };
90
99
91 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
92
101
93 #endif // LEGENDMARKERITEM_P_H
102 #endif // LEGENDMARKERITEM_P_H
@@ -1,599 +1,597
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" // TODO: deprecated
27 #include "legendmarker_p.h" // TODO: deprecated
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 "chartlayout_p.h"
41 #include "chartlayout_p.h"
42 #include <QPainter>
42 #include <QPainter>
43 #include <QPen>
43 #include <QPen>
44 #include <QTimer>
44 #include <QTimer>
45 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
46
46
47 #include <QLegendMarker>
47 #include <QLegendMarker>
48 #include "qlegendmarker_p.h"
48 #include "qlegendmarker_p.h"
49 #include "legendmarkeritem_p.h"
49 #include "legendmarkeritem_p.h"
50
50
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52
52
53 /*!
53 /*!
54 \class QLegend
54 \class QLegend
55 \brief Legend object
55 \brief Legend object
56 \mainclass
56 \mainclass
57
57
58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
60 handle the drawing manually.
60 handle the drawing manually.
61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
62
62
63 \image examples_percentbarchart_legend.png
63 \image examples_percentbarchart_legend.png
64
64
65 \sa QChart
65 \sa QChart
66 */
66 */
67 /*!
67 /*!
68 \qmlclass Legend QLegend
68 \qmlclass Legend QLegend
69 \brief Legend is part of QtCommercial Chart QML API.
69 \brief Legend is part of QtCommercial Chart QML API.
70
70
71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
72 series have been changed. Legend is used via ChartView class. For example:
72 series have been changed. Legend is used via ChartView class. For example:
73 \code
73 \code
74 ChartView {
74 ChartView {
75 legend.visible: true
75 legend.visible: true
76 legend.alignment: Qt.AlignBottom
76 legend.alignment: Qt.AlignBottom
77 // Add a few series...
77 // Add a few series...
78 }
78 }
79 \endcode
79 \endcode
80
80
81 \image examples_percentbarchart_legend.png
81 \image examples_percentbarchart_legend.png
82 */
82 */
83
83
84 /*!
84 /*!
85 \property QLegend::alignment
85 \property QLegend::alignment
86 \brief The alignment of the legend.
86 \brief The alignment of the legend.
87
87
88 Legend paints on the defined position in the chart. The following alignments are supported:
88 Legend paints on the defined position in the chart. The following alignments are supported:
89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
90 */
90 */
91 /*!
91 /*!
92 \qmlproperty Qt.Alignment Legend::alignment
92 \qmlproperty Qt.Alignment Legend::alignment
93 \brief The alignment of the legend.
93 \brief The alignment of the legend.
94
94
95 Legend paints on the defined position in the chart. The following alignments are supported:
95 Legend paints on the defined position in the chart. The following alignments are supported:
96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
97 */
97 */
98
98
99 /*!
99 /*!
100 \property QLegend::backgroundVisible
100 \property QLegend::backgroundVisible
101 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
102 */
102 */
103 /*!
103 /*!
104 \qmlproperty bool Legend::backgroundVisible
104 \qmlproperty bool Legend::backgroundVisible
105 Whether the legend background is visible or not.
105 Whether the legend background is visible or not.
106 */
106 */
107
107
108 /*!
108 /*!
109 \property QLegend::color
109 \property QLegend::color
110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
112 */
112 */
113 /*!
113 /*!
114 \qmlproperty color Legend::color
114 \qmlproperty color Legend::color
115 The color of the legend, i.e. the background (brush) color.
115 The color of the legend, i.e. the background (brush) color.
116 */
116 */
117
117
118 /*!
118 /*!
119 \property QLegend::borderColor
119 \property QLegend::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 \qmlproperty color Legend::borderColor
123 \qmlproperty color Legend::borderColor
124 The border color of the legend, i.e. the line color.
124 The border color of the legend, i.e. the line color.
125 */
125 */
126
126
127 /*!
127 /*!
128 \property QLegend::font
128 \property QLegend::font
129 The font of markers used by legend
129 The font of markers used by legend
130 */
130 */
131 /*!
131 /*!
132 \qmlproperty Font Legend::font
132 \qmlproperty Font Legend::font
133 The font of markers used by legend
133 The font of markers used by legend
134 */
134 */
135
135
136 /*!
136 /*!
137 \property QLegend::labelColor
137 \property 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 \qmlproperty color QLegend::labelColor
141 \qmlproperty color QLegend::labelColor
142 The color of brush used to draw labels.
142 The color of brush used to draw labels.
143 */
143 */
144
144
145 /*!
145 /*!
146 \fn void QLegend::backgroundVisibleChanged(bool)
146 \fn void QLegend::backgroundVisibleChanged(bool)
147 The visibility of the legend background changed to \a visible.
147 The visibility of the legend background changed to \a visible.
148 */
148 */
149
149
150 /*!
150 /*!
151 \fn void QLegend::colorChanged(QColor)
151 \fn void QLegend::colorChanged(QColor)
152 The color of the legend background changed to \a color.
152 The color of the legend background changed to \a color.
153 */
153 */
154
154
155 /*!
155 /*!
156 \fn void QLegend::borderColorChanged(QColor)
156 \fn void QLegend::borderColorChanged(QColor)
157 The border color of the legend background changed to \a color.
157 The border color of the legend background changed to \a color.
158 */
158 */
159
159
160 /*!
160 /*!
161 \fn void QLegend::fontChanged(QFont)
161 \fn void QLegend::fontChanged(QFont)
162 The font of markers of the legend changed to \a font.
162 The font of markers of the legend changed to \a font.
163 */
163 */
164
164
165 /*!
165 /*!
166 \fn void QLegend::labelColorChanged(QColor color)
166 \fn void QLegend::labelColorChanged(QColor color)
167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
168 */
168 */
169
169
170 /*!
170 /*!
171 Constructs the legend object and sets the parent to \a parent
171 Constructs the legend object and sets the parent to \a parent
172 */
172 */
173
173
174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
176 {
176 {
177 setZValue(ChartPresenter::LegendZValue);
177 setZValue(ChartPresenter::LegendZValue);
178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
182 setLayout(d_ptr->m_layout);
182 setLayout(d_ptr->m_layout);
183 }
183 }
184
184
185 /*!
185 /*!
186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
187 */
187 */
188 QLegend::~QLegend()
188 QLegend::~QLegend()
189 {
189 {
190 }
190 }
191
191
192 /*!
192 /*!
193 \internal
193 \internal
194 */
194 */
195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
196 {
196 {
197 Q_UNUSED(option)
197 Q_UNUSED(option)
198 Q_UNUSED(widget)
198 Q_UNUSED(widget)
199
199
200 if (!d_ptr->m_backgroundVisible)
200 if (!d_ptr->m_backgroundVisible)
201 return;
201 return;
202
202
203 painter->setOpacity(opacity());
203 painter->setOpacity(opacity());
204 painter->setPen(d_ptr->m_pen);
204 painter->setPen(d_ptr->m_pen);
205 painter->setBrush(d_ptr->m_brush);
205 painter->setBrush(d_ptr->m_brush);
206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
207 }
207 }
208
208
209
209
210 /*!
210 /*!
211 Sets the \a brush of legend. Brush affects the background of legend.
211 Sets the \a brush of legend. Brush affects the background of legend.
212 */
212 */
213 void QLegend::setBrush(const QBrush &brush)
213 void QLegend::setBrush(const QBrush &brush)
214 {
214 {
215 if (d_ptr->m_brush != brush) {
215 if (d_ptr->m_brush != brush) {
216 d_ptr->m_brush = brush;
216 d_ptr->m_brush = brush;
217 update();
217 update();
218 emit colorChanged(brush.color());
218 emit colorChanged(brush.color());
219 }
219 }
220 }
220 }
221
221
222 /*!
222 /*!
223 Returns the brush used by legend.
223 Returns the brush used by legend.
224 */
224 */
225 QBrush QLegend::brush() const
225 QBrush QLegend::brush() const
226 {
226 {
227 return d_ptr->m_brush;
227 return d_ptr->m_brush;
228 }
228 }
229
229
230 void QLegend::setColor(QColor color)
230 void QLegend::setColor(QColor color)
231 {
231 {
232 QBrush b = d_ptr->m_brush;
232 QBrush b = d_ptr->m_brush;
233 if (b.style() != Qt::SolidPattern || b.color() != color) {
233 if (b.style() != Qt::SolidPattern || b.color() != color) {
234 b.setStyle(Qt::SolidPattern);
234 b.setStyle(Qt::SolidPattern);
235 b.setColor(color);
235 b.setColor(color);
236 setBrush(b);
236 setBrush(b);
237 }
237 }
238 }
238 }
239
239
240 QColor QLegend::color()
240 QColor QLegend::color()
241 {
241 {
242 return d_ptr->m_brush.color();
242 return d_ptr->m_brush.color();
243 }
243 }
244
244
245 /*!
245 /*!
246 Sets the \a pen of legend. Pen affects the legend borders.
246 Sets the \a pen of legend. Pen affects the legend borders.
247 */
247 */
248 void QLegend::setPen(const QPen &pen)
248 void QLegend::setPen(const QPen &pen)
249 {
249 {
250 if (d_ptr->m_pen != pen) {
250 if (d_ptr->m_pen != pen) {
251 d_ptr->m_pen = pen;
251 d_ptr->m_pen = pen;
252 update();
252 update();
253 emit borderColorChanged(pen.color());
253 emit borderColorChanged(pen.color());
254 }
254 }
255 }
255 }
256
256
257 /*!
257 /*!
258 Returns the pen used by legend
258 Returns the pen used by legend
259 */
259 */
260
260
261 QPen QLegend::pen() const
261 QPen QLegend::pen() const
262 {
262 {
263 return d_ptr->m_pen;
263 return d_ptr->m_pen;
264 }
264 }
265
265
266 void QLegend::setFont(const QFont &font)
266 void QLegend::setFont(const QFont &font)
267 {
267 {
268 if (d_ptr->m_font != font) {
268 if (d_ptr->m_font != font) {
269 d_ptr->m_font = font;
269 d_ptr->m_font = font;
270 foreach (LegendMarker *marker, d_ptr->markers())
270 foreach (LegendMarker *marker, d_ptr->markers())
271 marker->setFont(d_ptr->m_font);
271 marker->setFont(d_ptr->m_font);
272 layout()->invalidate();
272 layout()->invalidate();
273 emit fontChanged(font);
273 emit fontChanged(font);
274 }
274 }
275 }
275 }
276
276
277 QFont QLegend::font() const
277 QFont QLegend::font() const
278 {
278 {
279 return d_ptr->m_font;
279 return d_ptr->m_font;
280 }
280 }
281
281
282 void QLegend::setBorderColor(QColor color)
282 void QLegend::setBorderColor(QColor color)
283 {
283 {
284 QPen p = d_ptr->m_pen;
284 QPen p = d_ptr->m_pen;
285 if (p.color() != color) {
285 if (p.color() != color) {
286 p.setColor(color);
286 p.setColor(color);
287 setPen(p);
287 setPen(p);
288 }
288 }
289 }
289 }
290
290
291 QColor QLegend::borderColor()
291 QColor QLegend::borderColor()
292 {
292 {
293 return d_ptr->m_pen.color();
293 return d_ptr->m_pen.color();
294 }
294 }
295
295
296 /*!
296 /*!
297 Set brush used to draw labels to \a brush.
297 Set brush used to draw labels to \a brush.
298 */
298 */
299 void QLegend::setLabelBrush(const QBrush &brush)
299 void QLegend::setLabelBrush(const QBrush &brush)
300 {
300 {
301 if (d_ptr->m_labelBrush != brush) {
301 if (d_ptr->m_labelBrush != brush) {
302 d_ptr->m_labelBrush = brush;
302 d_ptr->m_labelBrush = brush;
303 foreach (LegendMarker *marker, d_ptr->markers()) {
303 foreach (LegendMarker *marker, d_ptr->markers()) {
304 marker->setLabelBrush(d_ptr->m_labelBrush);
304 marker->setLabelBrush(d_ptr->m_labelBrush);
305 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
305 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
306 // instead of mapping it from label brush color
306 // instead of mapping it from label brush color
307 marker->setPen(brush.color());
307 marker->setPen(brush.color());
308 }
308 }
309 emit labelColorChanged(brush.color());
309 emit labelColorChanged(brush.color());
310 }
310 }
311 }
311 }
312
312
313 /*!
313 /*!
314 Brush used to draw labels.
314 Brush used to draw labels.
315 */
315 */
316 QBrush QLegend::labelBrush() const
316 QBrush QLegend::labelBrush() const
317 {
317 {
318 return d_ptr->m_labelBrush;
318 return d_ptr->m_labelBrush;
319 }
319 }
320
320
321 void QLegend::setLabelColor(QColor color)
321 void QLegend::setLabelColor(QColor color)
322 {
322 {
323 QBrush b = d_ptr->m_labelBrush;
323 QBrush b = d_ptr->m_labelBrush;
324 if (b.style() != Qt::SolidPattern || b.color() != color) {
324 if (b.style() != Qt::SolidPattern || b.color() != color) {
325 b.setStyle(Qt::SolidPattern);
325 b.setStyle(Qt::SolidPattern);
326 b.setColor(color);
326 b.setColor(color);
327 setLabelBrush(b);
327 setLabelBrush(b);
328 }
328 }
329 }
329 }
330
330
331 QColor QLegend::labelColor() const
331 QColor QLegend::labelColor() const
332 {
332 {
333 return d_ptr->m_labelBrush.color();
333 return d_ptr->m_labelBrush.color();
334 }
334 }
335
335
336
336
337 void QLegend::setAlignment(Qt::Alignment alignment)
337 void QLegend::setAlignment(Qt::Alignment alignment)
338 {
338 {
339 if (d_ptr->m_alignment != alignment) {
339 if (d_ptr->m_alignment != alignment) {
340 d_ptr->m_alignment = alignment;
340 d_ptr->m_alignment = alignment;
341 layout()->invalidate();
341 layout()->invalidate();
342 }
342 }
343 }
343 }
344
344
345 Qt::Alignment QLegend::alignment() const
345 Qt::Alignment QLegend::alignment() const
346 {
346 {
347 return d_ptr->m_alignment;
347 return d_ptr->m_alignment;
348 }
348 }
349
349
350 /*!
350 /*!
351 Detaches the legend from chart. Chart won't change layout of the legend.
351 Detaches the legend from chart. Chart won't change layout of the legend.
352 */
352 */
353 void QLegend::detachFromChart()
353 void QLegend::detachFromChart()
354 {
354 {
355 d_ptr->m_attachedToChart = false;
355 d_ptr->m_attachedToChart = false;
356 layout()->invalidate();
356 layout()->invalidate();
357 setParent(0);
357 setParent(0);
358
358
359 }
359 }
360
360
361 /*!
361 /*!
362 Attaches the legend to chart. Chart may change layout of the legend.
362 Attaches the legend to chart. Chart may change layout of the legend.
363 */
363 */
364 void QLegend::attachToChart()
364 void QLegend::attachToChart()
365 {
365 {
366 d_ptr->m_attachedToChart = true;
366 d_ptr->m_attachedToChart = true;
367 layout()->invalidate();
367 layout()->invalidate();
368 setParent(d_ptr->m_chart);
368 setParent(d_ptr->m_chart);
369 }
369 }
370
370
371 /*!
371 /*!
372 Returns true, if legend is attached to chart.
372 Returns true, if legend is attached to chart.
373 */
373 */
374 bool QLegend::isAttachedToChart()
374 bool QLegend::isAttachedToChart()
375 {
375 {
376 return d_ptr->m_attachedToChart;
376 return d_ptr->m_attachedToChart;
377 }
377 }
378
378
379 /*!
379 /*!
380 Sets the visibility of legend background to \a visible
380 Sets the visibility of legend background to \a visible
381 */
381 */
382 void QLegend::setBackgroundVisible(bool visible)
382 void QLegend::setBackgroundVisible(bool visible)
383 {
383 {
384 if (d_ptr->m_backgroundVisible != visible) {
384 if (d_ptr->m_backgroundVisible != visible) {
385 d_ptr->m_backgroundVisible = visible;
385 d_ptr->m_backgroundVisible = visible;
386 update();
386 update();
387 emit backgroundVisibleChanged(visible);
387 emit backgroundVisibleChanged(visible);
388 }
388 }
389 }
389 }
390
390
391 /*!
391 /*!
392 Returns the visibility of legend background
392 Returns the visibility of legend background
393 */
393 */
394 bool QLegend::isBackgroundVisible() const
394 bool QLegend::isBackgroundVisible() const
395 {
395 {
396 return d_ptr->m_backgroundVisible;
396 return d_ptr->m_backgroundVisible;
397 }
397 }
398
398
399
399
400 QList<QLegendMarker*> QLegend::markers() const
400 QList<QLegendMarker*> QLegend::markers() const
401 {
401 {
402 // TODO: name of PIMPL method will change.
402 // TODO: name of PIMPL method will change.
403 return d_ptr->legendMarkers();
403 return d_ptr->legendMarkers();
404 }
404 }
405
405
406 void QLegend::appendSeries(QAbstractSeries* series)
406 void QLegend::appendSeries(QAbstractSeries* series)
407 {
407 {
408 d_ptr->appendSeries(series);
408 d_ptr->appendSeries(series);
409 }
409 }
410
410
411 void QLegend::removeSeries(QAbstractSeries* series)
411 void QLegend::removeSeries(QAbstractSeries* series)
412 {
412 {
413 d_ptr->removeSeries(series);
413 d_ptr->removeSeries(series);
414 }
414 }
415
415
416 /*!
416 /*!
417 \internal \a event see QGraphicsWidget for details
417 \internal \a event see QGraphicsWidget for details
418 */
418 */
419 void QLegend::hideEvent(QHideEvent *event)
419 void QLegend::hideEvent(QHideEvent *event)
420 {
420 {
421 if (isAttachedToChart())
421 if (isAttachedToChart())
422 d_ptr->m_presenter->layout()->invalidate();
422 d_ptr->m_presenter->layout()->invalidate();
423 QGraphicsWidget::hideEvent(event);
423 QGraphicsWidget::hideEvent(event);
424 }
424 }
425 /*!
425 /*!
426 \internal \a event see QGraphicsWidget for details
426 \internal \a event see QGraphicsWidget for details
427 */
427 */
428 void QLegend::showEvent(QShowEvent *event)
428 void QLegend::showEvent(QShowEvent *event)
429 {
429 {
430 if (isAttachedToChart()) {
430 if (isAttachedToChart()) {
431 d_ptr->items()->setVisible(false);
431 d_ptr->items()->setVisible(false);
432 layout()->invalidate();
432 layout()->invalidate();
433 }
433 }
434 QGraphicsWidget::showEvent(event);
434 QGraphicsWidget::showEvent(event);
435 //layout activation will show the items
435 //layout activation will show the items
436 }
436 }
437
437
438 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
438 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
439
439
440 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
440 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
441 : q_ptr(q),
441 : q_ptr(q),
442 m_presenter(presenter),
442 m_presenter(presenter),
443 m_layout(new LegendLayout(q)),
443 m_layout(new LegendLayout(q)),
444 m_chart(chart),
444 m_chart(chart),
445 m_items(new QGraphicsItemGroup(q)),
445 m_items(new QGraphicsItemGroup(q)),
446 m_alignment(Qt::AlignTop),
446 m_alignment(Qt::AlignTop),
447 m_brush(QBrush()),
447 m_brush(QBrush()),
448 m_pen(QPen()),
448 m_pen(QPen()),
449 m_labelBrush(QBrush()),
449 m_labelBrush(QBrush()),
450 m_diameter(5),
450 m_diameter(5),
451 m_attachedToChart(true),
451 m_attachedToChart(true),
452 m_backgroundVisible(false)
452 m_backgroundVisible(false)
453 {
453 {
454
454
455 }
455 }
456
456
457 QLegendPrivate::~QLegendPrivate()
457 QLegendPrivate::~QLegendPrivate()
458 {
458 {
459
459
460 }
460 }
461
461
462 void QLegendPrivate::setOffset(qreal x, qreal y)
462 void QLegendPrivate::setOffset(qreal x, qreal y)
463 {
463 {
464 m_layout->setOffset(x, y);
464 m_layout->setOffset(x, y);
465 }
465 }
466
466
467 QPointF QLegendPrivate::offset() const
467 QPointF QLegendPrivate::offset() const
468 {
468 {
469 return m_layout->offset();
469 return m_layout->offset();
470 }
470 }
471
471
472 int QLegendPrivate::roundness(qreal size)
472 int QLegendPrivate::roundness(qreal size)
473 {
473 {
474 return 100 * m_diameter / int(size);
474 return 100 * m_diameter / int(size);
475 }
475 }
476
476
477 void QLegendPrivate::appendSeries(QAbstractSeries* series)
477 void QLegendPrivate::appendSeries(QAbstractSeries* series)
478 {
478 {
479 Q_UNUSED(series);
479 Q_UNUSED(series);
480 // TODO:
480 // TODO:
481 /*
481 /*
482 if (!m_series.contains(series)) {
482 if (!m_series.contains(series)) {
483 m_series.append(series);
483 m_series.append(series);
484 handleSeriesAdded(series,0); // Dummy domain
484 handleSeriesAdded(series,0); // Dummy domain
485 }
485 }
486 */
486 */
487 }
487 }
488
488
489 void QLegendPrivate::removeSeries(QAbstractSeries* series)
489 void QLegendPrivate::removeSeries(QAbstractSeries* series)
490 {
490 {
491 Q_UNUSED(series);
491 Q_UNUSED(series);
492 // TODO:
492 // TODO:
493 /*
493 /*
494 if (m_series.contains(series)) {
494 if (m_series.contains(series)) {
495 m_series.removeOne(series);
495 m_series.removeOne(series);
496 handleSeriesRemoved(series);
496 handleSeriesRemoved(series);
497 }
497 }
498 */
498 */
499 }
499 }
500
500
501 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
501 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
502 {
502 {
503 Q_UNUSED(domain)
503 Q_UNUSED(domain)
504
504
505 qDebug() << "QLegendPrivate::handleSeriesAdded";
505 qDebug() << "QLegendPrivate::handleSeriesAdded";
506
506
507 // New markers --->
507 // New markers --->
508 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
508 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
509 foreach (QLegendMarker* marker, newMarkers) {
509 foreach (QLegendMarker* marker, newMarkers) {
510 marker->setFont(m_font);
510 marker->setFont(m_font);
511 marker->setLabelBrush(m_labelBrush);
511 marker->setLabelBrush(m_labelBrush);
512 marker->setVisible(series->isVisible());
512 marker->setVisible(series->isVisible());
513 // TODO: possible hazard. What if marker is deleted and group still has pointer?
514 m_items->addToGroup(marker->d_ptr.data()->item());
513 m_items->addToGroup(marker->d_ptr.data()->item());
515 // qDebug() << "item:" << marker->d_ptr.data()->item();
516 m_legendMarkers << marker;
514 m_legendMarkers << marker;
517 }
515 }
518
516
519 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
517 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
520 // <--- New markers
518 // <--- New markers
521
519
522 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
520 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
523
521
524 foreach (LegendMarker *marker, markers) {
522 foreach (LegendMarker *marker, markers) {
525 marker->setFont(m_font);
523 marker->setFont(m_font);
526 marker->setLabelBrush(m_labelBrush);
524 marker->setLabelBrush(m_labelBrush);
527 marker->setVisible(series->isVisible());
525 marker->setVisible(series->isVisible());
528 m_items->addToGroup(marker);
526 m_items->addToGroup(marker);
529 m_markers << marker;
527 m_markers << marker;
530 }
528 }
531
529
532 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
530 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
533 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
531 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
534
532
535 m_items->setVisible(false);
533 m_items->setVisible(false);
536 m_layout->invalidate();
534 m_layout->invalidate();
537 }
535 }
538
536
539 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
537 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
540 {
538 {
541 // New markers --->
539 // New markers --->
542 foreach (QLegendMarker *marker, m_legendMarkers) {
540 foreach (QLegendMarker *marker, m_legendMarkers) {
543 if (marker->series() == series) {
541 if (marker->series() == series) {
544 delete marker;
542 delete marker;
545 m_legendMarkers.removeAll(marker);
543 m_legendMarkers.removeAll(marker);
546 }
544 }
547 }
545 }
548
546
549 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
547 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
550 // <--- New markers
548 // <--- New markers
551
549
552 foreach (LegendMarker *marker, m_markers) {
550 foreach (LegendMarker *marker, m_markers) {
553 if (marker->series() == series) {
551 if (marker->series() == series) {
554 delete marker;
552 delete marker;
555 m_markers.removeAll(marker);
553 m_markers.removeAll(marker);
556 }
554 }
557 }
555 }
558
556
559 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
557 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
560 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
558 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
561 m_layout->invalidate();
559 m_layout->invalidate();
562 }
560 }
563
561
564 void QLegendPrivate::handleSeriesVisibleChanged()
562 void QLegendPrivate::handleSeriesVisibleChanged()
565 {
563 {
566 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
564 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
567 Q_ASSERT(series);
565 Q_ASSERT(series);
568
566
569 // New markers --->
567 // New markers --->
570 foreach (QLegendMarker* marker, m_legendMarkers) {
568 foreach (QLegendMarker* marker, m_legendMarkers) {
571 if (marker->series() == series) {
569 if (marker->series() == series) {
572 marker->setVisible(series->isVisible());
570 marker->setVisible(series->isVisible());
573 }
571 }
574 }
572 }
575
573
576 // <--- New markers
574 // <--- New markers
577
575
578 foreach (LegendMarker* marker, m_markers) {
576 foreach (LegendMarker* marker, m_markers) {
579 if (marker->series() == series) {
577 if (marker->series() == series) {
580 marker->setVisible(series->isVisible());
578 marker->setVisible(series->isVisible());
581 }
579 }
582 m_layout->invalidate();
580 m_layout->invalidate();
583 }
581 }
584
582
585 void QLegendPrivate::handleCountChanged()
583 void QLegendPrivate::handleCountChanged()
586 {
584 {
587 // With new markers, the series shoud notify markers directly?
585 // With new markers, the series shoud notify markers directly?
588
586
589 // Handle new or removed markers
587 // Handle new or removed markers
590 // Handle changes of marker pen/brush/label. every property that legend is interested
588 // Handle changes of marker pen/brush/label. every property that legend is interested
591 handleSeriesRemoved(series);
589 handleSeriesRemoved(series);
592 Domain domain;
590 Domain domain;
593 handleSeriesAdded(series, &domain);
591 handleSeriesAdded(series, &domain);
594 }
592 }
595
593
596 #include "moc_qlegend.cpp"
594 #include "moc_qlegend.cpp"
597 #include "moc_qlegend_p.cpp"
595 #include "moc_qlegend_p.cpp"
598
596
599 QTCOMMERCIALCHART_END_NAMESPACE
597 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,137 +1,189
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 "qlegendmarker.h"
21 #include "qlegendmarker.h"
22 #include "qlegendmarker_p.h"
22 #include "qlegendmarker_p.h"
23 #include "legendmarkeritem_p.h"
23 #include "legendmarkeritem_p.h"
24 #include <QDebug>
24 #include <QDebug>
25 #include <QFontMetrics>
25 #include <QFontMetrics>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 /*
28 /*
29 QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) :
29 QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) :
30 QObject(parent),
30 QObject(parent),
31 d_ptr(new QLegendMarkerPrivate(series, this))
31 d_ptr(new QLegendMarkerPrivate(series, this))
32 {
32 {
33 }
33 }
34 */
34 */
35 QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) :
35 QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) :
36 QObject(parent),
36 QObject(parent),
37 d_ptr(&d)
37 d_ptr(&d)
38 {
38 {
39 }
39 }
40
40
41 QLegendMarker::~QLegendMarker()
41 QLegendMarker::~QLegendMarker()
42 {
42 {
43 }
43 }
44
44
45 QString QLegendMarker::label() const
45 QString QLegendMarker::label() const
46 {
46 {
47 return d_ptr->m_label;
47 return d_ptr->label();
48 }
48 }
49
49
50 void QLegendMarker::setLabel(const QString &label)
50 void QLegendMarker::setLabel(const QString &label)
51 {
51 {
52 d_ptr->m_label = label;
52 d_ptr->setLabel(label);
53 }
53 }
54
54
55 QBrush QLegendMarker::labelBrush() const
55 QBrush QLegendMarker::labelBrush() const
56 {
56 {
57 return d_ptr->m_labelBrush;
57 return d_ptr->labelBrush();
58 }
58 }
59
59
60 void QLegendMarker::setLabelBrush(const QBrush &brush)
60 void QLegendMarker::setLabelBrush(const QBrush &brush)
61 {
61 {
62 d_ptr->m_labelBrush = brush;
62 d_ptr->setLabelBrush(brush);
63 }
63 }
64
64
65 QFont QLegendMarker::font() const
65 QFont QLegendMarker::font() const
66 {
66 {
67 return d_ptr->m_font;
67 return d_ptr->font();
68 }
68 }
69
69
70 void QLegendMarker::setFont(const QFont &font)
70 void QLegendMarker::setFont(const QFont &font)
71 {
71 {
72 d_ptr->m_font = font;
72 d_ptr->setFont(font);
73 }
73 }
74
74
75 QPen QLegendMarker::pen() const
75 QPen QLegendMarker::pen() const
76 {
76 {
77 return d_ptr->m_pen;
77 return d_ptr->pen();
78 }
78 }
79
79
80 void QLegendMarker::setPen(const QPen &pen)
80 void QLegendMarker::setPen(const QPen &pen)
81 {
81 {
82 d_ptr->m_pen = pen;
82 d_ptr->setPen(pen);
83 }
83 }
84
84
85 QBrush QLegendMarker::brush() const
85 QBrush QLegendMarker::brush() const
86 {
86 {
87 return d_ptr->m_brush;
87 return d_ptr->brush();
88 }
88 }
89
89
90 void QLegendMarker::setBrush(const QBrush &brush)
90 void QLegendMarker::setBrush(const QBrush &brush)
91 {
91 {
92 d_ptr->m_brush = brush;
92 d_ptr->setBrush(brush);
93 }
93 }
94
94
95 bool QLegendMarker::isVisible() const
95 bool QLegendMarker::isVisible() const
96 {
96 {
97 return d_ptr->m_visible;
97 return d_ptr->isVisible();
98 }
98 }
99
99
100 void QLegendMarker::setVisible(bool visible)
100 void QLegendMarker::setVisible(bool visible)
101 {
101 {
102 d_ptr->m_visible = visible;
102 d_ptr->setVisible(visible);
103 }
103 }
104
104
105 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106 /*
107 QLegendMarkerPrivate::QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q) :
108 q_ptr(q),
109 m_series(series)
110 {
111 m_item = new LegendMarkerItem(m_series);
112 }
113 */
114 QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q) :
106 QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q) :
115 q_ptr(q)
107 q_ptr(q)
116 {
108 {
117 qDebug() << "QLegendMarkerPrivate created";
109 // qDebug() << "QLegendMarkerPrivate created";
118 m_item = new LegendMarkerItem(this);
110 m_item = new LegendMarkerItem(this);
119 }
111 }
120
112
121 QLegendMarkerPrivate::~QLegendMarkerPrivate()
113 QLegendMarkerPrivate::~QLegendMarkerPrivate()
122 {
114 {
123 }
115 }
124
116
125 void QLegendMarkerPrivate::handleMousePressEvent(QGraphicsSceneEvent *event)
117 void QLegendMarkerPrivate::handleMousePressEvent(QGraphicsSceneEvent *event)
126 {
118 {
127 // Just emit clicked signal for now
119 // Just emit clicked signal for now
128 Q_UNUSED(event);
120 Q_UNUSED(event);
129 Q_Q(QLegendMarker);
121 Q_Q(QLegendMarker);
130 emit q->clicked();
122 emit q->clicked();
131 }
123 }
132
124
125 void QLegendMarkerPrivate::setPen(const QPen &pen)
126 {
127 m_item->setPen(pen);
128 }
129
130 QPen QLegendMarkerPrivate::pen() const
131 {
132 return m_item->pen();
133 }
134
135 void QLegendMarkerPrivate::setBrush(const QBrush &brush)
136 {
137 m_item->setBrush(brush);
138 }
139
140 QBrush QLegendMarkerPrivate::brush() const
141 {
142 return m_item->brush();
143 }
144
145 void QLegendMarkerPrivate::setFont(const QFont &font)
146 {
147 m_item->setFont(font);
148 }
149
150 QFont QLegendMarkerPrivate::font() const
151 {
152 return m_item->font();
153 }
154
155 void QLegendMarkerPrivate::setLabel(const QString label)
156 {
157 m_item->setLabel(label);
158 }
159
160 QString QLegendMarkerPrivate::label() const
161 {
162 return m_item->label();
163 }
164
165 void QLegendMarkerPrivate::setLabelBrush(const QBrush &brush)
166 {
167 m_item->setLabelBrush(brush);
168 }
169
170 QBrush QLegendMarkerPrivate::labelBrush() const
171 {
172 return m_item->labelBrush();
173 }
174
175 bool QLegendMarkerPrivate::isVisible() const
176 {
177 return m_item->isVisible();
178 }
179
180 void QLegendMarkerPrivate::setVisible(bool visible)
181 {
182 m_item->setVisible(visible);
183 }
184
133
185
134 #include "moc_qlegendmarker.cpp"
186 #include "moc_qlegendmarker.cpp"
135 #include "moc_qlegendmarker_p.cpp"
187 #include "moc_qlegendmarker_p.cpp"
136
188
137 QTCOMMERCIALCHART_END_NAMESPACE
189 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,90 +1,88
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 QLEGENDMARKER_H
21 #ifndef QLEGENDMARKER_H
22 #define QLEGENDMARKER_H
22 #define QLEGENDMARKER_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QObject>
25 #include <QObject>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28 #include <QFont>
28 #include <QFont>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QLegendMarkerPrivate;
32 class QLegendMarkerPrivate;
33 class QAbstractSeries;
33 class QAbstractSeries;
34
34
35 // TODO: should this be QAbstractLegendMarker?
35 // TODO: should this be QAbstractLegendMarker?
36 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
36 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
37 {
37 {
38 Q_OBJECT
38 Q_OBJECT
39
39
40 // TODO: need for these?
40 // TODO: need for these?
41 // Q_PROPERTY(QString label READ label WRITE setlabel NOTIFY labelChanged);
41 // Q_PROPERTY(QString label READ label WRITE setlabel NOTIFY labelChanged);
42 // Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged);
42 // Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged);
43 // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged);
43 // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged);
44
44
45 public:
45 public:
46 // explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0);
46 // explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0);
47 virtual ~QLegendMarker();
47 virtual ~QLegendMarker();
48
48
49 QString label() const;
49 QString label() const;
50 void setLabel(const QString &label);
50 void setLabel(const QString &label);
51
51
52 QBrush labelBrush() const;
52 QBrush labelBrush() const;
53 void setLabelBrush(const QBrush &brush);
53 void setLabelBrush(const QBrush &brush);
54
54
55 QFont font() const;
55 QFont font() const;
56 void setFont(const QFont &font);
56 void setFont(const QFont &font);
57
57
58 QPen pen() const;
58 QPen pen() const;
59 void setPen(const QPen &pen);
59 void setPen(const QPen &pen);
60
60
61 QBrush brush() const;
61 QBrush brush() const;
62 void setBrush(const QBrush &brush);
62 void setBrush(const QBrush &brush);
63
63
64 bool isVisible() const;
64 bool isVisible() const;
65 void setVisible(bool visible);
65 void setVisible(bool visible);
66
66
67 // virtual QAbstractSeries::SeriesType type() = 0; // TODO?
67 // virtual QAbstractSeries::SeriesType type() = 0; // TODO?
68 virtual QAbstractSeries* series() = 0;
68 virtual QAbstractSeries* series() = 0;
69 virtual QObject* peerObject() = 0;
69 virtual QObject* peerObject() = 0;
70
70
71 protected:
71 protected:
72 explicit QLegendMarker(QLegendMarkerPrivate &d, QObject *parent = 0);
72 explicit QLegendMarker(QLegendMarkerPrivate &d, QObject *parent = 0);
73
73
74 Q_SIGNALS:
74 Q_SIGNALS:
75 void clicked();
75 void clicked();
76 void hovered(bool status);
76 void hovered(bool status);
77
77
78 public Q_SLOTS:
79 virtual void updated() = 0; // TODO: private. Idea is that series signals, when some property has changed
80
81 protected:
78 protected:
82 QScopedPointer<QLegendMarkerPrivate> d_ptr;
79 QScopedPointer<QLegendMarkerPrivate> d_ptr;
83 Q_DISABLE_COPY(QLegendMarker)
80 Q_DISABLE_COPY(QLegendMarker)
84 friend class QLegendPrivate;
81 friend class QLegendPrivate;
85 friend class QLegendMarkerPrivate;
82 friend class QLegendMarkerPrivate;
83 friend class LegendLayout;
86 };
84 };
87
85
88 QTCOMMERCIALCHART_END_NAMESPACE
86 QTCOMMERCIALCHART_END_NAMESPACE
89
87
90 #endif // QLEGENDMARKER_H
88 #endif // QLEGENDMARKER_H
@@ -1,111 +1,115
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 QLEGENDMARKERPRIVATE_H
30 #ifndef QLEGENDMARKERPRIVATE_H
31 #define QLEGENDMARKERPRIVATE_H
31 #define QLEGENDMARKERPRIVATE_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 #include <QGraphicsLayoutItem>
38 #include <QGraphicsLayoutItem>
39 #include <QDebug>
39 #include <QDebug>
40
40
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
42
42
43 // TODO: check these
43 // TODO: check these
44 class QAbstractSeries;
44 class QAbstractSeries;
45 class QAreaSeries;
45 class QAreaSeries;
46 class QXYSeries;
46 class QXYSeries;
47 class QBarSet;
47 class QBarSet;
48 class QAbstractBarSeries;
48 class QAbstractBarSeries;
49 class QPieSlice;
49 class QPieSlice;
50 class QLegend;
50 class QLegend;
51 class QPieSeries;
51 class QPieSeries;
52
52
53 class QLegendMarker;
53 class QLegendMarker;
54 class LegendMarkerItem;
54 class LegendMarkerItem;
55
55
56 class QLegendMarkerPrivate : public QObject
56 class QLegendMarkerPrivate : public QObject
57 {
57 {
58 Q_OBJECT
58 Q_OBJECT
59 public:
59 public:
60 // explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q);
60 // explicit QLegendMarkerPrivate(QAbstractSeries *series, QLegendMarker *q);
61 explicit QLegendMarkerPrivate(QLegendMarker *q);
61 explicit QLegendMarkerPrivate(QLegendMarker *q);
62 virtual ~QLegendMarkerPrivate();
62 virtual ~QLegendMarkerPrivate();
63 /*
63
64 void setPen(const QPen &pen);
64 void setPen(const QPen &pen);
65 QPen pen() const;
65 QPen pen() const;
66
66
67 void setBrush(const QBrush &brush);
67 void setBrush(const QBrush &brush);
68 QBrush brush() const;
68 QBrush brush() const;
69
69
70 void setFont(const QFont &font);
70 void setFont(const QFont &font);
71 QFont font() const;
71 QFont font() const;
72
72
73 void setLabel(const QString label);
73 void setLabel(const QString label);
74 QString label() const;
74 QString label() const;
75
75
76 void setLabelBrush(const QBrush &brush);
76 void setLabelBrush(const QBrush &brush);
77 QBrush labelBrush() const;
77 QBrush labelBrush() const;
78 */
78
79 bool isVisible() const;
80 void setVisible(bool visible);
81
79 // Helper for now.
82 // Helper for now.
80 LegendMarkerItem* item() const { return m_item; }
83 LegendMarkerItem* item() const { return m_item; }
81
84
82 // Item gets the event, logic for event is here
85 // Item gets the event, logic for event is here
83 void handleMousePressEvent(QGraphicsSceneEvent *event);
86 void handleMousePressEvent(QGraphicsSceneEvent *event);
84
87
85 public Q_SLOTS:
88 public Q_SLOTS:
86 virtual void updated() {};
89 virtual void updated() {};
87
90
88 protected:
91 protected:
89 LegendMarkerItem *m_item;
92 LegendMarkerItem *m_item;
90
93
91 private:
94 private:
92 QLegendMarker *q_ptr;
95 QLegendMarker *q_ptr;
93 /*
96 /*
94 QLegend* m_legend;
97 QLegend* m_legend;
95 */
98 */
96
99
97 // New legend marker properties
100 // New legend marker properties
101 /*
98 QString m_label;
102 QString m_label;
99 QBrush m_labelBrush;
103 QBrush m_labelBrush;
100 QFont m_font;
104 QFont m_font;
101 QPen m_pen;
105 QPen m_pen;
102 QBrush m_brush;
106 QBrush m_brush;
103 bool m_visible;
107 bool m_visible;
104
108 */
105 friend class QLegendPrivate; // TODO: Is this needed?
109 friend class QLegendPrivate; // TODO: Is this needed?
106 Q_DECLARE_PUBLIC(QLegendMarker)
110 Q_DECLARE_PUBLIC(QLegendMarker)
107 };
111 };
108
112
109 QTCOMMERCIALCHART_END_NAMESPACE
113 QTCOMMERCIALCHART_END_NAMESPACE
110
114
111 #endif // QLEGENDMARKERPRIVATE_H
115 #endif // QLEGENDMARKERPRIVATE_H
@@ -1,91 +1,88
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 "qpielegendmarker.h"
21 #include "qpielegendmarker.h"
22 #include "qpielegendmarker_p.h"
22 #include "qpielegendmarker_p.h"
23 #include <QPieSeries>
23 #include <QPieSeries>
24 #include <QDebug>
24 #include <QDebug>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 /*
27
28 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
28 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
29 QLegendMarker(series, parent),
29 QLegendMarker(*new QPieLegendMarkerPrivate(series,slice,this), parent)
30 d_ptr(new QPieLegendMarkerPrivate(series,slice,this))
31 {
30 {
32 QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated()));
33 QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated()));
34 updated();
35 }
31 }
36
32
37 */
33 QPieLegendMarker::~QPieLegendMarker()
38 QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) :
39 QLegendMarker(*new QPieLegendMarkerPrivate(series,slice,this), parent)
40 {
34 {
41 QObject::connect(slice, SIGNAL(labelChanged()), this, SLOT(updated()));
42 QObject::connect(slice, SIGNAL(brushChanged()), this, SLOT(updated()));
43 // updated();
44 }
35 }
45
36
46 /*!
37 /*!
47 \internal
38 \internal
48 */
39 */
49 QPieLegendMarker::QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent) :
40 QPieLegendMarker::QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent) :
50 QLegendMarker(d, parent)
41 QLegendMarker(d, parent)
51 {
42 {
52 }
43 }
53
44
54 QAbstractSeries* QPieLegendMarker::series()
45 QAbstractSeries* QPieLegendMarker::series()
55 {
46 {
56 Q_D(QPieLegendMarker);
47 Q_D(QPieLegendMarker);
57 return d->m_series;
48 return d->m_series;
58 }
49 }
59
50
60 QPieSlice* QPieLegendMarker::peerObject()
51 QPieSlice* QPieLegendMarker::peerObject()
61 {
52 {
62 Q_D(QPieLegendMarker);
53 Q_D(QPieLegendMarker);
63 return d->m_slice;
54 return d->m_slice;
64 }
55 }
65
56
66 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67
58
68 //QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QAbstractSeries *series, QPieLegendMarker *q) :
69 QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QPieSeries *series, QPieSlice *slice, QPieLegendMarker *q) :
59 QPieLegendMarkerPrivate::QPieLegendMarkerPrivate(QPieSeries *series, QPieSlice *slice, QPieLegendMarker *q) :
70 QLegendMarkerPrivate(q),
60 QLegendMarkerPrivate(q),
71 m_series(series),
61 m_series(series),
72 m_slice(slice)
62 m_slice(slice)
73 {
63 {
74 qDebug() << "QPieLegendMarkerPrivate created";
64 qDebug() << "QPieLegendMarkerPrivate created";
65 QObject::connect(m_slice, SIGNAL(labelChanged()), this, SLOT(updated()));
66 QObject::connect(m_slice, SIGNAL(brushChanged()), this, SLOT(updated()));
75 updated();
67 updated();
76 }
68 }
77
69
78 QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate()
70 QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate()
79 {
71 {
72 QObject::disconnect(m_slice, SIGNAL(labelChanged()), this, SLOT(updated()));
73 QObject::disconnect(m_slice, SIGNAL(brushChanged()), this, SLOT(updated()));
80 }
74 }
81
75
82 void QPieLegendMarkerPrivate::updated()
76 void QPieLegendMarkerPrivate::updated()
83 {
77 {
78 qDebug() << "QPieLegendMarkerPrivate::updated";
84 m_item->setBrush(m_slice->brush());
79 m_item->setBrush(m_slice->brush());
85 m_item->setLabel(m_slice->label());
80 m_item->setLabel(m_slice->label());
81 m_item->setPen(m_slice->pen());
82 m_item->setBrush(m_slice->brush());
86 }
83 }
87
84
88 #include "moc_qpielegendmarker.cpp"
85 #include "moc_qpielegendmarker.cpp"
89 #include "moc_qpielegendmarker_p.cpp"
86 #include "moc_qpielegendmarker_p.cpp"
90
87
91 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,56
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 QPIELEGENDMARKER_H
21 #ifndef QPIELEGENDMARKER_H
22 #define QPIELEGENDMARKER_H
22 #define QPIELEGENDMARKER_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QLegendMarker>
25 #include <QLegendMarker>
26 #include <QPieSlice>
26 #include <QPieSlice>
27 #include "qpielegendmarker_p.h"
27 #include "qpielegendmarker_p.h"
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker
31 class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public:
35 public:
36 explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0);
36 explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0);
37 virtual ~QPieLegendMarker();
37
38
38 virtual QAbstractSeries* series();
39 virtual QAbstractSeries* series();
39 virtual QPieSlice* peerObject();
40 virtual QPieSlice* peerObject();
40
41
41 protected:
42 protected:
42 QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent = 0);
43 QPieLegendMarker(QPieLegendMarkerPrivate &d, QObject *parent = 0);
43
44
44 //Q_SIGNALS:
45 //Q_SIGNALS:
45
46
46 //public Q_SLOTS:
47 //public Q_SLOTS:
47
48
48 private:
49 private:
49 Q_DECLARE_PRIVATE(QPieLegendMarker)
50 Q_DECLARE_PRIVATE(QPieLegendMarker)
50 Q_DISABLE_COPY(QPieLegendMarker)
51 Q_DISABLE_COPY(QPieLegendMarker)
51
52
52 };
53 };
53
54
54 QTCOMMERCIALCHART_END_NAMESPACE
55 QTCOMMERCIALCHART_END_NAMESPACE
55 #endif // QPIELEGENDMARKER_H
56 #endif // QPIELEGENDMARKER_H
General Comments 0
You need to be logged in to leave comments. Login now