qlegend.cpp
533 lines
| 13.3 KiB
| text/x-c
|
CppLexer
/ src / qlegend.cpp
|
r794 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r524 | #include "qlegend.h" | ||
|
r790 | #include "qchart_p.h" | ||
|
r524 | #include "qseries.h" | ||
|
r547 | #include "legendmarker_p.h" | ||
|
r565 | #include "qxyseries.h" | ||
#include "qlineseries.h" | ||||
#include "qareaseries.h" | ||||
#include "qscatterseries.h" | ||||
#include "qsplineseries.h" | ||||
#include "qbarseries.h" | ||||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
#include "qbarset.h" | ||||
#include "qpieseries.h" | ||||
#include "qpieslice.h" | ||||
|
r626 | #include "chartpresenter_p.h" | ||
|
r529 | #include <QPainter> | ||
#include <QPen> | ||||
|
r855 | #include <QTimer> | ||
|
r524 | |||
|
r547 | #include <QGraphicsSceneEvent> | ||
|
r529 | |||
|
r547 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r529 | |||
|
r728 | /*! | ||
\class QLegend | ||||
\brief part of QtCommercial chart API. | ||||
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. | ||||
\mainclass | ||||
\sa QChart, QSeries | ||||
*/ | ||||
|
r724 | /*! | ||
|
r888 | \enum QLegend::Alignment | ||
|
r724 | |||
This enum describes the possible position for legend inside chart. | ||||
|
r888 | \value AlignmentTop | ||
\value AlignmentBottom | ||||
\value AlignmentLeft | ||||
\value AlignmentRight | ||||
|
r724 | */ | ||
/*! | ||||
|
r888 | \fn qreal QLegend::minWidth() const | ||
Returns minimum width of the legend | ||||
|
r724 | */ | ||
/*! | ||||
|
r888 | \fn qreal QLegend::minHeight() const | ||
Returns minimum height of the legend | ||||
|
r724 | */ | ||
/*! | ||||
Constructs the legend object and sets the parent to \a parent | ||||
*/ | ||||
|
r855 | |||
|
r791 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), | ||
|
r783 | m_margin(5), | ||
|
r855 | m_offsetX(0), | ||
m_offsetY(0), | ||||
|
r785 | m_brush(Qt::darkGray), // TODO: default should come from theme | ||
|
r800 | m_alignment(QLegend::AlignmentTop), | ||
|
r855 | m_markers(new QGraphicsItemGroup(this)), | ||
|
r803 | m_attachedToChart(true), | ||
|
r855 | m_chart(chart), | ||
m_minWidth(0), | ||||
m_minHeight(0), | ||||
m_width(0), | ||||
m_height(0), | ||||
|
r882 | m_backgroundVisible(false) | ||
|
r762 | { | ||
|
r626 | setZValue(ChartPresenter::LegendZValue); | ||
|
r855 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
|
r882 | setVisible(false); // By default legend is invisible | ||
|
r524 | } | ||
|
r724 | /*! | ||
Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. | ||||
*/ | ||||
|
r855 | |||
|
r524 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r855 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
|
r882 | if(!m_backgroundVisible) return; | ||
|
r611 | |||
|
r724 | painter->setOpacity(opacity()); | ||
|
r645 | painter->setPen(m_pen); | ||
painter->setBrush(m_brush); | ||||
|
r855 | painter->drawRect(boundingRect()); | ||
|
r524 | } | ||
|
r724 | /*! | ||
Bounding rect of legend. | ||||
*/ | ||||
|
r855 | |||
|
r524 | QRectF QLegend::boundingRect() const | ||
{ | ||||
|
r855 | return m_rect; | ||
|
r524 | } | ||
|
r724 | /*! | ||
Sets the \a brush of legend. Brush affects the background of legend. | ||||
*/ | ||||
|
r737 | void QLegend::setBrush(const QBrush &brush) | ||
|
r540 | { | ||
|
r737 | if (m_brush != brush) { | ||
|
r645 | m_brush = brush; | ||
update(); | ||||
} | ||||
} | ||||
|
r724 | /*! | ||
Returns the brush used by legend. | ||||
*/ | ||||
|
r645 | QBrush QLegend::brush() const | ||
{ | ||||
return m_brush; | ||||
} | ||||
|
r724 | /*! | ||
Sets the \a pen of legend. Pen affects the legend borders. | ||||
*/ | ||||
|
r737 | void QLegend::setPen(const QPen &pen) | ||
|
r645 | { | ||
|
r737 | if (m_pen != pen) { | ||
|
r645 | m_pen = pen; | ||
update(); | ||||
} | ||||
|
r540 | } | ||
|
r724 | /*! | ||
Returns the pen used by legend | ||||
*/ | ||||
|
r645 | QPen QLegend::pen() const | ||
|
r540 | { | ||
|
r645 | return m_pen; | ||
|
r540 | } | ||
|
r724 | /*! | ||
|
r888 | Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart. | ||
\sa QLegend::Alignment | ||||
|
r724 | */ | ||
|
r803 | void QLegend::setAlignmnent(QLegend::Alignments alignment) | ||
|
r616 | { | ||
|
r855 | if(m_alignment!=alignment && m_attachedToChart) { | ||
|
r803 | m_alignment = alignment; | ||
updateLayout(); | ||||
|
r855 | } | ||
|
r716 | } | ||
|
r724 | /*! | ||
Returns the preferred layout for legend | ||||
*/ | ||||
|
r803 | QLegend::Alignments QLegend::alignment() const | ||
|
r716 | { | ||
|
r766 | return m_alignment; | ||
|
r616 | } | ||
|
r724 | /*! | ||
\internal \a series \a domain Should be called when series is added to chart. | ||||
*/ | ||||
|
r737 | void QLegend::handleSeriesAdded(QSeries *series, Domain *domain) | ||
|
r524 | { | ||
|
r611 | Q_UNUSED(domain) | ||
|
r792 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeLine: { | ||||
QLineSeries *lineSeries = static_cast<QLineSeries *>(series); | ||||
appendMarkers(lineSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); | ||||
appendMarkers(areaSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeBar: { | ||||
QBarSeries *barSeries = static_cast<QBarSeries *>(series); | ||||
appendMarkers(barSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeStackedBar: { | ||||
QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series); | ||||
appendMarkers(stackedBarSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePercentBar: { | ||||
QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series); | ||||
appendMarkers(percentBarSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeScatter: { | ||||
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | ||||
appendMarkers(scatterSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
appendMarkers(pieSeries); | ||||
connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypeSpline: { | ||||
QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series); | ||||
appendMarkers(splineSeries); | ||||
break; | ||||
} | ||||
default: { | ||||
qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type."; | ||||
break; | ||||
} | ||||
} | ||||
|
r871 | updateLayout(); | ||
|
r586 | } | ||
|
r724 | /*! | ||
\internal \a series Should be called when series is removed from chart. | ||||
*/ | ||||
|
r737 | void QLegend::handleSeriesRemoved(QSeries *series) | ||
|
r586 | { | ||
|
r792 | switch (series->type()) | ||
{ | ||||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); | ||||
deleteMarkers(areaSeries); | ||||
break; | ||||
} | ||||
case QSeries::SeriesTypePie: { | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>))); | ||||
deleteMarkers(series); | ||||
break; | ||||
} | ||||
default: { | ||||
// All other types | ||||
|
r586 | deleteMarkers(series); | ||
|
r792 | break; | ||
} | ||||
|
r586 | } | ||
|
r716 | updateLayout(); | ||
|
r586 | } | ||
|
r724 | /*! | ||
\internal \a slices Should be called when slices are added to pie chart. | ||||
*/ | ||||
|
r737 | void QLegend::handleAdded(QList<QPieSlice *> slices) | ||
|
r637 | { | ||
|
r737 | QPieSeries* series = static_cast<QPieSeries *> (sender()); | ||
|
r855 | foreach(QPieSlice* slice, slices) { | ||
PieLegendMarker* marker = new PieLegendMarker(series,slice, this); | ||||
m_markers->addToGroup(marker); | ||||
|
r637 | } | ||
|
r716 | updateLayout(); | ||
|
r637 | } | ||
|
r724 | /*! | ||
\internal \a slices Should be called when slices are removed from pie chart. Currently unused, | ||||
because removed slices are also deleted and we listen destroyed signal | ||||
*/ | ||||
|
r643 | void QLegend::handleRemoved(QList<QPieSlice *> slices) | ||
{ | ||||
|
r647 | Q_UNUSED(slices) | ||
|
r643 | } | ||
|
r799 | /*! | ||
Detaches the legend from chart. Chart won't change layout of the legend. | ||||
*/ | ||||
void QLegend::detachFromChart() | ||||
{ | ||||
m_attachedToChart = false; | ||||
} | ||||
/*! | ||||
Attaches the legend to chart. Chart may change layout of the legend. | ||||
*/ | ||||
void QLegend::attachToChart() | ||||
{ | ||||
m_attachedToChart = true; | ||||
} | ||||
/*! | ||||
Returns true, if legend is attached to chart. | ||||
*/ | ||||
|
r855 | bool QLegend::isAttachedToChart() | ||
|
r799 | { | ||
return m_attachedToChart; | ||||
} | ||||
|
r724 | /*! | ||
|
r792 | \internal Helper function. Appends markers from \a series to legend. | ||
|
r724 | */ | ||
|
r792 | void QLegend::appendMarkers(QAreaSeries* series) | ||
|
r586 | { | ||
|
r855 | AreaLegendMarker* marker = new AreaLegendMarker(series,this); | ||
m_markers->addToGroup(marker); | ||||
|
r576 | } | ||
|
r724 | /*! | ||
\internal Helper function. Appends markers from \a series to legend. | ||||
*/ | ||||
|
r586 | void QLegend::appendMarkers(QXYSeries* series) | ||
|
r529 | { | ||
|
r855 | XYLegendMarker* marker = new XYLegendMarker(series,this); | ||
m_markers->addToGroup(marker); | ||||
|
r565 | } | ||
|
r724 | /*! | ||
|
r792 | \internal Helper function. Appends markers from \a series to legend. | ||
|
r724 | */ | ||
|
r586 | void QLegend::appendMarkers(QBarSeries *series) | ||
|
r565 | { | ||
|
r786 | foreach(QBarSet* set, series->barSets()) { | ||
|
r855 | BarLegendMarker* marker = new BarLegendMarker(series,set, this); | ||
m_markers->addToGroup(marker); | ||||
|
r529 | } | ||
|
r565 | } | ||
|
r529 | |||
|
r724 | /*! | ||
|
r792 | \internal Helper function. Appends markers from \a series to legend. | ||
|
r724 | */ | ||
|
r586 | void QLegend::appendMarkers(QPieSeries *series) | ||
|
r565 | { | ||
|
r786 | foreach(QPieSlice* slice, series->slices()) { | ||
|
r855 | PieLegendMarker* marker = new PieLegendMarker(series,slice, this); | ||
m_markers->addToGroup(marker); | ||||
|
r529 | } | ||
} | ||||
|
r724 | /*! | ||
\internal Deletes all markers that are created from \a series | ||||
*/ | ||||
|
r586 | void QLegend::deleteMarkers(QSeries *series) | ||
{ | ||||
// Search all markers that belong to given series and delete them. | ||||
|
r855 | |||
QList<QGraphicsItem *> items = m_markers->childItems(); | ||||
|
r870 | foreach (QGraphicsItem *markers, items) { | ||
LegendMarker *marker = static_cast<LegendMarker*>(markers); | ||||
|
r855 | if (marker->series() == series) { | ||
delete marker; | ||||
|
r586 | } | ||
} | ||||
} | ||||
|
r724 | /*! | ||
\internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend. | ||||
If items don't fit, sets the visibility of scroll buttons accordingly. | ||||
Causes legend to be resized. | ||||
*/ | ||||
|
r529 | |||
|
r855 | void QLegend::setOffset(const QPointF& point) | ||
{ | ||||
|
r626 | |||
|
r855 | switch(m_alignment) { | ||
|
r716 | |||
|
r855 | case AlignmentTop: | ||
case AlignmentBottom: { | ||||
if(m_width<=m_rect.width()) return; | ||||
|
r810 | |||
|
r855 | if (point.x() != m_offsetX) { | ||
m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width()); | ||||
m_markers->setPos(-m_offsetX,m_rect.top()); | ||||
|
r626 | } | ||
|
r855 | break; | ||
|
r616 | } | ||
|
r855 | case AlignmentLeft: | ||
case AlignmentRight: { | ||||
|
r716 | |||
|
r855 | if(m_height<=m_rect.height()) return; | ||
|
r716 | |||
|
r855 | if (point.y() != m_offsetY) { | ||
m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height()); | ||||
m_markers->setPos(m_rect.left(),-m_offsetY); | ||||
|
r716 | } | ||
|
r855 | break; | ||
|
r716 | } | ||
|
r810 | } | ||
|
r855 | } | ||
|
r716 | |||
|
r855 | QPointF QLegend::offset() const | ||
{ | ||||
return QPointF(m_offsetX,m_offsetY); | ||||
|
r716 | } | ||
|
r855 | // this function runs first to set min max values | ||
void QLegend::updateLayout() | ||||
|
r716 | { | ||
|
r855 | m_offsetX=0; | ||
QList<QGraphicsItem *> items = m_markers->childItems(); | ||||
if(items.isEmpty()) return; | ||||
m_minWidth=0; | ||||
m_minHeight=0; | ||||
switch(m_alignment) { | ||||
case AlignmentTop: | ||||
case AlignmentBottom: { | ||||
QPointF point = m_rect.topLeft(); | ||||
m_width = 0; | ||||
foreach (QGraphicsItem *item, items) { | ||||
item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); | ||||
const QRectF& rect = item->boundingRect(); | ||||
qreal w = rect.width(); | ||||
m_minWidth=qMax(m_minWidth,w); | ||||
m_minHeight=qMax(m_minHeight,rect.height()); | ||||
m_width+=w; | ||||
point.setX(point.x() + w); | ||||
} | ||||
if(m_width<m_rect.width()){ | ||||
m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); | ||||
}else{ | ||||
m_markers->setPos(m_rect.topLeft()); | ||||
} | ||||
m_height=m_minHeight; | ||||
} | ||||
break; | ||||
case AlignmentLeft: | ||||
case AlignmentRight:{ | ||||
QPointF point = m_rect.topLeft(); | ||||
m_height = 0; | ||||
foreach (QGraphicsItem *item, items) { | ||||
item->setPos(point); | ||||
const QRectF& rect = item->boundingRect(); | ||||
qreal h = rect.height(); | ||||
m_minWidth=qMax(m_minWidth,rect.width()); | ||||
m_minHeight=qMax(m_minHeight,h); | ||||
m_height+=h; | ||||
point.setY(point.y() + h); | ||||
} | ||||
if(m_height<m_rect.height()){ | ||||
m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); | ||||
}else{ | ||||
m_markers->setPos(m_rect.topLeft()); | ||||
} | ||||
m_width=m_minWidth; | ||||
} | ||||
break; | ||||
} | ||||
|
r716 | |||
|
r855 | m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme; | ||
|
r716 | } | ||
|
r888 | /*! | ||
Sets the visibility of legend background to \a visible | ||||
*/ | ||||
|
r855 | void QLegend::setBackgroundVisible(bool visible) | ||
{ | ||||
|
r882 | if(m_backgroundVisible!=visible) | ||
|
r855 | { | ||
|
r882 | m_backgroundVisible=visible; | ||
|
r855 | update(); | ||
|
r716 | } | ||
} | ||||
|
r888 | /*! | ||
Returns the visibility of legend background | ||||
*/ | ||||
|
r855 | bool QLegend::isBackgroundVisible() const | ||
{ | ||||
|
r882 | return m_backgroundVisible; | ||
|
r855 | } | ||
|
r716 | |||
|
r888 | /*! | ||
\internal \a event see QGraphicsWidget for details | ||||
*/ | ||||
|
r855 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) | ||
{ | ||||
const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); | ||||
QGraphicsWidget::resizeEvent(event); | ||||
if(m_rect != rect){ | ||||
m_rect = rect; | ||||
updateLayout(); | ||||
|
r716 | } | ||
|
r855 | } | ||
|
r716 | |||
|
r888 | /*! | ||
\internal \a event see QGraphicsWidget for details | ||||
*/ | ||||
|
r855 | void QLegend::hideEvent(QHideEvent *event) | ||
{ | ||||
QGraphicsWidget::hideEvent(event); | ||||
setEnabled(false); | ||||
updateLayout(); | ||||
|
r716 | } | ||
|
r888 | /*! | ||
\internal \a event see QGraphicsWidget for details | ||||
*/ | ||||
|
r855 | void QLegend::showEvent(QShowEvent *event) | ||
|
r716 | { | ||
|
r855 | QGraphicsWidget::showEvent(event); | ||
setEnabled(true); | ||||
updateLayout(); | ||||
} | ||||
|
r724 | |||
|
r524 | #include "moc_qlegend.cpp" | ||
|
r737 | |||
|
r524 | QTCOMMERCIALCHART_END_NAMESPACE | ||