qlegend.cpp
710 lines
| 19.0 KiB
| text/x-c
|
CppLexer
Miikka Heikkinen
|
r2854 | /**************************************************************************** | ||
Titta Heikkala
|
r2845 | ** | ||
Miikka Heikkinen
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
Titta Heikkala
|
r2845 | ** | ||
Miikka Heikkinen
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
Titta Heikkala
|
r2845 | ** | ||
Miikka Heikkinen
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
Titta Heikkala
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
Miikka Heikkinen
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
Titta Heikkala
|
r2845 | ** | ||
** $QT_END_LICENSE$ | ||||
** | ||||
Miikka Heikkinen
|
r2854 | ****************************************************************************/ | ||
Jani Honkonen
|
r794 | |||
Titta Heikkala
|
r2714 | #include <QtCharts/QLegend> | ||
#include <private/qlegend_p.h> | ||||
#include <QtCharts/QAbstractSeries> | ||||
#include <private/qabstractseries_p.h> | ||||
#include <private/qchart_p.h> | ||||
#include <private/legendlayout_p.h> | ||||
#include <private/chartpresenter_p.h> | ||||
#include <private/abstractchartlayout_p.h> | ||||
#include <QtCharts/QLegendMarker> | ||||
#include <private/qlegendmarker_p.h> | ||||
#include <private/legendmarkeritem_p.h> | ||||
#include <private/chartdataset_p.h> | ||||
#include <QtGui/QPainter> | ||||
#include <QtGui/QPen> | ||||
#include <QtWidgets/QGraphicsItemGroup> | ||||
sauimone
|
r2163 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
sauimone
|
r529 | |||
sauimone
|
r728 | /*! | ||
Tero Ahola
|
r995 | \class QLegend | ||
Titta Heikkala
|
r2639 | \inmodule Qt Charts | ||
Caroline Chao
|
r2770 | \inherits QGraphicsWidget | ||
Miikka Heikkinen
|
r2520 | \brief Legend object. | ||
sauimone
|
r728 | |||
Miikka Heikkinen
|
r2494 | QLegend is a graphical object for displaying the legend of the chart. Legend state is updated by QChart, when | ||
Tero Ahola
|
r995 | 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. | ||||
sauimone
|
r728 | |||
Tero Ahola
|
r995 | \image examples_percentbarchart_legend.png | ||
sauimone
|
r728 | |||
Tero Ahola
|
r995 | \sa QChart | ||
*/ | ||||
Tero Ahola
|
r1452 | /*! | ||
Titta Heikkala
|
r2639 | \qmltype Legend | ||
\instantiates QLegend | ||||
Titta Heikkala
|
r2712 | \inqmlmodule QtCharts | ||
Tero Ahola
|
r1452 | |||
Titta Heikkala
|
r2712 | \brief Legend is part of Qt Chart QML API. | ||
Caroline Chao
|
r2770 | Legend is a graphical object, which displays legend of the chart. Legend state is updated by | ||
Titta Heikkala
|
r2712 | ChartView, when series have been changed. Legend is used via ChartView class. For example: | ||
\code | ||||
ChartView { | ||||
legend.visible: true | ||||
legend.alignment: Qt.AlignBottom | ||||
// Add a few series... | ||||
} | ||||
\endcode | ||||
\image examples_percentbarchart_legend.png | ||||
Tero Ahola
|
r2240 | |||
Titta Heikkala
|
r2712 | Please note that there is no QML API available for modifying legend markers, unlike in the Qt | ||
API of Charts. The use case of modifying markers can be implemented for example by creating your | ||||
own custom legend. For an example on how to do this, | ||||
see \l {qmlcustomlegend}{Qml Custom Example} application. | ||||
Tero Ahola
|
r1452 | */ | ||
/*! | ||||
\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. | ||||
*/ | ||||
Caroline Chao
|
r2770 | /*! | ||
\qmlproperty bool Legend::visible | ||||
\brief Whether the legend is visible or not. | ||||
By default, this property is \c true. | ||||
\sa QGraphicsObject::visible | ||||
*/ | ||||
Tero Ahola
|
r1452 | /*! | ||
\property QLegend::color | ||||
Tero Ahola
|
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. | ||||
Tero Ahola
|
r1452 | */ | ||
/*! | ||||
\qmlproperty color Legend::color | ||||
Tero Ahola
|
r1472 | The color of the legend, i.e. the background (brush) color. | ||
Tero Ahola
|
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. | ||||
*/ | ||||
sauimone
|
r728 | |||
sauimone
|
r1522 | /*! | ||
\property QLegend::font | ||||
Titta Heikkala
|
r2686 | The font of markers used by legend. | ||
sauimone
|
r1522 | */ | ||
/*! | ||||
Tero Ahola
|
r2113 | \qmlproperty Font Legend::font | ||
Titta Heikkala
|
r2686 | The font of markers used by legend. | ||
sauimone
|
r1522 | */ | ||
Tero Ahola
|
r1529 | /*! | ||
\property QLegend::labelColor | ||||
The color of brush used to draw labels. | ||||
*/ | ||||
/*! | ||||
Titta Heikkala
|
r2686 | \qmlproperty color Legend::labelColor | ||
Tero Ahola
|
r1529 | The color of brush used to draw labels. | ||
*/ | ||||
Titta Heikkala
|
r2676 | /*! | ||
\property QLegend::reverseMarkers | ||||
Whether reverse order is used for the markers in legend or not. False by default. | ||||
*/ | ||||
/*! | ||||
\qmlproperty bool Legend::reverseMarkers | ||||
Whether reverse order is used for the markers in legend or not. False by default. | ||||
*/ | ||||
Andy Shaw
|
r2864 | /*! | ||
\property QLegend::showToolTips | ||||
Whether tooltips are shown when the text is truncated. This is false by default. | ||||
This will not have any effect when used in QML. | ||||
*/ | ||||
/*! | ||||
\qmlproperty bool Legend::showToolTips | ||||
Whether tooltips are shown when the text is truncated. This is false by default. | ||||
This currently has no effect as there is no support for tooltips in QML. | ||||
*/ | ||||
Tero Ahola
|
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. | ||||
Tero Ahola
|
r1357 | */ | ||
sauimone
|
r724 | |||
sauimone
|
r1522 | /*! | ||
\fn void QLegend::fontChanged(QFont) | ||||
The font of markers of the legend changed to \a font. | ||||
*/ | ||||
Tero Ahola
|
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. | ||||
*/ | ||||
Titta Heikkala
|
r2686 | /*! | ||
\fn void QLegend::reverseMarkersChanged(bool) | ||||
The use of reverse order for the markers in legend is changed to \a reverseMarkers. | ||||
*/ | ||||
Andy Shaw
|
r2864 | /*! | ||
\fn void QLegend::showToolTipsChanged(bool showToolTips) | ||||
This signal is emitted when the visibility of tooltips is changed to \a showToolTips. | ||||
*/ | ||||
Jani Honkonen
|
r2097 | QLegend::QLegend(QChart *chart): QGraphicsWidget(chart), | ||
d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this)) | ||||
sauimone
|
r762 | { | ||
sauimone
|
r626 | setZValue(ChartPresenter::LegendZValue); | ||
Michal Klocek
|
r855 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
Michal Klocek
|
r2273 | QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*))); | ||
Jani Honkonen
|
r2131 | QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*))); | ||
Michal Klocek
|
r1534 | setLayout(d_ptr->m_layout); | ||
Michal Klocek
|
r950 | } | ||
Tero Ahola
|
r1002 | /*! | ||
Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. | ||||
*/ | ||||
Michal Klocek
|
r950 | QLegend::~QLegend() | ||
{ | ||||
sauimone
|
r524 | } | ||
sauimone
|
r724 | /*! | ||
sauimone
|
r1501 | \internal | ||
Michal Klocek
|
r950 | */ | ||
sauimone
|
r524 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Michal Klocek
|
r950 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
Michal Klocek
|
r1963 | |||
Jani Honkonen
|
r2097 | if (!d_ptr->m_backgroundVisible) | ||
return; | ||||
Tero Ahola
|
r611 | |||
sauimone
|
r724 | painter->setOpacity(opacity()); | ||
Michal Klocek
|
r950 | painter->setPen(d_ptr->m_pen); | ||
painter->setBrush(d_ptr->m_brush); | ||||
Jani Honkonen
|
r2097 | painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height())); | ||
sauimone
|
r524 | } | ||
Michal Klocek
|
r1534 | |||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | Sets the \a brush of legend. Brush affects the background of legend. | ||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::setBrush(const QBrush &brush) | ||
sauimone
|
r540 | { | ||
Michal Klocek
|
r950 | if (d_ptr->m_brush != brush) { | ||
d_ptr->m_brush = brush; | ||||
Michal Klocek
|
r645 | update(); | ||
Tero Ahola
|
r1543 | emit colorChanged(brush.color()); | ||
Michal Klocek
|
r645 | } | ||
} | ||||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | Returns the brush used by legend. | ||
*/ | ||||
Michal Klocek
|
r645 | QBrush QLegend::brush() const | ||
{ | ||||
Titta Heikkala
|
r2852 | if (d_ptr->m_brush == QChartPrivate::defaultBrush()) | ||
return QBrush(); | ||||
else | ||||
return d_ptr->m_brush; | ||||
Michal Klocek
|
r645 | } | ||
Tero Ahola
|
r1452 | void QLegend::setColor(QColor color) | ||
{ | ||||
Titta Heikkala
|
r2852 | QBrush b = brush(); | ||
Tero Ahola
|
r1472 | if (b.style() != Qt::SolidPattern || b.color() != color) { | ||
b.setStyle(Qt::SolidPattern); | ||||
Tero Ahola
|
r1452 | b.setColor(color); | ||
setBrush(b); | ||||
} | ||||
} | ||||
QColor QLegend::color() | ||||
{ | ||||
return d_ptr->m_brush.color(); | ||||
} | ||||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | Sets the \a pen of legend. Pen affects the legend borders. | ||
*/ | ||||
Tero Ahola
|
r737 | void QLegend::setPen(const QPen &pen) | ||
Michal Klocek
|
r645 | { | ||
Michal Klocek
|
r950 | if (d_ptr->m_pen != pen) { | ||
d_ptr->m_pen = pen; | ||||
Michal Klocek
|
r645 | update(); | ||
Tero Ahola
|
r1543 | emit borderColorChanged(pen.color()); | ||
Michal Klocek
|
r645 | } | ||
sauimone
|
r540 | } | ||
sauimone
|
r724 | /*! | ||
Titta Heikkala
|
r2686 | Returns the pen used by legend. | ||
Michal Klocek
|
r950 | */ | ||
sauimone
|
r724 | |||
Michal Klocek
|
r645 | QPen QLegend::pen() const | ||
sauimone
|
r540 | { | ||
Titta Heikkala
|
r2852 | if (d_ptr->m_pen == QChartPrivate::defaultPen()) | ||
return QPen(); | ||||
else | ||||
return d_ptr->m_pen; | ||||
sauimone
|
r540 | } | ||
Michal Klocek
|
r1534 | void QLegend::setFont(const QFont &font) | ||
{ | ||||
Michal Klocek
|
r2273 | if (d_ptr->m_font != font) { | ||
Titta Heikkala
|
r2652 | // Hide items to avoid flickering | ||
d_ptr->items()->setVisible(false); | ||||
Michal Klocek
|
r1534 | d_ptr->m_font = font; | ||
Michal Klocek
|
r2273 | foreach (QLegendMarker *marker, d_ptr->markers()) { | ||
marker->setFont(d_ptr->m_font); | ||||
} | ||||
layout()->invalidate(); | ||||
emit fontChanged(font); | ||||
Michal Klocek
|
r1534 | } | ||
} | ||||
QFont QLegend::font() const | ||||
{ | ||||
Jani Honkonen
|
r2097 | return d_ptr->m_font; | ||
Michal Klocek
|
r1534 | } | ||
Tero Ahola
|
r1452 | void QLegend::setBorderColor(QColor color) | ||
{ | ||||
Titta Heikkala
|
r2852 | QPen p = pen(); | ||
Tero Ahola
|
r1452 | if (p.color() != color) { | ||
p.setColor(color); | ||||
setPen(p); | ||||
} | ||||
} | ||||
Tero Ahola
|
r1357 | |||
Tero Ahola
|
r1452 | QColor QLegend::borderColor() | ||
{ | ||||
return d_ptr->m_pen.color(); | ||||
} | ||||
Tero Ahola
|
r1357 | |||
Tero Ahola
|
r1529 | /*! | ||
Set brush used to draw labels to \a brush. | ||||
*/ | ||||
sauimone
|
r1527 | void QLegend::setLabelBrush(const QBrush &brush) | ||
{ | ||||
if (d_ptr->m_labelBrush != brush) { | ||||
Michal Klocek
|
r1534 | d_ptr->m_labelBrush = brush; | ||
sauimone
|
r2185 | foreach (QLegendMarker *marker, d_ptr->markers()) { | ||
Michal Klocek
|
r1534 | marker->setLabelBrush(d_ptr->m_labelBrush); | ||
Tero Ahola
|
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()); | ||||
Michal Klocek
|
r1534 | } | ||
Tero Ahola
|
r1543 | emit labelColorChanged(brush.color()); | ||
sauimone
|
r1527 | } | ||
} | ||||
Tero Ahola
|
r1529 | /*! | ||
Brush used to draw labels. | ||||
*/ | ||||
sauimone
|
r1527 | QBrush QLegend::labelBrush() const | ||
{ | ||||
Titta Heikkala
|
r2852 | if (d_ptr->m_labelBrush == QChartPrivate::defaultBrush()) | ||
return QBrush(); | ||||
else | ||||
return d_ptr->m_labelBrush; | ||||
sauimone
|
r1527 | } | ||
void QLegend::setLabelColor(QColor color) | ||||
{ | ||||
Titta Heikkala
|
r2852 | QBrush b = labelBrush(); | ||
sauimone
|
r1527 | 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(); | ||||
} | ||||
Michal Klocek
|
r1534 | |||
Tero Ahola
|
r1357 | void QLegend::setAlignment(Qt::Alignment alignment) | ||
sauimone
|
r616 | { | ||
Jani Honkonen
|
r2097 | if (d_ptr->m_alignment != alignment) { | ||
Michal Klocek
|
r950 | d_ptr->m_alignment = alignment; | ||
Michal Klocek
|
r2088 | layout()->invalidate(); | ||
Michal Klocek
|
r855 | } | ||
sauimone
|
r716 | } | ||
Tero Ahola
|
r1357 | Qt::Alignment QLegend::alignment() const | ||
sauimone
|
r716 | { | ||
Michal Klocek
|
r950 | return d_ptr->m_alignment; | ||
sauimone
|
r616 | } | ||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | Detaches the legend from chart. Chart won't change layout of the legend. | ||
*/ | ||||
void QLegend::detachFromChart() | ||||
sauimone
|
r524 | { | ||
Michal Klocek
|
r950 | d_ptr->m_attachedToChart = false; | ||
sauimone
|
r2392 | // layout()->invalidate(); | ||
d_ptr->m_chart->layout()->invalidate(); | ||||
Michal Klocek
|
r1534 | setParent(0); | ||
sauimone
|
r586 | } | ||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | Attaches the legend to chart. Chart may change layout of the legend. | ||
*/ | ||||
void QLegend::attachToChart() | ||||
sauimone
|
r586 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_attachedToChart = true; | ||
sauimone
|
r2392 | // layout()->invalidate(); | ||
d_ptr->m_chart->layout()->invalidate(); | ||||
Michal Klocek
|
r1534 | setParent(d_ptr->m_chart); | ||
sauimone
|
r586 | } | ||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | Returns true, if legend is attached to chart. | ||
*/ | ||||
bool QLegend::isAttachedToChart() | ||||
sauimone
|
r637 | { | ||
Michal Klocek
|
r950 | return d_ptr->m_attachedToChart; | ||
sauimone
|
r637 | } | ||
sauimone
|
r799 | /*! | ||
Michal Klocek
|
r950 | Sets the visibility of legend background to \a visible | ||
*/ | ||||
void QLegend::setBackgroundVisible(bool visible) | ||||
sauimone
|
r799 | { | ||
Jani Honkonen
|
r2097 | if (d_ptr->m_backgroundVisible != visible) { | ||
Tero Ahola
|
r1452 | d_ptr->m_backgroundVisible = visible; | ||
Michal Klocek
|
r950 | update(); | ||
Tero Ahola
|
r1452 | emit backgroundVisibleChanged(visible); | ||
Michal Klocek
|
r950 | } | ||
sauimone
|
r799 | } | ||
/*! | ||||
Michal Klocek
|
r950 | Returns the visibility of legend background | ||
*/ | ||||
bool QLegend::isBackgroundVisible() const | ||||
sauimone
|
r799 | { | ||
Michal Klocek
|
r950 | return d_ptr->m_backgroundVisible; | ||
sauimone
|
r799 | } | ||
sauimone
|
r2216 | /*! | ||
Returns the list of markers in legend. The list can be filtered with \a series parameter. | ||||
If \a series is given, only markers related to that series are returned. | ||||
*/ | ||||
sauimone
|
r2193 | QList<QLegendMarker*> QLegend::markers(QAbstractSeries *series) const | ||
sauimone
|
r2160 | { | ||
sauimone
|
r2193 | return d_ptr->markers(series); | ||
sauimone
|
r2160 | } | ||
Titta Heikkala
|
r2676 | bool QLegend::reverseMarkers() | ||
{ | ||||
return d_ptr->m_reverseMarkers; | ||||
} | ||||
void QLegend::setReverseMarkers(bool reverseMarkers) | ||||
{ | ||||
if (d_ptr->m_reverseMarkers != reverseMarkers) { | ||||
d_ptr->m_reverseMarkers = reverseMarkers; | ||||
layout()->invalidate(); | ||||
emit reverseMarkersChanged(reverseMarkers); | ||||
} | ||||
} | ||||
Andy Shaw
|
r2864 | /*! | ||
Returns whether the tooltips are shown or not for the legend labels | ||||
when they are elided. | ||||
*/ | ||||
bool QLegend::showToolTips() const | ||||
{ | ||||
return d_ptr->m_showToolTips; | ||||
} | ||||
/*! | ||||
When \a show is true, the legend labels will show a tooltip when | ||||
the mouse hovers over them if the label itself is shown elided. | ||||
This is false by default. | ||||
*/ | ||||
void QLegend::setShowToolTips(bool show) | ||||
{ | ||||
if (d_ptr->m_showToolTips != show) { | ||||
d_ptr->m_showToolTips = show; | ||||
d_ptr->updateToolTips(); | ||||
emit showToolTipsChanged(show); | ||||
} | ||||
} | ||||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::hideEvent(QHideEvent *event) | ||||
sauimone
|
r529 | { | ||
Jani Honkonen
|
r2097 | if (isAttachedToChart()) | ||
d_ptr->m_presenter->layout()->invalidate(); | ||||
QGraphicsWidget::hideEvent(event); | ||||
sauimone
|
r565 | } | ||
sauimone
|
r724 | /*! | ||
Michal Klocek
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::showEvent(QShowEvent *event) | ||||
sauimone
|
r565 | { | ||
Titta Heikkala
|
r2602 | if (isAttachedToChart()) | ||
Jani Honkonen
|
r2097 | layout()->invalidate(); | ||
QGraphicsWidget::showEvent(event); | ||||
//layout activation will show the items | ||||
Michal Klocek
|
r950 | } | ||
Michal Klocek
|
r855 | |||
Michal Klocek
|
r950 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
Jani Honkonen
|
r2104 | QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q) | ||
Jani Honkonen
|
r2097 | : q_ptr(q), | ||
m_presenter(presenter), | ||||
m_layout(new LegendLayout(q)), | ||||
m_chart(chart), | ||||
m_items(new QGraphicsItemGroup(q)), | ||||
m_alignment(Qt::AlignTop), | ||||
Titta Heikkala
|
r2852 | m_brush(QChartPrivate::defaultBrush()), | ||
m_pen(QChartPrivate::defaultPen()), | ||||
m_labelBrush(QChartPrivate::defaultBrush()), | ||||
Jani Honkonen
|
r2097 | m_diameter(5), | ||
m_attachedToChart(true), | ||||
Titta Heikkala
|
r2676 | m_backgroundVisible(false), | ||
Andy Shaw
|
r2864 | m_reverseMarkers(false), | ||
m_showToolTips(false) | ||||
Michal Klocek
|
r950 | { | ||
sauimone
|
r2169 | m_items->setHandlesChildEvents(false); | ||
sauimone
|
r586 | } | ||
Michal Klocek
|
r950 | QLegendPrivate::~QLegendPrivate() | ||
{ | ||||
sauimone
|
r529 | |||
Michal Klocek
|
r950 | } | ||
sauimone
|
r2189 | void QLegendPrivate::setOffset(const QPointF &offset) | ||
Michal Klocek
|
r855 | { | ||
sauimone
|
r2189 | m_layout->setOffset(offset.x(), offset.y()); | ||
Michal Klocek
|
r855 | } | ||
sauimone
|
r716 | |||
sauimone
|
r1458 | QPointF QLegendPrivate::offset() const | ||
{ | ||||
Michal Klocek
|
r1534 | return m_layout->offset(); | ||
sauimone
|
r1300 | } | ||
sauimone
|
r1453 | int QLegendPrivate::roundness(qreal size) | ||
{ | ||||
Jani Honkonen
|
r2097 | return 100 * m_diameter / int(size); | ||
sauimone
|
r1453 | } | ||
sauimone
|
r2193 | QList<QLegendMarker*> QLegendPrivate::markers(QAbstractSeries *series) | ||
{ | ||||
// Return all markers | ||||
if (!series) { | ||||
return m_markers; | ||||
} | ||||
// Create filtered list | ||||
QList<QLegendMarker *> markers; | ||||
foreach (QLegendMarker *marker, m_markers) { | ||||
if (marker->series() == series) { | ||||
markers.append(marker); | ||||
} | ||||
} | ||||
return markers; | ||||
} | ||||
sauimone
|
r2209 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series) | ||
sauimone
|
r2167 | { | ||
if (m_series.contains(series)) { | ||||
sauimone
|
r2173 | return; | ||
sauimone
|
r2167 | } | ||
sauimone
|
r2164 | |||
sauimone
|
r2163 | QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr); | ||
sauimone
|
r2182 | decorateMarkers(newMarkers); | ||
addMarkers(newMarkers); | ||||
sauimone
|
r2163 | |||
sauimone
|
r2192 | QObject::connect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged())); | ||
sauimone
|
r2163 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||
Michal Klocek
|
r956 | |||
sauimone
|
r2173 | m_series.append(series); | ||
Michal Klocek
|
r2085 | m_items->setVisible(false); | ||
Michal Klocek
|
r2088 | m_layout->invalidate(); | ||
sauimone
|
r716 | } | ||
sauimone
|
r2209 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) | ||
sauimone
|
r716 | { | ||
sauimone
|
r2173 | if (m_series.contains(series)) { | ||
m_series.removeOne(series); | ||||
} | ||||
sauimone
|
r2171 | |||
sauimone
|
r2182 | // Find out, which markers to remove | ||
QList<QLegendMarker *> removed; | ||||
sauimone
|
r2185 | foreach (QLegendMarker *m, m_markers) { | ||
sauimone
|
r2182 | if (m->series() == series) { | ||
removed << m; | ||||
sauimone
|
r2163 | } | ||
} | ||||
sauimone
|
r2182 | removeMarkers(removed); | ||
sauimone
|
r2163 | |||
sauimone
|
r2192 | QObject::disconnect(series->d_ptr.data(), SIGNAL(countChanged()), this, SLOT(handleCountChanged())); | ||
sauimone
|
r2163 | QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||
Michal Klocek
|
r2088 | m_layout->invalidate(); | ||
sauimone
|
r2173 | } | ||
sauimone
|
r1459 | void QLegendPrivate::handleSeriesVisibleChanged() | ||
{ | ||||
Jani Honkonen
|
r2104 | QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender()); | ||
Michal Klocek
|
r2085 | Q_ASSERT(series); | ||
sauimone
|
r1459 | |||
sauimone
|
r2193 | foreach (QLegendMarker *marker, m_markers) { | ||
sauimone
|
r2163 | if (marker->series() == series) { | ||
marker->setVisible(series->isVisible()); | ||||
} | ||||
} | ||||
Titta Heikkala
|
r2605 | |||
if (m_chart->isVisible()) | ||||
m_layout->invalidate(); | ||||
sauimone
|
r1459 | } | ||
Michal Klocek
|
r2085 | void QLegendPrivate::handleCountChanged() | ||
sauimone
|
r2081 | { | ||
sauimone
|
r2182 | // Here we handle the changes in marker count. | ||
// Can happen for example when pieslice(s) have been added to or removed from pieseries. | ||||
sauimone
|
r2173 | |||
sauimone
|
r2192 | QAbstractSeriesPrivate *series = qobject_cast<QAbstractSeriesPrivate *> (sender()); | ||
QList<QLegendMarker *> createdMarkers = series->createLegendMarkers(q_ptr); | ||||
sauimone
|
r2182 | |||
// Find out removed markers and created markers | ||||
QList<QLegendMarker *> removedMarkers; | ||||
sauimone
|
r2185 | foreach (QLegendMarker *oldMarker, m_markers) { | ||
sauimone
|
r2182 | // we have marker, which is related to sender. | ||
sauimone
|
r2192 | if (oldMarker->series() == series->q_ptr) { | ||
sauimone
|
r2182 | bool found = false; | ||
foreach(QLegendMarker *newMarker, createdMarkers) { | ||||
// New marker considered existing if: | ||||
// - d_ptr->relatedObject() is same for both markers. | ||||
if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) { | ||||
// Delete the new marker, since we already have existing marker, that might be connected on user side. | ||||
found = true; | ||||
createdMarkers.removeOne(newMarker); | ||||
delete newMarker; | ||||
} | ||||
} | ||||
if (!found) { | ||||
// No related object found for marker, add to removedMarkers list | ||||
removedMarkers << oldMarker; | ||||
} | ||||
} | ||||
} | ||||
removeMarkers(removedMarkers); | ||||
sauimone
|
r2183 | decorateMarkers(createdMarkers); | ||
sauimone
|
r2182 | addMarkers(createdMarkers); | ||
q_ptr->layout()->invalidate(); | ||||
} | ||||
void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers) | ||||
{ | ||||
sauimone
|
r2193 | foreach (QLegendMarker *marker, markers) { | ||
sauimone
|
r2182 | m_items->addToGroup(marker->d_ptr.data()->item()); | ||
sauimone
|
r2185 | m_markers << marker; | ||
sauimone
|
r2197 | m_markerHash.insert(marker->d_ptr->item(), marker); | ||
sauimone
|
r2182 | } | ||
} | ||||
void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers) | ||||
{ | ||||
foreach (QLegendMarker *marker, markers) { | ||||
marker->d_ptr->item()->setVisible(false); | ||||
m_items->removeFromGroup(marker->d_ptr->item()); | ||||
sauimone
|
r2185 | m_markers.removeOne(marker); | ||
sauimone
|
r2197 | m_markerHash.remove(marker->d_ptr->item()); | ||
delete marker; | ||||
sauimone
|
r2182 | } | ||
} | ||||
void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers) | ||||
{ | ||||
sauimone
|
r2193 | foreach (QLegendMarker *marker, markers) { | ||
sauimone
|
r2182 | marker->setFont(m_font); | ||
marker->setLabelBrush(m_labelBrush); | ||||
} | ||||
sauimone
|
r2081 | } | ||
Andy Shaw
|
r2864 | void QLegendPrivate::updateToolTips() | ||
{ | ||||
foreach (QLegendMarker *m, m_markers) { | ||||
if (m->d_ptr->m_item->displayedLabel() != m->label()) | ||||
m->d_ptr->m_item->setToolTip(m->label()); | ||||
else | ||||
m->d_ptr->m_item->setToolTip(QString()); | ||||
} | ||||
} | ||||
sauimone
|
r524 | #include "moc_qlegend.cpp" | ||
Michal Klocek
|
r950 | #include "moc_qlegend_p.cpp" | ||
Tero Ahola
|
r737 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_END_NAMESPACE | ||