| 1 |
// SPDX-FileCopyrightText: 2018 James Turner |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
import QtQuick 2.4 |
| 5 |
import FlightGear 1.0 |
| 6 |
|
| 7 |
Item { |
| 8 |
id: root |
| 9 |
property alias label: labelText.text |
| 10 |
|
| 11 |
property int min: 0 |
| 12 |
property int max: 100 |
| 13 |
property int value: 50 |
| 14 |
|
| 15 |
property int sliderWidth: 0 |
| 16 |
|
| 17 |
readonly property real __percentFull: value / (max - min) |
| 18 |
|
| 19 |
implicitHeight: labelText.height |
| 20 |
implicitWidth: Style.strutSize * 5 |
| 21 |
|
| 22 |
Text { |
| 23 |
id: labelText |
| 24 |
width: parent.width - (emptyTrack.width + Style.margin) |
| 25 |
horizontalAlignment: Text.AlignRight |
| 26 |
font.pixelSize: Style.baseFontPixelSize |
| 27 |
} |
| 28 |
|
| 29 |
Rectangle { |
| 30 |
id: emptyTrack |
| 31 |
|
| 32 |
width: sliderWidth > 0 ? sliderWidth : parent.width - (labelText.implicitWidth + Style.margin) |
| 33 |
|
| 34 |
anchors.right: parent.right |
| 35 |
anchors.verticalCenter: parent.verticalCenter |
| 36 |
|
| 37 |
height: 2 |
| 38 |
color: Style.inactiveThemeColor |
| 39 |
|
| 40 |
Rectangle { |
| 41 |
id: fullTrack |
| 42 |
height: parent.height |
| 43 |
color: Style.frameColor |
| 44 |
width: parent.width * __percentFull |
| 45 |
} |
| 46 |
|
| 47 |
MouseArea { |
| 48 |
id: clickTrackArea |
| 49 |
anchors.centerIn: parent |
| 50 |
width: parent.width |
| 51 |
height: root.height |
| 52 |
|
| 53 |
onClicked: { |
| 54 |
var frac = mouse.x / width; |
| 55 |
root.value = min + 0.5 + (max - min) * frac; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// invisible item that moves directly with the mouse |
| 60 |
Item { |
| 61 |
id: dragThumb |
| 62 |
height: 2 |
| 63 |
width: 2 |
| 64 |
|
| 65 |
anchors.verticalCenter: parent.verticalCenter |
| 66 |
|
| 67 |
Drag.active: thumbMouse.drag.active |
| 68 |
|
| 69 |
onXChanged: { |
| 70 |
if (thumbMouse.drag.active) { |
| 71 |
var frac = x / emptyTrack.width; |
| 72 |
value = min + (max - min) * frac; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
Rectangle { |
| 78 |
id: thumb |
| 79 |
width: radius * 2 |
| 80 |
height: radius * 2 |
| 81 |
radius: Style.roundRadius |
| 82 |
color: Style.themeColor |
| 83 |
|
| 84 |
anchors.verticalCenter: parent.verticalCenter |
| 85 |
x: parent.width * __percentFull |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
MouseArea { |
| 90 |
id: thumbMouse |
| 91 |
hoverEnabled: true |
| 92 |
anchors.fill: parent |
| 93 |
|
| 94 |
drag.axis: Drag.XAxis |
| 95 |
drag.minimumX: 0 |
| 96 |
drag.maximumX: emptyTrack.width |
| 97 |
drag.target: dragThumb |
| 98 |
|
| 99 |
onPressed: { |
| 100 |
dragThumb.x = emptyTrack.width * __percentFull |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
} // of base track rectangle |
| 107 |
|
| 108 |
} |