qlegend.cpp
564 lines
| 14.3 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
|
r950 | ** | ||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r794 | |||
|
r524 | #include "qlegend.h" | ||
|
r950 | #include "qlegend_p.h" | ||
|
r988 | #include "qabstractseries.h" | ||
#include "qabstractseries_p.h" | ||||
|
r950 | #include "qchart_p.h" | ||
|
r1534 | #include "legendlayout_p.h" | ||
|
r547 | #include "legendmarker_p.h" | ||
|
r565 | #include "qxyseries.h" | ||
#include "qlineseries.h" | ||||
#include "qareaseries.h" | ||||
#include "qscatterseries.h" | ||||
#include "qsplineseries.h" | ||||
|
r1586 | #include "qabstractbarseries.h" | ||
|
r565 | #include "qstackedbarseries.h" | ||
#include "qpercentbarseries.h" | ||||
#include "qbarset.h" | ||||
#include "qpieseries.h" | ||||
|
r1009 | #include "qpieseries_p.h" | ||
|
r565 | #include "qpieslice.h" | ||
|
r626 | #include "chartpresenter_p.h" | ||
|
r2105 | #include "chartlayout_p.h" | ||
|
r529 | #include <QPainter> | ||
#include <QPen> | ||||
|
r855 | #include <QTimer> | ||
|
r547 | #include <QGraphicsSceneEvent> | ||
|
r529 | |||
|
r2163 | #include <QLegendMarker> | ||
|
r547 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r529 | |||
|
r728 | /*! | ||
|
r995 | \class QLegend | ||
|
r1501 | \brief Legend object | ||
|
r995 | \mainclass | ||
|
r728 | |||
|
r995 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when | ||
series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and | ||||
handle the drawing manually. | ||||
User isn't supposed to create or delete legend objects, but can reference it via QChart class. | ||||
|
r728 | |||
|
r995 | \image examples_percentbarchart_legend.png | ||
|
r728 | |||
|
r995 | \sa QChart | ||
*/ | ||||
|
r1452 | /*! | ||
\qmlclass Legend QLegend | ||||
\brief Legend is part of QtCommercial Chart QML API. | ||||
Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when | ||||
|
r1475 | series have been changed. Legend is used via ChartView class. For example: | ||
|
r1452 | \code | ||
ChartView { | ||||
legend.visible: true | ||||
legend.alignment: Qt.AlignBottom | ||||
// Add a few series... | ||||
} | ||||
\endcode | ||||
\image examples_percentbarchart_legend.png | ||||
*/ | ||||
/*! | ||||
\property QLegend::alignment | ||||
\brief The alignment of the legend. | ||||
Legend paints on the defined position in the chart. The following alignments are supported: | ||||
Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined. | ||||
*/ | ||||
/*! | ||||
\qmlproperty Qt.Alignment Legend::alignment | ||||
\brief The alignment of the legend. | ||||
Legend paints on the defined position in the chart. The following alignments are supported: | ||||
Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined. | ||||
*/ | ||||
/*! | ||||
\property QLegend::backgroundVisible | ||||
Whether the legend background is visible or not. | ||||
*/ | ||||
/*! | ||||
\qmlproperty bool Legend::backgroundVisible | ||||
Whether the legend background is visible or not. | ||||
*/ | ||||
/*! | ||||
\property QLegend::color | ||||
|
r1472 | The color of the legend, i.e. the background (brush) color. Note that if you change the color | ||
of the legend, the style of the legend brush is set to Qt::SolidPattern. | ||||
|
r1452 | */ | ||
/*! | ||||
\qmlproperty color Legend::color | ||||
|
r1472 | The color of the legend, i.e. the background (brush) color. | ||
|
r1452 | */ | ||
/*! | ||||
\property QLegend::borderColor | ||||
The border color of the legend, i.e. the line color. | ||||
*/ | ||||
/*! | ||||
\qmlproperty color Legend::borderColor | ||||
The border color of the legend, i.e. the line color. | ||||
*/ | ||||
|
r728 | |||
|
r1522 | /*! | ||
\property QLegend::font | ||||
The font of markers used by legend | ||||
*/ | ||||
/*! | ||||
|
r2113 | \qmlproperty Font Legend::font | ||
|
r1522 | The font of markers used by legend | ||
*/ | ||||
|
r1529 | /*! | ||
\property QLegend::labelColor | ||||
The color of brush used to draw labels. | ||||
*/ | ||||
/*! | ||||
\qmlproperty color QLegend::labelColor | ||||
The color of brush used to draw labels. | ||||
*/ | ||||
|
r1452 | /*! | ||
\fn void QLegend::backgroundVisibleChanged(bool) | ||||
The visibility of the legend background changed to \a visible. | ||||
*/ | ||||
/*! | ||||
\fn void QLegend::colorChanged(QColor) | ||||
The color of the legend background changed to \a color. | ||||
*/ | ||||
/*! | ||||
\fn void QLegend::borderColorChanged(QColor) | ||||
The border color of the legend background changed to \a color. | ||||
|
r1357 | */ | ||
|
r724 | |||
|
r1522 | /*! | ||
\fn void QLegend::fontChanged(QFont) | ||||
The font of markers of the legend changed to \a font. | ||||
*/ | ||||
|
r1529 | /*! | ||
\fn void QLegend::labelColorChanged(QColor color) | ||||
This signal is emitted when the color of brush used to draw labels has changed to \a color. | ||||
*/ | ||||
|
r724 | /*! | ||
|
r950 | Constructs the legend object and sets the parent to \a parent | ||
*/ | ||||
|
r855 | |||
|
r2097 | QLegend::QLegend(QChart *chart): QGraphicsWidget(chart), | ||
d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this)) | ||||
|
r762 | { | ||
|
r626 | setZValue(ChartPresenter::LegendZValue); | ||
|
r855 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
|
r2097 | QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); | ||
|
r2131 | QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*))); | ||
|
r2081 | // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); | ||
|
r1534 | setLayout(d_ptr->m_layout); | ||
|
r950 | } | ||
|
r1002 | /*! | ||
Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. | ||||
*/ | ||||
|
r950 | QLegend::~QLegend() | ||
{ | ||||
|
r524 | } | ||
|
r724 | /*! | ||
|
r1501 | \internal | ||
|
r950 | */ | ||
|
r524 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r950 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
|
r1963 | |||
|
r2097 | if (!d_ptr->m_backgroundVisible) | ||
return; | ||||
|
r611 | |||
|
r724 | painter->setOpacity(opacity()); | ||
|
r950 | painter->setPen(d_ptr->m_pen); | ||
painter->setBrush(d_ptr->m_brush); | ||||
|
r2097 | painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height())); | ||
|
r524 | } | ||
|
r1534 | |||
|
r724 | /*! | ||
|
r950 | Sets the \a brush of legend. Brush affects the background of legend. | ||
*/ | ||||
|
r737 | void QLegend::setBrush(const QBrush &brush) | ||
|
r540 | { | ||
|
r950 | if (d_ptr->m_brush != brush) { | ||
d_ptr->m_brush = brush; | ||||
|
r645 | update(); | ||
|
r1543 | emit colorChanged(brush.color()); | ||
|
r645 | } | ||
} | ||||
|
r724 | /*! | ||
|
r950 | Returns the brush used by legend. | ||
*/ | ||||
|
r645 | QBrush QLegend::brush() const | ||
{ | ||||
|
r950 | return d_ptr->m_brush; | ||
|
r645 | } | ||
|
r1452 | void QLegend::setColor(QColor color) | ||
{ | ||||
QBrush b = d_ptr->m_brush; | ||||
|
r1472 | if (b.style() != Qt::SolidPattern || b.color() != color) { | ||
b.setStyle(Qt::SolidPattern); | ||||
|
r1452 | b.setColor(color); | ||
setBrush(b); | ||||
} | ||||
} | ||||
QColor QLegend::color() | ||||
{ | ||||
return d_ptr->m_brush.color(); | ||||
} | ||||
|
r724 | /*! | ||
|
r950 | Sets the \a pen of legend. Pen affects the legend borders. | ||
*/ | ||||
|
r737 | void QLegend::setPen(const QPen &pen) | ||
|
r645 | { | ||
|
r950 | if (d_ptr->m_pen != pen) { | ||
d_ptr->m_pen = pen; | ||||
|
r645 | update(); | ||
|
r1543 | emit borderColorChanged(pen.color()); | ||
|
r645 | } | ||
|
r540 | } | ||
|
r724 | /*! | ||
|
r950 | Returns the pen used by legend | ||
*/ | ||||
|
r724 | |||
|
r645 | QPen QLegend::pen() const | ||
|
r540 | { | ||
|
r950 | return d_ptr->m_pen; | ||
|
r540 | } | ||
|
r1534 | void QLegend::setFont(const QFont &font) | ||
{ | ||||
if (d_ptr->m_font != font) { | ||||
d_ptr->m_font = font; | ||||
|
r2097 | foreach (LegendMarker *marker, d_ptr->markers()) | ||
|
r1534 | marker->setFont(d_ptr->m_font); | ||
|
r1536 | layout()->invalidate(); | ||
|
r1534 | emit fontChanged(font); | ||
} | ||||
} | ||||
QFont QLegend::font() const | ||||
{ | ||||
|
r2097 | return d_ptr->m_font; | ||
|
r1534 | } | ||
|
r1452 | void QLegend::setBorderColor(QColor color) | ||
{ | ||||
QPen p = d_ptr->m_pen; | ||||
if (p.color() != color) { | ||||
p.setColor(color); | ||||
setPen(p); | ||||
} | ||||
} | ||||
|
r1357 | |||
|
r1452 | QColor QLegend::borderColor() | ||
{ | ||||
return d_ptr->m_pen.color(); | ||||
} | ||||
|
r1357 | |||
|
r1529 | /*! | ||
Set brush used to draw labels to \a brush. | ||||
*/ | ||||
|
r1527 | void QLegend::setLabelBrush(const QBrush &brush) | ||
{ | ||||
if (d_ptr->m_labelBrush != brush) { | ||||
|
r1534 | d_ptr->m_labelBrush = brush; | ||
foreach (LegendMarker *marker, d_ptr->markers()) { | ||||
marker->setLabelBrush(d_ptr->m_labelBrush); | ||||
|
r1795 | // Note: The pen of the marker rectangle could be exposed in the public QLegend API | ||
// instead of mapping it from label brush color | ||||
marker->setPen(brush.color()); | ||||
|
r1534 | } | ||
|
r1543 | emit labelColorChanged(brush.color()); | ||
|
r1527 | } | ||
} | ||||
|
r1529 | /*! | ||
Brush used to draw labels. | ||||
*/ | ||||
|
r1527 | QBrush QLegend::labelBrush() const | ||
{ | ||||
return d_ptr->m_labelBrush; | ||||
} | ||||
void QLegend::setLabelColor(QColor color) | ||||
{ | ||||
QBrush b = d_ptr->m_labelBrush; | ||||
if (b.style() != Qt::SolidPattern || b.color() != color) { | ||||
b.setStyle(Qt::SolidPattern); | ||||
b.setColor(color); | ||||
setLabelBrush(b); | ||||
} | ||||
} | ||||
QColor QLegend::labelColor() const | ||||
{ | ||||
return d_ptr->m_labelBrush.color(); | ||||
} | ||||
|
r1534 | |||
|
r1357 | void QLegend::setAlignment(Qt::Alignment alignment) | ||
|
r616 | { | ||
|
r2097 | if (d_ptr->m_alignment != alignment) { | ||
|
r950 | d_ptr->m_alignment = alignment; | ||
|
r2088 | layout()->invalidate(); | ||
|
r855 | } | ||
|
r716 | } | ||
|
r1357 | Qt::Alignment QLegend::alignment() const | ||
|
r716 | { | ||
|
r950 | return d_ptr->m_alignment; | ||
|
r616 | } | ||
|
r724 | /*! | ||
|
r950 | Detaches the legend from chart. Chart won't change layout of the legend. | ||
*/ | ||||
void QLegend::detachFromChart() | ||||
|
r524 | { | ||
|
r950 | d_ptr->m_attachedToChart = false; | ||
|
r2088 | layout()->invalidate(); | ||
|
r1534 | setParent(0); | ||
|
r586 | } | ||
|
r724 | /*! | ||
|
r950 | Attaches the legend to chart. Chart may change layout of the legend. | ||
*/ | ||||
void QLegend::attachToChart() | ||||
|
r586 | { | ||
|
r1534 | d_ptr->m_attachedToChart = true; | ||
|
r2088 | layout()->invalidate(); | ||
|
r1534 | setParent(d_ptr->m_chart); | ||
|
r586 | } | ||
|
r724 | /*! | ||
|
r950 | Returns true, if legend is attached to chart. | ||
*/ | ||||
bool QLegend::isAttachedToChart() | ||||
|
r637 | { | ||
|
r950 | return d_ptr->m_attachedToChart; | ||
|
r637 | } | ||
|
r799 | /*! | ||
|
r950 | Sets the visibility of legend background to \a visible | ||
*/ | ||||
void QLegend::setBackgroundVisible(bool visible) | ||||
|
r799 | { | ||
|
r2097 | if (d_ptr->m_backgroundVisible != visible) { | ||
|
r1452 | d_ptr->m_backgroundVisible = visible; | ||
|
r950 | update(); | ||
|
r1452 | emit backgroundVisibleChanged(visible); | ||
|
r950 | } | ||
|
r799 | } | ||
/*! | ||||
|
r950 | Returns the visibility of legend background | ||
*/ | ||||
bool QLegend::isBackgroundVisible() const | ||||
|
r799 | { | ||
|
r950 | return d_ptr->m_backgroundVisible; | ||
|
r799 | } | ||
|
r2160 | |||
QList<QLegendMarker*> QLegend::markers() const | ||||
{ | ||||
// TODO: name of PIMPL method will change. | ||||
return d_ptr->legendMarkers(); | ||||
} | ||||
|
r724 | /*! | ||
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::hideEvent(QHideEvent *event) | ||||
|
r529 | { | ||
|
r2097 | if (isAttachedToChart()) | ||
d_ptr->m_presenter->layout()->invalidate(); | ||||
QGraphicsWidget::hideEvent(event); | ||||
|
r565 | } | ||
|
r724 | /*! | ||
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::showEvent(QShowEvent *event) | ||||
|
r565 | { | ||
|
r2097 | if (isAttachedToChart()) { | ||
d_ptr->items()->setVisible(false); | ||||
layout()->invalidate(); | ||||
} | ||||
QGraphicsWidget::showEvent(event); | ||||
//layout activation will show the items | ||||
|
r950 | } | ||
|
r855 | |||
|
r950 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r2104 | QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q) | ||
|
r2097 | : q_ptr(q), | ||
m_presenter(presenter), | ||||
m_layout(new LegendLayout(q)), | ||||
m_chart(chart), | ||||
m_items(new QGraphicsItemGroup(q)), | ||||
m_alignment(Qt::AlignTop), | ||||
m_brush(QBrush()), | ||||
m_pen(QPen()), | ||||
m_labelBrush(QBrush()), | ||||
m_diameter(5), | ||||
m_attachedToChart(true), | ||||
m_backgroundVisible(false) | ||||
|
r950 | { | ||
|
r855 | |||
|
r586 | } | ||
|
r950 | QLegendPrivate::~QLegendPrivate() | ||
{ | ||||
|
r529 | |||
|
r950 | } | ||
void QLegendPrivate::setOffset(qreal x, qreal y) | ||||
|
r855 | { | ||
|
r2097 | m_layout->setOffset(x, y); | ||
|
r855 | } | ||
|
r716 | |||
|
r1458 | QPointF QLegendPrivate::offset() const | ||
{ | ||||
|
r1534 | return m_layout->offset(); | ||
|
r1300 | } | ||
|
r1453 | int QLegendPrivate::roundness(qreal size) | ||
{ | ||||
|
r2097 | return 100 * m_diameter / int(size); | ||
|
r1453 | } | ||
|
r988 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) | ||
|
r855 | { | ||
|
r950 | Q_UNUSED(domain) | ||
|
r716 | |||
|
r2164 | qDebug() << "QLegendPrivate::handleSeriesAdded"; | ||
|
r2163 | // New markers ---> | ||
QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr); | ||||
foreach (QLegendMarker* marker, newMarkers) { | ||||
marker->setFont(m_font); | ||||
marker->setLabelBrush(m_labelBrush); | ||||
marker->setVisible(series->isVisible()); | ||||
// TODO: QLegendMarker has QGraphicsItem vs QLegendMarker is QGraphicsItem | ||||
|
r2164 | // TODO: possible hazard. What if marker is deleted and group still has pointer? | ||
// m_items->addToGroup(marker->d_ptr.data()); | ||||
|
r2163 | m_legendMarkers << marker; | ||
} | ||||
QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||||
// <--- New markers | ||||
QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); | ||||
|
r1534 | |||
|
r2097 | foreach (LegendMarker *marker, markers) { | ||
|
r1522 | marker->setFont(m_font); | ||
|
r1527 | marker->setLabelBrush(m_labelBrush); | ||
|
r1645 | marker->setVisible(series->isVisible()); | ||
|
r1534 | m_items->addToGroup(marker); | ||
|
r2097 | m_markers << marker; | ||
|
r1522 | } | ||
|
r716 | |||
|
r1459 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||
|
r2085 | QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged())); | ||
|
r956 | |||
|
r2085 | m_items->setVisible(false); | ||
|
r2088 | m_layout->invalidate(); | ||
|
r716 | } | ||
|
r988 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) | ||
|
r716 | { | ||
|
r2163 | // New markers ---> | ||
foreach (QLegendMarker *marker, m_legendMarkers) { | ||||
if (marker->series() == series) { | ||||
delete marker; | ||||
m_legendMarkers.removeAll(marker); | ||||
} | ||||
} | ||||
QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||||
// <--- New markers | ||||
|
r1534 | foreach (LegendMarker *marker, m_markers) { | ||
|
r956 | if (marker->series() == series) { | ||
delete marker; | ||||
|
r1534 | m_markers.removeAll(marker); | ||
|
r956 | } | ||
} | ||||
|
r2081 | QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||
|
r2085 | QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged())); | ||
|
r2088 | m_layout->invalidate(); | ||
|
r855 | } | ||
|
r724 | |||
|
r1459 | void QLegendPrivate::handleSeriesVisibleChanged() | ||
{ | ||||
|
r2104 | QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender()); | ||
|
r2085 | Q_ASSERT(series); | ||
|
r1459 | |||
|
r2163 | // New markers ---> | ||
foreach (QLegendMarker* marker, m_legendMarkers) { | ||||
if (marker->series() == series) { | ||||
marker->setVisible(series->isVisible()); | ||||
} | ||||
} | ||||
// <--- New markers | ||||
foreach (LegendMarker* marker, m_markers) { | ||||
if (marker->series() == series) { | ||||
|
r1645 | marker->setVisible(series->isVisible()); | ||
|
r1459 | } | ||
|
r2088 | m_layout->invalidate(); | ||
|
r1459 | } | ||
|
r2085 | void QLegendPrivate::handleCountChanged() | ||
|
r2081 | { | ||
|
r2163 | // With new markers, the series shoud notify markers directly? | ||
// Handle new or removed markers | ||||
// Handle changes of marker pen/brush/label. every property that legend is interested | ||||
handleSeriesRemoved(series); | ||||
Domain domain; | ||||
handleSeriesAdded(series, &domain); | ||||
|
r2081 | } | ||
|
r524 | #include "moc_qlegend.cpp" | ||
|
r950 | #include "moc_qlegend_p.cpp" | ||
|
r737 | |||
|
r524 | QTCOMMERCIALCHART_END_NAMESPACE | ||