SwitchDelegate.qml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import QtQuick.Templates as T
  6. import QtQuick.Controls.impl
  7. import QtQuick.Controls.Imagine
  8. import QtQuick.Controls.Imagine.impl
  9. T.SwitchDelegate {
  10. id: control
  11. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  12. implicitContentWidth + leftPadding + rightPadding)
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. implicitContentHeight + topPadding + bottomPadding,
  15. implicitIndicatorHeight + topPadding + bottomPadding)
  16. spacing: 12 // ###
  17. topPadding: background ? background.topPadding : 0
  18. leftPadding: background ? background.leftPadding : 0
  19. rightPadding: background ? background.rightPadding : 0
  20. bottomPadding: background ? background.bottomPadding : 0
  21. topInset: background ? -background.topInset || 0 : 0
  22. leftInset: background ? -background.leftInset || 0 : 0
  23. rightInset: background ? -background.rightInset || 0 : 0
  24. bottomInset: background ? -background.bottomInset || 0 : 0
  25. icon.width: 24
  26. icon.height: 24
  27. icon.color: control.palette.text
  28. indicator: NinePatchImage {
  29. x: control.text ? (control.mirrored ? control.leftPadding : control.width - width - control.rightPadding) : control.leftPadding + (control.availableWidth - width) / 2
  30. y: control.topPadding + (control.availableHeight - height) / 2
  31. width: Math.max(implicitWidth, handle.leftPadding && handle.rightPadding ? handle.implicitWidth : 2 * handle.implicitWidth)
  32. height: Math.max(implicitHeight, handle.implicitHeight)
  33. source: control.Imagine.url + "switchdelegate-indicator"
  34. NinePatchImageSelector on source {
  35. states: [
  36. {"disabled": !control.enabled},
  37. {"pressed": control.down},
  38. {"checked": control.checked},
  39. {"focused": control.visualFocus},
  40. {"highlighted": control.highlighted},
  41. {"mirrored": control.mirrored},
  42. {"hovered": control.enabled && control.hovered}
  43. ]
  44. }
  45. property NinePatchImage handle: NinePatchImage {
  46. readonly property real minPos: parent.leftPadding - leftPadding
  47. readonly property real maxPos: parent.width - width + rightPadding - parent.rightPadding
  48. readonly property real dragPos: control.visualPosition * parent.width - (width / 2)
  49. parent: control.indicator
  50. x: Math.max(minPos, Math.min(maxPos, control.visualPosition * parent.width - (width / 2)))
  51. y: (parent.height - height) / 2
  52. source: control.Imagine.url + "switchdelegate-handle"
  53. NinePatchImageSelector on source {
  54. states: [
  55. {"disabled": !control.enabled},
  56. {"pressed": control.down},
  57. {"checked": control.checked},
  58. {"focused": control.visualFocus},
  59. {"highlighted": control.highlighted},
  60. {"mirrored": control.mirrored},
  61. {"hovered": control.enabled && control.hovered}
  62. ]
  63. }
  64. Behavior on x {
  65. enabled: !control.down
  66. SmoothedAnimation { velocity: 200 }
  67. }
  68. }
  69. }
  70. contentItem: IconLabel {
  71. leftPadding: control.mirrored ? control.indicator.width + control.spacing : 0
  72. rightPadding: !control.mirrored ? control.indicator.width + control.spacing : 0
  73. spacing: control.spacing
  74. mirrored: control.mirrored
  75. display: control.display
  76. alignment: control.display === IconLabel.IconOnly || control.display === IconLabel.TextUnderIcon ? Qt.AlignCenter : Qt.AlignLeft
  77. icon: control.icon
  78. text: control.text
  79. font: control.font
  80. color: control.palette.text
  81. }
  82. background: NinePatchImage {
  83. source: control.Imagine.url + "switchdelegate-background"
  84. NinePatchImageSelector on source {
  85. states: [
  86. {"disabled": !control.enabled},
  87. {"pressed": control.down},
  88. {"checked": control.checked},
  89. {"focused": control.visualFocus},
  90. {"highlighted": control.highlighted},
  91. {"mirrored": control.mirrored},
  92. {"hovered": control.enabled && control.hovered}
  93. ]
  94. }
  95. }
  96. }