##// END OF EJS Templates
fixed bug in legend name drawing
sauimone -
r585:b761105be1ea
parent child
Show More
@@ -8,11 +8,12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
9 : QGraphicsObject(parent)
9 : QGraphicsObject(parent)
10 ,mBoundingRect(0,0,1,1)
10 ,mBoundingRect(0,0,1,1)
11 ,mName("")
11 ,mMarkerBoundingRect(0,0,1,1)
12 ,mSeries(series)
12 ,mSeries(series)
13 ,mBarset(0)
13 ,mBarset(0)
14 ,mPieslice(0)
14 ,mPieslice(0)
15 ,mType(LegendMarkerTypeSeries)
15 ,mType(LegendMarkerTypeSeries)
16 ,mTextItem(new QGraphicsSimpleTextItem(this))
16 {
17 {
17 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 }
19 }
@@ -20,11 +21,12 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
20 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
21 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
21 : QGraphicsObject(parent)
22 : QGraphicsObject(parent)
22 ,mBoundingRect(0,0,1,1)
23 ,mBoundingRect(0,0,1,1)
23 ,mName("")
24 ,mMarkerBoundingRect(0,0,1,1)
24 ,mSeries(series)
25 ,mSeries(series)
25 ,mBarset(barset)
26 ,mBarset(barset)
26 ,mPieslice(0)
27 ,mPieslice(0)
27 ,mType(LegendMarkerTypeBarset)
28 ,mType(LegendMarkerTypeBarset)
29 ,mTextItem(new QGraphicsSimpleTextItem(this))
28 {
30 {
29 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
31 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
30 }
32 }
@@ -32,11 +34,12 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *pare
32 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
34 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
33 : QGraphicsObject(parent)
35 : QGraphicsObject(parent)
34 ,mBoundingRect(0,0,1,1)
36 ,mBoundingRect(0,0,1,1)
35 ,mName("")
37 ,mMarkerBoundingRect(0,0,1,1)
36 ,mSeries(series)
38 ,mSeries(series)
37 ,mBarset(0)
39 ,mBarset(0)
38 ,mPieslice(pieslice)
40 ,mPieslice(pieslice)
39 ,mType(LegendMarkerTypePieslice)
41 ,mType(LegendMarkerTypePieslice)
42 ,mTextItem(new QGraphicsSimpleTextItem(this))
40 {
43 {
41 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
44 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
42 }
45 }
@@ -44,6 +47,15 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *
44 void LegendMarker::setBoundingRect(const QRectF rect)
47 void LegendMarker::setBoundingRect(const QRectF rect)
45 {
48 {
46 mBoundingRect = rect;
49 mBoundingRect = rect;
50 // Calculate Marker pos
51
52 // TODO: remove hard coding. 5 is marigin around marker
53 QSizeF markerSize(10,10);
54 qreal x = mBoundingRect.x() + 5;
55 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
56 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
57
58 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
47 }
59 }
48
60
49 void LegendMarker::setBrush(const QBrush brush)
61 void LegendMarker::setBrush(const QBrush brush)
@@ -58,12 +70,12 QBrush LegendMarker::brush() const
58
70
59 void LegendMarker::setName(const QString name)
71 void LegendMarker::setName(const QString name)
60 {
72 {
61 mName = name;
73 mTextItem.setText(name);
62 }
74 }
63
75
64 QString LegendMarker::name() const
76 QString LegendMarker::name() const
65 {
77 {
66 return mName;
78 return mTextItem.text();
67 }
79 }
68
80
69 QSeries* LegendMarker::series() const
81 QSeries* LegendMarker::series() const
@@ -74,7 +86,7 QSeries* LegendMarker::series() const
74 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
86 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
75 {
87 {
76 painter->setBrush(mBrush);
88 painter->setBrush(mBrush);
77 painter->drawRect(mBoundingRect);
89 painter->drawRect(mMarkerBoundingRect);
78 }
90 }
79
91
80 QRectF LegendMarker::boundingRect() const
92 QRectF LegendMarker::boundingRect() const
@@ -51,13 +51,15 Q_SIGNALS:
51
51
52 private:
52 private:
53 QRectF mBoundingRect;
53 QRectF mBoundingRect;
54 QRectF mMarkerBoundingRect;
54 QBrush mBrush;
55 QBrush mBrush;
55 QString mName;
56 QSeries* mSeries;
56 QSeries* mSeries;
57 QBarSet* mBarset;
57 QBarSet* mBarset;
58 QPieSlice* mPieslice;
58 QPieSlice* mPieslice;
59
59
60 LegendMarkerType mType;
60 LegendMarkerType mType;
61 QGraphicsSimpleTextItem mTextItem;
62
61 };
63 };
62
64
63 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
@@ -344,11 +344,10 void QChart::updateLayout()
344 // TODO: better layout
344 // TODO: better layout
345 if (m_legend) {
345 if (m_legend) {
346 QRectF boundingRect(m_rect.adjusted(margin(),
346 QRectF boundingRect(m_rect.adjusted(margin(),
347 rect.height() + margin() + margin()/2 - m_legend->minimumSize().height()/2,
347 rect.height() + margin() + margin()/2,
348 -margin(),
348 -margin(),
349 -margin()/2 + m_legend->minimumSize().height()/2));
349 -margin()/2 + m_legend->minimumSize().height()));
350 m_legend->handleGeometryChanged(boundingRect);
350 m_legend->handleGeometryChanged(boundingRect);
351 qDebug() << "legend rect:" << m_legend->boundingRect();
352 }
351 }
353 }
352 }
354 #include "moc_qchart.cpp"
353 #include "moc_qchart.cpp"
@@ -32,12 +32,6 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
32 {
32 {
33 painter->setBrush(mBackgroundBrush);
33 painter->setBrush(mBackgroundBrush);
34 painter->drawRect(mBoundingRect);
34 painter->drawRect(mBoundingRect);
35
36 foreach(LegendMarker* m, mMarkers) {
37 QRectF r = m->boundingRect();
38 painter->setBrush(m->brush());
39 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
40 }
41 }
35 }
42
36
43 QRectF QLegend::boundingRect() const
37 QRectF QLegend::boundingRect() const
@@ -211,19 +205,12 void QLegend::layoutChanged()
211 return;
205 return;
212 }
206 }
213
207
214 // TODO: marker defined by series.
215 QSizeF markerSize(10,10);
216
217 // TODO: better layout, this is just concept.
218 // Leave some space around markers like this: | x x x x |
219 qreal steps = mMarkers.count();
208 qreal steps = mMarkers.count();
220
221 qreal xStep = mBoundingRect.width() / steps;
209 qreal xStep = mBoundingRect.width() / steps;
222 qreal yStep = mBoundingRect.height() / steps;
210 qreal x=mBoundingRect.x();
223 qreal x = mBoundingRect.x() + 5;
211 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
224 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
225 foreach (LegendMarker* m, mMarkers) {
212 foreach (LegendMarker* m, mMarkers) {
226 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
213 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
227 x += xStep;
214 x += xStep;
228 }
215 }
229 }
216 }
General Comments 0
You need to be logged in to leave comments. Login now