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