-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubmeter.cpp
340 lines (289 loc) · 9.3 KB
/
submeter.cpp
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
/**
** This file is part of the CatRadio project.
** Copyright 2022 Gianfranco Sordetti IZ8EWD <iz8ewd@pianetaradio.it>.
**
** 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 3 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, see <http://www.gnu.org/licenses/>.
**/
#include "submeter.h"
#include <QPainter>
#include <math.h>
SubMeter::SubMeter(QWidget *parent) : QWidget(parent)
{
lineColor = QColor(Qt::black);
bgColor = QColor(Qt::white);
progressColor = QColor(Qt::green);
scaleColor = QColor(Qt::black);
//Default value
minValue = 0;
maxValue = 10;
gateValue = 10;
longStep = 5;
shortStep = 1;
precision = 0;
meterSWR = 0;
currentValue = 0;
peakValue = minValue;
peakFactor = 0.1;
peakHold = true;
}
void SubMeter::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QFont font;
font = painter.font();
font.setPointSize(font.pointSize() - 2);
painter.setFont(font);
drawMeter(&painter);
drawProgress(&painter);
if (peakHold) drawPeak(&painter);
drawScale(&painter);
}
void SubMeter::drawMeter(QPainter *painter)
{
painter->save();
QPen pen(lineColor, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
painter->setBrush(bgColor);
painter->drawRect(0, height()/3+2, width()-12, height()/3-4);
painter->restore();
}
void SubMeter::drawProgress(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(progressColor);
double length = width()-14;
double increment;
double initX, initXX;
if (meterSWR) increment = length / (10 * log10(maxValue));
else increment = length / (maxValue - minValue);
if (currentValue>gateValue)
{
if (currentValue > maxValue) currentValue = maxValue; //Trim value if overload scale
if (meterSWR) initX = 10 * log10(gateValue) * increment;
else initX = (gateValue - minValue) * increment;
QRect rect(1, height()/3+2+1, initX, height()/3-4-2);
painter->drawRect(rect);
//Red bar
if (meterSWR)
{
if (currentValue > maxValue) currentValue = maxValue; //Trim value if overload scale
initXX = 10 * (log10(currentValue) - log10(gateValue)) * increment;
}
else
{
if (currentValue > maxValue) currentValue = maxValue; //Trim value if overload scale
initXX = (currentValue - gateValue) * increment;
}
QRect rect2(initX+1, height()/3+2+1, initXX+1, height()/3-4-2);
painter->setBrush(Qt::red);
painter->drawRect(rect2);
}
else
{
if (meterSWR) initX = 10 * log10(currentValue) * increment;
else initX = (currentValue - minValue) * increment;
QRect rect(1, height()/3+2+1, initX, height()/3-4-2);
painter->drawRect(rect);
}
painter->restore();
}
void SubMeter::drawPeak(QPainter *painter)
{
double max, min;
double gate;
painter->save();
painter->setPen(Qt::NoPen);
max = maxValue;
min = minValue;
gate = gateValue;
if (meterSWR) min = 1; //SWR meter
double length = width()-14;
double increment;
double initX;
if (currentValue>=peakValue) peakValue = currentValue;
else peakValue = peakValue - peakFactor*(peakValue - currentValue);
if (peakValue>max) peakValue = max;
if (peakValue>gate) painter->setBrush(QColor(Qt::red));
else painter->setBrush(progressColor);
if (meterSWR)
{
increment = length / (10 * log10(maxValue));
initX = 10 * log10(peakValue) * increment;
}
else
{
increment = length / (max - min);
initX = (peakValue - min) * increment;
}
//initX = (peakValue - min) * increment;
QRect rect(initX - 2, height()/3+2+1, 2, height()/3-4-2);
painter->drawRect(rect);
painter->restore();
}
void SubMeter::drawScale(QPainter *painter)
{
painter->save();
painter->setPen(scaleColor);
double initX = 0;
double initTopY = height()*2/3-2;
double length = width()-12;
double increment;
int longLineLen = 6;
int shortLineLen = 3;
QFontMetrics meterFont(painter->font());
double textHeight = meterFont.height();
if (meterSWR) increment = length / (10 * log10(maxValue));
else increment = length / (maxValue - minValue);
if (meterSWR) //Draw SWR meter with log scale
{
double j = 1.0;
for (int i = 0; i <= 10; i++) //1.0 to 2.0
{
QPointF topPot = QPointF(initX, initTopY);
QPointF bottomPot;
if (abs(j - 1.5) < 0.01 || abs(j - 2.0) < 0.01)
{
bottomPot = QPointF(initX, initTopY + longLineLen);
QString strValue = QString::number(j, 'f', 1);
double textWidth = fontMetrics().horizontalAdvance(strValue);
QPointF textPot = QPointF(initX - textWidth / 2, initTopY + textHeight + longLineLen - 2);
painter->drawText(textPot, strValue);
}
else bottomPot = QPointF(initX, initTopY + shortLineLen);
painter->drawLine(topPot, bottomPot);
j +=0.1;
initX = 10 * log10(j) * increment;
}
for (int i = 11; i <= 25; i++) //2.1 to 3.5
{
QPointF topPot = QPointF(initX, initTopY);
QPointF bottomPot;
if (abs(j - 2.5) < 0.01 || abs(j - 3.0) < 0.01 || abs(j - 3.5) < 0.01)
{
bottomPot = QPointF(initX, initTopY + longLineLen);
QString strValue = QString::number(j, 'f', 1);
double textWidth = fontMetrics().horizontalAdvance(strValue);
QPointF textPot = QPointF(initX - textWidth / 2, initTopY + textHeight + longLineLen - 2);
painter->drawText(textPot, strValue);
painter->drawLine(topPot, bottomPot);
}
j +=0.1;
initX = 10 * log10(j) * increment;
}
}
else //Draw scale and scale values based on range values
{
int stepNumber = (maxValue - minValue) / shortStep;
for (int i = 0; i <= stepNumber; i++)
{
double j = i * shortStep + minValue;
if (fmod(j,longStep) == 0)
{
if (j == minValue) //Do not draw the first value
{
initX += increment * shortStep;
continue;
}
QPointF topPot = QPointF(initX, initTopY);
QPointF bottomPot = QPointF(initX, initTopY + longLineLen);
painter->drawLine(topPot, bottomPot);
QString strValue = QString("%1").arg(j, 0, 'f', precision);
double textWidth = fontMetrics().horizontalAdvance(strValue);
QPointF textPot = QPointF(initX - textWidth / 2, initTopY + textHeight + longLineLen - 2);
painter->drawText(textPot, strValue);
}
else
{
QPointF topPot = QPointF(initX, initTopY);
QPointF bottomPot = QPointF(initX, initTopY + shortLineLen);
painter->drawLine(topPot, bottomPot);
}
initX += increment * shortStep;
}
}
painter->restore();
}
void SubMeter::setMinValue(double value)
{
minValue = value;
update();
}
void SubMeter::setMaxValue(double value)
{
maxValue = value;
update();
}
void SubMeter::setGateValue(double value)
{
gateValue = value;
}
void SubMeter::setLongStep(double value)
{
longStep = value;
update();
}
void SubMeter::setShortStep(double value)
{
shortStep = value;
update();
}
void SubMeter::setPrecision(int value)
{
precision = value;
update();
}
void SubMeter::setBgColor(QColor color)
{
bgColor = color;
}
void SubMeter::setLineColor(QColor color)
{
lineColor = color;
}
void SubMeter::setProgressColor(QColor color)
{
progressColor = color;
}
void SubMeter::setScaleColor(QColor color)
{
scaleColor = color;
}
void SubMeter::setValue(double value)
{
currentValue = value;
update();
}
void SubMeter::setValue(int value)
{
setValue(double(value));
}
void SubMeter::setMeterSWR(bool swr)
{
meterSWR = swr;
}
void SubMeter::setPeak(bool Peak)
{
peakHold = Peak;
}
void SubMeter::setPeakFactor(double factor)
{
peakFactor = factor;
}
void SubMeter::resetPeakValue()
{
if (!meterSWR) peakValue = minValue;
else peakValue = 1.0;
}