77void QQuickMenuItemIconLabelPrivate::updateImplicitSize()
79 Q_Q(QQuickMenuItemIconLabel);
80 const bool showIcon = image && hasIcon();
81 const bool showText = label && hasText();
82 const bool showShortcutText = shortcutLabel && !shortcut().isEmpty();
83 const qreal horizontalPadding = leftPadding + rightPadding;
84 const qreal verticalPadding = topPadding + bottomPadding;
85 const qreal iconImplicitWidth = showIcon ? image->implicitWidth() : 0;
86 const qreal iconImplicitHeight = showIcon ? image->implicitHeight() : 0;
87 const qreal textImplicitWidth = showText ? label->implicitWidth() : 0;
88 const qreal textImplicitHeight = showText ? label->implicitHeight() : 0;
89 const qreal shortcutTextImplicitWidth = showShortcutText ? shortcutLabel->implicitWidth() : 0;
90 const qreal shortcutTextImplicitHeight = showShortcutText ? shortcutLabel->implicitHeight() : 0;
91 qreal effectiveSpacing = showText && showIcon && image->implicitWidth() > 0 ? spacing : 0;
92 if (showShortcutText && shortcutLabel->implicitWidth() > 0)
93 effectiveSpacing += spacing;
94 const qreal implicitWidth = display == QQuickIconLabel::TextBesideIcon
95 ? iconImplicitWidth + textImplicitWidth + shortcutTextImplicitHeight + effectiveSpacing
96 : qMax(qMax(iconImplicitWidth, textImplicitWidth), shortcutTextImplicitWidth);
97 const qreal implicitHeight = display == QQuickIconLabel::TextUnderIcon
98 ? iconImplicitHeight + textImplicitHeight + shortcutTextImplicitHeight + effectiveSpacing
99 : qMax(qMax(iconImplicitHeight, textImplicitHeight), shortcutTextImplicitHeight);
100 q->setImplicitSize(implicitWidth + horizontalPadding, implicitHeight + verticalPadding);
103void QQuickMenuItemIconLabelPrivate::layout()
105 Q_Q(QQuickIconLabel);
106 if (!componentComplete)
109 const qreal availableWidth = width - leftPadding - rightPadding;
110 const qreal availableHeight = height - topPadding - bottomPadding;
112 auto layoutShortcutLabel = [
this, availableWidth, availableHeight](
const qreal availableWidthForShortcut){
113 if (availableWidthForShortcut - spacing - shortcutLabel->implicitWidth() > 0) {
115 shortcutLabel->setVisible(
true);
118 const QRectF shortcutTextRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, QSizeF(
119 qMin(shortcutLabel->implicitWidth(), availableWidth),
120 qMin(shortcutLabel->implicitHeight(), availableHeight)),
121 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
122 shortcutLabel->setSize(shortcutTextRect.size());
123 shortcutLabel->setPosition(shortcutTextRect.topLeft());
128 shortcutLabel->setVisible(
false);
133 case QQuickIconLabel::IconOnly:
134 QQuickIconLabelPrivate::layout();
136 case QQuickIconLabel::TextOnly: {
138 const QRectF textRect = alignedRect(mirrored, alignment, QSizeF(
139 qMin(label->implicitWidth(), availableWidth),
140 qMin(label->implicitHeight(), availableHeight)),
141 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
142 label->setSize(textRect.size());
143 label->setPosition(textRect.topLeft());
147 layoutShortcutLabel(availableWidth - (label->width() + spacing));
149 }
case QQuickIconLabel::TextUnderIcon: {
153 QQuickIconLabelPrivate::layout();
156 case QQuickIconLabel::TextBesideIcon:
158 QSizeF iconSize(0, 0);
159 QSizeF textSize(0, 0);
160 QSizeF shortcutLabelSize(0, 0);
162 iconSize.setWidth(qMin(image->implicitWidth(), availableWidth));
163 iconSize.setHeight(qMin(image->implicitHeight(), availableHeight));
165 qreal spacingBetweenIconAndLabel = 0;
167 if (!iconSize.isEmpty())
168 spacingBetweenIconAndLabel = spacing;
169 textSize.setWidth(qMin(label->implicitWidth(), availableWidth - iconSize.width()
170 - spacingBetweenIconAndLabel));
171 textSize.setHeight(qMin(label->implicitHeight(), availableHeight));
174 qreal spacingBetweenLabelAndShortcutLabel = 0;
176 if (!textSize.isEmpty())
177 spacingBetweenLabelAndShortcutLabel = spacing;
178 shortcutLabelSize.setWidth(qMin(shortcutLabel->implicitWidth(), availableWidth - iconSize.width()
179 - spacingBetweenIconAndLabel - textSize.width() - spacingBetweenLabelAndShortcutLabel));
180 shortcutLabelSize.setHeight(qMin(shortcutLabel->implicitHeight(), availableHeight));
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 const QRectF combinedImageAndLabelRect = alignedRect(mirrored, alignment, QSizeF(
201 iconSize.width() + spacingBetweenIconAndLabel + textSize.width(),
202 qMax(iconSize.height(), textSize.height())),
203 QRectF(leftPadding, topPadding, availableWidth, availableHeight));
205 const QRectF iconRect = alignedRect(mirrored, Qt::AlignLeft | Qt::AlignVCenter, iconSize, combinedImageAndLabelRect);
206 image->setSize(iconRect.size());
207 image->snapPositionTo(iconRect.topLeft());
210 const QRectF textRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, textSize, combinedImageAndLabelRect);
211 label->setSize(textRect.size());
212 label->setPosition(textRect.topLeft());
215 layoutShortcutLabel(availableWidth - combinedImageAndLabelRect.width());
219 q->setBaselineOffset(label ? label->y() + label->baselineOffset() : 0);
284void QQuickMenuItemIconLabel::componentComplete()
286 Q_D(QQuickMenuItemIconLabel);
287 if (d->shortcutLabel)
288 QQuickIconLabelPrivate::completeComponent(d->shortcutLabel);
290 QQuickIconLabel::componentComplete();
293 const auto paintOrderChildItems = QQuickItemPrivate::get(
this)->paintOrderChildItems();
294 const auto bottomMostFosterChildIt = std::find_if(paintOrderChildItems.constBegin(),
295 paintOrderChildItems.constEnd(), [d](QQuickItem *item) {
296 return item != d->label && item != d->image && item != d->shortcutLabel;
298 if (bottomMostFosterChildIt != paintOrderChildItems.constEnd()) {
299 const QQuickItem *bottomMostFosterChild = *bottomMostFosterChildIt;
300 if (d->shortcutLabel)
301 d->shortcutLabel->stackBefore(bottomMostFosterChild);