summaryrefslogtreecommitdiff
path: root/map65/plotter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'map65/plotter.cpp')
-rw-r--r--map65/plotter.cpp310
1 files changed, 261 insertions, 49 deletions
diff --git a/map65/plotter.cpp b/map65/plotter.cpp
index ab53a4ef..6397222b 100644
--- a/map65/plotter.cpp
+++ b/map65/plotter.cpp
@@ -1,11 +1,18 @@
#include "plotter.h"
#include <math.h>
+#include <algorithm>
#include <QDebug>
+#include <QFont>
+#include <QFontMetrics>
+#include <QPainter>
#include <fstream>
#include <iostream>
+#include <iterator>
#define MAX_SCREENSIZE 2048
+extern int g_sampleRate;
+extern int g_activeNfft;
CPlotter::CPlotter(QWidget *parent) : //CPlotter Constructor
QFrame(parent)
@@ -34,7 +41,6 @@ CPlotter::CPlotter(QWidget *parent) : //CPlotter Constructor
m_fQSO = 125;
m_TXkHz = m_fQSO;
m_line = 0;
- m_fSample = 96000;
m_paintAllZoom = false;
m_TxDF=0;
m_bDecodeFinished=false;
@@ -42,6 +48,18 @@ CPlotter::CPlotter(QWidget *parent) : //CPlotter Constructor
CPlotter::~CPlotter() { } // Destructor
+void CPlotter::ensureWaterfallHistory(int rows)
+{
+ rows = std::max(0, rows);
+ int stride = std::max(0, g_activeNfft);
+ size_t size = static_cast<size_t>(stride) * static_cast<size_t>(rows);
+ if (m_waterfallStride != stride || m_waterfallRows != rows || m_zwf.size() != size) {
+ m_zwf.assign(size, 0);
+ m_waterfallStride = stride;
+ m_waterfallRows = rows;
+ }
+}
+
QSize CPlotter::minimumSizeHint() const
{
return QSize(50, 50);
@@ -59,14 +77,14 @@ void CPlotter::resizeEvent(QResizeEvent* ) //resizeEvent()
//if changed, resize pixmaps to new screensize
m_Size = size();
int w = m_Size.width();
- int h = (m_Size.height()-60)/2;
+ int h = std::max(0, (m_Size.height()-60)/2);
m_WaterfallPixmap = QPixmap(w,h);
m_ZoomWaterfallPixmap = QPixmap(w,h);
m_2DPixmap = QPixmap(w,h);
m_WaterfallPixmap.fill(Qt::black);
m_ZoomWaterfallPixmap.fill(Qt::black);
m_2DPixmap.fill(Qt::black);
- memset(m_zwf,0,32768*h);
+ ensureWaterfallHistory(h);
m_ScalePixmap = QPixmap(w,30);
m_ZoomScalePixmap = QPixmap(w,30); //(no change on resize...)
m_ScalePixmap.fill(Qt::white);
@@ -101,24 +119,34 @@ void CPlotter::paintEvent(QPaintEvent *) // paintEvent()
QRect source(0,0,w,30);
painter.drawPixmap(target,m_ZoomScalePixmap,source);
- float df=m_fSample/32768.0;
- int x0=16384 + (0.001*(m_ZoomStartFreq+m_fCal)+m_fQSO-m_nkhz+1.27046) * \
+ float df = float(g_sampleRate) / float(g_activeNfft);
+ int x0 = g_activeNfft/2 + (0.001*(m_ZoomStartFreq+m_fCal)+m_fQSO-m_nkhz+1.27046) * \
1000.0/df + 0.5;
QPainter painter1(&m_WaterfallPixmap);
QPainter painter2(&m_ZoomWaterfallPixmap);
for(int i=0; i<w; i++) { //Paint the top row
- painter2.setPen(m_ColorTbl[m_zwf[x0+i]]);
+ int idx = x0 + i;
+ unsigned char color = 0;
+ if (idx >= 0 && idx < m_waterfallStride && !m_zwf.empty()) {
+ color = m_zwf[static_cast<size_t>(idx)];
+ }
+ painter2.setPen(m_ColorTbl[color]);
painter2.drawPoint(i,0);
}
if(m_paintAllZoom or (x0 != x00 and x00 != -99)) {
// If new fQSO, paint all rows
int k=x0;
for(int j=1; j<h; j++) {
- k += 32768;
+ k += g_activeNfft;
if(x0 != x00 and x00 != -99) {
for(int i=0; i<w; i++) {
- painter2.setPen(m_ColorTbl[m_zwf[i+k]]);
+ int idx = k + i;
+ unsigned char color = 0;
+ if (idx >= 0 && static_cast<size_t>(idx) < m_zwf.size()) {
+ color = m_zwf[static_cast<size_t>(idx)];
+ }
+ painter2.setPen(m_ColorTbl[color]);
painter2.drawPoint(i,j);
}
}
@@ -169,6 +197,11 @@ void CPlotter::paintEvent(QPaintEvent *) // paintEvent()
QRect target2(0,h+60,w,h); // (x,y,width,height)
QRect source2(0,0,w,h);
painter.drawPixmap(target2,m_ZoomWaterfallPixmap,source2);
+ // Decoded-callsign overlay — rendered last so labels sit on top of
+ // the upper waterfall pixmap. WideGraph maintains the list lifecycle.
+ if (!m_decodeLabels.isEmpty()) {
+ paintDecodeLabels(painter);
+ }
m_paintEventBusy=false;
}
@@ -186,13 +219,17 @@ void CPlotter::draw(float s[], int i0, float splot[]) //draw()
m_i0=i0;
w = m_WaterfallPixmap.width();
h = m_WaterfallPixmap.height();
+ ensureWaterfallHistory(h);
double gain = pow(10.0,0.05*(m_plotGain+7));
//move current data down one line
//(must do this before attaching a QPainter object)
m_WaterfallPixmap.scroll(0,1,0,0,w,h);
m_ZoomWaterfallPixmap.scroll(0,1,0,0, w, h);
- memmove(&m_zwf[32768],m_zwf,32768*(h-1));
+ if (m_waterfallStride > 0 && m_waterfallRows > 1) {
+ size_t stride = static_cast<size_t>(m_waterfallStride);
+ memmove(&m_zwf[stride], m_zwf.data(), stride * static_cast<size_t>(m_waterfallRows - 1));
+ }
QPainter painter1(&m_WaterfallPixmap);
QPainter painter2D(&m_2DPixmap);
@@ -241,14 +278,17 @@ void CPlotter::draw(float s[], int i0, float splot[]) //draw()
}
}
- for(i=0; i<32768; i++) {
+ int rowBins = std::min(g_activeNfft, m_waterfallStride);
+ for (i = 0; i < rowBins; i++) {
y = 10.0*log10(splot[i]);
int y1 = 5.0*gain*(y + 30 - m_plotZero);
if (y1<0) y1=0;
if (y1>254) y1=254;
if (splot[i]>1.e29) y1=255;
m_hist2[y1]++;
- m_zwf[i]=y1;
+ if (!m_zwf.empty()) {
+ m_zwf[static_cast<size_t>(i)] = static_cast<unsigned char>(y1);
+ }
}
if(s[0]>1.0e29) m_line=0;
@@ -265,9 +305,9 @@ void CPlotter::draw(float s[], int i0, float splot[]) //draw()
void CPlotter::UTCstr()
{
int ihr,imin;
- if(datcom_.ndiskdat != 0) {
- ihr=datcom_.nutc/100;
- imin=(datcom_.nutc) % 100;
+ if(getNdiskdat() != 0) {
+ ihr=getNutc()/100;
+ imin=(getNutc()) % 100;
} else {
qint64 ms = QDateTime::currentMSecsSinceEpoch() % 86400000;
imin=ms/60000;
@@ -295,10 +335,17 @@ void CPlotter::DrawOverlay() //DrawOverlay()
painter0.setFont(Font);
painter0.setPen(Qt::black);
- m_binsPerPixel = m_nSpan * 32768.0/(w*0.001*m_fSample) + 0.5;
double FreqPerDiv=5.0;
- double df = m_binsPerPixel*0.001*m_fSample/32768.0;
- m_hdivs = w*df/FreqPerDiv + 0.9999;
+ double df = m_binsPerPixel * 0.001 * double(g_sampleRate) / double(g_activeNfft);
+ m_fSpan = w * df;
+
+ m_hdivs = int(w * df / FreqPerDiv + 0.9999);
+ // Clamp to array size
+ const int MAX_HDIVS = 482;
+ if (m_hdivs < 0) m_hdivs = 0;
+ if (m_hdivs > MAX_HDIVS) m_hdivs = MAX_HDIVS;
+
+
m_fSpan = w*df;
m_ScalePixmap.fill(Qt::white);
painter0.drawRect(0, 0, w, 30);
@@ -345,7 +392,7 @@ void CPlotter::DrawOverlay() //DrawOverlay()
painter0.setPen(pen0);
x = m_xClick;
painter0.drawLine(x,15,x,30);
- int x0=(16384-m_i0)/m_binsPerPixel;
+ int x0=((g_activeNfft/2) - m_i0)/m_binsPerPixel;
m_fGreen=(x-x0)*df;
x0 += (x0-x);
QPen pen3(Qt::red, 3);
@@ -369,8 +416,10 @@ void CPlotter::DrawOverlay() //DrawOverlay()
painter3.setPen(Qt::black);
FreqPerDiv=0.2;
- df = 0.001*m_fSample/32768.0;
- m_hdivs = 32768*df/FreqPerDiv + 0.9999;
+ df = 0.001f * float(g_sampleRate) / float(g_activeNfft);
+ m_hdivs = int(g_activeNfft * df / FreqPerDiv + 0.9999);
+ if (m_hdivs < 0) m_hdivs = 0;
+ if (m_hdivs > MAX_HDIVS) m_hdivs = MAX_HDIVS;
int nlabs=df*w/0.2 + 1.0;
m_ZoomScalePixmap.fill(Qt::white);
painter3.drawRect(0, 0, w, 30);
@@ -392,7 +441,7 @@ void CPlotter::DrawOverlay() //DrawOverlay()
m_HDivText[i]);
}
- df=m_fSample/32768.0;
+ df = float(g_sampleRate) / float(g_activeNfft);
if(m_bLockTxRx) m_TxDF=m_DF;
x = (m_DF + m_mode65*66*11025.0/4096.0 - m_ZoomStartFreq)/df;
QPen pen2(Qt::darkGreen, 3); //Mark top JT65B tone with dark Green tick
@@ -421,6 +470,7 @@ void CPlotter::DrawOverlay() //DrawOverlay()
void CPlotter::MakeFrequencyStrs() //MakeFrequencyStrs
{
+
float StartFreq = m_StartFreq;
float freq;
int i,j;
@@ -430,7 +480,7 @@ void CPlotter::MakeFrequencyStrs() //MakeFrequencyStrs
m_FreqUnits = 1;
FreqPerDiv = 200;
int w = m_WaterfallPixmap.width();
- float df=m_fSample/32768.0;
+ float df = float(g_sampleRate) / float(g_activeNfft);
StartFreq = -w*df/2;
int n=StartFreq/FreqPerDiv;
StartFreq=n*200;
@@ -520,10 +570,13 @@ int CPlotter::getPlotGain() //getPlotGain()
void CPlotter::SetCenterFreq(int f) //setCenterFreq()
{
// f is the integer kHz portion of cfreq, from Linrad packets
+// Store the true (un-snapped) center frequency so that pixel<->freq
+// conversions (FreqfromX, XfromFreq, setFQSO) are accurate. The old
+// 5 kHz snap distorted every click-to-frequency calculation and caused
+// the green tickmark to land several kHz away from where the user
+// clicked. Scale labels are rounded independently in MakeFrequencyStrs.
if(f<0) f=m_nkhz;
- int ns = (f+m_FreqOffset-0.5*m_fSpan)/5.0 + 0.5;
- double fs = 5*ns;
- m_CenterFreq = fs + 0.5*m_fSpan;
+ m_CenterFreq = f + m_FreqOffset;
}
qint64 CPlotter::centerFreq() //centerFreq()
@@ -575,9 +628,9 @@ void CPlotter::setFQSO(int x, bool bf) //setFQSO()
m_xClick=x;
}
if(m_bLockTxRx) m_TXkHz=m_fQSO;
- m_TXfreq = floor(datcom_.fcenter) + 0.001*m_TXkHz;
- DrawOverlay();
- update();
+ m_TXfreq = floor(static_cast<int>(getFcenter())) + 0.001*m_TXkHz;
+ DrawOverlay();
+ update();
}
void CPlotter::setFcal(int n) //setFcal()
@@ -597,8 +650,15 @@ int CPlotter::DF() {return m_DF;} // get DF
void CPlotter::mousePressEvent(QMouseEvent *event) //mousePressEvent
{
int h = (m_Size.height()-60)/2;
- int x=event->x();
- int y=event->y();
+ int x;
+ int y;
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ x = event->position().x();
+ y = event->position().y();
+#else
+ x = event->pos().x();
+ y = event->pos().y();
+#endif
int button=event->button();
if(y < h+30) { // Wideband waterfall
if(button==1) {
@@ -608,11 +668,12 @@ void CPlotter::mousePressEvent(QMouseEvent *event) //mousePressEvent
if(x<0) x=0; // x is pixel number
if(x>m_Size.width()) x=m_Size.width();
m_TXkHz = int(FreqfromX(x)+0.5);
- m_TXfreq = floor(datcom_.fcenter) + 0.001*m_TXkHz;
+ m_TXfreq = floor(static_cast<int>(getFcenter())) + 0.001*m_TXkHz;
}
} else { // Zoomed waterfall
- if(button==1) m_DF=int(m_ZoomStartFreq + x*m_fSample/32768.0);
- if(button==2 and !m_bLockTxRx) m_TxDF=int(m_ZoomStartFreq + x*m_fSample/32768.0);
+ float df = float(g_sampleRate) / float(g_activeNfft);
+ if (button == 1) m_DF = int(m_ZoomStartFreq + x * df);
+ if (button == 2 && !m_bLockTxRx) m_TxDF = int(m_ZoomStartFreq + x * df);
if(m_bLockTxRx) m_TxDF=m_DF;
}
DrawOverlay();
@@ -623,14 +684,22 @@ void CPlotter::mouseDoubleClickEvent(QMouseEvent *event) //mouse2click
{
if(event->button()!=1) return; //Act only on left double-click
int h = (m_Size.height()-60)/2;
- int x=event->x();
- int y=event->y();
+ int x;
+ int y;
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ x = event->position().x();
+ y = event->position().y();
+#else
+ x = event->pos().x();
+ y = event->pos().y();
+#endif
if(y < h+30) {
m_DF=0;
setFQSO(x,false);
emit freezeDecode1(2); //### ???
} else {
- float f = m_ZoomStartFreq + x*m_fSample/32768.0;
+ float df = float(g_sampleRate) / float(g_activeNfft);
+ float f = m_ZoomStartFreq + x * df;
m_DF=int(f);
emit freezeDecode1(1);
DrawOverlay();
@@ -652,7 +721,7 @@ int CPlotter::autoZero() //autoZero()
int sum2=0;
for(int i=0; i<256; i++) {
sum2 += m_hist2[i];
- if(sum2 > 16384) {
+ if (sum2 > g_activeNfft/2) {
m_z2=i;
break;
}
@@ -667,8 +736,23 @@ int CPlotter::autoZero() //autoZero()
void CPlotter::setNSpan(int n) //setNSpan()
{
m_nSpan=n;
+// ui->widePlot->resetWaterfall();
}
+/*
+void CPlotter::resetWaterfall()
+{
+ int w = m_WaterfallPixmap.width();
+ int h = m_WaterfallPixmap.height();
+ m_WaterfallPixmap = QPixmap(w, h);
+ m_WaterfallPixmap.fill(Qt::black);
+
+ m_ScalePixmap = QPixmap(w, 30);
+ m_ScalePixmap.fill(Qt::white);
+
+ m_lastWaterfallLine = 0;
+}
+*/
void CPlotter::setPalette(QString palette) //setPalette()
{
if(palette=="Linrad") {
@@ -727,11 +811,6 @@ void CPlotter::setPalette(QString palette) //setPalette()
}
}
-void CPlotter::setFsample(int n)
-{
- m_fSample=n;
-}
-
void CPlotter::setMode65(int n)
{
m_mode65=n;
@@ -767,23 +846,156 @@ void CPlotter::setLockTxRx(bool b)
void CPlotter::mouseMoveEvent (QMouseEvent * event)
{
int h = m_WaterfallPixmap.height();
- int x=event->x();
- int y=event->y();
+ int x;
+ int y;
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ x = event->position().x();
+ y = event->position().y();
+#else
+ x = event->pos().x();
+ y = event->pos().y();
+#endif
bool lower=(y > 30+h);
float freq=FreqfromX(x);
- float df=m_fSample/32768.0;
- int ndf=x*df + m_ZoomStartFreq;
+ float df = float(g_sampleRate) / float(g_activeNfft);
+ int ndf = int(x * df + m_ZoomStartFreq + 0.5f);
if(lower) {
- QToolTip::showText(event->globalPos(),QString::number(ndf));
+ QToolTip::showText(
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ event->globalPosition().toPoint()
+#else
+ event->globalPos()
+#endif
+ , QString::number(ndf));
} else {
- QToolTip::showText(event->globalPos(),QString::number(freq,'f',3));
+ QToolTip::showText(
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ event->globalPosition().toPoint()
+#else
+ event->globalPos()
+#endif
+ , QString::number(freq, 'f', 3));
}
QWidget::mouseMoveEvent(event);
}
double CPlotter::rxFreq()
{
- return floor(datcom_.fcenter) + 0.001*m_fQSO + 0.000001*m_DF;
+ return floor(static_cast<int>(getFcenter())) + 0.001*m_fQSO + 0.000001*m_DF;
+}
+
+void CPlotter::setDecodeLabels(const QList<DecodeLabel>& labels)
+{
+ m_decodeLabels = labels;
+ update();
+}
+
+void CPlotter::setDecodeLabelAlpha(int alpha)
+{
+ if (alpha < 0) alpha = 0;
+ if (alpha > 255) alpha = 255;
+ if (m_decodeLabelAlpha == alpha) return;
+ m_decodeLabelAlpha = alpha;
+ update();
+}
+
+void CPlotter::setDecodeLabelFontSize(DecodeLabelFontSize sz)
+{
+ if (m_decodeFontSize == sz) return;
+ m_decodeFontSize = sz;
+ update();
+}
+
+void CPlotter::setDecodeLabelPosition(DecodeLabelPosition p)
+{
+ if (m_decodeLabelPosition == p) return;
+ m_decodeLabelPosition = p;
+ update();
+}
+
+void CPlotter::paintDecodeLabels(QPainter& painter)
+{
+ // Sort left-to-right so the stacking pass below assigns rows in
+ // order of x-position. Same pattern as the QMAP/WSJT-X overlay.
+ QList<DecodeLabel> sorted = m_decodeLabels;
+ std::sort(sorted.begin(), sorted.end(),
+ [this](const DecodeLabel& a, const DecodeLabel& b) {
+ return XfromFreq(static_cast<float>(a.freq_khz))
+ < XfromFreq(static_cast<float>(b.freq_khz));
+ });
+
+ QFont font("Arial", static_cast<int>(m_decodeFontSize), QFont::Bold);
+ painter.setFont(font);
+ QFontMetrics metrics(font);
+ const int row_height = metrics.height() + 1;
+
+ // Up to 5 stack rows — MAP65's wideband span typically shows fewer
+ // simultaneous decoded stations than FT8 so 5 is plenty.
+ constexpr int max_rows = 5;
+ // Upper-waterfall geometry mirrors QMAP: paint event uses y=30 for
+ // the top edge and height = (widget − 60)/2. Bottom edge = top + h.
+ constexpr int waterfall_top_y = 30;
+ const int waterfall_h = (m_Size.height() - 60) / 2;
+ const int waterfall_bottom_y = waterfall_top_y + waterfall_h;
+ const bool stack_from_bottom = (m_decodeLabelPosition == DecodeLabelPosition::Bottom);
+ int row_right_edge[max_rows];
+ for (int i = 0; i < max_rows; ++i) row_right_edge[i] = -1000;
+
+ for (const auto& l : sorted) {
+ const int x = XfromFreq(static_cast<float>(l.freq_khz));
+ const int text_w = metrics.horizontalAdvance(l.callsign);
+ const int rect_w = text_w + 4;
+ const int rect_x = x - rect_w / 2;
+
+ int row = 0;
+ for (; row < max_rows; ++row) {
+ if (rect_x > row_right_edge[row] + 4) break;
+ }
+ if (row >= max_rows) continue;
+ row_right_edge[row] = rect_x + rect_w;
+
+ // Top-anchor: row 0 at the top of the upper waterfall, stacks down.
+ // Bottom-anchor: row 0 just above the divider, stacks up.
+ const int y_top = stack_from_bottom
+ ? (waterfall_bottom_y - row_height * (row + 1))
+ : (waterfall_top_y + row_height * row);
+ const QRect rect(rect_x, y_top, rect_w, row_height);
+ // View-menu transparency selector.
+ // master == 255 ("None"): force every element to alpha 255,
+ // overriding the legacy semi-transparent
+ // background (180) and tick (200/220) so
+ // "None" really is no transparency.
+ // master < 255 (Medium/High): scale each element's base alpha
+ // proportionally so the relative
+ // layering is preserved as it fades.
+ const int master_alpha = m_decodeLabelAlpha;
+ auto applyAlpha = [master_alpha](QColor c) {
+ if (master_alpha >= 255) {
+ c.setAlpha(255);
+ } else {
+ c.setAlpha((c.alpha() * master_alpha) / 255);
+ }
+ return c;
+ };
+ painter.fillRect(rect, applyAlpha(QColor(0, 0, 0, 180)));
+ // DG2YCB 2026-05-13 round 2: orange+yellow read as "both red/orange"
+ // on his display. Move JT65 to cyan — opposite hue from Q65 yellow,
+ // still legible on the dark waterfall. Q65 stays yellow to match
+ // the QMAP wide-mode overlay.
+ const QColor mode_col = l.is_jt65 ? QColor( 0, 255, 255) // cyan (JT65)
+ : QColor(255, 255, 0); // yellow (Q65)
+ const QColor tick_col = l.is_jt65 ? QColor( 0, 255, 255, 220)
+ : QColor(255, 255, 0, 200);
+ painter.setPen(applyAlpha(tick_col));
+ // Tick points toward the visible spectrum side: down (toward the
+ // divider/signal trace below) when stacked from top; up (toward
+ // the freq scale at y=30) when stacked from bottom.
+ const int tick_y0 = stack_from_bottom ? y_top : y_top + row_height;
+ const int tick_y1 = stack_from_bottom ? y_top - 4 : y_top + row_height + 4;
+ painter.drawLine(x, tick_y0, x, tick_y1);
+ painter.setPen(applyAlpha(mode_col));
+ painter.drawText(rect, Qt::AlignHCenter | Qt::AlignVCenter, l.callsign);
+ }
}
double CPlotter::txFreq()