From 344f31f1bb279450b52292ea555ac3791cb684ab 2012-03-27 13:20:01 From: Michal Klocek Date: 2012-03-27 13:20:01 Subject: [PATCH] Adds PIMPL to qchart --- diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp index 983263b..f0b918b 100644 --- a/qmlplugin/declarativechart.cpp +++ b/qmlplugin/declarativechart.cpp @@ -17,7 +17,7 @@ DeclarativeChart::~DeclarativeChart() DeclarativeChart::ChartTheme DeclarativeChart::theme() { - return (ChartTheme) m_chart->chartTheme(); + return (ChartTheme) m_chart->theme(); } void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) diff --git a/qmlplugin/declarativechart.h b/qmlplugin/declarativechart.h index 24afe2f..f9a7ce4 100644 --- a/qmlplugin/declarativechart.h +++ b/qmlplugin/declarativechart.h @@ -34,7 +34,7 @@ public: // From QDeclarativeItem/QGraphicsItem void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); public: - void setTheme(ChartTheme theme) {m_chart->setChartTheme((QChart::ChartTheme) theme);} + void setTheme(ChartTheme theme) {m_chart->setTheme((QChart::ChartTheme) theme);} ChartTheme theme(); public: diff --git a/src/chartbackground_p.h b/src/chartbackground_p.h index 00549ee..8d0a280 100644 --- a/src/chartbackground_p.h +++ b/src/chartbackground_p.h @@ -28,6 +28,8 @@ private: }; +QTCOMMERCIALCHART_END_NAMESPACE + #endif /* CHARTBACKGROUND_H_ */ -QTCOMMERCIALCHART_END_NAMESPACE + diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index e8206ca..d68269e 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -36,7 +36,7 @@ ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char m_themeForce(false) { createConnections(); - setChartTheme(QChart::ChartThemeDefault,false); + setTheme(QChart::ChartThemeDefault,false); } ChartPresenter::~ChartPresenter() @@ -270,7 +270,7 @@ void ChartPresenter::handleSeriesRemoved(QSeries* series) delete item; } -void ChartPresenter::setChartTheme(QChart::ChartTheme theme,bool force) +void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) { if(m_chartTheme && m_chartTheme->id() == theme) return; delete m_chartTheme; @@ -281,7 +281,7 @@ void ChartPresenter::setChartTheme(QChart::ChartTheme theme,bool force) resetAllElements(); } -QChart::ChartTheme ChartPresenter::chartTheme() +QChart::ChartTheme ChartPresenter::theme() { return m_chartTheme->id(); } diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index c72ef70..ed78c12 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -41,11 +41,11 @@ public: QRectF geometry() const; ChartAnimator* animator() const {return m_animator;} - ChartTheme *theme() { return m_chartTheme; } + ChartTheme *chartTheme() { return m_chartTheme; } ChartDataSet *dataSet() { return m_dataset; } - void setChartTheme(QChart::ChartTheme theme,bool force = true); - QChart::ChartTheme chartTheme(); + void setTheme(QChart::ChartTheme theme,bool force = true); + QChart::ChartTheme theme(); void setAnimationOptions(QChart::AnimationOptions options); QChart::AnimationOptions animationOptions() const; diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index 17dd2d7..74462c7 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -51,7 +51,7 @@ void PieChartItem::handleSlicesAdded(QList slices) { bool isEmpty = m_slices.isEmpty(); - presenter()->theme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false); + presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false); foreach (QPieSlice *s, slices) { PieSliceItem* item = new PieSliceItem(this); @@ -72,7 +72,7 @@ void PieChartItem::handleSlicesAdded(QList slices) void PieChartItem::handleSlicesRemoved(QList slices) { - presenter()->theme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false); + presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series), false); foreach (QPieSlice *s, slices) { if (animator()) diff --git a/src/qchart.cpp b/src/qchart.cpp index 467ebdb..68f3cf5 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -1,453 +1,466 @@ #include "qchart.h" -#include "qchartaxis.h" -#include "qlegend.h" -#include "chartpresenter_p.h" -#include "chartdataset_p.h" -#include "chartbackground_p.h" +#include "qchart_p.h" #include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! - \enum QChart::ChartTheme + \enum QChart::ChartTheme - This enum describes the theme used by the chart. + This enum describes the theme used by the chart. - \value ChartThemeDefault Follows the GUI style of the Operating System - \value ChartThemeLight - \value ChartThemeBlueCerulean - \value ChartThemeDark - \value ChartThemeBrownSand - \value ChartThemeBlueNcs - \value ChartThemeIcy - \value ChartThemeScientific - \value ChartThemeCount Not really a theme; the total count of themes. -*/ + \value ChartThemeDefault Follows the GUI style of the Operating System + \value ChartThemeLight + \value ChartThemeBlueCerulean + \value ChartThemeDark + \value ChartThemeBrownSand + \value ChartThemeBlueNcs + \value ChartThemeIcy + \value ChartThemeScientific + \value ChartThemeCount Not really a theme; the total count of themes. + */ /*! - \enum QChart::AnimationOption + \enum QChart::AnimationOption - For enabling/disabling animations. Defaults to NoAnimation. + For enabling/disabling animations. Defaults to NoAnimation. - \value NoAnimation - \value GridAxisAnimations - \value SeriesAnimations - \value AllAnimations -*/ + \value NoAnimation + \value GridAxisAnimations + \value SeriesAnimations + \value AllAnimations + */ /*! - \class QChart - \brief QtCommercial chart API. + \class QChart + \brief QtCommercial chart API. - QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical - representation of different types of QChartSeries and other chart related objects like - QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the - convenience class QChartView instead of QChart. - \sa QChartView -*/ + QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical + representation of different types of QChartSeries and other chart related objects like + QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the + convenience class QChartView instead of QChart. + \sa QChartView + */ /*! - Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. -*/ + Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. + */ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), - m_backgroundItem(0), - m_titleItem(0), - m_legend(new QLegend(this)), - m_dataset(new ChartDataSet(this)), - m_presenter(new ChartPresenter(this,m_dataset)), - m_padding(50), - m_backgroundPadding(10) +d_ptr(new QChartPrivate(this)) { - connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); - connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); + + d_ptr->m_legend = new QLegend(this); + d_ptr->m_dataset = new ChartDataSet(this); + d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); + + connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); + connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); } /*! - Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. -*/ + Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. + */ QChart::~QChart() { //delete first presenter , since this is a root of all the graphical items - delete m_presenter; - m_presenter=0; + delete d_ptr->m_presenter; + d_ptr->m_presenter=0; } /*! - Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. - If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and - the y axis). -*/ + Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. + If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and + the y axis). + */ void QChart::addSeries(QSeries* series, QChartAxis* axisY) { - m_dataset->addSeries(series, axisY); + d_ptr->m_dataset->addSeries(series, axisY); } /*! - Removes the \a series specified in a perameter from the QChartView. - It releses its ownership of the specified QChartSeries object. - It does not delete the pointed QChartSeries data object - \sa addSeries(), removeAllSeries() -*/ + Removes the \a series specified in a perameter from the QChartView. + It releses its ownership of the specified QChartSeries object. + It does not delete the pointed QChartSeries data object + \sa addSeries(), removeAllSeries() + */ void QChart::removeSeries(QSeries* series) { - m_dataset->removeSeries(series); + d_ptr->m_dataset->removeSeries(series); } /*! - Removes all the QChartSeries that have been added to the QChartView - It also deletes the pointed QChartSeries data objects - \sa addSeries(), removeSeries() -*/ + Removes all the QChartSeries that have been added to the QChartView + It also deletes the pointed QChartSeries data objects + \sa addSeries(), removeSeries() + */ void QChart::removeAllSeries() { - m_dataset->removeAllSeries(); + d_ptr->m_dataset->removeAllSeries(); } /*! - Sets the \a brush that is used for painting the background of the chart area. -*/ + Sets the \a brush that is used for painting the background of the chart area. + */ void QChart::setBackgroundBrush(const QBrush& brush) { - createChartBackgroundItem(); - m_backgroundItem->setBrush(brush); - m_backgroundItem->update(); + d_ptr->createChartBackgroundItem(); + d_ptr->m_backgroundItem->setBrush(brush); + d_ptr->m_backgroundItem->update(); } QBrush QChart::backgroundBrush() const { - if(!m_backgroundItem) return QBrush(); - return m_backgroundItem->brush(); + if(!d_ptr->m_backgroundItem) return QBrush(); + return (d_ptr->m_backgroundItem)->brush(); } /*! - Sets the \a pen that is used for painting the background of the chart area. -*/ + Sets the \a pen that is used for painting the background of the chart area. + */ void QChart::setBackgroundPen(const QPen& pen) { - createChartBackgroundItem(); - m_backgroundItem->setPen(pen); - m_backgroundItem->update(); + d_ptr->createChartBackgroundItem(); + d_ptr->m_backgroundItem->setPen(pen); + d_ptr->m_backgroundItem->update(); } QPen QChart::backgroundPen() const { - if(!m_backgroundItem) return QPen(); - return m_backgroundItem->pen(); + if(!d_ptr->m_backgroundItem) return QPen(); + return d_ptr->m_backgroundItem->pen(); } /*! - Sets the chart \a title. The description text that is drawn above the chart. -*/ + Sets the chart \a title. The description text that is drawn above the chart. + */ void QChart::setTitle(const QString& title) { - createChartTitleItem(); - m_titleItem->setText(title); - updateLayout(); + d_ptr->createChartTitleItem(); + d_ptr->m_titleItem->setText(title); + d_ptr->updateLayout(); } /*! - Returns the chart title. The description text that is drawn above the chart. -*/ + Returns the chart title. The description text that is drawn above the chart. + */ QString QChart::title() const { - if(m_titleItem) - return m_titleItem->text(); + if(d_ptr->m_titleItem) + return d_ptr->m_titleItem->text(); else return QString(); } /*! - Sets the \a font that is used for rendering the description text that is rendered above the chart. -*/ + Sets the \a font that is used for rendering the description text that is rendered above the chart. + */ void QChart::setTitleFont(const QFont& font) { - createChartTitleItem(); - m_titleItem->setFont(font); - updateLayout(); + d_ptr->createChartTitleItem(); + d_ptr->m_titleItem->setFont(font); + d_ptr->updateLayout(); } /*! - Sets the \a brush used for rendering the title text. -*/ + Sets the \a brush used for rendering the title text. + */ void QChart::setTitleBrush(const QBrush &brush) { - createChartTitleItem(); - m_titleItem->setBrush(brush); - updateLayout(); + d_ptr->createChartTitleItem(); + d_ptr->m_titleItem->setBrush(brush); + d_ptr->updateLayout(); } /*! - Returns the brush used for rendering the title text. -*/ + Returns the brush used for rendering the title text. + */ QBrush QChart::titleBrush() const { - if(!m_titleItem) return QBrush(); - return m_titleItem->brush(); -} - -void QChart::createChartBackgroundItem() -{ - if(!m_backgroundItem) { - m_backgroundItem = new ChartBackground(this); - m_backgroundItem->setPen(Qt::NoPen); - m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); - } -} - -void QChart::createChartTitleItem() -{ - if(!m_titleItem) { - m_titleItem = new QGraphicsSimpleTextItem(this); - m_titleItem->setZValue(ChartPresenter::BackgroundZValue); - } + if(!d_ptr->m_titleItem) return QBrush(); + return d_ptr->m_titleItem->brush(); } /*! - Sets the \a theme used by the chart for rendering the graphical representation of the data - \sa ChartTheme, chartTheme() -*/ -void QChart::setChartTheme(QChart::ChartTheme theme) + Sets the \a theme used by the chart for rendering the graphical representation of the data + \sa ChartTheme, chartTheme() + */ +void QChart::setTheme(QChart::ChartTheme theme) { - m_presenter->setChartTheme(theme); + d_ptr->m_presenter->setTheme(theme); } /*! - Returns the theme enum used by the chart. - \sa ChartTheme, setChartTheme() -*/ -QChart::ChartTheme QChart::chartTheme() const + Returns the theme enum used by the chart. + \sa ChartTheme, setChartTheme() + */ +QChart::ChartTheme QChart::theme() const { - return m_presenter->chartTheme(); + return d_ptr->m_presenter->theme(); } /*! - Zooms in the view by a factor of 2 -*/ + Zooms in the view by a factor of 2 + */ void QChart::zoomIn() { - m_presenter->zoomIn(); + d_ptr->m_presenter->zoomIn(); } /*! - Zooms in the view to a maximum level at which \a rect is still fully visible. -*/ + Zooms in the view to a maximum level at which \a rect is still fully visible. + */ void QChart::zoomIn(const QRectF& rect) { - if(!rect.isValid()) return; - m_presenter->zoomIn(rect); + d_ptr->m_presenter->zoomIn(rect); } /*! - Restores the view zoom level to the previous one. -*/ + Restores the view zoom level to the previous one. + */ void QChart::zoomOut() { - m_presenter->zoomOut(); + d_ptr->m_presenter->zoomOut(); } /*! - Returns the pointer to the x axis object of the chart -*/ + Returns the pointer to the x axis object of the chart + */ QChartAxis* QChart::axisX() const { - return m_dataset->axisX(); + return d_ptr->m_dataset->axisX(); } /*! - Returns the pointer to the y axis object of the chart -*/ + Returns the pointer to the y axis object of the chart + */ QChartAxis* QChart::axisY() const { - return m_dataset->axisY(); + return d_ptr->m_dataset->axisY(); } /*! - Returns the legend object of the chart. Ownership stays in chart. -*/ + Returns the legend object of the chart. Ownership stays in chart. + */ QLegend& QChart::legend() const { - return *m_legend; + return *d_ptr->m_legend; } /*! - Gives ownership of legend to user. -*/ + Gives ownership of legend to user. + */ QLegend* QChart::takeLegend() { - QLegend* l = m_legend; - m_legend = 0; + QLegend* l = d_ptr->m_legend; + d_ptr->m_legend = 0; return l; } /*! - Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one -*/ -void QChart::giveLegend(QLegend* legend) + Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one + */ +void QChart::giveLegend(QLegend *legend) { - if (m_legend) { + if (d_ptr->m_legend) { // Should not happen. qDebug() << "Warning! Giving more than one legend to chart."; - delete m_legend; + delete d_ptr->m_legend; } - m_legend = legend; + d_ptr->m_legend = legend; // Reconnect legend, in case not already connected. - disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); - disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); - connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); - connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); + disconnect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); + disconnect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); + connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); + connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); } /*! - Resizes and updates the chart area using the \a event data -*/ + Resizes and updates the chart area using the \a event data + */ void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) { - - m_rect = QRectF(QPoint(0,0),event->newSize()); - updateLayout(); + d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize()); + d_ptr->updateLayout(); QGraphicsWidget::resizeEvent(event); update(); } /*! - Sets animation \a options for the chart -*/ + Sets animation \a options for the chart + */ void QChart::setAnimationOptions(AnimationOptions options) { - m_presenter->setAnimationOptions(options); + d_ptr->m_presenter->setAnimationOptions(options); } /*! - Returns animation options for the chart -*/ + Returns animation options for the chart + */ QChart::AnimationOptions QChart::animationOptions() const { - return m_presenter->animationOptions(); + return d_ptr->m_presenter->animationOptions(); } void QChart::scrollLeft() { - m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); + d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); } void QChart::scrollRight() { - m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); + d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); } + void QChart::scrollUp() { - m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1)); + d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1)); } + void QChart::scrollDown() { - m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1)); + d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1)); } -void QChart::updateLayout() +void QChart::setPadding(int padding) { - if(!m_rect.isValid()) return; - - 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 + m_padding/2); + if(d_ptr->m_padding==padding) { + d_ptr->m_padding = padding; + d_ptr->m_presenter->handleGeometryChanged(); + d_ptr->updateLayout(); } +} - //recalculate background gradient - if (m_backgroundItem) { - m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding)); - } +int QChart::padding() const +{ + return d_ptr->m_padding; +} - // recalculate legend position - if (m_legend) { - if (m_legend->parentObject() == this) { - updateLegendLayout(); - } +void QChart::setBackgroundPadding(int padding) +{ + if(d_ptr->m_backgroundPadding!=padding) { + d_ptr->m_backgroundPadding = padding; + d_ptr->updateLayout(); } } -void QChart::updateLegendLayout() +void QChart::setBackgroundDiameter(int diameter) { - QRectF plotRect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding); - QRectF legendRect; + d_ptr->createChartBackgroundItem(); + d_ptr->m_backgroundItem->setDimeter(diameter); + d_ptr->m_backgroundItem->update(); +} - switch (m_legend->preferredLayout()) - { - case QLegend::PreferredLayoutTop:{ -// legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height()); - legendRect = plotRect.adjusted(0,0,0,-m_padding - plotRect.height()); - break; - } - case QLegend::PreferredLayoutBottom: { - legendRect = plotRect.adjusted(m_padding,m_padding + plotRect.height(),-m_padding,0); - break; - } - case QLegend::PreferredLayoutLeft: { - legendRect = plotRect.adjusted(0,m_padding,-m_padding - plotRect.width(),-m_padding); - break; - } - case QLegend::PreferredLayoutRight: { - legendRect = plotRect.adjusted(m_padding + plotRect.width(),m_padding,0,-m_padding); - break; - } - default: { - legendRect = plotRect; - break; - } - } +void QChart::setBackgroundVisible(bool visible) +{ + d_ptr->createChartBackgroundItem(); + d_ptr->m_backgroundItem->setVisible(visible); +} - m_legend->setMaximumSize(legendRect.size()); - m_legend->setPos(legendRect.topLeft()); +bool QChart::isBackgroundVisible() const +{ + if(!d_ptr->m_backgroundItem) return false; + return d_ptr->m_backgroundItem->isVisible(); } +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -int QChart::padding() const +QChartPrivate::QChartPrivate(QChart *parent): +q_ptr(parent), +m_backgroundItem(0), +m_titleItem(0), +m_legend(0), +m_dataset(0), +m_presenter(0), +m_padding(50), +m_backgroundPadding(10) { - return m_padding; + } -void QChart::setPadding(int padding) +QChartPrivate::~QChartPrivate() { - if(m_padding==padding){ - m_padding = padding; - m_presenter->handleGeometryChanged(); - updateLayout(); - } + } -void QChart::setBackgroundPadding(int padding) +void QChartPrivate::createChartBackgroundItem() { - if(m_backgroundPadding!=padding){ - m_backgroundPadding = padding; - updateLayout(); + if(!m_backgroundItem) { + m_backgroundItem = new ChartBackground(q_ptr); + m_backgroundItem->setPen(Qt::NoPen); + m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); } } -void QChart::setBackgroundDiameter(int diameter) +void QChartPrivate::createChartTitleItem() { - createChartBackgroundItem(); - m_backgroundItem->setDimeter(diameter); - m_backgroundItem->update(); + if(!m_titleItem) { + m_titleItem = new QGraphicsSimpleTextItem(q_ptr); + m_titleItem->setZValue(ChartPresenter::BackgroundZValue); + } } -void QChart::setBackgroundVisible(bool visible) +void QChartPrivate::updateLegendLayout() { - createChartBackgroundItem(); - m_backgroundItem->setVisible(visible); + QRectF plotRect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding); + QRectF legendRect; + + switch (m_legend->preferredLayout()) + { + case QLegend::PreferredLayoutTop: { + // legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height()); + legendRect = plotRect.adjusted(0,0,0,-m_padding - plotRect.height()); + break; + } + case QLegend::PreferredLayoutBottom: { + legendRect = plotRect.adjusted(m_padding,m_padding + plotRect.height(),-m_padding,0); + break; + } + case QLegend::PreferredLayoutLeft: { + legendRect = plotRect.adjusted(0,m_padding,-m_padding - plotRect.width(),-m_padding); + break; + } + case QLegend::PreferredLayoutRight: { + legendRect = plotRect.adjusted(m_padding + plotRect.width(),m_padding,0,-m_padding); + break; + } + default: { + legendRect = plotRect; + break; + } + } + + m_legend->setMaximumSize(legendRect.size()); + m_legend->setPos(legendRect.topLeft()); } -bool QChart::isBackgroundVisible() const +void QChartPrivate::updateLayout() { - if(!m_backgroundItem) return false; - return m_backgroundItem->isVisible(); -} + if(!m_rect.isValid()) return; + + 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 + m_padding/2); + } + + //recalculate background gradient + if (m_backgroundItem) { + m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding)); + } + // recalculate legend position + if (m_legend) { + if (m_legend->parentObject() == q_ptr) { + updateLegendLayout(); + } + } +} #include "moc_qchart.cpp" diff --git a/src/qchart.h b/src/qchart.h index 5b288dd..2b85115 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -1,11 +1,8 @@ #ifndef QCHART_H #define QCHART_H -#include -#include +#include #include -#include -#include class QGraphicsSceneResizeEvent; @@ -13,16 +10,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class Axis; class QSeries; -class PlotDomain; -class BarChartItem; class QChartAxis; -class ChartTheme; -class ChartItem; -class ChartDataSet; -class ChartPresenter; class QLegend; -class ChartBackground; - +class QChartPrivate; class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget { @@ -52,12 +42,12 @@ public: QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); ~QChart(); - void addSeries(QSeries* series, QChartAxis* axisY = 0); - void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached - void removeAllSeries(); // deletes series and axis + void addSeries(QSeries *series, QChartAxis *axisY = 0); + void removeSeries(QSeries *series); + void removeAllSeries(); - void setChartTheme(QChart::ChartTheme theme); - QChart::ChartTheme chartTheme() const; + void setTheme(QChart::ChartTheme theme); + QChart::ChartTheme theme() const; void setTitle(const QString& title); QString title() const; @@ -96,25 +86,16 @@ public: protected: void resizeEvent(QGraphicsSceneResizeEvent *event); +protected: + QScopedPointer d_ptr; + private: - inline void createChartBackgroundItem(); - inline void createChartTitleItem(); void setPadding(int padding); void setBackgroundPadding(int padding); void setBackgroundDiameter(int diameter); - void updateLayout(); - void updateLegendLayout(); private: - Q_DISABLE_COPY(QChart) - ChartBackground* m_backgroundItem; - QGraphicsSimpleTextItem* m_titleItem; - QRectF m_rect; - QLegend* m_legend; - ChartDataSet *m_dataset; - ChartPresenter *m_presenter; - int m_padding; - int m_backgroundPadding; + Q_DISABLE_COPY(QChart); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.cpp b/src/qchartview.cpp index d423bdb..639128e 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -322,7 +322,7 @@ void QChartView::keyPressEvent(QKeyEvent *event) */ void QChartView::setChartTheme(QChart::ChartTheme theme) { - m_chart->setChartTheme(theme); + m_chart->setTheme(theme); } /*! @@ -331,7 +331,7 @@ void QChartView::setChartTheme(QChart::ChartTheme theme) */ QChart::ChartTheme QChartView::chartTheme() const { - return m_chart->chartTheme(); + return m_chart->theme(); } /*! diff --git a/src/src.pro b/src/src.pro index 3053cb7..3551eb1 100644 --- a/src/src.pro +++ b/src/src.pro @@ -30,7 +30,8 @@ PRIVATE_HEADERS += \ $$PWD/legendscrollbutton_p.h \ $$PWD/chartbackground_p.h \ $$PWD/chart_p.h \ - $$PWD/chartconfig_p.h + $$PWD/chartconfig_p.h \ + $$PWD/qchart_p.h PUBLIC_HEADERS += \ $$PWD/qchart.h \ $$PWD/qchartglobal.h \ @@ -53,10 +54,8 @@ include(themes/themes.pri) HEADERS += $$PUBLIC_HEADERS HEADERS += $$PRIVATE_HEADERS HEADERS += $$THEMES -INCLUDEPATH += linechart \ - barchart \ - themes \ - . +INCLUDEPATH += ../include + OBJECTS_DIR = $$CHART_BUILD_DIR/lib MOC_DIR = $$CHART_BUILD_DIR/lib UI_DIR = $$CHART_BUILD_DIR/lib