From 6bd3a3b0fd91e297a0960883d509d5ce6924a645 2012-05-30 09:34:07 From: sauimone Date: 2012-05-30 09:34:07 Subject: [PATCH] legend example to documentation. minor legend fixes --- diff --git a/doc/src/examples-legend.qdoc b/doc/src/examples-legend.qdoc new file mode 100644 index 0000000..9b94278 --- /dev/null +++ b/doc/src/examples-legend.qdoc @@ -0,0 +1,29 @@ +/*! + \example examples/legend + \title Legend Example + \subtitle + + This example shows how to detach legend from chart and how to attach it back. By default the chart + draws the legend inside same view with the chart. In some cases user may want to draw legend to somewhere else. To make this possible the legend can be detached from the chart. Detaching means that chart doesn't draw the legend or try to change it's layout. Detached can then be drawn where user wants, for example in different graphics scene. In this example we do so. + + First we create our chart as usual. + + \snippet ../examples/legend/mainwidget.cpp 1 + + Here we create custom graphics scene, where we want to draw the detached legend. + + \snippet ../examples/legend/mainwidget.cpp 2 + + Add some series to the chart. + + \snippet ../examples/legend/mainwidget.cpp 3 + + Here we detach the legend. After detaching, we set new geometry for it and add it to the custom scene. The custom scene adopts the legend and becomes new parent for it. Note that chart will still update the contents of the legend. + + \snippet ../examples/legend/mainwidget.cpp 4 + + To attach legend back to chart we first remove it from the custom scene. Then we add legend to the scene, where chart is. Last we attach legend to chart. Attaching legend to chart automatically sets the QChart as parent for legend. + + \snippet ../examples/legend/mainwidget.cpp 5 + +*/ diff --git a/doc/src/examples.qdoc b/doc/src/examples.qdoc index 6530210..8e99ca7 100644 --- a/doc/src/examples.qdoc +++ b/doc/src/examples.qdoc @@ -17,6 +17,7 @@
  • Bar chart
  • Custom chart
  • Grouped bar chart
  • +
  • Legend
  • Line chart
  • Model data
  • Percent bar chart
  • diff --git a/examples/legend/mainwidget.cpp b/examples/legend/mainwidget.cpp index 7ff9761..302d019 100644 --- a/examples/legend/mainwidget.cpp +++ b/examples/legend/mainwidget.cpp @@ -13,10 +13,7 @@ QTCOMMERCIALCHART_USE_NAMESPACE MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { - m_setCount = 0; - m_chart = new QChart(); - -//![1] + // Create buttons for ui m_buttonLayout = new QGridLayout(); QPushButton *detachLegendButton = new QPushButton("detach legend"); connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend())); @@ -31,12 +28,15 @@ MainWidget::MainWidget(QWidget *parent) : QPushButton *removeBarsetButton = new QPushButton("remove barset"); connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset())); m_buttonLayout->addWidget(removeBarsetButton, 3, 0); -//![1] // Create chart view with the chart +//![1] + m_chart = new QChart(); m_chartView = new QChartView(m_chart, this); m_chartView->setRubberBand(QChartView::HorizonalRubberBand); +//![1] + // Create custom scene and view, where detached legend will be drawn //![2] m_customView = new QGraphicsView(this); m_customScene = new QGraphicsScene(this); @@ -55,6 +55,7 @@ MainWidget::MainWidget(QWidget *parent) : void MainWidget::createSeries() { +//![3] m_series = new QBarSeries(); addBarset(); addBarset(); @@ -69,54 +70,51 @@ void MainWidget::createSeries() m_chart->axisY()->setNiceNumbersEnabled(true); m_chartView->setRenderHint(QPainter::Antialiasing); +//![3] } void MainWidget::detachLegend() { -//![3] - // Detach legend from chart + // Detach legend from chart and + // put legend to our custom scene +//![4] QLegend *legend = m_chart->legend(); legend->detachFromChart(); legend->setGeometry(m_customView->rect()); -// legend->setAlignment(QLegend::AlignmentLeft); - - // Put legend to our custom scene m_customScene->addItem(legend); -//![3] +//![4] - // This causes redraw - QSize size(1,1); - this->resize(this->size() + size); - this->resize(this->size() - size); + // This forces redraw + QSize delta(1,1); + resize(size() + delta); + resize(size() - delta); } void MainWidget::attachLegend() { -//![4] + // Remove legend from custom scene and put it back to chartview scene. + // Attach legend back to chart, so that layout works. + +//![5] QLegend *legend = m_chart->legend(); if (m_customScene->items().contains(legend)) { - // Remove legend from custom scene and put it back to chartview scene. - // Attach legend back to chart, so that layout works. m_customScene->removeItem(legend); - legend->setParent(m_chart); m_chartView->scene()->addItem(legend); -// legend->setAlignment(QLegend::AlignmentBottom); legend->attachToChart(); } -//![4] +//![5] - // This causes redraw - QSize size(1,1); - this->resize(this->size() + size); - this->resize(this->size() - size); + // This forces redraw + QSize delta(1,1); + resize(size() + delta); + resize(size() - delta); } void MainWidget::addBarset() { - QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_setCount)); - m_setCount++; + QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount())); qreal delta = m_series->barsetCount() * 0.1; *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta); m_series->append(barSet); diff --git a/examples/legend/mainwidget.h b/examples/legend/mainwidget.h index 0226e8e..ab39527 100644 --- a/examples/legend/mainwidget.h +++ b/examples/legend/mainwidget.h @@ -33,17 +33,12 @@ private: QChart *m_chart; QBarSeries *m_series; - QGraphicsScene *m_scene; QChartView *m_chartView; QGridLayout *m_mainLayout; QGridLayout *m_buttonLayout; QGraphicsView *m_customView; QGraphicsScene *m_customScene; - QGraphicsGridLayout *m_customLayout; - - - int m_setCount; }; #endif // MAINWIDGET_H diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 6d8b255..3e3e793 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -316,7 +316,7 @@ void ChartPresenter::updateLayout() QLegend* legend = m_chart->d_ptr->m_legend; // recalculate legend position - if (legend->isAttachedToChart() && legend->isEnabled()) { + if (legend != 0 && legend->isAttachedToChart() && legend->isEnabled()) { QRect legendRect; diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 9d0d163..395462c 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -87,7 +87,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), -d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this)) +d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) { setZValue(ChartPresenter::LegendZValue); setFlags(QGraphicsItem::ItemClipsChildrenToShape); @@ -200,7 +200,7 @@ void QLegend::detachFromChart() */ void QLegend::attachToChart() { - d_ptr->m_attachedToChart = true; + d_ptr->attachToChart(); } /*! @@ -292,19 +292,20 @@ qreal QLegend::minHeight() const //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QLegendPrivate::QLegendPrivate(ChartPresenter* presenter,QLegend *q): -q_ptr(q), -m_presenter(presenter), -m_markers(new QGraphicsItemGroup(q)), -m_alignment(QLegend::AlignmentTop), -m_offsetX(0), -m_offsetY(0), -m_minWidth(0), -m_minHeight(0), -m_width(0), -m_height(0), -m_attachedToChart(true), -m_backgroundVisible(false) +QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): + q_ptr(q), + m_presenter(presenter), + m_chart(chart), + m_markers(new QGraphicsItemGroup(q)), + m_alignment(QLegend::AlignmentTop), + m_offsetX(0), + m_offsetY(0), + m_minWidth(0), + m_minHeight(0), + m_width(0), + m_height(0), + m_attachedToChart(true), + m_backgroundVisible(false) { } @@ -407,6 +408,12 @@ void QLegendPrivate::updateLayout() } } +void QLegendPrivate::attachToChart() +{ + m_attachedToChart = true; + q_ptr->setParent(m_chart); +} + void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) { Q_UNUSED(domain) diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index c2e6453..4678b14 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -34,6 +34,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChart; class ChartPresenter; class QAbstractSeries; @@ -41,11 +42,12 @@ class QLegendPrivate : public QObject { Q_OBJECT public: - QLegendPrivate(ChartPresenter *chart,QLegend *q); + QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q); ~QLegendPrivate(); void setOffset(qreal x, qreal y); void updateLayout(); + void attachToChart(); public Q_SLOTS: void handleSeriesAdded(QAbstractSeries *series, Domain *domain); @@ -56,6 +58,7 @@ public Q_SLOTS: private: QLegend *q_ptr; ChartPresenter *m_presenter; + QChart* m_chart; QGraphicsItemGroup* m_markers; QLegend::Alignments m_alignment; QBrush m_brush;