xylinechartitem.cpp
202 lines
| 4.7 KiB
| text/x-c
|
CppLexer
|
r21 | #include "xylinechartitem_p.h" | ||
#include "qxychartseries.h" | ||||
|
r131 | #include "chartpresenter_p.h" | ||
|
r21 | #include <QPainter> | ||
#include <QStyleOptionGraphicsItem> | ||||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r21 | |||
|
r131 | XYLineChartItem::XYLineChartItem(ChartPresenter* presenter, QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent), | ||
m_presenter(presenter), | ||||
m_series(series), | ||||
m_dirtyData(false), | ||||
m_dirtyGeometry(false), | ||||
m_dirtyDomain(false) | ||||
|
r21 | { | ||
} | ||||
|
r131 | QRectF XYLineChartItem::boundingRect() const | ||
|
r103 | { | ||
|
r131 | return m_rect; | ||
|
r103 | } | ||
|
r85 | |||
|
r131 | QPainterPath XYLineChartItem::shape() const | ||
|
r67 | { | ||
|
r131 | return m_path; | ||
} | ||||
|
r85 | |||
|
r131 | |||
void XYLineChartItem::addPoints(const QVector<QPointF>& points) | ||||
{ | ||||
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); | ||||
item->setPos(point.x()-1,point.y()-1); | ||||
m_points << item; | ||||
} | ||||
} | ||||
void XYLineChartItem::addPoint(const QPointF& point) | ||||
{ | ||||
m_data << point; | ||||
QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); | ||||
item->setPos(point.x()-1,point.y()-1); | ||||
m_points << item; | ||||
} | ||||
void XYLineChartItem::removePoint(const QPointF& point) | ||||
{ | ||||
Q_ASSERT(m_data.count() == m_points.count()); | ||||
int index = m_data.lastIndexOf(point,0); | ||||
m_data.remove(index); | ||||
delete(m_points.takeAt(index)); | ||||
} | ||||
void XYLineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint) | ||||
{ | ||||
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); | ||||
} | ||||
} | ||||
void XYLineChartItem::setPoint(int index,const QPointF& point) | ||||
{ | ||||
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); | ||||
} | ||||
void XYLineChartItem::clear() | ||||
{ | ||||
qDeleteAll(m_points); | ||||
m_points.clear(); | ||||
m_hash.clear(); | ||||
m_path = QPainterPath(); | ||||
m_rect = QRect(); | ||||
m_data.clear(); | ||||
} | ||||
void XYLineChartItem::clearView() | ||||
{ | ||||
qDeleteAll(m_points); | ||||
m_points.clear(); | ||||
m_path = QPainterPath(); | ||||
m_rect = QRect(); | ||||
m_data.clear(); | ||||
} | ||||
|
r108 | void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
|
r131 | Q_UNUSED(widget); | ||
Q_UNUSED(option); | ||||
|
r139 | painter->setPen(m_pen); | ||
|
r131 | painter->drawPath(m_path); | ||
|
r67 | } | ||
|
r108 | |||
|
r131 | void XYLineChartItem::calculatePoint(QPointF& point, int index, const QXYChartSeries* 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 | |||
void XYLineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QXYChartSeries* series,const QSizeF& size, const Domain& domain) const | ||||
{ | ||||
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; | ||||
if(x<0 || x > size.width()) continue; | ||||
qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height(); | ||||
if(y<0 || y > size.height()) continue; | ||||
hash[i] = points.size(); | ||||
points << QPointF(x,y); | ||||
} | ||||
} | ||||
void XYLineChartItem::updateDomain() | ||||
{ | ||||
clear(); | ||||
calculatePoints(m_data,m_hash,m_series,m_size, m_domain); | ||||
addPoints(m_data); | ||||
} | ||||
void XYLineChartItem::updateData() | ||||
{ | ||||
//for now the same | ||||
updateDomain(); | ||||
} | ||||
|
r67 | void XYLineChartItem::updateGeometry() | ||
{ | ||||
|
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 | } | ||
|
r139 | void XYLineChartItem::setPen(const QPen& pen) | ||
|
r131 | { | ||
|
r139 | m_pen = pen; | ||
} | ||||
|
r131 | |||
|
r139 | //handlers | ||
|
r131 | |||
|
r139 | void XYLineChartItem::handleModelChanged(int index) | ||
{ | ||||
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 | |||
|
r139 | void XYLineChartItem::handleDomainChanged(const Domain& domain) | ||
{ | ||||
m_domain = domain; | ||||
updateDomain(); | ||||
|
r131 | update(); | ||
} | ||||
|
r139 | void XYLineChartItem::handleGeometryChanged(const QRectF& rect) | ||
{ | ||||
Q_ASSERT(rect.isValid()); | ||||
m_size=rect.size(); | ||||
updateDomain(); | ||||
updateGeometry(); | ||||
setPos(rect.topLeft()); | ||||
update(); | ||||
} | ||||
|
r131 | #include "moc_xylinechartitem_p.cpp" | ||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||