SwitchDelegate.qml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: http://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL3$
  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 http://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
  28. ** Software Foundation and appearing in the file LICENSE.GPL included in
  29. ** the packaging of this file. Please review the following information to
  30. ** ensure the GNU General Public License version 2.0 requirements will be
  31. ** met: http://www.gnu.org/licenses/gpl-2.0.html.
  32. **
  33. ** $QT_END_LICENSE$
  34. **
  35. ****************************************************************************/
  36. import QtQuick
  37. import QtQuick.Templates as T
  38. import QtQuick.Controls.impl
  39. import QtQuick.Controls.Imagine
  40. import QtQuick.Controls.Imagine.impl
  41. T.SwitchDelegate {
  42. id: control
  43. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  44. implicitContentWidth + leftPadding + rightPadding)
  45. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  46. implicitContentHeight + topPadding + bottomPadding,
  47. implicitIndicatorHeight + topPadding + bottomPadding)
  48. spacing: 12 // ###
  49. topPadding: background ? background.topPadding : 0
  50. leftPadding: background ? background.leftPadding : 0
  51. rightPadding: background ? background.rightPadding : 0
  52. bottomPadding: background ? background.bottomPadding : 0
  53. topInset: background ? -background.topInset || 0 : 0
  54. leftInset: background ? -background.leftInset || 0 : 0
  55. rightInset: background ? -background.rightInset || 0 : 0
  56. bottomInset: background ? -background.bottomInset || 0 : 0
  57. icon.width: 24
  58. icon.height: 24
  59. icon.color: control.palette.text
  60. indicator: NinePatchImage {
  61. x: control.text ? (control.mirrored ? control.leftPadding : control.width - width - control.rightPadding) : control.leftPadding + (control.availableWidth - width) / 2
  62. y: control.topPadding + (control.availableHeight - height) / 2
  63. width: Math.max(implicitWidth, handle.leftPadding && handle.rightPadding ? handle.implicitWidth : 2 * handle.implicitWidth)
  64. height: Math.max(implicitHeight, handle.implicitHeight)
  65. source: control.Imagine.url + "switchdelegate-indicator"
  66. NinePatchImageSelector on source {
  67. states: [
  68. {"disabled": !control.enabled},
  69. {"pressed": control.down},
  70. {"checked": control.checked},
  71. {"focused": control.visualFocus},
  72. {"highlighted": control.highlighted},
  73. {"mirrored": control.mirrored},
  74. {"hovered": control.hovered}
  75. ]
  76. }
  77. property NinePatchImage handle: NinePatchImage {
  78. readonly property real minPos: parent.leftPadding - leftPadding
  79. readonly property real maxPos: parent.width - width + rightPadding - parent.rightPadding
  80. readonly property real dragPos: control.visualPosition * parent.width - (width / 2)
  81. parent: control.indicator
  82. x: Math.max(minPos, Math.min(maxPos, control.visualPosition * parent.width - (width / 2)))
  83. y: (parent.height - height) / 2
  84. source: control.Imagine.url + "switchdelegate-handle"
  85. NinePatchImageSelector on source {
  86. states: [
  87. {"disabled": !control.enabled},
  88. {"pressed": control.down},
  89. {"checked": control.checked},
  90. {"focused": control.visualFocus},
  91. {"highlighted": control.highlighted},
  92. {"mirrored": control.mirrored},
  93. {"hovered": control.hovered}
  94. ]
  95. }
  96. Behavior on x {
  97. enabled: !control.down
  98. SmoothedAnimation { velocity: 200 }
  99. }
  100. }
  101. }
  102. contentItem: IconLabel {
  103. leftPadding: control.mirrored ? control.indicator.width + control.spacing : 0
  104. rightPadding: !control.mirrored ? control.indicator.width + control.spacing : 0
  105. spacing: control.spacing
  106. mirrored: control.mirrored
  107. display: control.display
  108. alignment: control.display === IconLabel.IconOnly || control.display === IconLabel.TextUnderIcon ? Qt.AlignCenter : Qt.AlignLeft
  109. icon: control.icon
  110. text: control.text
  111. font: control.font
  112. color: control.palette.text
  113. }
  114. background: NinePatchImage {
  115. source: control.Imagine.url + "switchdelegate-background"
  116. NinePatchImageSelector on source {
  117. states: [
  118. {"disabled": !control.enabled},
  119. {"pressed": control.down},
  120. {"checked": control.checked},
  121. {"focused": control.visualFocus},
  122. {"highlighted": control.highlighted},
  123. {"mirrored": control.mirrored},
  124. {"hovered": control.hovered}
  125. ]
  126. }
  127. }
  128. }