SwitchStyle.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 SwitchStyle
  44. \inqmlmodule QtQuick.Controls.Styles
  45. \since 5.2
  46. \ingroup controlsstyling
  47. \brief Provides custom styling for Switch.
  48. Example:
  49. \qml
  50. Switch {
  51. style: SwitchStyle {
  52. groove: Rectangle {
  53. implicitWidth: 100
  54. implicitHeight: 20
  55. radius: 9
  56. border.color: control.activeFocus ? "darkblue" : "gray"
  57. border.width: 1
  58. }
  59. }
  60. }
  61. \endqml
  62. */
  63. Style {
  64. id: switchstyle
  65. /*! The content padding. */
  66. padding {
  67. top: 0
  68. left: 0
  69. right: 0
  70. bottom: 0
  71. }
  72. /*! This defines the switch handle. */
  73. property Component handle: Rectangle {
  74. opacity: control.enabled ? 1.0 : 0.5
  75. implicitWidth: Math.round((parent.parent.width - padding.left - padding.right)/2)
  76. implicitHeight: control.height - padding.top - padding.bottom
  77. border.color: control.activeFocus ? Qt.darker(highlight, 2) : Qt.darker(button, 2)
  78. property color bg: control.activeFocus ? Qt.darker(highlight, 1.2) : button
  79. property color highlight: SystemPaletteSingleton.highlight(control.enabled)
  80. property color button: SystemPaletteSingleton.button(control.enabled)
  81. gradient: Gradient {
  82. GradientStop {color: Qt.lighter(bg, 1.4) ; position: 0}
  83. GradientStop {color: bg ; position: 1}
  84. }
  85. radius: 2
  86. }
  87. /*! This property holds the background groove of the switch. */
  88. property Component groove: Rectangle {
  89. property color shadow: control.checked ? Qt.darker(highlight, 1.2): "#999"
  90. property color bg: control.checked ? highlight:"#bbb"
  91. property color highlight: SystemPaletteSingleton.highlight(control.enabled)
  92. implicitWidth: Math.round(implicitHeight * 3)
  93. implicitHeight: Math.max(16, Math.round(TextSingleton.implicitHeight))
  94. border.color: "gray"
  95. color: "red"
  96. radius: 2
  97. Behavior on shadow {ColorAnimation{ duration: 80 }}
  98. Behavior on bg {ColorAnimation{ duration: 80 }}
  99. gradient: Gradient {
  100. GradientStop {color: shadow; position: 0}
  101. GradientStop {color: bg ; position: 0.2}
  102. GradientStop {color: bg ; position: 1}
  103. }
  104. Rectangle {
  105. color: "#44ffffff"
  106. height: 1
  107. anchors.bottom: parent.bottom
  108. anchors.bottomMargin: -1
  109. width: parent.width - 2
  110. x: 1
  111. }
  112. }
  113. /*! \internal */
  114. property Component panel: Item {
  115. implicitWidth: Math.round(grooveLoader.width + padding.left + padding.right)
  116. implicitHeight: grooveLoader.implicitHeight + padding.top + padding.bottom
  117. property var __handle: handleLoader
  118. property int min: padding.left
  119. property int max: grooveLoader.width - handleLoader.width - padding.right
  120. Loader {
  121. id: grooveLoader
  122. y: padding.top
  123. x: padding.left
  124. sourceComponent: groove
  125. anchors.verticalCenter: parent.verticalCenter
  126. Loader {
  127. id: handleLoader
  128. z:1
  129. x: control.checked ? max : min
  130. anchors.top: grooveLoader.top
  131. anchors.bottom: grooveLoader.bottom
  132. anchors.topMargin: padding.top
  133. anchors.bottomMargin: padding.bottom
  134. Behavior on x {
  135. id: behavior
  136. enabled: handleLoader.status === Loader.Ready
  137. NumberAnimation {
  138. duration: 150
  139. easing.type: Easing.OutCubic
  140. }
  141. }
  142. sourceComponent: handle
  143. }
  144. }
  145. }
  146. }