78void QQuickMenuItemIconLabelPrivate::updateImplicitSize()
80 Q_Q(QQuickMenuItemIconLabel);
81 const bool showIcon = image && hasIcon();
82 const bool showText = label && hasText();
83 const bool showShortcutText = shortcutLabel && !shortcut().isEmpty();
84 const qreal horizontalPadding = leftPadding + rightPadding;
85 const qreal verticalPadding = topPadding + bottomPadding;
86 const qreal iconImplicitWidth = showIcon ? image->implicitWidth() : 0;
87 const qreal iconImplicitHeight = showIcon ? image->implicitHeight() : 0;
88 const qreal textImplicitWidth = showText ? label->implicitWidth() : 0;
89 const qreal textImplicitHeight = showText ? label->implicitHeight() : 0;
90 const qreal shortcutTextImplicitWidth = showShortcutText ? shortcutLabel->implicitWidth() : 0;
91 const qreal shortcutTextImplicitHeight = showShortcutText ? shortcutLabel->implicitHeight() : 0;
92 qreal effectiveSpacing = showText && showIcon && image->implicitWidth() > 0 ? spacing : 0;
93 if (showShortcutText && shortcutLabel->implicitWidth() > 0)
94 effectiveSpacing += spacing;
95 const qreal implicitWidth = display == QQuickIconLabel::TextBesideIcon
96 ? iconImplicitWidth + textImplicitWidth + shortcutTextImplicitHeight + effectiveSpacing
97 : qMax(qMax(iconImplicitWidth, textImplicitWidth), shortcutTextImplicitWidth);
98 const qreal implicitHeight = display == QQuickIconLabel::TextUnderIcon
99 ? iconImplicitHeight + textImplicitHeight + shortcutTextImplicitHeight + effectiveSpacing
100 : qMax(qMax(iconImplicitHeight, textImplicitHeight), shortcutTextImplicitHeight);
101 q->setImplicitSize(implicitWidth + horizontalPadding, implicitHeight + verticalPadding);
104void QQuickMenuItemIconLabelPrivate::layout()
106 Q_Q(QQuickIconLabel);
107 if (!componentComplete)
110 const qreal availableWidth = width - leftPadding - rightPadding;
111 const qreal availableHeight = height - topPadding - bottomPadding;
113 auto layoutShortcutLabel = [
this, availableWidth, availableHeight](
const qreal availableWidthForShortcut){
114 if (availableWidthForShortcut - spacing - shortcutLabel->implicitWidth() > 0) {
116 shortcutLabel->setVisible(
true);
119 const QRectF shortcutTextRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, QSizeF(
120 qMin(shortcutLabel->implicitWidth(), availableWidth),
121 qMin(shortcutLabel->implicitHeight(), availableHeight)),
122 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
123 shortcutLabel->setSize(shortcutTextRect.size());
124 shortcutLabel->setPosition(shortcutTextRect.topLeft());
129 shortcutLabel->setVisible(
false);
134 case QQuickIconLabel::IconOnly:
135 QQuickIconLabelPrivate::layout();
137 case QQuickIconLabel::TextOnly: {
139 const QRectF textRect = alignedRect(mirrored, alignment, QSizeF(
140 qMin(label->implicitWidth(), availableWidth),
141 qMin(label->implicitHeight(), availableHeight)),
142 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
143 label->setSize(textRect.size());
144 label->setPosition(textRect.topLeft());
148 layoutShortcutLabel(availableWidth - (label->width() + spacing));
150 }
case QQuickIconLabel::TextUnderIcon: {
154 QQuickIconLabelPrivate::layout();
157 case QQuickIconLabel::TextBesideIcon:
159 QSizeF iconSize(0, 0);
160 QSizeF textSize(0, 0);
161 QSizeF shortcutLabelSize(0, 0);
163 iconSize.setWidth(qMin(image->implicitWidth(), availableWidth));
164 iconSize.setHeight(qMin(image->implicitHeight(), availableHeight));
166 qreal spacingBetweenIconAndLabel = 0;
168 if (!iconSize.isEmpty())
169 spacingBetweenIconAndLabel = spacing;
170 textSize.setWidth(qMin(label->implicitWidth(), availableWidth - iconSize.width()
171 - spacingBetweenIconAndLabel));
172 textSize.setHeight(qMin(label->implicitHeight(), availableHeight));
175 qreal spacingBetweenLabelAndShortcutLabel = 0;
177 if (!textSize.isEmpty())
178 spacingBetweenLabelAndShortcutLabel = spacing;
179 shortcutLabelSize.setWidth(qMin(shortcutLabel->implicitWidth(), availableWidth - iconSize.width()
180 - spacingBetweenIconAndLabel - textSize.width() - spacingBetweenLabelAndShortcutLabel));
181 shortcutLabelSize.setHeight(qMin(shortcutLabel->implicitHeight(), availableHeight));
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 const QRectF combinedImageAndLabelRect = alignedRect(mirrored, alignment, QSizeF(
202 iconSize.width() + spacingBetweenIconAndLabel + textSize.width(),
203 qMax(iconSize.height(), textSize.height())),
204 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
206 const QRectF iconRect = alignedRect(mirrored, Qt::AlignLeft | Qt::AlignVCenter, iconSize, combinedImageAndLabelRect);
207 image->setSize(iconRect.size());
208 image->snapPositionTo(iconRect.topLeft());
211 const QRectF textRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, textSize, combinedImageAndLabelRect);
212 label->setSize(textRect.size());
213 label->setPosition(textRect.topLeft());
216 layoutShortcutLabel(availableWidth - combinedImageAndLabelRect.width());
220 q->setBaselineOffset(label ? label->y() + label->baselineOffset() : 0);
285void QQuickMenuItemIconLabel::componentComplete()
287 Q_D(QQuickMenuItemIconLabel);
288 if (d->shortcutLabel)
289 QQuickIconLabelPrivate::completeComponent(d->shortcutLabel);
291 QQuickIconLabel::componentComplete();
294 const auto paintOrderChildItems = QQuickItemPrivate::get(
this)->paintOrderChildItems();
295 const auto bottomMostFosterChildIt = std::find_if(paintOrderChildItems.constBegin(),
296 paintOrderChildItems.constEnd(), [d](QQuickItem *item) {
297 return item != d->label && item != d->image && item != d->shortcutLabel;
299 if (bottomMostFosterChildIt != paintOrderChildItems.constEnd()) {
300 const QQuickItem *bottomMostFosterChild = *bottomMostFosterChildIt;
301 if (d->shortcutLabel)
302 d->shortcutLabel->stackBefore(bottomMostFosterChild);