legendmarker.cpp
198 lines
| 5.6 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
|
r855 | ** | ||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r547 | #include "legendmarker_p.h" | ||
|
r938 | #include "qxyseries.h" | ||
#include "qxyseries_p.h" | ||||
|
r855 | #include "qlegend.h" | ||
#include "qbarseries.h" | ||||
#include "qpieseries.h" | ||||
|
r938 | #include "qpieslice.h" | ||
#include "qbarset.h" | ||||
|
r953 | #include "qbarset_p.h" | ||
|
r938 | #include "qareaseries.h" | ||
#include "qareaseries_p.h" | ||||
|
r547 | #include <QPainter> | ||
#include <QGraphicsSceneEvent> | ||||
|
r626 | #include <QGraphicsSimpleTextItem> | ||
|
r1068 | #include <QDebug> | ||
|
r547 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1328 | LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : | ||
QGraphicsObject(legend), | ||||
m_series(series), | ||||
m_markerRect(0,0,10.0,10.0), | ||||
m_boundingRect(0,0,0,0), | ||||
m_legend(legend), | ||||
m_textItem(new QGraphicsSimpleTextItem(this)), | ||||
m_rectItem(new QGraphicsRectItem(this)) | ||||
|
r766 | { | ||
|
r855 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||
m_rectItem->setRect(m_markerRect); | ||||
updateLayout(); | ||||
|
r547 | } | ||
|
r766 | void LegendMarker::setPen(const QPen &pen) | ||
|
r724 | { | ||
|
r855 | m_textItem->setPen(pen); | ||
updateLayout(); | ||||
|
r724 | } | ||
QPen LegendMarker::pen() const | ||||
{ | ||||
|
r855 | return m_textItem->pen(); | ||
|
r724 | } | ||
|
r766 | void LegendMarker::setBrush(const QBrush &brush) | ||
|
r547 | { | ||
|
r855 | m_rectItem->setBrush(brush); | ||
|
r547 | } | ||
|
r565 | QBrush LegendMarker::brush() const | ||
{ | ||||
|
r855 | return m_rectItem->brush(); | ||
|
r565 | } | ||
|
r1429 | void LegendMarker::setLabel(const QString label) | ||
|
r547 | { | ||
|
r1429 | m_textItem->setText(label); | ||
|
r855 | updateLayout(); | ||
|
r547 | } | ||
|
r855 | void LegendMarker::setSize(const QSize& size) | ||
|
r547 | { | ||
|
r855 | m_markerRect = QRectF(0,0,size.width(),size.height()); | ||
|
r547 | } | ||
|
r855 | QString LegendMarker::label() const | ||
|
r576 | { | ||
|
r855 | return m_textItem->text(); | ||
|
r576 | } | ||
|
r547 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
|
r855 | Q_UNUSED(painter) | ||
|
r547 | } | ||
QRectF LegendMarker::boundingRect() const | ||||
{ | ||||
|
r778 | return m_boundingRect; | ||
|
r547 | } | ||
|
r855 | void LegendMarker::updateLayout() | ||
|
r626 | { | ||
|
r855 | static const qreal margin = 2; | ||
static const qreal space = 4; | ||||
|
r626 | |||
|
r855 | const QRectF& textRect = m_textItem->boundingRect(); | ||
prepareGeometryChange(); | ||||
m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin)); | ||||
m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2); | ||||
m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2); | ||||
|
r626 | |||
} | ||||
|
r547 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
|
r855 | QGraphicsObject::mousePressEvent(event); | ||
|
r1068 | qDebug()<<"Not implemented"; //TODO: selected signal removed for now | ||
|
r547 | } | ||
|
r855 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), | ||||
m_series(series) | ||||
|
r587 | { | ||
|
r1068 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | ||
|
r938 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | ||
|
r1328 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | ||
|
r855 | updated(); | ||
} | ||||
void AreaLegendMarker::updated() | ||||
{ | ||||
setBrush(m_series->brush()); | ||||
setLabel(m_series->name()); | ||||
} | ||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
|
r953 | BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), | ||
|
r855 | m_barset(barset) | ||
{ | ||||
|
r1068 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); | ||
|
r1008 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); | ||
|
r855 | updated(); | ||
} | ||||
void BarLegendMarker::updated() | ||||
{ | ||||
setBrush(m_barset->brush()); | ||||
|
r1429 | setLabel(m_barset->label()); | ||
|
r855 | } | ||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), | ||||
m_pieslice(pieslice) | ||||
{ | ||||
|
r1224 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); | ||
|
r1274 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); | ||
|
r855 | updated(); | ||
} | ||||
void PieLegendMarker::updated() | ||||
{ | ||||
setBrush(m_pieslice->brush()); | ||||
setLabel(m_pieslice->label()); | ||||
} | ||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), | ||||
m_series(series) | ||||
{ | ||||
|
r1068 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | ||
|
r938 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | ||
|
r1328 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | ||
|
r855 | updated(); | ||
} | ||||
void XYLegendMarker::updated() | ||||
{ | ||||
setLabel(m_series->name()); | ||||
|
r988 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) | ||
|
r855 | { | ||
setBrush(m_series->brush()); | ||||
|
r800 | } | ||
|
r855 | else { | ||
setBrush(QBrush(m_series->pen().color())); | ||||
|
r587 | } | ||
} | ||||
|
r547 | #include "moc_legendmarker_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||