splinechartitem.cpp
89 lines
| 2.0 KiB
| text/x-c
|
CppLexer
|
r460 | #include "splinechartitem_p.h" | ||
|
r476 | #include "chartpresenter_p.h" | ||
|
r401 | #include <QPainter> | ||
|
r295 | |||
|
r401 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r476 | SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) : | ||
|
r465 | XYChartItem(series, parent), | ||
|
r580 | m_series(series), | ||
m_pointsVisible(false) | ||||
|
r295 | { | ||
|
r476 | setZValue(ChartPresenter::LineChartZValue); | ||
QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); | ||||
handleUpdated(); | ||||
|
r295 | } | ||
|
r465 | QRectF SplineChartItem::boundingRect() const | ||
{ | ||||
return m_rect; | ||||
} | ||||
|
r295 | |||
|
r465 | QPainterPath SplineChartItem::shape() const | ||
{ | ||||
return m_path; | ||||
} | ||||
|
r401 | |||
|
r460 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const | ||
|
r401 | { | ||
|
r465 | return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); | ||
|
r419 | } | ||
|
r557 | void SplineChartItem::setLayout(QVector<QPointF>& points) | ||
|
r419 | { | ||
|
r476 | |||
|
r545 | if(points.size()==0) | ||
{ | ||||
|
r557 | XYChartItem::setLayout(points); | ||
|
r545 | return; | ||
} | ||||
|
r423 | |||
QPainterPath splinePath; | ||||
const QPointF& point = points.at(0); | ||||
splinePath.moveTo(point); | ||||
|
r434 | for (int i = 0; i < points.size() - 1; i++) | ||
|
r423 | { | ||
const QPointF& point = points.at(i + 1); | ||||
splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point); | ||||
} | ||||
prepareGeometryChange(); | ||||
m_path = splinePath; | ||||
m_rect = splinePath.boundingRect(); | ||||
|
r557 | XYChartItem::setLayout(points); | ||
|
r465 | } | ||
|
r476 | //handlers | ||
void SplineChartItem::handleUpdated() | ||||
{ | ||||
|
r580 | m_pointsVisible = m_series->pointsVisible(); | ||
m_linePen = m_series->pen(); | ||||
m_pointPen = m_series->pen(); | ||||
m_pointPen.setWidthF(2*m_pointPen.width()); | ||||
|
r476 | update(); | ||
} | ||||
//painter | ||||
|
r465 | |||
|
r460 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
|
r423 | { | ||
Q_UNUSED(widget); | ||||
Q_UNUSED(option); | ||||
painter->save(); | ||||
|
r465 | painter->setClipRect(clipRect()); | ||
|
r580 | painter->setPen(m_linePen); | ||
|
r423 | painter->drawPath(m_path); | ||
|
r580 | if(m_pointsVisible){ | ||
painter->setPen(m_pointPen); | ||||
painter->drawPoints(points()); | ||||
} | ||||
|
r423 | painter->restore(); | ||
} | ||||
|
r417 | |||
|
r476 | |||
|
r460 | #include "moc_splinechartitem_p.cpp" | ||
|
r401 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||