From d2e48e985993e5495df79070fa4afe2698a905c2 2012-03-29 09:13:18 From: sauimone Date: 2012-03-29 09:13:18 Subject: [PATCH] improved legend layout --- diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index be48b8e..611d33a 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -1,4 +1,5 @@ #include "qchart.h" +#include "qlegend.h" #include "qchartaxis.h" #include "chartpresenter_p.h" #include "chartdataset_p.h" @@ -57,8 +58,12 @@ void ChartPresenter::createConnections() void ChartPresenter::handleGeometryChanged() { + qDebug() << "legend h:" << m_chart->legend()->size().height(); QRectF rect(QPoint(0,0),m_chart->size()); - rect.adjust(m_padding,m_padding,-m_padding,-m_padding); + rect.adjust(m_padding, + m_padding + m_chart->legend()->size().height(), + -m_padding, + -m_padding); //rewrite zoom stack /* @@ -273,7 +278,7 @@ void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) m_themeForce = force; m_chartTheme = ChartTheme::createTheme(theme); m_chartTheme->decorate(m_chart,m_themeForce); - m_chartTheme->decorate(&m_chart->legend(),m_themeForce); + m_chartTheme->decorate(m_chart->legend(),m_themeForce); resetAllElements(); } diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 9b13110..07025cb 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -94,7 +94,7 @@ void ChartTheme::decorate(QLegend* legend,bool force) QBrush brush; if (pen == legend->pen() || force){ - //TODO:: legend->setPen(); + legend->setPen(Qt::NoPen); } diff --git a/src/qchart.cpp b/src/qchart.cpp index 50ec346..52c1fff 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -264,39 +264,9 @@ QChartAxis* QChart::axisY() const /*! Returns the legend object of the chart. Ownership stays in chart. */ -QLegend& QChart::legend() const +QLegend* QChart::legend() const { - return *d_ptr->m_legend; -} - -/*! - Gives ownership of legend to user. - */ -QLegend* QChart::takeLegend() -{ - 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) -{ - if (d_ptr->m_legend) { - // Should not happen. - qDebug() << "Warning! Giving more than one legend to chart."; - delete d_ptr->m_legend; - } - - d_ptr->m_legend = legend; - - // Reconnect legend, in case not already connected. - 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*))); + return d_ptr->m_legend; } /*! @@ -424,7 +394,18 @@ void QChartPrivate::updateLegendLayout() } m_legend->setMaximumSize(legendRect.size()); - m_legend->setPos(legendRect.topLeft()); + + qreal width = legendRect.width() - m_legend->size().width(); + qreal height = legendRect.height() - m_legend->size().height(); + + QPointF pos = legendRect.topLeft(); + if (width > 0) { + pos.setX(pos.x() + width/2); + } + if (height > 0) { + pos.setY(pos.y() + height/2); + } + m_legend->setPos(pos); } void QChartPrivate::updateLayout() diff --git a/src/qchart.h b/src/qchart.h index 1850793..ea6477f 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -75,9 +75,9 @@ public: QFont titleFont() const; void setTitleBrush(const QBrush &brush); QBrush titleBrush() const; - void setBackgroundBrush(const QBrush& brush); + void setBackgroundBrush(const QBrush &brush); QBrush backgroundBrush() const; - void setBackgroundPen(const QPen& pen); + void setBackgroundPen(const QPen &pen); QPen backgroundPen() const; void setBackgroundVisible(bool visible); @@ -87,7 +87,7 @@ public: AnimationOptions animationOptions() const; void zoomIn(); - void zoomIn(const QRectF& rect); + void zoomIn(const QRectF &rect); void zoomOut(); void scrollLeft(); void scrollRight(); @@ -97,9 +97,7 @@ public: QChartAxis* axisX() const; QChartAxis* axisY() const; - QLegend& legend() const; - QLegend* takeLegend(); - void giveLegend(QLegend* legend); + QLegend* legend() const; protected: void resizeEvent(QGraphicsSceneResizeEvent *event); diff --git a/src/qlegend.cpp b/src/qlegend.cpp index df8a425..f424794 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -67,14 +67,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Constructs the legend object and sets the parent to \a parent */ QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent), + m_margin(5), m_pos(0,0), - m_size(0,0), m_minimumSize(50,20), // TODO: magic numbers m_maximumSize(150,100), - m_brush(Qt::darkGray), // TODO: from theme? + m_size(m_minimumSize), + m_brush(Qt::darkGray), m_alignment(QLegend::LayoutTop), - mFirstMarker(0), - m_margin(5) + mFirstMarker(0) { m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this); m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this); diff --git a/src/qlegend.h b/src/qlegend.h index 3598721..bf27ce7 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -84,10 +84,11 @@ private: void checkFirstMarkerBounds(); bool scrollButtonsVisible(); + qreal m_margin; QPointF m_pos; - QSizeF m_size; QSizeF m_minimumSize; QSizeF m_maximumSize; + QSizeF m_size; QList m_markers; @@ -102,7 +103,6 @@ private: LegendScrollButton *m_scrollButtonUp; LegendScrollButton *m_scrollButtonDown; - qreal m_margin; // <--- PIMPL };