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
qquickabstractbutton.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
13#include "qquickicon_p_p.h"
17
18#include <QtCore/qloggingcategory.h>
19#include <QtGui/qstylehints.h>
20#include <QtGui/qguiapplication.h>
21#include <QtGui/qtextdocument.h>
22#if QT_CONFIG(shortcut)
23# include <QtGui/private/qshortcutmap_p.h>
24#endif
25#if QT_CONFIG(accessibility)
26#include <QtGui/private/qaccessiblehelper_p.h>
27#endif
28#include <QtGui/private/qguiapplication_p.h>
29#include <QtGui/qpa/qplatformtheme.h>
30#include <QtQuick/private/qquickevents_p_p.h>
31#include <QtQuick/private/qquickevents_p_p.h>
32#include <QtQuick/private/qquickaccessibleattached_p.h>
33#include <QtQml/qqmllist.h>
34
36
37Q_STATIC_LOGGING_CATEGORY(lcButtonAction, "qt.quick.controls.abstractbutton.action")
38#if !QT_CONFIG(shortcut)
39Q_STATIC_LOGGING_CATEGORY(lcButtonMnemonics, "qt.quick.controls.abstractbutton.mnemonics")
40#endif
41
42/*!
43 \qmltype AbstractButton
44 \inherits Control
45//! \nativetype QQuickAbstractButton
46 \inqmlmodule QtQuick.Controls
47 \since 5.7
48 \ingroup qtquickcontrols-buttons
49 \brief Abstract base type providing functionality common to buttons.
50
51 AbstractButton provides the interface for controls with button-like
52 behavior; for example, push buttons and checkable controls like
53 radio buttons and check boxes. As an abstract control, it has no delegate
54 implementations, leaving them to the types that derive from it.
55
56 \sa ButtonGroup, {Button Controls}
57*/
58
59/*!
60 \qmlsignal QtQuick.Controls::AbstractButton::pressed()
61
62 This signal is emitted when the button is interactively pressed by the user via touch, mouse, or keyboard.
63*/
64
65/*!
66 \qmlsignal QtQuick.Controls::AbstractButton::released()
67
68 This signal is emitted when the button is interactively released by the user via touch, mouse, or keyboard.
69*/
70
71/*!
72 \qmlsignal QtQuick.Controls::AbstractButton::canceled()
73
74 This signal is emitted when the button loses mouse grab
75 while being pressed, or when it would emit the \l released
76 signal but the mouse cursor is not inside the button.
77*/
78
79/*!
80 \qmlsignal QtQuick.Controls::AbstractButton::clicked()
81
82 This signal is emitted when the button is interactively clicked by the user via touch, mouse, or keyboard.
83
84 \sa click(), animateClick(), {Call a C++ function from QML when a Button is clicked}
85*/
86
87/*!
88 \since QtQuick.Controls 2.2 (Qt 5.9)
89 \qmlsignal QtQuick.Controls::AbstractButton::toggled()
90
91 This signal is emitted when a checkable button is interactively toggled by the user via touch, mouse, or keyboard.
92*/
93
94/*!
95 \qmlsignal QtQuick.Controls::AbstractButton::pressAndHold()
96
97 This signal is emitted when the button is interactively pressed and held down by the user via touch or mouse.
98 It is not emitted when \l autoRepeat is enabled.
99*/
100
101/*!
102 \qmlsignal QtQuick.Controls::AbstractButton::doubleClicked()
103
104 This signal is emitted when the button is interactively double clicked by the user via touch or mouse.
105*/
106
107void QQuickAbstractButtonPrivate::init()
108{
109 Q_Q(QQuickAbstractButton);
110 q->setActiveFocusOnTab(true);
111#ifdef Q_OS_MACOS
112 q->setFocusPolicy(Qt::TabFocus);
113#else
114 q->setFocusPolicy(Qt::StrongFocus);
115#endif
116 q->setAcceptedMouseButtons(Qt::LeftButton);
117#if QT_CONFIG(quicktemplates2_multitouch)
118 q->setAcceptTouchEvents(true);
119#endif
120#if QT_CONFIG(cursor)
121 q->setCursor(Qt::ArrowCursor);
122#endif
123 setSizePolicy(QLayoutPolicy::Preferred, QLayoutPolicy::Fixed);
124 mnemonicEnabled = QGuiApplicationPrivate::platformTheme()->themeHint(
125 QPlatformTheme::MnemonicsEnabled).toBool();
126}
127
128QPointF QQuickAbstractButtonPrivate::centerPressPoint() const
129{
130 return QPointF(qRound(width / 2), qRound(height / 2));
131}
132
133void QQuickAbstractButtonPrivate::setPressPoint(const QPointF &point)
134{
135 pressPoint = point;
136 setMovePoint(point);
137}
138
139void QQuickAbstractButtonPrivate::setMovePoint(const QPointF &point)
140{
141 Q_Q(QQuickAbstractButton);
142 bool xChange = !qFuzzyCompare(point.x(), movePoint.x());
143 bool yChange = !qFuzzyCompare(point.y(), movePoint.y());
144 movePoint = point;
145 if (xChange)
146 emit q->pressXChanged();
147 if (yChange)
148 emit q->pressYChanged();
149}
150
151bool QQuickAbstractButtonPrivate::handlePress(const QPointF &point, ulong timestamp)
152{
153 Q_Q(QQuickAbstractButton);
154 if (pressed)
155 return true;
156 QQuickControlPrivate::handlePress(point, timestamp);
157 setPressPoint(point);
158 q->setPressed(true);
159
160 emit q->pressed();
161
162 if (autoRepeat)
163 startRepeatDelay();
164 else if (touchId != -1 || Qt::LeftButton == (pressButtons & Qt::LeftButton))
165 startPressAndHold();
166 else
167 stopPressAndHold();
168
169 // Tool tips on desktop should hide when their parent item is pressed.
170 if (touchId == -1)
171 QQuickToolTipAttachedPrivate::maybeSetVisibleImplicitly(q, false);
172
173 return true;
174}
175
176bool QQuickAbstractButtonPrivate::handleMove(const QPointF &point, ulong timestamp)
177{
178 Q_Q(QQuickAbstractButton);
179 QQuickControlPrivate::handleMove(point, timestamp);
180 setMovePoint(point);
181 q->setPressed(keepPressed || q->contains(point));
182
183 if (!pressed && autoRepeat)
184 stopPressRepeat();
185 else if (holdTimer > 0 && (!pressed || QLineF(pressPoint, point).length() > QGuiApplication::styleHints()->startDragDistance()))
186 stopPressAndHold();
187 return true;
188}
189
190bool QQuickAbstractButtonPrivate::handleRelease(const QPointF &point, ulong timestamp)
191{
192 Q_Q(QQuickAbstractButton);
193 // Store this here since the base class' handleRelease clears it.
194 const int pressTouchId = touchId;
195
196 QQuickControlPrivate::handleRelease(point, timestamp);
197 bool wasPressed = pressed;
198 setPressPoint(point);
199 q->setPressed(false);
200 pressButtons = Qt::NoButton;
201
202 const bool touchDoubleClick = pressTouchId != -1 && lastTouchReleaseTimestamp != 0
203 && QQuickDeliveryAgentPrivate::isWithinDoubleClickInterval(timestamp - lastTouchReleaseTimestamp)
204 && isDoubleClickConnected();
205
206 if (!wasHeld && wasPressed && (keepPressed || q->contains(point)))
207 q->nextCheckState();
208
209 if (wasPressed) {
210 if (wasHeld && pressTouchId != -1) {
211 // We were potentially showing a tool tip on long press; if we were, hide it now that
212 // we're no longer pressed.
213 QQuickToolTipAttachedPrivate::maybeSetVisibleImplicitly(q, false);
214 }
215
216 emit q->released();
217 if (!wasHeld && !wasDoubleClick)
218 trigger(touchDoubleClick);
219 } else {
220 emit q->canceled();
221 }
222
223 if (autoRepeat)
224 stopPressRepeat();
225 else
226 stopPressAndHold();
227
228 if (!touchDoubleClick) {
229 // This is not a double click yet, but it is potentially the
230 // first release before a double click.
231 if (pressTouchId != -1) {
232 // The corresponding press for this release was a touch press.
233 // Keep track of the timestamp of the release so that we can
234 // emit doubleClicked() if another one comes afterwards.
235 lastTouchReleaseTimestamp = timestamp;
236 }
237 } else {
238 // We just did a double click, so clear the release timestamp
239 // to prepare for any possible future double clicks.
240 lastTouchReleaseTimestamp = 0;
241 }
242
243 wasDoubleClick = false;
244 return true;
245}
246
247void QQuickAbstractButtonPrivate::handleUngrab()
248{
249 Q_Q(QQuickAbstractButton);
250 QQuickControlPrivate::handleUngrab();
251 pressButtons = Qt::NoButton;
252 if (!pressed)
253 return;
254
255 q->setPressed(false);
256 stopPressRepeat();
257 stopPressAndHold();
258 wasDoubleClick = false;
259 lastTouchReleaseTimestamp = 0;
260 emit q->canceled();
261}
262
263bool QQuickAbstractButtonPrivate::acceptKeyClick(Qt::Key key) const
264{
265 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
266 return buttonPressKeys.contains(key);
267}
268
269bool QQuickAbstractButtonPrivate::isPressAndHoldConnected()
270{
271 Q_Q(QQuickAbstractButton);
272 static const QMetaMethod method = [&]() {
273 const auto signal = &QQuickAbstractButton::pressAndHold;
274 return QMetaMethod::fromSignal(signal);
275 }();
276 return q->isSignalConnected(method);
277}
278
279bool QQuickAbstractButtonPrivate::isDoubleClickConnected()
280{
281 Q_Q(QQuickAbstractButton);
282 static const QMetaMethod method = [&]() {
283 const auto signal = &QQuickAbstractButton::doubleClicked;
284 return QMetaMethod::fromSignal(signal);
285 }();
286 return q->isSignalConnected(method);
287}
288
289void QQuickAbstractButtonPrivate::startPressAndHold()
290{
291 Q_Q(QQuickAbstractButton);
292 wasHeld = false;
293 stopPressAndHold();
294 if (isPressAndHoldConnected())
295 holdTimer = q->startTimer(QGuiApplication::styleHints()->mousePressAndHoldInterval());
296}
297
298void QQuickAbstractButtonPrivate::stopPressAndHold()
299{
300 Q_Q(QQuickAbstractButton);
301 if (holdTimer > 0) {
302 q->killTimer(holdTimer);
303 holdTimer = 0;
304 }
305}
306
307void QQuickAbstractButtonPrivate::startRepeatDelay()
308{
309 Q_Q(QQuickAbstractButton);
310 stopPressRepeat();
311 delayTimer = q->startTimer(repeatDelay);
312}
313
314void QQuickAbstractButtonPrivate::startPressRepeat()
315{
316 Q_Q(QQuickAbstractButton);
317 stopPressRepeat();
318 repeatTimer = q->startTimer(repeatInterval);
319}
320
321void QQuickAbstractButtonPrivate::stopPressRepeat()
322{
323 Q_Q(QQuickAbstractButton);
324 if (delayTimer > 0) {
325 q->killTimer(delayTimer);
326 delayTimer = 0;
327 }
328 if (repeatTimer > 0) {
329 q->killTimer(repeatTimer);
330 repeatTimer = 0;
331 }
332}
333
334#if QT_CONFIG(shortcut)
335void QQuickAbstractButtonPrivate::grabShortcut()
336{
337 Q_Q(QQuickAbstractButton);
338 if (shortcut.isEmpty())
339 return;
340
341 shortcutId = QGuiApplicationPrivate::instance()->shortcutMap.addShortcut(q, shortcut, Qt::WindowShortcut, QQuickShortcutContext::matcher);
342
343 if (!q->isEnabled())
344 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(false, shortcutId, q);
345}
346
347void QQuickAbstractButtonPrivate::ungrabShortcut()
348{
349 Q_Q(QQuickAbstractButton);
350 if (!shortcutId)
351 return;
352
353 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(shortcutId, q);
354 shortcutId = 0;
355}
356#endif
357
358void QQuickAbstractButtonPrivate::actionTextChange()
359{
360 Q_Q(QQuickAbstractButton);
361 if (explicitText)
362 return;
363
364 q->buttonChange(QQuickAbstractButton::ButtonTextChange);
365}
366
367bool QQuickAbstractButtonPrivate::needsTextFormatWarning() const
368{
369 Q_Q(const QQuickAbstractButton);
370 return !textFormat && !textFormatWarningDisplayed && Qt::mightBeRichText(q->text());
371}
372
373void QQuickAbstractButtonPrivate::setText(const QString &newText, QQml::PropertyUtils::State propertyState)
374{
375 Q_Q(QQuickAbstractButton);
376 const QString oldText = q->text();
377 explicitText = isExplicitlySet(propertyState);
378 text = newText;
379 if (oldText == q->text())
380 return;
381
382 q->buttonChange(QQuickAbstractButton::ButtonTextChange);
383}
384
385/*!
386 \internal
387
388 Whether the \l text supports \l {QShortcut#mnemonic}{mnemonics} keys. An
389 example of a mnemonic is "E&xit": a button with this text can be clicked
390 by the keyboard shortcut \c Alt+X, and the \c x will be underlined.
391
392 If \c true, the mnemonic indicator (\c &) is always removed from the
393 displayed text. Depending on the current platform's conventions, this may
394 also enable the corresponding keyboard shortcut and underline the mnemonic.
395
396 If \c false, the mnemonic indicator (\c &) will not be removed from
397 the text and the shortcut will be disabled. This allows \c & to
398 be used literally, as in \c {"Cats & Dogs"}.
399
400 This isn't exposed to QML: whether mnemonics are supported is determined
401 by the platform (QPlatformTheme::MnemonicsEnabled) by default, and forced
402 to \c false by certain derived types (such as \l ItemDelegate and
403 \l DelayButton) for which mnemonics don't make sense. It isn't something
404 app code should be able to toggle per-instance.
405
406 When Qt is \l {Qt Configure Options}{configured} with
407 \c -no-feature-shortcut, shortcuts are disabled, and this has no effect.
408*/
409void QQuickAbstractButtonPrivate::setMnemonicEnabled(bool enabled)
410{
411 if (mnemonicEnabled == enabled)
412 return;
413
414 mnemonicEnabled = enabled;
415#if QT_CONFIG(shortcut)
416 Q_Q(QQuickAbstractButton);
417 q->setShortcut(mnemonicEnabled ? QKeySequence::mnemonic(text) : QKeySequence());
418#else
419 qCDebug(lcButtonMnemonics) << "The mnemonicEnabled property has no effect when Qt is configured"
420 << "with -no-feature-shortcut";
421#endif
422}
423
424void QQuickAbstractButtonPrivate::updateEffectiveIcon()
425{
426 Q_Q(QQuickAbstractButton);
427 // We store effectiveIcon because we need to be able to tell if the icon has actually changed.
428 // If we only stored our icon and the action's icon, and resolved in the getter, we'd have
429 // no way of knowing what the old value was here. As an added benefit, we only resolve when
430 // something has changed, as opposed to doing it unconditionally in the icon() getter.
431 QQuickIcon newEffectiveIcon = action ? icon.resolve(action->icon()) : icon;
432
433 bool unchanged = newEffectiveIcon == effectiveIcon;
434 if (action) {
435 // We can't rely purely on QQuickIcon::operator== for our unchanged check, because it
436 // doesn't account for the color being resolved. QQuickIconLabelPrivate::syncImage and
437 // createImage rely on the color's resolve mask to determine if a color was set on it
438 // that should override the style default (see QQuickIconLabel::defaultIconColor for
439 // more info). If we didn't check the resolve mask
440 // and the user set the color to transparent (the default), the resolveMask of d->icon
441 // wouldn't indicate that the color was resolved and iconChanged wouldn't be emitted,
442 // leading to the user's request being ignored.
443 const bool actionIconColorResolved = QQuickIconPrivate::isResolved(action->icon(),
444 QQuickIconPrivate::ColorResolved);
445 const bool iconColorResolved = QQuickIconPrivate::isResolved(icon,
446 QQuickIconPrivate::ColorResolved);
447
448 unchanged = newEffectiveIcon == effectiveIcon;
449
450 // Only set it to false if there was a change in icon color that would otherwise
451 // be undetectable.
452 if (unchanged && !iconColorResolved && actionIconColorResolved)
453 unchanged = false;
454
455 /*
456 A color wasn't set on the button's icon (which should always win over an Action's),
457 but one was set on the action's icon. Mark the effective icon's color as inherited
458 so that QQuickIconLabel can detect that it's set and respect it.
459
460 Don't use ColorResolved for this: the effective icon is what icon() returns, so it
461 gets written back to us by setIcon() whenever another icon property is set on the
462 button (e.g. icon.width), and the action's color would then be mistaken for one
463 set on the button (QTBUG-151707).
464 */
465 if (actionIconColorResolved && !iconColorResolved) {
466 QQuickIconPrivate::setResolved(newEffectiveIcon,
467 QQuickIconPrivate::InheritedColorResolved);
468 }
469 }
470
471 // Always update effectiveIcon because the color may have been resolved in icon,
472 // which isn't accounted for QQuickIcon::operator==.
473 effectiveIcon = newEffectiveIcon;
474
475 if (unchanged)
476 return;
477
478 emit q->iconChanged();
479}
480
481void QQuickAbstractButtonPrivate::click()
482{
483 Q_Q(QQuickAbstractButton);
484 // If our Action's trigger signal handler disables us (or the action, which causes us to be
485 // disabled), we still want clicked to be emitted so that e.g. menus close when a menu item is
486 // clicked. That's why we check wasEnabledBeforeTrigger here.
487 if (effectiveEnable || wasEnabledBeforeTrigger)
488 emit q->clicked();
489}
490
491void QQuickAbstractButtonPrivate::accessiblePressAction()
492{
493 Q_Q(QQuickAbstractButton);
494 q->click();
495}
496
497void QQuickAbstractButtonPrivate::accessibleToggleAction()
498{
499 accessiblePressAction();
500}
501
502void QQuickAbstractButtonPrivate::trigger(bool doubleClick)
503{
504 Q_Q(QQuickAbstractButton);
505 const QScopedValueRollback<bool> rollback(wasEnabledBeforeTrigger, effectiveEnable);
506
507 bool actionTriggered = false;
508 if (action && action->isEnabled())
509 actionTriggered = QQuickActionPrivate::get(action)->trigger(q, false);
510
511 // Don't emit the signals below if we were disabled or if our action already emitted its
512 // triggered signal (which we connect to click, which emits clicked, so would emit twice).
513 if (!wasEnabledBeforeTrigger || actionTriggered)
514 return;
515
516 if (!doubleClick)
517 emit q->clicked();
518 else
519 emit q->doubleClicked();
520}
521
522void QQuickAbstractButtonPrivate::toggle(bool value)
523{
524 Q_Q(QQuickAbstractButton);
525 const bool wasChecked = checked;
526 q->setChecked(value);
527 if (wasChecked != checked)
528 emit q->toggled();
529}
530
531void QQuickAbstractButtonPrivate::cancelIndicator()
532{
533 Q_Q(QQuickAbstractButton);
534 quickCancelDeferred(q, indicatorName());
535}
536
537void QQuickAbstractButtonPrivate::executeIndicator(bool complete)
538{
539 Q_Q(QQuickAbstractButton);
540 if (indicator.wasExecuted())
541 return;
542
543 if (!indicator || complete)
544 quickBeginDeferred(q, indicatorName(), indicator);
545 if (complete)
546 quickCompleteDeferred(q, indicatorName(), indicator);
547}
548
549void QQuickAbstractButtonPrivate::itemImplicitWidthChanged(QQuickItem *item)
550{
551 Q_Q(QQuickAbstractButton);
552 QQuickControlPrivate::itemImplicitWidthChanged(item);
553 if (item == indicator)
554 emit q->implicitIndicatorWidthChanged();
555}
556
557void QQuickAbstractButtonPrivate::itemImplicitHeightChanged(QQuickItem *item)
558{
559 Q_Q(QQuickAbstractButton);
560 QQuickControlPrivate::itemImplicitHeightChanged(item);
561 if (item == indicator)
562 emit q->implicitIndicatorHeightChanged();
563}
564
565void QQuickAbstractButtonPrivate::itemDestroyed(QQuickItem *item)
566{
567 Q_Q(QQuickAbstractButton);
568 QQuickControlPrivate::itemDestroyed(item);
569 if (item == indicator) {
570 indicator = nullptr;
571 emit q->implicitIndicatorWidthChanged();
572 emit q->implicitIndicatorHeightChanged();
573 }
574}
575
576QQuickAbstractButton *QQuickAbstractButtonPrivate::findCheckedButton() const
577{
578 Q_Q(const QQuickAbstractButton);
579 if (group)
580 return group->checkedButton();
581
582 const QList<QQuickAbstractButton *> buttons = findExclusiveButtons();
583 // TODO: A singular QRadioButton can be unchecked, which seems logical,
584 // because there's nothing to be exclusive with. However, a RadioButton
585 // from QtQuick.Controls 1.x can never be unchecked, which is the behavior
586 // that QQuickRadioButton adopted. Uncommenting the following count check
587 // gives the QRadioButton behavior. Notice that tst_radiobutton.qml needs
588 // to be updated.
589 if (!autoExclusive /*|| buttons.count() == 1*/)
590 return nullptr;
591
592 for (QQuickAbstractButton *button : buttons) {
593 if (button->isChecked() && button != q)
594 return button;
595 }
596 return checked ? const_cast<QQuickAbstractButton *>(q) : nullptr;
597}
598
599QList<QQuickAbstractButton *> QQuickAbstractButtonPrivate::findExclusiveButtons() const
600{
601 QList<QQuickAbstractButton *> buttons;
602 if (group) {
603 QQmlListProperty<QQuickAbstractButton> groupButtons = group->buttons();
604 int count = groupButtons.count(&groupButtons);
605 for (int i = 0; i < count; ++i) {
606 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(groupButtons.at(&groupButtons, i));
607 if (button)
608 buttons += button;
609 }
610 } else if (parentItem) {
611 const auto childItems = parentItem->childItems();
612 for (QQuickItem *child : childItems) {
613 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(child);
614 if (button && button->autoExclusive() && !QQuickAbstractButtonPrivate::get(button)->group)
615 buttons += button;
616 }
617 }
618 return buttons;
619}
620
621QQuickAbstractButton::QQuickAbstractButton(QQuickItem *parent)
622 : QQuickControl(*(new QQuickAbstractButtonPrivate), parent)
623{
624 Q_D(QQuickAbstractButton);
625 d->init();
626}
627
628QQuickAbstractButton::QQuickAbstractButton(QQuickAbstractButtonPrivate &dd, QQuickItem *parent)
629 : QQuickControl(dd, parent)
630{
631 Q_D(QQuickAbstractButton);
632 d->init();
633}
634
635QQuickAbstractButton::~QQuickAbstractButton()
636{
637 Q_D(QQuickAbstractButton);
638 d->removeImplicitSizeListener(d->indicator);
639 if (d->group) {
640 auto *attached = qobject_cast<QQuickButtonGroupAttached *>(
641 qmlAttachedPropertiesObject<QQuickButtonGroup>(this, false));
642 if (attached)
643 attached->setGroup(nullptr);
644 else
645 d->group->removeButton(this);
646 }
647#if QT_CONFIG(shortcut)
648 d->ungrabShortcut();
649#endif
650}
651
652/*!
653 \qmlproperty string QtQuick.Controls::AbstractButton::text
654
655 This property holds a textual description of the button.
656
657 \note The text is used for accessibility purposes, so it makes sense to
658 set a textual description even if the content item is an image.
659
660 \sa icon, display, {Control::contentItem}{contentItem}
661*/
662QString QQuickAbstractButton::text() const
663{
664 Q_D(const QQuickAbstractButton);
665 return d->explicitText || !d->action ? d->text : d->action->text();
666}
667
668void QQuickAbstractButton::setText(const QString &text)
669{
670 Q_D(QQuickAbstractButton);
671 d->setText(text, QQml::PropertyUtils::State::ExplicitlySet);
672}
673
674void QQuickAbstractButton::resetText()
675{
676 Q_D(QQuickAbstractButton);
677 d->setText(QString(), QQml::PropertyUtils::State::ImplicitlySet);
678}
679
680/*!
681 \qmlproperty bool QtQuick.Controls::AbstractButton::down
682
683 This property holds whether the button is visually down.
684
685 Unless explicitly set, this property follows the value of \l pressed. To
686 return to the default value, set this property to \c undefined.
687
688 \sa pressed
689*/
690bool QQuickAbstractButton::isDown() const
691{
692 Q_D(const QQuickAbstractButton);
693 return d->down;
694}
695
696void QQuickAbstractButton::setDown(bool down)
697{
698 Q_D(QQuickAbstractButton);
699 d->explicitDown = true;
700
701 if (d->down == down)
702 return;
703
704 d->down = down;
705 emit downChanged();
706}
707
708void QQuickAbstractButton::resetDown()
709{
710 Q_D(QQuickAbstractButton);
711 if (!d->explicitDown)
712 return;
713
714 setDown(d->pressed);
715 d->explicitDown = false;
716}
717
718/*!
719 \qmlproperty bool QtQuick.Controls::AbstractButton::pressed
720 \readonly
721
722 This property holds whether the button is physically pressed. A button can
723 be pressed by either touch or key events.
724
725 \sa down
726*/
727bool QQuickAbstractButton::isPressed() const
728{
729 Q_D(const QQuickAbstractButton);
730 return d->pressed;
731}
732
733void QQuickAbstractButton::setPressed(bool isPressed)
734{
735 Q_D(QQuickAbstractButton);
736 if (d->pressed == isPressed)
737 return;
738
739 d->pressed = isPressed;
740 setAccessibleProperty("pressed", isPressed);
741 emit pressedChanged();
742 buttonChange(ButtonPressedChanged);
743
744 if (!d->explicitDown) {
745 setDown(d->pressed);
746 d->explicitDown = false;
747 }
748}
749
750/*!
751 \qmlproperty bool QtQuick.Controls::AbstractButton::checked
752
753 This property holds whether the button is checked.
754
755 Since Qt 6.2, setting this property no longer affects the
756 \l {AbstractButton::}{checkable} property. Explicitly set the
757 \c checkable property if needed.
758
759 \sa checkable
760*/
761bool QQuickAbstractButton::isChecked() const
762{
763 Q_D(const QQuickAbstractButton);
764 return d->checked;
765}
766
767void QQuickAbstractButton::setChecked(bool checked)
768{
769 Q_D(QQuickAbstractButton);
770 if (d->checked == checked)
771 return;
772
773 d->checked = checked;
774 if (d->action)
775 d->action->setChecked(checked);
776 setAccessibleProperty("checked", checked);
777 buttonChange(ButtonCheckedChange);
778 emit checkedChanged();
779}
780
781/*!
782 \qmlproperty bool QtQuick.Controls::AbstractButton::checkable
783
784 This property holds whether the button is checkable.
785
786 A checkable button toggles between checked (on) and unchecked (off) when
787 the user clicks on it or presses the space bar while the button has active
788 focus.
789
790 The default value is \c false.
791
792 \sa checked
793*/
794bool QQuickAbstractButton::isCheckable() const
795{
796 Q_D(const QQuickAbstractButton);
797 return d->checkable;
798}
799
800void QQuickAbstractButton::setCheckable(bool checkable)
801{
802 Q_D(QQuickAbstractButton);
803 if (d->checkable == checkable)
804 return;
805
806 d->checkable = checkable;
807 if (d->action)
808 d->action->setCheckable(checkable);
809 setAccessibleProperty("checkable", checkable);
810 buttonChange(ButtonCheckableChange);
811 emit checkableChanged();
812}
813
814/*!
815 \qmlproperty bool QtQuick.Controls::AbstractButton::autoExclusive
816
817 This property holds whether auto-exclusivity is enabled.
818
819 If auto-exclusivity is enabled, checkable buttons that belong to the same
820 parent item behave as if they were part of the same ButtonGroup. Only
821 one button can be checked at any time; checking another button automatically
822 unchecks the previously checked one.
823
824 \note The property has no effect on buttons that belong to a ButtonGroup.
825
826 RadioButton and TabButton are auto-exclusive by default.
827*/
828bool QQuickAbstractButton::autoExclusive() const
829{
830 Q_D(const QQuickAbstractButton);
831 return d->autoExclusive;
832}
833
834void QQuickAbstractButton::setAutoExclusive(bool exclusive)
835{
836 Q_D(QQuickAbstractButton);
837 if (d->autoExclusive == exclusive)
838 return;
839
840 d->autoExclusive = exclusive;
841 emit autoExclusiveChanged();
842}
843
844/*!
845 \qmlproperty bool QtQuick.Controls::AbstractButton::autoRepeat
846
847 This property holds whether the button repeats \l pressed(), \l released()
848 and \l clicked() signals while the button is pressed and held down.
849
850 If this property is set to \c true, the \l pressAndHold() signal will not
851 be emitted.
852
853 The default value is \c false.
854
855 The initial delay and the repetition interval are defined in milliseconds
856 by \l autoRepeatDelay and \l autoRepeatInterval.
857*/
858bool QQuickAbstractButton::autoRepeat() const
859{
860 Q_D(const QQuickAbstractButton);
861 return d->autoRepeat;
862}
863
864void QQuickAbstractButton::setAutoRepeat(bool repeat)
865{
866 Q_D(QQuickAbstractButton);
867 if (d->autoRepeat == repeat)
868 return;
869
870 d->stopPressRepeat();
871 d->autoRepeat = repeat;
872 emit autoRepeatChanged();
873}
874
875/*!
876 \qmlproperty Item QtQuick.Controls::AbstractButton::indicator
877
878 This property holds the indicator item.
879*/
880QQuickItem *QQuickAbstractButton::indicator() const
881{
882 QQuickAbstractButtonPrivate *d = const_cast<QQuickAbstractButtonPrivate *>(d_func());
883 if (!d->indicator)
884 d->executeIndicator();
885 return d->indicator;
886}
887
888void QQuickAbstractButton::setIndicator(QQuickItem *indicator)
889{
890 Q_D(QQuickAbstractButton);
891 if (d->indicator == indicator)
892 return;
893
894 QQuickControlPrivate::warnIfCustomizationNotSupported(this, indicator, QStringLiteral("indicator"));
895
896 if (!d->indicator.isExecuting())
897 d->cancelIndicator();
898
899 const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth();
900 const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight();
901
902 d->removeImplicitSizeListener(d->indicator);
903 QQuickControlPrivate::hideOldItem(d->indicator);
904 d->indicator = indicator;
905
906 if (indicator) {
907 if (!indicator->parentItem())
908 indicator->setParentItem(this);
909 indicator->setAcceptedMouseButtons(Qt::LeftButton);
910 d->addImplicitSizeListener(indicator);
911 }
912
913 if (!qFuzzyCompare(oldImplicitIndicatorWidth, implicitIndicatorWidth()))
914 emit implicitIndicatorWidthChanged();
915 if (!qFuzzyCompare(oldImplicitIndicatorHeight, implicitIndicatorHeight()))
916 emit implicitIndicatorHeightChanged();
917 if (!d->indicator.isExecuting())
918 emit indicatorChanged();
919}
920
921/*!
922 \qmlproperty string QtQuick.Controls::AbstractButton::icon.name
923 \qmlproperty url QtQuick.Controls::AbstractButton::icon.source
924 \qmlproperty int QtQuick.Controls::AbstractButton::icon.width
925 \qmlproperty int QtQuick.Controls::AbstractButton::icon.height
926 \qmlproperty color QtQuick.Controls::AbstractButton::icon.color
927 \qmlproperty bool QtQuick.Controls::AbstractButton::icon.cache
928
929 \since QtQuick.Controls 2.3
930
931 \include qquickicon.qdocinc grouped-properties
932
933 \sa text, display, {Icons in Qt Quick Controls}
934*/
935
936QQuickIcon QQuickAbstractButton::icon() const
937{
938 Q_D(const QQuickAbstractButton);
939 return d->effectiveIcon;
940}
941
942void QQuickAbstractButton::setIcon(const QQuickIcon &icon)
943{
944 Q_D(QQuickAbstractButton);
945 d->icon = icon;
946 // Setting a single icon property (e.g. icon.width) writes back the whole effective icon,
947 // including its InheritedColorResolved mark (see its docs for what an inherited color is)
948 // and the action's current color. Unmark it; otherwise the button would treat the action's
949 // color as its own and stop picking up the action's later changes.
950 QQuickIconPrivate::clearResolved(d->icon, QQuickIconPrivate::InheritedColorResolved);
951 d->icon.ensureRelativeSourceResolved(this);
952 d->updateEffectiveIcon();
953}
954
955/*!
956 \since QtQuick.Controls 2.3 (Qt 5.10)
957 \qmlproperty enumeration QtQuick.Controls::AbstractButton::display
958
959 This property determines how the \l icon and \l text are displayed within
960 the button.
961
962 \table
963 \header \li Display \li Result
964 \row \li \c AbstractButton.IconOnly
965 \li \image qtquickcontrols-button-icononly.png
966 {Button displaying only icon}
967 \row \li \c AbstractButton.TextOnly
968 \li \image qtquickcontrols-button-textonly.png
969 {Button displaying only text}
970 \row \li \c AbstractButton.TextBesideIcon (default)
971 \li \image qtquickcontrols-button-textbesideicon.png
972 {Button with text beside icon}
973 \row \li \c AbstractButton.TextUnderIcon
974 \li \image qtquickcontrols-button-textundericon.png
975 {Button with text under icon}
976 \endtable
977
978 \sa {Control::}{spacing}, {Control::}{padding}
979*/
980QQuickAbstractButton::Display QQuickAbstractButton::display() const
981{
982 Q_D(const QQuickAbstractButton);
983 return d->display;
984}
985
986void QQuickAbstractButton::setDisplay(Display display)
987{
988 Q_D(QQuickAbstractButton);
989 if (display == d->display)
990 return;
991
992 d->display = display;
993 emit displayChanged();
994}
995
996/*!
997 \since QtQuick.Controls 2.3 (Qt 5.10)
998 \qmlproperty Action QtQuick.Controls::AbstractButton::action
999
1000 This property holds the button action.
1001
1002 \sa Action
1003*/
1004QQuickAction *QQuickAbstractButton::action() const
1005{
1006 Q_D(const QQuickAbstractButton);
1007 return d->action;
1008}
1009
1010void QQuickAbstractButton::setAction(QQuickAction *action)
1011{
1012 Q_D(QQuickAbstractButton);
1013 qCDebug(lcButtonAction) << this << "setAction called with" << action;
1014 if (d->action == action)
1015 return;
1016
1017 const QString oldText = text();
1018
1019 if (QQuickAction *oldAction = d->action.data()) {
1020 QQuickActionPrivate::get(oldAction)->unregisterItem(this);
1021 QObjectPrivate::disconnect(oldAction, &QQuickAction::triggered, d, &QQuickAbstractButtonPrivate::click);
1022 QObjectPrivate::disconnect(oldAction, &QQuickAction::textChanged, d, &QQuickAbstractButtonPrivate::actionTextChange);
1023
1024 QObjectPrivate::disconnect(oldAction, &QQuickAction::iconChanged, d, &QQuickAbstractButtonPrivate::updateEffectiveIcon);
1025 disconnect(oldAction, &QQuickAction::checkedChanged, this, &QQuickAbstractButton::setChecked);
1026 disconnect(oldAction, &QQuickAction::checkableChanged, this, &QQuickAbstractButton::setCheckable);
1027 disconnect(oldAction, &QQuickAction::enabledChanged, this, &QQuickItem::setEnabled);
1028 }
1029
1030 if (action) {
1031 QQuickActionPrivate::get(action)->registerItem(this);
1032 QObjectPrivate::connect(action, &QQuickAction::triggered, d, &QQuickAbstractButtonPrivate::click);
1033 QObjectPrivate::connect(action, &QQuickAction::textChanged, d, &QQuickAbstractButtonPrivate::actionTextChange);
1034
1035 QObjectPrivate::connect(action, &QQuickAction::iconChanged, d, &QQuickAbstractButtonPrivate::updateEffectiveIcon);
1036 connect(action, &QQuickAction::checkedChanged, this, &QQuickAbstractButton::setChecked);
1037 connect(action, &QQuickAction::checkableChanged, this, &QQuickAbstractButton::setCheckable);
1038 connect(action, &QQuickAction::enabledChanged, this, &QQuickItem::setEnabled);
1039
1040 setChecked(action->isChecked());
1041 setCheckable(action->isCheckable());
1042 setEnabled(action->isEnabled());
1043 }
1044
1045#if QT_CONFIG(accessibility)
1046 auto attached = qobject_cast<QQuickAccessibleAttached*>(qmlAttachedPropertiesObject<QQuickAccessibleAttached>(this, true));
1047 Q_ASSERT(attached);
1048 attached->setProxying(qobject_cast<QQuickAccessibleAttached*>(qmlAttachedPropertiesObject<QQuickAccessibleAttached>(action, true)));
1049#endif
1050
1051 d->action = action;
1052
1053 if (oldText != text())
1054 buttonChange(ButtonTextChange);
1055
1056 d->updateEffectiveIcon();
1057
1058 emit actionChanged();
1059}
1060
1061/*!
1062 \since QtQuick.Controls 2.4 (Qt 5.11)
1063 \qmlproperty int QtQuick.Controls::AbstractButton::autoRepeatDelay
1064
1065 This property holds the initial delay of auto-repetition in milliseconds.
1066 The default value is \c 300 ms.
1067
1068 \sa autoRepeat, autoRepeatInterval
1069*/
1070int QQuickAbstractButton::autoRepeatDelay() const
1071{
1072 Q_D(const QQuickAbstractButton);
1073 return d->repeatDelay;
1074}
1075
1076void QQuickAbstractButton::setAutoRepeatDelay(int delay)
1077{
1078 Q_D(QQuickAbstractButton);
1079 if (d->repeatDelay == delay)
1080 return;
1081
1082 d->repeatDelay = delay;
1083 emit autoRepeatDelayChanged();
1084}
1085
1086/*!
1087 \since QtQuick.Controls 2.4 (Qt 5.11)
1088 \qmlproperty int QtQuick.Controls::AbstractButton::autoRepeatInterval
1089
1090 This property holds the interval of auto-repetition in milliseconds.
1091 The default value is \c 100 ms.
1092
1093 \sa autoRepeat, autoRepeatDelay
1094*/
1095int QQuickAbstractButton::autoRepeatInterval() const
1096{
1097 Q_D(const QQuickAbstractButton);
1098 return d->repeatInterval;
1099}
1100
1101void QQuickAbstractButton::setAutoRepeatInterval(int interval)
1102{
1103 Q_D(QQuickAbstractButton);
1104 if (d->repeatInterval == interval)
1105 return;
1106
1107 d->repeatInterval = interval;
1108 emit autoRepeatIntervalChanged();
1109}
1110
1111#if QT_CONFIG(shortcut)
1112/*!
1113 \internal
1114
1115 This is the mnemonic shortcut that is derived from the text;
1116 see \c QQuickAbstractButton::buttonChange().
1117*/
1118QKeySequence QQuickAbstractButton::shortcut() const
1119{
1120 Q_D(const QQuickAbstractButton);
1121 return d->shortcut;
1122}
1123
1124void QQuickAbstractButton::setShortcut(const QKeySequence &shortcut)
1125{
1126 Q_D(QQuickAbstractButton);
1127 if (d->shortcut == shortcut)
1128 return;
1129
1130 d->ungrabShortcut();
1131 d->shortcut = shortcut;
1132 if (isVisible() && d->mnemonicEnabled)
1133 d->grabShortcut();
1134}
1135#endif
1136
1137/*!
1138 \readonly
1139 \since QtQuick.Controls 2.4 (Qt 5.11)
1140 \qmlproperty real QtQuick.Controls::AbstractButton::pressX
1141
1142 This property holds the x-coordinate of the last press.
1143
1144 \note The value is updated on touch moves, but left intact after touch release.
1145
1146 \sa pressY
1147*/
1148qreal QQuickAbstractButton::pressX() const
1149{
1150 Q_D(const QQuickAbstractButton);
1151 return d->movePoint.x();
1152}
1153
1154/*!
1155 \readonly
1156 \since QtQuick.Controls 2.4 (Qt 5.11)
1157 \qmlproperty real QtQuick.Controls::AbstractButton::pressY
1158
1159 This property holds the y-coordinate of the last press.
1160
1161 \note The value is updated on touch moves, but left intact after touch release.
1162
1163 \sa pressX
1164*/
1165qreal QQuickAbstractButton::pressY() const
1166{
1167 Q_D(const QQuickAbstractButton);
1168 return d->movePoint.y();
1169}
1170
1171/*!
1172 \since QtQuick.Controls 2.5 (Qt 5.12)
1173 \qmlproperty real QtQuick.Controls::AbstractButton::implicitIndicatorWidth
1174 \readonly
1175
1176 This property holds the implicit indicator width.
1177
1178 The value is equal to \c {indicator ? indicator.implicitWidth : 0}.
1179
1180 This is typically used, together with \l {Control::}{implicitContentWidth} and
1181 \l {Control::}{implicitBackgroundWidth}, to calculate the \l {Item::}{implicitWidth}.
1182
1183 \sa implicitIndicatorHeight
1184*/
1185qreal QQuickAbstractButton::implicitIndicatorWidth() const
1186{
1187 Q_D(const QQuickAbstractButton);
1188 if (!d->indicator)
1189 return 0;
1190 return d->indicator->implicitWidth();
1191}
1192
1193/*!
1194 \since QtQuick.Controls 2.5 (Qt 5.12)
1195 \qmlproperty real QtQuick.Controls::AbstractButton::implicitIndicatorHeight
1196 \readonly
1197
1198 This property holds the implicit indicator height.
1199
1200 The value is equal to \c {indicator ? indicator.implicitHeight : 0}.
1201
1202 This is typically used, together with \l {Control::}{implicitContentHeight} and
1203 \l {Control::}{implicitBackgroundHeight}, to calculate the \l {Item::}{implicitHeight}.
1204
1205 \sa implicitIndicatorWidth
1206*/
1207qreal QQuickAbstractButton::implicitIndicatorHeight() const
1208{
1209 Q_D(const QQuickAbstractButton);
1210 if (!d->indicator)
1211 return 0;
1212 return d->indicator->implicitHeight();
1213}
1214
1215/*!
1216 \qmlmethod void QtQuick.Controls::AbstractButton::toggle()
1217
1218 Toggles the checked state of the button.
1219
1220 \sa click(), animateClick()
1221*/
1222void QQuickAbstractButton::toggle()
1223{
1224 Q_D(QQuickAbstractButton);
1225 setChecked(!d->checked);
1226}
1227
1228/*!
1229 \since Qt 6.8
1230 \qmlmethod void QtQuick.Controls::AbstractButton::click()
1231
1232 Simulates the button being clicked with no delay between press and release.
1233
1234 All signals associated with a click are emitted as appropriate.
1235
1236 If the \l [QML] {Item::} {focusPolicy} includes \c Qt.ClickFocus,
1237 \l [QML] {Item::} {activeFocus} will become \c true.
1238
1239 This function does nothing if the button is
1240 \l [QML] {Item::enabled} {disabled}.
1241
1242 Calling this function again before the button is released resets
1243 the release timer.
1244
1245 \sa animateClick(), pressed(), released(), clicked()
1246*/
1247void QQuickAbstractButton::click()
1248{
1249 Q_D(QQuickAbstractButton);
1250 if (!isEnabled())
1251 return;
1252
1253 // QQuickItemPrivate::deliverPointerEvent calls setFocusIfNeeded on real clicks,
1254 // so we need to do it ourselves.
1255 const bool setFocusOnPress = !QGuiApplication::styleHints()->setFocusOnTouchRelease();
1256 if (setFocusOnPress && focusPolicy() & Qt::ClickFocus)
1257 forceActiveFocus(Qt::MouseFocusReason);
1258
1259 const QPointF eventPos(d->width / 2, d->height / 2);
1260 d->handlePress(eventPos, 0);
1261 d->handleRelease(eventPos, 0);
1262}
1263
1264/*!
1265 \since Qt 6.8
1266 \qmlmethod void QtQuick.Controls::AbstractButton::animateClick()
1267
1268 Simulates the button being clicked, with a 100 millisecond delay
1269 between press and release, animating its visual state in the
1270 process.
1271
1272 All signals associated with a click are emitted as appropriate.
1273
1274 If the \l [QML] {Item::} {focusPolicy} includes \c Qt.ClickFocus,
1275 \l [QML] {Item::}{activeFocus} will become \c true.
1276
1277 This function does nothing if the button is
1278 \l [QML] {Item::enabled} {disabled}.
1279
1280 Calling this function again before the button is released resets
1281 the release timer.
1282
1283 \sa click(), pressed(), released(), clicked()
1284*/
1285void QQuickAbstractButton::animateClick()
1286{
1287 Q_D(QQuickAbstractButton);
1288 if (!isEnabled())
1289 return;
1290
1291 // See comment in click() for why we do this.
1292 const bool setFocusOnPress = !QGuiApplication::styleHints()->setFocusOnTouchRelease();
1293 if (setFocusOnPress && focusPolicy() & Qt::ClickFocus)
1294 forceActiveFocus(Qt::MouseFocusReason);
1295
1296 // If the timer was already running, kill it so we can restart it.
1297 if (d->animateTimer != 0) {
1298 killTimer(d->animateTimer);
1299 d->animateTimer = 0;
1300 } else {
1301 d->handlePress(QPointF(d->width / 2, d->height / 2), 0);
1302 }
1303
1304 d->animateTimer = startTimer(100);
1305}
1306
1307void QQuickAbstractButton::componentComplete()
1308{
1309 Q_D(QQuickAbstractButton);
1310 d->executeIndicator(true);
1311 QQuickControl::componentComplete();
1312 if (d->needsTextFormatWarning())
1313 polish();
1314}
1315
1316void QQuickAbstractButton::updatePolish()
1317{
1318 Q_D(QQuickAbstractButton);
1319 QQuickControl::updatePolish();
1320 if (d->needsTextFormatWarning()) {
1321 QQuickTemplatesUtils::warnAboutImplicitRichText(this);
1322 d->textFormatWarningDisplayed = true;
1323 }
1324}
1325
1326bool QQuickAbstractButton::event(QEvent *event)
1327{
1328#if QT_CONFIG(shortcut)
1329 Q_D(QQuickAbstractButton);
1330 if (event->type() == QEvent::Shortcut) {
1331 QShortcutEvent *se = static_cast<QShortcutEvent *>(event);
1332 if (se->shortcutId() == d->shortcutId) {
1333 d->trigger();
1334 return true;
1335 }
1336 }
1337#endif
1338 return QQuickControl::event(event);
1339}
1340
1341void QQuickAbstractButton::focusOutEvent(QFocusEvent *event)
1342{
1343 Q_D(QQuickAbstractButton);
1344 QQuickControl::focusOutEvent(event);
1345 if (d->touchId == -1) // don't ungrab on multi-touch if another control gets focused
1346 d->handleUngrab();
1347}
1348
1349void QQuickAbstractButton::keyPressEvent(QKeyEvent *event)
1350{
1351 Q_D(QQuickAbstractButton);
1352 QQuickControl::keyPressEvent(event);
1353 if (d->acceptKeyClick(static_cast<Qt::Key>(event->key()))) {
1354 d->setPressPoint(d->centerPressPoint());
1355 setPressed(true);
1356
1357 if (d->autoRepeat)
1358 d->startRepeatDelay();
1359
1360 emit pressed();
1361 event->accept();
1362 }
1363}
1364
1365void QQuickAbstractButton::keyReleaseEvent(QKeyEvent *event)
1366{
1367 Q_D(QQuickAbstractButton);
1368 QQuickControl::keyReleaseEvent(event);
1369 if (d->pressed && d->acceptKeyClick(static_cast<Qt::Key>(event->key()))) {
1370 setPressed(false);
1371
1372 nextCheckState();
1373 emit released();
1374 d->trigger();
1375
1376 if (d->autoRepeat)
1377 d->stopPressRepeat();
1378 event->accept();
1379 }
1380}
1381
1382void QQuickAbstractButton::mousePressEvent(QMouseEvent *event)
1383{
1384 if (!(event->buttons() & Qt::LeftButton)) {
1385 event->ignore();
1386 return;
1387 }
1388
1389 Q_D(QQuickAbstractButton);
1390 d->pressButtons = event->buttons();
1391 QQuickControl::mousePressEvent(event);
1392}
1393
1394void QQuickAbstractButton::mouseDoubleClickEvent(QMouseEvent *event)
1395{
1396 Q_UNUSED(event);
1397 Q_D(QQuickAbstractButton);
1398 if (d->isDoubleClickConnected()) {
1399 // don't call QQuickItem::mouseDoubleClickEvent(): it would ignore()
1400 emit doubleClicked();
1401 d->wasDoubleClick = true;
1402 }
1403}
1404
1405void QQuickAbstractButton::timerEvent(QTimerEvent *event)
1406{
1407 Q_D(QQuickAbstractButton);
1408 QQuickControl::timerEvent(event);
1409 if (event->timerId() == d->holdTimer) {
1410 d->stopPressAndHold();
1411 d->wasHeld = true;
1412 emit pressAndHold();
1413 QQuickToolTipAttachedPrivate::maybeSetVisibleImplicitly(this, true);
1414 } else if (event->timerId() == d->delayTimer) {
1415 d->startPressRepeat();
1416 } else if (event->timerId() == d->repeatTimer) {
1417 emit released();
1418 d->trigger();
1419 emit pressed();
1420 } else if (event->timerId() == d->animateTimer) {
1421 const bool setFocusOnRelease = QGuiApplication::styleHints()->setFocusOnTouchRelease();
1422 if (setFocusOnRelease && focusPolicy() & Qt::ClickFocus)
1423 forceActiveFocus(Qt::MouseFocusReason);
1424 d->handleRelease(QPointF(d->width / 2, d->height / 2), 0);
1425 killTimer(d->animateTimer);
1426 d->animateTimer = 0;
1427 }
1428}
1429
1430void QQuickAbstractButton::itemChange(ItemChange change, const ItemChangeData &value)
1431{
1432 QQuickControl::itemChange(change, value);
1433#if QT_CONFIG(shortcut)
1434 Q_D(QQuickAbstractButton);
1435 if (change == ItemVisibleHasChanged && d->mnemonicEnabled) {
1436 if (value.boolValue)
1437 d->grabShortcut();
1438 else
1439 d->ungrabShortcut();
1440 }
1441#endif
1442}
1443
1444void QQuickAbstractButton::buttonChange(ButtonChange change)
1445{
1446 Q_D(QQuickAbstractButton);
1447 switch (change) {
1448 case ButtonCheckedChange:
1449 if (d->checked) {
1450 QQuickAbstractButton *button = d->findCheckedButton();
1451 if (button && button != this)
1452 button->setChecked(false);
1453 }
1454 break;
1455 case ButtonTextChange: {
1456 const QString txt = text();
1457#if QT_CONFIG(accessibility)
1458 maybeSetAccessibleName(qt_accStripAmp(txt));
1459#endif
1460#if QT_CONFIG(shortcut)
1461 if (d->mnemonicEnabled)
1462 setShortcut(QKeySequence::mnemonic(txt));
1463#endif
1464 if (isComponentComplete() && d->needsTextFormatWarning())
1465 polish();
1466 emit textChanged();
1467 break;
1468 }
1469 default:
1470 break;
1471 }
1472}
1473
1474void QQuickAbstractButton::nextCheckState()
1475{
1476 Q_D(QQuickAbstractButton);
1477 if (!d->checkable)
1478 return;
1479
1480 if (d->checked) {
1481 if (d->findCheckedButton() == this)
1482 return;
1483 if (d->action) {
1484 // For non-exclusive groups checkedAction is null
1485 if (const auto group = QQuickActionPrivate::get(d->action)->group)
1486 if (group->checkedAction() == d->action)
1487 return;
1488 }
1489 }
1490
1491 d->toggle(!d->checked);
1492}
1493
1494#if QT_CONFIG(accessibility)
1495void QQuickAbstractButton::accessibilityActiveChanged(bool active)
1496{
1497 QQuickControl::accessibilityActiveChanged(active);
1498
1499 Q_D(QQuickAbstractButton);
1500 if (active) {
1501 maybeSetAccessibleName(qt_accStripAmp(text()));
1502 setAccessibleProperty("pressed", d->pressed);
1503 setAccessibleProperty("checked", d->checked);
1504 setAccessibleProperty("checkable", d->checkable);
1505 }
1506}
1507
1508QAccessible::Role QQuickAbstractButton::accessibleRole() const
1509{
1510 Q_D(const QQuickAbstractButton);
1511 if (d->checkable) {
1512 return QAccessible::CheckBox;
1513 }
1514 return QAccessible::Button;
1515}
1516
1517void QQuickAbstractButton::accessiblePressAction()
1518{
1519 Q_D(QQuickAbstractButton);
1520 d->accessiblePressAction();
1521}
1522
1523void QQuickAbstractButton::accessibleToggleAction()
1524{
1525 Q_D(QQuickAbstractButton);
1526 d->accessibleToggleAction();
1527}
1528#endif
1529
1530/*!
1531 \since Qt 6.13
1532 \qmlproperty enumeration QtQuick.Controls::AbstractButton::textFormat
1533
1534 This property holds the format of the \l {text} displayed by the button.
1535
1536 The supported values are the same as for \l {Text::textFormat}{Text.textFormat}.
1537 The default value is \c Text.AutoText.
1538
1539 Set this to \c Text.PlainText when the text comes from a source that is
1540 not under the control of the application, such as file or folder names,
1541 so that it is always displayed as-is.
1542
1543 \note This property is applied by the button's \l {Control::}{contentItem}.
1544 A custom content item needs to take it into account itself.
1545*/
1546QQuickText::TextFormat QQuickAbstractButton::textFormat() const
1547{
1548 Q_D(const QQuickAbstractButton);
1549 return d->textFormat.value_or(QQuickText::AutoText);
1550}
1551
1552void QQuickAbstractButton::setTextFormat(QQuickText::TextFormat textFormat)
1553{
1554 Q_D(QQuickAbstractButton);
1555 const bool changed = textFormat != this->textFormat();
1556 d->textFormat = textFormat;
1557 if (changed)
1558 emit textFormatChanged();
1559}
1560
1561void QQuickAbstractButton::resetTextFormat()
1562{
1563 Q_D(QQuickAbstractButton);
1564 const bool changed = textFormat() != QQuickText::AutoText;
1565 d->textFormat.reset();
1566 if (changed)
1567 emit textFormatChanged();
1568 if (d->needsTextFormatWarning())
1569 polish();
1570}
1571
1572QT_END_NAMESPACE
1573
1574#include "moc_qquickabstractbutton_p.cpp"
Combined button and popup list for selecting options.