DelayButton.qml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Extras module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQml 2.14 as Qml
  40. import QtQuick 2.2
  41. import QtQuick.Controls 1.4
  42. import QtQuick.Controls.Styles 1.4
  43. import QtQuick.Controls.Private 1.0
  44. /*!
  45. \qmltype DelayButton
  46. \inherits QtQuickControls::Button
  47. \inqmlmodule QtQuick.Extras
  48. \since 5.5
  49. \ingroup extras
  50. \ingroup extras-interactive
  51. \brief A checkable button that triggers an action when held in long enough.
  52. \image delaybutton.png A DelayButton
  53. The DelayButton is a checkable button that incorporates a delay before
  54. the button becomes checked and the \l activated signal is emitted. This
  55. delay prevents accidental presses.
  56. The current progress is expressed as a decimal value between \c 0.0 and
  57. \c 1.0. The time it takes for \l activated to be emitted is measured in
  58. milliseconds, and can be set with the \l delay property.
  59. The progress is indicated by a progress indicator around the button. When
  60. the indicator reaches completion, it flashes.
  61. \image delaybutton-progress.png A DelayButton being held down
  62. A DelayButton being held down
  63. \image delaybutton-activated.png A DelayButton after being activated
  64. A DelayButton after being activated
  65. You can create a custom appearance for a DelayButton by assigning a
  66. \l {DelayButtonStyle}.
  67. */
  68. Button {
  69. id: root
  70. style: Settings.styleComponent(Settings.style, "DelayButtonStyle.qml", root)
  71. /*!
  72. \qmlproperty real DelayButton::progress
  73. This property holds the current progress as displayed by the progress
  74. indicator, in the range \c 0.0 - \c 1.0.
  75. */
  76. readonly property alias progress: root.__progress
  77. /*!
  78. This property holds the time it takes (in milliseconds) for \l progress
  79. to reach \c 1.0 and emit \l activated.
  80. The default value is \c 3000 ms.
  81. */
  82. property int delay: 3000
  83. /*!
  84. This signal is emitted when \l progress reaches \c 1.0 and the button
  85. becomes checked.
  86. */
  87. signal activated
  88. /*! \internal */
  89. property real __progress: 0.0
  90. Behavior on __progress {
  91. id: progressBehavior
  92. NumberAnimation {
  93. id: numberAnimation
  94. }
  95. }
  96. Qml.Binding {
  97. // Force checkable to false to get full control over the checked -property
  98. target: root
  99. property: "checkable"
  100. value: false
  101. restoreMode: Binding.RestoreBinding
  102. }
  103. onProgressChanged: {
  104. if (__progress === 1.0) {
  105. checked = true;
  106. activated();
  107. }
  108. }
  109. onCheckedChanged: {
  110. if (checked) {
  111. if (__progress < 1) {
  112. // Programmatically activated the button; don't animate.
  113. progressBehavior.enabled = false;
  114. __progress = 1;
  115. progressBehavior.enabled = true;
  116. }
  117. } else {
  118. // Unchecked the button after it was flashing; it should instantly stop
  119. // flashing (with no reversed progress bar).
  120. progressBehavior.enabled = false;
  121. __progress = 0;
  122. progressBehavior.enabled = true;
  123. }
  124. }
  125. onPressedChanged: {
  126. if (checked) {
  127. if (pressed) {
  128. // Pressed the button to stop the activation.
  129. checked = false;
  130. }
  131. } else {
  132. var effectiveDelay = pressed ? delay : delay * 0.3;
  133. // Not active. Either the button is being held down or let go.
  134. numberAnimation.duration = Math.max(0, (pressed ? 1 - __progress : __progress) * effectiveDelay);
  135. __progress = pressed ? 1 : 0;
  136. }
  137. }
  138. }