CheckBoxStyle.qml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.Layouts 1.1
  42. import QtQuick.Window 2.1
  43. import QtQuick.Controls.Private 1.0
  44. /*!
  45. \qmltype CheckBoxStyle
  46. \inqmlmodule QtQuick.Controls.Styles
  47. \since 5.1
  48. \ingroup controlsstyling
  49. \brief Provides custom styling for CheckBox.
  50. Example:
  51. \qml
  52. CheckBox {
  53. text: "Check Box"
  54. style: CheckBoxStyle {
  55. indicator: Rectangle {
  56. implicitWidth: 16
  57. implicitHeight: 16
  58. radius: 3
  59. border.color: control.activeFocus ? "darkblue" : "gray"
  60. border.width: 1
  61. Rectangle {
  62. visible: control.checked
  63. color: "#555"
  64. border.color: "#333"
  65. radius: 1
  66. anchors.margins: 4
  67. anchors.fill: parent
  68. }
  69. }
  70. }
  71. }
  72. \endqml
  73. */
  74. Style {
  75. id: checkboxStyle
  76. /*! The \l CheckBox this style is attached to. */
  77. readonly property CheckBox control: __control
  78. /*! This defines the text label. */
  79. property Component label: Item {
  80. implicitWidth: text.implicitWidth + 2
  81. implicitHeight: text.implicitHeight
  82. baselineOffset: text.baselineOffset
  83. Rectangle {
  84. anchors.fill: text
  85. anchors.margins: -1
  86. anchors.leftMargin: -3
  87. anchors.rightMargin: -3
  88. visible: control.activeFocus
  89. height: 6
  90. radius: 3
  91. color: "#224f9fef"
  92. border.color: "#47b"
  93. opacity: 0.6
  94. }
  95. Text {
  96. id: text
  97. text: StyleHelpers.stylizeMnemonics(control.text)
  98. anchors.centerIn: parent
  99. color: SystemPaletteSingleton.text(control.enabled)
  100. renderType: Settings.isMobile ? Text.QtRendering : Text.NativeRendering
  101. }
  102. }
  103. /*! The background under indicator and label. */
  104. property Component background
  105. /*! The spacing between indicator and label. */
  106. property int spacing: Math.round(TextSingleton.implicitHeight/4)
  107. /*! This defines the indicator button. */
  108. property Component indicator: Item {
  109. implicitWidth: Math.round(TextSingleton.implicitHeight)
  110. implicitHeight: implicitWidth
  111. Rectangle {
  112. anchors.fill: parent
  113. anchors.bottomMargin: -1
  114. color: "#44ffffff"
  115. radius: baserect.radius
  116. }
  117. Rectangle {
  118. id: baserect
  119. gradient: Gradient {
  120. GradientStop {color: "#eee" ; position: 0}
  121. GradientStop {color: control.pressed ? "#eee" : "#fff" ; position: 0.1}
  122. GradientStop {color: "#fff" ; position: 1}
  123. }
  124. radius: TextSingleton.implicitHeight * 0.16
  125. anchors.fill: parent
  126. border.color: control.activeFocus ? "#47b" : "#999"
  127. }
  128. Image {
  129. source: "images/check.png"
  130. opacity: control.checkedState === Qt.Checked ? control.enabled ? 1 : 0.5 : 0
  131. anchors.centerIn: parent
  132. anchors.verticalCenterOffset: 1
  133. Behavior on opacity {NumberAnimation {duration: 80}}
  134. }
  135. Rectangle {
  136. anchors.fill: parent
  137. anchors.margins: Math.round(baserect.radius)
  138. antialiasing: true
  139. gradient: Gradient {
  140. GradientStop {color: control.pressed ? "#555" : "#999" ; position: 0}
  141. GradientStop {color: "#555" ; position: 1}
  142. }
  143. radius: baserect.radius - 1
  144. anchors.centerIn: parent
  145. anchors.alignWhenCentered: true
  146. border.color: "#222"
  147. Behavior on opacity {NumberAnimation {duration: 80}}
  148. opacity: control.checkedState === Qt.PartiallyChecked ? control.enabled ? 1 : 0.5 : 0
  149. }
  150. }
  151. /*! \internal */
  152. property Component panel: Item {
  153. implicitWidth: Math.max(backgroundLoader.implicitWidth, row.implicitWidth + padding.left + padding.right)
  154. implicitHeight: Math.max(backgroundLoader.implicitHeight, labelLoader.implicitHeight + padding.top + padding.bottom,indicatorLoader.implicitHeight + padding.top + padding.bottom)
  155. baselineOffset: labelLoader.item ? padding.top + labelLoader.item.baselineOffset : 0
  156. Loader {
  157. id: backgroundLoader
  158. sourceComponent: background
  159. anchors.fill: parent
  160. }
  161. RowLayout {
  162. id: row
  163. anchors.fill: parent
  164. anchors.leftMargin: padding.left
  165. anchors.rightMargin: padding.right
  166. anchors.topMargin: padding.top
  167. anchors.bottomMargin: padding.bottom
  168. spacing: checkboxStyle.spacing
  169. Loader {
  170. id: indicatorLoader
  171. sourceComponent: indicator
  172. }
  173. Loader {
  174. id: labelLoader
  175. Layout.fillWidth: true
  176. sourceComponent: label
  177. }
  178. }
  179. }
  180. }