areachartitem.cpp
262 lines
| 9.4 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
|
r794 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r794 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r794 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r421 | #include "areachartitem_p.h" | ||
#include "qareaseries.h" | ||||
|
r938 | #include "qareaseries_p.h" | ||
|
r421 | #include "qlineseries.h" | ||
|
r564 | #include "chartpresenter_p.h" | ||
|
r2275 | #include "abstractdomain_p.h" | ||
|
r421 | #include <QPainter> | ||
|
r571 | #include <QGraphicsSceneMouseEvent> | ||
|
r1241 | #include <QDebug> | ||
|
r421 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r421 | |||
|
r2273 | AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, QGraphicsItem* item) | ||
|
r2439 | : ChartItem(areaSeries->d_func(),item), | ||
|
r2097 | m_series(areaSeries), | ||
m_upper(0), | ||||
m_lower(0), | ||||
|
r2689 | m_pointsVisible(false), | ||
m_pointLabelsVisible(false), | ||||
|
r2695 | m_pointLabelsFormat(areaSeries->pointLabelsFormat()), | ||
|
r2689 | m_pointLabelsFont(areaSeries->pointLabelsFont()), | ||
m_pointLabelsColor(areaSeries->pointLabelsColor()) | ||||
|
r421 | { | ||
|
r2264 | setAcceptHoverEvents(true); | ||
|
r564 | setZValue(ChartPresenter::LineChartZValue); | ||
|
r2601 | if (m_series->upperSeries()) | ||
m_upper = new AreaBoundItem(this, m_series->upperSeries()); | ||||
|
r2097 | if (m_series->lowerSeries()) | ||
|
r2273 | m_lower = new AreaBoundItem(this, m_series->lowerSeries()); | ||
|
r421 | |||
|
r2097 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
|
r1342 | QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
|
r2067 | QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | ||
|
r2097 | QObject::connect(this, SIGNAL(clicked(QPointF)), areaSeries, SIGNAL(clicked(QPointF))); | ||
|
r2264 | QObject::connect(this, SIGNAL(hovered(QPointF,bool)), areaSeries, SIGNAL(hovered(QPointF,bool))); | ||
|
r2689 | QObject::connect(areaSeries, SIGNAL(pointLabelsFormatChanged(QString)), | ||
this, SLOT(handleUpdated())); | ||||
QObject::connect(areaSeries, SIGNAL(pointLabelsVisibilityChanged(bool)), | ||||
this, SLOT(handleUpdated())); | ||||
QObject::connect(areaSeries, SIGNAL(pointLabelsFontChanged(QFont)), | ||||
this, SLOT(handleUpdated())); | ||||
QObject::connect(areaSeries, SIGNAL(pointLabelsColorChanged(QColor)), | ||||
this, SLOT(handleUpdated())); | ||||
|
r421 | |||
handleUpdated(); | ||||
} | ||||
AreaChartItem::~AreaChartItem() | ||||
{ | ||||
delete m_upper; | ||||
delete m_lower; | ||||
|
r761 | } | ||
|
r421 | |||
|
r2273 | void AreaChartItem::setPresenter(ChartPresenter *presenter) | ||
{ | ||||
|
r2601 | if (m_upper) | ||
m_upper->setPresenter(presenter); | ||||
|
r2273 | if (m_lower) { | ||
m_lower->setPresenter(presenter); | ||||
} | ||||
ChartItem::setPresenter(presenter); | ||||
} | ||||
|
r421 | QRectF AreaChartItem::boundingRect() const | ||
{ | ||||
return m_rect; | ||||
} | ||||
QPainterPath AreaChartItem::shape() const | ||||
{ | ||||
return m_path; | ||||
} | ||||
void AreaChartItem::updatePath() | ||||
{ | ||||
QPainterPath path; | ||||
|
r2273 | QRectF rect(QPointF(0,0),domain()->size()); | ||
|
r421 | |||
|
r1792 | path = m_upper->path(); | ||
|
r690 | |||
|
r743 | if (m_lower) { | ||
|
r2483 | // Note: Polarcharts always draw area correctly only when both series have equal width or are | ||
// fully displayed. If one series is partally off-chart, the connecting line between | ||||
// the series does not attach to the end of the partially hidden series but to the point | ||||
// where it intersects the axis line. The problem is especially noticeable when one of the series | ||||
// is entirely off-chart, in which case the connecting line connects two ends of the | ||||
// visible series. | ||||
// This happens because we get the paths from linechart, which omits off-chart segments. | ||||
// To properly fix, linechart would need to provide true full path, in right, left, and the rest | ||||
// portions to enable proper clipping. However, combining those to single visually unified area | ||||
// would be a nightmare, since they would have to be painted separately. | ||||
|
r1792 | path.connectPath(m_lower->path().toReversed()); | ||
|
r743 | } else { | ||
|
r421 | QPointF first = path.pointAtPercent(0); | ||
QPointF last = path.pointAtPercent(1); | ||||
|
r2483 | if (presenter()->chartType() == QChart::ChartTypeCartesian) { | ||
path.lineTo(last.x(), rect.bottom()); | ||||
path.lineTo(first.x(), rect.bottom()); | ||||
} else { // polar | ||||
path.lineTo(rect.center()); | ||||
} | ||||
|
r421 | } | ||
path.closeSubpath(); | ||||
|
r2603 | |||
// Only zoom in if the bounding rect of the path fits inside int limits. QWidget::update() uses | ||||
// a region that has to be compatible with QRect. | ||||
if (path.boundingRect().height() <= INT_MAX | ||||
&& path.boundingRect().width() <= INT_MAX) { | ||||
prepareGeometryChange(); | ||||
m_path = path; | ||||
m_rect = path.boundingRect(); | ||||
update(); | ||||
} | ||||
|
r421 | } | ||
void AreaChartItem::handleUpdated() | ||||
{ | ||||
|
r1346 | setVisible(m_series->isVisible()); | ||
|
r563 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
|
r557 | m_brush = m_series->brush(); | ||
|
r563 | m_pointPen = m_series->pen(); | ||
|
r761 | m_pointPen.setWidthF(2 * m_pointPen.width()); | ||
|
r2067 | setOpacity(m_series->opacity()); | ||
|
r2689 | m_pointLabelsFormat = m_series->pointLabelsFormat(); | ||
m_pointLabelsVisible = m_series->pointLabelsVisible(); | ||||
m_pointLabelsFont = m_series->pointLabelsFont(); | ||||
m_pointLabelsColor = m_series->pointLabelsColor(); | ||||
|
r421 | update(); | ||
} | ||||
|
r1698 | void AreaChartItem::handleDomainUpdated() | ||
|
r421 | { | ||
|
r2601 | if (m_upper) { | ||
AbstractDomain* d = m_upper->domain(); | ||||
d->setSize(domain()->size()); | ||||
d->setRange(domain()->minX(),domain()->maxX(),domain()->minY(),domain()->maxY()); | ||||
m_upper->handleDomainUpdated(); | ||||
} | ||||
|
r2273 | |||
|
r2097 | if (m_lower) { | ||
|
r2285 | AbstractDomain* d = m_lower->domain(); | ||
|
r2712 | d->setSize(domain()->size()); | ||
d->setRange(domain()->minX(),domain()->maxX(),domain()->minY(),domain()->maxY()); | ||||
m_lower->handleDomainUpdated(); | ||||
|
r1698 | } | ||
|
r421 | } | ||
void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
|
r611 | Q_UNUSED(widget) | ||
Q_UNUSED(option) | ||||
|
r1346 | painter->save(); | ||
painter->setPen(m_linePen); | ||||
painter->setBrush(m_brush); | ||||
|
r2483 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); | ||
if (presenter()->chartType() == QChart::ChartTypePolar) | ||||
painter->setClipRegion(QRegion(clipRect.toRect(), QRegion::Ellipse)); | ||||
else | ||||
painter->setClipRect(clipRect); | ||||
|
r1346 | painter->drawPath(m_path); | ||
if (m_pointsVisible) { | ||||
|
r2097 | painter->setPen(m_pointPen); | ||
painter->drawPoints(m_upper->geometryPoints()); | ||||
if (m_lower) | ||||
painter->drawPoints(m_lower->geometryPoints()); | ||||
|
r563 | } | ||
|
r2689 | |||
// Draw series point label | ||||
if (m_pointLabelsVisible) { | ||||
static const QString xPointTag(QLatin1String("@xPoint")); | ||||
static const QString yPointTag(QLatin1String("@yPoint")); | ||||
const int labelOffset = 2; | ||||
painter->setFont(m_pointLabelsFont); | ||||
painter->setPen(QPen(m_pointLabelsColor)); | ||||
QFontMetrics fm(painter->font()); | ||||
QString pointLabel = m_pointLabelsFormat; | ||||
if (m_series->upperSeries()) { | ||||
for (int i(0); i < m_series->upperSeries()->count(); i++) { | ||||
|
r2707 | pointLabel.replace(xPointTag, | ||
presenter()->numberToString(m_series->upperSeries()->at(i).x())); | ||||
pointLabel.replace(yPointTag, | ||||
presenter()->numberToString(m_series->upperSeries()->at(i).y())); | ||||
|
r2689 | |||
// Position text in relation to the point | ||||
int pointLabelWidth = fm.width(pointLabel); | ||||
QPointF position(m_upper->geometryPoints().at(i)); | ||||
position.setX(position.x() - pointLabelWidth / 2); | ||||
position.setY(position.y() - m_series->upperSeries()->pen().width() / 2 - labelOffset); | ||||
painter->drawText(position, pointLabel); | ||||
} | ||||
} | ||||
if (m_series->lowerSeries()) { | ||||
for (int i(0); i < m_series->lowerSeries()->count(); i++) { | ||||
|
r2707 | pointLabel.replace(xPointTag, | ||
presenter()->numberToString(m_series->lowerSeries()->at(i).x())); | ||||
pointLabel.replace(yPointTag, | ||||
presenter()->numberToString(m_series->lowerSeries()->at(i).y())); | ||||
|
r2689 | |||
// Position text in relation to the point | ||||
int pointLabelWidth = fm.width(pointLabel); | ||||
QPointF position(m_lower->geometryPoints().at(i)); | ||||
position.setX(position.x() - pointLabelWidth / 2); | ||||
position.setY(position.y() - m_series->lowerSeries()->pen().width() / 2 - labelOffset); | ||||
painter->drawText(position, pointLabel); | ||||
} | ||||
} | ||||
} | ||||
|
r1346 | painter->restore(); | ||
|
r421 | } | ||
|
r743 | void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
|
r571 | { | ||
|
r2273 | emit clicked(m_upper->domain()->calculateDomainPoint(event->pos())); | ||
|
r1747 | ChartItem::mousePressEvent(event); | ||
|
r571 | } | ||
|
r2264 | void AreaChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | ||
{ | ||||
|
r2273 | emit hovered(domain()->calculateDomainPoint(event->pos()), true); | ||
|
r2264 | event->accept(); | ||
// QGraphicsItem::hoverEnterEvent(event); | ||||
} | ||||
void AreaChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
|
r2273 | emit hovered(domain()->calculateDomainPoint(event->pos()), false); | ||
|
r2264 | event->accept(); | ||
// QGraphicsItem::hoverEnterEvent(event); | ||||
} | ||||
|
r421 | #include "moc_areachartitem_p.cpp" | ||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||