| 1 |
/* |
| 2 |
* Audacity: A Digital Audio Editor |
| 3 |
*/ |
| 4 |
import QtQuick 2.15 |
| 5 |
|
| 6 |
import Muse.Ui 1.0 |
| 7 |
import Muse.UiComponents 1.0 |
| 8 |
|
| 9 |
Canvas { |
| 10 |
id: root |
| 11 |
|
| 12 |
property real currentVolumePressure: -60.0 |
| 13 |
property real minDisplayedVolumePressure: -60.0 |
| 14 |
property real maxDisplayedVolumePressure: 0.0 |
| 15 |
|
| 16 |
property real recentPeak: -60 |
| 17 |
property real maxPeak: -60 |
| 18 |
|
| 19 |
property bool showRuler: false |
| 20 |
|
| 21 |
property bool isClipping: currentVolumePressure >= maxDisplayedVolumePressure |
| 22 |
|
| 23 |
readonly property int overloadWidth: 4 |
| 24 |
|
| 25 |
width: parent.width |
| 26 |
height: root.showRuler ? prv.indicatorHeight + prv.unitsTextWidth : prv.indicatorHeight |
| 27 |
|
| 28 |
opacity: enabled ? 1.0 : ui.theme.itemOpacityDisabled |
| 29 |
|
| 30 |
QtObject { |
| 31 |
id: prv |
| 32 |
|
| 33 |
readonly property real indicatorWidth: root.width - root.overloadWidth |
| 34 |
readonly property real indicatorHeight: 6 |
| 35 |
|
| 36 |
// value ranges |
| 37 |
readonly property int fullValueRangeLength: root.maxDisplayedVolumePressure - root.minDisplayedVolumePressure |
| 38 |
readonly property real divisionPixels: prv.indicatorWidth / fullValueRangeLength |
| 39 |
|
| 40 |
readonly property real leftStepMargin: 4 |
| 41 |
|
| 42 |
readonly property color meterBackgroundColor: Utils.colorWithAlpha(ui.theme.strokeColor, 0.7) |
| 43 |
readonly property real unitsTextWidth: 12 |
| 44 |
readonly property color unitTextColor: Utils.colorWithAlpha(ui.theme.fontPrimaryColor, 0.8) |
| 45 |
readonly property string unitTextFont: { |
| 46 |
var pxSize = String('8px') |
| 47 |
var family = String('\'' + ui.theme.bodyFont.family + '\'') |
| 48 |
|
| 49 |
return pxSize + ' ' + family |
| 50 |
} |
| 51 |
|
| 52 |
onMeterBackgroundColorChanged: { root.requestPaint() } |
| 53 |
onUnitTextColorChanged: { root.requestPaint() } |
| 54 |
onUnitTextFontChanged: { root.requestPaint() } |
| 55 |
} |
| 56 |
|
| 57 |
function drawRuler(ctx, originHPos, originVPos) { |
| 58 |
ctx.clearRect(0, prv.indicatorHeight, root.width, root.height - prv.indicatorHeight) |
| 59 |
ctx.font = prv.unitTextFont |
| 60 |
|
| 61 |
var currentStrokeHPos = 0 |
| 62 |
var fullStep = 12 |
| 63 |
var division = prv.divisionPixels |
| 64 |
var negativeMargin = 8 |
| 65 |
|
| 66 |
for (var i = 0; i <= prv.fullValueRangeLength; i+=fullStep) { |
| 67 |
var value = String(root.minDisplayedVolumePressure + i) |
| 68 |
|
| 69 |
if (i === 0) { // first element |
| 70 |
value = root.minDisplayedVolumePressure + prv.leftStepMargin |
| 71 |
currentStrokeHPos = originHPos + negativeMargin |
| 72 |
} else if (i === fullStep) { // second element |
| 73 |
currentStrokeHPos = originHPos + division * fullStep |
| 74 |
} else { |
| 75 |
currentStrokeHPos += division * fullStep |
| 76 |
} |
| 77 |
|
| 78 |
ctx.save() |
| 79 |
|
| 80 |
ctx.fillStyle = prv.unitTextColor |
| 81 |
ctx.fillText(value, currentStrokeHPos - (value <= -10 ? negativeMargin : 2), originVPos) |
| 82 |
|
| 83 |
ctx.restore() |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
onPaint: { |
| 88 |
var ctx = root.context |
| 89 |
|
| 90 |
if (!ctx) { |
| 91 |
ctx = getContext("2d") |
| 92 |
} |
| 93 |
|
| 94 |
ctx.clearRect(0, 0, root.width, prv.indicatorHeight) |
| 95 |
|
| 96 |
var xPos = 0 |
| 97 |
var yPos = 0 |
| 98 |
|
| 99 |
ctx.fillStyle = prv.meterBackgroundColor |
| 100 |
ctx.fillRect(xPos, yPos, prv.indicatorWidth , prv.indicatorHeight) |
| 101 |
|
| 102 |
var isClipping = root.currentVolumePressure >= root.maxDisplayedVolumePressure |
| 103 |
var clipingColor = "#EF476F" |
| 104 |
|
| 105 |
ctx.fillStyle = isClipping ? clipingColor : ui.theme.strokeColor |
| 106 |
ctx.fillRect(xPos + prv.indicatorWidth, yPos, root.overloadWidth, prv.indicatorHeight) |
| 107 |
|
| 108 |
ctx.fillStyle = isClipping ? clipingColor : ui.theme.accentColor |
| 109 |
ctx.fillRect(xPos, yPos, prv.divisionPixels * (prv.fullValueRangeLength - Math.abs(root.currentVolumePressure)), prv.indicatorHeight) |
| 110 |
|
| 111 |
ctx.fillRect(xPos + root.recentPeak * root.width, yPos, 1, prv.indicatorHeight) |
| 112 |
|
| 113 |
ctx.fillStyle = prv.unitTextColor |
| 114 |
ctx.fillRect(xPos + root.maxPeak * root.width, yPos, 1, prv.indicatorHeight) |
| 115 |
|
| 116 |
if (root.showRuler) { |
| 117 |
yPos = root.height - 4 |
| 118 |
drawRuler(ctx, xPos, yPos) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
onCurrentVolumePressureChanged: { |
| 123 |
requestPaint() |
| 124 |
} |
| 125 |
|
| 126 |
onRecentPeakChanged: { |
| 127 |
requestPaint() |
| 128 |
} |
| 129 |
|
| 130 |
onMaxPeakChanged: { |
| 131 |
requestPaint() |
| 132 |
} |
| 133 |
|
| 134 |
Component.onCompleted: { |
| 135 |
requestPaint() |
| 136 |
} |
| 137 |
} |