BasicButton.qml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.Controls.Styles 1.1
  43. import QtQuick.Window 2.2
  44. /*!
  45. \qmltype BasicButton
  46. \internal
  47. \qmlabstract
  48. \inqmlmodule QtQuick.Controls.Private
  49. */
  50. Control {
  51. id: button
  52. /*! This signal is emitted when the button is clicked. */
  53. signal clicked
  54. /*! \qmlproperty bool BasicButton::pressed
  55. This property holds whether the button is being pressed. */
  56. readonly property alias pressed: button.__effectivePressed
  57. /*! \qmlproperty bool BasicButton::hovered
  58. This property indicates whether the control is being hovered.
  59. */
  60. readonly property alias hovered: behavior.containsMouse
  61. /*! This property holds whether the button is checkable.
  62. The default value is \c false. */
  63. property bool checkable: false
  64. Accessible.checkable: checkable
  65. /*! This property holds whether the button is checked.
  66. Only checkable buttons can be checked.
  67. The default value is \c false. */
  68. property bool checked: false
  69. Accessible.checked: checked
  70. /*! This property holds the ExclusiveGroup that the button belongs to.
  71. The default value is \c null. */
  72. property ExclusiveGroup exclusiveGroup: null
  73. /*! This property holds the associated button action.
  74. If a button has an action associated, the action defines the
  75. button's properties like checked, text, tooltip etc.
  76. When an action is set, it's still possible to override the \l text,
  77. \l tooltip, \l iconSource, and \l iconName properties.
  78. The default value is \c null. */
  79. property Action action: null
  80. /*! This property specifies whether the button should gain active focus when pressed.
  81. The default value is \c false. */
  82. property bool activeFocusOnPress: false
  83. /*! This property holds the text shown on the button. If the button has no
  84. text, the \l text property will be an empty string.
  85. The default value is the empty string.
  86. */
  87. property string text: action ? action.text : ""
  88. /*! This property holds the button tooltip. */
  89. property string tooltip: action ? (action.tooltip || StyleHelpers.removeMnemonics(action.text)) : ""
  90. /*! This property holds the icon shown on the button. If the button has no
  91. icon, the iconSource property will be an empty string.
  92. The default value is the empty string.
  93. */
  94. property url iconSource: action ? action.iconSource : ""
  95. /*! The image label source as theme name.
  96. When an icon from the platform icon theme is found, this takes
  97. precedence over iconSource.
  98. \include icons.qdocinc iconName
  99. */
  100. property string iconName: action ? action.iconName : ""
  101. /*! \internal */
  102. property string __position: "only"
  103. /*! \internal */
  104. readonly property bool __iconOverriden: button.action && (button.action.iconSource !== button.iconSource || button.action.iconName !== button.iconName)
  105. /*! \internal */
  106. property Action __action: action || ownAction
  107. /*! \internal */
  108. readonly property Action __iconAction: __iconOverriden ? ownAction : __action
  109. /*! \internal */
  110. onExclusiveGroupChanged: {
  111. if (exclusiveGroup)
  112. exclusiveGroup.bindCheckable(button)
  113. }
  114. Accessible.role: Accessible.Button
  115. Accessible.description: tooltip
  116. /*! \internal */
  117. function accessiblePressAction() {
  118. __action.trigger(button)
  119. }
  120. Action {
  121. id: ownAction
  122. enabled: button.enabled
  123. iconSource: !button.action || __iconOverriden ? button.iconSource : ""
  124. iconName: !button.action || __iconOverriden ? button.iconName : ""
  125. // let ownAction handle mnemonic if and only if the button does
  126. // not already have an action assigned to avoid ambiguous shortcuts
  127. text: button.action ? "" : button.text
  128. }
  129. Connections {
  130. target: __action
  131. function onTriggered() { button.clicked() }
  132. }
  133. activeFocusOnTab: true
  134. Keys.onPressed: {
  135. if (event.key === Qt.Key_Space && !event.isAutoRepeat && !behavior.pressed) {
  136. behavior.keyPressed = true;
  137. event.accepted = true;
  138. }
  139. }
  140. onFocusChanged: if (!focus) behavior.keyPressed = false
  141. Keys.onReleased: {
  142. if (event.key === Qt.Key_Space && !event.isAutoRepeat && behavior.keyPressed) {
  143. behavior.keyPressed = false;
  144. __action.trigger(button)
  145. behavior.toggle()
  146. event.accepted = true;
  147. }
  148. }
  149. MouseArea {
  150. id: behavior
  151. property bool keyPressed: false
  152. property bool effectivePressed: pressed && containsMouse || keyPressed
  153. anchors.fill: parent
  154. hoverEnabled: Settings.hoverEnabled
  155. enabled: !keyPressed
  156. function toggle() {
  157. if (button.checkable && !button.action && !(button.checked && button.exclusiveGroup))
  158. button.checked = !button.checked
  159. }
  160. onReleased: {
  161. if (containsMouse) {
  162. toggle()
  163. __action.trigger(button)
  164. }
  165. }
  166. onExited: Tooltip.hideText()
  167. onCanceled: Tooltip.hideText()
  168. onPressed: {
  169. if (activeFocusOnPress)
  170. button.forceActiveFocus()
  171. }
  172. Timer {
  173. interval: 1000
  174. running: behavior.containsMouse && !pressed && tooltip.length && behavior.Window.visibility !== Window.Hidden
  175. onTriggered: Tooltip.showText(behavior, Qt.point(behavior.mouseX, behavior.mouseY), tooltip)
  176. }
  177. }
  178. /*! \internal */
  179. property var __behavior: behavior
  180. /*! \internal */
  181. property bool __effectivePressed: behavior.effectivePressed
  182. states: [
  183. State {
  184. name: "boundAction"
  185. when: action !== null
  186. PropertyChanges {
  187. target: button
  188. enabled: action.enabled
  189. checkable: action.checkable
  190. checked: action.checked
  191. }
  192. }
  193. ]
  194. }