splinechartitem.cpp
190 lines
| 5.3 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
|
r1218 | ** | ||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r794 | |||
|
r460 | #include "splinechartitem_p.h" | ||
|
r938 | #include "qsplineseries_p.h" | ||
|
r476 | #include "chartpresenter_p.h" | ||
|
r1735 | #include "splineanimation_p.h" | ||
|
r401 | #include <QPainter> | ||
|
r1218 | #include <QGraphicsSceneMouseEvent> | ||
|
r295 | |||
|
r401 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r2097 | SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) | ||
: XYChart(series, presenter), | ||||
QGraphicsItem(presenter ? presenter->rootItem() : 0), | ||||
m_series(series), | ||||
m_pointsVisible(false), | ||||
m_animation(0) | ||||
|
r295 | { | ||
|
r2256 | setAcceptHoverEvents(true); | ||
|
r1747 | setZValue(ChartPresenter::SplineChartZValue); | ||
|
r2097 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
|
r1342 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
|
r2067 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | ||
|
r476 | handleUpdated(); | ||
|
r295 | } | ||
|
r465 | QRectF SplineChartItem::boundingRect() const | ||
{ | ||||
return m_rect; | ||||
} | ||||
|
r295 | |||
|
r465 | QPainterPath SplineChartItem::shape() const | ||
{ | ||||
|
r2256 | QPainterPathStroker stroker; | ||
return stroker.createStroke(m_path); | ||||
|
r465 | } | ||
|
r401 | |||
|
r2104 | void SplineChartItem::setAnimation(SplineAnimation *animation) | ||
|
r1217 | { | ||
|
r2097 | m_animation = animation; | ||
|
r1218 | XYChart::setAnimation(animation); | ||
|
r1217 | } | ||
|
r2104 | ChartAnimation *SplineChartItem::animation() const | ||
|
r1735 | { | ||
return m_animation; | ||||
} | ||||
|
r1217 | void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points) | ||
{ | ||||
|
r2097 | m_controlPoints = points; | ||
|
r1217 | } | ||
QVector<QPointF> SplineChartItem::controlGeometryPoints() const | ||||
{ | ||||
return m_controlPoints; | ||||
} | ||||
|
r2097 | void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index) | ||
|
r1082 | { | ||
|
r622 | QVector<QPointF> controlPoints; | ||
|
r2097 | if (newPoints.count() >= 2) | ||
controlPoints.resize(newPoints.count() * 2 - 2); | ||||
|
r622 | |||
|
r743 | for (int i = 0; i < newPoints.size() - 1; i++) { | ||
|
r2097 | controlPoints[2 * i] = calculateGeometryControlPoint(2 * i); | ||
|
r622 | controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); | ||
} | ||||
|
r1782 | if (m_animation) | ||
m_animation->setup(oldPoints, newPoints, m_controlPoints, controlPoints, index); | ||||
|
r622 | |||
|
r1782 | m_points = newPoints; | ||
m_controlPoints = controlPoints; | ||||
|
r1770 | setDirty(false); | ||
|
r2097 | if (m_animation) | ||
|
r1241 | presenter()->startAnimation(m_animation); | ||
|
r2097 | else | ||
|
r1217 | updateGeometry(); | ||
|
r622 | } | ||
|
r460 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const | ||
|
r401 | { | ||
|
r1218 | return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index)); | ||
|
r419 | } | ||
|
r1217 | void SplineChartItem::updateGeometry() | ||
|
r946 | { | ||
|
r1782 | const QVector<QPointF> &points = m_points; | ||
const QVector<QPointF> &controlPoints = m_controlPoints; | ||||
|
r946 | |||
|
r2097 | if ((points.size() < 2) || (controlPoints.size() < 2)) { | ||
prepareGeometryChange(); | ||||
|
r1085 | m_path = QPainterPath(); | ||
|
r1271 | m_rect = QRect(); | ||
|
r545 | return; | ||
} | ||||
|
r423 | |||
|
r2097 | Q_ASSERT(points.count() * 2 - 2 == controlPoints.count()); | ||
|
r622 | |||
QPainterPath splinePath(points.at(0)); | ||||
|
r423 | |||
|
r743 | for (int i = 0; i < points.size() - 1; i++) { | ||
|
r2104 | const QPointF &point = points.at(i + 1); | ||
|
r2097 | splinePath.cubicTo(controlPoints[2 * i], controlPoints[2 * i + 1], point); | ||
|
r423 | } | ||
prepareGeometryChange(); | ||||
|
r2256 | // QPainterPathStroker stroker; | ||
// stroker.setWidth(m_linePen.width() / 2.0); | ||||
// m_path = stroker.createStroke(splinePath); | ||||
|
r423 | m_path = splinePath; | ||
m_rect = splinePath.boundingRect(); | ||||
|
r1218 | setPos(origin()); | ||
|
r465 | } | ||
|
r476 | //handlers | ||
void SplineChartItem::handleUpdated() | ||||
{ | ||||
|
r1346 | setVisible(m_series->isVisible()); | ||
|
r2067 | setOpacity(m_series->opacity()); | ||
|
r580 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
m_pointPen = m_series->pen(); | ||||
|
r2097 | m_pointPen.setWidthF(2 * m_pointPen.width()); | ||
|
r476 | update(); | ||
} | ||||
//painter | ||||
|
r465 | |||
|
r460 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
|
r423 | { | ||
|
r611 | Q_UNUSED(widget) | ||
Q_UNUSED(option) | ||||
|
r1346 | |||
painter->save(); | ||||
painter->setPen(m_linePen); | ||||
|
r2256 | // painter->setBrush(m_linePen.color()); | ||
painter->setClipRect(clipRect()); | ||||
|
r1346 | painter->drawPath(m_path); | ||
if (m_pointsVisible) { | ||||
painter->setPen(m_pointPen); | ||||
painter->drawPoints(geometryPoints()); | ||||
|
r580 | } | ||
|
r1346 | painter->restore(); | ||
|
r423 | } | ||
|
r417 | |||
|
r1218 | void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
emit XYChart::clicked(calculateDomainPoint(event->pos())); | ||||
|
r1747 | QGraphicsItem::mousePressEvent(event); | ||
|
r1218 | } | ||
|
r476 | |||
|
r2256 | void SplineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | ||
{ | ||||
emit XYChart::hovered(calculateDomainPoint(event->pos()), true); | ||||
event->accept(); | ||||
} | ||||
void SplineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
emit XYChart::hovered(calculateDomainPoint(event->pos()), false); | ||||
event->accept(); | ||||
} | ||||
|
r460 | #include "moc_splinechartitem_p.cpp" | ||
|
r401 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||