From a2705c21cb29673a546496bf6346d8af6b5cbb55 2012-03-29 14:55:31 From: sauimone Date: 2012-03-29 14:55:31 Subject: [PATCH] legend pos to theme example, legend padding --- diff --git a/demos/chartthemes/themewidget.cpp b/demos/chartthemes/themewidget.cpp index aa5008f..e460d2f 100644 --- a/demos/chartthemes/themewidget.cpp +++ b/demos/chartthemes/themewidget.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -67,7 +68,9 @@ ThemeWidget::ThemeWidget(QWidget* parent) : m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)), m_themeComboBox(createThemeBox()), m_antialiasCheckBox(new QCheckBox("Anti aliasing")), - m_animatedComboBox(createAnimationBox()) + m_animatedComboBox(createAnimationBox()), + m_legendComboBox(createLegendBox()) + { connectSignals(); @@ -78,6 +81,7 @@ ThemeWidget::ThemeWidget(QWidget* parent) : settingsLayout->addWidget(m_themeComboBox); settingsLayout->addWidget(new QLabel("Animation:")); settingsLayout->addWidget(m_animatedComboBox); + settingsLayout->addWidget(m_legendComboBox); settingsLayout->addWidget(m_antialiasCheckBox); settingsLayout->addStretch(); baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); @@ -123,6 +127,7 @@ void ThemeWidget::connectSignals() connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); + connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); } DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const @@ -172,6 +177,16 @@ QComboBox* ThemeWidget::createAnimationBox() const return animationComboBox; } +QComboBox* ThemeWidget::createLegendBox() const +{ + QComboBox* legendComboBox = new QComboBox(); + legendComboBox->addItem("Legend Top", QLegend::AlignmentTop); + legendComboBox->addItem("Legend Bottom", QLegend::AlignmentBottom); + legendComboBox->addItem("Legend Left", QLegend::AlignmentLeft); + legendComboBox->addItem("Legend Right", QLegend::AlignmentRight); + return legendComboBox; +} + QChart* ThemeWidget::createAreaChart() const { // area chart @@ -342,5 +357,11 @@ void ThemeWidget::updateUI() foreach (QChartView *chartView, m_charts) chartView->chart()->setAnimationOptions(options); } + + QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); + foreach (QChartView *chartView, m_charts) { + qDebug() << alignment; + chartView->chart()->legend()->setAlignmnent(alignment); + } } diff --git a/demos/chartthemes/themewidget.h b/demos/chartthemes/themewidget.h index 207f752..4a17c98 100644 --- a/demos/chartthemes/themewidget.h +++ b/demos/chartthemes/themewidget.h @@ -70,6 +70,7 @@ private: DataTable generateRandomData(int listCount,int valueMax,int valueCount) const; QComboBox* createThemeBox() const; QComboBox* createAnimationBox() const; + QComboBox* createLegendBox() const; void connectSignals(); QChart* createAreaChart() const; QChart* createBarChart(int valueCount) const; @@ -88,6 +89,7 @@ private: QComboBox *m_themeComboBox; QCheckBox *m_antialiasCheckBox; QComboBox *m_animatedComboBox; + QComboBox *m_legendComboBox; }; #endif /* THEMEWINDOW_H_ */ diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 7a4c43d..56759b4 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -19,7 +19,6 @@ ****************************************************************************/ #include "qchart.h" -#include "qlegend.h" #include "qchartaxis.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" @@ -55,7 +54,6 @@ ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char m_rect(QRectF(QPoint(0,0),m_chart->size())), m_options(QChart::NoAnimation), m_themeForce(false), - m_padding(50), m_backgroundPadding(10) { createConnections(); @@ -79,55 +77,8 @@ void ChartPresenter::createConnections() void ChartPresenter::handleGeometryChanged() { QRectF rect(QPoint(0,0),m_chart->size()); - QLegend* legend = m_chart->legend(); - if ((legend->attachedToChart()) && (legend->isVisible())) { - - // Reserve some space for legend - switch (m_chart->legend()->alignment()) { - case QLegend::AlignmentTop: { - rect.adjust(m_padding, - m_padding + legend->size().height(), - -m_padding, - -m_padding); - break; - } - case QLegend::AlignmentBottom: { - rect.adjust(m_padding, - m_padding, - -m_padding, - -m_padding - legend->size().height()); - break; - } - case QLegend::AlignmentLeft: { - rect.adjust(m_padding + legend->size().width(), - m_padding, - -m_padding, - -m_padding); - break; - } - case QLegend::AlignmentRight: { - rect.adjust(m_padding, - m_padding, - -m_padding - legend->size().width(), - -m_padding); - break; - } - default: { - rect.adjust(m_padding, - m_padding, - -m_padding, - -m_padding); - break; - } - } - } else { - - // Legend is detached, or not visible - rect.adjust(m_padding, - m_padding, - -m_padding, - -m_padding); - } + QRectF padding = m_chart->padding(); + rect.adjust(padding.left(), padding.top(), -padding.right(), -padding.bottom()); //rewrite zoom stack /* @@ -393,7 +344,7 @@ void ChartPresenter::zoomIn() void ChartPresenter::zoomIn(const QRectF& rect) { QRectF r = rect.normalized(); - r.translate(-m_padding, -m_padding); + r.translate(-m_chart->padding().topLeft()); if(m_animator) { QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height()); @@ -414,8 +365,8 @@ void ChartPresenter::zoomOut() QSizeF size = geometry().size(); QRectF rect = geometry(); - rect.translate(-m_padding, -m_padding); - m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); + rect.translate(-m_chart->padding().topLeft()); + m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); if(m_animator){ diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 1409827..a8bcf87 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -55,7 +55,6 @@ public: ChartPresenter(QChart* chart,ChartDataSet *dataset); virtual ~ChartPresenter(); - int padding() const { return m_padding; } int backgroundPadding() const { return m_backgroundPadding; } QRectF geometry() const { return m_rect; } ChartAnimator* animator() const { return m_animator; } @@ -98,7 +97,6 @@ private: QRectF m_rect; QChart::AnimationOptions m_options; bool m_themeForce; - int m_padding; int m_backgroundPadding; }; diff --git a/src/qchart.cpp b/src/qchart.cpp index 13f719a..c73da90 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -73,8 +73,7 @@ d_ptr(new QChartPrivate(this)) 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); - int padding = d_ptr->m_presenter->padding(); - setMinimumSize(3*padding,3*padding); + 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*))); } @@ -269,6 +268,12 @@ QLegend* QChart::legend() const return d_ptr->m_legend; } +QRectF QChart::padding() const +{ + return d_ptr->m_padding; +} + + /*! Resizes and updates the chart area using the \a event data */ @@ -336,7 +341,8 @@ m_backgroundItem(0), m_titleItem(0), m_legend(0), m_dataset(0), -m_presenter(0) +m_presenter(0), +m_padding(QRectF(50,50,50,50)) { } @@ -365,10 +371,65 @@ void QChartPrivate::createChartTitleItem() void QChartPrivate::updateLegendLayout() { - int padding = m_presenter->padding(); - QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding); + //int legendPadding = m_chart->legend()->padding(); + int legendPadding = 30; + QRectF rect = m_rect; + + if ((m_legend->attachedToChart()) && (m_legend->isVisible())) { + + // Reserve some space for legend + switch (m_legend->alignment()) { + case QLegend::AlignmentTop: { + rect.adjust(m_padding.left(), + m_padding.top() + legendPadding, + -m_padding.right(), + -m_padding.bottom()); + break; + } + case QLegend::AlignmentBottom: { + rect.adjust(m_padding.left(), + m_padding.top(), + -m_padding.right(), + -m_padding.bottom() - legendPadding); + break; + } + case QLegend::AlignmentLeft: { + rect.adjust(m_padding.left() + legendPadding, + m_padding.top(), + -m_padding.right(), + -m_padding.bottom()); + break; + } + case QLegend::AlignmentRight: { + rect.adjust(m_padding.left(), + m_padding.top(), + -m_padding.right() - legendPadding, + -m_padding.bottom()); + break; + } + default: { + rect.adjust(m_padding.left(), + m_padding.top(), + -m_padding.right(), + -m_padding.bottom()); + break; + } + } + } else { + + rect.adjust(m_padding.left(), + m_padding.top(), + -m_padding.right(), + -m_padding.bottom()); + } + + QRectF plotRect = m_rect.adjusted(m_padding.left() + ,m_padding.top() + ,-m_padding.right() + ,-m_padding.bottom()); QRectF legendRect; + int padding = 0; // TODO: fix this switch (m_legend->alignment()) { case QLegend::AlignmentTop: { @@ -406,7 +467,6 @@ void QChartPrivate::updateLegendLayout() pos.setY(pos.y() + height/2); } - qDebug() << "lenged topleft:" << pos; m_legend->setPos(pos); } @@ -414,7 +474,7 @@ void QChartPrivate::updateLayout() { if (!m_rect.isValid()) return; - int padding = m_presenter->padding(); + int padding = m_padding.top(); int backgroundPadding = m_presenter->backgroundPadding(); // recalculate title position diff --git a/src/qchart.h b/src/qchart.h index 464a25c..aa5a034 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -99,6 +99,8 @@ public: QLegend* legend() const; + QRectF padding() const; + protected: void resizeEvent(QGraphicsSceneResizeEvent *event); diff --git a/src/qchart_p.h b/src/qchart_p.h index a28a2af..40db1a8 100644 --- a/src/qchart_p.h +++ b/src/qchart_p.h @@ -57,6 +57,7 @@ struct QChartPrivate QLegend* m_legend; ChartDataSet *m_dataset; ChartPresenter *m_presenter; + QRectF m_padding; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.cpp b/src/qchartview.cpp index b962f8c..155161b 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -117,7 +117,7 @@ void QChartView::mousePressEvent(QMouseEvent *event) { if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { - int padding = d_ptr->m_presenter->padding(); + int padding = d_ptr->m_chart->padding().top(); QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); if (rect.contains(event->pos())) { @@ -139,7 +139,7 @@ void QChartView::mousePressEvent(QMouseEvent *event) void QChartView::mouseMoveEvent(QMouseEvent *event) { if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { - int padding = d_ptr->m_presenter->padding(); + int padding = d_ptr->m_chart->padding().top(); QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x(); int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y(); diff --git a/src/qlegend.cpp b/src/qlegend.cpp index 1e8f9c1..0ddcbde 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -95,7 +95,8 @@ QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), m_brush(Qt::darkGray), // TODO: default should come from theme m_alignment(QLegend::AlignmentTop), mFirstMarker(0), - m_attachedToChart(true) + m_attachedToChart(true), + m_chart(chart) { m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this); m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this); @@ -169,16 +170,19 @@ QPen QLegend::pen() const Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart. \sa QLegend::Layout */ -void QLegend::setAlignmnent(QLegend::Alignment alignment) +void QLegend::setAlignmnent(QLegend::Alignments alignment) { - m_alignment = alignment; - updateLayout(); +// if (!m_attachedToChart) { + m_alignment = alignment; + updateLayout(); + m_chart->resize(m_chart->size()); +// } } /*! Returns the preferred layout for legend */ -QLegend::Alignment QLegend::alignment() const +QLegend::Alignments QLegend::alignment() const { return m_alignment; } diff --git a/src/qlegend.h b/src/qlegend.h index 773ae27..3e5923e 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -52,6 +52,9 @@ public: AlignmentLeft = Qt::AlignLeft, AlignmentRight = Qt::AlignRight }; + + Q_DECLARE_FLAGS(Alignments, Alignment) + private: explicit QLegend(QChart *chart); @@ -65,8 +68,8 @@ public: void setPen(const QPen &pen); QPen pen() const; - void setAlignmnent(QLegend::Alignment alignment); - QLegend::Alignment alignment() const; + void setAlignmnent(QLegend::Alignments alignment); + QLegend::Alignments alignment() const; QSizeF maximumSize() const; void setMaximumSize(const QSizeF size); @@ -86,6 +89,7 @@ Q_SIGNALS: void clicked(QSeries *series, Qt::MouseButton button); void clicked(QBarSet *barset, Qt::MouseButton button); void clicked(QPieSlice *slice, Qt::MouseButton button); + void legendGeometryChanged(); public Q_SLOTS: // PIMPL ---> @@ -120,7 +124,7 @@ private: QBrush m_brush; QPen m_pen; - QLegend::Alignment m_alignment; + QLegend::Alignments m_alignment; int mFirstMarker; @@ -131,6 +135,7 @@ private: bool m_attachedToChart; + QChart *m_chart; friend class QChart; // <--- PIMPL };