Debian Codegraph @

au4/src/projectscene/qml/Audacity/ProjectScene/trackspanel/audio/VolumePressureMeter.qml

raw
1 /*
2 * Audacity: A Digital Audio Editor
3 */
4 import QtQuick 2.15
5
6 import Muse.UiComponents 1.0
7 import Muse.Ui 1.0
8
9 Canvas {
10 id: root
11
12 enum Style {
13 Solid,
14 Gradient
15 }
16
17 property real currentVolumePressure: -60.0
18 property real minDisplayedVolumePressure: -60.0
19 property real maxDisplayedVolumePressure: 0.0
20
21 property real indicatorWidth
22 property bool showRuler: false
23 property int style: VolumePressureMeter.Style.Solid
24 property color meterColor: "#7689E6" // TODO: Use the track color
25
26 property bool isClipping: currentVolumePressure >= maxDisplayedVolumePressure
27
28 width: root.showRuler ? indicatorWidth + 20 : indicatorWidth
29
30 QtObject {
31 id: prv
32
33 property var gradient: null
34 readonly property int overloadHeight: 4
35
36 readonly property real indicatorHeight: root.height - prv.overloadHeight - 6
37
38
39 // value ranges
40 readonly property int fullValueRangeLength: root.maxDisplayedVolumePressure - root.minDisplayedVolumePressure
41 readonly property real heightPerUnit: (prv.indicatorHeight - prv.overloadHeight) / fullValueRangeLength
42
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 onUnitTextColorChanged: { prv.rulerNeedsPaint = true; root.requestPaint() }
53 onUnitTextFontChanged: { prv.rulerNeedsPaint = true; root.requestPaint() }
54
55 // strokes
56 readonly property real strokeHorizontalMargin: 2
57 readonly property real longStrokeHeight: 1
58 readonly property real longStrokeWidth: 5
59 readonly property color longStrokeColor: Utils.colorWithAlpha(ui.theme.fontPrimaryColor, 0.5)
60 readonly property real shortStrokeHeight: 1
61 readonly property real shortStrokeWidth: 2
62 readonly property color shortStrokeColor: Utils.colorWithAlpha(ui.theme.fontPrimaryColor, 0.3)
63
64 onLongStrokeColorChanged: { prv.rulerNeedsPaint = true; root.requestPaint() }
65 onShortStrokeColorChanged: { prv.rulerNeedsPaint = true; root.requestPaint() }
66
67 property bool rulerNeedsPaint: true
68 }
69
70 // Rounding up fullStep value to the predefined one,
71 // to avoid getting funny intervals like 3, or 7
72 function roundUpToFixedValue(value) {
73 // full and small step is
74 // a number units per each respective notch on the ruler
75 const steps = [
76 { fullStep: 1, smallStep: 0 },
77 { fullStep: 2, smallStep: 1 },
78 { fullStep: 5, smallStep: 1 },
79 { fullStep: 10, smallStep: 2 },
80 { fullStep: 20, smallStep: 4 },
81 { fullStep: 50, smallStep: 10 },
82 { fullStep: 100, smallStep: 20 }
83 ];
84
85 // Find the nearest full step
86 for (let i = 0; i < steps.length; i++) {
87 if (value <= steps[i].fullStep) {
88 return steps[i];
89 }
90 }
91
92 // Should not happen
93 return steps[steps.length - 1];
94 }
95
96 // Draws a rectangle rounded at the top, bottom or both
97 function drawRoundedRect(ctx, fillStyle, x, y, width, height, radius, roundedEdge) {
98 ctx.save();
99 ctx.fillStyle = fillStyle;
100 ctx.beginPath();
101
102 if (roundedEdge === "top" || roundedEdge === "both") {
103 ctx.moveTo(x + radius, y);
104 ctx.arcTo(x + width, y, x + width, y + radius, radius);
105 } else {
106 ctx.moveTo(x, y);
107 ctx.lineTo(x + width, y);
108 }
109
110 if (roundedEdge === "bottom" || roundedEdge === "both") {
111 ctx.lineTo(x + width, y + height - radius);
112 ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius);
113 ctx.lineTo(x + radius, y + height);
114 ctx.arcTo(x, y + height, x, y + height - radius, radius);
115 } else {
116 ctx.lineTo(x + width, y + height);
117 ctx.lineTo(x, y + height);
118 }
119
120 if (roundedEdge === "top" || roundedEdge === "both") {
121 ctx.lineTo(x, y + radius);
122 ctx.arcTo(x, y, x + radius, y, radius);
123 } else {
124 ctx.lineTo(x, y);
125 }
126
127 ctx.closePath();
128 ctx.fill();
129 ctx.restore();
130 }
131
132 function getMeterFillStyle(ctx) {
133 if (root.style === VolumePressureMeter.Style.Gradient) {
134 if (!prv.gradient) {
135 // Preparing the gradient to draw the volume pressure
136 prv.gradient = ctx.createLinearGradient(0, prv.indicatorHeight - prv.overloadHeight, 0, prv.overloadHeight)
137 prv.gradient.addColorStop(0.0, "#26E386")
138 prv.gradient.addColorStop(0.55, "#CBED41")
139 prv.gradient.addColorStop(0.80, "#FC8226")
140 }
141 return prv.gradient
142 }
143 return root.meterColor
144 }
145
146 function drawRuler(ctx, originHPos, originVPos) {
147 ctx.clearRect(indicatorWidth, 0, root.width - indicatorWidth, root.height)
148 ctx.font = prv.unitTextFont
149
150 // Minimal height of a single full step
151 const minimalFullStepHeight = 20;
152 // Number of full steps to draw
153 const fullStepCount = Math.ceil(root.height / minimalFullStepHeight);
154 // Number of units per full step
155 const unitsPerStep = root.maxDisplayedVolumePressure - root.minDisplayedVolumePressure;
156 // Calculating normalized full and small step value
157 const { fullStep, smallStep } = roundUpToFixedValue(unitsPerStep / fullStepCount);
158 // Number of small steps to draw
159 const smallStepCount = smallStep ? unitsPerStep / smallStep : 0
160
161 // Drawing small steps
162 for (let k = 1; k < smallStepCount; k++) {
163 const vPos = originVPos + prv.heightPerUnit * smallStep * k;
164 ctx.fillStyle = prv.shortStrokeColor
165 ctx.fillRect(originHPos, vPos,
166 prv.shortStrokeWidth,
167 prv.shortStrokeHeight)
168 }
169
170 // Drawing full steps
171 for (let j = 0; j <= fullStepCount; j++) {
172 const vPos = originVPos + prv.heightPerUnit * fullStep * j;
173 ctx.fillStyle = prv.longStrokeColor
174 ctx.fillRect(originHPos, vPos,
175 prv.longStrokeWidth,
176 prv.longStrokeHeight)
177
178 let textHPos = originHPos + prv.longStrokeWidth + prv.strokeHorizontalMargin
179 ctx.fillStyle = prv.unitTextColor
180 ctx.fillText(fullStep * j, textHPos, vPos + 2)
181 }
182
183 prv.rulerNeedsPaint = false
184 }
185
186 onPaint: {
187 var ctx = root.context
188
189 if (!ctx) {
190 ctx = getContext("2d")
191
192 if (ui.currentLanguageLayoutDirection() === Qt.RightToLeft) {
193 ctx.textAlign = "end"
194 } else {
195 ctx.textAlign = "start"
196 }
197 }
198
199 ctx.clearRect(0, 0, indicatorWidth, prv.indicatorHeight)
200
201 // Filling the background of the meter
202 drawRoundedRect(ctx, ui.theme.strokeColor, 0, 0, indicatorWidth, prv.indicatorHeight, 2, "both")
203
204 // Drawing the Overload indicator
205 const overloadStyle = root.isClipping ? "#EF476F" : ui.theme.buttonColor
206 drawRoundedRect(ctx, overloadStyle, 0, 0, indicatorWidth, prv.overloadHeight, 2, "top")
207
208 // Clamping the current volume pressure
209 const volumePressure = Math.max(minDisplayedVolumePressure,
210 Math.min(currentVolumePressure, maxDisplayedVolumePressure));
211 // calculating the current volume height depending on the panel height
212 const meterHeight = prv.heightPerUnit * (volumePressure - root.minDisplayedVolumePressure)
213
214 if (meterHeight > 0) {
215 // Drawing the volume pressure
216 drawRoundedRect(ctx, getMeterFillStyle(ctx),
217 0, root.height - 10 - meterHeight,
218 indicatorWidth, meterHeight,
219 /* radius */ 2, /* rounded edge */ "bottom")
220 }
221
222 if (prv.rulerNeedsPaint) {
223 var originVPos = prv.overloadHeight
224 var originHPos = indicatorWidth + prv.strokeHorizontalMargin
225
226 drawRuler(ctx, originHPos, originVPos)
227 }
228 }
229
230 onHeightChanged: {
231 // Gradient and the ruler need to be updated
232 prv.rulerNeedsPaint = true
233 prv.gradient = null
234 requestPaint();
235 }
236
237 onCurrentVolumePressureChanged: {
238 requestPaint()
239 }
240
241 Component.onCompleted: {
242 prv.rulerNeedsPaint = true
243 requestPaint()
244 }
245 }