| 1 |
/* |
| 2 |
* Audacity: A Digital Audio Editor |
| 3 |
*/ |
| 4 |
import QtQuick 2.15 |
| 5 |
import QtQuick.Controls 2.15 |
| 6 |
|
| 7 |
import Muse.Ui 1.0 |
| 8 |
import Muse.UiComponents 1.0 |
| 9 |
|
| 10 |
Dial { |
| 11 |
id: root |
| 12 |
|
| 13 |
property alias navigation: navCtrl |
| 14 |
|
| 15 |
property real radius: 16 |
| 16 |
property real backgroundHeight: radius + radius * Math.sin(prv.startAngle) |
| 17 |
|
| 18 |
property bool isBalanceKnob: false |
| 19 |
|
| 20 |
property bool accentControl: true |
| 21 |
|
| 22 |
property alias mouseArea: mouseArea |
| 23 |
|
| 24 |
implicitWidth: root.radius * 2 |
| 25 |
implicitHeight: implicitWidth |
| 26 |
|
| 27 |
width: implicitWidth |
| 28 |
height: width |
| 29 |
|
| 30 |
wheelEnabled: true |
| 31 |
|
| 32 |
from: 0 |
| 33 |
to: 1 |
| 34 |
value: 0 |
| 35 |
|
| 36 |
signal newValueRequested(real newValue) |
| 37 |
signal increaseRequested() |
| 38 |
signal decreaseRequested() |
| 39 |
signal mouseEntered() |
| 40 |
signal mouseExited() |
| 41 |
|
| 42 |
QtObject { |
| 43 |
id: prv |
| 44 |
|
| 45 |
readonly property bool reversed: root.isBalanceKnob ? root.angle < 0 : false |
| 46 |
|
| 47 |
readonly property real handlerHeight: 8 |
| 48 |
readonly property real handlerWidth: 2 |
| 49 |
|
| 50 |
readonly property real outerArcLineWidth: 3 |
| 51 |
readonly property real innerArcLineWidth: 2 |
| 52 |
|
| 53 |
readonly property real startAngle: -140 * (Math.PI/180) - Math.PI/2 |
| 54 |
readonly property real endAngle: 140 * (Math.PI/180) - Math.PI/2 |
| 55 |
|
| 56 |
readonly property color valueArcColor: accentControl ? ui.theme.accentColor : Utils.colorWithAlpha(ui.theme.fontPrimaryColor, 0.3) |
| 57 |
readonly property color outerArcColor: Utils.colorWithAlpha(ui.theme.buttonColor, 0.7) |
| 58 |
readonly property color innerArcColor: Utils.colorWithAlpha(ui.theme.fontPrimaryColor, 0.5) |
| 59 |
|
| 60 |
readonly property real startValueArcAngle: root.isBalanceKnob ? 0 : -140 |
| 61 |
|
| 62 |
property real initialValue: 0 |
| 63 |
property real dragStartX: 0 |
| 64 |
property real dragStartY: 0 |
| 65 |
property bool dragActive: false |
| 66 |
|
| 67 |
function requestNewValue(newValue) { |
| 68 |
newValue = Math.max(root.from, Math.min(newValue, root.to)) |
| 69 |
|
| 70 |
if (newValue === root.value) { |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
root.newValueRequested(newValue) |
| 75 |
} |
| 76 |
|
| 77 |
onValueArcColorChanged: { backgroundCanvas.requestPaint() } |
| 78 |
onOuterArcColorChanged: { backgroundCanvas.requestPaint() } |
| 79 |
onInnerArcColorChanged: { backgroundCanvas.requestPaint() } |
| 80 |
} |
| 81 |
|
| 82 |
NavigationControl { |
| 83 |
id: navCtrl |
| 84 |
name: root.objectName != "" ? root.objectName : "KnobControl" |
| 85 |
enabled: root.enabled && root.visible |
| 86 |
|
| 87 |
accessible.role: MUAccessible.Range |
| 88 |
accessible.visualItem: root |
| 89 |
|
| 90 |
accessible.value: root.value |
| 91 |
accessible.minimumValue: root.from |
| 92 |
accessible.maximumValue: root.to |
| 93 |
accessible.stepSize: root.stepSize |
| 94 |
|
| 95 |
onNavigationEvent: function(event) { |
| 96 |
switch(event.type) { |
| 97 |
case NavigationEvent.Left: |
| 98 |
prv.requestNewValue(root.value - root.stepSize) |
| 99 |
event.accepted = true |
| 100 |
break |
| 101 |
case NavigationEvent.Right: |
| 102 |
prv.requestNewValue(root.value + root.stepSize) |
| 103 |
event.accepted = true |
| 104 |
break |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
background: Canvas { |
| 110 |
id: backgroundCanvas |
| 111 |
|
| 112 |
width: root.radius * 2 |
| 113 |
height: width |
| 114 |
|
| 115 |
antialiasing: true |
| 116 |
|
| 117 |
NavigationFocusBorder { |
| 118 |
navigationCtrl: navCtrl |
| 119 |
} |
| 120 |
|
| 121 |
onPaint: { |
| 122 |
var ctx = backgroundCanvas.context |
| 123 |
|
| 124 |
if (!ctx) { |
| 125 |
ctx = getContext("2d") |
| 126 |
ctx.lineCap = "squared" |
| 127 |
} |
| 128 |
|
| 129 |
ctx.clearRect(0, 0, canvasSize.width, canvasSize.height) |
| 130 |
ctx.lineWidth = prv.outerArcLineWidth |
| 131 |
|
| 132 |
ctx.strokeStyle = prv.outerArcColor |
| 133 |
ctx.beginPath() |
| 134 |
ctx.arc(width/2, height/2, root.radius - prv.outerArcLineWidth/2, prv.startAngle, prv.endAngle, false) |
| 135 |
ctx.stroke() |
| 136 |
|
| 137 |
ctx.strokeStyle = prv.valueArcColor |
| 138 |
ctx.beginPath() |
| 139 |
ctx.arc(width/2, height/2, root.radius - prv.outerArcLineWidth/2, prv.startValueArcAngle * (Math.PI/180) - Math.PI/2, root.angle * (Math.PI/180) - Math.PI/2, prv.reversed) |
| 140 |
ctx.stroke() |
| 141 |
|
| 142 |
ctx.lineWidth = prv.innerArcLineWidth |
| 143 |
ctx.strokeStyle = prv.innerArcColor |
| 144 |
ctx.beginPath() |
| 145 |
ctx.arc(width/2, height/2, root.radius - (prv.outerArcLineWidth + prv.innerArcLineWidth/2), 0, Math.PI * 2, false) |
| 146 |
ctx.stroke() |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
handle: Rectangle { |
| 151 |
x: root.radius - prv.handlerWidth / 2 |
| 152 |
y: prv.outerArcLineWidth + prv.innerArcLineWidth + 2 |
| 153 |
|
| 154 |
height: prv.handlerHeight |
| 155 |
width: prv.handlerWidth |
| 156 |
radius: prv.handlerWidth / 2 |
| 157 |
|
| 158 |
color: ui.theme.fontPrimaryColor |
| 159 |
antialiasing: true |
| 160 |
|
| 161 |
transformOrigin: Item.Bottom |
| 162 |
rotation: root.angle |
| 163 |
} |
| 164 |
|
| 165 |
onValueChanged: { |
| 166 |
backgroundCanvas.requestPaint() |
| 167 |
} |
| 168 |
|
| 169 |
Component.onCompleted: { |
| 170 |
backgroundCanvas.requestPaint() |
| 171 |
} |
| 172 |
|
| 173 |
onMoved: { |
| 174 |
navigation.requestActiveByInteraction() |
| 175 |
|
| 176 |
newValueRequested(value) |
| 177 |
} |
| 178 |
|
| 179 |
MouseArea { |
| 180 |
id: mouseArea |
| 181 |
anchors.fill: parent |
| 182 |
hoverEnabled: true |
| 183 |
onDoubleClicked: { |
| 184 |
prv.requestNewValue(0) |
| 185 |
} |
| 186 |
|
| 187 |
// The MouseArea steals mouse press events from the slider. |
| 188 |
// There is really no way to prevent that. |
| 189 |
// (if you set mouse.accepted to false in the onPressed handler, |
| 190 |
// the MouseArea won't receive doubleClick events). |
| 191 |
// So we have to reimplement the dragging behaviour. |
| 192 |
// That gives us the opportunity to do it better than Qt. |
| 193 |
// We will allow both dragging vertically and horizontally. |
| 194 |
|
| 195 |
preventStealing: true // Don't let a Flickable steal the mouse |
| 196 |
|
| 197 |
onPressed: function(mouse) { |
| 198 |
prv.initialValue = root.value |
| 199 |
prv.dragStartX = mouse.x |
| 200 |
prv.dragStartY = mouse.y |
| 201 |
prv.dragActive = true |
| 202 |
} |
| 203 |
|
| 204 |
onEntered: { |
| 205 |
mouseEntered() |
| 206 |
} |
| 207 |
|
| 208 |
onExited: { |
| 209 |
// Consider the mouse is still inside, |
| 210 |
// if we are dragging outside |
| 211 |
if (!prv.dragActive) { |
| 212 |
mouseExited() |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
onReleased: { |
| 217 |
prv.dragActive = false |
| 218 |
if (!containsMouse) { |
| 219 |
mouseExited() |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
onPositionChanged: function(mouse) { |
| 224 |
if (prv.dragActive) { |
| 225 |
let dx = mouse.x - prv.dragStartX |
| 226 |
let dy = mouse.y - prv.dragStartY |
| 227 |
let dist = Math.sqrt(dx * dx + dy * dy) |
| 228 |
let sgn = (dy < dx) ? 1 : -1 |
| 229 |
let newValue = prv.initialValue + dist * root.stepSize * sgn |
| 230 |
prv.requestNewValue(newValue) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// We also listen for wheel events here, but for a different reason: |
| 235 |
// Qml Dial has a bug that it doesn't emit moved() when the value is changed through a wheel event. |
| 236 |
// So when we see a wheel event, we let the dial handle it, but we will account for emitting the signal. |
| 237 |
onWheel: function(wheel) { |
| 238 |
wheel.accepted = false |
| 239 |
Qt.callLater(function() { root.newValueRequested(Math.round(value)) }) |
| 240 |
} |
| 241 |
} |
| 242 |
} |