legendmarker.cpp
254 lines
| 6.9 KiB
| text/x-c
|
CppLexer
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
|
r938 | #include "qxyseries.h" | ||
#include "qxyseries_p.h" | ||||
Michal Klocek
|
r855 | #include "qlegend.h" | ||
sauimone
|
r1586 | #include "qabstractbarseries.h" | ||
Michal Klocek
|
r855 | #include "qpieseries.h" | ||
Michal Klocek
|
r938 | #include "qpieslice.h" | ||
#include "qbarset.h" | ||||
sauimone
|
r953 | #include "qbarset_p.h" | ||
Michal Klocek
|
r938 | #include "qareaseries.h" | ||
#include "qareaseries_p.h" | ||||
sauimone
|
r547 | #include <QPainter> | ||
#include <QGraphicsSceneEvent> | ||||
sauimone
|
r626 | #include <QGraphicsSimpleTextItem> | ||
Michal Klocek
|
r2085 | #include <QGraphicsLayout> | ||
sauimone
|
r547 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Tero Ahola
|
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)), | ||||
Michal Klocek
|
r1534 | m_rectItem(new QGraphicsRectItem(this)), | ||
Michal Klocek
|
r1965 | m_margin(4), | ||
Michal Klocek
|
r1534 | m_space(4) | ||
sauimone
|
r766 | { | ||
Michal Klocek
|
r855 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||
m_rectItem->setRect(m_markerRect); | ||||
sauimone
|
r547 | } | ||
sauimone
|
r766 | void LegendMarker::setPen(const QPen &pen) | ||
sauimone
|
r724 | { | ||
Tero Ahola
|
r1528 | m_rectItem->setPen(pen); | ||
sauimone
|
r724 | } | ||
QPen LegendMarker::pen() const | ||||
{ | ||||
Tero Ahola
|
r1528 | return m_rectItem->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 | } | ||
sauimone
|
r1522 | void LegendMarker::setFont(const QFont &font) | ||
{ | ||||
m_textItem->setFont(font); | ||||
Michal Klocek
|
r1539 | QFontMetrics fn(font); | ||
m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); | ||||
Michal Klocek
|
r1536 | updateGeometry(); | ||
sauimone
|
r1522 | } | ||
QFont LegendMarker::font() const | ||||
{ | ||||
return m_textItem->font(); | ||||
} | ||||
sauimone
|
r1429 | void LegendMarker::setLabel(const QString label) | ||
sauimone
|
r547 | { | ||
Michal Klocek
|
r1965 | m_text = label; | ||
updateGeometry(); | ||||
Michal Klocek
|
r2085 | m_legend->layout()->invalidate(); | ||
sauimone
|
r547 | } | ||
Michal Klocek
|
r1534 | QString LegendMarker::label() const | ||
sauimone
|
r547 | { | ||
Michal Klocek
|
r1965 | return m_text; | ||
sauimone
|
r547 | } | ||
Michal Klocek
|
r1534 | QRectF LegendMarker::boundingRect() const | ||
sauimone
|
r576 | { | ||
Michal Klocek
|
r1534 | return m_boundingRect; | ||
sauimone
|
r576 | } | ||
sauimone
|
r1527 | void LegendMarker::setLabelBrush(const QBrush &brush) | ||
{ | ||||
m_textItem->setBrush(brush); | ||||
} | ||||
QBrush LegendMarker::labelBrush() const | ||||
{ | ||||
return m_textItem->brush(); | ||||
} | ||||
Michal Klocek
|
r1534 | |||
void LegendMarker::setGeometry(const QRectF& rect) | ||||
{ | ||||
Michal Klocek
|
r1965 | QFontMetrics fn (font()); | ||
int width = rect.width(); | ||||
qreal x = m_margin + m_markerRect.width() + m_space + m_margin; | ||||
qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin); | ||||
if (fn.boundingRect(m_text).width() + x > width) | ||||
{ | ||||
QString string = m_text + "..."; | ||||
while(fn.boundingRect(string).width() + x > width && string.length() > 3) | ||||
string.remove(string.length() - 4, 1); | ||||
m_textItem->setText(string); | ||||
} | ||||
else | ||||
m_textItem->setText(m_text); | ||||
Michal Klocek
|
r1534 | const QRectF& textRect = m_textItem->boundingRect(); | ||
Michal Klocek
|
r1965 | |||
m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2); | ||||
Michal Klocek
|
r1539 | m_rectItem->setRect(m_markerRect); | ||
Michal Klocek
|
r1965 | m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2); | ||
Michal Klocek
|
r1534 | |||
prepareGeometryChange(); | ||||
Michal Klocek
|
r1965 | m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y); | ||
Michal Klocek
|
r1534 | } | ||
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 | } | ||
Michal Klocek
|
r1534 | QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const | ||
sauimone
|
r626 | { | ||
Michal Klocek
|
r1534 | Q_UNUSED(constraint) | ||
sauimone
|
r626 | |||
Michal Klocek
|
r1534 | QFontMetrics fn(m_textItem->font()); | ||
QSizeF sh; | ||||
sauimone
|
r626 | |||
Michal Klocek
|
r1534 | switch (which) { | ||
case Qt::MinimumSize: | ||||
Michal Klocek
|
r1965 | sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); | ||
Michal Klocek
|
r1534 | break; | ||
case Qt::PreferredSize: | ||||
Michal Klocek
|
r1965 | sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); | ||
Michal Klocek
|
r1534 | break; | ||
default: | ||||
break; | ||||
} | ||||
sauimone
|
r626 | |||
Michal Klocek
|
r1534 | return sh; | ||
sauimone
|
r626 | } | ||
sauimone
|
r547 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
Michal Klocek
|
r855 | QGraphicsObject::mousePressEvent(event); | ||
Michal Klocek
|
r1965 | //TODO: selected signal removed for now | ||
sauimone
|
r547 | } | ||
Michal Klocek
|
r855 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), | ||||
m_series(series) | ||||
sauimone
|
r587 | { | ||
Michal Klocek
|
r1068 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | ||
Michal Klocek
|
r938 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | ||
Tero Ahola
|
r1328 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | ||
Michal Klocek
|
r855 | updated(); | ||
} | ||||
void AreaLegendMarker::updated() | ||||
{ | ||||
setBrush(m_series->brush()); | ||||
setLabel(m_series->name()); | ||||
} | ||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
sauimone
|
r1584 | BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), | ||
Michal Klocek
|
r855 | m_barset(barset) | ||
{ | ||||
Michal Klocek
|
r1068 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); | ||
sauimone
|
r1008 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); | ||
Michal Klocek
|
r855 | updated(); | ||
} | ||||
void BarLegendMarker::updated() | ||||
{ | ||||
setBrush(m_barset->brush()); | ||||
sauimone
|
r1429 | setLabel(m_barset->label()); | ||
Michal Klocek
|
r855 | } | ||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), | ||||
m_pieslice(pieslice) | ||||
{ | ||||
Jani Honkonen
|
r1224 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); | ||
Jani Honkonen
|
r1274 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); | ||
Michal Klocek
|
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) | ||||
{ | ||||
Michal Klocek
|
r1068 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | ||
Michal Klocek
|
r938 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | ||
Tero Ahola
|
r1328 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | ||
Michal Klocek
|
r855 | updated(); | ||
} | ||||
void XYLegendMarker::updated() | ||||
{ | ||||
setLabel(m_series->name()); | ||||
Tero Ahola
|
r988 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) | ||
Michal Klocek
|
r855 | { | ||
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 | ||||