##// END OF EJS Templates
legend layout with padding
sauimone -
r799:0ecf2967daa4
parent child
Show More
@@ -79,10 +79,55 void ChartPresenter::createConnections()
79 void ChartPresenter::handleGeometryChanged()
79 void ChartPresenter::handleGeometryChanged()
80 {
80 {
81 QRectF rect(QPoint(0,0),m_chart->size());
81 QRectF rect(QPoint(0,0),m_chart->size());
82 QLegend* legend = m_chart->legend();
83 if ((legend->attachedToChart()) && (legend->isVisible())) {
84
85 // Reserve some space for legend
86 switch (m_chart->legend()->alignment()) {
87 case QLegend::AlignmentTop: {
82 rect.adjust(m_padding,
88 rect.adjust(m_padding,
83 m_padding + m_chart->legend()->size().height(),
89 m_padding + legend->size().height(),
90 -m_padding,
91 -m_padding);
92 break;
93 }
94 case QLegend::AlignmentBottom: {
95 rect.adjust(m_padding,
96 m_padding,
97 -m_padding,
98 -m_padding - legend->size().height());
99 break;
100 }
101 case QLegend::AlignmentLeft: {
102 rect.adjust(m_padding + legend->size().width(),
103 m_padding,
84 -m_padding,
104 -m_padding,
85 -m_padding);
105 -m_padding);
106 break;
107 }
108 case QLegend::AlignmentRight: {
109 rect.adjust(m_padding,
110 m_padding,
111 -m_padding - legend->size().width(),
112 -m_padding);
113 break;
114 }
115 default: {
116 rect.adjust(m_padding,
117 m_padding,
118 -m_padding,
119 -m_padding);
120 break;
121 }
122 }
123 } else {
124
125 // Legend is detached, or not visible
126 rect.adjust(m_padding,
127 m_padding,
128 -m_padding,
129 -m_padding);
130 }
86
131
87 //rewrite zoom stack
132 //rewrite zoom stack
88 /*
133 /*
@@ -371,20 +371,20 void QChartPrivate::updateLegendLayout()
371
371
372 switch (m_legend->alignment())
372 switch (m_legend->alignment())
373 {
373 {
374 case QLegend::LayoutTop: {
374 case QLegend::AlignmentTop: {
375 legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height());
375 legendRect = m_rect.adjusted(0,0,0,-padding - plotRect.height());
376 break;
376 break;
377 }
377 }
378 case QLegend::LayoutBottom: {
378 case QLegend::AlignmentBottom: {
379 legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0);
379 legendRect = m_rect.adjusted(padding,padding + plotRect.height(),-padding,0);
380 break;
380 break;
381 }
381 }
382 case QLegend::LayoutLeft: {
382 case QLegend::AlignmentLeft: {
383 legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding);
383 legendRect = m_rect.adjusted(0,padding,-padding - plotRect.width(),-padding);
384 break;
384 break;
385 }
385 }
386 case QLegend::LayoutRight: {
386 case QLegend::AlignmentRight: {
387 legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding);
387 legendRect = m_rect.adjusted(padding + plotRect.width(),padding,0,-padding);
388 break;
388 break;
389 }
389 }
390 default: {
390 default: {
@@ -405,6 +405,8 void QChartPrivate::updateLegendLayout()
405 if (height > 0) {
405 if (height > 0) {
406 pos.setY(pos.y() + height/2);
406 pos.setY(pos.y() + height/2);
407 }
407 }
408
409 qDebug() << "lenged topleft:" << pos;
408 m_legend->setPos(pos);
410 m_legend->setPos(pos);
409 }
411 }
410
412
@@ -428,7 +430,7 void QChartPrivate::updateLayout()
428
430
429 // recalculate legend position
431 // recalculate legend position
430 if (m_legend) {
432 if (m_legend) {
431 if (m_legend->parentObject() == q_ptr) {
433 if ((m_legend->attachedToChart()) && (m_legend->parentObject() == q_ptr)) {
432 updateLegendLayout();
434 updateLegendLayout();
433 }
435 }
434 }
436 }
@@ -93,8 +93,9 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
93 m_maximumSize(150,100),
93 m_maximumSize(150,100),
94 m_size(m_minimumSize),
94 m_size(m_minimumSize),
95 m_brush(Qt::darkGray), // TODO: default should come from theme
95 m_brush(Qt::darkGray), // TODO: default should come from theme
96 m_alignment(QLegend::LayoutTop),
96 m_alignment(QLegend::AlignmentRight),
97 mFirstMarker(0)
97 mFirstMarker(0),
98 m_attachedToChart(true)
98 {
99 {
99 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
100 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
100 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
101 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
@@ -168,7 +169,7 QPen QLegend::pen() const
168 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
169 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
169 \sa QLegend::Layout
170 \sa QLegend::Layout
170 */
171 */
171 void QLegend::setAlignmnent(QLegend::Layout alignment)
172 void QLegend::setAlignmnent(QLegend::Alignment alignment)
172 {
173 {
173 m_alignment = alignment;
174 m_alignment = alignment;
174 updateLayout();
175 updateLayout();
@@ -177,7 +178,7 void QLegend::setAlignmnent(QLegend::Layout alignment)
177 /*!
178 /*!
178 Returns the preferred layout for legend
179 Returns the preferred layout for legend
179 */
180 */
180 QLegend::Layout QLegend::alignment() const
181 QLegend::Alignment QLegend::alignment() const
181 {
182 {
182 return m_alignment;
183 return m_alignment;
183 }
184 }
@@ -389,6 +390,30 void QLegend::scrollButtonClicked(LegendScrollButton *scrollButton)
389 }
390 }
390
391
391 /*!
392 /*!
393 Detaches the legend from chart. Chart won't change layout of the legend.
394 */
395 void QLegend::detachFromChart()
396 {
397 m_attachedToChart = false;
398 }
399
400 /*!
401 Attaches the legend to chart. Chart may change layout of the legend.
402 */
403 void QLegend::attachToChart()
404 {
405 m_attachedToChart = true;
406 }
407
408 /*!
409 Returns true, if legend is attached to chart.
410 */
411 bool QLegend::attachedToChart()
412 {
413 return m_attachedToChart;
414 }
415
416 /*!
392 \internal Helper function. Appends markers from \a series to legend.
417 \internal Helper function. Appends markers from \a series to legend.
393 */
418 */
394 void QLegend::appendMarkers(QAreaSeries* series)
419 void QLegend::appendMarkers(QAreaSeries* series)
@@ -498,8 +523,8 void QLegend::updateLayout()
498 switch (m_alignment)
523 switch (m_alignment)
499 {
524 {
500 // Both cases organise items horizontally
525 // Both cases organise items horizontally
501 case QLegend::LayoutBottom:
526 case QLegend::AlignmentBottom:
502 case QLegend::LayoutTop: {
527 case QLegend::AlignmentTop: {
503
528
504 qreal xStep = markerMaxSize.width();
529 qreal xStep = markerMaxSize.width();
505 qreal x = m_pos.x() + m_margin;
530 qreal x = m_pos.x() + m_margin;
@@ -553,8 +578,8 void QLegend::updateLayout()
553 break;
578 break;
554 }
579 }
555 // Both cases organize items vertically
580 // Both cases organize items vertically
556 case QLegend::LayoutLeft:
581 case QLegend::AlignmentLeft:
557 case QLegend::LayoutRight: {
582 case QLegend::AlignmentRight: {
558 qreal yStep = markerMaxSize.height();
583 qreal yStep = markerMaxSize.height();
559 qreal x = m_pos.x() + m_margin;
584 qreal x = m_pos.x() + m_margin;
560 qreal y = m_pos.y() + m_margin;
585 qreal y = m_pos.y() + m_margin;
@@ -655,7 +680,7 QSizeF QLegend::maximumMarkerSize()
655 */
680 */
656 void QLegend::checkFirstMarkerBounds()
681 void QLegend::checkFirstMarkerBounds()
657 {
682 {
658 if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) {
683 if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) {
659 // Bounds limited by height.
684 // Bounds limited by height.
660 int max;
685 int max;
661 if (scrollButtonsVisible()) {
686 if (scrollButtonsVisible()) {
@@ -689,9 +714,9 void QLegend::checkFirstMarkerBounds()
689 bool QLegend::scrollButtonsVisible()
714 bool QLegend::scrollButtonsVisible()
690 {
715 {
691 // Just a helper to clarify, what the magic below means :)
716 // Just a helper to clarify, what the magic below means :)
692 if ((m_alignment == QLegend::LayoutTop) || (m_alignment == QLegend::LayoutBottom)) {
717 if ((m_alignment == QLegend::AlignmentTop) || (m_alignment == QLegend::AlignmentBottom)) {
693 return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width());
718 return (maximumMarkerSize().width() * m_markers.count() + m_margin * 2 > m_maximumSize.width());
694 } else if ((m_alignment == QLegend::LayoutLeft) || (m_alignment == QLegend::LayoutRight)) {
719 } else if ((m_alignment == QLegend::AlignmentLeft) || (m_alignment == QLegend::AlignmentRight)) {
695 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
720 return (maximumMarkerSize().height() * m_markers.count() + m_margin * 2 > m_maximumSize.height());
696 }
721 }
697
722
@@ -40,18 +40,17 class LegendScrollButton;
40 class QSeries;
40 class QSeries;
41 class QChart;
41 class QChart;
42
42
43 // TODO: This as widget
44 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
45 {
44 {
46 Q_OBJECT
45 Q_OBJECT
47 public:
46 public:
48
47
49 // We only support these alignments (for now)
48 // We only support these alignments (for now)
50 enum Layout {
49 enum Alignment {
51 LayoutTop = Qt::AlignTop,
50 AlignmentTop = Qt::AlignTop,
52 LayoutBottom = Qt::AlignBottom,
51 AlignmentBottom = Qt::AlignBottom,
53 LayoutLeft = Qt::AlignLeft,
52 AlignmentLeft = Qt::AlignLeft,
54 LayoutRight = Qt::AlignRight
53 AlignmentRight = Qt::AlignRight
55 };
54 };
56 private:
55 private:
57 explicit QLegend(QChart *chart);
56 explicit QLegend(QChart *chart);
@@ -66,8 +65,8 public:
66 void setPen(const QPen &pen);
65 void setPen(const QPen &pen);
67 QPen pen() const;
66 QPen pen() const;
68
67
69 void setAlignmnent(QLegend::Layout alignment);
68 void setAlignmnent(QLegend::Alignment alignment);
70 QLegend::Layout alignment() const;
69 QLegend::Alignment alignment() const;
71
70
72 QSizeF maximumSize() const;
71 QSizeF maximumSize() const;
73 void setMaximumSize(const QSizeF size);
72 void setMaximumSize(const QSizeF size);
@@ -78,6 +77,10 public:
78
77
79 void scrollButtonClicked(LegendScrollButton *scrollButton);
78 void scrollButtonClicked(LegendScrollButton *scrollButton);
80
79
80 void detachFromChart();
81 void attachToChart();
82 bool attachedToChart();
83
81 Q_SIGNALS:
84 Q_SIGNALS:
82 // for interactions.
85 // for interactions.
83 void clicked(QSeries *series, Qt::MouseButton button);
86 void clicked(QSeries *series, Qt::MouseButton button);
@@ -117,7 +120,7 private:
117
120
118 QBrush m_brush;
121 QBrush m_brush;
119 QPen m_pen;
122 QPen m_pen;
120 QLegend::Layout m_alignment;
123 QLegend::Alignment m_alignment;
121
124
122 int mFirstMarker;
125 int mFirstMarker;
123
126
@@ -126,6 +129,8 private:
126 LegendScrollButton *m_scrollButtonUp;
129 LegendScrollButton *m_scrollButtonUp;
127 LegendScrollButton *m_scrollButtonDown;
130 LegendScrollButton *m_scrollButtonDown;
128
131
132 bool m_attachedToChart;
133
129 friend class QChart;
134 friend class QChart;
130 // <--- PIMPL
135 // <--- PIMPL
131 };
136 };
General Comments 0
You need to be logged in to leave comments. Login now