legendmarker.cpp
237 lines
| 6.4 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)), | ||||
|
r1534 | m_rectItem(new QGraphicsRectItem(this)), | ||
m_margin(2), | ||||
m_space(4) | ||||
|
r766 | { | ||
|
r855 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||
m_rectItem->setRect(m_markerRect); | ||||
|
r547 | } | ||
|
r766 | void LegendMarker::setPen(const QPen &pen) | ||
|
r724 | { | ||
|
r1528 | m_rectItem->setPen(pen); | ||
|
r1534 | m_textItem->setPen(pen); | ||
|
r724 | } | ||
QPen LegendMarker::pen() const | ||||
{ | ||||
|
r1528 | return m_rectItem->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 | } | ||
|
r1522 | void LegendMarker::setFont(const QFont &font) | ||
{ | ||||
m_textItem->setFont(font); | ||||
|
r1539 | QFontMetrics fn(font); | ||
m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); | ||||
|
r1536 | updateGeometry(); | ||
|
r1522 | } | ||
QFont LegendMarker::font() const | ||||
{ | ||||
return m_textItem->font(); | ||||
} | ||||
|
r1429 | void LegendMarker::setLabel(const QString label) | ||
|
r547 | { | ||
|
r1429 | m_textItem->setText(label); | ||
|
r547 | } | ||
|
r1534 | QString LegendMarker::label() const | ||
|
r547 | { | ||
|
r1534 | return m_textItem->text(); | ||
|
r547 | } | ||
|
r1534 | QRectF LegendMarker::boundingRect() const | ||
|
r576 | { | ||
|
r1534 | return m_boundingRect; | ||
|
r576 | } | ||
|
r1527 | void LegendMarker::setLabelBrush(const QBrush &brush) | ||
{ | ||||
m_textItem->setBrush(brush); | ||||
} | ||||
QBrush LegendMarker::labelBrush() const | ||||
{ | ||||
return m_textItem->brush(); | ||||
} | ||||
|
r1534 | |||
void LegendMarker::setGeometry(const QRectF& rect) | ||||
{ | ||||
const QRectF& textRect = m_textItem->boundingRect(); | ||||
m_textItem->setPos(m_markerRect.width() + m_space + m_margin,rect.height()/2 - textRect.height()/2); | ||||
|
r1539 | m_rectItem->setRect(m_markerRect); | ||
|
r1534 | m_rectItem->setPos(m_margin,rect.height()/2 - m_markerRect.height()/2); | ||
prepareGeometryChange(); | ||||
m_boundingRect = rect; | ||||
} | ||||
|
r547 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
|
r855 | Q_UNUSED(painter) | ||
|
r547 | } | ||
|
r1534 | QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const | ||
|
r626 | { | ||
|
r1534 | Q_UNUSED(constraint) | ||
|
r626 | |||
|
r1534 | QFontMetrics fn(m_textItem->font()); | ||
QSizeF sh; | ||||
|
r626 | |||
|
r1534 | switch (which) { | ||
case Qt::MinimumSize: | ||||
sh = QSizeF(fn.boundingRect("...").width(),fn.height()); | ||||
break; | ||||
case Qt::PreferredSize: | ||||
sh = QSizeF(fn.boundingRect(m_textItem->text()).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); | ||||
break; | ||||
default: | ||||
break; | ||||
} | ||||
|
r626 | |||
|
r1534 | return sh; | ||
|
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 | ||||