ColorSlider.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Dialogs 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.Private 1.0
  41. Item {
  42. id: colorSlider
  43. property real value: 1
  44. property real maximum: 1
  45. property real minimum: 0
  46. property string text: ""
  47. property bool pressed: mouseArea.pressed
  48. property bool integer: false
  49. property Component trackDelegate
  50. property string handleSource: "../images/slider_handle.png"
  51. width: parent.width
  52. height: handle.height + textText.implicitHeight
  53. function updatePos() {
  54. if (maximum > minimum) {
  55. var pos = (track.width - 10) * (value - minimum) / (maximum - minimum) + 5;
  56. return Math.min(Math.max(pos, 5), track.width - 5) - 10;
  57. } else {
  58. return 5;
  59. }
  60. }
  61. SystemPalette { id: palette }
  62. Column {
  63. id: column
  64. width: parent.width
  65. spacing: 12
  66. Text {
  67. id: textText
  68. anchors.horizontalCenter: parent.horizontalCenter
  69. text: colorSlider.text
  70. anchors.left: parent.left
  71. color: palette.windowText
  72. renderType: Settings.isMobile ? Text.QtRendering : Text.NativeRendering
  73. }
  74. Item {
  75. id: track
  76. height: 8
  77. anchors.left: parent.left
  78. anchors.right: parent.right
  79. Loader {
  80. sourceComponent: trackDelegate
  81. width: parent.height
  82. height: parent.width
  83. y: width
  84. }
  85. BorderImage {
  86. source: "../images/sunken_frame.png"
  87. border.left: 8
  88. border.right: 8
  89. border.top:8
  90. border.bottom: 8
  91. anchors.fill: track
  92. anchors.margins: -1
  93. anchors.topMargin: -2
  94. anchors.leftMargin: -2
  95. }
  96. Image {
  97. id: handle
  98. anchors.verticalCenter: parent.verticalCenter
  99. smooth: true
  100. source: "../images/slider_handle.png"
  101. x: updatePos() - 8
  102. z: 1
  103. }
  104. MouseArea {
  105. id: mouseArea
  106. anchors {left: parent.left; right: parent.right; verticalCenter: parent.verticalCenter}
  107. height: handle.height
  108. width: handle.width
  109. preventStealing: true
  110. onPressed: {
  111. var handleX = Math.max(0, Math.min(mouseX, mouseArea.width))
  112. var realValue = (maximum - minimum) * handleX / mouseArea.width + minimum;
  113. value = colorSlider.integer ? Math.round(realValue) : realValue;
  114. }
  115. onPositionChanged: {
  116. if (pressed) {
  117. var handleX = Math.max(0, Math.min(mouseX, mouseArea.width))
  118. var realValue = (maximum - minimum) * handleX / mouseArea.width + minimum;
  119. value = colorSlider.integer ? Math.round(realValue) : realValue;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }