86void QQuickMenuItemIconLabelPrivate::updateImplicitSize()
88 Q_Q(QQuickMenuItemIconLabel);
89 const bool showIcon = image && hasIcon();
90 const bool showText = label && hasText();
91#if QT_CONFIG(shortcut)
92 const bool showShortcutText = shortcutLabel && !shortcut().isEmpty();
94 const bool showShortcutText =
false;
96 const qreal horizontalPadding = leftPadding + rightPadding;
97 const qreal verticalPadding = topPadding + bottomPadding;
98 const qreal iconImplicitWidth = showIcon ? image->implicitWidth() : 0;
99 const qreal iconImplicitHeight = showIcon ? image->implicitHeight() : 0;
100 const qreal textImplicitWidth = showText ? label->implicitWidth() : 0;
101 const qreal textImplicitHeight = showText ? label->implicitHeight() : 0;
102 const qreal shortcutTextImplicitWidth = showShortcutText ? shortcutLabel->implicitWidth() : 0;
103 const qreal shortcutTextImplicitHeight = showShortcutText ? shortcutLabel->implicitHeight() : 0;
104 qreal effectiveSpacing = showText && showIcon && image->implicitWidth() > 0 ? spacing : 0;
105 if (showShortcutText && shortcutLabel->implicitWidth() > 0)
106 effectiveSpacing += spacing;
107 const bool shortcutFollowsText = display == QQuickIconLabel::TextBesideIcon
108 || display == QQuickIconLabel::TextOnly;
109 const qreal implicitWidth = shortcutFollowsText
110 ? iconImplicitWidth + textImplicitWidth + shortcutTextImplicitWidth + effectiveSpacing
111 : qMax(qMax(iconImplicitWidth, textImplicitWidth), shortcutTextImplicitWidth);
112 const qreal implicitHeight = display == QQuickIconLabel::TextUnderIcon
113 ? iconImplicitHeight + textImplicitHeight + shortcutTextImplicitHeight + effectiveSpacing
114 : qMax(qMax(iconImplicitHeight, textImplicitHeight), shortcutTextImplicitHeight);
115 q->setImplicitSize(implicitWidth + horizontalPadding, implicitHeight + verticalPadding);
118void QQuickMenuItemIconLabelPrivate::layout()
120 Q_Q(QQuickIconLabel);
121 if (!componentComplete)
124 const qreal availableWidth = width - leftPadding - rightPadding;
125 const qreal availableHeight = height - topPadding - bottomPadding;
127 auto layoutShortcutLabel = [
this, availableWidth, availableHeight](
const qreal availableWidthForShortcut){
128 if (availableWidthForShortcut - spacing - shortcutLabel->implicitWidth() >= 0) {
130 shortcutLabel->setVisible(
true);
133 const QRectF shortcutTextRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, QSizeF(
134 qMin(shortcutLabel->implicitWidth(), availableWidth),
135 qMin(shortcutLabel->implicitHeight(), availableHeight)),
136 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
137 shortcutLabel->setSize(shortcutTextRect.size());
138 shortcutLabel->setPosition(shortcutTextRect.topLeft());
143 shortcutLabel->setVisible(
false);
148 case QQuickIconLabel::IconOnly:
149 QQuickIconLabelPrivate::layout();
151 case QQuickIconLabel::TextOnly: {
153 const QRectF textRect = alignedRect(mirrored, alignment, QSizeF(
154 qMin(label->implicitWidth(), availableWidth),
155 qMin(label->implicitHeight(), availableHeight)),
156 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
157 label->setSize(textRect.size());
158 label->setPosition(textRect.topLeft());
162 layoutShortcutLabel(availableWidth - label->width());
164 }
case QQuickIconLabel::TextUnderIcon: {
168 QQuickIconLabelPrivate::layout();
171 case QQuickIconLabel::TextBesideIcon:
173 QSizeF iconSize(0, 0);
174 QSizeF textSize(0, 0);
175 QSizeF shortcutLabelSize(0, 0);
177 iconSize.setWidth(qMin(image->implicitWidth(), availableWidth));
178 iconSize.setHeight(qMin(image->implicitHeight(), availableHeight));
180 qreal spacingBetweenIconAndLabel = 0;
182 if (!iconSize.isEmpty())
183 spacingBetweenIconAndLabel = spacing;
184 textSize.setWidth(qMin(label->implicitWidth(), availableWidth - iconSize.width()
185 - spacingBetweenIconAndLabel));
186 textSize.setHeight(qMin(label->implicitHeight(), availableHeight));
189 qreal spacingBetweenLabelAndShortcutLabel = 0;
191 if (!textSize.isEmpty())
192 spacingBetweenLabelAndShortcutLabel = spacing;
193 shortcutLabelSize.setWidth(qMin(shortcutLabel->implicitWidth(), availableWidth - iconSize.width()
194 - spacingBetweenIconAndLabel - textSize.width() - spacingBetweenLabelAndShortcutLabel));
195 shortcutLabelSize.setHeight(qMin(shortcutLabel->implicitHeight(), availableHeight));
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 const QRectF combinedImageAndLabelRect = alignedRect(mirrored, alignment, QSizeF(
216 iconSize.width() + spacingBetweenIconAndLabel + textSize.width(),
217 qMax(iconSize.height(), textSize.height())),
218 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
220 const QRectF iconRect = alignedRect(mirrored, Qt::AlignLeft | Qt::AlignVCenter, iconSize, combinedImageAndLabelRect);
221 image->setSize(iconRect.size());
222 image->snapPositionTo(iconRect.topLeft());
225 const QRectF textRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, textSize, combinedImageAndLabelRect);
226 label->setSize(textRect.size());
227 label->setPosition(textRect.topLeft());
230 layoutShortcutLabel(availableWidth - combinedImageAndLabelRect.width());
234 q->setBaselineOffset(label ? label->y() + label->baselineOffset() : 0);
301void QQuickMenuItemIconLabel::componentComplete()
303 Q_D(QQuickMenuItemIconLabel);
304 if (d->shortcutLabel)
305 QQuickIconLabelPrivate::completeComponent(d->shortcutLabel);
307 QQuickIconLabel::componentComplete();
310 const auto paintOrderChildItems = QQuickItemPrivate::get(
this)->paintOrderChildItems();
311 const auto bottomMostFosterChildIt = std::find_if(paintOrderChildItems.constBegin(),
312 paintOrderChildItems.constEnd(), [d](QQuickItem *item) {
313 return item != d->label && item != d->image && item != d->shortcutLabel;
315 if (bottomMostFosterChildIt != paintOrderChildItems.constEnd()) {
316 const QQuickItem *bottomMostFosterChild = *bottomMostFosterChildIt;
317 if (d->shortcutLabel)
318 d->shortcutLabel->stackBefore(bottomMostFosterChild);