Implement QQuickIconLabel::TextUnderIcon
This will be the default for TabButton and RoundButton. Change-Id: I9ccb0d35f33a80fe7e3da26617ed6f42232afe17 Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
This commit is contained in:
parent
a6be134c9d
commit
4503d45b93
|
@ -68,9 +68,11 @@ void QQuickIconLabelPrivate::updateImplicitSize()
|
|||
const qreal textImplicitWidth = showText ? label->implicitWidth() : 0;
|
||||
const qreal textImplicitHeight = showText ? label->implicitHeight() : 0;
|
||||
const qreal effectiveSpacing = showText && showIcon && icon->implicitWidth() > 0 ? spacing : 0;
|
||||
const qreal implicitWidth = iconImplicitWidth + textImplicitWidth + effectiveSpacing + horizontalPadding;
|
||||
const qreal implicitHeight = qMax(iconImplicitHeight, textImplicitHeight) + verticalPadding;
|
||||
q->setImplicitSize(implicitWidth, implicitHeight);
|
||||
const qreal implicitWidth = display == QQuickIconLabel::TextBesideIcon ? iconImplicitWidth + textImplicitWidth + effectiveSpacing
|
||||
: qMax(iconImplicitWidth, textImplicitWidth);
|
||||
const qreal implicitHeight = display == QQuickIconLabel::TextUnderIcon ? iconImplicitHeight + textImplicitHeight + effectiveSpacing
|
||||
: qMax(iconImplicitHeight, textImplicitHeight);
|
||||
q->setImplicitSize(implicitWidth + horizontalPadding, implicitHeight + verticalPadding);
|
||||
}
|
||||
|
||||
// adapted from QStyle::alignedRect()
|
||||
|
@ -127,6 +129,42 @@ void QQuickIconLabelPrivate::layout()
|
|||
if (icon)
|
||||
icon->setVisible(false);
|
||||
break;
|
||||
|
||||
case QQuickIconLabel::TextUnderIcon: {
|
||||
// Work out the sizes first, as the positions depend on them.
|
||||
QSizeF iconSize;
|
||||
QSizeF textSize;
|
||||
if (icon) {
|
||||
iconSize.setWidth(qMin(icon->implicitWidth(), availableWidth));
|
||||
iconSize.setHeight(qMin(icon->implicitHeight(), availableHeight));
|
||||
}
|
||||
qreal effectiveSpacing = 0;
|
||||
if (label) {
|
||||
if (!iconSize.isEmpty())
|
||||
effectiveSpacing = spacing;
|
||||
textSize.setWidth(qMin(label->implicitWidth(), availableWidth));
|
||||
textSize.setHeight(qMin(label->implicitHeight(), availableHeight - iconSize.height() - effectiveSpacing));
|
||||
}
|
||||
|
||||
QRectF combinedRect = alignedRect(mirrored, alignment,
|
||||
QSizeF(qMax(iconSize.width(), textSize.width()),
|
||||
iconSize.height() + effectiveSpacing + textSize.height()),
|
||||
QRectF(leftPadding, topPadding, availableWidth, availableHeight));
|
||||
if (icon) {
|
||||
QRectF iconRect = alignedRect(mirrored, Qt::AlignHCenter | Qt::AlignTop, iconSize, combinedRect);
|
||||
icon->setSize(iconRect.size());
|
||||
icon->setPosition(iconRect.topLeft());
|
||||
icon->setVisible(true);
|
||||
}
|
||||
if (label) {
|
||||
QRectF textRect = alignedRect(mirrored, Qt::AlignHCenter | Qt::AlignBottom, textSize, combinedRect);
|
||||
label->setSize(textRect.size());
|
||||
label->setPosition(textRect.topLeft());
|
||||
label->setVisible(true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case QQuickIconLabel::TextBesideIcon:
|
||||
default:
|
||||
// Work out the sizes first, as the positions depend on them.
|
||||
|
|
|
@ -74,7 +74,7 @@ public:
|
|||
IconOnly,
|
||||
TextOnly,
|
||||
TextBesideIcon,
|
||||
TextUnderIcon // unused, but reserved for future use
|
||||
TextUnderIcon
|
||||
};
|
||||
Q_ENUM(Display)
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@ public:
|
|||
IconOnly,
|
||||
TextOnly,
|
||||
TextBesideIcon,
|
||||
TextUnderIcon // unused, but reserved for future use
|
||||
TextUnderIcon
|
||||
};
|
||||
Q_ENUM(Display)
|
||||
|
||||
|
|
|
@ -453,9 +453,11 @@ TestCase {
|
|||
return [
|
||||
{ "tag": "IconOnly", display: Button.IconOnly },
|
||||
{ "tag": "TextOnly", display: Button.TextOnly },
|
||||
{ "tag": "TextUnderIcon", display: Button.TextUnderIcon },
|
||||
{ "tag": "TextBesideIcon", display: Button.TextBesideIcon },
|
||||
{ "tag": "IconOnly, mirrored", display: Button.IconOnly, mirrored: true },
|
||||
{ "tag": "TextOnly, mirrored", display: Button.TextOnly, mirrored: true },
|
||||
{ "tag": "TextUnderIcon, mirrored", display: Button.TextUnderIcon, mirrored: true },
|
||||
{ "tag": "TextBesideIcon, mirrored", display: Button.TextBesideIcon, mirrored: true }
|
||||
]
|
||||
}
|
||||
|
@ -482,11 +484,20 @@ TestCase {
|
|||
compare(iconImage.visible, true)
|
||||
compare(label.visible, false)
|
||||
compare(iconImage.x, (control.availableWidth - iconImage.width) / 2)
|
||||
compare(iconImage.y, (control.availableHeight - iconImage.height) / 2)
|
||||
break;
|
||||
case Button.TextOnly:
|
||||
compare(iconImage.visible, false)
|
||||
compare(label.visible, true)
|
||||
compare(label.x, (control.availableWidth - label.width) / 2)
|
||||
compare(label.y, (control.availableHeight - label.height) / 2)
|
||||
break;
|
||||
case Button.TextUnderIcon:
|
||||
compare(iconImage.visible, true)
|
||||
compare(label.visible, true)
|
||||
compare(iconImage.x, (control.availableWidth - iconImage.width) / 2)
|
||||
compare(label.x, (control.availableWidth - label.width) / 2)
|
||||
verify(iconImage.y < label.y)
|
||||
break;
|
||||
case Button.TextBesideIcon:
|
||||
compare(iconImage.visible, true)
|
||||
|
@ -495,6 +506,8 @@ TestCase {
|
|||
verify(label.x < iconImage.x)
|
||||
else
|
||||
verify(iconImage.x < label.x)
|
||||
compare(iconImage.y, (control.availableHeight - iconImage.height) / 2)
|
||||
compare(label.y, (control.availableHeight - label.height) / 2)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,9 +186,11 @@ TestCase {
|
|||
return [
|
||||
{ "tag": "IconOnly", display: ToolButton.IconOnly },
|
||||
{ "tag": "TextOnly", display: ToolButton.TextOnly },
|
||||
{ "tag": "TextUnderIcon", display: ToolButton.TextUnderIcon },
|
||||
{ "tag": "TextBesideIcon", display: ToolButton.TextBesideIcon },
|
||||
{ "tag": "IconOnly, mirrored", display: ToolButton.IconOnly, mirrored: true },
|
||||
{ "tag": "TextOnly, mirrored", display: ToolButton.TextOnly, mirrored: true },
|
||||
{ "tag": "TextUnderIcon, mirrored", display: ToolButton.TextUnderIcon, mirrored: true },
|
||||
{ "tag": "TextBesideIcon, mirrored", display: ToolButton.TextBesideIcon, mirrored: true }
|
||||
]
|
||||
}
|
||||
|
@ -215,11 +217,20 @@ TestCase {
|
|||
compare(iconImage.visible, true)
|
||||
compare(label.visible, false)
|
||||
compare(iconImage.x, (control.availableWidth - iconImage.width) / 2)
|
||||
compare(iconImage.y, (control.availableHeight - iconImage.height) / 2)
|
||||
break;
|
||||
case ToolButton.TextOnly:
|
||||
compare(iconImage.visible, false)
|
||||
compare(label.visible, true)
|
||||
compare(label.x, (control.availableWidth - label.width) / 2)
|
||||
compare(label.y, (control.availableHeight - label.height) / 2)
|
||||
break;
|
||||
case ToolButton.TextUnderIcon:
|
||||
compare(iconImage.visible, true)
|
||||
compare(label.visible, true)
|
||||
compare(iconImage.x, (control.availableWidth - iconImage.width) / 2)
|
||||
compare(label.x, (control.availableWidth - label.width) / 2)
|
||||
verify(iconImage.y < label.y)
|
||||
break;
|
||||
case ToolButton.TextBesideIcon:
|
||||
compare(iconImage.visible, true)
|
||||
|
@ -228,6 +239,8 @@ TestCase {
|
|||
verify(label.x < iconImage.x)
|
||||
else
|
||||
verify(iconImage.x < label.x)
|
||||
compare(iconImage.y, (control.availableHeight - iconImage.height) / 2)
|
||||
compare(label.y, (control.availableHeight - label.height) / 2)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,13 +69,24 @@ void tst_qquickiconlabel::display_data()
|
|||
typedef QVector<QQuickIconLabel::Display> DisplayVector;
|
||||
QQuickIconLabel::Display IconOnly = QQuickIconLabel::IconOnly;
|
||||
QQuickIconLabel::Display TextOnly = QQuickIconLabel::TextOnly;
|
||||
QQuickIconLabel::Display TextUnderIcon = QQuickIconLabel::TextUnderIcon;
|
||||
QQuickIconLabel::Display TextBesideIcon = QQuickIconLabel::TextBesideIcon;
|
||||
|
||||
QTest::addRow("IconOnly") << (DisplayVector() << IconOnly) << false << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextOnly") << (DisplayVector() << TextOnly) << false << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextUnderIcon") << (DisplayVector() << TextUnderIcon) << false << -1.0 << -1.0 << 10.0;
|
||||
QTest::addRow("TextBesideIcon") << (DisplayVector() << TextBesideIcon) << false << -1.0 << -1.0 << 10.0;
|
||||
QTest::addRow("IconOnly, spacing=10") << (DisplayVector() << IconOnly) << false << -1.0 << -1.0 << 10.0;
|
||||
QTest::addRow("TextOnly, spacing=10") << (DisplayVector() << TextOnly) << false << -1.0 << -1.0 << 10.0;
|
||||
QTest::addRow("TextUnderIcon, spacing=10") << (DisplayVector() << TextUnderIcon) << false << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextUnderIcon => IconOnly => TextUnderIcon")
|
||||
<< (DisplayVector() << TextUnderIcon << IconOnly << TextUnderIcon) << false << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextUnderIcon => IconOnly => TextUnderIcon, labelWidth=400")
|
||||
<< (DisplayVector() << TextUnderIcon << IconOnly << TextUnderIcon) << false << 400.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextUnderIcon => TextOnly => TextUnderIcon")
|
||||
<< (DisplayVector() << TextUnderIcon << TextOnly << TextUnderIcon) << false << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextUnderIcon => TextOnly => TextUnderIcon, labelWidth=400")
|
||||
<< (DisplayVector() << TextUnderIcon << TextOnly << TextUnderIcon) << false << 400.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextBesideIcon, spacing=10") << (DisplayVector() << TextBesideIcon) << false << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextBesideIcon => IconOnly => TextBesideIcon")
|
||||
<< (DisplayVector() << TextBesideIcon << IconOnly << TextBesideIcon) << false << -1.0 << -1.0 << 0.0;
|
||||
|
@ -87,6 +98,7 @@ void tst_qquickiconlabel::display_data()
|
|||
<< (DisplayVector() << TextBesideIcon << TextOnly << TextBesideIcon) << false << 400.0 << -1.0 << 0.0;
|
||||
QTest::addRow("IconOnly, mirrored") << (DisplayVector() << IconOnly) << true << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextOnly, mirrored") << (DisplayVector() << TextOnly) << true << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextUnderIcon, mirrored") << (DisplayVector() << TextUnderIcon) << true << -1.0 << -1.0 << 0.0;
|
||||
QTest::addRow("TextBesideIcon, mirrored") << (DisplayVector() << TextBesideIcon) << true << -1.0 << -1.0 << 0.0;
|
||||
}
|
||||
|
||||
|
@ -168,6 +180,23 @@ void tst_qquickiconlabel::display()
|
|||
QCOMPARE(label->implicitWidth(), text->implicitWidth() + horizontalPadding);
|
||||
QCOMPARE(label->implicitHeight(), text->implicitHeight() + verticalPadding);
|
||||
break;
|
||||
case QQuickIconLabel::TextUnderIcon: {
|
||||
const qreal combinedHeight = icon->height() + label->spacing() + text->height();
|
||||
const qreal contentY = verticalCenter - combinedHeight / 2;
|
||||
QCOMPARE(icon->x(), horizontalCenter - icon->width() / 2);
|
||||
QCOMPARE(icon->y(), contentY);
|
||||
QCOMPARE(icon->width(), icon->implicitWidth());
|
||||
QCOMPARE(icon->height(), icon->implicitHeight());
|
||||
QCOMPARE(icon->isVisible(), true);
|
||||
QCOMPARE(text->x(), horizontalCenter - text->width() / 2);
|
||||
QCOMPARE(text->y(), contentY + icon->height() + label->spacing());
|
||||
QCOMPARE(text->width(), text->implicitWidth());
|
||||
QCOMPARE(text->height(), text->implicitHeight());
|
||||
QCOMPARE(text->isVisible(), true);
|
||||
QCOMPARE(label->implicitWidth(), qMax(icon->implicitWidth(), text->implicitWidth()) + horizontalPadding);
|
||||
QCOMPARE(label->implicitHeight(), combinedHeight + verticalPadding);
|
||||
break;
|
||||
}
|
||||
case QQuickIconLabel::TextBesideIcon:
|
||||
default:
|
||||
const qreal combinedWidth = icon->width() + label->spacing() + text->width();
|
||||
|
|
Loading…
Reference in New Issue