From b32d31ef1d278a0cf20601cfae8fa25ff4f32d58 2012-04-04 09:09:57 From: Michal Klocek Date: 2012-04-04 09:09:57 Subject: [PATCH] Refactor , move dataset legend presentr connections to one place --- diff --git a/examples/presenterchart/chartview.cpp b/examples/presenterchart/chartview.cpp index 5c72c68..5004629 100644 --- a/examples/presenterchart/chartview.cpp +++ b/examples/presenterchart/chartview.cpp @@ -83,8 +83,8 @@ m_index(-1),m_chart(chart) QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&))); } //![4] - QTimer::singleShot(0,this,SLOT(handleTimeout())); m_timer.start(); + handleTimeout(); } ChartView::~ChartView() diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index ef2b61e..371f445 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -64,7 +64,6 @@ ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char m_marginTiny(10), m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) { - createConnections(); } ChartPresenter::~ChartPresenter() @@ -72,14 +71,6 @@ ChartPresenter::~ChartPresenter() delete m_chartTheme; } -void ChartPresenter::createConnections() -{ - QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*))); - QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); - QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*))); - QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); -} - void ChartPresenter::setGeometry(const QRectF& rect) { m_rect = rect; @@ -506,12 +497,14 @@ void ChartPresenter::updateLayout() QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); + legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); + if(m_chartRect!=chartRect){ m_chartRect=chartRect; emit geometryChanged(m_chartRect); } - legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); + } void ChartPresenter::createChartBackgroundItem() diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 338980f..7e0637a 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -81,7 +81,6 @@ public: qreal minimumBottomMargin() const { return m_minBottomMargin; } public: //TODO: fix me - void createConnections(); void resetAllElements(); void createChartBackgroundItem(); void createChartTitleItem(); diff --git a/src/qchart.cpp b/src/qchart.cpp index 112bad0..9661917 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -72,9 +72,8 @@ d_ptr(new QChartPrivate()) d_ptr->m_dataset = new ChartDataSet(this); d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); + d_ptr->createConnections(); //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3); - 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*))); } /*! @@ -346,6 +345,16 @@ QChartPrivate::~QChartPrivate() } +void QChartPrivate::createConnections() +{ + QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); + QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); + QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QSeries*,Domain*))); + QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_presenter,SLOT(handleSeriesRemoved(QSeries*))); + QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QChartAxis*,Domain*))); + QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),m_presenter,SLOT(handleAxisRemoved(QChartAxis*))); +} + #include "moc_qchart.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchart_p.h b/src/qchart_p.h index 8ad64a7..3fbf74b 100644 --- a/src/qchart_p.h +++ b/src/qchart_p.h @@ -47,6 +47,8 @@ struct QChartPrivate ChartDataSet *m_dataset; ChartPresenter *m_presenter; + void createConnections(); + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qlegend.cpp b/src/qlegend.cpp index 388f6df..b2b325a 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -100,8 +100,7 @@ QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), m_minHeight(0), m_width(0), m_height(0), - m_visible(false), - m_dirty(false) + m_visible(false) { setZValue(ChartPresenter::LegendZValue); setFlags(QGraphicsItem::ItemClipsChildrenToShape); @@ -247,11 +246,7 @@ void QLegend::handleSeriesAdded(QSeries *series, Domain *domain) } } - // wait for all series added - if(!m_dirty){ - QTimer::singleShot(0,this,SLOT(updateLayout())); - m_dirty=true; - } + updateLayout(); } /*! @@ -428,7 +423,6 @@ QPointF QLegend::offset() const // this function runs first to set min max values void QLegend::updateLayout() { - m_dirty=false; m_offsetX=0; QList items = m_markers->childItems(); diff --git a/src/qlegend.h b/src/qlegend.h index 2fd4e82..ffcef85 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -106,11 +106,6 @@ private: void appendMarkers(QBarSeries *series); void appendMarkers(QPieSeries *series); void deleteMarkers(QSeries *series); - - - - -private Q_SLOTS: void updateLayout(); private: @@ -136,7 +131,6 @@ private: qreal m_width; qreal m_height; bool m_visible; - bool m_dirty; friend class ScrolledQLegend; // <--- PIMPL };