import QtQuick 2.0
Item {
    id: root
    property bool mirrorSlider: false
    property real minValue: 0
    property real maxValue: 100
    property real value: 50
    transform: Rotation {
        angle: mirrorSlider ? 180 : 0
        axis { x: 0; y: 1; z: 0 }
        origin { x: root.width/2; y: root.height/2 }
    }
    ListView {
        id: view
        anchors.fill: parent
        anchors.rightMargin: handle.width
        rotation: 180 
        orientation: Qt.Vertical
        interactive: false
        property int itemHeight: 7
        model: height/itemHeight
        currentIndex: (root.value - root.minValue) / (root.maxValue - root.minValue) * view.count
        onCurrentItemChanged: if (currentItem) handle.currentItemY = currentItem.y
        delegate: Item {
            width: view.width
            height: view.itemHeight
            property int entry: index
            Rectangle {
                property bool active: view.currentIndex >= index
                anchors.right: parent.right
                width: parent.width
                height: parent.height - 3
                color: Qt.hsva((index/view.count)/3, active ? 1.0 : 0.5, active ? 1.0 : 0.75);
                scale: active ? 1.0 : 0.9
            }
        }
        Rectangle {
            id: handle
            property real currentItemY: view.currentItem ? view.currentItem.y : 0
            anchors.right: parent.left
            width: 10
            height: width
            radius: 5
            color: "darkgray"
            y: currentItemY - height / 2 + 3
            rotation: root.mirrorSlider ? -180 : 180
        }
        MouseArea {
            id: dragArea
            anchors.fill: view
            hoverEnabled: false
            preventStealing: true
            onClicked: updateCurrentIndex(mouse.x, mouse.y)
            onPositionChanged: updateCurrentIndex(mouse.x, mouse.y)
            function updateCurrentIndex(x, y) {
                var item = view.itemAt(x, y);
                if (item) {
                    var newValue = item.entry / view.count * (root.maxValue - root.minValue) + root.minValue;
                    root.value = Math.min(root.maxValue, Math.max(root.minValue, newValue));
                }
            }
        }
    }
}