splinechartitem.cpp
133 lines
| 3.6 KiB
| text/x-c
|
CppLexer
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r460 | #include "splinechartitem_p.h" | ||
Michal Klocek
|
r476 | #include "chartpresenter_p.h" | ||
Michal Klocek
|
r622 | #include "chartanimator_p.h" | ||
Marek Rosa
|
r401 | #include <QPainter> | ||
Marek Rosa
|
r295 | |||
Marek Rosa
|
r401 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
sauimone
|
r743 | SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) : | ||
XYChartItem(series, presenter), | ||||
m_series(series), | ||||
m_pointsVisible(false) | ||||
Marek Rosa
|
r295 | { | ||
Michal Klocek
|
r476 | setZValue(ChartPresenter::LineChartZValue); | ||
QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); | ||||
handleUpdated(); | ||||
Marek Rosa
|
r295 | } | ||
Michal Klocek
|
r465 | QRectF SplineChartItem::boundingRect() const | ||
{ | ||||
return m_rect; | ||||
} | ||||
Marek Rosa
|
r295 | |||
Michal Klocek
|
r465 | QPainterPath SplineChartItem::shape() const | ||
{ | ||||
return m_path; | ||||
} | ||||
Marek Rosa
|
r401 | |||
sauimone
|
r743 | void SplineChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index) | ||
Michal Klocek
|
r622 | { | ||
QVector<QPointF> controlPoints; | ||||
controlPoints.resize(newPoints.count()*2-2); | ||||
sauimone
|
r743 | for (int i = 0; i < newPoints.size() - 1; i++) { | ||
Michal Klocek
|
r622 | controlPoints[2*i] = calculateGeometryControlPoint(2 * i); | ||
controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); | ||||
} | ||||
sauimone
|
r743 | if (controlPoints.count()<2) { | ||
Michal Klocek
|
r622 | setLayout(newPoints,controlPoints); | ||
return; | ||||
} | ||||
sauimone
|
r743 | if (animator()) { | ||
Michal Klocek
|
r677 | animator()->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); | ||
sauimone
|
r743 | } else { | ||
Michal Klocek
|
r622 | setLayout(newPoints,controlPoints); | ||
} | ||||
} | ||||
Marek Rosa
|
r460 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const | ||
Marek Rosa
|
r401 | { | ||
Michal Klocek
|
r465 | return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); | ||
Marek Rosa
|
r419 | } | ||
sauimone
|
r743 | void SplineChartItem::setLayout(QVector<QPointF> &points, QVector<QPointF> &controlPoints) | ||
Marek Rosa
|
r419 | { | ||
sauimone
|
r743 | if ((points.size()<2) || (controlPoints.size()<2)) { | ||
Michal Klocek
|
r557 | XYChartItem::setLayout(points); | ||
Michal Klocek
|
r622 | m_controlPoints=controlPoints; | ||
Marek Rosa
|
r545 | return; | ||
} | ||||
Marek Rosa
|
r423 | |||
Michal Klocek
|
r622 | Q_ASSERT(points.count()*2-2 == controlPoints.count()); | ||
QPainterPath splinePath(points.at(0)); | ||||
Marek Rosa
|
r423 | |||
sauimone
|
r743 | for (int i = 0; i < points.size() - 1; i++) { | ||
Marek Rosa
|
r423 | const QPointF& point = points.at(i + 1); | ||
Michal Klocek
|
r622 | splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); | ||
Marek Rosa
|
r423 | } | ||
prepareGeometryChange(); | ||||
m_path = splinePath; | ||||
m_rect = splinePath.boundingRect(); | ||||
Michal Klocek
|
r557 | XYChartItem::setLayout(points); | ||
Michal Klocek
|
r622 | m_controlPoints=controlPoints; | ||
Michal Klocek
|
r465 | } | ||
Michal Klocek
|
r476 | //handlers | ||
void SplineChartItem::handleUpdated() | ||||
{ | ||||
Michal Klocek
|
r580 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
m_pointPen = m_series->pen(); | ||||
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) | ||||
Marek Rosa
|
r423 | painter->save(); | ||
Michal Klocek
|
r465 | painter->setClipRect(clipRect()); | ||
Michal Klocek
|
r580 | painter->setPen(m_linePen); | ||
Marek Rosa
|
r423 | painter->drawPath(m_path); | ||
sauimone
|
r743 | if (m_pointsVisible) { | ||
painter->setPen(m_pointPen); | ||||
painter->drawPoints(points()); | ||||
Michal Klocek
|
r580 | } | ||
Marek Rosa
|
r423 | painter->restore(); | ||
} | ||||
Marek Rosa
|
r417 | |||
Michal Klocek
|
r476 | |||
Marek Rosa
|
r460 | #include "moc_splinechartitem_p.cpp" | ||
Marek Rosa
|
r401 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||