qlegend.cpp
451 lines
| 10.9 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" | ||
|
r524 | #include "qseries.h" | ||
|
r950 | #include "qseries_p.h" | ||
#include "qchart_p.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 | /*! | ||
|
r950 | \class QLegend | ||
\brief part of QtCommercial chart API. | ||||
|
r728 | |||
|
r950 | 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 | |||
|
r950 | \mainclass | ||
|
r728 | |||
|
r950 | \sa QChart, QSeries | ||
*/ | ||||
|
r728 | |||
|
r724 | /*! | ||
|
r950 | \enum QLegend::Alignment | ||
|
r724 | |||
|
r950 | This enum describes the possible position for legend inside chart. | ||
|
r724 | |||
|
r950 | \value AlignmentTop | ||
\value AlignmentBottom | ||||
\value AlignmentLeft | ||||
\value AlignmentRight | ||||
*/ | ||||
|
r724 | |||
/*! | ||||
|
r950 | \fn qreal QLegend::minWidth() const | ||
Returns minimum width of the legend | ||||
*/ | ||||
|
r724 | |||
/*! | ||||
|
r950 | \fn qreal QLegend::minHeight() const | ||
Returns minimum height of the legend | ||||
*/ | ||||
|
r724 | |||
/*! | ||||
|
r950 | Constructs the legend object and sets the parent to \a parent | ||
*/ | ||||
|
r855 | |||
|
r791 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), | ||
|
r950 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this)) | ||
|
r762 | { | ||
|
r626 | setZValue(ChartPresenter::LegendZValue); | ||
|
r855 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
|
r950 | setEnabled(false); // By default legend is disabled | ||
|
r956 | setVisible(false); | ||
|
r950 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QSeries*,Domain*))); | ||
QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QSeries*))); | ||||
} | ||||
QLegend::~QLegend() | ||||
{ | ||||
|
r524 | } | ||
|
r724 | /*! | ||
|
r950 | 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) | ||
{ | ||||
|
r950 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
if(!d_ptr->m_backgroundVisible) return; | ||||
|
r611 | |||
|
r724 | painter->setOpacity(opacity()); | ||
|
r950 | painter->setPen(d_ptr->m_pen); | ||
painter->setBrush(d_ptr->m_brush); | ||||
|
r855 | painter->drawRect(boundingRect()); | ||
|
r524 | } | ||
|
r724 | /*! | ||
|
r950 | Bounding rect of legend. | ||
*/ | ||||
|
r855 | |||
|
r524 | QRectF QLegend::boundingRect() const | ||
{ | ||||
|
r950 | return d_ptr->m_rect; | ||
|
r524 | } | ||
|
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(); | ||
} | ||||
} | ||||
|
r724 | /*! | ||
|
r950 | Returns the brush used by legend. | ||
*/ | ||||
|
r645 | QBrush QLegend::brush() const | ||
{ | ||||
|
r950 | return d_ptr->m_brush; | ||
|
r645 | } | ||
|
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(); | ||
} | ||||
|
r540 | } | ||
|
r724 | /*! | ||
|
r950 | Returns the pen used by legend | ||
*/ | ||||
|
r724 | |||
|
r645 | QPen QLegend::pen() const | ||
|
r540 | { | ||
|
r950 | return d_ptr->m_pen; | ||
|
r540 | } | ||
|
r724 | /*! | ||
|
r950 | Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart. | ||
\sa QLegend::Alignment | ||||
*/ | ||||
|
r907 | void QLegend::setAlignment(QLegend::Alignments alignment) | ||
|
r616 | { | ||
|
r950 | if(d_ptr->m_alignment!=alignment && d_ptr->m_attachedToChart) { | ||
d_ptr->m_alignment = alignment; | ||||
d_ptr->updateLayout(); | ||||
|
r855 | } | ||
|
r716 | } | ||
|
r724 | /*! | ||
|
r950 | Returns the preferred layout for legend | ||
*/ | ||||
|
r803 | QLegend::Alignments 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; | ||
|
r586 | } | ||
|
r724 | /*! | ||
|
r950 | Attaches the legend to chart. Chart may change layout of the legend. | ||
*/ | ||||
void QLegend::attachToChart() | ||||
|
r586 | { | ||
|
r950 | d_ptr->m_attachedToChart = true; | ||
|
r586 | } | ||
|
r724 | /*! | ||
|
r950 | Returns true, if legend is attached to chart. | ||
*/ | ||||
bool QLegend::isAttachedToChart() | ||||
|
r637 | { | ||
|
r950 | return d_ptr->m_attachedToChart; | ||
|
r637 | } | ||
|
r950 | void QLegend::setOffset(const QPointF& point) | ||
|
r643 | { | ||
|
r950 | d_ptr->setOffset(point.x(),point.y()); | ||
|
r643 | } | ||
|
r950 | QPointF QLegend::offset() const | ||
|
r799 | { | ||
|
r950 | return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY); | ||
|
r799 | } | ||
/*! | ||||
|
r950 | Sets the visibility of legend background to \a visible | ||
*/ | ||||
void QLegend::setBackgroundVisible(bool visible) | ||||
|
r799 | { | ||
|
r950 | if(d_ptr->m_backgroundVisible!=visible) | ||
{ | ||||
d_ptr->m_backgroundVisible=visible; | ||||
update(); | ||||
} | ||||
|
r799 | } | ||
/*! | ||||
|
r950 | Returns the visibility of legend background | ||
*/ | ||||
bool QLegend::isBackgroundVisible() const | ||||
|
r799 | { | ||
|
r950 | return d_ptr->m_backgroundVisible; | ||
|
r799 | } | ||
|
r724 | /*! | ||
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) | ||||
|
r586 | { | ||
|
r950 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); | ||
QGraphicsWidget::resizeEvent(event); | ||||
if(d_ptr->m_rect != rect) { | ||||
d_ptr->m_rect = rect; | ||||
d_ptr->updateLayout(); | ||||
} | ||||
|
r576 | } | ||
|
r724 | /*! | ||
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::hideEvent(QHideEvent *event) | ||||
|
r529 | { | ||
|
r950 | QGraphicsWidget::hideEvent(event); | ||
setEnabled(false); | ||||
d_ptr->updateLayout(); | ||||
|
r565 | } | ||
|
r724 | /*! | ||
|
r950 | \internal \a event see QGraphicsWidget for details | ||
*/ | ||||
void QLegend::showEvent(QShowEvent *event) | ||||
|
r565 | { | ||
|
r950 | QGraphicsWidget::showEvent(event); | ||
setEnabled(true); | ||||
d_ptr->updateLayout(); | ||||
|
r565 | } | ||
|
r529 | |||
|
r950 | qreal QLegend::minWidth() const | ||
|
r565 | { | ||
|
r950 | return d_ptr->m_minWidth; | ||
|
r529 | } | ||
|
r950 | qreal QLegend::minHeight() const | ||
|
r586 | { | ||
|
r950 | return d_ptr->m_minHeight; | ||
} | ||||
|
r855 | |||
|
r950 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
QLegendPrivate::QLegendPrivate(ChartPresenter* presenter,QLegend *q): | ||||
q_ptr(q), | ||||
m_presenter(presenter), | ||||
m_markers(new QGraphicsItemGroup(q)), | ||||
m_alignment(QLegend::AlignmentTop), | ||||
m_offsetX(0), | ||||
m_offsetY(0), | ||||
m_minWidth(0), | ||||
m_minHeight(0), | ||||
m_width(0), | ||||
m_height(0), | ||||
m_attachedToChart(true), | ||||
m_backgroundVisible(false) | ||||
{ | ||||
|
r855 | |||
|
r586 | } | ||
|
r950 | QLegendPrivate::~QLegendPrivate() | ||
{ | ||||
|
r529 | |||
|
r950 | } | ||
void QLegendPrivate::setOffset(qreal x, qreal y) | ||||
|
r855 | { | ||
|
r626 | |||
|
r855 | switch(m_alignment) { | ||
|
r716 | |||
|
r950 | case QLegend::AlignmentTop: | ||
case QLegend::AlignmentBottom: { | ||||
|
r855 | if(m_width<=m_rect.width()) return; | ||
|
r810 | |||
|
r950 | if (x != m_offsetX) { | ||
m_offsetX = qBound(0.0, x, m_width - m_rect.width()); | ||||
|
r855 | m_markers->setPos(-m_offsetX,m_rect.top()); | ||
|
r626 | } | ||
|
r855 | break; | ||
|
r616 | } | ||
|
r950 | case QLegend::AlignmentLeft: | ||
case QLegend::AlignmentRight: { | ||||
|
r716 | |||
|
r855 | if(m_height<=m_rect.height()) return; | ||
|
r716 | |||
|
r950 | if (y != m_offsetY) { | ||
m_offsetY = qBound(0.0, y, m_height - m_rect.height()); | ||||
|
r855 | m_markers->setPos(m_rect.left(),-m_offsetY); | ||
|
r716 | } | ||
|
r855 | break; | ||
|
r716 | } | ||
|
r810 | } | ||
|
r855 | } | ||
|
r716 | |||
|
r950 | void QLegendPrivate::updateLayout() | ||
|
r716 | { | ||
|
r950 | m_offsetX=0; | ||
|
r855 | QList<QGraphicsItem *> items = m_markers->childItems(); | ||
if(items.isEmpty()) return; | ||||
m_minWidth=0; | ||||
m_minHeight=0; | ||||
switch(m_alignment) { | ||||
|
r950 | case QLegend::AlignmentTop: | ||
case QLegend::AlignmentBottom: { | ||||
|
r855 | 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); | ||||
} | ||||
|
r950 | if(m_width<m_rect.width()) { | ||
|
r855 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); | ||
|
r950 | } | ||
else { | ||||
|
r855 | m_markers->setPos(m_rect.topLeft()); | ||
} | ||||
m_height=m_minHeight; | ||||
} | ||||
break; | ||||
|
r950 | case QLegend::AlignmentLeft: | ||
case QLegend::AlignmentRight: { | ||||
|
r855 | 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); | ||||
} | ||||
|
r950 | if(m_height<m_rect.height()) { | ||
|
r855 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); | ||
|
r950 | } | ||
else { | ||||
|
r855 | m_markers->setPos(m_rect.topLeft()); | ||
} | ||||
m_width=m_minWidth; | ||||
} | ||||
break; | ||||
} | ||||
|
r716 | |||
|
r950 | m_presenter->updateLayout(); | ||
|
r716 | } | ||
|
r950 | void QLegendPrivate::handleSeriesAdded(QSeries *series, Domain *domain) | ||
|
r855 | { | ||
|
r950 | Q_UNUSED(domain) | ||
|
r716 | |||
|
r950 | QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); | ||
foreach(LegendMarker* marker , markers) | ||||
m_markers->addToGroup(marker); | ||||
|
r716 | |||
|
r956 | if(series->type()==QSeries::SeriesTypePie) | ||
{ | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
QObject::connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries())); | ||||
QObject::connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries())); | ||||
} | ||||
|
r855 | updateLayout(); | ||
|
r716 | } | ||
|
r950 | void QLegendPrivate::handleSeriesRemoved(QSeries *series) | ||
|
r716 | { | ||
|
r950 | |||
QList<QGraphicsItem *> items = m_markers->childItems(); | ||||
|
r956 | foreach (QGraphicsItem *markers, items) { | ||
LegendMarker *marker = static_cast<LegendMarker*>(markers); | ||||
if (marker->series() == series) { | ||||
delete marker; | ||||
} | ||||
} | ||||
if(series->type()==QSeries::SeriesTypePie) | ||||
{ | ||||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
QObject::disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries())); | ||||
QObject::disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries())); | ||||
} | ||||
|
r950 | |||
|
r855 | updateLayout(); | ||
} | ||||
|
r724 | |||
|
r956 | void QLegendPrivate::handleUpdateSeries() | ||
{ | ||||
//TODO: reimplement to be optimal | ||||
QSeries* series = qobject_cast<QSeries *> (sender()); | ||||
Q_ASSERT(series); | ||||
handleSeriesRemoved(series); | ||||
handleSeriesAdded(series,0); | ||||
} | ||||
|
r524 | #include "moc_qlegend.cpp" | ||
|
r950 | #include "moc_qlegend_p.cpp" | ||
|
r737 | |||
|
r524 | QTCOMMERCIALCHART_END_NAMESPACE | ||