##// END OF EJS Templates
legend layouting change
sauimone -
r616:4f2467dfeb41
parent child
Show More
@@ -32,7 +32,7 BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) :
32 BarPresenterBase::~BarPresenterBase()
32 BarPresenterBase::~BarPresenterBase()
33 {
33 {
34 disconnect(this,SLOT(showToolTip(QPoint,QString)));
34 disconnect(this,SLOT(showToolTip(QPoint,QString)));
35 disconnect(this,SLOT(enableSeparators(bool)));
35 // disconnect(this,SLOT(enableSeparators(bool)));
36 }
36 }
37
37
38 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
38 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -2,6 +2,7
2 #include "legendmarker_p.h"
2 #include "legendmarker_p.h"
3 #include <qpieslice.h>
3 #include <qpieslice.h>
4 #include <qbarset.h>
4 #include <qbarset.h>
5 #include <qxyseries.h>
5 #include <QPainter>
6 #include <QPainter>
6 #include <QGraphicsSceneEvent>
7 #include <QGraphicsSceneEvent>
7
8
@@ -58,6 +59,7 void LegendMarker::setBoundingRect(const QRectF rect)
58 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
59 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
59
60
60 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
61 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
62 qDebug() << "text item bound:" << mTextItem.boundingRect();
61 }
63 }
62
64
63 void LegendMarker::setBrush(const QBrush brush)
65 void LegendMarker::setBrush(const QBrush brush)
@@ -126,7 +128,9 void LegendMarker::changed()
126 switch (mType)
128 switch (mType)
127 {
129 {
128 case LegendMarkerTypeSeries: {
130 case LegendMarkerTypeSeries: {
129 // TODO:
131 QXYSeries* s = static_cast<QXYSeries*> (mSeries);
132 setBrush(s->brush());
133 setName(s->name());
130 break;
134 break;
131 }
135 }
132 case LegendMarkerTypeBarset: {
136 case LegendMarkerTypeBarset: {
@@ -352,7 +352,10 void QChart::updateLayout()
352 rect.height() + margin() + margin()/2,
352 rect.height() + margin() + margin()/2,
353 -margin(),
353 -margin(),
354 -margin()/2 + m_legend->minimumSize().height()));
354 -margin()/2 + m_legend->minimumSize().height()));
355 m_legend->handleGeometryChanged(boundingRect);
355 // m_legend->handleGeometryChanged(boundingRect);
356 QRectF br(0,0,margin(),m_rect.height());
357 m_legend->handleGeometryChanged(br);
358 m_legend->setPreferredLayout(QLegend::PreferredLayoutVertical);
356 }
359 }
357 }
360 }
358 #include "moc_qchart.cpp"
361 #include "moc_qchart.cpp"
@@ -81,6 +81,7 public:
81 QChartAxis* axisX() const;
81 QChartAxis* axisX() const;
82 QChartAxis* axisY() const;
82 QChartAxis* axisY() const;
83
83
84 // TODO: take (and give) legend instead of this.
84 QLegend* legend();
85 QLegend* legend();
85
86
86 protected:
87 protected:
@@ -25,6 +25,8 QLegend::QLegend(QGraphicsItem *parent)
25 ,mBoundingRect(0,0,1,1)
25 ,mBoundingRect(0,0,1,1)
26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
27 ,mMinimumSize(50,20) // TODO: magic numbers
27 ,mMinimumSize(50,20) // TODO: magic numbers
28 ,mMaximumSize(150,100)
29 ,mPreferredLayout(QLegend::PreferredLayoutVertical)
28 {
30 {
29 setVisible(false);
31 setVisible(false);
30 }
32 }
@@ -53,6 +55,12 QBrush QLegend::backgroundBrush() const
53 return mBackgroundBrush;
55 return mBackgroundBrush;
54 }
56 }
55
57
58 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
59 {
60 mPreferredLayout = preferred;
61 layoutChanged();
62 }
63
56 QSizeF QLegend::minimumSize() const
64 QSizeF QLegend::minimumSize() const
57 {
65 {
58 return mMinimumSize;
66 return mMinimumSize;
@@ -208,14 +216,45 void QLegend::layoutChanged()
208 return;
216 return;
209 }
217 }
210
218
211 qreal steps = mMarkers.count();
219 // Limit legend size to maximum.
212 qreal xStep = mBoundingRect.width() / steps;
220 QSizeF size = mBoundingRect.size();
213 qreal x=mBoundingRect.x();
221
214 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
222
215 foreach (LegendMarker* m, mMarkers) {
223 // TODO: grid layout
216 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
224 switch (mPreferredLayout)
217 x += xStep;
225 {
226 case QLegend::PreferredLayoutHorizontal: {
227
228 qreal steps = mMarkers.count();
229 qreal xStep = size.width() / (steps+1);
230 qreal x = mBoundingRect.x() + xStep/2;
231 qreal y = mBoundingRect.y(); // Half of legend marker min size
232 foreach (LegendMarker* m, mMarkers) {
233 m->setBoundingRect(QRectF(x,y,xStep,size.height()/2));
234 x += xStep;
235 }
236 break;
218 }
237 }
238 case QLegend::PreferredLayoutVertical: {
239 // Limit markers to bounding rect size.
240 if (size.width() > mBoundingRect.width()) {
241 size.setWidth(mBoundingRect.width());
242 }
243 qreal steps = mMarkers.count();
244 qreal yStep = size.height() / (steps+1);
245 qreal x = mBoundingRect.x();
246 qreal y = mBoundingRect.y() + yStep/2;
247 foreach (LegendMarker* m, mMarkers) {
248 m->setBoundingRect(QRectF(x,y,size.width(),yStep));
249 y += yStep;
250 }
251 break;
252 }
253 default: {
254 break;
255 }
256 }
257
219 }
258 }
220
259
221 #include "moc_qlegend.cpp"
260 #include "moc_qlegend.cpp"
@@ -20,6 +20,11 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
20 Q_OBJECT
20 Q_OBJECT
21 public:
21 public:
22
22
23 enum PreferredLayout {
24 PreferredLayoutHorizontal,
25 PreferredLayoutVertical
26 };
27
23 explicit QLegend(QGraphicsItem *parent = 0);
28 explicit QLegend(QGraphicsItem *parent = 0);
24
29
25 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
@@ -28,8 +33,12 public:
28 void setBackgroundBrush(const QBrush& brush);
33 void setBackgroundBrush(const QBrush& brush);
29 QBrush backgroundBrush() const;
34 QBrush backgroundBrush() const;
30
35
36 void setPreferredLayout(QLegend::PreferredLayout preferred);
37
31 QSizeF minimumSize() const;
38 QSizeF minimumSize() const;
32 void setMinimumSize(const QSizeF size);
39 void setMinimumSize(const QSizeF size);
40 QSizeF maximumSize() const;
41 void setMaximumSize(const QSizeF size);
33
42
34 signals:
43 signals:
35 // for interactions.
44 // for interactions.
@@ -49,7 +58,7 private:
49 void appendMarkers(QBarSeries* series);
58 void appendMarkers(QBarSeries* series);
50 void appendMarkers(QPieSeries* series);
59 void appendMarkers(QPieSeries* series);
51 void deleteMarkers(QSeries* series);
60 void deleteMarkers(QSeries* series);
52 void layoutChanged();
61 void layoutChanged(); // TODO: rename this to layoutChanged and remove original layoutChanged, when ready
53 // <--- PIMPL
62 // <--- PIMPL
54
63
55
64
@@ -59,6 +68,8 private:
59
68
60 QBrush mBackgroundBrush;
69 QBrush mBackgroundBrush;
61 QSizeF mMinimumSize;
70 QSizeF mMinimumSize;
71 QSizeF mMaximumSize;
72 QLegend::PreferredLayout mPreferredLayout;
62 };
73 };
63
74
64 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -23,14 +23,6 public:
23 SeriesTypeSpline
23 SeriesTypeSpline
24 };
24 };
25
25
26 // Helper class to contain legend and color for it
27 // TODO: This is actually quite close to current LegendMarker.. combine them?
28 /* class LegendEntry {
29 public:
30 QString mName;
31 /QBrush mBrush;
32 };*/
33
34 protected:
26 protected:
35 QSeries(QObject *parent = 0) : QObject(parent) {m_model = NULL;}
27 QSeries(QObject *parent = 0) : QObject(parent) {m_model = NULL;}
36
28
General Comments 0
You need to be logged in to leave comments. Login now