##// END OF EJS Templates
first prototry of QLegendMarker API
sauimone -
r2160:5f63b94a2ef5
parent child
Show More
@@ -0,0 +1,80
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "qlegendmarker.h"
22 //#include "qlegendmarker_p.h"
23 #include <QDebug>
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
27 QLegendMarker::QLegendMarker(QObject *parent) :
28 QObject(parent)//,
29 // d_ptr(new QLegendMarkerPrivate(this))
30 {
31 }
32
33 QLegendMarker::~QLegendMarker()
34 {
35 }
36
37 QString QLegendMarker::label() const
38 {
39 return m_label;
40 }
41
42 void QLegendMarker::setLabel(const QString &label)
43 {
44 m_label = label;
45 }
46
47 QPen QLegendMarker::pen() const
48 {
49 return m_pen;
50 }
51
52 void QLegendMarker::setPen(const QPen &pen)
53 {
54 m_pen = pen;
55 }
56
57 QBrush QLegendMarker::brush() const
58 {
59 return m_brush;
60 }
61
62 void QLegendMarker::setBrush(const QBrush &brush)
63 {
64 m_brush = brush;
65 }
66
67 bool QLegendMarker::isVisible() const
68 {
69 return m_visible;
70 }
71
72 void QLegendMarker::setVisible(bool visible)
73 {
74 m_visible = visible;
75 }
76
77 #include "moc_qlegendmarker.cpp"
78 //#include "moc_qlegendmarker_p.cpp"
79
80 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,81
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef QLEGENDMARKER_H
22 #define QLEGENDMARKER_H
23
24 #include <QChartGlobal>
25 #include <QObject>
26 #include <QPen>
27 #include <QBrush>
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31 // TODO:
32 //class QLegendMarkerPrivate;
33
34 // TODO: should this be abstract?
35 class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject
36 {
37 Q_OBJECT
38
39 // TODO: need for these?
40 // Q_PROPERTY(QString label READ label WRITE setlabel NOTIFY labelChanged);
41 // Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged);
42 // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged);
43
44 public:
45 explicit QLegendMarker(QObject *parent = 0);
46 virtual ~QLegendMarker();
47
48 QString label() const;
49 void setLabel(const QString &label);
50
51 QPen pen() const;
52 void setPen(const QPen &pen);
53
54 QBrush brush() const;
55 void setBrush(const QBrush &brush);
56
57 bool isVisible() const;
58 void setVisible(bool visible);
59
60 Q_SIGNALS:
61 void clicked();
62 void hovered(bool status);
63
64 public Q_SLOTS:
65 void markersUpdated(); // TODO: private? Idea is that series signals, when for example pieslices have been added/removed.
66
67 public:
68 // TODO:
69 // QScopedPointer<QLegendMarkerPrivate> d_ptr;
70 Q_DISABLE_COPY(QLegendMarker)
71
72 // TODO: move to PIMPL
73 QString m_label;
74 QPen m_pen;
75 QBrush m_brush;
76 bool m_visible;
77 };
78
79 QTCOMMERCIALCHART_END_NAMESPACE
80
81 #endif // QLEGENDMARKER_H
@@ -1,17 +1,19
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/qlegend.cpp \
6 6 $$PWD/legendmarker.cpp \
7 $$PWD/legendlayout.cpp
7 $$PWD/legendlayout.cpp \
8 $$PWD/qlegendmarker.cpp
8 9
9 10 PRIVATE_HEADERS += \
10 11 $$PWD/legendmarker_p.h \
11 12 $$PWD/legendscroller_p.h \
12 13 $$PWD/qlegend_p.h \
13 14 $$PWD/legendlayout_p.h
14 15
15 16
16 17 PUBLIC_HEADERS += \
17 $$PWD/qlegend.h No newline at end of file
18 $$PWD/qlegend.h \
19 $$PWD/qlegendmarker.h
@@ -1,514 +1,522
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qlegend.h"
22 22 #include "qlegend_p.h"
23 23 #include "qabstractseries.h"
24 24 #include "qabstractseries_p.h"
25 25 #include "qchart_p.h"
26 26 #include "legendlayout_p.h"
27 27 #include "legendmarker_p.h"
28 28 #include "qxyseries.h"
29 29 #include "qlineseries.h"
30 30 #include "qareaseries.h"
31 31 #include "qscatterseries.h"
32 32 #include "qsplineseries.h"
33 33 #include "qabstractbarseries.h"
34 34 #include "qstackedbarseries.h"
35 35 #include "qpercentbarseries.h"
36 36 #include "qbarset.h"
37 37 #include "qpieseries.h"
38 38 #include "qpieseries_p.h"
39 39 #include "qpieslice.h"
40 40 #include "chartpresenter_p.h"
41 41 #include "chartlayout_p.h"
42 42 #include <QPainter>
43 43 #include <QPen>
44 44 #include <QTimer>
45 45 #include <QGraphicsSceneEvent>
46 46
47 47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48 48
49 49 /*!
50 50 \class QLegend
51 51 \brief Legend object
52 52 \mainclass
53 53
54 54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
55 55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
56 56 handle the drawing manually.
57 57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
58 58
59 59 \image examples_percentbarchart_legend.png
60 60
61 61 \sa QChart
62 62 */
63 63 /*!
64 64 \qmlclass Legend QLegend
65 65 \brief Legend is part of QtCommercial Chart QML API.
66 66
67 67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
68 68 series have been changed. Legend is used via ChartView class. For example:
69 69 \code
70 70 ChartView {
71 71 legend.visible: true
72 72 legend.alignment: Qt.AlignBottom
73 73 // Add a few series...
74 74 }
75 75 \endcode
76 76
77 77 \image examples_percentbarchart_legend.png
78 78 */
79 79
80 80 /*!
81 81 \property QLegend::alignment
82 82 \brief The alignment of the legend.
83 83
84 84 Legend paints on the defined position in the chart. The following alignments are supported:
85 85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
86 86 */
87 87 /*!
88 88 \qmlproperty Qt.Alignment Legend::alignment
89 89 \brief The alignment of the legend.
90 90
91 91 Legend paints on the defined position in the chart. The following alignments are supported:
92 92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
93 93 */
94 94
95 95 /*!
96 96 \property QLegend::backgroundVisible
97 97 Whether the legend background is visible or not.
98 98 */
99 99 /*!
100 100 \qmlproperty bool Legend::backgroundVisible
101 101 Whether the legend background is visible or not.
102 102 */
103 103
104 104 /*!
105 105 \property QLegend::color
106 106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
107 107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
108 108 */
109 109 /*!
110 110 \qmlproperty color Legend::color
111 111 The color of the legend, i.e. the background (brush) color.
112 112 */
113 113
114 114 /*!
115 115 \property QLegend::borderColor
116 116 The border color of the legend, i.e. the line color.
117 117 */
118 118 /*!
119 119 \qmlproperty color Legend::borderColor
120 120 The border color of the legend, i.e. the line color.
121 121 */
122 122
123 123 /*!
124 124 \property QLegend::font
125 125 The font of markers used by legend
126 126 */
127 127 /*!
128 128 \qmlproperty Font Legend::font
129 129 The font of markers used by legend
130 130 */
131 131
132 132 /*!
133 133 \property QLegend::labelColor
134 134 The color of brush used to draw labels.
135 135 */
136 136 /*!
137 137 \qmlproperty color QLegend::labelColor
138 138 The color of brush used to draw labels.
139 139 */
140 140
141 141 /*!
142 142 \fn void QLegend::backgroundVisibleChanged(bool)
143 143 The visibility of the legend background changed to \a visible.
144 144 */
145 145
146 146 /*!
147 147 \fn void QLegend::colorChanged(QColor)
148 148 The color of the legend background changed to \a color.
149 149 */
150 150
151 151 /*!
152 152 \fn void QLegend::borderColorChanged(QColor)
153 153 The border color of the legend background changed to \a color.
154 154 */
155 155
156 156 /*!
157 157 \fn void QLegend::fontChanged(QFont)
158 158 The font of markers of the legend changed to \a font.
159 159 */
160 160
161 161 /*!
162 162 \fn void QLegend::labelColorChanged(QColor color)
163 163 This signal is emitted when the color of brush used to draw labels has changed to \a color.
164 164 */
165 165
166 166 /*!
167 167 Constructs the legend object and sets the parent to \a parent
168 168 */
169 169
170 170 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
171 171 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
172 172 {
173 173 setZValue(ChartPresenter::LegendZValue);
174 174 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
175 175 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
176 176 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
177 177 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
178 178 setLayout(d_ptr->m_layout);
179 179 }
180 180
181 181 /*!
182 182 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
183 183 */
184 184 QLegend::~QLegend()
185 185 {
186 186 }
187 187
188 188 /*!
189 189 \internal
190 190 */
191 191 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
192 192 {
193 193 Q_UNUSED(option)
194 194 Q_UNUSED(widget)
195 195
196 196 if (!d_ptr->m_backgroundVisible)
197 197 return;
198 198
199 199 painter->setOpacity(opacity());
200 200 painter->setPen(d_ptr->m_pen);
201 201 painter->setBrush(d_ptr->m_brush);
202 202 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
203 203 }
204 204
205 205
206 206 /*!
207 207 Sets the \a brush of legend. Brush affects the background of legend.
208 208 */
209 209 void QLegend::setBrush(const QBrush &brush)
210 210 {
211 211 if (d_ptr->m_brush != brush) {
212 212 d_ptr->m_brush = brush;
213 213 update();
214 214 emit colorChanged(brush.color());
215 215 }
216 216 }
217 217
218 218 /*!
219 219 Returns the brush used by legend.
220 220 */
221 221 QBrush QLegend::brush() const
222 222 {
223 223 return d_ptr->m_brush;
224 224 }
225 225
226 226 void QLegend::setColor(QColor color)
227 227 {
228 228 QBrush b = d_ptr->m_brush;
229 229 if (b.style() != Qt::SolidPattern || b.color() != color) {
230 230 b.setStyle(Qt::SolidPattern);
231 231 b.setColor(color);
232 232 setBrush(b);
233 233 }
234 234 }
235 235
236 236 QColor QLegend::color()
237 237 {
238 238 return d_ptr->m_brush.color();
239 239 }
240 240
241 241 /*!
242 242 Sets the \a pen of legend. Pen affects the legend borders.
243 243 */
244 244 void QLegend::setPen(const QPen &pen)
245 245 {
246 246 if (d_ptr->m_pen != pen) {
247 247 d_ptr->m_pen = pen;
248 248 update();
249 249 emit borderColorChanged(pen.color());
250 250 }
251 251 }
252 252
253 253 /*!
254 254 Returns the pen used by legend
255 255 */
256 256
257 257 QPen QLegend::pen() const
258 258 {
259 259 return d_ptr->m_pen;
260 260 }
261 261
262 262 void QLegend::setFont(const QFont &font)
263 263 {
264 264 if (d_ptr->m_font != font) {
265 265 d_ptr->m_font = font;
266 266 foreach (LegendMarker *marker, d_ptr->markers())
267 267 marker->setFont(d_ptr->m_font);
268 268 layout()->invalidate();
269 269 emit fontChanged(font);
270 270 }
271 271 }
272 272
273 273 QFont QLegend::font() const
274 274 {
275 275 return d_ptr->m_font;
276 276 }
277 277
278 278 void QLegend::setBorderColor(QColor color)
279 279 {
280 280 QPen p = d_ptr->m_pen;
281 281 if (p.color() != color) {
282 282 p.setColor(color);
283 283 setPen(p);
284 284 }
285 285 }
286 286
287 287 QColor QLegend::borderColor()
288 288 {
289 289 return d_ptr->m_pen.color();
290 290 }
291 291
292 292 /*!
293 293 Set brush used to draw labels to \a brush.
294 294 */
295 295 void QLegend::setLabelBrush(const QBrush &brush)
296 296 {
297 297 if (d_ptr->m_labelBrush != brush) {
298 298 d_ptr->m_labelBrush = brush;
299 299 foreach (LegendMarker *marker, d_ptr->markers()) {
300 300 marker->setLabelBrush(d_ptr->m_labelBrush);
301 301 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
302 302 // instead of mapping it from label brush color
303 303 marker->setPen(brush.color());
304 304 }
305 305 emit labelColorChanged(brush.color());
306 306 }
307 307 }
308 308
309 309 /*!
310 310 Brush used to draw labels.
311 311 */
312 312 QBrush QLegend::labelBrush() const
313 313 {
314 314 return d_ptr->m_labelBrush;
315 315 }
316 316
317 317 void QLegend::setLabelColor(QColor color)
318 318 {
319 319 QBrush b = d_ptr->m_labelBrush;
320 320 if (b.style() != Qt::SolidPattern || b.color() != color) {
321 321 b.setStyle(Qt::SolidPattern);
322 322 b.setColor(color);
323 323 setLabelBrush(b);
324 324 }
325 325 }
326 326
327 327 QColor QLegend::labelColor() const
328 328 {
329 329 return d_ptr->m_labelBrush.color();
330 330 }
331 331
332 332
333 333 void QLegend::setAlignment(Qt::Alignment alignment)
334 334 {
335 335 if (d_ptr->m_alignment != alignment) {
336 336 d_ptr->m_alignment = alignment;
337 337 layout()->invalidate();
338 338 }
339 339 }
340 340
341 341 Qt::Alignment QLegend::alignment() const
342 342 {
343 343 return d_ptr->m_alignment;
344 344 }
345 345
346 346 /*!
347 347 Detaches the legend from chart. Chart won't change layout of the legend.
348 348 */
349 349 void QLegend::detachFromChart()
350 350 {
351 351 d_ptr->m_attachedToChart = false;
352 352 layout()->invalidate();
353 353 setParent(0);
354 354
355 355 }
356 356
357 357 /*!
358 358 Attaches the legend to chart. Chart may change layout of the legend.
359 359 */
360 360 void QLegend::attachToChart()
361 361 {
362 362 d_ptr->m_attachedToChart = true;
363 363 layout()->invalidate();
364 364 setParent(d_ptr->m_chart);
365 365 }
366 366
367 367 /*!
368 368 Returns true, if legend is attached to chart.
369 369 */
370 370 bool QLegend::isAttachedToChart()
371 371 {
372 372 return d_ptr->m_attachedToChart;
373 373 }
374 374
375 375 /*!
376 376 Sets the visibility of legend background to \a visible
377 377 */
378 378 void QLegend::setBackgroundVisible(bool visible)
379 379 {
380 380 if (d_ptr->m_backgroundVisible != visible) {
381 381 d_ptr->m_backgroundVisible = visible;
382 382 update();
383 383 emit backgroundVisibleChanged(visible);
384 384 }
385 385 }
386 386
387 387 /*!
388 388 Returns the visibility of legend background
389 389 */
390 390 bool QLegend::isBackgroundVisible() const
391 391 {
392 392 return d_ptr->m_backgroundVisible;
393 393 }
394 394
395
396 QList<QLegendMarker*> QLegend::markers() const
397 {
398 // TODO: name of PIMPL method will change.
399 return d_ptr->legendMarkers();
400 }
401
402
395 403 /*!
396 404 \internal \a event see QGraphicsWidget for details
397 405 */
398 406 void QLegend::hideEvent(QHideEvent *event)
399 407 {
400 408 if (isAttachedToChart())
401 409 d_ptr->m_presenter->layout()->invalidate();
402 410 QGraphicsWidget::hideEvent(event);
403 411 }
404 412 /*!
405 413 \internal \a event see QGraphicsWidget for details
406 414 */
407 415 void QLegend::showEvent(QShowEvent *event)
408 416 {
409 417 if (isAttachedToChart()) {
410 418 d_ptr->items()->setVisible(false);
411 419 layout()->invalidate();
412 420 }
413 421 QGraphicsWidget::showEvent(event);
414 422 //layout activation will show the items
415 423 }
416 424
417 425 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
418 426
419 427 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
420 428 : q_ptr(q),
421 429 m_presenter(presenter),
422 430 m_layout(new LegendLayout(q)),
423 431 m_chart(chart),
424 432 m_items(new QGraphicsItemGroup(q)),
425 433 m_alignment(Qt::AlignTop),
426 434 m_brush(QBrush()),
427 435 m_pen(QPen()),
428 436 m_labelBrush(QBrush()),
429 437 m_diameter(5),
430 438 m_attachedToChart(true),
431 439 m_backgroundVisible(false)
432 440 {
433 441
434 442 }
435 443
436 444 QLegendPrivate::~QLegendPrivate()
437 445 {
438 446
439 447 }
440 448
441 449 void QLegendPrivate::setOffset(qreal x, qreal y)
442 450 {
443 451 m_layout->setOffset(x, y);
444 452 }
445 453
446 454 QPointF QLegendPrivate::offset() const
447 455 {
448 456 return m_layout->offset();
449 457 }
450 458
451 459 int QLegendPrivate::roundness(qreal size)
452 460 {
453 461 return 100 * m_diameter / int(size);
454 462 }
455 463
456 464 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
457 465 {
458 466 Q_UNUSED(domain)
459 467
460 468 QList<LegendMarker *> markers = series->d_ptr->createLegendMarker(q_ptr);
461 469
462 470 foreach (LegendMarker *marker, markers) {
463 471 marker->setFont(m_font);
464 472 marker->setLabelBrush(m_labelBrush);
465 473 marker->setVisible(series->isVisible());
466 474 m_items->addToGroup(marker);
467 475 m_markers << marker;
468 476 }
469 477
470 478 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
471 479 QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
472 480
473 481 m_items->setVisible(false);
474 482 m_layout->invalidate();
475 483 }
476 484
477 485 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
478 486 {
479 487 foreach (LegendMarker *marker, m_markers) {
480 488 if (marker->series() == series) {
481 489 delete marker;
482 490 m_markers.removeAll(marker);
483 491 }
484 492 }
485 493
486 494 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
487 495 QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
488 496 m_layout->invalidate();
489 497 }
490 498
491 499 void QLegendPrivate::handleSeriesVisibleChanged()
492 500 {
493 501 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
494 502 Q_ASSERT(series);
495 503
496 504 foreach (LegendMarker *marker, m_markers) {
497 505 if (marker->series() == series)
498 506 marker->setVisible(series->isVisible());
499 507 }
500 508 m_layout->invalidate();
501 509 }
502 510
503 511 void QLegendPrivate::handleCountChanged()
504 512 {
505 513 QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender());
506 514 Q_ASSERT(series);
507 515 handleSeriesRemoved(series->q_ptr);
508 516 handleSeriesAdded(series->q_ptr, 0);
509 517 }
510 518
511 519 #include "moc_qlegend.cpp"
512 520 #include "moc_qlegend_p.cpp"
513 521
514 522 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,107 +1,111
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QLEGEND_H
22 22 #define QLEGEND_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QGraphicsWidget>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QPieSlice;
32 32 class QXYSeries;
33 33 class QBarSet;
34 34 class QAbstractBarSeries;
35 35 class QPieSeries;
36 36 class QAreaSeries;
37 37 class QChart;
38 38 class QLegendPrivate;
39 class QLegendMarker;
39 40
40 41 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
41 42 {
42 43 Q_OBJECT
43 44 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
44 45 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
45 46 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
46 47 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
47 48 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
48 49 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
49 50
50 51 private:
51 52 explicit QLegend(QChart *chart);
52 53
53 54 public:
54 55 ~QLegend();
55 56
56 57 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
57 58
58 59 void setBrush(const QBrush &brush);
59 60 QBrush brush() const;
60 61 void setColor(QColor color);
61 62 QColor color();
62 63
63 64 void setPen(const QPen &pen);
64 65 QPen pen() const;
65 66 void setBorderColor(QColor color);
66 67 QColor borderColor();
67 68
68 69 void setFont(const QFont &font);
69 70 QFont font() const;
70 71 void setLabelBrush(const QBrush &brush);
71 72 QBrush labelBrush() const;
72 73
73 74 void setLabelColor(QColor color);
74 75 QColor labelColor() const;
75 76
76 77 void setAlignment(Qt::Alignment alignment);
77 78 Qt::Alignment alignment() const;
78 79
79 80 void detachFromChart();
80 81 void attachToChart();
81 82 bool isAttachedToChart();
82 83
83 84 void setBackgroundVisible(bool visible = true);
84 85 bool isBackgroundVisible() const;
85 86
87 // New stuff:
88 QList <QLegendMarker*> markers() const;
89
86 90 protected:
87 91 void hideEvent(QHideEvent *event);
88 92 void showEvent(QShowEvent *event);
89 93
90 94 Q_SIGNALS:
91 95 void backgroundVisibleChanged(bool visible);
92 96 void colorChanged(QColor color);
93 97 void borderColorChanged(QColor color);
94 98 void fontChanged(QFont font);
95 99 void labelColorChanged(QColor color);
96 100
97 101 private:
98 102 QScopedPointer<QLegendPrivate> d_ptr;
99 103 Q_DISABLE_COPY(QLegend)
100 104 friend class LegendScroller;
101 105 friend class LegendLayout;
102 106 friend class ChartLayout;
103 107 };
104 108
105 109 QTCOMMERCIALCHART_END_NAMESPACE
106 110
107 111 #endif // QLEGEND_H
@@ -1,87 +1,92
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QLEGEND_P_H
31 31 #define QLEGEND_P_H
32 32
33 33 #include "qlegend.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QChart;
38 38 class ChartPresenter;
39 39 class QAbstractSeries;
40 40 class LegendLayout;
41 41 class LegendMarker;
42 42 class Domain;
43 43
44 44 class QLegendPrivate : public QObject
45 45 {
46 46 Q_OBJECT
47 47 public:
48 48 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
49 49 ~QLegendPrivate();
50 50
51 51 void setOffset(qreal x, qreal y);
52 52 QPointF offset() const;
53 53 int roundness(qreal size);
54 54
55 QList<LegendMarker *> markers() { return m_markers; }
56 QGraphicsItemGroup *items() { return m_items; }
55 QList<LegendMarker*> markers() { return m_markers; } // TODO: this will be removed
56 QGraphicsItemGroup* items() { return m_items; }
57
58 // New stuff:
59 QList<QLegendMarker*> legendMarkers() { return m_legendMarkers; } // TODO: function name will change
57 60
58 61 public Q_SLOTS:
59 62 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
60 63 void handleSeriesRemoved(QAbstractSeries *series);
61 64 void handleSeriesVisibleChanged();
62 65 void handleCountChanged();
63 66
64 67 private:
65 68 QLegend *q_ptr;
66 69 ChartPresenter *m_presenter;
67 70 LegendLayout *m_layout;
68 QChart *m_chart;
69 QGraphicsItemGroup *m_items;
70 QList<LegendMarker *> m_markers;
71 QChart* m_chart;
72 QGraphicsItemGroup* m_items;
73 QList<LegendMarker*> m_markers; // TODO: this will be removed
71 74 Qt::Alignment m_alignment;
72 75 QBrush m_brush;
73 76 QPen m_pen;
74 77 QFont m_font;
75 78 QBrush m_labelBrush;
76 79
77 80 qreal m_diameter;
78 81 bool m_attachedToChart;
79 82 bool m_backgroundVisible;
80 83
81 84 friend class QLegend;
82 85 friend class LegendLayout;
86 QList<QLegendMarker*> m_legendMarkers; // TODO: rename to m_markers eventually.
87
83 88 };
84 89
85 90 QTCOMMERCIALCHART_END_NAMESPACE
86 91
87 92 #endif
General Comments 0
You need to be logged in to leave comments. Login now