-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathlevelmeter.cpp
More file actions
439 lines (389 loc) · 15.2 KB
/
levelmeter.cpp
File metadata and controls
439 lines (389 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/******************************************************************************\
* Copyright (c) 2004-2026
*
* Author(s):
* Volker Fischer
*
* Description:
* Implements a multi color LED bar
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/
#include "levelmeter.h"
/* Implementation *************************************************************/
CLevelMeter::CLevelMeter ( QWidget* parent ) : QWidget ( parent ), eLevelMeterType ( MT_BAR_WIDE )
{
// initialize LED meter
QWidget* pLEDMeter = new QWidget();
QVBoxLayout* pLEDLayout = new QVBoxLayout ( pLEDMeter );
pLEDLayout->setAlignment ( Qt::AlignHCenter );
pLEDLayout->setContentsMargins ( 0, 0, 0, 0 );
pLEDLayout->setSpacing ( 0 );
// create LEDs plus the clip LED
vecpLEDs.Init ( NUM_LEDS_INCL_CLIP_LED );
for ( int iLEDIdx = NUM_LEDS_INCL_CLIP_LED - 1; iLEDIdx >= 0; iLEDIdx-- )
{
// create LED object
vecpLEDs[iLEDIdx] = new cLED ( parent );
// add LED to layout with spacer (do not add spacer on the bottom of the first LED)
if ( iLEDIdx < NUM_LEDS_INCL_CLIP_LED - 1 )
{
pLEDLayout->addStretch();
}
pLEDLayout->addWidget ( vecpLEDs[iLEDIdx]->GetLabelPointer() );
}
// initialize bar meter
pBarMeter = new QProgressBar();
pBarMeter->setOrientation ( Qt::Vertical );
pBarMeter->setRange ( 0, 100 * NUM_STEPS_LED_BAR ); // use factor 100 to reduce quantization (bar is continuous)
pBarMeter->setFormat ( "" ); // suppress percent numbers
// setup stacked layout for meter type switching mechanism
pMinStackedLayout = new CMinimumStackedLayout ( this );
pMinStackedLayout->addWidget ( pLEDMeter );
pMinStackedLayout->addWidget ( pBarMeter );
// according to QScrollArea description: "When using a scroll area to display the
// contents of a custom widget, it is important to ensure that the size hint of
// the child widget is set to a suitable value."
pBarMeter->setMinimumSize ( QSize ( 1, 1 ) );
pLEDMeter->setMinimumSize ( QSize ( 1, 1 ) );
// update the meter type (using the default value of the meter type)
SetLevelMeterType ( eLevelMeterType );
// setup clip indicator timer
TimerClip.setSingleShot ( true );
TimerClip.setInterval ( CLIP_IND_TIME_OUT_MS );
// Connections -------------------------------------------------------------
QObject::connect ( &TimerClip, &QTimer::timeout, this, &CLevelMeter::ClipReset );
}
CLevelMeter::~CLevelMeter()
{
// clean up the LED objects
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
{
delete vecpLEDs[iLEDIdx];
}
}
void CLevelMeter::SetLevelMeterType ( const ELevelMeterType eNType )
{
eLevelMeterType = eNType;
switch ( eNType )
{
case MT_LED_STRIPE:
// initialize all LEDs
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
{
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_BLACK );
}
pMinStackedLayout->setCurrentIndex ( 0 );
break;
case MT_LED_ROUND_BIG:
// initialize all LEDs
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
{
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_BIG_BLACK );
}
pMinStackedLayout->setCurrentIndex ( 0 );
break;
case MT_LED_ROUND_SMALL:
// initialize all LEDs
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
{
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_SMALL_BLACK );
}
pMinStackedLayout->setCurrentIndex ( 0 );
break;
case MT_BAR_WIDE:
case MT_BAR_NARROW:
pMinStackedLayout->setCurrentIndex ( 1 );
break;
}
// update bar meter style and reset clip state
SetBarMeterStyleAndClipStatus ( eNType, false );
}
void CLevelMeter::SetBarMeterStyleAndClipStatus ( const ELevelMeterType eNType, const bool bIsClip )
{
switch ( eNType )
{
case MT_BAR_NARROW:
if ( bIsClip )
{
pBarMeter->setStyleSheet ( "QProgressBar { border: 0px solid red;"
" margin: 0px;"
" padding: 0px;"
" width: 4px;"
" background: red; }"
"QProgressBar::chunk { background: green; }" );
}
else
{
pBarMeter->setStyleSheet ( "QProgressBar { border: 0px;"
" margin: 0px;"
" padding: 0px;"
" width: 4px; }"
"QProgressBar::chunk { background: green; }" );
}
break;
default: /* MT_BAR_WIDE */
if ( bIsClip )
{
pBarMeter->setStyleSheet ( "QProgressBar { border: 2px solid red;"
" margin: 1px;"
" padding: 1px;"
" width: 15px;"
" background: transparent; }"
"QProgressBar::chunk { background: green; }" );
}
else
{
pBarMeter->setStyleSheet ( "QProgressBar { margin: 1px;"
" padding: 1px;"
" width: 15px; }"
"QProgressBar::chunk { background: green; }" );
}
break;
}
}
void CLevelMeter::SetValue ( const double dValue )
{
switch ( eLevelMeterType )
{
case MT_LED_STRIPE:
// update state of all LEDs for current level value (except of the clip LED)
for ( int iLEDIdx = 0; iLEDIdx < NUM_STEPS_LED_BAR; iLEDIdx++ )
{
// set active LED color if value is above current LED index
if ( iLEDIdx < dValue )
{
// check which color we should use (green, yellow or red)
if ( iLEDIdx < YELLOW_BOUND_LED_BAR )
{
// green region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_GREEN );
}
else
{
if ( iLEDIdx < RED_BOUND_LED_BAR )
{
// yellow region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_YELLOW );
}
else
{
// red region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_RED );
}
}
}
else
{
// we use black LED for inactive state
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_BLACK );
}
}
break;
case MT_LED_ROUND_BIG:
// update state of all LEDs for current level value (except of the clip LED)
for ( int iLEDIdx = 0; iLEDIdx < NUM_STEPS_LED_BAR; iLEDIdx++ )
{
// set active LED color if value is above current LED index
if ( iLEDIdx < dValue )
{
// check which color we should use (green, yellow or red)
if ( iLEDIdx < YELLOW_BOUND_LED_BAR )
{
// green region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_BIG_GREEN );
}
else
{
if ( iLEDIdx < RED_BOUND_LED_BAR )
{
// yellow region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_BIG_YELLOW );
}
else
{
// red region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_BIG_RED );
}
}
}
else
{
// we use black LED for inactive state
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_BIG_BLACK );
}
}
break;
case MT_LED_ROUND_SMALL:
// update state of all LEDs for current level value (except of the clip LED)
for ( int iLEDIdx = 0; iLEDIdx < NUM_STEPS_LED_BAR; iLEDIdx++ )
{
// set active LED color if value is above current LED index
if ( iLEDIdx < dValue )
{
// check which color we should use (green, yellow or red)
if ( iLEDIdx < YELLOW_BOUND_LED_BAR )
{
// green region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_SMALL_GREEN );
}
else
{
if ( iLEDIdx < RED_BOUND_LED_BAR )
{
// yellow region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_SMALL_YELLOW );
}
else
{
// red region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_SMALL_RED );
}
}
}
else
{
// we use black LED for inactive state
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_ROUND_SMALL_BLACK );
}
}
break;
case MT_BAR_WIDE:
case MT_BAR_NARROW:
pBarMeter->setValue ( 100 * dValue );
break;
}
// clip indicator management (note that in case of clipping, i.e. full
// scale level, the value is above NUM_STEPS_LED_BAR since the minimum
// value of int16 is -32768 but we normalize with 32767 -> therefore
// we really only show the clipping indicator, if actually the largest
// value of int16 is used)
if ( dValue > NUM_STEPS_LED_BAR )
{
switch ( eLevelMeterType )
{
case MT_LED_STRIPE:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_RED );
break;
case MT_LED_ROUND_BIG:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_ROUND_BIG_RED );
break;
case MT_LED_ROUND_SMALL:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_ROUND_SMALL_RED );
break;
case MT_BAR_WIDE:
case MT_BAR_NARROW:
SetBarMeterStyleAndClipStatus ( eLevelMeterType, true );
break;
}
TimerClip.start();
}
}
void CLevelMeter::ClipReset()
{
// we manually want to reset the clipping indicator: stop timer and reset
// clipping indicator GUI element
TimerClip.stop();
switch ( eLevelMeterType )
{
case MT_LED_STRIPE:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_BLACK );
break;
case MT_LED_ROUND_BIG:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_ROUND_BIG_BLACK );
break;
case MT_LED_ROUND_SMALL:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_ROUND_SMALL_BLACK );
break;
case MT_BAR_WIDE:
case MT_BAR_NARROW:
SetBarMeterStyleAndClipStatus ( eLevelMeterType, false );
break;
}
}
CLevelMeter::cLED::cLED ( QWidget* parent ) :
BitmCubeLedBlack ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDBlack.png" ) ),
BitmCubeLedGreen ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDGreen.png" ) ),
BitmCubeLedYellow ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDYellow.png" ) ),
BitmCubeLedRed ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDRed.png" ) ),
BitmCubeRoundSmallLedBlack ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDBlackSmall.png" ) ),
BitmCubeRoundSmallLedGreen ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDGreenSmall.png" ) ),
BitmCubeRoundSmallLedYellow ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDYellowSmall.png" ) ),
BitmCubeRoundSmallLedRed ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDRedSmall.png" ) ),
BitmCubeRoundBigLedBlack ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDBlackBig.png" ) ),
BitmCubeRoundBigLedGreen ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDGreenBig.png" ) ),
BitmCubeRoundBigLedYellow ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDYellowBig.png" ) ),
BitmCubeRoundBigLedRed ( QString::fromUtf8 ( ":/png/LEDs/res/CLEDRedBig.png" ) )
{
// create LED label
pLEDLabel = new QLabel ( "", parent );
// set initial bitmap
pLEDLabel->setPixmap ( BitmCubeLedBlack );
eCurLightColor = RL_BLACK;
}
void CLevelMeter::cLED::SetColor ( const ELightColor eNewColor )
{
// only update LED if color has changed
if ( eNewColor != eCurLightColor )
{
switch ( eNewColor )
{
case RL_DISABLED:
// note that this is required for the compact channel mode
pLEDLabel->setPixmap ( QPixmap() );
break;
case RL_BLACK:
pLEDLabel->setPixmap ( BitmCubeLedBlack );
break;
case RL_GREEN:
pLEDLabel->setPixmap ( BitmCubeLedGreen );
break;
case RL_YELLOW:
pLEDLabel->setPixmap ( BitmCubeLedYellow );
break;
case RL_RED:
pLEDLabel->setPixmap ( BitmCubeLedRed );
break;
case RL_ROUND_SMALL_BLACK:
pLEDLabel->setPixmap ( BitmCubeRoundSmallLedBlack );
break;
case RL_ROUND_SMALL_GREEN:
pLEDLabel->setPixmap ( BitmCubeRoundSmallLedGreen );
break;
case RL_ROUND_SMALL_YELLOW:
pLEDLabel->setPixmap ( BitmCubeRoundSmallLedYellow );
break;
case RL_ROUND_SMALL_RED:
pLEDLabel->setPixmap ( BitmCubeRoundSmallLedRed );
break;
case RL_ROUND_BIG_BLACK:
pLEDLabel->setPixmap ( BitmCubeRoundBigLedBlack );
break;
case RL_ROUND_BIG_GREEN:
pLEDLabel->setPixmap ( BitmCubeRoundBigLedGreen );
break;
case RL_ROUND_BIG_YELLOW:
pLEDLabel->setPixmap ( BitmCubeRoundBigLedYellow );
break;
case RL_ROUND_BIG_RED:
pLEDLabel->setPixmap ( BitmCubeRoundBigLedRed );
break;
}
eCurLightColor = eNewColor;
}
}