Debian Codegraph @

src/ui/SoundSlider.qml

raw
1 import QtQuick 2.4
2 import QtQuick.Controls 2.0
3
4 import sfxr 1.0
5
6 Slider {
7 id: root
8 property Sound sound
9 property string soundProperty
10 property bool bipolar: false
11
12 // For some reason setting from to -1 does not work anymore: the handle stays
13 // stuck in the middle of the Slider (Qt 5.9.5 and Qt 5.11.1). To workaround
14 // this, we shift the value range by 1
15 // from: bipolar ? -1 : 0
16 to: bipolar ? 2 : 1
17
18 // Do not use stepSize: it draws too many ticks
19 property real step: (to - from) / 200
20 property real shiftMultiplier: 4
21 value: sliderValueForSoundValue(sound[soundProperty])
22
23 onValueChanged: {
24 updateTimer.start();
25 }
26
27 // Delay the update of the sound property to avoid glitchy sounds when
28 // moving the slider continuously
29 Timer {
30 id: updateTimer
31 interval: 200
32 onTriggered: {
33 root.sound[soundProperty] = soundValueForSliderValue(root.value);
34 }
35 }
36
37 MouseArea {
38 anchors.fill: parent
39 acceptedButtons: Qt.RightButton
40 onClicked: {
41 root.value = sliderValueForSoundValue(0);
42 }
43 onWheel: {
44 var delta = 0;
45 if (wheel.angleDelta.y < 0) {
46 delta = -step;
47 } else if (wheel.angleDelta.y > 0) {
48 delta = step;
49 }
50 if (wheel.modifiers & Qt.ShiftModifier) {
51 delta *= shiftMultiplier;
52 }
53 root.value += delta;
54 }
55 }
56
57 Rectangle {
58 width: 1
59 height: parent.height
60 z: -2
61 visible: bipolar
62 x: parent.width / 2
63 color: "#ccc"
64 }
65
66 function soundValueForSliderValue(v) {
67 return bipolar ? (v - 1) : v;
68 }
69
70 function sliderValueForSoundValue(v) {
71 return bipolar ? (v + 1) : v;
72 }
73 }