From 4f21953d9289b7054de0237af9e0b500e700b46d 2012-03-19 08:25:55 From: Michal Klocek Date: 2012-03-19 08:25:55 Subject: [PATCH] Changes background item * removes marin from public iterface * adds padding and backgroundpadding * adds roundess diameter --- diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 88e06e4..223bf84 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -32,7 +32,6 @@ ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char m_dataset(dataset), m_chartTheme(0), m_zoomIndex(0), - m_marginSize(0), m_rect(QRectF(QPoint(0,0),m_chart->size())), m_options(QChart::NoAnimation) { @@ -63,7 +62,7 @@ QRectF ChartPresenter::geometry() const void ChartPresenter::handleGeometryChanged() { QRectF rect(QPoint(0,0),m_chart->size()); - rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); + rect.adjust(m_chart->padding(),m_chart->padding(), -m_chart->padding(), -m_chart->padding()); //rewrite zoom stack for(int i=0;iaxisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); @@ -339,7 +328,7 @@ void ChartPresenter::zoomIn() void ChartPresenter::zoomIn(const QRectF& rect) { QRectF r = rect.normalized(); - r.translate(-m_marginSize, -m_marginSize); + r.translate(-m_chart->padding(), -m_chart->padding()); if(m_animator) { QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height()); diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 4900ceb..0394649 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -72,7 +72,6 @@ private: ChartDataSet* m_dataset; ChartTheme *m_chartTheme; int m_zoomIndex; - int m_marginSize; QMap m_chartItems; QMap m_axisItems; QVector m_zoomStack; diff --git a/src/qchart.cpp b/src/qchart.cpp index 1489e72..0ae74f2 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -3,6 +3,7 @@ #include "qlegend.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" +#include "chartbackground_p.h" #include #include #include @@ -54,7 +55,9 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( m_titleItem(0), m_legend(new QLegend(this)), m_dataset(new ChartDataSet(this)), - m_presenter(new ChartPresenter(this,m_dataset)) + m_presenter(new ChartPresenter(this,m_dataset)), + m_padding(50), + m_backgroundPadding(10) { connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); @@ -173,7 +176,7 @@ QBrush QChart::chartTitleBrush() void QChart::createChartBackgroundItem() { if(!m_backgroundItem) { - m_backgroundItem = new QGraphicsRectItem(this); + m_backgroundItem = new ChartBackground(this); m_backgroundItem->setPen(Qt::NoPen); m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); } @@ -188,25 +191,6 @@ void QChart::createChartTitleItem() } /*! - Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. - \sa setMargin() -*/ -int QChart::margin() const -{ - return m_presenter->margin(); -} - -/*! - Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. - \sa margin() -*/ -void QChart::setMargin(int margin) -{ - m_presenter->setMargin(margin); - updateLayout(); -} - -/*! Sets the \a theme used by the chart for rendering the graphical representation of the data \sa ChartTheme, chartTheme() */ @@ -332,17 +316,17 @@ void QChart::updateLayout() { if(!m_rect.isValid()) return; - QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); + QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding); // recalculate title position if (m_titleItem) { QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); - m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); + m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2); } //recalculate background gradient if (m_backgroundItem) { - m_backgroundItem->setRect(rect); + m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding)); } // recalculate legend position @@ -352,6 +336,37 @@ void QChart::updateLayout() m_legend->setPreferredLayout(QLegend::PreferredLayoutHorizontal); } } + + +int QChart::padding() const +{ + return m_padding; +} + +void QChart::setPadding(int padding) +{ + if(m_padding==padding){ + m_padding = padding; + m_presenter->handleGeometryChanged(); + updateLayout(); + } +} + +void QChart::setBackgroundPadding(int padding) +{ + if(m_backgroundPadding!=padding){ + m_backgroundPadding = padding; + updateLayout(); + } +} + +void QChart::setBackgroundDiameter(int diameter) +{ + createChartBackgroundItem(); + m_backgroundItem->setDimeter(diameter); + m_backgroundItem->update(); +} + #include "moc_qchart.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchart.h b/src/qchart.h index 4adf5f8..f3d4f0e 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -21,6 +21,8 @@ class ChartItem; class ChartDataSet; class ChartPresenter; class QLegend; +class ChartBackground; + class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget { @@ -53,8 +55,6 @@ public: void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached void removeAllSeries(); // deletes series and axis - void setMargin(int margin); - int margin() const; void setChartTheme(QChart::ChartTheme theme); QChart::ChartTheme chartTheme() const; @@ -84,22 +84,30 @@ public: // TODO: take (and give) legend instead of this. QLegend* legend(); + + int padding() const; + protected: void resizeEvent(QGraphicsSceneResizeEvent *event); private: inline void createChartBackgroundItem(); inline void createChartTitleItem(); + void setPadding(int padding); + void setBackgroundPadding(int padding); + void setBackgroundDiameter(int diameter); void updateLayout(); private: Q_DISABLE_COPY(QChart) - QGraphicsRectItem* m_backgroundItem; + ChartBackground* m_backgroundItem; QGraphicsSimpleTextItem* m_titleItem; QRectF m_rect; QLegend* m_legend; ChartDataSet *m_dataset; ChartPresenter *m_presenter; + int m_padding; + int m_backgroundPadding; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 534d9b5..25483a8 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -46,7 +46,6 @@ QChartView::QChartView(QWidget *parent) : setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setScene(m_scene); - m_chart->setMargin(50); m_scene->addItem(m_chart); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); } @@ -126,14 +125,6 @@ void QChartView::zoomOut() } /*! - Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. -*/ -int QChartView::margin() const -{ - return m_chart->margin(); -} - -/*! Sets the chart \a title. A description text that is drawn above the chart. */ void QChartView::setChartTitle(const QString& title) @@ -240,8 +231,8 @@ void QChartView::mousePressEvent(QMouseEvent *event) { if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { - int margin = m_chart->margin(); - QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); + int padding = m_chart->padding(); + QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); if (rect.contains(event->pos())) { m_rubberBandOrigin = event->pos(); @@ -262,8 +253,8 @@ void QChartView::mousePressEvent(QMouseEvent *event) void QChartView::mouseMoveEvent(QMouseEvent *event) { if(m_rubberBand && m_rubberBand->isVisible()) { - int margin = m_chart->margin(); - QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); + int padding = m_chart->padding(); + QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); int width = event->pos().x() - m_rubberBandOrigin.x(); int height = event->pos().y() - m_rubberBandOrigin.y(); if(!m_verticalRubberBand) { diff --git a/src/qchartview.h b/src/qchartview.h index 72ab0c1..0e6d3ea 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -30,7 +30,6 @@ public: void addSeries(QSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached void removeAllSeries(); // deletes series and axis - int margin() const; void setChartTitle(const QString& title); QString chartTitle() const; diff --git a/src/src.pro b/src/src.pro index af0b33a..edcb983 100644 --- a/src/src.pro +++ b/src/src.pro @@ -16,20 +16,22 @@ SOURCES += \ qchartview.cpp \ qseries.cpp \ qlegend.cpp \ - legendmarker.cpp + legendmarker.cpp \ + chartbackground.cpp PRIVATE_HEADERS += \ chartdataset_p.h \ chartitem_p.h \ chartpresenter_p.h \ charttheme_p.h \ domain_p.h \ - legendmarker_p.h + legendmarker_p.h \ + chartbackground_p.h PUBLIC_HEADERS += \ qchart.h \ qchartglobal.h \ qseries.h \ qchartview.h \ - qlegend.h + qlegend.h include(animations/animations.pri) include(axis/axis.pri)