legendlayout.cpp
384 lines
| 12.3 KiB
| text/x-c
|
CppLexer
|
r1535 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r1534 | #include "legendlayout_p.h" | ||
#include "chartpresenter_p.h" | ||||
#include "legendmarker_p.h" | ||||
#include "qlegend_p.h" | ||||
|
r2105 | #include "chartlayout_p.h" | ||
|
r1535 | |||
|
r1534 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r2104 | LegendLayout::LegendLayout(QLegend *legend) | ||
|
r2097 | : m_legend(legend) | ||
|
r1534 | { | ||
} | ||||
LegendLayout::~LegendLayout() | ||||
{ | ||||
} | ||||
void LegendLayout::setOffset(qreal x, qreal y) | ||||
{ | ||||
bool scrollHorizontal = true; | ||||
|
r2097 | switch (m_legend->alignment()) { | ||
case Qt::AlignTop: | ||||
case Qt::AlignBottom: | ||||
scrollHorizontal = true; | ||||
break; | ||||
case Qt::AlignLeft: | ||||
case Qt::AlignRight: | ||||
scrollHorizontal = false; | ||||
break; | ||||
|
r1534 | } | ||
// If detached, the scrolling direction is vertical instead of horizontal and vice versa. | ||||
|
r2097 | if (!m_legend->isAttachedToChart()) | ||
|
r1534 | scrollHorizontal = !scrollHorizontal; | ||
QRectF boundingRect = geometry(); | ||||
|
r1965 | qreal left, top, right, bottom; | ||
getContentsMargins(&left, &top, &right, &bottom); | ||||
|
r2097 | boundingRect.adjust(left, top, -right, -bottom); | ||
|
r1534 | |||
// Limit offset between m_minOffset and m_maxOffset | ||||
if (scrollHorizontal) { | ||||
|
r2097 | if (m_width <= boundingRect.width()) | ||
return; | ||||
|
r1534 | |||
if (x != m_offsetX) { | ||||
m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX); | ||||
|
r2097 | m_legend->d_ptr->items()->setPos(-m_offsetX, boundingRect.top()); | ||
|
r1534 | } | ||
|
r2097 | } else { | ||
if (m_height <= boundingRect.height()) | ||||
return; | ||||
|
r1534 | |||
if (y != m_offsetY) { | ||||
m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY); | ||||
|
r2097 | m_legend->d_ptr->items()->setPos(boundingRect.left(), -m_offsetY); | ||
|
r1534 | } | ||
} | ||||
} | ||||
QPointF LegendLayout::offset() const | ||||
{ | ||||
|
r2097 | return QPointF(m_offsetX, m_offsetY); | ||
|
r1534 | } | ||
|
r2088 | void LegendLayout::invalidate() | ||
{ | ||||
QGraphicsLayout::invalidate(); | ||||
|
r2097 | if (m_legend->isAttachedToChart()) | ||
|
r2088 | m_legend->d_ptr->m_presenter->layout()->invalidate(); | ||
} | ||||
|
r2104 | void LegendLayout::setGeometry(const QRectF &rect) | ||
|
r1534 | { | ||
|
r1837 | m_legend->d_ptr->items()->setVisible(m_legend->isVisible()); | ||
|
r1534 | |||
|
r2082 | QGraphicsLayout::setGeometry(rect); | ||
|
r1837 | |||
|
r2097 | if (m_legend->isAttachedToChart()) | ||
|
r1534 | setAttachedGeometry(rect); | ||
|
r2097 | else | ||
|
r1534 | setDettachedGeometry(rect); | ||
} | ||||
|
r2104 | void LegendLayout::setAttachedGeometry(const QRectF &rect) | ||
|
r1534 | { | ||
|
r2097 | if (!rect.isValid()) | ||
return; | ||||
|
r1534 | |||
|
r2097 | m_offsetX = 0; | ||
m_offsetY = 0; | ||||
|
r1534 | |||
|
r2097 | QSizeF size(0, 0); | ||
|
r1534 | |||
|
r2097 | if (m_legend->d_ptr->markers().isEmpty()) | ||
return; | ||||
m_width = 0; | ||||
m_height = 0; | ||||
|
r1534 | |||
|
r1965 | qreal left, top, right, bottom; | ||
getContentsMargins(&left, &top, &right, &bottom); | ||||
|
r2097 | QRectF geometry = rect.adjusted(left, top, -right, -bottom); | ||
|
r1534 | |||
|
r2097 | switch (m_legend->alignment()) { | ||
case Qt::AlignTop: | ||||
case Qt::AlignBottom: { | ||||
|
r2131 | QPointF point(0, 0); | ||
foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { | ||||
if (marker->isVisible()) { | ||||
marker->setGeometry(geometry); | ||||
marker->setPos(point.x(), geometry.height() / 2 - marker->boundingRect().height() / 2); | ||||
const QRectF &rect = marker->boundingRect(); | ||||
size = size.expandedTo(rect.size()); | ||||
qreal w = rect.width(); | ||||
m_width += w; | ||||
point.setX(point.x() + w); | ||||
|
r1534 | } | ||
} | ||||
|
r2131 | if (m_width < geometry.width()) | ||
m_legend->d_ptr->items()->setPos(geometry.width() / 2 - m_width / 2, geometry.top()); | ||||
else | ||||
m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||||
m_height = size.height(); | ||||
} | ||||
break; | ||||
|
r2097 | case Qt::AlignLeft: | ||
case Qt::AlignRight: { | ||||
QPointF point(0, 0); | ||||
foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { | ||||
if (marker->isVisible()) { | ||||
marker->setGeometry(geometry); | ||||
marker->setPos(point); | ||||
|
r2104 | const QRectF &rect = marker->boundingRect(); | ||
|
r2097 | qreal h = rect.height(); | ||
size = size.expandedTo(rect.size()); | ||||
m_height += h; | ||||
point.setY(point.y() + h); | ||||
|
r1534 | } | ||
} | ||||
|
r2097 | |||
if (m_height < geometry.height()) | ||||
m_legend->d_ptr->items()->setPos(geometry.left(), geometry.height() / 2 - m_height / 2); | ||||
else | ||||
m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||||
m_width = size.width(); | ||||
|
r1534 | break; | ||
|
r2131 | } | ||
|
r1534 | } | ||
|
r1965 | m_minOffsetX = -left; | ||
m_minOffsetY = - top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
|
r1534 | } | ||
|
r2104 | void LegendLayout::setDettachedGeometry(const QRectF &rect) | ||
|
r1534 | { | ||
|
r2097 | if (!rect.isValid()) | ||
return; | ||||
|
r1538 | |||
|
r1534 | // Detached layout is different. | ||
// In detached mode legend may have multiple rows and columns, so layout calculations | ||||
// differ a log from attached mode. | ||||
// Also the scrolling logic is bit different. | ||||
|
r2097 | m_offsetX = 0; | ||
m_offsetY = 0; | ||||
|
r1534 | |||
|
r1965 | qreal left, top, right, bottom; | ||
getContentsMargins(&left, &top, &right, &bottom); | ||||
|
r2097 | QRectF geometry = rect.adjusted(left, top, -right, -bottom); | ||
|
r1965 | |||
|
r2097 | QSizeF size(0, 0); | ||
|
r1534 | |||
|
r1536 | QList<LegendMarker *> markers = m_legend->d_ptr->markers(); | ||
|
r1534 | |||
|
r2097 | if (markers.isEmpty()) | ||
return; | ||||
|
r1534 | |||
switch (m_legend->alignment()) { | ||||
|
r2097 | case Qt::AlignTop: { | ||
|
r2131 | QPointF point(0, 0); | ||
m_width = 0; | ||||
m_height = 0; | ||||
for (int i = 0; i < markers.count(); i++) { | ||||
LegendMarker *marker = markers.at(i); | ||||
if (marker->isVisible()) { | ||||
marker->setGeometry(geometry); | ||||
marker->setPos(point.x(), point.y()); | ||||
const QRectF &boundingRect = marker->boundingRect(); | ||||
qreal w = boundingRect.width(); | ||||
qreal h = boundingRect.height(); | ||||
m_width = qMax(m_width, w); | ||||
m_height = qMax(m_height, h); | ||||
point.setX(point.x() + w); | ||||
if (point.x() + w > geometry.left() + geometry.width() - right) { | ||||
// Next item would go off rect. | ||||
point.setX(0); | ||||
point.setY(point.y() + h); | ||||
if (i + 1 < markers.count()) { | ||||
m_height += h; | ||||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2131 | m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||
m_minOffsetX = -left; | ||||
m_minOffsetY = -top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
} | ||||
break; | ||||
|
r2097 | case Qt::AlignBottom: { | ||
|
r2131 | QPointF point(0, geometry.height()); | ||
m_width = 0; | ||||
m_height = 0; | ||||
for (int i = 0; i < markers.count(); i++) { | ||||
LegendMarker *marker = markers.at(i); | ||||
if (marker->isVisible()) { | ||||
marker->setGeometry(geometry); | ||||
const QRectF &boundingRect = marker->boundingRect(); | ||||
qreal w = boundingRect.width(); | ||||
qreal h = boundingRect.height(); | ||||
m_width = qMax(m_width, w); | ||||
m_height = qMax(m_height, h); | ||||
marker->setPos(point.x(), point.y() - h); | ||||
point.setX(point.x() + w); | ||||
if (point.x() + w > geometry.left() + geometry.width() - right) { | ||||
// Next item would go off rect. | ||||
point.setX(0); | ||||
point.setY(point.y() - h); | ||||
if (i + 1 < markers.count()) { | ||||
m_height += h; | ||||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2131 | m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||
m_minOffsetX = -left; | ||||
m_minOffsetY = -m_height + geometry.height() - top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = -bottom; | ||||
} | ||||
break; | ||||
|
r2097 | case Qt::AlignLeft: { | ||
|
r2131 | QPointF point(0, 0); | ||
m_width = 0; | ||||
m_height = 0; | ||||
qreal maxWidth = 0; | ||||
for (int i = 0; i < markers.count(); i++) { | ||||
LegendMarker *marker = markers.at(i); | ||||
if (marker->isVisible()) { | ||||
marker->setGeometry(geometry); | ||||
const QRectF &boundingRect = marker->boundingRect(); | ||||
qreal w = boundingRect.width(); | ||||
qreal h = boundingRect.height(); | ||||
m_height = qMax(m_height, h); | ||||
maxWidth = qMax(maxWidth, w); | ||||
marker->setPos(point.x(), point.y()); | ||||
point.setY(point.y() + h); | ||||
if (point.y() + h > geometry.bottom() - bottom) { | ||||
// Next item would go off rect. | ||||
point.setX(point.x() + maxWidth); | ||||
point.setY(0); | ||||
if (i + 1 < markers.count()) { | ||||
m_width += maxWidth; | ||||
maxWidth = 0; | ||||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2131 | m_width += maxWidth; | ||
m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||||
m_minOffsetX = -left; | ||||
m_minOffsetY = -top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
} | ||||
break; | ||||
|
r2097 | case Qt::AlignRight: { | ||
|
r2131 | QPointF point(geometry.width(), 0); | ||
m_width = 0; | ||||
m_height = 0; | ||||
qreal maxWidth = 0; | ||||
for (int i = 0; i < markers.count(); i++) { | ||||
LegendMarker *marker = markers.at(i); | ||||
if (marker->isVisible()) { | ||||
marker->setGeometry(geometry); | ||||
const QRectF &boundingRect = marker->boundingRect(); | ||||
qreal w = boundingRect.width(); | ||||
qreal h = boundingRect.height(); | ||||
m_height = qMax(m_height, h); | ||||
maxWidth = qMax(maxWidth, w); | ||||
marker->setPos(point.x() - w, point.y()); | ||||
point.setY(point.y() + h); | ||||
if (point.y() + h > geometry.bottom() - bottom) { | ||||
// Next item would go off rect. | ||||
point.setX(point.x() - maxWidth); | ||||
point.setY(0); | ||||
if (i + 1 < markers.count()) { | ||||
m_width += maxWidth; | ||||
maxWidth = 0; | ||||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2131 | m_width += maxWidth; | ||
m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||||
m_minOffsetX = - m_width + geometry.width() - left; | ||||
m_minOffsetY = -top; | ||||
m_maxOffsetX = - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
} | ||||
break; | ||||
|
r2097 | default: | ||
|
r1534 | break; | ||
} | ||||
} | ||||
|
r2104 | QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1534 | { | ||
QSizeF size(0, 0); | ||||
qreal left, top, right, bottom; | ||||
getContentsMargins(&left, &top, &right, &bottom); | ||||
|
r2097 | if (constraint.isValid()) { | ||
foreach (LegendMarker *marker, m_legend->d_ptr->markers()) | ||||
|
r1534 | size = size.expandedTo(marker->effectiveSizeHint(which)); | ||
size = size.boundedTo(constraint); | ||||
|
r2097 | } else if (constraint.width() >= 0) { | ||
|
r1534 | qreal width = 0; | ||
qreal height = 0; | ||||
|
r2097 | foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { | ||
width += marker->effectiveSizeHint(which).width(); | ||||
height = qMax(height, marker->effectiveSizeHint(which).height()); | ||||
|
r1534 | } | ||
|
r2097 | size = QSizeF(qMin(constraint.width(), width), height); | ||
} else if (constraint.height() >= 0) { | ||||
|
r1534 | qreal width = 0; | ||
qreal height = 0; | ||||
|
r2097 | foreach (LegendMarker *marker, m_legend->d_ptr->markers()) { | ||
width = qMax(width, marker->effectiveSizeHint(which).width()); | ||||
height += height, marker->effectiveSizeHint(which).height(); | ||||
|
r1534 | } | ||
|
r2097 | size = QSizeF(width, qMin(constraint.height(), height)); | ||
} else { | ||||
foreach (LegendMarker *marker, m_legend->d_ptr->markers()) | ||||
|
r1534 | size = size.expandedTo(marker->effectiveSizeHint(which)); | ||
} | ||||
size += QSize(left + right, top + bottom); | ||||
return size; | ||||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||