File: meter.cpp

package info (click to toggle)
cutesdr 1.20-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,848 kB
  • sloc: cpp: 18,902; makefile: 21; sh: 5
file content (302 lines) | stat: -rw-r--r-- 9,280 bytes parent folder | download | duplicates (2)
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
//////////////////////////////////////////////////////////////////////
// meter.cpp: implementation of the CMeter class.
//
//  This class creates and draws an S meter widget
//
// History:
//	2013-10-02  Initial creation MSW
//	2014-07-11  Added Squelch threshold marker
//////////////////////////////////////////////////////////////////////

//==========================================================================================
// + + +   This Software is released under the "Simplified BSD License"  + + +
//Copyright 2013 Moe Wheatley. All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//	  conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//	  of conditions and the following disclaimer in the documentation and/or other materials
//	  provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Moe Wheatley ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Moe Wheatley OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Moe Wheatley.
//==========================================================================================
#include "gui/meter.h"
#include <stdlib.h>
#include <QDebug>

//////////////////////////////////////////////////////////////////////
// Local defines
//////////////////////////////////////////////////////////////////////
//ratio to total control width or height
#define CTRL_MARGIN 0.07		//left/right margin
#define CTRL_MAJOR_START 0.4	//top of major tic line
#define CTRL_MINOR_START 0.5	//top of minor tic line
#define CTRL_XAXIS_HEGHT 0.6	//vert position of horizontal axis
#define CTRL_NEEDLE_TOP 0.5		//vert position of top of needle triangle

#define MIN_DBM -121.0		//S1
#define MAX_DBM -13.0		//S9+60

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMeter::CMeter(QWidget *parent) :
	QFrame(parent)
{
	m_Overload = false;
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setFocusPolicy(Qt::StrongFocus);
	setAttribute(Qt::WA_PaintOnScreen,false);
	setAutoFillBackground(false);
	setAttribute(Qt::WA_OpaquePaintEvent, false);
	setAttribute(Qt::WA_NoSystemBackground, true);
	setMouseTracking ( true );

	m_Pixmap = QPixmap(0,0);
	m_OverlayPixmap = QPixmap(0,0);
	m_Size = QSize(0,0);
	m_Slevel = 0;
	m_dBm = -120;
	m_SquelchPos = 0;
}

CMeter::~CMeter()
{
}

//////////////////////////////////////////////////////////////////////
// Sizing interface
//////////////////////////////////////////////////////////////////////
QSize CMeter::minimumSizeHint() const
{
	return QSize(10, 10);
}

QSize CMeter::sizeHint() const
{
	return QSize(100, 30);
}


//////////////////////////////////////////////////////////////////////
// Called when screen size changes so must recalculate bitmaps
//////////////////////////////////////////////////////////////////////
void CMeter::resizeEvent(QResizeEvent* )
{
	if(!size().isValid())
		return;
	if( m_Size != size() )
	{	//if size changed, resize pixmaps to new screensize
		m_Size = size();
		m_OverlayPixmap = QPixmap(m_Size.width(), m_Size.height());
		m_OverlayPixmap.fill(Qt::black);
		m_Pixmap = QPixmap(m_Size.width(), m_Size.height());
		m_Pixmap.fill(Qt::black);
	}
	DrawOverlay();
	draw();
}


//////////////////////////////////////////////////////////////////////
// Slot called to update meter Receiver level position
//////////////////////////////////////////////////////////////////////
void CMeter::SetdBmLevel(TYPEREAL dbm, bool Overload)
{
	m_dBm = (int)dbm;
	m_Slevel = CalcPosFromdB(dbm);
	if(m_Overload != Overload)
	{
		m_Overload = Overload;
		DrawOverlay();
	}
	draw();
}


//////////////////////////////////////////////////////////////////////
// called to calculate meter position from a dB level
//////////////////////////////////////////////////////////////////////
int CMeter::CalcPosFromdB(TYPEREAL db)
{
qreal w = (qreal)m_Pixmap.width();
	w = w - 2.0*CTRL_MARGIN*w;	//width of meter scale in pixels
	if(db<MIN_DBM)
		db = MIN_DBM;
	if(db>MAX_DBM)
		db = MAX_DBM;
	if(db <= -73.0)
	{
		qreal div = w/14.0;
		return (int)( (db/6.0 + 121.0/6.0)*div + .5);
	}
	else
	{
		qreal div = w/14.0;
		return (int)( ( 8 + db/10.0 + 73.0/10.0 )*div + .5);
	}
}

//////////////////////////////////////////////////////////////////////
// called to set Squelch Threshold meter position from a dB level
//////////////////////////////////////////////////////////////////////
void CMeter::SetSquelchPos(TYPEREAL db)
{
	m_SquelchPos = CalcPosFromdB(db);
	DrawOverlay();
	draw();
}


//////////////////////////////////////////////////////////////////////
// Called by Qt when screen needs to be redrawn
//////////////////////////////////////////////////////////////////////
void CMeter::paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	painter.drawPixmap(0,0,m_Pixmap);
	return;
}

//////////////////////////////////////////////////////////////////////
// Called to update meter data display on the screen
//////////////////////////////////////////////////////////////////////
void CMeter::draw()
{
int w;
int h;
	if(m_Pixmap.isNull())
		return;
	//get/draw the 2D spectrum
	w = m_Pixmap.width();
	h = m_Pixmap.height();
	//first copy into 2Dbitmap the overlay bitmap.
	m_Pixmap = m_OverlayPixmap.copy(0,0,w,h);
	QPainter painter(&m_Pixmap);

	//DrawCurrent position indicator
	qreal hline = (qreal)h*CTRL_XAXIS_HEGHT;
	qreal marg = (qreal)w*CTRL_MARGIN;
	qreal ht = (qreal)h*CTRL_NEEDLE_TOP;
	qreal x = marg + m_Slevel;
	QPoint pts[3];
	pts[0].setX(x); pts[0].setY(ht);
	pts[1].setX(x-6); pts[1].setY(hline+6);
	pts[2].setX(x+6); pts[2].setY(hline+6);


	if(m_Overload)
		painter.setBrush(QBrush(Qt::red));
	else
		painter.setBrush(QBrush(Qt::yellow));
	painter.setOpacity(1.0);
	painter.drawPolygon(pts,3);

	//create Font to use for scales
	QFont Font("Arial");
	QFontMetrics metrics(Font);
	int y = (h)/3;
	Font.setPixelSize(y);
	Font.setWeight(QFont::Normal);
	painter.setFont(Font);

	painter.setPen(Qt::black);
	painter.setOpacity(1.0);
	m_Str.setNum(m_dBm);
	painter.drawText(marg, h-1, m_Str+" dBm" );
	update();
}

//////////////////////////////////////////////////////////////////////
// Called to draw an overlay bitmap containing graphics that
// does not need to be recreated every data update.
//////////////////////////////////////////////////////////////////////
void CMeter::DrawOverlay()
{
	if(m_OverlayPixmap.isNull())
		return;
	int w = m_OverlayPixmap.width();
	int h = m_OverlayPixmap.height();
	int x,y;
	QRect rect;
	QPainter painter(&m_OverlayPixmap);

	//fill background with gradient
	QLinearGradient gradient(0, 0, 0 ,h);
	if(m_Overload)
	{
		gradient.setColorAt(1, Qt::white);
		gradient.setColorAt(0, Qt::red);
	}
	else
	{
		gradient.setColorAt(1, Qt::cyan);
		gradient.setColorAt(0, Qt::blue);
	}
	painter.setBrush(gradient);
	painter.drawRect(0, 0, w, h);

	//Draw scale lines
	qreal marg = (qreal)w*CTRL_MARGIN;
	qreal hline = (qreal)h*CTRL_XAXIS_HEGHT;
	qreal majstart = (qreal)h*CTRL_MAJOR_START;
	qreal minstart = (qreal)h*CTRL_MINOR_START;
	qreal hstop = (qreal)w-marg;
	painter.setPen(QPen(Qt::white, 1,Qt::SolidLine));
	painter.drawLine( QLineF( marg, hline, hstop, hline) );
	qreal xpos = marg;
	for(x=0; x<15; x++)
	{
		if(x&1)	//minor tics
			painter.drawLine( QLineF(xpos, minstart, xpos, hline) );
		else
			painter.drawLine( QLineF(xpos, majstart, xpos, hline) );
		xpos += (hstop-marg)/14.0;
	}


	//draw scale text
	//create Font to use for scales
	QFont Font("Arial");
//	QFontMetrics metrics(Font);
	y = h/4;
	Font.setPixelSize(y);
	Font.setWeight(QFont::Normal);
	painter.setFont(Font);
	int rwidth = (int)((hstop-marg)/7.0);
	m_Str = "+60";
	rect.setRect(marg/2, 0, rwidth, majstart);
	for(x=1; x<=9; x+=2)
	{
		m_Str.setNum(x);
		painter.drawText(rect, Qt::AlignHCenter|Qt::AlignVCenter, m_Str);
		rect.translate( rwidth,0);
	}
	painter.setPen(QPen(Qt::red, 1,Qt::SolidLine));
	for(x=20; x<=60; x+=20)
	{
		m_Str = "+" + m_Str.setNum(x);
		painter.drawText(rect, Qt::AlignHCenter|Qt::AlignVCenter, m_Str);
		rect.translate( rwidth,0);
	}
	//Draw Squelch Threshold Position thingy
	painter.setPen(QPen(Qt::red, 2,Qt::SolidLine));
	painter.drawEllipse( marg + m_SquelchPos-1, majstart, 2, 2 );
}