legendmarker.cpp
214 lines
| 5.4 KiB
| text/x-c
|
CppLexer
/ src / legendmarker.cpp
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** 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 "qchartglobal.h" | ||
#include "legendmarker_p.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 | ||||
sauimone
|
r766 | LegendMarker::LegendMarker(QSeries *series, QGraphicsItem *parent) : QGraphicsObject(parent), | ||
sauimone
|
r778 | m_pos(0,0), | ||
m_size(0,0), | ||||
m_boundingRect(0,0,0,0), | ||||
m_markerBoundingRect(0,0,0,0), | ||||
m_series(series), | ||||
m_barset(0), | ||||
m_pieslice(0), | ||||
m_textItem(new QGraphicsSimpleTextItem(this)) | ||||
sauimone
|
r766 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
sauimone
|
r565 | |||
sauimone
|
r766 | LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent) : QGraphicsObject(parent), | ||
sauimone
|
r778 | m_pos(0,0), | ||
m_size(0,0), | ||||
m_boundingRect(0,0,0,0), | ||||
m_markerBoundingRect(0,0,0,0), | ||||
m_series(series), | ||||
m_barset(barset), | ||||
m_pieslice(0), | ||||
m_textItem(new QGraphicsSimpleTextItem(this)) | ||||
sauimone
|
r766 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
sauimone
|
r565 | |||
sauimone
|
r766 | LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent) : QGraphicsObject(parent), | ||
sauimone
|
r778 | m_pos(0,0), | ||
m_size(0,0), | ||||
m_boundingRect(0,0,0,0), | ||||
m_markerBoundingRect(0,0,0,0), | ||||
m_series(series), | ||||
m_barset(0), | ||||
m_pieslice(pieslice), | ||||
m_textItem(new QGraphicsSimpleTextItem(this)) | ||||
sauimone
|
r766 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
sauimone
|
r547 | |||
sauimone
|
r626 | void LegendMarker::setPos(qreal x, qreal y) | ||
sauimone
|
r547 | { | ||
sauimone
|
r778 | m_pos = QPointF(x,y); | ||
sauimone
|
r626 | layoutChanged(); | ||
sauimone
|
r547 | } | ||
sauimone
|
r766 | void LegendMarker::setPen(const QPen &pen) | ||
sauimone
|
r724 | { | ||
sauimone
|
r778 | m_pen = pen; | ||
sauimone
|
r724 | } | ||
QPen LegendMarker::pen() const | ||||
{ | ||||
sauimone
|
r778 | return m_pen; | ||
sauimone
|
r724 | } | ||
sauimone
|
r766 | void LegendMarker::setBrush(const QBrush &brush) | ||
sauimone
|
r547 | { | ||
sauimone
|
r778 | m_brush = brush; | ||
sauimone
|
r547 | } | ||
sauimone
|
r565 | QBrush LegendMarker::brush() const | ||
{ | ||||
sauimone
|
r778 | return m_brush; | ||
sauimone
|
r565 | } | ||
sauimone
|
r547 | void LegendMarker::setName(const QString name) | ||
{ | ||||
sauimone
|
r778 | m_textItem->setText(name); | ||
sauimone
|
r626 | layoutChanged(); | ||
sauimone
|
r547 | } | ||
QString LegendMarker::name() const | ||||
{ | ||||
sauimone
|
r778 | return m_textItem->text(); | ||
sauimone
|
r547 | } | ||
sauimone
|
r576 | QSeries* LegendMarker::series() const | ||
{ | ||||
sauimone
|
r778 | return m_series; | ||
sauimone
|
r576 | } | ||
sauimone
|
r547 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
sauimone
|
r778 | painter->setPen(m_pen); | ||
painter->setBrush(m_brush); | ||||
painter->drawRect(m_markerBoundingRect); | ||||
sauimone
|
r547 | } | ||
QRectF LegendMarker::boundingRect() const | ||||
{ | ||||
sauimone
|
r778 | return m_boundingRect; | ||
sauimone
|
r547 | } | ||
sauimone
|
r626 | void LegendMarker::layoutChanged() | ||
{ | ||||
QSizeF markerSize(10,10); | ||||
qreal margin = 2; | ||||
sauimone
|
r778 | m_size.setHeight(markerSize.height() + 2 * margin); | ||
m_size.setWidth(m_textItem->boundingRect().width() + markerSize.width() + 3 * margin); | ||||
sauimone
|
r626 | |||
sauimone
|
r778 | m_boundingRect = QRectF(m_pos.x(),m_pos.y(),m_size.width(),m_size.height()); | ||
sauimone
|
r626 | |||
sauimone
|
r778 | m_markerBoundingRect = QRectF(m_pos.x() + margin, m_pos.y() + margin, markerSize.width(),markerSize.height()); | ||
sauimone
|
r626 | |||
sauimone
|
r778 | m_textItem->setPos(m_pos.x() + markerSize.width() + 2 * margin, m_pos.y() + margin); | ||
sauimone
|
r626 | } | ||
sauimone
|
r547 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
sauimone
|
r792 | switch (m_series->type()) { | ||
case QSeries::SeriesTypeLine: | ||||
case QSeries::SeriesTypeArea: | ||||
case QSeries::SeriesTypeScatter: | ||||
case QSeries::SeriesTypeSpline: { | ||||
sauimone
|
r778 | emit clicked(m_series,event->button()); | ||
sauimone
|
r565 | break; | ||
sauimone
|
r569 | } | ||
sauimone
|
r792 | case QSeries::SeriesTypeBar: | ||
case QSeries::SeriesTypeStackedBar: | ||||
case QSeries::SeriesTypePercentBar: { | ||||
sauimone
|
r778 | emit clicked(m_barset,event->button()); | ||
sauimone
|
r565 | break; | ||
sauimone
|
r569 | } | ||
sauimone
|
r792 | case QSeries::SeriesTypePie: { | ||
sauimone
|
r778 | emit clicked(m_pieslice,event->button()); | ||
sauimone
|
r565 | break; | ||
} | ||||
default: { | ||||
break; | ||||
sauimone
|
r569 | } | ||
sauimone
|
r565 | } | ||
sauimone
|
r547 | } | ||
sauimone
|
r587 | void LegendMarker::changed() | ||
{ | ||||
sauimone
|
r800 | setPen(Qt::NoPen); | ||
sauimone
|
r792 | switch (m_series->type()) { | ||
case QSeries::SeriesTypeArea: { | ||||
QAreaSeries* s = static_cast<QAreaSeries*> (m_series); | ||||
sauimone
|
r616 | setBrush(s->brush()); | ||
setName(s->name()); | ||||
sauimone
|
r587 | break; | ||
} | ||||
sauimone
|
r792 | case QSeries::SeriesTypeLine: | ||
case QSeries::SeriesTypeSpline: { | ||||
QXYSeries* s = static_cast<QXYSeries*> (m_series); | ||||
sauimone
|
r800 | setBrush(QBrush(s->pen().color(),Qt::SolidPattern)); | ||
sauimone
|
r792 | setName(s->name()); | ||
break; | ||||
} | ||||
sauimone
|
r800 | case QSeries::SeriesTypeScatter: { | ||
QXYSeries* s = static_cast<QXYSeries*> (m_series); | ||||
setBrush(s->brush()); | ||||
setName(s->name()); | ||||
break; | ||||
} | ||||
sauimone
|
r792 | case QSeries::SeriesTypeBar: | ||
case QSeries::SeriesTypeStackedBar: | ||||
case QSeries::SeriesTypePercentBar: { | ||||
sauimone
|
r778 | setBrush(m_barset->brush()); | ||
setName(m_barset->name()); | ||||
sauimone
|
r587 | break; | ||
sauimone
|
r792 | } | ||
case QSeries::SeriesTypePie: { | ||||
sauimone
|
r778 | setBrush(m_pieslice->brush()); | ||
setName(m_pieslice->label()); | ||||
sauimone
|
r587 | break; | ||
sauimone
|
r792 | } | ||
default: { | ||||
sauimone
|
r800 | setBrush(Qt::NoBrush); | ||
sauimone
|
r792 | break; | ||
} | ||||
sauimone
|
r587 | } | ||
} | ||||
sauimone
|
r547 | #include "moc_legendmarker_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||