Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickdialogbuttonbox.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
10
11#include <QtCore/qpointer.h>
12#include <QtGui/private/qguiapplication_p.h>
13#include <QtGui/qpa/qplatformtheme.h>
14#include <QtQml/qqmlengine.h>
15#include <QtQml/qqmlcontext.h>
16#include <QtQml/qqmlcomponent.h>
17#if QT_CONFIG(accessibility)
18#include <QtQuick/private/qquickaccessibleattached_p.h>
19#endif
20#include <QtQuick/private/qquickitemview_p.h>
21
22#include <algorithm>
23
24QT_BEGIN_NAMESPACE
25
26/*!
27 \qmltype DialogButtonBox
28 \inherits Container
29//! \nativetype QQuickDialogButtonBox
30 \inqmlmodule QtQuick.Controls
31 \ingroup qtquickcontrols-dialogs
32 \brief A button box used in dialogs.
33 \since 5.8
34
35 Dialogs and message boxes typically present buttons in an order that
36 conforms to the interface guidelines for that platform. Invariably,
37 different platforms have their dialog buttons in different orders.
38 DialogButtonBox allows a developer to add buttons to it and will
39 automatically use the appropriate order for the user's platform.
40
41 Most buttons for a dialog follow certain roles. Such roles include:
42
43 \list
44 \li Accepting or rejecting the dialog.
45 \li Asking for help.
46 \li Performing actions on the dialog itself (such as resetting fields or
47 applying changes).
48 \endlist
49
50 There can also be alternate ways of dismissing the dialog which may cause
51 destructive results.
52
53 Most dialogs have buttons that can almost be considered standard (e.g.
54 \uicontrol OK and \uicontrol Cancel buttons). It is sometimes convenient
55 to create these buttons in a standard way.
56
57 There are a couple ways of using DialogButtonBox. One way is to specify
58 the standard buttons (e.g. \uicontrol OK, \uicontrol Cancel, \uicontrol Save)
59 and let the button box setup the buttons.
60
61 \image qtquickcontrols-dialogbuttonbox.png
62 {Dialog button box with OK and Cancel buttons}
63
64 \snippet qtquickcontrols-dialogbuttonbox.qml 1
65
66 Alternatively, buttons and their roles can be specified by hand:
67
68 \snippet qtquickcontrols-dialogbuttonbox-attached.qml 1
69
70 You can also mix and match normal buttons and standard buttons.
71
72 When a button is clicked in the button box, the \l clicked() signal is
73 emitted for the actual button that is pressed. In addition, the
74 following signals are automatically emitted when a button with the
75 respective role(s) is pressed:
76
77 \table
78 \header
79 \li Role
80 \li Signal
81 \row
82 \li \c AcceptRole, \c YesRole
83 \li \l accepted()
84 \row
85 \li \c ApplyRole
86 \li \l applied()
87 \row
88 \li \c DiscardRole
89 \li \l discarded()
90 \row
91 \li \c HelpRole
92 \li \l helpRequested()
93 \row
94 \li \c RejectRole, \c NoRole
95 \li \l rejected()
96 \row
97 \li \c ResetRole
98 \li \l reset()
99 \endtable
100
101 \sa Dialog
102*/
103
104/*!
105 \qmlsignal QtQuick.Controls::DialogButtonBox::accepted()
106
107 This signal is emitted when a button defined with the \c AcceptRole or
108 \c YesRole is clicked.
109
110 \sa rejected(), clicked(), helpRequested()
111*/
112
113/*!
114 \qmlsignal QtQuick.Controls::DialogButtonBox::rejected()
115
116 This signal is emitted when a button defined with the \c RejectRole or
117 \c NoRole is clicked.
118
119 \sa accepted(), helpRequested(), clicked()
120*/
121
122/*!
123 \since QtQuick.Controls 2.3 (Qt 5.10)
124 \qmlsignal QtQuick.Controls::DialogButtonBox::applied()
125
126 This signal is emitted when a button defined with the \c ApplyRole is
127 clicked.
128
129 \sa discarded(), reset()
130*/
131
132/*!
133 \since QtQuick.Controls 2.3 (Qt 5.10)
134 \qmlsignal QtQuick.Controls::DialogButtonBox::reset()
135
136 This signal is emitted when a button defined with the \c ResetRole is
137 clicked.
138
139 \sa discarded(), applied()
140*/
141
142/*!
143 \since QtQuick.Controls 2.3 (Qt 5.10)
144 \qmlsignal QtQuick.Controls::DialogButtonBox::discarded()
145
146 This signal is emitted when a button defined with the \c DiscardRole is
147 clicked.
148
149 \sa reset(), applied()
150*/
151
152/*!
153 \qmlsignal QtQuick.Controls::DialogButtonBox::helpRequested()
154
155 This signal is emitted when a button defined with the \c HelpRole is clicked.
156
157 \sa accepted(), rejected(), clicked()
158*/
159
160/*!
161 \qmlsignal QtQuick.Controls::DialogButtonBox::clicked(AbstractButton button)
162
163 This signal is emitted when a \a button inside the button box is clicked.
164
165 \sa accepted(), rejected(), helpRequested()
166*/
167
168static QPlatformDialogHelper::ButtonLayout platformButtonLayout()
169{
170 return QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::DialogButtonBoxLayout).value<QPlatformDialogHelper::ButtonLayout>();
171}
172
173void QQuickDialogButtonBoxPrivate::itemImplicitWidthChanged(QQuickItem *item)
174{
175 QQuickContainerPrivate::itemImplicitWidthChanged(item);
176 if (item == contentItem)
177 resizeContent();
178 else
179 updateImplicitContentWidth();
180}
181
182void QQuickDialogButtonBoxPrivate::itemImplicitHeightChanged(QQuickItem *item)
183{
184 QQuickContainerPrivate::itemImplicitHeightChanged(item);
185 if (item == contentItem)
186 resizeContent();
187 else
188 updateImplicitContentHeight();
189}
190
191// adapted from QStyle::alignedRect()
192static QRectF alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSizeF &size, const QRectF &rectangle)
193{
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();
199 // The qMax() calls ensure that the x and y values are never negative, which can happen if the contentWidth is smaller than the width.
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);
209}
210
211void QQuickDialogButtonBoxPrivate::resizeContent()
212{
213 Q_Q(QQuickDialogButtonBox);
214 if (!contentItem || !contentModel)
215 return;
216
217 QRectF geometry = q->boundingRect().adjusted(q->leftPadding(), q->topPadding(), -q->rightPadding(), -q->bottomPadding());
218 if (alignment != 0)
219 geometry = alignedRect(q->isMirrored() ? Qt::RightToLeft : Qt::LeftToRight, alignment, QSizeF(contentWidth, contentHeight), geometry);
220
221 contentItem->setPosition(geometry.topLeft());
222 contentItem->setSize(geometry.size());
223}
224
225void QQuickDialogButtonBoxPrivate::updateLayout()
226{
227 Q_Q(QQuickDialogButtonBox);
228 const int count = contentModel->count();
229 if (count <= 0)
230 return;
231
232 const int halign = alignment & Qt::AlignHorizontal_Mask;
233 const int valign = alignment & Qt::AlignVertical_Mask;
234
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;
238
239 for (int i = 0; i < count; ++i) {
240 QQuickItem *item = q->itemAt(i);
241 if (item) {
242 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
243 if (!p->widthValid()) {
244 if (!halign)
245 item->setWidth(itemWidth);
246 else
247 item->resetWidth();
248 if (!valign)
249 item->setHeight(contentHeight);
250 else
251 item->resetHeight();
252 p->widthValidFlag = false;
253 }
254 }
255 buttons += static_cast<QQuickAbstractButton *>(item);
256 }
257
258 struct ButtonLayout {
259 ButtonLayout(QPlatformDialogHelper::ButtonLayout layout)
260 : m_layout(QPlatformDialogHelper::buttonLayout(Qt::Horizontal, layout))
261 {
262 }
263
264 bool operator()(QQuickAbstractButton *first, QQuickAbstractButton *second)
265 {
266 const QPlatformDialogHelper::ButtonRole firstRole = QQuickDialogPrivate::buttonRole(first);
267 const QPlatformDialogHelper::ButtonRole secondRole = QQuickDialogPrivate::buttonRole(second);
268
269 if (firstRole != secondRole && firstRole != QPlatformDialogHelper::InvalidRole && secondRole != QPlatformDialogHelper::InvalidRole) {
270 const int *l = m_layout;
271 while (*l != QPlatformDialogHelper::EOL) {
272 // Unset the Reverse flag.
273 const int role = (*l & ~QPlatformDialogHelper::Reverse);
274 if (role == firstRole)
275 return true;
276 if (role == secondRole)
277 return false;
278 ++l;
279 }
280 }
281
282 if (firstRole == secondRole)
283 return false;
284
285 return firstRole != QPlatformDialogHelper::InvalidRole;
286 }
287 const int *m_layout;
288 };
289
290 std::stable_sort(buttons.begin(), buttons.end(), ButtonLayout(static_cast<QPlatformDialogHelper::ButtonLayout>(buttonLayout)));
291
292 for (int i = 0; i < buttons.size() - 1; ++i)
293 q->insertItem(i, buttons.at(i));
294}
295
296int QQuickDialogButtonBoxPrivate::indexOfDefaultStandardButton() const
297{
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;
304
305 if (isDefaultButton || isDefaultStdbutton)
306 return i;
307 }
308 }
309 return -1;
310}
311
312int QQuickDialogButtonBoxPrivate::indexOfAcceptButton() const
313{
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)
317 return i;
318 }
319 return -1;
320}
321
322void QQuickDialogButtonBoxPrivate::updateFocus()
323{
324 Q_Q(QQuickDialogButtonBox);
325 const int count = contentModel->count();
326 if (count <= 0)
327 return;
328
329 // Give focus to the first default button
330 int indexOfFocusButton = indexOfDefaultStandardButton();
331 QQuickButton *buttonToHighlight = qobject_cast<QQuickButton *>(q->itemAt(indexOfFocusButton));
332
333 // Give focus to the first button with the accept role, if there are no default buttons
334 if (indexOfFocusButton < 0)
335 indexOfFocusButton = indexOfAcceptButton();
336
337 if (QQuickItemView *itemView = qobject_cast<QQuickItemView *>(contentItem)) {
338 // QQuickItemView has the ItemIsFocusScope flag, and also calls setFocus(true) for the first delegate item it creates
339 // In order for the default button to have active focus, we call setFocus(true) for both the item view, and the default button.
340 itemView->setCurrentIndex(indexOfFocusButton);
341 itemView->setFocus(indexOfFocusButton >= 0);
342 }
343
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);
351#endif
352 }
353 }
354}
355
356qreal QQuickDialogButtonBoxPrivate::getContentWidth() const
357{
358 Q_Q(const QQuickDialogButtonBox);
359 if (!contentModel)
360 return 0;
361
362 const int count = contentModel->count();
363 const qreal totalSpacing = qMax(0, count - 1) * spacing;
364 qreal totalWidth = totalSpacing;
365 qreal maxWidth = 0;
366 for (int i = 0; i < count; ++i) {
367 QQuickItem *item = q->itemAt(i);
368 if (item) {
369 totalWidth += item->implicitWidth();
370 maxWidth = qMax(maxWidth, item->implicitWidth());
371 }
372 }
373 if ((alignment & Qt::AlignHorizontal_Mask) == 0)
374 totalWidth = qMax(totalWidth, count * maxWidth + totalSpacing);
375 return totalWidth;
376}
377
378qreal QQuickDialogButtonBoxPrivate::getContentHeight() const
379{
380 Q_Q(const QQuickDialogButtonBox);
381 if (!contentModel)
382 return 0;
383
384 const int count = contentModel->count();
385 qreal maxHeight = 0;
386 for (int i = 0; i < count; ++i) {
387 QQuickItem *item = q->itemAt(i);
388 if (item)
389 maxHeight = qMax(maxHeight, item->implicitHeight());
390 }
391 return maxHeight;
392}
393
394void QQuickDialogButtonBoxPrivate::handleClick()
395{
396 Q_Q(QQuickDialogButtonBox);
397 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
398 if (!button)
399 return;
400
401 // Can't fetch this *after* emitting clicked, as clicked may destroy the button
402 // or change its role. Now changing the role is not possible yet, but arguably
403 // both clicked and accepted/rejected/etc. should be emitted "atomically"
404 // depending on whatever role the button had at the time of the click.
405 const QPlatformDialogHelper::ButtonRole role = QQuickDialogPrivate::buttonRole(button);
406 QPointer<QQuickDialogButtonBox> guard(q);
407
408 emit q->clicked(button);
409
410 if (!guard)
411 return;
412
413 switch (role) {
414 case QPlatformDialogHelper::AcceptRole:
415 case QPlatformDialogHelper::YesRole:
416 emit q->accepted();
417 break;
418 case QPlatformDialogHelper::RejectRole:
419 case QPlatformDialogHelper::NoRole:
420 emit q->rejected();
421 break;
422 case QPlatformDialogHelper::ApplyRole:
423 emit q->applied();
424 break;
425 case QPlatformDialogHelper::ResetRole:
426 emit q->reset();
427 break;
428 case QPlatformDialogHelper::DestructiveRole:
429 emit q->discarded();
430 break;
431 case QPlatformDialogHelper::HelpRole:
432 emit q->helpRequested();
433 break;
434 default:
435 break;
436 }
437}
438
439QString QQuickDialogButtonBoxPrivate::buttonText(QPlatformDialogHelper::StandardButton standardButton)
440{
441 return QPlatformTheme::removeMnemonics(QGuiApplicationPrivate::platformTheme()->standardButtonText(standardButton));
442}
443
444QQuickAbstractButton *QQuickDialogButtonBoxPrivate::createStandardButton(QPlatformDialogHelper::StandardButton standardButton)
445{
446 Q_Q(QQuickDialogButtonBox);
447 if (!delegate)
448 return nullptr;
449
450 QQmlContext *creationContext = delegate->creationContext();
451 if (!creationContext)
452 creationContext = qmlContext(q);
453
454 QObject *object = delegate->beginCreate(creationContext);
455 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton*>(object);
456 if (button) {
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);
463 return button;
464 }
465
466 delete object;
467 return nullptr;
468}
469
470void QQuickDialogButtonBoxPrivate::removeStandardButtons()
471{
472 Q_Q(QQuickDialogButtonBox);
473 int i = q->count() - 1;
474 while (i >= 0) {
475 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->itemAt(i));
476 if (button) {
477 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(
478 qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button, false));
479 if (attached) {
480 QQuickDialogButtonBoxAttachedPrivate *p = QQuickDialogButtonBoxAttachedPrivate::get(attached);
481 if (p->standardButton != QPlatformDialogHelper::NoButton) {
482 q->removeItem(button);
483 button->deleteLater();
484 }
485 }
486 }
487 --i;
488 }
489}
490
491void QQuickDialogButtonBoxPrivate::updateLanguage()
492{
493 Q_Q(QQuickDialogButtonBox);
494 int i = q->count() - 1;
495 while (i >= 0) {
496 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(itemAt(i));
497 if (button) {
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;
502 // The button might be a custom one with explicitly specified text, so we shouldn't change it in that case.
503 if (standardButton != QPlatformDialogHelper::NoButton) {
504 button->setText(buttonText(standardButton));
505 }
506 }
507 --i;
508 }
509}
510
511QPlatformDialogHelper::StandardButton QQuickDialogButtonBoxPrivate::standardButton(QQuickAbstractButton *button) const {
512 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(button, false));
513 if (attached)
514 return QQuickDialogButtonBoxAttachedPrivate::get(attached)->standardButton;
515 else
516 return QPlatformDialogHelper::NoButton;
517}
518
519QQuickDialogButtonBox::QQuickDialogButtonBox(QQuickItem *parent)
520 : QQuickContainer(*(new QQuickDialogButtonBoxPrivate), parent)
521{
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);
527}
528
529QQuickDialogButtonBox::~QQuickDialogButtonBox()
530{
531 Q_D(QQuickDialogButtonBox);
532 // QQuickContainerPrivate does call this, but as our type information has already been
533 // destroyed by that point (since this destructor has already run), it won't call our
534 // implementation. So, we need to make sure our implementation is called. If we don't do this,
535 // the listener we installed on the contentItem won't get removed, possibly resulting in
536 // heap-use-after-frees.
537 contentItemChange(nullptr, d->contentItem);
538}
539
540/*!
541 \qmlproperty enumeration QtQuick.Controls::DialogButtonBox::position
542
543 This property holds the position of the button box.
544
545 \note If the button box is assigned as a header or footer of ApplicationWindow
546 or Page, the appropriate position is set automatically.
547
548 Possible values:
549 \value DialogButtonBox.Header The button box is at the top, as a window or page header.
550 \value DialogButtonBox.Footer The button box is at the bottom, as a window or page footer.
551
552 The default value is \c Footer.
553
554 \sa Dialog::header, Dialog::footer
555*/
556QQuickDialogButtonBox::Position QQuickDialogButtonBox::position() const
557{
558 Q_D(const QQuickDialogButtonBox);
559 return d->position;
560}
561
562void QQuickDialogButtonBox::setPosition(Position position)
563{
564 Q_D(QQuickDialogButtonBox);
565 if (d->position == position)
566 return;
567
568 d->position = position;
569 emit positionChanged();
570}
571
572/*!
573 \qmlproperty flags QtQuick.Controls::DialogButtonBox::alignment
574
575 This property holds the alignment of the buttons.
576
577 Possible values:
578 \value Qt.AlignLeft The buttons are aligned to the left.
579 \value Qt.AlignHCenter The buttons are horizontally centered.
580 \value Qt.AlignRight The buttons are aligned to the right.
581 \value Qt.AlignTop The buttons are aligned to the top.
582 \value Qt.AlignVCenter The buttons are vertically centered.
583 \value Qt.AlignBottom The buttons are aligned to the bottom.
584
585 By default, no specific alignment is set; reading the alignment property yields
586 a default flag value which compares equal to 0. The property can be reset to this
587 value by assigning \c{undefined} to it. In this case, the buttons are resized to
588 fill the available space.
589
590 \note This property assumes a horizontal layout of the buttons.
591 Note that when running the \l {iOS Style}{iOS style}, the DialogButtonBox will use
592 a vertical layout if this property is set to anything other than \c undefined and
593 there are more than two buttons.
594 In all other cases, the buttons will be arranged horizontally.
595*/
596Qt::Alignment QQuickDialogButtonBox::alignment() const
597{
598 Q_D(const QQuickDialogButtonBox);
599 return d->alignment;
600}
601
602void QQuickDialogButtonBox::setAlignment(Qt::Alignment alignment)
603{
604 Q_D(QQuickDialogButtonBox);
605 if (d->alignment == alignment)
606 return;
607
608 d->alignment = alignment;
609 if (isComponentComplete()) {
610 d->resizeContent();
611 polish();
612 }
613 emit alignmentChanged();
614}
615
616void QQuickDialogButtonBox::resetAlignment()
617{
618 setAlignment({});
619}
620
621/*!
622 \qmlproperty enumeration QtQuick.Controls::DialogButtonBox::standardButtons
623
624 This property holds a combination of standard buttons that are used by the button box.
625
626 \snippet qtquickcontrols-dialogbuttonbox.qml 1
627
628 The buttons will be positioned in the appropriate order for the user's platform.
629
630 Possible flags:
631 \value DialogButtonBox.Ok An "OK" button defined with the \c AcceptRole.
632 \value DialogButtonBox.Open An "Open" button defined with the \c AcceptRole.
633 \value DialogButtonBox.Save A "Save" button defined with the \c AcceptRole.
634 \value DialogButtonBox.Cancel A "Cancel" button defined with the \c RejectRole.
635 \value DialogButtonBox.Close A "Close" button defined with the \c RejectRole.
636 \value DialogButtonBox.Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the \c DestructiveRole.
637 \value DialogButtonBox.Apply An "Apply" button defined with the \c ApplyRole.
638 \value DialogButtonBox.Reset A "Reset" button defined with the \c ResetRole.
639 \value DialogButtonBox.RestoreDefaults A "Restore Defaults" button defined with the \c ResetRole.
640 \value DialogButtonBox.Help A "Help" button defined with the \c HelpRole.
641 \value DialogButtonBox.SaveAll A "Save All" button defined with the \c AcceptRole.
642 \value DialogButtonBox.Yes A "Yes" button defined with the \c YesRole.
643 \value DialogButtonBox.YesToAll A "Yes to All" button defined with the \c YesRole.
644 \value DialogButtonBox.No A "No" button defined with the \c NoRole.
645 \value DialogButtonBox.NoToAll A "No to All" button defined with the \c NoRole.
646 \value DialogButtonBox.Abort An "Abort" button defined with the \c RejectRole.
647 \value DialogButtonBox.Retry A "Retry" button defined with the \c AcceptRole.
648 \value DialogButtonBox.Ignore An "Ignore" button defined with the \c AcceptRole.
649 \value DialogButtonBox.NoButton An invalid button.
650
651 \sa standardButton()
652*/
653QPlatformDialogHelper::StandardButtons QQuickDialogButtonBox::standardButtons() const
654{
655 Q_D(const QQuickDialogButtonBox);
656 return d->standardButtons;
657}
658
659void QQuickDialogButtonBox::setStandardButtons(QPlatformDialogHelper::StandardButtons buttons)
660{
661 Q_D(QQuickDialogButtonBox);
662 if (d->standardButtons == buttons)
663 return;
664
665 d->removeStandardButtons();
666
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);
671 if (button)
672 addItem(button);
673 }
674 }
675
676 if (isComponentComplete())
677 polish();
678
679 d->standardButtons = buttons;
680 emit standardButtonsChanged();
681}
682
683/*!
684 \qmlmethod AbstractButton QtQuick.Controls::DialogButtonBox::standardButton(StandardButton button)
685
686 Returns the specified standard \a button, or \c null if it does not exist.
687
688 \sa standardButtons
689*/
690QQuickAbstractButton *QQuickDialogButtonBox::standardButton(QPlatformDialogHelper::StandardButton button) const
691{
692 Q_D(const QQuickDialogButtonBox);
693 if (Q_UNLIKELY(!(d->standardButtons & button)))
694 return nullptr;
695 for (int i = 0, n = count(); i < n; ++i) {
696 QQuickAbstractButton *btn = qobject_cast<QQuickAbstractButton *>(d->itemAt(i));
697 if (Q_LIKELY(btn)) {
698 QQuickDialogButtonBoxAttached *attached = qobject_cast<QQuickDialogButtonBoxAttached *>(qmlAttachedPropertiesObject<QQuickDialogButtonBox>(btn, false));
699 if (attached && QQuickDialogButtonBoxAttachedPrivate::get(attached)->standardButton == button)
700 return btn;
701 }
702 }
703 return nullptr;
704}
705
706
707/*!
708 \qmlproperty enumeration QtQuick.Controls::DialogButtonBox::defaultStandardButton
709 \since 6.11
710
711 This property holds the default standard button.
712
713 The default standard button will be
714 \l {QtQuick.Controls::Button::highlighted}{highlighted} and receive focus.
715
716 Only one button in the button box can be made default.
717 This property cannot be used together with the
718 \l {DialogButtonBox::defaultButton} {defaultButton} property.
719
720 \sa standardButtons, defaultButton
721*/
722QPlatformDialogHelper::StandardButton QQuickDialogButtonBox::defaultStandardButton() const
723{
724 Q_D(const QQuickDialogButtonBox);
725 return d->defaultStandardButton;
726}
727
728void QQuickDialogButtonBox::setDefaultStandardButton(QPlatformDialogHelper::StandardButton button)
729{
730 Q_D(QQuickDialogButtonBox);
731 if (d->defaultStandardButton == button)
732 return;
733
734 d->defaultStandardButton = button;
735
736 if (isComponentComplete())
737 polish();
738
739 emit defaultStandardButtonChanged();
740}
741
742/*!
743 \qmlproperty AbstractButton QtQuick.Controls::DialogButtonBox::defaultButton
744 \since 6.11
745
746 This property holds the default button.
747
748 The default button will be
749 \l {QtQuick.Controls::Button::highlighted}{highlighted} and receive focus.
750
751 Only one button in the button box can be made default.
752 This property cannot be used together with the
753 \l {DialogButtonBox::defaultStandardButton} {defaultStandardButton} property.
754
755 \sa standardButtons, defaultStandardButton
756*/
757
758QQuickAbstractButton *QQuickDialogButtonBox::defaultButton() const
759{
760 Q_D(const QQuickDialogButtonBox);
761 return d->defaultButton;
762}
763
764void QQuickDialogButtonBox::setDefaultButton(QQuickAbstractButton *button)
765{
766 Q_D(QQuickDialogButtonBox);
767 if (d->defaultButton == button)
768 return;
769
770 if (d->defaultButton)
771 removeItem(d->defaultButton);
772
773 d->defaultButton = button;
774
775 if (d->defaultButton)
776 addItem(d->defaultButton);
777
778 if (isComponentComplete())
779 polish();
780
781 emit defaultButtonChanged();
782}
783
784/*!
785 \qmlproperty Component QtQuick.Controls::DialogButtonBox::delegate
786
787 This property holds a delegate for creating standard buttons.
788
789 \include delegate-ownership.qdocinc {no-ownership} {DialogButtonBox}
790
791 \sa standardButtons
792*/
793QQmlComponent *QQuickDialogButtonBox::delegate() const
794{
795 Q_D(const QQuickDialogButtonBox);
796 return d->delegate;
797}
798
799void QQuickDialogButtonBox::setDelegate(QQmlComponent* delegate)
800{
801 Q_D(QQuickDialogButtonBox);
802 if (d->delegate == delegate)
803 return;
804
805 d->delegate = delegate;
806 emit delegateChanged();
807}
808
809QQuickDialogButtonBoxAttached *QQuickDialogButtonBox::qmlAttachedProperties(QObject *object)
810{
811 return new QQuickDialogButtonBoxAttached(object);
812}
813
814/*!
815 \since QtQuick.Controls 2.5 (Qt 5.12)
816 \qmlproperty enumeration QtQuick.Controls::DialogButtonBox::buttonLayout
817
818 This property holds the button layout policy to be used when arranging the buttons contained in the button box.
819 The default value is platform-specific.
820
821 Available values:
822 \value DialogButtonBox.WinLayout Use a policy appropriate for applications on Windows.
823 \value DialogButtonBox.MacLayout Use a policy appropriate for applications on macOS.
824 \value DialogButtonBox.KdeLayout Use a policy appropriate for applications on KDE.
825 \value DialogButtonBox.GnomeLayout Use a policy appropriate for applications on GNOME.
826 \value DialogButtonBox.AndroidLayout Use a policy appropriate for applications on Android.
827*/
828QPlatformDialogHelper::ButtonLayout QQuickDialogButtonBox::buttonLayout() const
829{
830 Q_D(const QQuickDialogButtonBox);
831 return d->buttonLayout;
832}
833
834void QQuickDialogButtonBox::setButtonLayout(QPlatformDialogHelper::ButtonLayout layout)
835{
836 Q_D(QQuickDialogButtonBox);
837 if (d->buttonLayout == layout)
838 return;
839
840 d->buttonLayout = layout;
841 if (isComponentComplete())
842 d->updateLayout();
843 emit buttonLayoutChanged();
844}
845
846void QQuickDialogButtonBox::resetButtonLayout()
847{
848 setButtonLayout(platformButtonLayout());
849}
850
851void QQuickDialogButtonBox::updatePolish()
852{
853 Q_D(QQuickDialogButtonBox);
854 QQuickContainer::updatePolish();
855 d->updateLayout();
856 d->updateFocus();
857}
858
859bool QQuickDialogButtonBox::event(QEvent *e)
860{
861 Q_D(QQuickDialogButtonBox);
862 if (e->type() == QEvent::LanguageChange)
863 d->updateLanguage();
864 return QQuickContainer::event(e);
865}
866
867void QQuickDialogButtonBox::componentComplete()
868{
869 Q_D(QQuickDialogButtonBox);
870 QQuickContainer::componentComplete();
871 d->updateLayout();
872 d->updateFocus();
873}
874
875void QQuickDialogButtonBox::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
876{
877 Q_D(QQuickDialogButtonBox);
878 QQuickContainer::geometryChange(newGeometry, oldGeometry);
879 d->updateLayout();
880}
881
882void QQuickDialogButtonBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
883{
884 Q_D(QQuickDialogButtonBox);
885 QQuickContainer::contentItemChange(newItem, oldItem);
886 if (oldItem)
887 QQuickItemPrivate::get(oldItem)->removeItemChangeListener(d, QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight);
888 if (newItem)
889 QQuickItemPrivate::get(newItem)->addItemChangeListener(d, QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight);
890}
891
892bool QQuickDialogButtonBox::isContent(QQuickItem *item) const
893{
894 return qobject_cast<QQuickAbstractButton *>(item);
895}
896
897void QQuickDialogButtonBox::itemAdded(int index, QQuickItem *item)
898{
899 Q_D(QQuickDialogButtonBox);
900 Q_UNUSED(index);
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())
907 polish();
908}
909
910void QQuickDialogButtonBox::itemRemoved(int index, QQuickItem *item)
911{
912 Q_D(QQuickDialogButtonBox);
913 Q_UNUSED(index);
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())
920 polish();
921}
922
923void QQuickDialogButtonBox::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
924{
925 QQuickContainer::itemChange(change, data);
926 if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue)
927 return;
928
929 Q_D(QQuickDialogButtonBox);
930 d->updateFocus();
931}
932
933#if QT_CONFIG(accessibility)
934QAccessible::Role QQuickDialogButtonBox::accessibleRole() const
935{
936 return QAccessible::Grouping;
937}
938#endif
939
940void QQuickDialogButtonBoxAttachedPrivate::setButtonBox(QQuickDialogButtonBox *box)
941{
942 Q_Q(QQuickDialogButtonBoxAttached);
943 if (buttonBox == box)
944 return;
945
946 buttonBox = box;
947 emit q->buttonBoxChanged();
948}
949
950QQuickDialogButtonBoxAttached::QQuickDialogButtonBoxAttached(QObject *parent)
951 : QObject(*(new QQuickDialogButtonBoxAttachedPrivate), parent)
952{
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();
958 }
959}
960
961/*!
962 \qmlattachedproperty DialogButtonBox QtQuick.Controls::DialogButtonBox::buttonBox
963 \readonly
964
965 This attached property holds the button box that manages this button, or
966 \c null if the button is not in a button box.
967*/
968QQuickDialogButtonBox *QQuickDialogButtonBoxAttached::buttonBox() const
969{
970 Q_D(const QQuickDialogButtonBoxAttached);
971 return d->buttonBox;
972}
973
974/*!
975 \qmlattachedproperty enumeration QtQuick.Controls::DialogButtonBox::buttonRole
976
977 This attached property holds the role of each button in a button box.
978
979 \snippet qtquickcontrols-dialogbuttonbox-attached.qml 1
980
981 Available values:
982 \value DialogButtonBox.InvalidRole The button is invalid.
983 \value DialogButtonBox.AcceptRole Clicking the button causes the dialog to be accepted (e.g. \uicontrol OK).
984 \value DialogButtonBox.RejectRole Clicking the button causes the dialog to be rejected (e.g. \uicontrol Cancel).
985 \value DialogButtonBox.DestructiveRole Clicking the button causes a destructive change (e.g. for discarding changes) and closes the dialog.
986 \value DialogButtonBox.ActionRole Clicking the button causes changes to the elements within the dialog.
987 \value DialogButtonBox.HelpRole The button can be clicked to request help.
988 \value DialogButtonBox.YesRole The button is a "Yes"-like button.
989 \value DialogButtonBox.NoRole The button is a "No"-like button.
990 \value DialogButtonBox.ResetRole The button resets the dialog's fields to default values.
991 \value DialogButtonBox.ApplyRole The button applies current changes.
992*/
993QPlatformDialogHelper::ButtonRole QQuickDialogButtonBoxAttached::buttonRole() const
994{
995 Q_D(const QQuickDialogButtonBoxAttached);
996 return d->buttonRole;
997}
998
999void QQuickDialogButtonBoxAttached::setButtonRole(QPlatformDialogHelper::ButtonRole role)
1000{
1001 Q_D(QQuickDialogButtonBoxAttached);
1002 if (d->buttonRole == role)
1003 return;
1004
1005 d->buttonRole = role;
1006 emit buttonRoleChanged();
1007}
1008
1009QT_END_NAMESPACE
1010
1011#include "moc_qquickdialogbuttonbox_p.cpp"
static QRectF alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSizeF &size, const QRectF &rectangle)