From c19659d852a492da476bc3aa0941a90ccbeb39ca 2012-02-09 14:16:19 From: Michal Klocek Date: 2012-02-09 14:16:19 Subject: [PATCH] Add public function for axis hadnling to qchart --- diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index dfa9875..6f7896e 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -26,36 +26,32 @@ m_dataset(dataset), m_chartTheme(0), m_domainIndex(0), m_marginSize(0), +m_axisX(new QChartAxis(this)), +m_axisY(new QChartAxis(this)), m_rect(QRectF(QPoint(0,0),m_chart->size())) { setChartTheme(QChart::ChartThemeDefault); + m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart); + m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart); createConnections(); - createDeafultAxis(); } ChartPresenter::~ChartPresenter() { } -void ChartPresenter::createDeafultAxis() -{ - //default axis - QChartAxis* axisX = new QChartAxis(this); - QChartAxis* axisY = new QChartAxis(this); - - m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart); - m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart); - - foreach(AxisItem* item, m_axis) { - 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&))); - } -} - void ChartPresenter::createConnections() { QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); + + QMapIterator i(m_axisItems); + + while (i.hasNext()) { + i.next(); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&))); + } } void ChartPresenter::handleGeometryChanged() @@ -226,14 +222,49 @@ QChart::ChartTheme ChartPresenter::chartTheme() return m_chartTheme->id(); } -void ChartPresenter::setAxisX(QChartAxis* axis) +QChartAxis* ChartPresenter::axisX() { + return m_axisX; +} +QChartAxis* ChartPresenter::axisY() +{ + return m_axisY; +} + +QChartAxis* ChartPresenter::addAxisX() +{ + //only one axis + if(m_axisX==0){ + m_axisX = new QChartAxis(this); + m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart); + } + return m_axisX; } -void ChartPresenter::addAxisY(QChartAxis* axis) +QChartAxis* ChartPresenter::addAxisY() { + if(m_axisY==0){ + m_axisY = new QChartAxis(this); + m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart); + return m_axisY; + } + + QChartAxis* axis = new QChartAxis(this); + m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart); + return axis; +} +void ChartPresenter::removeAxis(QChartAxis* axis) +{ + AxisItem* item = m_axisItems.take(axis); + if(item){ + delete item; + delete axis; + } + //reset pointers to default ones + if(axis == m_axisX) m_axisX=0; + else if(axis == m_axisY) m_axisY=0; } /* diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index e89ea17..816f056 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -33,11 +33,14 @@ public: void setChartTheme(QChart::ChartTheme theme); QChart::ChartTheme chartTheme(); - void setAxisX(QChartAxis* axis); - void addAxisY(QChartAxis* axis); + QChartAxis* axisX(); + QChartAxis* axisY(); + QChartAxis* addAxisX(); + QChartAxis* addAxisY(); + void removeAxis(QChartAxis* axis); + private: void createConnections(); - void createDeafultAxis(); public slots: void handleSeriesAdded(QChartSeries* series); @@ -51,11 +54,13 @@ signals: private: QMap m_chartItems; + QMap m_axisItems; QChart* m_chart; ChartDataSet* m_dataset; QVector m_domains; - QList m_axis; ChartTheme *m_chartTheme; + QChartAxis* m_axisX; + QChartAxis* m_axisY; int m_domainIndex; int m_marginSize; QRectF m_rect; diff --git a/src/qchart.cpp b/src/qchart.cpp index ec7326b..a727c6e 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -145,13 +145,29 @@ void QChart::zoomReset() m_presenter->zoomReset(); } -void QChart::setAxisX(QChartAxis* axis) +QChartAxis* QChart::axisX() { - m_presenter->setAxisX(axis); + return m_presenter->axisX(); } -void QChart::addAxisY(QChartAxis* axis) + +QChartAxis* QChart::axisY() +{ + return m_presenter->axisY(); +} + +QChartAxis* QChart::addAxisX() +{ + return m_presenter->addAxisX(); +} + +QChartAxis* QChart::addAxisY() +{ + return m_presenter->addAxisY(); +} + +void QChart::removeAxis(QChartAxis* axis) { - m_presenter->addAxisY(axis); + m_presenter->removeAxis(axis); } void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) diff --git a/src/qchart.h b/src/qchart.h index 7031ee9..b881f57 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -67,16 +67,16 @@ public: void zoomOut(); void zoomReset(); - void setAxisX(QChartAxis* axis); - void addAxisY(QChartAxis* axis); + QChartAxis* axisX(); + QChartAxis* axisY(); + QChartAxis* addAxisX(); + QChartAxis* addAxisY(); + void removeAxis(QChartAxis* axis); protected: void resizeEvent(QGraphicsSceneResizeEvent *event); private: - void setAxis(AxisItem *item, const QChartAxis& axis); - -private: Q_DISABLE_COPY(QChart) QGraphicsRectItem* m_backgroundItem; QGraphicsTextItem* m_titleItem; diff --git a/src/qchartaxis.h b/src/qchartaxis.h index 4f141d5..1582562 100644 --- a/src/qchartaxis.h +++ b/src/qchartaxis.h @@ -54,6 +54,7 @@ public: const QBrush& gridBrush(); */ + signals: void axisVisibilityChanged(); void axisPenChanged(); diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 5119e4d..83e2af9 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -199,4 +199,29 @@ QChart::ChartTheme QChartView::chartTheme() const return m_chart->chartTheme(); } +QChartAxis* QChartView::axisX() +{ + return m_chart->axisX(); +} + +QChartAxis* QChartView::axisY() +{ + return m_chart->axisY(); +} + +QChartAxis* QChartView::addAxisX() +{ + return m_chart->addAxisX(); +} + +QChartAxis* QChartView::addAxisY() +{ + return m_chart->addAxisY(); +} + +void QChartView::removeAxis(QChartAxis* axis) +{ + m_chart->removeAxis(axis); +} + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 991a423..24a4217 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -45,6 +45,12 @@ public: void setChartTheme(QChart::ChartTheme theme); QChart::ChartTheme chartTheme() const; + QChartAxis* axisX(); + QChartAxis* axisY(); + QChartAxis* addAxisX(); + QChartAxis* addAxisY(); + void removeAxis(QChartAxis* axis); + protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);