From f5ae8394e133efb45ec6ab198a08775e77fa9ab3 2012-02-09 09:45:09 From: Michal Klocek Date: 2012-02-09 09:45:09 Subject: [PATCH] Fix naming convention for lineseries * rename QXYChartSeries to QLineChartSeries * rename XYChartItem to LineChartItem --- diff --git a/src/xylinechart/linechartanimationitem.cpp b/src/linechart/linechartanimationitem.cpp similarity index 100% rename from src/xylinechart/linechartanimationitem.cpp rename to src/linechart/linechartanimationitem.cpp diff --git a/src/xylinechart/linechartanimationitem_p.h b/src/linechart/linechartanimationitem_p.h similarity index 100% rename from src/xylinechart/linechartanimationitem_p.h rename to src/linechart/linechartanimationitem_p.h diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp new file mode 100644 index 0000000..b4beb6f --- /dev/null +++ b/src/linechart/linechartitem.cpp @@ -0,0 +1,202 @@ +#include "linechartitem_p.h" +#include "qlinechartseries.h" +#include "chartpresenter_p.h" +#include + + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent), +m_presenter(presenter), +m_series(series), +m_dirtyData(false), +m_dirtyGeometry(false), +m_dirtyDomain(false) +{ +} + +QRectF LineChartItem::boundingRect() const +{ + return m_rect; +} + +QPainterPath LineChartItem::shape() const +{ + return m_path; +} + + +void LineChartItem::addPoints(const QVector& points) +{ + m_data = points; + for(int i=0; isetPos(point.x()-1,point.y()-1); + m_points << item; + } +} + +void LineChartItem::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 LineChartItem::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 LineChartItem::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 LineChartItem::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 LineChartItem::clear() +{ + qDeleteAll(m_points); + m_points.clear(); + m_hash.clear(); + m_path = QPainterPath(); + m_rect = QRect(); + m_data.clear(); +} + +void LineChartItem::clearView() +{ + qDeleteAll(m_points); + m_points.clear(); + m_path = QPainterPath(); + m_rect = QRect(); + m_data.clear(); +} + +void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(widget); + Q_UNUSED(option); + painter->setPen(m_pen); + painter->drawPath(m_path); +} + +void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const +{ + 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); +} + + +void LineChartItem::calculatePoints(QVector& points, QHash& hash,const QLineChartSeries* 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 LineChartItem::updateDomain() +{ + clear(); + calculatePoints(m_data,m_hash,m_series,m_size, m_domain); + addPoints(m_data); +} + +void LineChartItem::updateData() +{ + //for now the same + updateDomain(); +} + +void LineChartItem::updateGeometry() +{ + + if(m_data.size()==0) return; + + prepareGeometryChange(); + QPainterPath path; + const QPointF& point = m_data.at(0); + path.moveTo(point); + + foreach( const QPointF& point , m_data) { + path.lineTo(point); + } + + m_path = path; + m_rect = path.boundingRect(); +} + +void LineChartItem::setPen(const QPen& pen) +{ + m_pen = pen; +} + +//handlers + +void LineChartItem::handleModelChanged(int index) +{ + Q_ASSERT(indexcount()); + 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); + } + update(); +} + +void LineChartItem::handleDomainChanged(const Domain& domain) +{ + m_domain = domain; + updateDomain(); + update(); +} + +void LineChartItem::handleGeometryChanged(const QRectF& rect) +{ + Q_ASSERT(rect.isValid()); + + m_size=rect.size(); + updateDomain(); + updateGeometry(); + setPos(rect.topLeft()); + update(); +} + + +#include "moc_linechartitem_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h new file mode 100644 index 0000000..58cfc53 --- /dev/null +++ b/src/linechart/linechartitem_p.h @@ -0,0 +1,72 @@ +#ifndef LINECHARTITEM_H +#define LINECHARTITEM_H + +#include "qchartglobal.h" +#include "chartitem_p.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class ChartPresenter; +class QLineChartSeries; +class LineChartAnimationItem; + +class LineChartItem : public QObject , public ChartItem +{ + Q_OBJECT +public: + LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent = 0); + ~ LineChartItem(){}; + + //from QGraphicsItem + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + QPainterPath shape() const; + + void setPen(const QPen& pen); + const Domain& domain() const { return m_domain;} + + virtual void addPoint(const QPointF& ); + virtual void addPoints(const QVector& points); + virtual void removePoint(const QPointF& point); + virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint); + virtual void setPoint(int index,const QPointF& point); + void clear(); + void clearView(); + int count() const { return m_data.size();} + + const QVector& points(){ return m_data;} + +protected: + virtual void updateGeometry(); + virtual void updateData(); + virtual void updateDomain(); + //refactor + void calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const; + void calculatePoints(QVector& points,QHash& hash,const QLineChartSeries* series, const QSizeF& size, const Domain& domain) const; + +protected slots: + void handleModelChanged(int index); + void handleDomainChanged(const Domain& domain); + void handleGeometryChanged(const QRectF& size); + +private: + ChartPresenter* m_presenter; + QPainterPath m_path; + QSizeF m_size; + QRectF m_rect; + Domain m_domain; + QList m_points; + QVector m_data; + QHash m_hash; + QLineChartSeries* m_series; + QPen m_pen; + bool m_dirtyData; + bool m_dirtyGeometry; + bool m_dirtyDomain; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif diff --git a/src/linechart/qlinechartseries.cpp b/src/linechart/qlinechartseries.cpp new file mode 100644 index 0000000..a102e63 --- /dev/null +++ b/src/linechart/qlinechartseries.cpp @@ -0,0 +1,77 @@ +#include "qlinechartseries.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QLineChartSeries::QLineChartSeries(QObject* parent):QChartSeries(parent) +{ +} + +QLineChartSeries::~QLineChartSeries() +{ +} + +QLineChartSeries* QLineChartSeries::create(QObject* parent) +{ + //TODO: here we take QChartData when it is ready + // return null if malformed; + return new QLineChartSeries(parent); +} + +int QLineChartSeries::add(qreal x,qreal y) +{ + m_x< +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QTCOMMERCIALCHART_EXPORT QLineChartSeries : public QChartSeries +{ + Q_OBJECT +private: + QLineChartSeries(QObject* parent=0); +public: + virtual ~QLineChartSeries(); + +public: // from QChartSeries + static QLineChartSeries* create(QObject* parent=0); + virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;} + +public: + int add(qreal x, qreal y); + void set(int index,qreal x,qreal y); + void clear(); + + void setPen(const QPen& pen); + const QPen& pen() const { return m_pen;} + + int count() const; + qreal x(int pos) const; + qreal y(int pos) const; + friend QDebug operator<< (QDebug d, const QLineChartSeries series); + +signals: + void changed(int index); + +private: + QVector m_x; + QVector m_y; + QPen m_pen; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif diff --git a/src/xylinechart/qxychartseries.cpp b/src/linechart/qxychartseries.cpp similarity index 100% rename from src/xylinechart/qxychartseries.cpp rename to src/linechart/qxychartseries.cpp diff --git a/src/xylinechart/qxychartseries.h b/src/linechart/qxychartseries.h similarity index 100% rename from src/xylinechart/qxychartseries.h rename to src/linechart/qxychartseries.h diff --git a/src/xylinechart/xylinechartitem.cpp b/src/linechart/xylinechartitem.cpp similarity index 100% rename from src/xylinechart/xylinechartitem.cpp rename to src/linechart/xylinechartitem.cpp diff --git a/src/xylinechart/xylinechartitem_p.h b/src/linechart/xylinechartitem_p.h similarity index 100% rename from src/xylinechart/xylinechartitem_p.h rename to src/linechart/xylinechartitem_p.h