linechartitem.cpp
210 lines
| 5.0 KiB
| text/x-c
|
CppLexer
|
r144 | #include "linechartitem_p.h" | ||
#include "qlinechartseries.h" | ||||
|
r131 | #include "chartpresenter_p.h" | ||
|
r21 | #include <QPainter> | ||
|
r144 | |||
|
r21 | |||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r21 | |||
|
r150 | //TODO: optimazie : remove points which are not visible | ||
|
r144 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent), | ||
|
r131 | m_presenter(presenter), | ||
m_series(series), | ||||
m_dirtyData(false), | ||||
m_dirtyGeometry(false), | ||||
m_dirtyDomain(false) | ||||
|
r21 | { | ||
|
r262 | setZValue(ChartPresenter::LineChartZValue); | ||
|
r21 | } | ||
|
r144 | QRectF LineChartItem::boundingRect() const | ||
|
r103 | { | ||
|
r131 | return m_rect; | ||
|
r103 | } | ||
|
r85 | |||
|
r144 | QPainterPath LineChartItem::shape() const | ||
|
r67 | { | ||
|
r131 | return m_path; | ||
} | ||||
|
r85 | |||
|
r131 | |||
|
r144 | void LineChartItem::addPoints(const QVector<QPointF>& points) | ||
|
r131 | { | ||
m_data = points; | ||||
for(int i=0; i<m_data.size();i++){ | ||||
const QPointF& point =m_data[i]; | ||||
QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); | ||||
|
r150 | item->setPos(point.x()-1,point.y()-1);; | ||
|
r185 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); | ||
|
r131 | m_points << item; | ||
} | ||||
} | ||||
|
r144 | void LineChartItem::addPoint(const QPointF& point) | ||
|
r131 | { | ||
m_data << point; | ||||
QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); | ||||
|
r150 | m_clipRect.contains(point); | ||
|
r131 | item->setPos(point.x()-1,point.y()-1); | ||
|
r185 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); | ||
|
r131 | m_points << item; | ||
} | ||||
|
r144 | void LineChartItem::removePoint(const QPointF& point) | ||
|
r131 | { | ||
Q_ASSERT(m_data.count() == m_points.count()); | ||||
int index = m_data.lastIndexOf(point,0); | ||||
m_data.remove(index); | ||||
delete(m_points.takeAt(index)); | ||||
} | ||||
|
r144 | void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint) | ||
|
r131 | { | ||
Q_ASSERT(m_data.count() == m_points.count()); | ||||
int index = m_data.lastIndexOf(oldPoint,0); | ||||
if(index > -1){ | ||||
m_data.replace(index,newPoint); | ||||
QGraphicsItem* item = m_points.at(index); | ||||
item->setPos(newPoint.x()-1,newPoint.y()-1); | ||||
} | ||||
} | ||||
|
r144 | void LineChartItem::setPoint(int index,const QPointF& point) | ||
|
r131 | { | ||
Q_ASSERT(m_data.count() == m_points.count()); | ||||
Q_ASSERT(index>=0); | ||||
m_data.replace(index,point); | ||||
QGraphicsItem* item = m_points.at(index); | ||||
item->setPos(point.x()-1,point.y()-1); | ||||
} | ||||
|
r144 | void LineChartItem::clear() | ||
|
r131 | { | ||
qDeleteAll(m_points); | ||||
m_points.clear(); | ||||
m_hash.clear(); | ||||
m_path = QPainterPath(); | ||||
m_rect = QRect(); | ||||
m_data.clear(); | ||||
} | ||||
|
r144 | void LineChartItem::clearView() | ||
|
r131 | { | ||
qDeleteAll(m_points); | ||||
m_points.clear(); | ||||
m_path = QPainterPath(); | ||||
m_rect = QRect(); | ||||
m_data.clear(); | ||||
} | ||||
|
r144 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
|
r108 | { | ||
|
r213 | Q_UNUSED(widget); | ||
|
r131 | Q_UNUSED(option); | ||
|
r213 | painter->save(); | ||
|
r139 | painter->setPen(m_pen); | ||
|
r150 | painter->setClipRect(m_clipRect); | ||
|
r213 | painter->drawPath(m_path); | ||
painter->restore(); | ||||
|
r67 | } | ||
|
r108 | |||
|
r144 | void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const | ||
|
r85 | { | ||
|
r131 | const qreal deltaX = size.width()/domain.spanX(); | ||
const qreal deltaY = size.height()/domain.spanY(); | ||||
qreal x = (series->x(index) - domain.m_minX)* deltaX; | ||||
qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height(); | ||||
point.setX(x); | ||||
point.setY(y); | ||||
|
r85 | } | ||
|
r131 | |||
|
r144 | void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const | ||
|
r131 | { | ||
const qreal deltaX = size.width()/domain.spanX(); | ||||
const qreal deltaY = size.height()/domain.spanY(); | ||||
for (int i = 0; i < series->count(); ++i) { | ||||
qreal x = (series->x(i) - domain.m_minX)* deltaX; | ||||
qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height(); | ||||
hash[i] = points.size(); | ||||
points << QPointF(x,y); | ||||
} | ||||
} | ||||
|
r144 | void LineChartItem::updateDomain() | ||
|
r131 | { | ||
clear(); | ||||
|
r150 | prepareGeometryChange(); | ||
|
r131 | calculatePoints(m_data,m_hash,m_series,m_size, m_domain); | ||
addPoints(m_data); | ||||
} | ||||
|
r144 | void LineChartItem::updateData() | ||
|
r131 | { | ||
//for now the same | ||||
updateDomain(); | ||||
} | ||||
|
r144 | void LineChartItem::updateGeometry() | ||
|
r67 | { | ||
|
r25 | |||
|
r131 | if(m_data.size()==0) return; | ||
|
r25 | |||
|
r131 | prepareGeometryChange(); | ||
|
r85 | QPainterPath path; | ||
|
r131 | const QPointF& point = m_data.at(0); | ||
path.moveTo(point); | ||||
|
r25 | |||
|
r131 | foreach( const QPointF& point , m_data) { | ||
path.lineTo(point); | ||||
|
r67 | } | ||
|
r21 | |||
|
r131 | m_path = path; | ||
m_rect = path.boundingRect(); | ||||
|
r21 | } | ||
|
r144 | void LineChartItem::setPen(const QPen& pen) | ||
|
r131 | { | ||
|
r139 | m_pen = pen; | ||
} | ||||
|
r131 | |||
|
r139 | //handlers | ||
|
r131 | |||
|
r144 | void LineChartItem::handleModelChanged(int index) | ||
|
r139 | { | ||
Q_ASSERT(index<m_series->count()); | ||||
if(m_hash.contains(index)){ | ||||
int i = m_hash.value(index); | ||||
QPointF point; | ||||
calculatePoint(point,index,m_series,m_size,m_domain); | ||||
setPoint(i,point); | ||||
|
r131 | } | ||
|
r139 | update(); | ||
} | ||||
|
r131 | |||
|
r144 | void LineChartItem::handleDomainChanged(const Domain& domain) | ||
|
r139 | { | ||
m_domain = domain; | ||||
updateDomain(); | ||||
|
r131 | update(); | ||
} | ||||
|
r144 | void LineChartItem::handleGeometryChanged(const QRectF& rect) | ||
|
r139 | { | ||
Q_ASSERT(rect.isValid()); | ||||
m_size=rect.size(); | ||||
|
r150 | m_clipRect=rect.translated(-rect.topLeft()); | ||
|
r139 | updateDomain(); | ||
updateGeometry(); | ||||
setPos(rect.topLeft()); | ||||
update(); | ||||
} | ||||
|
r144 | #include "moc_linechartitem_p.cpp" | ||
|
r131 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||