diff --git a/src/barchart/bargroupbase.cpp b/src/barchart/bargroupbase.cpp index 023e016..6a3bd97 100644 --- a/src/barchart/bargroupbase.cpp +++ b/src/barchart/bargroupbase.cpp @@ -24,20 +24,6 @@ void BarGroupBase::setSeparatorsVisible(bool visible) mSeparatorsVisible = visible; } -void BarGroupBase::setSize(const QSizeF& size) -{ - mWidth = size.width(); - mHeight = size.height(); - layoutChanged(); - mLayoutSet = true; -} - -void BarGroupBase::setPlotDomain(const PlotDomain& data) -{ - qDebug() << "BarGroupBase::setPlotDomain"; - // TODO: -} - void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { if (!mLayoutSet) { @@ -119,4 +105,27 @@ void BarGroupBase::dataChanged() mLayoutDirty = true; } +//handlers + +void BarGroupBase::handleModelChanged(int index) +{ + qDebug() << "BarGroupBase::handleModelChanged"; +} + +void BarGroupBase::handleDomainChanged(const Domain& domain) +{ + qDebug() << "BarGroupBase::handleModelChanged"; +} + +void BarGroupBase::handleGeometryChanged(const QRectF& rect) +{ + mWidth = rect.width(); + mHeight = rect.height(); + layoutChanged(); + mLayoutSet = true; + setPos(rect.topLeft()); +} + +#include "moc_bargroupbase.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bargroupbase.h b/src/barchart/bargroupbase.h index 06995f1..8dfff8e 100644 --- a/src/barchart/bargroupbase.h +++ b/src/barchart/bargroupbase.h @@ -12,16 +12,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. -class BarGroupBase : public ChartItem, public ChartThemeObserver +class BarGroupBase : public QObject, public ChartItem { - + Q_OBJECT public: BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); void setSeparatorsVisible(bool visible = true); public: // From ChartItem - void setSize(const QSizeF &size); - void setPlotDomain(const PlotDomain& data); + void setSize(const QSizeF &size){}; + void setPlotDomain(const PlotDomain& data){}; // From QGraphicsItem void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -38,6 +38,12 @@ public: // From ChartItem virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes +protected slots: + void handleModelChanged(int index); + void handleDomainChanged(const Domain& domain); + void handleGeometryChanged(const QRectF& size); + + protected: BarChartSeriesBase& mSeries; diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 24b9ddb..2ef0947 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -1,5 +1,9 @@ #include "chartdataset_p.h" +//series #include "qxychartseries.h" +#include "barchartseries.h" +#include "stackedbarchartseries.h" +#include "percentbarchartseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -21,42 +25,114 @@ const Domain& ChartDataSet::domain() const void ChartDataSet::addSeries(QChartSeries* series) { - // TODO: we should check the series not already added - m_chartSeries << series; - m_domainIndex = 0; - m_domains.resize(1); - - Domain& domain = m_domains[m_domainIndex]; - - switch(series->type()) - { - case QChartSeries::SeriesTypeLine: { - - QXYChartSeries* xyseries = static_cast(series); - - for (int i = 0; i < xyseries->count(); i++) - { - qreal x = xyseries->x(i); - qreal y = xyseries->y(i); - domain.m_minX = qMin(domain.m_minX,x); - domain.m_minY = qMin(domain.m_minY,y); - domain.m_maxX = qMax(domain.m_maxX,x); - domain.m_maxY = qMax(domain.m_maxY,y); - } - - emit domainChanged(); - - break; - } - default: { - qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; - return; - break; - } - - } - - emit seriesAdded(series); + // TODO: we should check the series not already added + m_chartSeries << series; + m_domainIndex = 0; + m_domains.resize(1); + + Domain& domain = m_domains[m_domainIndex]; + + switch(series->type()) + { + case QChartSeries::SeriesTypeLine: { + + QXYChartSeries* xyseries = static_cast(series); + + for (int i = 0; i < xyseries->count(); i++) + { + qreal x = xyseries->x(i); + qreal y = xyseries->y(i); + domain.m_minX = qMin(domain.m_minX,x); + domain.m_minY = qMin(domain.m_minY,y); + domain.m_maxX = qMax(domain.m_maxX,x); + domain.m_maxY = qMax(domain.m_maxY,y); + } + break; + } + case QChartSeries::SeriesTypeBar: { + + BarChartSeries* barSeries = static_cast(series); + qreal x = barSeries->countColumns(); + qreal y = barSeries->max(); + domain.m_minX = qMin(domain.m_minX,x); + domain.m_minY = qMin(domain.m_minY,y); + domain.m_maxX = qMax(domain.m_maxX,x); + domain.m_maxY = qMax(domain.m_maxY,y); + } + break; + case QChartSeries::SeriesTypeStackedBar: { + + StackedBarChartSeries* stackedBarSeries = static_cast(series); + qreal x = stackedBarSeries->countColumns(); + qreal y = stackedBarSeries->maxColumnSum(); + domain.m_minX = qMin(domain.m_minX,x); + domain.m_minY = qMin(domain.m_minY,y); + domain.m_maxX = qMax(domain.m_maxX,x); + domain.m_maxY = qMax(domain.m_maxY,y); + } + break; + case QChartSeries::SeriesTypePercentBar: { + + PercentBarChartSeries* percentBarSeries = static_cast(series); + qreal x = percentBarSeries->countColumns(); + domain.m_minX = qMin(domain.m_minX,x); + domain.m_minY = 0; + domain.m_maxX = qMax(domain.m_maxX,x); + domain.m_maxY = 100; + } + break; + + default: { + qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; + return; + break; + } + + } + + emit seriesAdded(series); + emit domainChanged(domain); +} + +bool ChartDataSet::nextDomain() +{ + if (m_domainIndex < m_domains.count() - 1) { + m_domainIndex++; + emit domainChanged(m_domains[m_domainIndex]); + return true; + } + else { + return false; + } +} + +bool ChartDataSet::previousDomain() +{ + if (m_domainIndex > 0) { + m_domainIndex--; + emit domainChanged(m_domains[m_domainIndex]); + return true; + } + else { + return false; + } +} + +void ChartDataSet::clearDomains() +{ + if (m_domainIndex > 0) { + m_domainIndex = 0; + emit domainChanged(m_domains[m_domainIndex]); + } +} + +void ChartDataSet::addDomain(const Domain& domain) +{ + m_domains.resize(m_domainIndex + 1); + m_domains << domain; + m_domainIndex++; + + emit domainChanged(domain); } #include "moc_chartdataset_p.cpp" diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index bd3735b..4bb533c 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -15,11 +15,15 @@ public: virtual ~ChartDataSet(); void addSeries(QChartSeries* series); + void addDomain(const Domain& domain); + bool nextDomain(); + bool previousDomain(); + void clearDomains(); const Domain& domain() const; signals: void seriesAdded(QChartSeries* series); - void domainChanged(); + void domainChanged(const Domain& domain); private: QList m_chartSeries; diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 2ac89cf..9f05597 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -1,9 +1,17 @@ +#include "qchart.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" +//series +#include "barchartseries.h" +#include "stackedbarchartseries.h" +#include "percentbarchartseries.h" #include "qxychartseries.h" +//items +#include "bargroup.h" +#include "stackedbargroup.h" #include "xylinechartitem_p.h" +#include "percentbargroup.h" #include "linechartanimationitem_p.h" -#include "qchart.h" #include #include @@ -13,6 +21,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), m_chart(chart), m_dataset(dataset), +m_domainIndex(0), +m_marginSize(0), m_rect(QRectF(QPoint(0,0),m_chart->size())) { creteConnections(); @@ -26,21 +36,23 @@ void ChartPresenter::creteConnections() { QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); - QObject::connect(m_dataset,SIGNAL(domainChanged()),this,SLOT(handleDomainChanged())); } void ChartPresenter::handleGeometryChanged() { m_rect = QRectF(QPoint(0,0),m_chart->size()); + m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); + emit geometryChanged(m_rect); +} - int margin = m_chart->margin(); - m_rect.adjust(margin,margin, -margin, -margin); +int ChartPresenter::margin() const +{ + return m_marginSize; +} - foreach (ChartItem *item, m_chartItems) { - item->setPos(m_rect.topLeft()); - item->setSize(m_rect.size()); - item->updateItem(); - } +void ChartPresenter::setMargin(int margin) +{ + m_marginSize = margin; } void ChartPresenter::handleSeriesAdded(QChartSeries* series) @@ -48,38 +60,167 @@ void ChartPresenter::handleSeriesAdded(QChartSeries* series) switch(series->type()) { case QChartSeries::SeriesTypeLine: { + QXYChartSeries* lineSeries = static_cast(series); + XYLineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart); + item->setPen(lineSeries->pen()); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); + QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); + m_chartItems.insert(series,item); + break; + } + + case QChartSeries::SeriesTypeBar: { + BarChartSeries* barSeries = static_cast(series); + BarGroup* item = new BarGroup(*barSeries,m_chart); + + // Add some fugly colors for 5 fist series... + item->addColor(QColor(255,0,0,128)); + item->addColor(QColor(255,255,0,128)); + item->addColor(QColor(0,255,0,128)); + item->addColor(QColor(0,0,255,128)); + item->addColor(QColor(255,128,0,128)); - QXYChartSeries* xyseries = static_cast(series); - //TODO: series->createViewItem(); - //XYLineChartItem* item = new XYLineChartItem(this,m_chart); - XYLineChartItem* item = new LineChartAnimationItem(this,xyseries,m_chart); - item->setDomain(m_dataset->domain()); - item->updateItem(); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); + QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); m_chartItems.insert(series,item); + // m_axisXItem->setVisible(false); break; } + case QChartSeries::SeriesTypeStackedBar: { + + StackedBarChartSeries* stackedBarSeries = static_cast(series); + StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart); + + // Add some fugly colors for 5 fist series... + item->addColor(QColor(255,0,0,128)); + item->addColor(QColor(255,255,0,128)); + item->addColor(QColor(0,255,0,128)); + item->addColor(QColor(0,0,255,128)); + item->addColor(QColor(255,128,0,128)); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); + QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); + m_chartItems.insert(series,item); + break; + } + + case QChartSeries::SeriesTypePercentBar: { + + PercentBarChartSeries* percentBarSeries = static_cast(series); + PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart); + + // Add some fugly colors for 5 fist series... + item->addColor(QColor(255,0,0,128)); + item->addColor(QColor(255,255,0,128)); + item->addColor(QColor(0,255,0,128)); + item->addColor(QColor(0,0,255,128)); + item->addColor(QColor(255,128,0,128)); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); + QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); + m_chartItems.insert(series,item); + break; + } + /* + case QChartSeries::SeriesTypeScatter: { + QScatterSeries *scatterSeries = qobject_cast(series); + scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); + scatterSeries->d->setParentItem(this); + scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); + m_chartItems << scatterSeries->d; + m_chartTheme->addObserver(scatterSeries->d); + + foreach (qreal x, scatterSeries->d->m_x) { + domain.m_minX = qMin(domain.m_minX, x); + domain.m_maxX = qMax(domain.m_maxX, x); + } + foreach (qreal y, scatterSeries->d->m_y) { + domain.m_minY = qMin(domain.m_minY, y); + domain.m_maxY = qMax(domain.m_maxY, y); + } + + break; + } + case QChartSeries::SeriesTypePie: { + QPieSeries *pieSeries = qobject_cast(series); + pieSeries->d->setParentItem(this); + m_chartItems << pieSeries->d; + pieSeries->d->m_chartTheme = m_chartTheme; + m_chartTheme->addObserver(pieSeries->d); + break; + } + default: + break; + } + */ + default: { - qDebug()<< "Series type" << series->type() << "not implemented."; - break; + qDebug()<< "Series type" << series->type() << "not implemented."; + break; } } } void ChartPresenter::handleSeriesChanged(QChartSeries* series) { - switch(series->type()) - { - case QChartSeries::SeriesTypeLine: { + //TODO: +} + +void ChartPresenter::zoomInToRect(const QRectF& rect) +{ + if(!rect.isValid()) return; + QRectF r = rect.normalized(); + r.translate(-m_marginSize, -m_marginSize); + Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height())); + m_dataset->addDomain(domain); +} - break; - } - } +void ChartPresenter::zoomIn() +{ + if (!m_dataset->nextDomain()) { + QRectF rect = m_rect; + rect.setWidth(rect.width()/2); + rect.setHeight(rect.height()/2); + rect.moveCenter(m_rect.center()); + Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height())); + m_dataset->addDomain(domain); + } +} - //m_chartItems.value(series)->updateItem(); +void ChartPresenter::zoomOut() +{ + m_dataset->previousDomain(); } +void ChartPresenter::zoomReset() +{ + m_dataset->clearDomains(); +} + +/* +void ChartPresenter::setAxisX(const QChartAxis& axis) +{ + setAxis(m_axisXItem,axis); +} +void ChartPresenter::setAxisY(const QChartAxis& axis) +{ + setAxis(m_axisYItem.at(0),axis); +} + +void ChartPresenter::setAxisY(const QList& axis) +{ + //TODO not implemented +} + +void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis) +{ + item->setVisible(axis.isAxisVisible()); +} +*/ #include "moc_chartpresenter_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 779d7a5..a24aaf0 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -20,6 +20,21 @@ class ChartPresenter: public QObject public: ChartPresenter(QChart* chart,ChartDataSet *dataset); virtual ~ChartPresenter(); +/* + void setAxisX(const QChartAxis& axis); + void setAxisY(const QChartAxis& axis); + void setAxisY(const QList& axis); + + +*/ + + void setMargin(int margin); + int margin() const; + + void zoomInToRect(const QRectF& rectangle); + void zoomIn(); + void zoomOut(); + void zoomReset(); private: void creteConnections(); @@ -31,12 +46,16 @@ public slots: //void handleDomainChanged(Domain oldDomain,Domain newDomain); void handleGeometryChanged(); - +signals: + void geometryChanged(const QRectF& rect); private: QMap m_chartItems; QChart* m_chart; ChartDataSet* m_dataset; + QVector m_domains; + int m_domainIndex; + int m_marginSize; QRectF m_rect; }; diff --git a/src/domain.cpp b/src/domain.cpp index e011bae..081ba76 100644 --- a/src/domain.cpp +++ b/src/domain.cpp @@ -28,7 +28,7 @@ qreal Domain::spanY() const return m_maxY - m_minY; } -Domain Domain::subDomain(const QRect& rect, qreal maxWidth,qreal maxHeight) const +Domain Domain::subDomain(const QRectF& rect, qreal maxWidth,qreal maxHeight) const { Domain domain; diff --git a/src/domain_p.h b/src/domain_p.h index 3513216..ea1998d 100644 --- a/src/domain_p.h +++ b/src/domain_p.h @@ -13,7 +13,7 @@ public: qreal spanX() const; qreal spanY() const; - Domain subDomain(const QRect& rect, qreal maxWidth, qreal maxHeight) const; + Domain subDomain(const QRectF& rect, qreal maxWidth, qreal maxHeight) const; public: qreal m_minX; diff --git a/src/qchart.cpp b/src/qchart.cpp index 8fc3efe..2655711 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -4,22 +4,21 @@ #include "qscatterseries_p.h" #include "qpieseries.h" #include "qpieseries_p.h" -#include "qxychartseries.h" #include "qchartaxis.h" -#include "barchartseries.h" -#include "bargroup.h" -#include "stackedbarchartseries.h" -#include "stackedbargroup.h" -#include "percentbarchartseries.h" -#include "percentbargroup.h" #include "charttheme_p.h" #include "chartitem_p.h" - -#include "xylinechartitem_p.h" #include "plotdomain_p.h" #include "axisitem_p.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" + +//series +#include "barchartseries.h" +#include "stackedbarchartseries.h" +#include "percentbarchartseries.h" +#include "qxychartseries.h" + + #include #include #include @@ -31,18 +30,18 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( m_titleItem(0), m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)), m_plotDataIndex(0), - m_marginSize(0), m_chartTheme(new ChartTheme(this)), - m_dataset(0), - //m_dataset(new ChartDataSet(this)), - m_presenter(0) - //m_presenter(new ChartPresenter(this,m_dataset)) + //m_dataset(0), + m_dataset(new ChartDataSet(this)), + //m_presenter(0) + m_presenter(new ChartPresenter(this,m_dataset)) { // TODO: the default theme? setTheme(QChart::ChartThemeDefault); PlotDomain domain; m_plotDomainList << domain; + m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); m_chartItems << m_axisXItem; m_chartItems << m_axisYItem.at(0); @@ -52,154 +51,7 @@ QChart::~QChart(){} void QChart::addSeries(QChartSeries* series) { - if(m_dataset) { - m_dataset->addSeries(series); - return; - } - - Q_ASSERT(series); - Q_ASSERT(series->type() != QChartSeries::SeriesTypeInvalid); - - // TODO: we should check the series not already added - - - m_chartSeries << series; - - m_plotDataIndex = 0 ; - m_plotDomainList.resize(1); - PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; - - switch(series->type()) - { - case QChartSeries::SeriesTypeLine: { - - QXYChartSeries* xyseries = static_cast(series); - - for (int i = 0 ; i < xyseries->count() ; i++) { - qreal x = xyseries->x(i); - qreal y = xyseries->y(i); - domain.m_minX = qMin(domain.m_minX,x); - domain.m_minY = qMin(domain.m_minY,y); - domain.m_maxX = qMax(domain.m_maxX,x); - domain.m_maxY = qMax(domain.m_maxY,y); - } - - //XYLineChartItem* item = new XYLineChartItem(xyseries,0,this); - - //m_chartItems << item; - // TODO: - //m_chartTheme->addObserver(xyseries); - - break; - } - case QChartSeries::SeriesTypeBar: { - - qDebug() << "barSeries added"; - BarChartSeries* barSeries = static_cast(series); - BarGroup* barGroup = new BarGroup(*barSeries,this); - - // Add some fugly colors for 5 fist series... - barGroup->addColor(QColor(255,0,0,128)); - barGroup->addColor(QColor(255,255,0,128)); - barGroup->addColor(QColor(0,255,0,128)); - barGroup->addColor(QColor(0,0,255,128)); - barGroup->addColor(QColor(255,128,0,128)); - - m_chartItems << barGroup; - childItems().append(barGroup); - - qreal x = barSeries->countColumns(); - qreal y = barSeries->max(); - domain.m_minX = qMin(domain.m_minX,x); - domain.m_minY = qMin(domain.m_minY,y); - domain.m_maxX = qMax(domain.m_maxX,x); - domain.m_maxY = qMax(domain.m_maxY,y); - m_axisXItem->setVisible(false); - break; - } - case QChartSeries::SeriesTypeStackedBar: { - - qDebug() << "barSeries added"; - StackedBarChartSeries* stackedBarSeries = static_cast(series); - StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this); - - // Add some fugly colors for 5 fist series... - stackedBarGroup->addColor(QColor(255,0,0,128)); - stackedBarGroup->addColor(QColor(255,255,0,128)); - stackedBarGroup->addColor(QColor(0,255,0,128)); - stackedBarGroup->addColor(QColor(0,0,255,128)); - stackedBarGroup->addColor(QColor(255,128,0,128)); - - m_chartItems << stackedBarGroup; - childItems().append(stackedBarGroup); - - qreal x = stackedBarSeries->countColumns(); - qreal y = stackedBarSeries->maxColumnSum(); - domain.m_minX = qMin(domain.m_minX,x); - domain.m_minY = qMin(domain.m_minY,y); - domain.m_maxX = qMax(domain.m_maxX,x); - domain.m_maxY = qMax(domain.m_maxY,y); - m_axisXItem->setVisible(false); - break; - } - case QChartSeries::SeriesTypePercentBar: { - - qDebug() << "barSeries added"; - PercentBarChartSeries* percentBarSeries = static_cast(series); - PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this); - - // Add some fugly colors for 5 fist series... - percentBarGroup->addColor(QColor(255,0,0,128)); - percentBarGroup->addColor(QColor(255,255,0,128)); - percentBarGroup->addColor(QColor(0,255,0,128)); - percentBarGroup->addColor(QColor(0,0,255,128)); - percentBarGroup->addColor(QColor(255,128,0,128)); - - m_chartItems << percentBarGroup; - childItems().append(percentBarGroup); - - qreal x = percentBarSeries->countColumns(); - domain.m_minX = qMin(domain.m_minX,x); - domain.m_minY = 0; - domain.m_maxX = qMax(domain.m_maxX,x); - domain.m_maxY = 100; - m_axisXItem->setVisible(false); - break; - } - case QChartSeries::SeriesTypeScatter: { - QScatterSeries *scatterSeries = qobject_cast(series); - scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); - scatterSeries->d->setParentItem(this); - scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); - m_chartItems << scatterSeries->d; - m_chartTheme->addObserver(scatterSeries->d); - - foreach (qreal x, scatterSeries->d->m_x) { - domain.m_minX = qMin(domain.m_minX, x); - domain.m_maxX = qMax(domain.m_maxX, x); - } - foreach (qreal y, scatterSeries->d->m_y) { - domain.m_minY = qMin(domain.m_minY, y); - domain.m_maxY = qMax(domain.m_maxY, y); - } - - break; - } - case QChartSeries::SeriesTypePie: { - QPieSeries *pieSeries = qobject_cast(series); - pieSeries->d->setParentItem(this); - m_chartItems << pieSeries->d; - pieSeries->d->m_chartTheme = m_chartTheme; - m_chartTheme->addObserver(pieSeries->d); - break; - } - default: - break; - } - - // Update all the items to match the new visible area of the chart - foreach(ChartItem* i, m_chartItems) - i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); + m_dataset->addSeries(series); } QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) @@ -275,12 +127,12 @@ void QChart::setTitle(const QString& title,const QFont& font) int QChart::margin() const { - return m_marginSize; + m_presenter->margin(); } void QChart::setMargin(int margin) { - m_marginSize = margin; + m_presenter->setMargin(margin); } void QChart::setTheme(QChart::ChartThemeId theme) @@ -315,61 +167,22 @@ QChart::ChartThemeId QChart::theme() void QChart::zoomInToRect(const QRectF& rectangle) { - - if(!rectangle.isValid()) return; - - qreal margin = this->margin(); - - QRectF rect = rectangle.normalized(); - rect.translate(-margin, -margin); - - PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; - - PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); - - m_plotDomainList.resize(m_plotDataIndex + 1); - m_plotDomainList<setPlotDomain(m_plotDomainList[m_plotDataIndex]); - update(); + m_presenter->zoomInToRect(rectangle); } void QChart::zoomIn() { - if (m_plotDataIndex < m_plotDomainList.count() - 1) { - m_plotDataIndex++; - foreach (ChartItem* item, m_chartItems) - item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); - update(); - } else { - QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); - rect.setWidth(rect.width()/2); - rect.setHeight(rect.height()/2); - rect.moveCenter(m_rect.center()); - zoomInToRect(rect); - } + m_presenter->zoomIn(); } void QChart::zoomOut() { - if (m_plotDataIndex > 0) { - m_plotDataIndex--; - foreach (ChartItem* item, m_chartItems) - item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); - update(); - } + m_presenter->zoomOut(); } void QChart::zoomReset() { - if (m_plotDataIndex > 0) { - m_plotDataIndex = 0; - foreach (ChartItem* item, m_chartItems) - item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); - update(); - } + m_presenter->zoomReset(); } void QChart::setAxisX(const QChartAxis& axis) @@ -408,12 +221,6 @@ void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) m_backgroundItem->setRect(rect); } - // resize and reposition childs - foreach (ChartItem *item, m_chartItems) { - item->setPos(rect.topLeft()); - item->setSize(rect.size()); - } - QGraphicsWidget::resizeEvent(event); update(); } diff --git a/src/qchart.h b/src/qchart.h index 1e26f9f..fc55c8f 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -33,10 +33,6 @@ class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget { Q_OBJECT public: - enum GradientOrientation { - HorizonatlGradientOrientation, - VerticalGradientOrientation - }; enum ChartThemeId { ChartThemeInvalid = -1, /*! The default theme follows the GUI style of the Operating System */ @@ -85,7 +81,6 @@ private: private: Q_DISABLE_COPY(QChart) QGraphicsRectItem* m_backgroundItem; - GradientOrientation m_bacgroundOrinetation; QGraphicsTextItem* m_titleItem; AxisItem* m_axisXItem; QList m_axisYItem; @@ -94,7 +89,6 @@ private: QList m_chartItems; QVector m_plotDomainList; int m_plotDataIndex; - int m_marginSize; ChartTheme *m_chartTheme; diff --git a/src/xylinechart/xylinechartitem.cpp b/src/xylinechart/xylinechartitem.cpp index ca00ff0..51274de 100644 --- a/src/xylinechart/xylinechartitem.cpp +++ b/src/xylinechart/xylinechartitem.cpp @@ -13,7 +13,6 @@ m_dirtyData(false), m_dirtyGeometry(false), m_dirtyDomain(false) { - QObject::connect(series,SIGNAL(changed(int)),this,SLOT(handleSeriesChanged(int))); } QRectF XYLineChartItem::boundingRect() const @@ -26,23 +25,6 @@ QPainterPath XYLineChartItem::shape() const return m_path; } -void XYLineChartItem::setSize(const QSizeF& size) -{ - m_size=size; - m_dirtyGeometry=true; -} - -void XYLineChartItem::setDomain(const Domain& domain) -{ - m_domain=domain; - m_dirtyDomain=true; -} - -void XYLineChartItem::setSeries(QXYChartSeries* series) -{ - m_series = series; - m_dirtyData=true; -} void XYLineChartItem::addPoints(const QVector& points) { @@ -112,21 +94,11 @@ void XYLineChartItem::clearView() m_data.clear(); } -void XYLineChartItem::handleSeriesChanged(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); - } -} - void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(widget); Q_UNUSED(option); + painter->setPen(m_pen); painter->drawPath(m_path); } @@ -161,7 +133,6 @@ void XYLineChartItem::updateDomain() clear(); calculatePoints(m_data,m_hash,m_series,m_size, m_domain); addPoints(m_data); - m_dirtyGeometry = true; } void XYLineChartItem::updateData() @@ -188,26 +159,44 @@ void XYLineChartItem::updateGeometry() m_rect = path.boundingRect(); } -void XYLineChartItem::updateItem() +void XYLineChartItem::setPen(const QPen& pen) { - if(m_dirtyDomain) { - updateDomain(); - m_dirtyData = false; - } + m_pen = pen; +} - if(m_dirtyData) { - updateData(); - m_dirtyData = false; - } +//handlers - if(m_dirtyGeometry) { - updateGeometry(); - m_dirtyGeometry = false; +void XYLineChartItem::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 XYLineChartItem::handleDomainChanged(const Domain& domain) +{ + m_domain = domain; + updateDomain(); update(); } +void XYLineChartItem::handleGeometryChanged(const QRectF& rect) +{ + Q_ASSERT(rect.isValid()); + + m_size=rect.size(); + updateDomain(); + updateGeometry(); + setPos(rect.topLeft()); + update(); +} + + #include "moc_xylinechartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/xylinechart/xylinechartitem_p.h b/src/xylinechart/xylinechartitem_p.h index fea8bec..6bdcfba 100644 --- a/src/xylinechart/xylinechartitem_p.h +++ b/src/xylinechart/xylinechartitem_p.h @@ -3,6 +3,7 @@ #include "qchartglobal.h" #include "chartitem_p.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -21,15 +22,16 @@ public: QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QPainterPath shape() const; + + + void setPen(const QPen& pen); + //from ChartItem - void setSize(const QSizeF& size); + void setSize(const QSizeF& size){}; void setPlotDomain(const PlotDomain& data){}; - void setDomain(const Domain& data); - void setSeries(QXYChartSeries* series); - const Domain& domain() const { return m_domain;} - //ChartAnimationManager* animationManager(); - void updateItem(); + + const Domain& domain() const { return m_domain;} virtual void addPoint(const QPointF& ); virtual void addPoints(const QVector& points); @@ -50,8 +52,10 @@ protected: void calculatePoint(QPointF& point, int index, const QXYChartSeries* series,const QSizeF& size, const Domain& domain) const; void calculatePoints(QVector& points,QHash& hash,const QXYChartSeries* series, const QSizeF& size, const Domain& domain) const; -private slots: - void handleSeriesChanged(int index); +protected slots: + void handleModelChanged(int index); + void handleDomainChanged(const Domain& domain); + void handleGeometryChanged(const QRectF& size); private: ChartPresenter* m_presenter; @@ -63,6 +67,7 @@ private: QVector m_data; QHash m_hash; QXYChartSeries* m_series; + QPen m_pen; bool m_dirtyData; bool m_dirtyGeometry; bool m_dirtyDomain;