From 2b967c4f6e8e6abae637a781621c01f28b965868 2012-03-21 12:46:44 From: Michal Klocek Date: 2012-03-21 12:46:44 Subject: [PATCH] Refactors chartitem * new base chart * unify all series constructors --- diff --git a/examples/linechart/main.cpp b/examples/linechart/main.cpp index 4e1c643..74de58c 100644 --- a/examples/linechart/main.cpp +++ b/examples/linechart/main.cpp @@ -11,7 +11,6 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); //![1] - QLineSeries* series0 = new QLineSeries(); QPen blue(Qt::blue); blue.setWidth(3); diff --git a/src/animations/axisanimation.cpp b/src/animations/axisanimation.cpp index fe07afd..1e0b6dc 100644 --- a/src/animations/axisanimation.cpp +++ b/src/animations/axisanimation.cpp @@ -6,7 +6,7 @@ Q_DECLARE_METATYPE(QVector) QTCOMMERCIALCHART_BEGIN_NAMESPACE -AxisAnimation::AxisAnimation(AxisItem *axis): ChartAnimation(axis), +AxisAnimation::AxisAnimation(Axis *axis): ChartAnimation(axis), m_axis(axis) { } diff --git a/src/animations/axisanimation_p.h b/src/animations/axisanimation_p.h index 3b28bab..2728db2 100644 --- a/src/animations/axisanimation_p.h +++ b/src/animations/axisanimation_p.h @@ -10,13 +10,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class AxisAnimation: public ChartAnimation { public: - AxisAnimation(AxisItem *axis); + AxisAnimation(Axis *axis); ~AxisAnimation(); protected: virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const; virtual void updateCurrentValue (const QVariant & value ); private: - AxisItem* m_axis; + Axis* m_axis; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/chartanimator.cpp b/src/animations/chartanimator.cpp index f9d77be..cebab1e 100644 --- a/src/animations/chartanimator.cpp +++ b/src/animations/chartanimator.cpp @@ -26,7 +26,7 @@ ChartAnimator::~ChartAnimator() { } -void ChartAnimator::addAnimation(AxisItem* item) +void ChartAnimator::addAnimation(Axis* item) { ChartAnimation* animation = m_animations.value(item); @@ -99,13 +99,13 @@ void ChartAnimator::addAnimation(BarChartItem* item) } -void ChartAnimator::removeAnimation(ChartItem* item) +void ChartAnimator::removeAnimation(Chart* item) { item->setAnimator(0); m_animations.remove(item); } -void ChartAnimator::updateLayout(AxisItem* item , QVector& newLayout) +void ChartAnimator::updateLayout(Axis* item , QVector& newLayout) { AxisAnimation* animation = static_cast(m_animations.value(item)); @@ -123,13 +123,13 @@ void ChartAnimator::updateLayout(AxisItem* item , QVector& newLayout) for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--) { - oldLayout[i]= item->axisType()==AxisItem::X_AXIS?rect.left():rect.bottom(); - oldLayout[j]= item->axisType()==AxisItem::X_AXIS?rect.right():rect.top(); + oldLayout[i]= item->axisType()==Axis::X_AXIS?rect.left():rect.bottom(); + oldLayout[j]= item->axisType()==Axis::X_AXIS?rect.right():rect.top(); } } break; case ZoomInState: { - int index = qMin(oldLayout.count()*(item->axisType()==AxisItem::X_AXIS?m_point.x():(1 -m_point.y())),newLayout.count()-1.0); + int index = qMin(oldLayout.count()*(item->axisType()==Axis::X_AXIS?m_point.x():(1 -m_point.y())),newLayout.count()-1.0); oldLayout.resize(newLayout.count()); for(int i=0;i& newLayout) QRectF rect = item->geometry(); for(int i=0, j=oldLayout.count()-1;iaxisType()==AxisItem::X_AXIS?rect.left():rect.top(); + oldLayout[i]= item->axisType()==Axis::X_AXIS?rect.left():rect.top(); } } break; diff --git a/src/animations/chartanimator_p.h b/src/animations/chartanimator_p.h index fef1ae6..ca3fbca 100644 --- a/src/animations/chartanimator_p.h +++ b/src/animations/chartanimator_p.h @@ -9,7 +9,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartItem; -class AxisItem; +class Axis; class AreaChartItem; class SplineChartItem; class ScatterChartItem; @@ -19,23 +19,24 @@ class XYChartItem; class ChartAnimator : public QObject { public: - //TODO: this should be flags in case of two state at the time enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState,ScrollRightState,ZoomInState,ZoomOutState}; + Q_DECLARE_FLAGS(States, State); + ChartAnimator(QObject *parent = 0); virtual ~ChartAnimator(); - void addAnimation(AxisItem* item); + void addAnimation(Axis* item); void addAnimation(PieChartItem* item); void addAnimation(ScatterChartItem* item); void addAnimation(LineChartItem* item); void addAnimation(SplineChartItem* item); void addAnimation(BarChartItem* item); - void removeAnimation(ChartItem* item); + void removeAnimation(Chart* item); void animationStarted(); void updateLayout(XYChartItem* item, QVector& oldLayout,QVector& newLayout,int index); void updateLayout(SplineChartItem* item, QVector& oldPoints , QVector& newPoints, QVector& oldControlPoints, QVector& newContorlPoints,int index); - void updateLayout(AxisItem* item, QVector& layout); + void updateLayout(Axis* item, QVector& layout); void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty); void removeAnimation(PieChartItem* item, QPieSlice *slice); @@ -47,7 +48,7 @@ public: void setState(State state,const QPointF& point = QPointF()); private: - QMap m_animations; + QMap m_animations; State m_state; QPointF m_point; }; diff --git a/src/animations/xyanimation.cpp b/src/animations/xyanimation.cpp index cd18d3d..56b5fc5 100644 --- a/src/animations/xyanimation.cpp +++ b/src/animations/xyanimation.cpp @@ -69,7 +69,7 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant & end, } break; default: - qWarning()<<"Unknow type of animation"; + qWarning()<<"Unknown type of animation"; break; } diff --git a/src/areachart/areachartitem.cpp b/src/areachart/areachartitem.cpp index 6a3204f..91cab8c 100644 --- a/src/areachart/areachartitem.cpp +++ b/src/areachart/areachartitem.cpp @@ -10,16 +10,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //TODO: optimize : remove points which are not visible -AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent), +AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,ChartPresenter *presenter):Chart(presenter),QGraphicsItem(presenter->rootItem()), m_series(areaSeries), m_upper(0), m_lower(0), m_pointsVisible(false) { setZValue(ChartPresenter::LineChartZValue); - m_upper = new AreaBoundItem(this,m_series->upperSeries()); + m_upper = new AreaBoundItem(this,m_series->upperSeries(),presenter); if(m_series->lowerSeries()){ - m_lower = new AreaBoundItem(this,m_series->lowerSeries()); + m_lower = new AreaBoundItem(this,m_series->lowerSeries(),presenter); } QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated())); diff --git a/src/areachart/areachartitem_p.h b/src/areachart/areachartitem_p.h index 477efad..ced1a66 100644 --- a/src/areachart/areachartitem_p.h +++ b/src/areachart/areachartitem_p.h @@ -10,11 +10,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QAreaSeries; class AreaChartItem; -class AreaChartItem : public QObject ,public ChartItem +class AreaChartItem : public Chart, public QGraphicsItem { Q_OBJECT public: - AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0); + AreaChartItem(QAreaSeries* areaSeries, ChartPresenter *presenter); ~ AreaChartItem(); //from QGraphicsItem @@ -55,7 +55,7 @@ private: class AreaBoundItem : public LineChartItem { public: - AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries), + AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries,ChartPresenter *presenter):LineChartItem(lineSeries,presenter), m_item(item){}; ~AreaBoundItem(){}; diff --git a/src/axis/axisitem.cpp b/src/axis/axisitem.cpp index 6359fb6..5cd0c82 100644 --- a/src/axis/axisitem.cpp +++ b/src/axis/axisitem.cpp @@ -9,25 +9,25 @@ static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -AxisItem::AxisItem(QChartAxis* axis,ChartPresenter* presenter,AxisType type,QGraphicsItem* parent) : -ChartItem(parent), -m_presenter(presenter), +Axis::Axis(QChartAxis* axis,ChartPresenter* presenter,AxisType type) : +Chart(presenter), m_chartAxis(axis), m_type(type), m_labelsAngle(0), -m_grid(parent), -m_shades(parent), -m_labels(parent), -m_axis(parent), +m_grid(presenter->rootItem()), +m_shades(presenter->rootItem()), +m_labels(presenter->rootItem()), +m_axis(presenter->rootItem()), m_min(0), m_max(0), m_ticksCount(0) { //initial initialization m_axis.setZValue(ChartPresenter::AxisZValue); + m_axis.setHandlesChildEvents(false); + m_shades.setZValue(ChartPresenter::ShadesZValue); m_grid.setZValue(ChartPresenter::GridZValue); - setFlags(QGraphicsItem::ItemHasNoContents); QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); @@ -35,20 +35,15 @@ m_ticksCount(0) handleAxisUpdated(); } -AxisItem::~AxisItem() -{ -} - -QRectF AxisItem::boundingRect() const +Axis::~Axis() { - return QRectF(); } -void AxisItem::createItems(int count) +void Axis::createItems(int count) { if(m_axis.children().size()==0) - m_axis.addToGroup(new QGraphicsLineItem()); + m_axis.addToGroup(new AxisItem(this)); for (int i = 0; i < count; ++i) { m_grid.addToGroup(new QGraphicsLineItem()); m_labels.addToGroup(new QGraphicsSimpleTextItem()); @@ -57,7 +52,7 @@ void AxisItem::createItems(int count) } } -void AxisItem::deleteItems(int count) +void Axis::deleteItems(int count) { QList lines = m_grid.childItems(); QList labels = m_labels.childItems(); @@ -72,15 +67,15 @@ void AxisItem::deleteItems(int count) } } -void AxisItem::updateLayout(QVector& layout) +void Axis::updateLayout(QVector& layout) { - if(m_animator){ - m_animator->updateLayout(this,layout); + if(animator()){ + animator()->updateLayout(this,layout); } else setLayout(layout); } -QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const +QStringList Axis::createLabels(int ticks, qreal min, qreal max) const { Q_ASSERT(max>=min); Q_ASSERT(ticks>0); @@ -103,47 +98,47 @@ QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const return labels; } -void AxisItem::setAxisOpacity(qreal opacity) +void Axis::setAxisOpacity(qreal opacity) { m_axis.setOpacity(opacity); } -qreal AxisItem::axisOpacity() const +qreal Axis::axisOpacity() const { return m_axis.opacity(); } -void AxisItem::setGridOpacity(qreal opacity) +void Axis::setGridOpacity(qreal opacity) { m_grid.setOpacity(opacity); } -qreal AxisItem::gridOpacity() const +qreal Axis::gridOpacity() const { return m_grid.opacity(); } -void AxisItem::setLabelsOpacity(qreal opacity) +void Axis::setLabelsOpacity(qreal opacity) { m_labels.setOpacity(opacity); } -qreal AxisItem::labelsOpacity() const +qreal Axis::labelsOpacity() const { return m_labels.opacity(); } -void AxisItem::setShadesOpacity(qreal opacity) +void Axis::setShadesOpacity(qreal opacity) { m_shades.setOpacity(opacity); } -qreal AxisItem::shadesOpacity() const +qreal Axis::shadesOpacity() const { return m_shades.opacity(); } -void AxisItem::setLabelsAngle(int angle) +void Axis::setLabelsAngle(int angle) { foreach(QGraphicsItem* item , m_labels.childItems()) { QPointF center = item->boundingRect().center(); @@ -153,56 +148,56 @@ void AxisItem::setLabelsAngle(int angle) m_labelsAngle=angle; } -void AxisItem::setLabelsPen(const QPen& pen) +void Axis::setLabelsPen(const QPen& pen) { foreach(QGraphicsItem* item , m_labels.childItems()) { static_cast(item)->setPen(pen); } } -void AxisItem::setLabelsBrush(const QBrush& brush) +void Axis::setLabelsBrush(const QBrush& brush) { foreach(QGraphicsItem* item , m_labels.childItems()) { static_cast(item)->setBrush(brush); } } -void AxisItem::setLabelsFont(const QFont& font) +void Axis::setLabelsFont(const QFont& font) { foreach(QGraphicsItem* item , m_labels.childItems()) { static_cast(item)->setFont(font); } } -void AxisItem::setShadesBrush(const QBrush& brush) +void Axis::setShadesBrush(const QBrush& brush) { foreach(QGraphicsItem* item , m_shades.childItems()) { static_cast(item)->setBrush(brush); } } -void AxisItem::setShadesPen(const QPen& pen) +void Axis::setShadesPen(const QPen& pen) { foreach(QGraphicsItem* item , m_shades.childItems()) { static_cast(item)->setPen(pen); } } -void AxisItem::setAxisPen(const QPen& pen) +void Axis::setAxisPen(const QPen& pen) { foreach(QGraphicsItem* item , m_axis.childItems()) { static_cast(item)->setPen(pen); } } -void AxisItem::setGridPen(const QPen& pen) +void Axis::setGridPen(const QPen& pen) { foreach(QGraphicsItem* item , m_grid.childItems()) { static_cast(item)->setPen(pen); } } -QVector AxisItem::calculateLayout() const +QVector Axis::calculateLayout() const { Q_ASSERT(m_ticksCount>=2); @@ -233,7 +228,7 @@ QVector AxisItem::calculateLayout() const return points; } -void AxisItem::setLayout(QVector& layout) +void Axis::setLayout(QVector& layout) { int diff = m_layoutVector.size() - layout.size(); @@ -311,20 +306,20 @@ void AxisItem::setLayout(QVector& layout) m_layoutVector=layout; } -bool AxisItem::isEmpty() +bool Axis::isEmpty() { return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0; } //handlers -void AxisItem::handleAxisCategoriesUpdated() +void Axis::handleAxisCategoriesUpdated() { if(isEmpty()) return; updateLayout(m_layoutVector); } -void AxisItem::handleAxisUpdated() +void Axis::handleAxisUpdated() { if(isEmpty()) return; @@ -369,7 +364,7 @@ void AxisItem::handleAxisUpdated() } -void AxisItem::handleRangeChanged(qreal min, qreal max,int tickCount) +void Axis::handleRangeChanged(qreal min, qreal max,int tickCount) { if(min==max || tickCount<2) return; @@ -383,7 +378,7 @@ void AxisItem::handleRangeChanged(qreal min, qreal max,int tickCount) } -void AxisItem::handleGeometryChanged(const QRectF& rect) +void Axis::handleGeometryChanged(const QRectF& rect) { m_rect = rect; if(isEmpty()) return; @@ -391,13 +386,9 @@ void AxisItem::handleGeometryChanged(const QRectF& rect) updateLayout(layout); } -//painter - -void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +void Axis::axisSelected() { - Q_UNUSED(painter) - Q_UNUSED(option) - Q_UNUSED(widget) + qDebug()<<"TODO axis clicked"; } //TODO "nice numbers algorithm" diff --git a/src/axis/axisitem_p.h b/src/axis/axisitem_p.h index 5d2f4a3..b1a57ea 100644 --- a/src/axis/axisitem_p.h +++ b/src/axis/axisitem_p.h @@ -2,7 +2,7 @@ #define AXISITEM_H_ #include "domain_p.h" -#include "chartitem_p.h" +#include "chart_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -10,18 +10,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChartAxis; class ChartPresenter; -class AxisItem : public QObject, public ChartItem +class Axis : public Chart { Q_OBJECT public: enum AxisType{X_AXIS,Y_AXIS}; - AxisItem(QChartAxis* axis,ChartPresenter* presenter,AxisType type = X_AXIS,QGraphicsItem* parent = 0); - ~AxisItem(); - - //from QGraphicsItem - QRectF boundingRect() const; - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + Axis(QChartAxis* axis,ChartPresenter* presenter,AxisType type = X_AXIS); + ~Axis(); AxisType axisType() const {return m_type;}; @@ -70,9 +66,9 @@ private: void setLayout(QVector& layout); QStringList createLabels(int ticks, qreal min, qreal max) const; + void axisSelected(); private: - ChartPresenter* m_presenter; QChartAxis* m_chartAxis; AxisType m_type; QRectF m_rect; @@ -88,6 +84,36 @@ private: qreal m_zoomFactor; friend class AxisAnimation; + friend class AxisItem; + +}; + +class AxisItem: public QGraphicsLineItem +{ +public: + + AxisItem(Axis* axis,QGraphicsItem* parent=0):QGraphicsLineItem(parent),m_axis(axis){}; + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event) + { + Q_UNUSED(event) + m_axis->axisSelected(); + } + + QRectF boundingRect() const + { + return QGraphicsLineItem::boundingRect().adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?10:0,m_axis->axisType()!=Axis::Y_AXIS?10:0); + } + + QPainterPath shape() const + { + QPainterPath path; + path.addRect(boundingRect()); + return path; + } +private: + Axis* m_axis; }; diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index ab37334..47e5b42 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -13,13 +13,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarChartItem::BarChartItem(QBarSeries *series, QChart *parent) : - ChartItem(parent), +BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : + ChartItem(presenter), mHeight(0), mWidth(0), mLayoutSet(false), - mSeries(series), - mChart(parent) + mSeries(series) { connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index 61568f9..fbd282c 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -20,9 +20,12 @@ class BarChartItem : public QObject, public ChartItem { Q_OBJECT public: - BarChartItem(QBarSeries *series, QChart *parent = 0); + BarChartItem(QBarSeries *series, ChartPresenter *presenter); virtual ~BarChartItem(); +// Common implemantation of different presenters. Not to be instantiated. +// TODO: combine this with BarPresenter and derive other presenters from it? + public: // From QGraphicsItem void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -68,7 +71,6 @@ protected: QBarSeries* mSeries; QList mBars; QList mFloatingValues; - QChart* mChart; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index 3ead549..e61216b 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -6,9 +6,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE - -PercentBarChartItem::PercentBarChartItem(QBarSeries *series, QChart *parent) : - BarChartItem(series, parent) +PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter* presenter) : + BarChartItem(series, presenter) { } diff --git a/src/barchart/separator.cpp b/src/barchart/separator.cpp index b260848..5d4e997 100644 --- a/src/barchart/separator.cpp +++ b/src/barchart/separator.cpp @@ -5,7 +5,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Separator::Separator(QGraphicsItem *parent) - : ChartItem(parent) + : QGraphicsItem(parent) { } diff --git a/src/barchart/separator_p.h b/src/barchart/separator_p.h index cee7485..91d1667 100644 --- a/src/barchart/separator_p.h +++ b/src/barchart/separator_p.h @@ -5,7 +5,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class Separator : public ChartItem +class Separator : public QGraphicsItem { public: Separator(QGraphicsItem *parent = 0); diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index 7bac8ee..402e2cd 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -6,8 +6,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -StackedBarChartItem::StackedBarChartItem(QBarSeries *series, QChart *parent) : - BarChartItem(series,parent) +StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) : + BarChartItem(series,presenter) { } diff --git a/src/chartitem_p.h b/src/chartitem_p.h index 98b74a0..7bf7fa5 100644 --- a/src/chartitem_p.h +++ b/src/chartitem_p.h @@ -1,31 +1,17 @@ #ifndef CHARTITEM_H_ #define CHARTITEM_H_ -#include "domain_p.h" +#include "chart_p.h" +#include "chartpresenter_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartAnimator; - -class ChartItem : public QGraphicsItem +class ChartItem : public QGraphicsItem, public Chart { enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM}; public: - ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent), - m_animator(0){}; - //TODO make pure - virtual void handleGeometryChanged(const QRectF&){}; - virtual void handleDomainChanged(qreal,qreal,qreal,qreal){}; - - void setAnimator(ChartAnimator* animator){ - m_animator=animator; - } - - virtual ~ChartItem(){}; - -protected: - ChartAnimator* m_animator; + ChartItem(ChartPresenter *presenter):QGraphicsItem(presenter->rootItem()),Chart(presenter){}; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 93f20b5..8937572 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -88,7 +88,7 @@ void ChartPresenter::handleGeometryChanged() void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) { - AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); + Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS); if(m_options.testFlag(QChart::GridAxisAnimations)){ m_animator->addAnimation(item); @@ -116,7 +116,7 @@ void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) void ChartPresenter::handleAxisRemoved(QChartAxis* axis) { - AxisItem* item = m_axisItems.take(axis); + Axis* item = m_axisItems.take(axis); Q_ASSERT(item); if(m_animator) m_animator->removeAnimation(item); delete item; @@ -125,14 +125,14 @@ void ChartPresenter::handleAxisRemoved(QChartAxis* axis) void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) { - ChartItem *item = 0 ; + Chart *item = 0 ; switch(series->type()) { case QSeries::SeriesTypeLine: { QLineSeries* lineSeries = static_cast(series); - LineChartItem* line = new LineChartItem(lineSeries,m_chart); + LineChartItem* line = new LineChartItem(lineSeries,this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(line); } @@ -146,7 +146,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeArea: { QAreaSeries* areaSeries = static_cast(series); - AreaChartItem* area = new AreaChartItem(areaSeries,m_chart); + AreaChartItem* area = new AreaChartItem(areaSeries,this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(area->upperLineItem()); if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem()); @@ -160,7 +160,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeBar: { QBarSeries* barSeries = static_cast(series); - BarChartItem* bar = new BarChartItem(barSeries,m_chart); + BarChartItem* bar = new BarChartItem(barSeries,this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(bar); } @@ -173,7 +173,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeStackedBar: { QStackedBarSeries* stackedBarSeries = static_cast(series); - StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,m_chart); + StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(bar); } @@ -186,7 +186,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypePercentBar: { QPercentBarSeries* percentBarSeries = static_cast(series); - PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,m_chart); + PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(bar); } @@ -199,7 +199,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeScatter: { QScatterSeries *scatterSeries = static_cast(series); - ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart); + ScatterChartItem *scatter = new ScatterChartItem(scatterSeries,this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(scatter); } @@ -212,7 +212,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypePie: { QPieSeries *pieSeries = static_cast(series); - PieChartItem* pie = new PieChartItem(pieSeries, this, m_chart); + PieChartItem* pie = new PieChartItem(pieSeries, this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(pie); } @@ -231,7 +231,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeSpline: { QSplineSeries* splineSeries = static_cast(series); - SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart); + SplineChartItem* spline = new SplineChartItem(splineSeries, this); if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(spline); } @@ -256,7 +256,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) void ChartPresenter::handleSeriesRemoved(QSeries* series) { - ChartItem* item = m_chartItems.take(series); + Chart* item = m_chartItems.take(series); Q_ASSERT(item); if(m_animator) { //small hack to handle area animations diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 78d5d19..1265f6c 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -8,11 +8,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartItem; +class Chart; class QSeries; class ChartDataSet; class Domain; -class AxisItem; +class Axis; class ChartTheme; class ChartAnimator; @@ -50,6 +50,8 @@ public: void setAnimationOptions(QChart::AnimationOptions options); QChart::AnimationOptions animationOptions() const; + QGraphicsItem* rootItem() const {return m_chart;}; + void zoomIn(); void zoomIn(const QRectF& rect); void zoomOut(); @@ -76,8 +78,8 @@ private: ChartDataSet* m_dataset; ChartTheme *m_chartTheme; int m_zoomIndex; - QMap m_chartItems; - QMap m_axisItems; + QMap m_chartItems; + QMap m_axisItems; QVector m_zoomStack; QRectF m_rect; QChart::AnimationOptions m_options; diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 20a61e4..aea0c15 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -8,7 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //TODO: optimize : remove points which are not visible -LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent), +LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChartItem(series,presenter), m_series(series), m_pointsVisible(false) { diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h index 9b3561d..a5c7f71 100644 --- a/src/linechart/linechartitem_p.h +++ b/src/linechart/linechartitem_p.h @@ -8,12 +8,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QLineSeries; +class ChartPresenter; class LineChartItem : public XYChartItem { Q_OBJECT public: - explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0); + explicit LineChartItem(QLineSeries* series,ChartPresenter *presenter); ~ LineChartItem(){}; //from QGraphicsItem diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index dcd3725..7e5f5d6 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -12,10 +12,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter *presenter, QGraphicsItem *parent) - :ChartItem(parent), - m_series(series), - m_presenter(presenter) +PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter) + :ChartItem(presenter), + m_series(series) { Q_ASSERT(series); connect(series, SIGNAL(added(QList)), this, SLOT(handleSlicesAdded(QList))); @@ -52,7 +51,7 @@ void PieChartItem::handleSlicesAdded(QList slices) { bool isEmpty = m_slices.isEmpty(); - m_presenter->theme()->decorate(m_series, m_presenter->dataSet()->seriesIndex(m_series)); + presenter()->theme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); foreach (QPieSlice *s, slices) { PieSliceItem* item = new PieSliceItem(this); @@ -73,11 +72,11 @@ void PieChartItem::handleSlicesAdded(QList slices) void PieChartItem::handleSlicesRemoved(QList slices) { - m_presenter->theme()->decorate(m_series, m_presenter->dataSet()->seriesIndex(m_series)); + presenter()->theme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); foreach (QPieSlice *s, slices) { - if (m_animator) - m_animator->removeAnimation(this, s); + if (animator()) + animator()->removeAnimation(this, s); else destroySlice(s); } @@ -148,8 +147,8 @@ PieLayout PieChartItem::calculateLayout() void PieChartItem::applyLayout(const PieLayout &layout) { - if (m_animator) - m_animator->updateLayout(this, layout); + if (animator()) + animator()->updateLayout(this, layout); else setLayout(layout); } diff --git a/src/piechart/piechartitem_p.h b/src/piechart/piechartitem_p.h index 3ec4da8..243de60 100644 --- a/src/piechart/piechartitem_p.h +++ b/src/piechart/piechartitem_p.h @@ -12,13 +12,13 @@ class ChartPresenter; typedef QHash PieLayout; -class PieChartItem : public QObject, public ChartItem +class PieChartItem : public ChartItem { Q_OBJECT public: // TODO: use a generic data class instead of x and y - PieChartItem(QPieSeries *series, ChartPresenter *presenter, QGraphicsItem *parent); + PieChartItem(QPieSeries *series, ChartPresenter *presenter); ~PieChartItem(); public: // from QGraphicsItem @@ -51,7 +51,6 @@ private: QRectF m_rect; QPointF m_pieCenter; qreal m_pieRadius; - ChartPresenter *m_presenter; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchart.h b/src/qchart.h index 77f9c6d..c6eb87e 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -11,7 +11,7 @@ class QGraphicsSceneResizeEvent; QTCOMMERCIALCHART_BEGIN_NAMESPACE -class AxisItem; +class Axis; class QSeries; class PlotDomain; class BarChartItem; diff --git a/src/scatterseries/scatterchartitem.cpp b/src/scatterseries/scatterchartitem.cpp index e3c9fd5..63a0342 100644 --- a/src/scatterseries/scatterchartitem.cpp +++ b/src/scatterseries/scatterchartitem.cpp @@ -6,17 +6,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : - XYChartItem(series,parent), +ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) : + XYChartItem(series,presenter), m_series(series), m_items(this), m_shape(QScatterSeries::MarkerShapeRectangle), m_size(15) { - Q_ASSERT(parent); - Q_ASSERT(series); - QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); setZValue(ChartPresenter::ScatterSeriesZValue); diff --git a/src/scatterseries/scatterchartitem_p.h b/src/scatterseries/scatterchartitem_p.h index ad01028..90107da 100644 --- a/src/scatterseries/scatterchartitem_p.h +++ b/src/scatterseries/scatterchartitem_p.h @@ -15,7 +15,7 @@ class ScatterChartItem : public XYChartItem { Q_OBJECT public: - explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0); + explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter); public: //from QGraphicsItem diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index ad08556..8f5233f 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -5,8 +5,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) : -XYChartItem(series, parent), +SplineChartItem::SplineChartItem(QSplineSeries* series, ChartPresenter *presenter) : +XYChartItem(series, presenter), m_series(series), m_pointsVisible(false) { @@ -42,8 +42,8 @@ void SplineChartItem::updateLayout(QVector& oldPoints,QVector& return; } - if(m_animator){ - m_animator->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); + if(animator()){ + animator()->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); }else{ setLayout(newPoints,controlPoints); } diff --git a/src/splinechart/splinechartitem_p.h b/src/splinechart/splinechartitem_p.h index b6f0a44..df99fee 100644 --- a/src/splinechart/splinechartitem_p.h +++ b/src/splinechart/splinechartitem_p.h @@ -12,7 +12,7 @@ class SplineChartItem : public XYChartItem { Q_OBJECT public: - SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0); + SplineChartItem(QSplineSeries* series, ChartPresenter *presenter); //from QGraphicsItem QRectF boundingRect() const; diff --git a/src/src.pro b/src/src.pro index e89c305..bd8927d 100644 --- a/src/src.pro +++ b/src/src.pro @@ -17,7 +17,8 @@ SOURCES += \ qseries.cpp \ qlegend.cpp \ legendmarker.cpp \ - chartbackground.cpp + chartbackground.cpp \ + chart.cpp PRIVATE_HEADERS += \ chartdataset_p.h \ chartitem_p.h \ @@ -25,7 +26,8 @@ PRIVATE_HEADERS += \ charttheme_p.h \ domain_p.h \ legendmarker_p.h \ - chartbackground_p.h + chartbackground_p.h \ + chart_p.h PUBLIC_HEADERS += \ qchart.h \ qchartglobal.h \ diff --git a/src/xychart/xychartitem.cpp b/src/xychart/xychartitem.cpp index f4e57d8..cfc7b2b 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -10,7 +10,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //TODO: optimize : remove points which are not visible -XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent), +XYChartItem::XYChartItem(QXYSeries* series, ChartPresenter *presenter):ChartItem(presenter), m_minX(0), m_maxX(0), m_minY(0), @@ -68,8 +68,8 @@ QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const void XYChartItem::updateLayout(QVector& oldPoints,QVector& newPoints,int index) { - if(m_animator){ - m_animator->updateLayout(this,oldPoints,newPoints,index); + if(animator()){ + animator()->updateLayout(this,oldPoints,newPoints,index); }else{ setLayout(newPoints); } diff --git a/src/xychart/xychartitem_p.h b/src/xychart/xychartitem_p.h index bb83d74..8b23892 100644 --- a/src/xychart/xychartitem_p.h +++ b/src/xychart/xychartitem_p.h @@ -10,16 +10,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartPresenter; class QXYSeries; -class XYChartItem : public QObject , public ChartItem +class XYChartItem : public ChartItem { Q_OBJECT public: - explicit XYChartItem(QXYSeries* series, QGraphicsItem *parent = 0); + explicit XYChartItem(QXYSeries* series, ChartPresenter *presenter); ~ XYChartItem(){}; QVector points() const {return m_points;} QRectF clipRect() const { return m_clipRect;} + public slots: void handlePointAdded(int index); void handlePointRemoved(int index); @@ -31,8 +32,10 @@ signals: void clicked(const QPointF& point); protected: + virtual void setLayout(QVector& points); virtual void updateLayout(QVector& oldPoints,QVector& newPoints,int index = 0); + QPointF calculateGeometryPoint(const QPointF& point) const; QPointF calculateGeometryPoint(int index) const; QPointF calculateDomainPoint(const QPointF& point) const;