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