areachartitem.cpp
142 lines
| 3.7 KiB
| text/x-c
|
CppLexer
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r421 | #include "areachartitem_p.h" | ||
#include "qareaseries.h" | ||||
|
r938 | #include "qareaseries_p.h" | ||
|
r421 | #include "qlineseries.h" | ||
|
r564 | #include "chartpresenter_p.h" | ||
|
r421 | #include <QPainter> | ||
|
r571 | #include <QGraphicsSceneMouseEvent> | ||
|
r421 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
//TODO: optimize : remove points which are not visible | ||||
|
r761 | AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter) | ||
: ChartItem(presenter), | ||||
|
r743 | m_series(areaSeries), | ||
m_upper(0), | ||||
m_lower(0), | ||||
m_pointsVisible(false) | ||||
|
r421 | { | ||
|
r564 | setZValue(ChartPresenter::LineChartZValue); | ||
|
r690 | m_upper = new AreaBoundItem(this,m_series->upperSeries()); | ||
|
r761 | if (m_series->lowerSeries()) | ||
|
r743 | m_lower = new AreaBoundItem(this,m_series->lowerSeries()); | ||
|
r421 | |||
|
r938 | connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); | ||
|
r967 | connect(this,SIGNAL(clicked(QPointF)),areaSeries,SIGNAL(clicked(QPointF))); | ||
|
r421 | |||
handleUpdated(); | ||||
} | ||||
AreaChartItem::~AreaChartItem() | ||||
{ | ||||
delete m_upper; | ||||
delete m_lower; | ||||
|
r761 | } | ||
|
r421 | |||
QRectF AreaChartItem::boundingRect() const | ||||
{ | ||||
return m_rect; | ||||
} | ||||
QPainterPath AreaChartItem::shape() const | ||||
{ | ||||
return m_path; | ||||
} | ||||
void AreaChartItem::updatePath() | ||||
{ | ||||
QPainterPath path; | ||||
|
r690 | path = m_upper->shape(); | ||
|
r743 | if (m_lower) { | ||
path.connectPath(m_lower->shape().toReversed()); | ||||
} else { | ||||
|
r421 | QPointF first = path.pointAtPercent(0); | ||
QPointF last = path.pointAtPercent(1); | ||||
path.lineTo(last.x(),m_clipRect.bottom()); | ||||
path.lineTo(first.x(),m_clipRect.bottom()); | ||||
} | ||||
path.closeSubpath(); | ||||
prepareGeometryChange(); | ||||
|
r761 | m_path = path; | ||
m_rect = path.boundingRect(); | ||||
|
r421 | update(); | ||
} | ||||
void AreaChartItem::handleUpdated() | ||||
{ | ||||
|
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()); | ||
|
r563 | |||
|
r421 | update(); | ||
} | ||||
|
r439 | void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | ||
|
r421 | { | ||
|
r439 | m_upper->handleDomainChanged(minX,maxX,minY,maxY); | ||
|
r743 | if (m_lower) | ||
m_lower->handleDomainChanged(minX,maxX,minY,maxY); | ||||
|
r421 | } | ||
|
r743 | void AreaChartItem::handleGeometryChanged(const QRectF &rect) | ||
|
r421 | { | ||
m_clipRect=rect.translated(-rect.topLeft()); | ||||
setPos(rect.topLeft()); | ||||
m_upper->handleGeometryChanged(rect); | ||||
|
r743 | if (m_lower) | ||
m_lower->handleGeometryChanged(rect); | ||||
|
r421 | } | ||
void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
|
r611 | Q_UNUSED(widget) | ||
Q_UNUSED(option) | ||||
|
r421 | painter->save(); | ||
|
r690 | painter->setPen(m_linePen); | ||
painter->setBrush(m_brush); | ||||
|
r421 | painter->setClipRect(m_clipRect); | ||
painter->drawPath(m_path); | ||||
|
r743 | if (m_pointsVisible) { | ||
|
r563 | painter->setPen(m_pointPen); | ||
painter->drawPoints(m_upper->points()); | ||||
|
r743 | if (m_lower) | ||
painter->drawPoints(m_lower->points()); | ||||
|
r563 | } | ||
|
r421 | painter->restore(); | ||
} | ||||
|
r743 | void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
|
r571 | { | ||
emit clicked(m_upper->calculateDomainPoint(event->pos())); | ||||
} | ||||
|
r421 | #include "moc_areachartitem_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||