From 683f8b9c197be8b33d291a3248a92e34744c4628 2012-02-08 16:32:26 From: Michal Klocek Date: 2012-02-08 16:32:26 Subject: [PATCH] Adds refactored axis to presenter --- diff --git a/src/axisitem.cpp b/src/axisitem.cpp index be588f6..57ed94c 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -1,4 +1,5 @@ #include "axisitem_p.h" +#include "qchartaxis.h" #include #include @@ -6,8 +7,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : +AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) : ChartItem(parent), + m_axis(axis), m_ticks(4), m_type(type) { @@ -37,17 +39,6 @@ QRectF AxisItem::boundingRect() const return m_rect; } -void AxisItem::setPlotDomain(const PlotDomain& plotDomain) -{ - m_plotDomain = plotDomain; - createItems(); -} - -void AxisItem::setSize(const QSizeF &size) -{ - m_rect = QRectF(QPoint(0,0),size); - createItems(); -} /* void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) @@ -104,15 +95,7 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,Q void AxisItem::createItems() { - //TODO: this is very inefficient handling - - qDeleteAll(m_shades); - m_shades.clear(); - qDeleteAll(m_grid); - m_grid.clear(); - qDeleteAll(m_labels); - m_labels.clear(); - + if(!m_rect.isValid()) return; if(m_type==X_AXIS) { @@ -122,7 +105,7 @@ void AxisItem::createItems() int x = i * deltaX + m_rect.left(); - qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks); + qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks); m_grid<drawRect(m_rect.adjusted(0, 0, -1, -1)); } +void AxisItem::clear() +{ + qDeleteAll(m_shades); + m_shades.clear(); + qDeleteAll(m_grid); + m_grid.clear(); + qDeleteAll(m_labels); + m_labels.clear(); +} + +void AxisItem::updateDomain() +{ + clear(); + createItems(); +} + +void AxisItem::handleAxisChanged() +{ + //m_axis-> +} + +void AxisItem::handleDomainChanged(const Domain& domain) +{ + m_domain = domain; + clear(); + createItems(); +} + +void AxisItem::handleGeometryChanged(const QRectF& rect) +{ + Q_ASSERT(rect.isValid()); + m_rect = rect; + clear(); + createItems(); +} + //TODO "nice numbers algorithm" +#include "moc_axisitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axisitem_p.h b/src/axisitem_p.h index c0dc844..c8dd8a4 100644 --- a/src/axisitem_p.h +++ b/src/axisitem_p.h @@ -1,45 +1,71 @@ #ifndef AXISITEM_H_ #define AXISITEM_H_ -#include "plotdomain_p.h" +#include "domain_p.h" #include "chartitem_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class AxisItem : public ChartItem +class QChartAxis; + +class AxisItem : public QObject, public ChartItem { + Q_OBJECT public: enum AxisType{X_AXIS,Y_AXIS}; - AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); + AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0); ~AxisItem(); //from QGraphicsItem QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){}; +protected slots: + void handleAxisChanged(); + void handleDomainChanged(const Domain& domain); + void handleGeometryChanged(const QRectF& size); + +protected: + void updateDomain(); + +private: + void clear(); + + public: // from ChartItem - void setSize(const QSizeF &size); - void setPlotDomain(const PlotDomain& data); + void setSize(const QSizeF &size){}; + void setPlotDomain(const PlotDomain& data){}; public: void setLength(int length); void setWidth(int width); AxisType axisType() const {return m_type;}; +protected: + + private: void createItems(); private: - QRectF m_rect; + + QChartAxis* m_axis; + AxisType m_type; int m_ticks; - PlotDomain m_plotDomain; + Domain m_domain; + + + + QRectF m_rect; + QPainterPath m_path; + QList m_grid; QList m_shades; QList m_labels; - AxisType m_type; + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 9f05597..25a2140 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -1,4 +1,5 @@ #include "qchart.h" +#include "qchartaxis.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" //series @@ -7,6 +8,7 @@ #include "percentbarchartseries.h" #include "qxychartseries.h" //items +#include "axisitem_p.h" #include "bargroup.h" #include "stackedbargroup.h" #include "xylinechartitem_p.h" @@ -25,14 +27,30 @@ m_domainIndex(0), m_marginSize(0), m_rect(QRectF(QPoint(0,0),m_chart->size())) { - creteConnections(); + createConnections(); + createDeafultAxis(); } ChartPresenter::~ChartPresenter() { } -void ChartPresenter::creteConnections() +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*))); @@ -123,15 +141,15 @@ void ChartPresenter::handleSeriesAdded(QChartSeries* series) 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); + 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); @@ -166,7 +184,7 @@ void ChartPresenter::handleSeriesAdded(QChartSeries* series) void ChartPresenter::handleSeriesChanged(QChartSeries* series) { - //TODO: + //TODO: } void ChartPresenter::zoomInToRect(const QRectF& rect) @@ -178,7 +196,6 @@ void ChartPresenter::zoomInToRect(const QRectF& rect) m_dataset->addDomain(domain); } - void ChartPresenter::zoomIn() { if (!m_dataset->nextDomain()) { @@ -202,25 +219,25 @@ void ChartPresenter::zoomReset() } /* -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()); -} -*/ + 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 a24aaf0..fef89ff 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -11,8 +11,7 @@ class QChartSeries; class ChartDataSet; class QChart; class Domain; -class QXYChartSeries; -class XYLineChartItem; +class AxisItem; class ChartPresenter: public QObject { @@ -37,7 +36,8 @@ public: void zoomReset(); private: - void creteConnections(); + void createConnections(); + void createDeafultAxis(); public slots: void handleSeriesAdded(QChartSeries* series); @@ -54,6 +54,7 @@ private: QChart* m_chart; ChartDataSet* m_dataset; QVector m_domains; + QList m_axis; int m_domainIndex; int m_marginSize; QRectF m_rect; diff --git a/src/qchart.cpp b/src/qchart.cpp index 2655711..51d044e 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -28,7 +28,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), m_backgroundItem(0), m_titleItem(0), - m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)), + m_axisXItem(0), m_plotDataIndex(0), m_chartTheme(new ChartTheme(this)), //m_dataset(0), @@ -42,9 +42,8 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( PlotDomain domain; m_plotDomainList << domain; - m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); - m_chartItems << m_axisXItem; - m_chartItems << m_axisYItem.at(0); + //m_chartItems << m_axisXItem; + //m_chartItems << m_axisYItem.at(0); } QChart::~QChart(){} diff --git a/src/qchartaxis.cpp b/src/qchartaxis.cpp index 66456de..43e2a17 100644 --- a/src/qchartaxis.cpp +++ b/src/qchartaxis.cpp @@ -3,7 +3,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -QChartAxis::QChartAxis(): +QChartAxis::QChartAxis(QObject* parent):QObject(parent), m_axisVisible(true), m_girdVisible(true), m_labelsVisible(true), @@ -18,6 +18,16 @@ QChartAxis::~QChartAxis() // TODO Auto-generated destructor stub } +void QChartAxis::setAxisPen(const QPen& pen) +{ + m_axisPen = pen; +} + +void QChartAxis::setAxisBrush(const QBrush& brush) +{ + m_axisBrush = brush; +} + void QChartAxis::setAxisVisible(bool visible) { m_axisVisible=visible; @@ -38,6 +48,6 @@ void QChartAxis::setRowShadesVisible(bool visible) m_rowShadesVisible=visible; } - +#include "moc_qchartaxis.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartaxis.h b/src/qchartaxis.h index 2e817e3..4f141d5 100644 --- a/src/qchartaxis.h +++ b/src/qchartaxis.h @@ -2,26 +2,70 @@ #define QCHARTAXIS_H_ #include +#include + QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QChartAxis +class QChartAxis : public QObject { + Q_OBJECT + + Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged); + Q_PROPERTY(QPen axisPen READ axisPen WRITE setAxisPen NOTIFY axisPenChanged); + Q_PROPERTY(QBrush axisBrush READ axisBrush WRITE setAxisBrush NOTIFY axisBurshChanged); + + // Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged); public: - QChartAxis(); + enum LabelOrientation{ HORIZONTAL, VERTICAL , SLIDE }; + + QChartAxis(QObject* parent = 0); virtual ~QChartAxis(); + //axis bool isAxisVisible() const { return m_axisVisible;}; void setAxisVisible(bool visible); + void setAxisPen(const QPen& pen); + const QPen& axisPen() const { return m_axisPen;}; + void setAxisBrush(const QBrush& brush); + const QBrush& axisBrush() const { return m_axisBrush;}; + + //grid bool isGridVisible() const { return m_girdVisible;}; void setGridVisible(bool visible); + bool isLabelsVisible() const { return m_labelsVisible;}; void setLabelsVisible(bool visible); + bool isRowShadesVisible() const { return m_rowShadesVisible;}; void setRowShadesVisible(bool visible); + /* + void setLabelFont(const QFont& font); + const QFont& labelFont(); + + void setLabelPen(const QPen& pen); + const QPen& labelPen(); + + void setGridPen(const QPen& pen); + const QPen& gridPen(); + + void setGridBrush(const QBrush& brush); + const QBrush& gridBrush(); + */ + +signals: + void axisVisibilityChanged(); + void axisPenChanged(); + void axisBurshChanged(); + + private: + bool m_axisVisible; + QPen m_axisPen; + QBrush m_axisBrush; + bool m_girdVisible; bool m_labelsVisible; bool m_rowShadesVisible;