AbstractCheckable.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Controls 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 QtQuick 2.2
  40. import QtQuick.Controls 1.2
  41. import QtQuick.Controls.Private 1.0
  42. import QtQuick.Window 2.2
  43. /*!
  44. \qmltype AbstractCheckable
  45. \inqmlmodule QtQuick.Controls
  46. \brief An abstract representation of a checkable control with a label
  47. \qmlabstract
  48. \internal
  49. A checkable control is one that has two states: checked (on) and
  50. unchecked (off). AbstractCheckable encapsulates the basic behavior and
  51. states that are required by checkable controls.
  52. Examples of checkable controls are RadioButton and
  53. CheckBox. CheckBox extends AbstractCheckable's behavior by adding a third
  54. state: partially checked.
  55. */
  56. Control {
  57. id: abstractCheckable
  58. /*!
  59. Emitted whenever the control is clicked.
  60. */
  61. signal clicked
  62. /*!
  63. \qmlproperty bool AbstractCheckable::pressed
  64. This property is \c true if the control is being pressed.
  65. Set this property to manually invoke a mouse click.
  66. */
  67. property alias pressed: mouseArea.effectivePressed
  68. /*! \qmlproperty bool AbstractCheckcable::hovered
  69. This property indicates whether the control is being hovered.
  70. */
  71. readonly property alias hovered: mouseArea.containsMouse
  72. /*!
  73. This property is \c true if the control is checked.
  74. */
  75. property bool checked: false
  76. Accessible.checked: checked
  77. Accessible.checkable: true
  78. /*!
  79. This property is \c true if the control takes the focus when it is
  80. pressed; \l{QQuickItem::forceActiveFocus()}{forceActiveFocus()} will be
  81. called on the control.
  82. */
  83. property bool activeFocusOnPress: false
  84. /*!
  85. This property stores the ExclusiveGroup that the control belongs to.
  86. */
  87. property ExclusiveGroup exclusiveGroup: null
  88. /*!
  89. This property holds the text that the label should display.
  90. */
  91. property string text
  92. /*!
  93. This property holds the button tooltip.
  94. \since QtQuick.Controls 1.7
  95. */
  96. property string tooltip
  97. Accessible.description: tooltip
  98. /*! \internal */
  99. property var __cycleStatesHandler: cycleRadioButtonStates
  100. activeFocusOnTab: true
  101. MouseArea {
  102. id: mouseArea
  103. focus: true
  104. anchors.fill: parent
  105. hoverEnabled: Settings.hoverEnabled
  106. enabled: !keyPressed
  107. property bool keyPressed: false
  108. property bool effectivePressed: pressed && containsMouse || keyPressed
  109. onClicked: abstractCheckable.clicked();
  110. onPressed: if (activeFocusOnPress) forceActiveFocus();
  111. onExited: Tooltip.hideText()
  112. onCanceled: Tooltip.hideText()
  113. onReleased: {
  114. if (containsMouse && (!exclusiveGroup || !checked))
  115. __cycleStatesHandler();
  116. }
  117. Timer {
  118. interval: 1000
  119. running: mouseArea.containsMouse && !pressed && tooltip.length && mouseArea.Window.visibility !== Window.Hidden
  120. onTriggered: Tooltip.showText(mouseArea, Qt.point(mouseArea.mouseX, mouseArea.mouseY), tooltip)
  121. }
  122. }
  123. /*! \internal */
  124. onExclusiveGroupChanged: {
  125. if (exclusiveGroup)
  126. exclusiveGroup.bindCheckable(abstractCheckable)
  127. }
  128. Keys.onPressed: {
  129. if (event.key === Qt.Key_Space && !event.isAutoRepeat && !mouseArea.pressed)
  130. mouseArea.keyPressed = true;
  131. }
  132. Keys.onReleased: {
  133. if (event.key === Qt.Key_Space && !event.isAutoRepeat && mouseArea.keyPressed) {
  134. mouseArea.keyPressed = false;
  135. if (!exclusiveGroup || !checked)
  136. __cycleStatesHandler();
  137. clicked();
  138. }
  139. }
  140. Action {
  141. // handle mnemonic
  142. text: abstractCheckable.text
  143. onTriggered: {
  144. if (!abstractCheckable.exclusiveGroup || !abstractCheckable.checked)
  145. abstractCheckable.__cycleStatesHandler();
  146. abstractCheckable.clicked();
  147. }
  148. }
  149. }