##// END OF EJS Templates
improved legend layout
sauimone -
r783:d2e48e985993
parent child
Show More
@@ -1,4 +1,5
1 1 #include "qchart.h"
2 #include "qlegend.h"
2 3 #include "qchartaxis.h"
3 4 #include "chartpresenter_p.h"
4 5 #include "chartdataset_p.h"
@@ -57,8 +58,12 void ChartPresenter::createConnections()
57 58
58 59 void ChartPresenter::handleGeometryChanged()
59 60 {
61 qDebug() << "legend h:" << m_chart->legend()->size().height();
60 62 QRectF rect(QPoint(0,0),m_chart->size());
61 rect.adjust(m_padding,m_padding,-m_padding,-m_padding);
63 rect.adjust(m_padding,
64 m_padding + m_chart->legend()->size().height(),
65 -m_padding,
66 -m_padding);
62 67
63 68 //rewrite zoom stack
64 69 /*
@@ -273,7 +278,7 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
273 278 m_themeForce = force;
274 279 m_chartTheme = ChartTheme::createTheme(theme);
275 280 m_chartTheme->decorate(m_chart,m_themeForce);
276 m_chartTheme->decorate(&m_chart->legend(),m_themeForce);
281 m_chartTheme->decorate(m_chart->legend(),m_themeForce);
277 282 resetAllElements();
278 283 }
279 284
@@ -94,7 +94,7 void ChartTheme::decorate(QLegend* legend,bool force)
94 94 QBrush brush;
95 95
96 96 if (pen == legend->pen() || force){
97 //TODO:: legend->setPen();
97 legend->setPen(Qt::NoPen);
98 98 }
99 99
100 100
@@ -264,39 +264,9 QChartAxis* QChart::axisY() const
264 264 /*!
265 265 Returns the legend object of the chart. Ownership stays in chart.
266 266 */
267 QLegend& QChart::legend() const
267 QLegend* QChart::legend() const
268 268 {
269 return *d_ptr->m_legend;
270 }
271
272 /*!
273 Gives ownership of legend to user.
274 */
275 QLegend* QChart::takeLegend()
276 {
277 QLegend* l = d_ptr->m_legend;
278 d_ptr->m_legend = 0;
279 return l;
280 }
281
282 /*!
283 Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one
284 */
285 void QChart::giveLegend(QLegend *legend)
286 {
287 if (d_ptr->m_legend) {
288 // Should not happen.
289 qDebug() << "Warning! Giving more than one legend to chart.";
290 delete d_ptr->m_legend;
291 }
292
293 d_ptr->m_legend = legend;
294
295 // Reconnect legend, in case not already connected.
296 disconnect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
297 disconnect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
298 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
299 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
269 return d_ptr->m_legend;
300 270 }
301 271
302 272 /*!
@@ -424,7 +394,18 void QChartPrivate::updateLegendLayout()
424 394 }
425 395
426 396 m_legend->setMaximumSize(legendRect.size());
427 m_legend->setPos(legendRect.topLeft());
397
398 qreal width = legendRect.width() - m_legend->size().width();
399 qreal height = legendRect.height() - m_legend->size().height();
400
401 QPointF pos = legendRect.topLeft();
402 if (width > 0) {
403 pos.setX(pos.x() + width/2);
404 }
405 if (height > 0) {
406 pos.setY(pos.y() + height/2);
407 }
408 m_legend->setPos(pos);
428 409 }
429 410
430 411 void QChartPrivate::updateLayout()
@@ -97,9 +97,7 public:
97 97 QChartAxis* axisX() const;
98 98 QChartAxis* axisY() const;
99 99
100 QLegend& legend() const;
101 QLegend* takeLegend();
102 void giveLegend(QLegend* legend);
100 QLegend* legend() const;
103 101
104 102 protected:
105 103 void resizeEvent(QGraphicsSceneResizeEvent *event);
@@ -67,14 +67,14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
67 67 Constructs the legend object and sets the parent to \a parent
68 68 */
69 69 QLegend::QLegend(QGraphicsItem *parent) : QGraphicsObject(parent),
70 m_margin(5),
70 71 m_pos(0,0),
71 m_size(0,0),
72 72 m_minimumSize(50,20), // TODO: magic numbers
73 73 m_maximumSize(150,100),
74 m_brush(Qt::darkGray), // TODO: from theme?
74 m_size(m_minimumSize),
75 m_brush(Qt::darkGray),
75 76 m_alignment(QLegend::LayoutTop),
76 mFirstMarker(0),
77 m_margin(5)
77 mFirstMarker(0)
78 78 {
79 79 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
80 80 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
@@ -84,10 +84,11 private:
84 84 void checkFirstMarkerBounds();
85 85 bool scrollButtonsVisible();
86 86
87 qreal m_margin;
87 88 QPointF m_pos;
88 QSizeF m_size;
89 89 QSizeF m_minimumSize;
90 90 QSizeF m_maximumSize;
91 QSizeF m_size;
91 92
92 93 QList<LegendMarker *> m_markers;
93 94
@@ -102,7 +103,6 private:
102 103 LegendScrollButton *m_scrollButtonUp;
103 104 LegendScrollButton *m_scrollButtonDown;
104 105
105 qreal m_margin;
106 106 // <--- PIMPL
107 107 };
108 108
General Comments 0
You need to be logged in to leave comments. Login now