ButtonStyle.qml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /*!
  43. \qmltype ButtonStyle
  44. \inqmlmodule QtQuick.Controls.Styles
  45. \since 5.1
  46. \ingroup controlsstyling
  47. \brief Provides custom styling for Button.
  48. You can create a custom button by replacing the "background" delegate
  49. of the ButtonStyle with a custom design.
  50. Example:
  51. \qml
  52. Button {
  53. text: "A button"
  54. style: ButtonStyle {
  55. background: Rectangle {
  56. implicitWidth: 100
  57. implicitHeight: 25
  58. border.width: control.activeFocus ? 2 : 1
  59. border.color: "#888"
  60. radius: 4
  61. gradient: Gradient {
  62. GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
  63. GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
  64. }
  65. }
  66. }
  67. }
  68. \endqml
  69. If you need a custom label, you can replace the label item.
  70. */
  71. Style {
  72. id: buttonstyle
  73. /*! The \l {QtQuick.Controls::}{Button} this style is attached to. */
  74. readonly property Button control: __control
  75. /*! The padding between the background and the label components. */
  76. padding {
  77. top: 4
  78. left: 4
  79. right: 4 + (control.menu !== null ? Math.round(TextSingleton.implicitHeight * 0.5) : 0)
  80. bottom: 4
  81. }
  82. /*! This defines the background of the button. */
  83. property Component background: Item {
  84. property bool down: control.pressed || (control.checkable && control.checked)
  85. implicitWidth: Math.round(TextSingleton.implicitHeight * 4.5)
  86. implicitHeight: Math.max(25, Math.round(TextSingleton.implicitHeight * 1.2))
  87. Rectangle {
  88. anchors.fill: parent
  89. anchors.bottomMargin: down ? 0 : -1
  90. color: "#10000000"
  91. radius: baserect.radius
  92. }
  93. Rectangle {
  94. id: baserect
  95. gradient: Gradient {
  96. GradientStop {color: down ? "#aaa" : "#fefefe" ; position: 0}
  97. GradientStop {color: down ? "#ccc" : "#e3e3e3" ; position: down ? 0.1: 1}
  98. }
  99. radius: TextSingleton.implicitHeight * 0.16
  100. anchors.fill: parent
  101. border.color: control.activeFocus ? "#47b" : "#999"
  102. Rectangle {
  103. anchors.fill: parent
  104. radius: parent.radius
  105. color: control.activeFocus ? "#47b" : "white"
  106. opacity: control.hovered || control.activeFocus ? 0.1 : 0
  107. Behavior on opacity {NumberAnimation{ duration: 100 }}
  108. }
  109. }
  110. Image {
  111. id: imageItem
  112. visible: control.menu !== null
  113. source: "images/arrow-down.png"
  114. anchors.verticalCenter: parent.verticalCenter
  115. anchors.right: parent.right
  116. anchors.rightMargin: 4
  117. opacity: control.enabled ? 0.6 : 0.5
  118. }
  119. }
  120. /*! This defines the label of the button. */
  121. property Component label: Item {
  122. implicitWidth: row.implicitWidth
  123. implicitHeight: row.implicitHeight
  124. baselineOffset: row.y + text.y + text.baselineOffset
  125. Row {
  126. id: row
  127. anchors.centerIn: parent
  128. spacing: 2
  129. Image {
  130. source: control.iconSource
  131. anchors.verticalCenter: parent.verticalCenter
  132. }
  133. Text {
  134. id: text
  135. renderType: Settings.isMobile ? Text.QtRendering : Text.NativeRendering
  136. anchors.verticalCenter: parent.verticalCenter
  137. text: StyleHelpers.stylizeMnemonics(control.text)
  138. color: SystemPaletteSingleton.buttonText(control.enabled)
  139. }
  140. }
  141. }
  142. /*! \internal */
  143. property Component panel: Item {
  144. anchors.fill: parent
  145. implicitWidth: Math.max(labelLoader.implicitWidth + padding.left + padding.right, backgroundLoader.implicitWidth)
  146. implicitHeight: Math.max(labelLoader.implicitHeight + padding.top + padding.bottom, backgroundLoader.implicitHeight)
  147. baselineOffset: labelLoader.item ? padding.top + labelLoader.item.baselineOffset : 0
  148. Loader {
  149. id: backgroundLoader
  150. anchors.fill: parent
  151. sourceComponent: background
  152. }
  153. Loader {
  154. id: labelLoader
  155. sourceComponent: label
  156. anchors.fill: parent
  157. anchors.leftMargin: padding.left
  158. anchors.topMargin: padding.top
  159. anchors.rightMargin: padding.right
  160. anchors.bottomMargin: padding.bottom
  161. }
  162. }
  163. }