192static QRectF alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment,
const QSizeF &size,
const QRectF &rectangle)
194 alignment = QGuiApplicationPrivate::visualAlignment(direction, alignment);
195 qreal x = rectangle.x();
196 qreal y = rectangle.y();
197 qreal w = size.width();
198 qreal h = size.height();
200 if ((alignment & Qt::AlignVCenter) == Qt::AlignVCenter || (alignment & Qt::AlignVertical_Mask) == 0)
201 y += qMax<qreal>(0, (rectangle.size().height() - h) / 2);
202 else if ((alignment & Qt::AlignBottom) == Qt::AlignBottom)
203 y += qMax<qreal>(0, rectangle.size().height() - h);
204 if ((alignment & Qt::AlignRight) == Qt::AlignRight)
205 x += qMax<qreal>(0, rectangle.size().width() - w);
206 else if ((alignment & Qt::AlignHCenter) == Qt::AlignHCenter)
207 x += qMax<qreal>(0, (rectangle.size().width() - w) / 2);
208 return QRectF(x, y, w, h);
211void QQuickDialogButtonBoxPrivate::resizeContent()
213 Q_Q(QQuickDialogButtonBox);
214 if (!contentItem || !contentModel)
217 QRectF geometry = q->boundingRect().adjusted(q->leftPadding(), q->topPadding(), -q->rightPadding(), -q->bottomPadding());
219 geometry = alignedRect(q->isMirrored() ? Qt::RightToLeft : Qt::LeftToRight, alignment, QSizeF(contentWidth, contentHeight), geometry);
221 contentItem->setPosition(geometry.topLeft());
222 contentItem->setSize(geometry.size());
225void QQuickDialogButtonBoxPrivate::updateLayout()
227 Q_Q(QQuickDialogButtonBox);
228 const int count = contentModel->count();
232 const int halign = alignment & Qt::AlignHorizontal_Mask;
233 const int valign = alignment & Qt::AlignVertical_Mask;
235 QList<QQuickAbstractButton *> buttons;
236 const qreal cw = (alignment & Qt::AlignHorizontal_Mask) == 0 ? q->availableWidth() : contentWidth;
237 const qreal itemWidth = (cw - qMax(0, count - 1) * spacing) / count;
239 for (
int i = 0; i < count; ++i) {
240 QQuickItem *item = q->itemAt(i);
242 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
243 if (!p->widthValid()) {
245 item->setWidth(itemWidth);
249 item->setHeight(contentHeight);
252 p->widthValidFlag =
false;
255 buttons +=
static_cast<QQuickAbstractButton *>(item);
258 struct ButtonLayout {
259 ButtonLayout(QPlatformDialogHelper::ButtonLayout layout)
260 : m_layout(QPlatformDialogHelper::buttonLayout(Qt::Horizontal, layout))
264 bool operator()(QQuickAbstractButton *first, QQuickAbstractButton *second)
266 const QPlatformDialogHelper::ButtonRole firstRole = QQuickDialogPrivate::buttonRole(first);
267 const QPlatformDialogHelper::ButtonRole secondRole = QQuickDialogPrivate::buttonRole(second);
269 if (firstRole != secondRole && firstRole != QPlatformDialogHelper::InvalidRole && secondRole != QPlatformDialogHelper::InvalidRole) {
270 const int *l = m_layout;
271 while (*l != QPlatformDialogHelper::EOL) {
273 const int role = (*l & ~QPlatformDialogHelper::Reverse);
274 if (role == firstRole)
276 if (role == secondRole)
282 if (firstRole == secondRole)
285 return firstRole != QPlatformDialogHelper::InvalidRole;
290 std::stable_sort(buttons.begin(), buttons.end(), ButtonLayout(
static_cast<QPlatformDialogHelper::ButtonLayout>(buttonLayout)));
292 for (
int i = 0; i < buttons.size() - 1; ++i)
293 q->insertItem(i, buttons.at(i));
296int QQuickDialogButtonBoxPrivate::indexOfDefaultStandardButton()
const
298 for (
int i = 0; i < contentModel->count(); ++i) {
299 if (
auto *button = qobject_cast<QQuickButton *>(itemAt(i))) {
300 const auto stdButton = standardButton(button);
301 const bool isDefaultStdbutton = defaultStandardButton != QPlatformDialogHelper::NoButton
302 && stdButton == defaultStandardButton;
303 const bool isDefaultButton = defaultButton == button;
305 if (isDefaultButton || isDefaultStdbutton)
312int QQuickDialogButtonBoxPrivate::indexOfAcceptButton()
const
314 for (
int i = 0; i < contentModel->count(); ++i) {
315 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(itemAt(i));
316 if (button && QQuickDialogPrivate::buttonRole(button) == QPlatformDialogHelper::ButtonRole::AcceptRole)
322void QQuickDialogButtonBoxPrivate::updateFocus()
324 Q_Q(QQuickDialogButtonBox);
325 const int count = contentModel->count();
330 int indexOfFocusButton = indexOfDefaultStandardButton();
331 QQuickButton *buttonToHighlight = qobject_cast<QQuickButton *>(q->itemAt(indexOfFocusButton));
334 if (indexOfFocusButton < 0)
335 indexOfFocusButton = indexOfAcceptButton();
337 if (QQuickItemView *itemView = qobject_cast<QQuickItemView *>(contentItem)) {
340 itemView->setCurrentIndex(indexOfFocusButton);
341 itemView->setFocus(indexOfFocusButton >= 0);
344 for (
int i = 0; i < count; ++i) {
345 if (
auto *button = qobject_cast<QQuickButton *>(contentModel->get(i))) {
346 button->setFocus(i == indexOfFocusButton);
347 button->setHighlighted(button == buttonToHighlight);
348#if QT_CONFIG(accessibility)
349 if (
auto *accessibleAttached = qobject_cast<QQuickAccessibleAttached *>(qmlAttachedPropertiesObject<QQuickAccessibleAttached>(button,
true)))
350 accessibleAttached->set_defaultButton(button == buttonToHighlight);
356qreal QQuickDialogButtonBoxPrivate::getContentWidth()
const
358 Q_Q(
const QQuickDialogButtonBox);
362 const int count = contentModel->count();
363 const qreal totalSpacing = qMax(0, count - 1) * spacing;
364 qreal totalWidth = totalSpacing;
366 for (
int i = 0; i < count; ++i) {
367 QQuickItem *item = q->itemAt(i);
369 totalWidth += item->implicitWidth();
370 maxWidth = qMax(maxWidth, item->implicitWidth());
373 if ((alignment & Qt::AlignHorizontal_Mask) == 0)
374 totalWidth = qMax(totalWidth, count * maxWidth + totalSpacing);
378qreal QQuickDialogButtonBoxPrivate::getContentHeight()
const
380 Q_Q(
const QQuickDialogButtonBox);
384 const int count = contentModel->count();
386 for (
int i = 0; i < count; ++i) {
387 QQuickItem *item = q->itemAt(i);
389 maxHeight = qMax(maxHeight, item->implicitHeight());
394void QQuickDialogButtonBoxPrivate::handleClick()
396 Q_Q(QQuickDialogButtonBox);
397 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
405 const QPlatformDialogHelper::ButtonRole role = QQuickDialogPrivate::buttonRole(button);
406 QPointer<QQuickDialogButtonBox> guard(q);
408 emit q->clicked(button);
414 case QPlatformDialogHelper::AcceptRole:
415 case QPlatformDialogHelper::YesRole:
418 case QPlatformDialogHelper::RejectRole:
419 case QPlatformDialogHelper::NoRole:
422 case QPlatformDialogHelper::ApplyRole:
425 case QPlatformDialogHelper::ResetRole:
428 case QPlatformDialogHelper::DestructiveRole:
431 case QPlatformDialogHelper::HelpRole:
432 emit q->helpRequested();
444QQuickAbstractButton *QQuickDialogButtonBoxPrivate::createStandardButton(QPlatformDialogHelper::StandardButton standardButton)
446 Q_Q(QQuickDialogButtonBox);
450 QQmlContext *creationContext = delegate->creationContext();
451 if (!creationContext)
452 creationContext = qmlContext(q);
454 QObject *object = delegate->beginCreate(creationContext);
455 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton*>(object);
457 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button,
true));
458 QQuickDialogButtonBoxAttachedPrivate::get(attached)->standardButton = standardButton;
459 attached->setButtonRole(QPlatformDialogHelper::buttonRole(standardButton));
460 button->setText(buttonText(standardButton));
461 delegate->completeCreate();
462 button->setParent(q);
470void QQuickDialogButtonBoxPrivate::removeStandardButtons()
472 Q_Q(QQuickDialogButtonBox);
473 int i = q->count() - 1;
475 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->itemAt(i));
477 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(
478 qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button,
false));
480 QQuickDialogButtonBoxAttachedPrivate *p = QQuickDialogButtonBoxAttachedPrivate::get(attached);
481 if (p->standardButton != QPlatformDialogHelper::NoButton) {
482 q->removeItem(button);
483 button->deleteLater();
491void QQuickDialogButtonBoxPrivate::updateLanguage()
493 Q_Q(QQuickDialogButtonBox);
494 int i = q->count() - 1;
496 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(itemAt(i));
498 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(
499 qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button,
true));
500 const auto boxAttachedPrivate = QQuickDialogButtonBoxAttachedPrivate::get(attached);
501 const QPlatformDialogHelper::StandardButton standardButton = boxAttachedPrivate->standardButton;
503 if (standardButton != QPlatformDialogHelper::NoButton) {
504 button->setText(buttonText(standardButton));
511QPlatformDialogHelper::StandardButton QQuickDialogButtonBoxPrivate::standardButton(QQuickAbstractButton *button)
const {
512 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button,
false));
514 return QQuickDialogButtonBoxAttachedPrivate::get(attached)->standardButton;
516 return QPlatformDialogHelper::NoButton;
519QQuickDialogButtonBox::QQuickDialogButtonBox(QQuickItem *parent)
520 : QQuickContainer(*(
new QQuickDialogButtonBoxPrivate), parent)
522 Q_D(QQuickDialogButtonBox);
523 setFlag(ItemIsFocusScope);
524 d->changeTypes |= QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight;
525 d->buttonLayout = platformButtonLayout();
526 d->setSizePolicy(QLayoutPolicy::Preferred, QLayoutPolicy::Fixed);
602void QQuickDialogButtonBox::setAlignment(Qt::Alignment alignment)
604 Q_D(QQuickDialogButtonBox);
605 if (d->alignment == alignment)
608 d->alignment = alignment;
609 if (isComponentComplete()) {
613 emit alignmentChanged();
659void QQuickDialogButtonBox::setStandardButtons(QPlatformDialogHelper::StandardButtons buttons)
661 Q_D(QQuickDialogButtonBox);
662 if (d->standardButtons == buttons)
665 d->removeStandardButtons();
667 for (
int i = QPlatformDialogHelper::FirstButton; i <= QPlatformDialogHelper::LastButton; i<<=1) {
668 QPlatformDialogHelper::StandardButton standardButton =
static_cast<QPlatformDialogHelper::StandardButton>(i);
669 if (standardButton & buttons) {
670 QQuickAbstractButton *button = d->createStandardButton(standardButton);
676 if (isComponentComplete())
679 d->standardButtons = buttons;
680 emit standardButtonsChanged();
690QQuickAbstractButton *QQuickDialogButtonBox::standardButton(QPlatformDialogHelper::StandardButton button)
const
692 Q_D(
const QQuickDialogButtonBox);
693 if (Q_UNLIKELY(!(d->standardButtons & button)))
695 for (
int i = 0, n = count(); i < n; ++i) {
696 QQuickAbstractButton *btn = qobject_cast<QQuickAbstractButton *>(d->itemAt(i));
698 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(btn,
false));
699 if (attached && QQuickDialogButtonBoxAttachedPrivate::get(attached)->standardButton == button)
728void QQuickDialogButtonBox::setDefaultStandardButton(QPlatformDialogHelper::StandardButton button)
730 Q_D(QQuickDialogButtonBox);
731 if (d->defaultStandardButton == button)
734 d->defaultStandardButton = button;
736 if (isComponentComplete())
739 emit defaultStandardButtonChanged();
764void QQuickDialogButtonBox::setDefaultButton(QQuickAbstractButton *button)
766 Q_D(QQuickDialogButtonBox);
767 if (d->defaultButton == button)
770 if (d->defaultButton)
771 removeItem(d->defaultButton);
773 d->defaultButton = button;
775 if (d->defaultButton)
776 addItem(d->defaultButton);
778 if (isComponentComplete())
781 emit defaultButtonChanged();
834void QQuickDialogButtonBox::setButtonLayout(QPlatformDialogHelper::ButtonLayout layout)
836 Q_D(QQuickDialogButtonBox);
837 if (d->buttonLayout == layout)
840 d->buttonLayout = layout;
841 if (isComponentComplete())
843 emit buttonLayoutChanged();
882void QQuickDialogButtonBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
884 Q_D(QQuickDialogButtonBox);
885 QQuickContainer::contentItemChange(newItem, oldItem);
887 QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight);
889 QQuickItemPrivate::get(newItem)->addItemChangeListener(d, QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight);
897void QQuickDialogButtonBox::itemAdded(
int index, QQuickItem *item)
899 Q_D(QQuickDialogButtonBox);
901 if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(item))
902 QObjectPrivate::connect(button, &QQuickAbstractButton::clicked, d, &QQuickDialogButtonBoxPrivate::handleClick);
903 if (QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(item,
false)))
904 QQuickDialogButtonBoxAttachedPrivate::get(attached)->setButtonBox(
this);
905 d->updateImplicitContentSize();
906 if (isComponentComplete())
910void QQuickDialogButtonBox::itemRemoved(
int index, QQuickItem *item)
912 Q_D(QQuickDialogButtonBox);
914 if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(item))
915 QObjectPrivate::disconnect(button, &QQuickAbstractButton::clicked, d, &QQuickDialogButtonBoxPrivate::handleClick);
916 if (QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(item,
false)))
917 QQuickDialogButtonBoxAttachedPrivate::get(attached)->setButtonBox(
nullptr);
918 d->updateImplicitContentSize();
919 if (isComponentComplete())
923void QQuickDialogButtonBox::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
925 QQuickContainer::itemChange(change, data);
926 if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue)
929 Q_D(QQuickDialogButtonBox);
950QQuickDialogButtonBoxAttached::QQuickDialogButtonBoxAttached(QObject *parent)
951 : QObject(*(
new QQuickDialogButtonBoxAttachedPrivate), parent)
953 Q_D(QQuickDialogButtonBoxAttached);
954 QQuickItem *parentItem = qobject_cast<QQuickItem *>(parent);
955 while (parentItem && !d->buttonBox) {
956 d->buttonBox = qobject_cast<QQuickDialogButtonBox *>(parentItem);
957 parentItem = parentItem->parentItem();