splinepresenter.cpp
75 lines
| 2.3 KiB
| text/x-c
|
CppLexer
Marek Rosa
|
r295 | #include "splinepresenter_p.h" | ||
Marek Rosa
|
r401 | #include <QPainter> | ||
Marek Rosa
|
r295 | |||
Marek Rosa
|
r401 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : | ||||
Michal Klocek
|
r439 | LineChartItem(series, parent)//,m_boundingRect() | ||
Marek Rosa
|
r295 | { | ||
Marek Rosa
|
r431 | // | ||
Marek Rosa
|
r295 | } | ||
Marek Rosa
|
r401 | |||
Marek Rosa
|
r419 | QPointF SplinePresenter::calculateGeometryControlPoint(int index) const | ||
Marek Rosa
|
r401 | { | ||
Marek Rosa
|
r419 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); | ||
Michal Klocek
|
r439 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); | ||
const qreal deltaY = m_size.height()/(m_maxY-m_minY); | ||||
qreal x = (splineSeries->controlPoint(index).x() - m_minX)* deltaX; | ||||
qreal y = (splineSeries->controlPoint(index).y() - m_minY)*-deltaY + m_size.height(); | ||||
Marek Rosa
|
r419 | return QPointF(x,y); | ||
} | ||||
void SplinePresenter::applyGeometry(QVector<QPointF>& points) | ||||
{ | ||||
Marek Rosa
|
r423 | if(points.size()==0) return; | ||
QPainterPath splinePath; | ||||
const QPointF& point = points.at(0); | ||||
splinePath.moveTo(point); | ||||
Marek Rosa
|
r434 | // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); | ||
for (int i = 0; i < points.size() - 1; i++) | ||||
Marek Rosa
|
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(); | ||||
Marek Rosa
|
r401 | } | ||
Marek Rosa
|
r423 | void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Q_UNUSED(widget); | ||||
Q_UNUSED(option); | ||||
painter->save(); | ||||
painter->setPen(m_pen); | ||||
painter->setClipRect(m_clipRect); | ||||
painter->drawPath(m_path); | ||||
QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); | ||||
Marek Rosa
|
r450 | for (int i = 0; i < m_points.size() - 1; i++) | ||
Marek Rosa
|
r423 | { | ||
painter->setPen(Qt::red); | ||||
Marek Rosa
|
r434 | painter->drawEllipse(m_points[i], 2, 2); | ||
Marek Rosa
|
r423 | |||
painter->setPen(Qt::blue); | ||||
// painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); | ||||
// painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); | ||||
Marek Rosa
|
r431 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); | ||
// painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); | ||||
} | ||||
if (m_points.count() > 0) | ||||
{ | ||||
painter->setPen(Qt::red); | ||||
Marek Rosa
|
r434 | painter->drawEllipse(m_points[m_points.count() - 1], 2, 2); | ||
Marek Rosa
|
r423 | } | ||
painter->restore(); | ||||
} | ||||
Marek Rosa
|
r417 | |||
Marek Rosa
|
r401 | #include "moc_splinepresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||