| 1 | // Copyright (C) 2020 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 | #include "qquickindicatorbutton_p.h" |
| 4 | #include "qquickdeferredexecute_p_p.h" |
| 5 | #include "qquickcontrol_p_p.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | class QQuickIndicatorButton; |
| 10 | |
| 11 | void QQuickIndicatorButtonPrivate::cancelIndicator() |
| 12 | { |
| 13 | Q_Q(QQuickIndicatorButton); |
| 14 | quickCancelDeferred(object: q, property: indicatorName()); |
| 15 | } |
| 16 | |
| 17 | void QQuickIndicatorButtonPrivate::executeIndicator(bool complete) |
| 18 | { |
| 19 | Q_Q(QQuickIndicatorButton); |
| 20 | if (indicator.wasExecuted()) |
| 21 | return; |
| 22 | |
| 23 | if (!indicator || complete) |
| 24 | quickBeginDeferred(object: q, property: indicatorName(), delegate&: indicator); |
| 25 | if (complete) |
| 26 | quickCompleteDeferred(object: q, property: indicatorName(), delegate&: indicator); |
| 27 | } |
| 28 | |
| 29 | QQuickIndicatorButton::QQuickIndicatorButton(QObject *parent) |
| 30 | : QObject(*(new QQuickIndicatorButtonPrivate), parent) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | QQuickIndicatorButton::~QQuickIndicatorButton() |
| 35 | { |
| 36 | Q_D(QQuickIndicatorButton); |
| 37 | QQuickControl *parentControl = static_cast<QQuickControl *>(parent()); |
| 38 | if (parentControl) |
| 39 | QQuickControlPrivate::get(control: parentControl)->removeImplicitSizeListener(item: d->indicator); |
| 40 | } |
| 41 | |
| 42 | bool QQuickIndicatorButton::isPressed() const |
| 43 | { |
| 44 | Q_D(const QQuickIndicatorButton); |
| 45 | return d->pressed; |
| 46 | } |
| 47 | |
| 48 | void QQuickIndicatorButton::setPressed(bool pressed) |
| 49 | { |
| 50 | Q_D(QQuickIndicatorButton); |
| 51 | if (d->pressed == pressed) |
| 52 | return; |
| 53 | |
| 54 | d->pressed = pressed; |
| 55 | emit pressedChanged(); |
| 56 | } |
| 57 | |
| 58 | QQuickItem *QQuickIndicatorButton::indicator() const |
| 59 | { |
| 60 | QQuickIndicatorButtonPrivate *d = const_cast<QQuickIndicatorButtonPrivate *>(d_func()); |
| 61 | if (!d->indicator) |
| 62 | d->executeIndicator(); |
| 63 | return d->indicator; |
| 64 | } |
| 65 | |
| 66 | void QQuickIndicatorButton::setIndicator(QQuickItem *indicator) |
| 67 | { |
| 68 | Q_D(QQuickIndicatorButton); |
| 69 | if (d->indicator == indicator) |
| 70 | return; |
| 71 | |
| 72 | if (!d->indicator.isExecuting()) |
| 73 | d->cancelIndicator(); |
| 74 | |
| 75 | const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth(); |
| 76 | const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight(); |
| 77 | |
| 78 | QQuickControl *par = static_cast<QQuickControl *>(parent()); |
| 79 | QQuickControlPrivate::warnIfCustomizationNotSupported(control: par, item: indicator, QStringLiteral("indicator" )); |
| 80 | |
| 81 | QQuickControlPrivate::get(control: par)->removeImplicitSizeListener(item: d->indicator); |
| 82 | QQuickControlPrivate::hideOldItem(item: d->indicator); |
| 83 | d->indicator = indicator; |
| 84 | |
| 85 | if (indicator) { |
| 86 | if (!indicator->parentItem()) |
| 87 | indicator->setParentItem(par); |
| 88 | QQuickControlPrivate::get(control: par)->addImplicitSizeListener(item: indicator); |
| 89 | } |
| 90 | |
| 91 | if (!qFuzzyCompare(p1: oldImplicitIndicatorWidth, p2: implicitIndicatorWidth())) |
| 92 | emit implicitIndicatorWidthChanged(); |
| 93 | if (!qFuzzyCompare(p1: oldImplicitIndicatorHeight, p2: implicitIndicatorHeight())) |
| 94 | emit implicitIndicatorHeightChanged(); |
| 95 | if (!d->indicator.isExecuting()) |
| 96 | emit indicatorChanged(); |
| 97 | } |
| 98 | |
| 99 | bool QQuickIndicatorButton::isHovered() const |
| 100 | { |
| 101 | Q_D(const QQuickIndicatorButton); |
| 102 | return d->hovered; |
| 103 | } |
| 104 | |
| 105 | void QQuickIndicatorButton::setHovered(bool hovered) |
| 106 | { |
| 107 | Q_D(QQuickIndicatorButton); |
| 108 | if (d->hovered == hovered) |
| 109 | return; |
| 110 | |
| 111 | d->hovered = hovered; |
| 112 | emit hoveredChanged(); |
| 113 | } |
| 114 | |
| 115 | qreal QQuickIndicatorButton::implicitIndicatorWidth() const |
| 116 | { |
| 117 | Q_D(const QQuickIndicatorButton); |
| 118 | if (!d->indicator) |
| 119 | return 0; |
| 120 | return d->indicator->implicitWidth(); |
| 121 | } |
| 122 | |
| 123 | qreal QQuickIndicatorButton::implicitIndicatorHeight() const |
| 124 | { |
| 125 | Q_D(const QQuickIndicatorButton); |
| 126 | if (!d->indicator) |
| 127 | return 0; |
| 128 | return d->indicator->implicitHeight(); |
| 129 | } |
| 130 | |
| 131 | QT_END_NAMESPACE |
| 132 | |
| 133 | #include "moc_qquickindicatorbutton_p.cpp" |
| 134 | |