legendmarker.cpp
192 lines
| 5.3 KiB
| text/x-c
|
CppLexer
/ src / legendmarker.cpp
Jani Honkonen
|
r794 | /**************************************************************************** | ||
Michal Klocek
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
sauimone
|
r547 | #include "legendmarker_p.h" | ||
Michal Klocek
|
r855 | #include "qlegend.h" | ||
#include "qbarseries.h" | ||||
#include "qpieseries.h" | ||||
sauimone
|
r587 | #include <qpieslice.h> | ||
#include <qbarset.h> | ||||
sauimone
|
r616 | #include <qxyseries.h> | ||
sauimone
|
r792 | #include <qareaseries.h> | ||
sauimone
|
r547 | #include <QPainter> | ||
#include <QGraphicsSceneEvent> | ||||
sauimone
|
r626 | #include <QGraphicsSimpleTextItem> | ||
sauimone
|
r547 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r855 | LegendMarker::LegendMarker(QSeries* 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)) | ||||
sauimone
|
r766 | { | ||
Michal Klocek
|
r855 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||
m_rectItem->setRect(m_markerRect); | ||||
updateLayout(); | ||||
sauimone
|
r547 | } | ||
sauimone
|
r766 | void LegendMarker::setPen(const QPen &pen) | ||
sauimone
|
r724 | { | ||
Michal Klocek
|
r855 | m_textItem->setPen(pen); | ||
updateLayout(); | ||||
sauimone
|
r724 | } | ||
QPen LegendMarker::pen() const | ||||
{ | ||||
Michal Klocek
|
r855 | return m_textItem->pen(); | ||
sauimone
|
r724 | } | ||
sauimone
|
r766 | void LegendMarker::setBrush(const QBrush &brush) | ||
sauimone
|
r547 | { | ||
Michal Klocek
|
r855 | m_rectItem->setBrush(brush); | ||
sauimone
|
r547 | } | ||
sauimone
|
r565 | QBrush LegendMarker::brush() const | ||
{ | ||||
Michal Klocek
|
r855 | return m_rectItem->brush(); | ||
sauimone
|
r565 | } | ||
Michal Klocek
|
r855 | void LegendMarker::setLabel(const QString name) | ||
sauimone
|
r547 | { | ||
sauimone
|
r778 | m_textItem->setText(name); | ||
Michal Klocek
|
r855 | updateLayout(); | ||
sauimone
|
r547 | } | ||
Michal Klocek
|
r855 | void LegendMarker::setSize(const QSize& size) | ||
sauimone
|
r547 | { | ||
Michal Klocek
|
r855 | m_markerRect = QRectF(0,0,size.width(),size.height()); | ||
sauimone
|
r547 | } | ||
Michal Klocek
|
r855 | QString LegendMarker::label() const | ||
sauimone
|
r576 | { | ||
Michal Klocek
|
r855 | return m_textItem->text(); | ||
sauimone
|
r576 | } | ||
sauimone
|
r547 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
Michal Klocek
|
r855 | Q_UNUSED(painter) | ||
sauimone
|
r547 | } | ||
QRectF LegendMarker::boundingRect() const | ||||
{ | ||||
sauimone
|
r778 | return m_boundingRect; | ||
sauimone
|
r547 | } | ||
Michal Klocek
|
r855 | void LegendMarker::updateLayout() | ||
sauimone
|
r626 | { | ||
Michal Klocek
|
r855 | static const qreal margin = 2; | ||
static const qreal space = 4; | ||||
sauimone
|
r626 | |||
Michal Klocek
|
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); | ||||
sauimone
|
r626 | |||
} | ||||
sauimone
|
r547 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
Michal Klocek
|
r855 | QGraphicsObject::mousePressEvent(event); | ||
emit selected(); | ||||
sauimone
|
r547 | } | ||
Michal Klocek
|
r855 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), | ||||
m_series(series) | ||||
sauimone
|
r587 | { | ||
Michal Klocek
|
r855 | QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | ||
QObject::connect(series,SIGNAL(updated()), this, SLOT(updated())); | ||||
updated(); | ||||
} | ||||
void AreaLegendMarker::updated() | ||||
{ | ||||
setBrush(m_series->brush()); | ||||
setLabel(m_series->name()); | ||||
} | ||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
BarLegendMarker::BarLegendMarker(QBarSeries *series,QBarSet *barset, QLegend *legend) : LegendMarker(series,legend), | ||||
m_barset(barset) | ||||
{ | ||||
QObject::connect(this, SIGNAL(selected()),series, SIGNAL(selected())); | ||||
QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(updated())); | ||||
updated(); | ||||
} | ||||
void BarLegendMarker::updated() | ||||
{ | ||||
setBrush(m_barset->brush()); | ||||
setLabel(m_barset->name()); | ||||
} | ||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), | ||||
m_pieslice(pieslice) | ||||
{ | ||||
QObject::connect(this, SIGNAL(selected()),pieslice, SIGNAL(selected())); | ||||
QObject::connect(pieslice, SIGNAL(changed()), this, SLOT(updated())); | ||||
QObject::connect(pieslice, SIGNAL(destroyed()), this, SLOT(deleteLater())); //TODO:checkthis | ||||
updated(); | ||||
} | ||||
void PieLegendMarker::updated() | ||||
{ | ||||
setBrush(m_pieslice->brush()); | ||||
setLabel(m_pieslice->label()); | ||||
} | ||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), | ||||
m_series(series) | ||||
{ | ||||
QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | ||||
QObject::connect(series,SIGNAL(updated()), this, SLOT(updated())); | ||||
updated(); | ||||
} | ||||
void XYLegendMarker::updated() | ||||
{ | ||||
setLabel(m_series->name()); | ||||
if(m_series->type()== QSeries::SeriesTypeScatter) | ||||
{ | ||||
setBrush(m_series->brush()); | ||||
sauimone
|
r800 | } | ||
Michal Klocek
|
r855 | else { | ||
setBrush(QBrush(m_series->pen().color())); | ||||
sauimone
|
r587 | } | ||
} | ||||
sauimone
|
r547 | #include "moc_legendmarker_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||