splinechartitem.cpp
102 lines
| 2.5 KiB
| text/x-c
|
CppLexer
Marek Rosa
|
r460 | #include "splinechartitem_p.h" | ||
Michal Klocek
|
r476 | #include "chartpresenter_p.h" | ||
Marek Rosa
|
r401 | #include <QPainter> | ||
Marek Rosa
|
r295 | |||
Marek Rosa
|
r401 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Michal Klocek
|
r476 | SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) : | ||
Michal Klocek
|
r465 | XYChartItem(series, parent), | ||
m_series(series) | ||||
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 | |||
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 | } | ||
Michal Klocek
|
r464 | void SplineChartItem::setGeometry(QVector<QPointF>& points) | ||
Marek Rosa
|
r419 | { | ||
Michal Klocek
|
r476 | |||
Marek Rosa
|
r423 | if(points.size()==0) return; | ||
QPainterPath splinePath; | ||||
const QPointF& point = points.at(0); | ||||
splinePath.moveTo(point); | ||||
Marek Rosa
|
r434 | 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(); | ||||
Michal Klocek
|
r465 | XYChartItem::setGeometry(points); | ||
} | ||||
Michal Klocek
|
r476 | void SplineChartItem::setLinePen(const QPen& pen) | ||
Michal Klocek
|
r465 | { | ||
m_pen = pen; | ||||
Marek Rosa
|
r401 | } | ||
Michal Klocek
|
r476 | //handlers | ||
void SplineChartItem::handleUpdated() | ||||
{ | ||||
//m_items.setVisible(m_series->pointsVisible()); | ||||
setLinePen(m_series->pen()); | ||||
update(); | ||||
} | ||||
//painter | ||||
Michal Klocek
|
r465 | |||
Marek Rosa
|
r460 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
Marek Rosa
|
r423 | { | ||
Q_UNUSED(widget); | ||||
Q_UNUSED(option); | ||||
painter->save(); | ||||
Michal Klocek
|
r465 | painter->setClipRect(clipRect()); | ||
Michal Klocek
|
r476 | painter->setPen(m_pen); | ||
Marek Rosa
|
r423 | painter->drawPath(m_path); | ||
Michal Klocek
|
r465 | const QVector<QPointF> points = XYChartItem::points(); | ||
for (int i = 0; i < points.size() - 1; i++) | ||||
Marek Rosa
|
r423 | { | ||
painter->setPen(Qt::red); | ||||
Michal Klocek
|
r465 | painter->drawEllipse(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); | ||||
} | ||||
Michal Klocek
|
r465 | if (points.count() > 0) | ||
Marek Rosa
|
r431 | { | ||
painter->setPen(Qt::red); | ||||
Michal Klocek
|
r465 | painter->drawEllipse(points[points.count() - 1], 2, 2); | ||
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 | ||||