linechartitem.cpp
219 lines
| 5.0 KiB
| text/x-c
|
CppLexer
Michal Klocek
|
r144 | #include "linechartitem_p.h" | ||
Michal Klocek
|
r349 | #include "qlineseries.h" | ||
Michal Klocek
|
r131 | #include "chartpresenter_p.h" | ||
Michal Klocek
|
r21 | #include <QPainter> | ||
Michal Klocek
|
r144 | |||
Michal Klocek
|
r21 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Michal Klocek
|
r21 | |||
Michal Klocek
|
r391 | //TODO: optimize : remove points which are not visible | ||
Michal Klocek
|
r150 | |||
Michal Klocek
|
r349 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent), | ||
Michal Klocek
|
r131 | m_presenter(presenter), | ||
Michal Klocek
|
r391 | m_series(series), | ||
m_items(this) | ||||
Michal Klocek
|
r21 | { | ||
Michal Klocek
|
r391 | //m_items.setZValue(ChartPresenter::LineChartZValue); | ||
Michal Klocek
|
r262 | setZValue(ChartPresenter::LineChartZValue); | ||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r144 | QRectF LineChartItem::boundingRect() const | ||
Tero Ahola
|
r103 | { | ||
Michal Klocek
|
r131 | return m_rect; | ||
Tero Ahola
|
r103 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r144 | QPainterPath LineChartItem::shape() const | ||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
r131 | return m_path; | ||
} | ||||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r389 | void LineChartItem::createPoints(int count) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r389 | for (int i = 0; i < count; ++i) { | ||
Michal Klocek
|
r391 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3); | ||
Michal Klocek
|
r389 | m_items.addToGroup(item); | ||
Michal Klocek
|
r131 | } | ||
} | ||||
Michal Klocek
|
r389 | void LineChartItem::clearPoints(int count) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r389 | QList<QGraphicsItem *> items = m_items.childItems(); | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r389 | for (int i = 0; i < count; ++i) { | ||
delete(items.takeLast()); | ||||
Michal Klocek
|
r131 | } | ||
} | ||||
Michal Klocek
|
r391 | QPointF LineChartItem::calculateGeometryPoint(int index) const | ||
Michal Klocek
|
r85 | { | ||
Michal Klocek
|
r389 | const qreal deltaX = m_size.width()/m_domain.spanX(); | ||
const qreal deltaY = m_size.height()/m_domain.spanY(); | ||||
qreal x = (m_series->x(index) - m_domain.m_minX)* deltaX; | ||||
qreal y = (m_series->y(index) - m_domain.m_minY)*-deltaY + m_size.height(); | ||||
return QPointF(x,y); | ||||
Michal Klocek
|
r85 | } | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r391 | QVector<QPointF> LineChartItem::calculateGeometryPoints() const | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r389 | const qreal deltaX = m_size.width()/m_domain.spanX(); | ||
const qreal deltaY = m_size.height()/m_domain.spanY(); | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r391 | QVector<QPointF> points; | ||
points.reserve(m_series->count()); | ||||
Michal Klocek
|
r389 | for (int i = 0; i < m_series->count(); ++i) { | ||
qreal x = (m_series->x(i) - m_domain.m_minX)* deltaX; | ||||
qreal y = (m_series->y(i) - m_domain.m_minY)*-deltaY + m_size.height(); | ||||
Michal Klocek
|
r131 | points << QPointF(x,y); | ||
} | ||||
Michal Klocek
|
r391 | return points; | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r391 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r391 | applyGeometry(newPoints); | ||
oldPoints = newPoints; | ||||
} | ||||
Michal Klocek
|
r389 | |||
Michal Klocek
|
r391 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint) | ||
{ | ||||
oldPoints.replace(index,newPoint); | ||||
applyGeometry(oldPoints); | ||||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r389 | void LineChartItem::applyGeometry(QVector<QPointF>& points) | ||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
r389 | if(points.size()==0) return; | ||
Michal Klocek
|
r25 | |||
Michal Klocek
|
r389 | QList<QGraphicsItem*> items = m_items.childItems(); | ||
Michal Klocek
|
r25 | |||
Michal Klocek
|
r85 | QPainterPath path; | ||
Michal Klocek
|
r389 | const QPointF& point = points.at(0); | ||
Michal Klocek
|
r131 | path.moveTo(point); | ||
Michal Klocek
|
r389 | QGraphicsItem* item = items.at(0); | ||
item->setPos(point.x()-1,point.y()-1); | ||||
Michal Klocek
|
r391 | if(!m_clipRect.contains(point)) item->setVisible(false); | ||
Michal Klocek
|
r389 | |||
for(int i=1 ; i< points.size();i++) { | ||||
QGraphicsItem* item = items.at(i); | ||||
const QPointF& point = points.at(i); | ||||
item->setPos(point.x()-1,point.y()-1); | ||||
if(!m_clipRect.contains(point)) item->setVisible(false); | ||||
Michal Klocek
|
r131 | path.lineTo(point); | ||
Michal Klocek
|
r67 | } | ||
Michal Klocek
|
r21 | |||
Michal Klocek
|
r389 | prepareGeometryChange(); | ||
Michal Klocek
|
r131 | m_path = path; | ||
m_rect = path.boundingRect(); | ||||
Michal Klocek
|
r21 | } | ||
Michal Klocek
|
r144 | void LineChartItem::setPen(const QPen& pen) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r139 | m_pen = pen; | ||
} | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r139 | //handlers | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r389 | void LineChartItem::handlePointAdded(int index) | ||
Michal Klocek
|
r139 | { | ||
Q_ASSERT(index<m_series->count()); | ||||
Michal Klocek
|
r389 | Q_ASSERT(index>=0); | ||
Michal Klocek
|
r391 | QPointF point = calculateGeometryPoint(index); | ||
createPoints(1); | ||||
QVector<QPointF> points = m_points; | ||||
points.insert(index,point); | ||||
updateItem(m_points,points); | ||||
update(); | ||||
Michal Klocek
|
r389 | } | ||
void LineChartItem::handlePointRemoved(int index) | ||||
{ | ||||
Q_ASSERT(index<m_series->count()); | ||||
Q_ASSERT(index>=0); | ||||
Michal Klocek
|
r391 | QPointF point = calculateGeometryPoint(index); | ||
clearPoints(1); | ||||
QVector<QPointF> points = m_points; | ||||
points.remove(index); | ||||
updateItem(m_points,points); | ||||
update(); | ||||
Michal Klocek
|
r389 | } | ||
void LineChartItem::handlePointReplaced(int index) | ||||
{ | ||||
Q_ASSERT(index<m_series->count()); | ||||
Q_ASSERT(index>=0); | ||||
Michal Klocek
|
r391 | QPointF point = calculateGeometryPoint(index); | ||
updateItem(m_points,index,point); | ||||
Michal Klocek
|
r139 | update(); | ||
} | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r144 | void LineChartItem::handleDomainChanged(const Domain& domain) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r391 | |||
Michal Klocek
|
r139 | m_domain = domain; | ||
Michal Klocek
|
r391 | |||
Michal Klocek
|
r389 | if(m_domain.isEmpty()) return; | ||
Michal Klocek
|
r391 | if(!m_clipRect.isValid()) return; | ||
Michal Klocek
|
r389 | |||
Michal Klocek
|
r391 | QVector<QPointF> points = calculateGeometryPoints(); | ||
int diff = m_points.size() - points.size(); | ||||
if(diff>0) { | ||||
clearPoints(diff); | ||||
} | ||||
else if(diff<0) { | ||||
createPoints(-diff); | ||||
} | ||||
updateItem(m_points,points); | ||||
Michal Klocek
|
r131 | update(); | ||
} | ||||
Michal Klocek
|
r144 | void LineChartItem::handleGeometryChanged(const QRectF& rect) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r391 | Q_ASSERT(rect.isValid()); | ||
m_size=rect.size(); | ||||
m_clipRect=rect.translated(-rect.topLeft()); | ||||
setPos(rect.topLeft()); | ||||
Michal Klocek
|
r389 | |||
Michal Klocek
|
r391 | if(m_domain.isEmpty()) return; | ||
Michal Klocek
|
r389 | |||
Michal Klocek
|
r391 | QVector<QPointF> points = calculateGeometryPoints(); | ||
int diff = m_points.size() - points.size(); | ||||
if(diff>0) { | ||||
clearPoints(diff); | ||||
} | ||||
else if(diff<0) { | ||||
createPoints(-diff); | ||||
} | ||||
updateItem(m_points,points); | ||||
update(); | ||||
Michal Klocek
|
r389 | } | ||
Michal Klocek
|
r392 | void LineChartItem::handleUpdated() | ||
Michal Klocek
|
r389 | { | ||
m_items.setVisible(m_series->pointsVisible()); | ||||
setPen(m_series->pen()); | ||||
Michal Klocek
|
r139 | update(); | ||
} | ||||
Michal Klocek
|
r391 | //painter | ||
void LineChartItem::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); | ||||
painter->restore(); | ||||
} | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r144 | #include "moc_linechartitem_p.cpp" | ||
Michal Klocek
|
r131 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||