diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index d363813..7a4c43d 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -79,10 +79,55 @@ void ChartPresenter::createConnections() void ChartPresenter::handleGeometryChanged() { QRectF rect(QPoint(0,0),m_chart->size()); - rect.adjust(m_padding, - m_padding + m_chart->legend()->size().height(), - -m_padding, - -m_padding); + 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); + } //rewrite zoom stack /* diff --git a/src/qchart.cpp b/src/qchart.cpp index 52c1fff..90daf8e 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -371,20 +371,20 @@ void QChartPrivate::updateLegendLayout() switch (m_legend->alignment()) { - case QLegend::LayoutTop: { - legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height()); + case QLegend::AlignmentTop: { + legendRect = m_rect.adjusted(0,0,0,-padding - plotRect.height()); break; } - case QLegend::LayoutBottom: { - legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0); + case QLegend::AlignmentBottom: { + legendRect = m_rect.adjusted(padding,padding + plotRect.height(),-padding,0); break; } - case QLegend::LayoutLeft: { - legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding); + case QLegend::AlignmentLeft: { + legendRect = m_rect.adjusted(0,padding,-padding - plotRect.width(),-padding); break; } - case QLegend::LayoutRight: { - legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding); + case QLegend::AlignmentRight: { + legendRect = m_rect.adjusted(padding + plotRect.width(),padding,0,-padding); break; } default: { @@ -405,6 +405,8 @@ void QChartPrivate::updateLegendLayout() if (height > 0) { pos.setY(pos.y() + height/2); } + + qDebug() << "lenged topleft:" << pos; m_legend->setPos(pos); } @@ -428,7 +430,7 @@ void QChartPrivate::updateLayout() // recalculate legend position if (m_legend) { - if (m_legend->parentObject() == q_ptr) { + if ((m_legend->attachedToChart()) && (m_legend->parentObject() == q_ptr)) { updateLegendLayout(); } } diff --git a/src/qlegend.cpp b/src/qlegend.cpp index 5ca6bfa..20eade9 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -93,8 +93,9 @@ QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), m_maximumSize(150,100), m_size(m_minimumSize), m_brush(Qt::darkGray), // TODO: default should come from theme - m_alignment(QLegend::LayoutTop), - mFirstMarker(0) + m_alignment(QLegend::AlignmentRight), + mFirstMarker(0), + m_attachedToChart(true) { m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this); m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this); @@ -168,7 +169,7 @@ 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::Layout alignment) +void QLegend::setAlignmnent(QLegend::Alignment alignment) { m_alignment = alignment; updateLayout(); @@ -177,7 +178,7 @@ void QLegend::setAlignmnent(QLegend::Layout alignment) /*! Returns the preferred layout for legend */ -QLegend::Layout QLegend::alignment() const +QLegend::Alignment QLegend::alignment() const { return m_alignment; } @@ -389,6 +390,30 @@ void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton) } /*! + Detaches the legend from chart. Chart won't change layout of the legend. +*/ +void QLegend::detachFromChart() +{ + m_attachedToChart = false; +} + +/*! + Attaches the legend to chart. Chart may change layout of the legend. +*/ +void QLegend::attachToChart() +{ + m_attachedToChart = true; +} + +/*! + Returns true, if legend is attached to chart. +*/ +bool QLegend::attachedToChart() +{ + return m_attachedToChart; +} + +/*! \internal Helper function. Appends markers from \a series to legend. */ void QLegend::appendMarkers(QAreaSeries* series) @@ -498,8 +523,8 @@ void QLegend::updateLayout() switch (m_alignment) { // Both cases organise items horizontally - case QLegend::LayoutBottom: - case QLegend::LayoutTop: { + case QLegend::AlignmentBottom: + case QLegend::AlignmentTop: { qreal xStep = markerMaxSize.width(); qreal x = m_pos.x() + m_margin; @@ -553,8 +578,8 @@ void QLegend::updateLayout() break; } // Both cases organize items vertically - case QLegend::LayoutLeft: - case QLegend::LayoutRight: { + case QLegend::AlignmentLeft: + case QLegend::AlignmentRight: { qreal yStep = markerMaxSize.height(); qreal x = m_pos.x() + m_margin; qreal y = m_pos.y() + m_margin; @@ -655,7 +680,7 @@ QSizeF QLegend::maximumMarkerSize() */ void QLegend::checkFirstMarkerBounds() { - if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) { + if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) { // Bounds limited by height. int max; if (scrollButtonsVisible()) { @@ -689,9 +714,9 @@ void QLegend::checkFirstMarkerBounds() bool QLegend::scrollButtonsVisible() { // Just a helper to clarify, what the magic below means :) - if ((m_alignment == QLegend::LayoutTop) || (m_alignment == QLegend::LayoutBottom)) { + if ((m_alignment == QLegend::AlignmentTop) || (m_alignment == QLegend::AlignmentBottom)) { return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width()); - } else if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) { + } else if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) { return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height()); } diff --git a/src/qlegend.h b/src/qlegend.h index 8c79266..773ae27 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -40,18 +40,17 @@ class LegendScrollButton; class QSeries; class QChart; -// TODO: This as widget class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget { Q_OBJECT public: // We only support these alignments (for now) - enum Layout { - LayoutTop = Qt::AlignTop, - LayoutBottom = Qt::AlignBottom, - LayoutLeft = Qt::AlignLeft, - LayoutRight = Qt::AlignRight + enum Alignment { + AlignmentTop = Qt::AlignTop, + AlignmentBottom = Qt::AlignBottom, + AlignmentLeft = Qt::AlignLeft, + AlignmentRight = Qt::AlignRight }; private: explicit QLegend(QChart *chart); @@ -66,8 +65,8 @@ public: void setPen(const QPen &pen); QPen pen() const; - void setAlignmnent(QLegend::Layout alignment); - QLegend::Layout alignment() const; + void setAlignmnent(QLegend::Alignment alignment); + QLegend::Alignment alignment() const; QSizeF maximumSize() const; void setMaximumSize(const QSizeF size); @@ -78,6 +77,10 @@ public: void scrollButtonClicked(LegendScrollButton *scrollButton); + void detachFromChart(); + void attachToChart(); + bool attachedToChart(); + Q_SIGNALS: // for interactions. void clicked(QSeries *series, Qt::MouseButton button); @@ -117,7 +120,7 @@ private: QBrush m_brush; QPen m_pen; - QLegend::Layout m_alignment; + QLegend::Alignment m_alignment; int mFirstMarker; @@ -126,6 +129,8 @@ private: LegendScrollButton *m_scrollButtonUp; LegendScrollButton *m_scrollButtonDown; + bool m_attachedToChart; + friend class QChart; // <--- PIMPL };