##// 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 32 BarPresenterBase::~BarPresenterBase()
33 33 {
34 34 disconnect(this,SLOT(showToolTip(QPoint,QString)));
35 disconnect(this,SLOT(enableSeparators(bool)));
35 // disconnect(this,SLOT(enableSeparators(bool)));
36 36 }
37 37
38 38 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -2,6 +2,7
2 2 #include "legendmarker_p.h"
3 3 #include <qpieslice.h>
4 4 #include <qbarset.h>
5 #include <qxyseries.h>
5 6 #include <QPainter>
6 7 #include <QGraphicsSceneEvent>
7 8
@@ -58,6 +59,7 void LegendMarker::setBoundingRect(const QRectF rect)
58 59 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
59 60
60 61 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
62 qDebug() << "text item bound:" << mTextItem.boundingRect();
61 63 }
62 64
63 65 void LegendMarker::setBrush(const QBrush brush)
@@ -126,7 +128,9 void LegendMarker::changed()
126 128 switch (mType)
127 129 {
128 130 case LegendMarkerTypeSeries: {
129 // TODO:
131 QXYSeries* s = static_cast<QXYSeries*> (mSeries);
132 setBrush(s->brush());
133 setName(s->name());
130 134 break;
131 135 }
132 136 case LegendMarkerTypeBarset: {
@@ -352,7 +352,10 void QChart::updateLayout()
352 352 rect.height() + margin() + margin()/2,
353 353 -margin(),
354 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 361 #include "moc_qchart.cpp"
@@ -81,6 +81,7 public:
81 81 QChartAxis* axisX() const;
82 82 QChartAxis* axisY() const;
83 83
84 // TODO: take (and give) legend instead of this.
84 85 QLegend* legend();
85 86
86 87 protected:
@@ -25,6 +25,8 QLegend::QLegend(QGraphicsItem *parent)
25 25 ,mBoundingRect(0,0,1,1)
26 26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
27 27 ,mMinimumSize(50,20) // TODO: magic numbers
28 ,mMaximumSize(150,100)
29 ,mPreferredLayout(QLegend::PreferredLayoutVertical)
28 30 {
29 31 setVisible(false);
30 32 }
@@ -53,6 +55,12 QBrush QLegend::backgroundBrush() const
53 55 return mBackgroundBrush;
54 56 }
55 57
58 void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred)
59 {
60 mPreferredLayout = preferred;
61 layoutChanged();
62 }
63
56 64 QSizeF QLegend::minimumSize() const
57 65 {
58 66 return mMinimumSize;
@@ -208,14 +216,45 void QLegend::layoutChanged()
208 216 return;
209 217 }
210 218
211 qreal steps = mMarkers.count();
212 qreal xStep = mBoundingRect.width() / steps;
213 qreal x=mBoundingRect.x();
214 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
215 foreach (LegendMarker* m, mMarkers) {
216 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
217 x += xStep;
219 // Limit legend size to maximum.
220 QSizeF size = mBoundingRect.size();
221
222
223 // TODO: grid layout
224 switch (mPreferredLayout)
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 260 #include "moc_qlegend.cpp"
@@ -20,6 +20,11 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
20 20 Q_OBJECT
21 21 public:
22 22
23 enum PreferredLayout {
24 PreferredLayoutHorizontal,
25 PreferredLayoutVertical
26 };
27
23 28 explicit QLegend(QGraphicsItem *parent = 0);
24 29
25 30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
@@ -28,8 +33,12 public:
28 33 void setBackgroundBrush(const QBrush& brush);
29 34 QBrush backgroundBrush() const;
30 35
36 void setPreferredLayout(QLegend::PreferredLayout preferred);
37
31 38 QSizeF minimumSize() const;
32 39 void setMinimumSize(const QSizeF size);
40 QSizeF maximumSize() const;
41 void setMaximumSize(const QSizeF size);
33 42
34 43 signals:
35 44 // for interactions.
@@ -49,7 +58,7 private:
49 58 void appendMarkers(QBarSeries* series);
50 59 void appendMarkers(QPieSeries* series);
51 60 void deleteMarkers(QSeries* series);
52 void layoutChanged();
61 void layoutChanged(); // TODO: rename this to layoutChanged and remove original layoutChanged, when ready
53 62 // <--- PIMPL
54 63
55 64
@@ -59,6 +68,8 private:
59 68
60 69 QBrush mBackgroundBrush;
61 70 QSizeF mMinimumSize;
71 QSizeF mMaximumSize;
72 QLegend::PreferredLayout mPreferredLayout;
62 73 };
63 74
64 75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -23,14 +23,6 public:
23 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 26 protected:
35 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