From 4f2467dfeb41121b6c97b3ede692a4913edf0026 2012-03-15 14:53:02 From: sauimone Date: 2012-03-15 14:53:02 Subject: [PATCH] legend layouting change --- diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 007e8db..56e097a 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -32,7 +32,7 @@ BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : BarPresenterBase::~BarPresenterBase() { disconnect(this,SLOT(showToolTip(QPoint,QString))); - disconnect(this,SLOT(enableSeparators(bool))); +// disconnect(this,SLOT(enableSeparators(bool))); } void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) diff --git a/src/legendmarker.cpp b/src/legendmarker.cpp index 27ec6c3..aa1abe7 100644 --- a/src/legendmarker.cpp +++ b/src/legendmarker.cpp @@ -2,6 +2,7 @@ #include "legendmarker_p.h" #include #include +#include #include #include @@ -58,6 +59,7 @@ void LegendMarker::setBoundingRect(const QRectF rect) mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height()); mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y ); + qDebug() << "text item bound:" << mTextItem.boundingRect(); } void LegendMarker::setBrush(const QBrush brush) @@ -126,7 +128,9 @@ void LegendMarker::changed() switch (mType) { case LegendMarkerTypeSeries: { - // TODO: + QXYSeries* s = static_cast (mSeries); + setBrush(s->brush()); + setName(s->name()); break; } case LegendMarkerTypeBarset: { diff --git a/src/qchart.cpp b/src/qchart.cpp index ed194b8..bdcba10 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -352,7 +352,10 @@ void QChart::updateLayout() rect.height() + margin() + margin()/2, -margin(), -margin()/2 + m_legend->minimumSize().height())); - m_legend->handleGeometryChanged(boundingRect); +// m_legend->handleGeometryChanged(boundingRect); + QRectF br(0,0,margin(),m_rect.height()); + m_legend->handleGeometryChanged(br); + m_legend->setPreferredLayout(QLegend::PreferredLayoutVertical); } } #include "moc_qchart.cpp" diff --git a/src/qchart.h b/src/qchart.h index ef2f2ab..4adf5f8 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -81,6 +81,7 @@ public: QChartAxis* axisX() const; QChartAxis* axisY() const; + // TODO: take (and give) legend instead of this. QLegend* legend(); protected: diff --git a/src/qlegend.cpp b/src/qlegend.cpp index bc92451..016c110 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -25,6 +25,8 @@ QLegend::QLegend(QGraphicsItem *parent) ,mBoundingRect(0,0,1,1) ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? ,mMinimumSize(50,20) // TODO: magic numbers + ,mMaximumSize(150,100) + ,mPreferredLayout(QLegend::PreferredLayoutVertical) { setVisible(false); } @@ -53,6 +55,12 @@ QBrush QLegend::backgroundBrush() const return mBackgroundBrush; } +void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred) +{ + mPreferredLayout = preferred; + layoutChanged(); +} + QSizeF QLegend::minimumSize() const { return mMinimumSize; @@ -208,14 +216,45 @@ void QLegend::layoutChanged() return; } - qreal steps = mMarkers.count(); - qreal xStep = mBoundingRect.width() / steps; - qreal x=mBoundingRect.x(); - qreal y = mBoundingRect.y() + (mBoundingRect.height()/4); - foreach (LegendMarker* m, mMarkers) { - m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2)); - x += xStep; + // Limit legend size to maximum. + QSizeF size = mBoundingRect.size(); + + + // TODO: grid layout + switch (mPreferredLayout) + { + case QLegend::PreferredLayoutHorizontal: { + + qreal steps = mMarkers.count(); + qreal xStep = size.width() / (steps+1); + qreal x = mBoundingRect.x() + xStep/2; + qreal y = mBoundingRect.y(); // Half of legend marker min size + foreach (LegendMarker* m, mMarkers) { + m->setBoundingRect(QRectF(x,y,xStep,size.height()/2)); + x += xStep; + } + break; } + case QLegend::PreferredLayoutVertical: { + // Limit markers to bounding rect size. + if (size.width() > mBoundingRect.width()) { + size.setWidth(mBoundingRect.width()); + } + qreal steps = mMarkers.count(); + qreal yStep = size.height() / (steps+1); + qreal x = mBoundingRect.x(); + qreal y = mBoundingRect.y() + yStep/2; + foreach (LegendMarker* m, mMarkers) { + m->setBoundingRect(QRectF(x,y,size.width(),yStep)); + y += yStep; + } + break; + } + default: { + break; + } + } + } #include "moc_qlegend.cpp" diff --git a/src/qlegend.h b/src/qlegend.h index ef54ef2..d3a931b 100644 --- a/src/qlegend.h +++ b/src/qlegend.h @@ -20,6 +20,11 @@ class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject Q_OBJECT public: + enum PreferredLayout { + PreferredLayoutHorizontal, + PreferredLayoutVertical + }; + explicit QLegend(QGraphicsItem *parent = 0); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); @@ -28,8 +33,12 @@ public: void setBackgroundBrush(const QBrush& brush); QBrush backgroundBrush() const; + void setPreferredLayout(QLegend::PreferredLayout preferred); + QSizeF minimumSize() const; void setMinimumSize(const QSizeF size); + QSizeF maximumSize() const; + void setMaximumSize(const QSizeF size); signals: // for interactions. @@ -49,7 +58,7 @@ private: void appendMarkers(QBarSeries* series); void appendMarkers(QPieSeries* series); void deleteMarkers(QSeries* series); - void layoutChanged(); + void layoutChanged(); // TODO: rename this to layoutChanged and remove original layoutChanged, when ready // <--- PIMPL @@ -59,6 +68,8 @@ private: QBrush mBackgroundBrush; QSizeF mMinimumSize; + QSizeF mMaximumSize; + QLegend::PreferredLayout mPreferredLayout; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qseries.h b/src/qseries.h index fb6dd79..4c2c77a 100644 --- a/src/qseries.h +++ b/src/qseries.h @@ -23,14 +23,6 @@ public: SeriesTypeSpline }; - // Helper class to contain legend and color for it - // TODO: This is actually quite close to current LegendMarker.. combine them? -/* class LegendEntry { - public: - QString mName; - /QBrush mBrush; - };*/ - protected: QSeries(QObject *parent = 0) : QObject(parent) {m_model = NULL;}