FluSlider.qml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import QtQuick
  2. import QtQuick.Controls
  3. import Qt5Compat.GraphicalEffects
  4. import FluentUI
  5. Item{
  6. property int size: 180
  7. property int dotSize: 24
  8. property int value: 50
  9. property int maxValue: 100
  10. property bool vertical: false
  11. property bool tipEnabled : true
  12. property var onLineClickFunc
  13. signal pressed
  14. signal released
  15. id:root
  16. height: control.height
  17. width: control.width
  18. rotation: vertical ? 180 : 0
  19. Component.onCompleted: {
  20. seek(value)
  21. }
  22. MouseArea{
  23. id:mouse_line
  24. anchors.centerIn: control
  25. width: vertical ? 10 : control.width
  26. height: vertical ? control.height : 10
  27. hoverEnabled: true
  28. onClicked:
  29. (mouse) => {
  30. var val;
  31. if(vertical){
  32. val = mouse.y*maxValue/control.height
  33. }else{
  34. val = mouse.x*maxValue/control.width
  35. }
  36. if(onLineClickFunc){
  37. onLineClickFunc(val)
  38. }else{
  39. seek(val)
  40. }
  41. }
  42. }
  43. Rectangle {
  44. id: control
  45. width: vertical ? 4 :size
  46. height: vertical ? size : 4
  47. radius: 2
  48. anchors.verticalCenter: parent.verticalCenter
  49. color:FluTheme.dark ? Qt.rgba(162/255,162/255,162/255,1) : Qt.rgba(138/255,138/255,138/255,1)
  50. Rectangle{
  51. id:rect
  52. radius: 2.5
  53. width: vertical ? 5 : control.width*(value/maxValue)
  54. height: vertical ? control.height*(value/maxValue) : 5
  55. color:FluTheme.dark ? FluTheme.primaryColor.lighter :FluTheme.primaryColor.dark
  56. }
  57. }
  58. Rectangle{
  59. id:dot
  60. width: dotSize
  61. height: dotSize
  62. FluShadow{
  63. radius: dotSize/2
  64. }
  65. radius: dotSize/2
  66. anchors.verticalCenter: vertical ? undefined : parent.verticalCenter
  67. anchors.horizontalCenter: vertical ? parent.horizontalCenter : undefined
  68. color:FluTheme.dark ? Qt.rgba(69/255,69/255,69/255,1) :Qt.rgba(1,1,1,1)
  69. Rectangle{
  70. width: dotSize
  71. height: dotSize
  72. radius: dotSize/2
  73. color:FluTheme.dark ? FluTheme.primaryColor.lighter :FluTheme.primaryColor.dark
  74. anchors.centerIn: parent
  75. scale: {
  76. if(control_mouse.pressed){
  77. return 4/10
  78. }
  79. return control_mouse.containsMouse || mouse_line.containsMouse ? 6/10 : 5/10
  80. }
  81. Behavior on scale {
  82. NumberAnimation{
  83. duration: 150
  84. }
  85. }
  86. }
  87. MouseArea{
  88. id:control_mouse
  89. anchors.fill: parent
  90. hoverEnabled: true
  91. drag {
  92. target: dot
  93. axis: vertical ? Drag.YAxis : Drag.XAxis
  94. minimumX: vertical ? 0 : -dotSize/2
  95. maximumX: vertical ? 0 : (control.width - dotSize/2)
  96. minimumY: vertical ? -dotSize/2 : 0
  97. maximumY: vertical ? (control.height - dotSize/2) : 0
  98. }
  99. onPressed: {
  100. if(tipEnabled){
  101. tool_tip.visible = true
  102. }
  103. root.pressed()
  104. }
  105. onReleased: {
  106. tool_tip.visible = false
  107. root.released()
  108. }
  109. }
  110. FluTooltip{
  111. id:tool_tip
  112. text:String(root.value)
  113. y: vertical ? 32 : -40
  114. }
  115. }
  116. function seek(val){
  117. if(vertical){
  118. dot.y =val/maxValue*control.height - dotSize/2
  119. root.value = Qt.binding(function(){
  120. return (dot.y+dotSize/2)/control.height*maxValue
  121. })
  122. }else{
  123. dot.x =val/maxValue*control.width - dotSize/2
  124. root.value = Qt.binding(function(){
  125. return (dot.x+dotSize/2)/control.width*maxValue
  126. })
  127. }
  128. }
  129. }