legendlayout.cpp
504 lines
| 17.7 KiB
| text/x-c
|
CppLexer
|
r1535 | /**************************************************************************** | ||
** | ||||
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
|
r1535 | ** All rights reserved. | ||
|
r2740 | ** For any questions to Digia, please use contact form at http://qt.io | ||
|
r1535 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r1535 | ** | ||
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
** agreement between you and Digia. | ||||
|
r1535 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r1535 | ** | ||
****************************************************************************/ | ||||
|
r2714 | #include <private/legendlayout_p.h> | ||
#include <private/chartpresenter_p.h> | ||||
#include <private/qlegend_p.h> | ||||
#include <private/abstractchartlayout_p.h> | ||||
|
r1535 | |||
|
r2714 | #include <private/qlegendmarker_p.h> | ||
#include <private/legendmarkeritem_p.h> | ||||
#include <QtCharts/QLegendMarker> | ||||
|
r2168 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r1534 | |||
|
r2104 | LegendLayout::LegendLayout(QLegend *legend) | ||
|
r2196 | : m_legend(legend), | ||
m_offsetX(0), | ||||
m_offsetY(0) | ||||
|
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 | |||
|
r2196 | qreal oldOffsetX = m_offsetX; | ||
qreal oldOffsetY = m_offsetY; | ||||
|
r2097 | m_offsetX = 0; | ||
m_offsetY = 0; | ||||
|
r1534 | |||
|
r2097 | QSizeF size(0, 0); | ||
|
r1534 | |||
|
r2185 | if (m_legend->d_ptr->markers().isEmpty()) { | ||
|
r2097 | return; | ||
|
r2170 | } | ||
|
r2097 | |||
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 | |||
|
r2168 | switch(m_legend->alignment()) { | ||
|
r2097 | case Qt::AlignTop: | ||
case Qt::AlignBottom: { | ||||
|
r2617 | // Calculate the space required for items and add them to a sorted list. | ||
qreal markerItemsWidth = 0; | ||||
qreal itemMargins = 0; | ||||
QList<LegendWidthStruct *> legendWidthList; | ||||
foreach (QLegendMarker *marker, m_legend->d_ptr->markers()) { | ||||
LegendMarkerItem *item = marker->d_ptr->item(); | ||||
if (item->isVisible()) { | ||||
QSizeF dummySize; | ||||
qreal itemWidth = item->sizeHint(Qt::PreferredSize, dummySize).width(); | ||||
LegendWidthStruct *structItem = new LegendWidthStruct; | ||||
structItem->item = item; | ||||
structItem->width = itemWidth; | ||||
legendWidthList.append(structItem); | ||||
markerItemsWidth += itemWidth; | ||||
itemMargins += marker->d_ptr->item()->m_margin; | ||||
} | ||||
} | ||||
|
r2712 | std::sort(legendWidthList.begin(), legendWidthList.end(), widthLongerThan); | ||
|
r2617 | |||
// If the items would occupy more space than is available, start truncating them | ||||
// from the longest one. | ||||
qreal availableGeometry = geometry.width() - right - left * 2 - itemMargins; | ||||
|
r2673 | if (markerItemsWidth >= availableGeometry && legendWidthList.count() > 0) { | ||
|
r2617 | bool truncated(false); | ||
int count = legendWidthList.count(); | ||||
for (int i = 1; i < count; i++) { | ||||
int truncateIndex = i - 1; | ||||
while (legendWidthList.at(truncateIndex)->width >= legendWidthList.at(i)->width | ||||
&& !truncated) { | ||||
legendWidthList.at(truncateIndex)->width--; | ||||
markerItemsWidth--; | ||||
if (i > 1) { | ||||
// Truncate the items that are before the truncated one in the list. | ||||
for (int j = truncateIndex - 1; j >= 0; j--) { | ||||
if (legendWidthList.at(truncateIndex)->width | ||||
< legendWidthList.at(j)->width) { | ||||
legendWidthList.at(j)->width--; | ||||
markerItemsWidth--; | ||||
} | ||||
} | ||||
} | ||||
if (markerItemsWidth < availableGeometry) | ||||
truncated = true; | ||||
} | ||||
// Truncate the last item if needed. | ||||
if (i == count - 1) { | ||||
if (legendWidthList.at(count - 1)->width | ||||
> legendWidthList.at(truncateIndex)->width) { | ||||
legendWidthList.at(count - 1)->width--; | ||||
markerItemsWidth--; | ||||
} | ||||
} | ||||
if (truncated) | ||||
break; | ||||
} | ||||
|
r2665 | // Items are of same width and all of them need to be truncated | ||
// or there is just one item that is truncated. | ||||
|
r2617 | while (markerItemsWidth >= availableGeometry) { | ||
for (int i = 0; i < count; i++) { | ||||
legendWidthList.at(i)->width--; | ||||
markerItemsWidth--; | ||||
} | ||||
} | ||||
} | ||||
|
r2168 | QPointF point(0,0); | ||
|
r2676 | |||
int markerCount = m_legend->d_ptr->markers().count(); | ||||
for (int i = 0; i < markerCount; i++) { | ||||
QLegendMarker *marker; | ||||
if (m_legend->d_ptr->m_reverseMarkers) | ||||
marker = m_legend->d_ptr->markers().at(markerCount - 1 - i); | ||||
else | ||||
marker = m_legend->d_ptr->markers().at(i); | ||||
|
r2193 | LegendMarkerItem *item = marker->d_ptr->item(); | ||
|
r2170 | if (item->isVisible()) { | ||
|
r2617 | QRectF itemRect = geometry; | ||
qreal availableWidth = 0; | ||||
for (int i = 0; i < legendWidthList.size(); ++i) { | ||||
if (legendWidthList.at(i)->item == item) { | ||||
availableWidth = legendWidthList.at(i)->width; | ||||
break; | ||||
} | ||||
} | ||||
itemRect.setWidth(availableWidth); | ||||
item->setGeometry(itemRect); | ||||
|
r2168 | item->setPos(point.x(),geometry.height()/2 - item->boundingRect().height()/2); | ||
|
r2193 | const QRectF &rect = item->boundingRect(); | ||
|
r2168 | size = size.expandedTo(rect.size()); | ||
qreal w = rect.width(); | ||||
|
r2617 | m_width = m_width + w - item->m_margin; | ||
|
r2168 | point.setX(point.x() + w); | ||
} | ||||
|
r1534 | } | ||
|
r2617 | // Delete structs from the container | ||
qDeleteAll(legendWidthList); | ||||
|
r2168 | 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(); | ||||
|
r1534 | } | ||
|
r2168 | break; | ||
|
r2097 | case Qt::AlignLeft: | ||
case Qt::AlignRight: { | ||||
|
r2168 | QPointF point(0,0); | ||
|
r2676 | int markerCount = m_legend->d_ptr->markers().count(); | ||
for (int i = 0; i < markerCount; i++) { | ||||
QLegendMarker *marker; | ||||
if (m_legend->d_ptr->m_reverseMarkers) | ||||
marker = m_legend->d_ptr->markers().at(markerCount - 1 - i); | ||||
else | ||||
marker = m_legend->d_ptr->markers().at(i); | ||||
|
r2193 | LegendMarkerItem *item = marker->d_ptr->item(); | ||
|
r2170 | if (item->isVisible()) { | ||
|
r2168 | item->setGeometry(geometry); | ||
item->setPos(point); | ||||
|
r2193 | const QRectF &rect = item->boundingRect(); | ||
|
r2168 | qreal h = rect.height(); | ||
size = size.expandedTo(rect.size()); | ||||
m_height+=h; | ||||
point.setY(point.y() + h); | ||||
} | ||||
} | ||||
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(); | ||||
break; | ||||
|
r1534 | } | ||
} | ||||
|
r2097 | |||
|
r1965 | m_minOffsetX = -left; | ||
m_minOffsetY = - top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
|
r2196 | |||
setOffset(oldOffsetX, oldOffsetY); | ||||
|
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. | ||||
|
r2196 | qreal oldOffsetX = m_offsetX; | ||
qreal oldOffsetY = m_offsetY; | ||||
|
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 | |||
|
r2185 | QList<QLegendMarker *> 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++) { | ||||
|
r2170 | LegendMarkerItem *item = markers.at(i)->d_ptr->item(); | ||
if (item->isVisible()) { | ||||
item->setGeometry(geometry); | ||||
item->setPos(point.x(),point.y()); | ||||
|
r2193 | const QRectF &boundingRect = item->boundingRect(); | ||
|
r2170 | 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()) { | ||||
|
r2131 | 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++) { | ||||
|
r2170 | LegendMarkerItem *item = markers.at(i)->d_ptr->item(); | ||
if (item->isVisible()) { | ||||
item->setGeometry(geometry); | ||||
|
r2193 | const QRectF &boundingRect = item->boundingRect(); | ||
|
r2170 | qreal w = boundingRect.width(); | ||
qreal h = boundingRect.height(); | ||||
m_width = qMax(m_width,w); | ||||
m_height = qMax(m_height,h); | ||||
item->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()) { | ||||
|
r2131 | m_height += h; | ||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2171 | m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||
|
r2131 | |||
|
r2171 | m_minOffsetX = -left; | ||
m_minOffsetY = -m_height + geometry.height() - top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = -bottom; | ||||
|
r2131 | } | ||
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++) { | ||||
|
r2170 | LegendMarkerItem *item = markers.at(i)->d_ptr->item(); | ||
if (item->isVisible()) { | ||||
item->setGeometry(geometry); | ||||
|
r2193 | const QRectF &boundingRect = item->boundingRect(); | ||
|
r2131 | qreal w = boundingRect.width(); | ||
qreal h = boundingRect.height(); | ||||
|
r2170 | m_height = qMax(m_height,h); | ||
maxWidth = qMax(maxWidth,w); | ||||
item->setPos(point.x(),point.y()); | ||||
|
r2131 | 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); | ||||
|
r2170 | if (i+1 < markers.count()) { | ||
|
r2131 | m_width += maxWidth; | ||
maxWidth = 0; | ||||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2171 | m_width += maxWidth; | ||
m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||||
|
r2131 | |||
|
r2171 | m_minOffsetX = -left; | ||
m_minOffsetY = -top; | ||||
m_maxOffsetX = m_width - geometry.width() - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
|
r2131 | } | ||
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++) { | ||||
|
r2170 | LegendMarkerItem *item = markers.at(i)->d_ptr->item(); | ||
if (item->isVisible()) { | ||||
item->setGeometry(geometry); | ||||
|
r2193 | const QRectF &boundingRect = item->boundingRect(); | ||
|
r2170 | qreal w = boundingRect.width(); | ||
qreal h = boundingRect.height(); | ||||
m_height = qMax(m_height,h); | ||||
maxWidth = qMax(maxWidth,w); | ||||
item->setPos(point.x() - w,point.y()); | ||||
point.setY(point.y() + h); | ||||
if (point.y() + h > geometry.bottom()-bottom) { | ||||
|
r2131 | // Next item would go off rect. | ||
point.setX(point.x() - maxWidth); | ||||
point.setY(0); | ||||
|
r2170 | if (i+1 < markers.count()) { | ||
|
r2131 | m_width += maxWidth; | ||
maxWidth = 0; | ||||
|
r1534 | } | ||
} | ||||
} | ||||
} | ||||
|
r2171 | m_width += maxWidth; | ||
m_legend->d_ptr->items()->setPos(geometry.topLeft()); | ||||
|
r2131 | |||
|
r2171 | m_minOffsetX = - m_width + geometry.width() - left; | ||
m_minOffsetY = -top; | ||||
m_maxOffsetX = - right; | ||||
m_maxOffsetY = m_height - geometry.height() - bottom; | ||||
|
r2131 | } | ||
break; | ||||
|
r2097 | default: | ||
|
r1534 | break; | ||
} | ||||
|
r2196 | setOffset(oldOffsetX, oldOffsetY); | ||
|
r1534 | } | ||
|
r2104 | QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1534 | { | ||
|
r2168 | QSizeF size(0, 0); | ||
qreal left, top, right, bottom; | ||||
getContentsMargins(&left, &top, &right, &bottom); | ||||
if(constraint.isValid()) { | ||||
|
r2193 | foreach(QLegendMarker *marker, m_legend->d_ptr->markers()) { | ||
|
r2170 | LegendMarkerItem *item = marker->d_ptr->item(); | ||
|
r2168 | size = size.expandedTo(item->effectiveSizeHint(which)); | ||
} | ||||
size = size.boundedTo(constraint); | ||||
} | ||||
else if (constraint.width() >= 0) { | ||||
qreal width = 0; | ||||
qreal height = 0; | ||||
|
r2193 | foreach(QLegendMarker *marker, m_legend->d_ptr->markers()) { | ||
|
r2170 | LegendMarkerItem *item = marker->d_ptr->item(); | ||
|
r2168 | width+=item->effectiveSizeHint(which).width(); | ||
height=qMax(height,item->effectiveSizeHint(which).height()); | ||||
} | ||||
size = QSizeF(qMin(constraint.width(),width), height); | ||||
} | ||||
else if (constraint.height() >= 0) { | ||||
qreal width = 0; | ||||
qreal height = 0; | ||||
|
r2193 | foreach(QLegendMarker *marker, m_legend->d_ptr->markers()) { | ||
|
r2170 | LegendMarkerItem *item = marker->d_ptr->item(); | ||
|
r2168 | width=qMax(width,item->effectiveSizeHint(which).width()); | ||
height+=height,item->effectiveSizeHint(which).height(); | ||||
} | ||||
size = QSizeF(width,qMin(constraint.height(),height)); | ||||
} | ||||
else { | ||||
|
r2193 | foreach(QLegendMarker *marker, m_legend->d_ptr->markers()) { | ||
|
r2170 | LegendMarkerItem *item = marker->d_ptr->item(); | ||
|
r2168 | size = size.expandedTo(item->effectiveSizeHint(which)); | ||
} | ||||
} | ||||
size += QSize(left + right, top + bottom); | ||||
return size; | ||||
|
r1534 | } | ||
|
r2617 | bool LegendLayout::widthLongerThan(const LegendWidthStruct *item1, | ||
const LegendWidthStruct *item2) | ||||
{ | ||||
return item1->width > item2->width; | ||||
} | ||||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||