Draw the bandmap the way N1MM draws it

The bandmap was a list of frequencies and callsigns. It is now a frequency
scale with the stations written out beside it and a leader line from each
callsign back to where the station really is.

BandmapLayout does the placing and has no UI reference, so the rule is unit
tested: put the label centred on its frequency, and if it would cover the one
above, push it below instead. That is N1MM's CalculateOffset, and it is what
keeps three stations a hundred hertz apart readable.

The scale stops short of the top and bottom edges, or the first and last
frequency numbers come out cut in half.

VfoMarker draws the receiver as a bar as wide as the mode passes, rather than a
line. Only the receiver is fed: the radio does not report a transmit VFO or a
second radio yet, so VfoRole has the other two roles ready and nothing draws
them. Band-plan colouring of the scale is left out because the segments differ
by ITU region and there is no band-plan table to read them from.

Checked by running the program against a fake cluster node under Xvfb and
looking at the result: twelve spots, three of them stacked with fanned leaders.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-27 16:15:01 +00:00
co-authored by Claude Opus 5
parent 2b5ab54e94
commit b9f0166777
11 changed files with 443 additions and 48 deletions
+142
View File
@@ -0,0 +1,142 @@
using System.Globalization;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Nonemm.Core;
using Nonemm.Spotting;
namespace Nonemm.App.Windows;
/// Draws the band: a frequency scale down the left, the receiver and
/// transmitter as bars on it, and the stations as callsigns on the right with a
/// leader line back to where each one really is.
public sealed class BandmapView : Control
{
private const double ScaleX = 52;
private const double LabelX = 96;
private const double TickLength = 6;
private const double LabelHeight = 15;
private const double TextSize = 12;
private const double VfoWidth = 4;
/// The scale stops short of the top and bottom edges so the first and last
/// frequency numbers are not cut in half.
private const double Inset = 9;
private static readonly IBrush ReceiveBrush = new SolidColorBrush(Color.FromRgb(0x27, 0xAE, 0x60));
private static readonly IBrush TransmitBrush = new SolidColorBrush(Color.FromRgb(0xC0, 0x39, 0x2B));
private static readonly IBrush SecondBrush = new SolidColorBrush(Color.FromRgb(0xE6, 0x7E, 0x22));
private readonly Typeface typeface = new(new FontFamily("monospace"));
private IReadOnlyList<BandmapLabel> placed = [];
private BandmapScale scale = new(Bands.Band20M.Low, Bands.Band20M.High, 1);
public BandmapView() => Focusable = true;
public IReadOnlyList<Spot> Spots { get; set; } = [];
public IReadOnlyList<VfoMarker> Vfos { get; set; } = [];
public Frequency Low { get; set; } = Bands.Band20M.Low;
public Frequency High { get; set; } = Bands.Band20M.High;
/// How the callsign is coloured: the same scorer as the entry window.
public Func<Spot, IBrush> ColourOf { get; set; } = _ => Brushes.Gray;
/// The step between the frequency numbers down the scale.
public Frequency TickStep { get; set; } = Frequency.FromKilohertz(5);
public event EventHandler<Spot>? SpotPicked;
/// Clicking where no station is asks for the radio, not a callsign.
public event EventHandler<Frequency>? FrequencyPicked;
public override void Render(DrawingContext context)
{
scale = new BandmapScale(Low, High, Math.Max(1, Bounds.Height - (2 * Inset)));
IBrush ink = Foreground();
context.FillRectangle(Brushes.Transparent, new Rect(Bounds.Size));
DrawScale(context, ink);
DrawVfos(context);
placed = [.. BandmapLayout.Place(Spots, scale, LabelHeight)
.Select(l => l with { MarkY = l.MarkY + Inset, LabelY = l.LabelY + Inset })];
foreach (BandmapLabel label in placed)
{
DrawLabel(context, label, ink);
}
}
private void DrawScale(DrawingContext context, IBrush ink)
{
Pen line = new(Opaque(ink, 0.5), 1);
context.DrawLine(line, new Point(ScaleX, Inset), new Point(ScaleX, Inset + scale.Height));
foreach (Frequency tick in BandmapLayout.Ticks(scale, TickStep))
{
double y = Math.Round(scale.YFor(tick) + Inset) + 0.5;
context.DrawLine(line, new Point(ScaleX - TickLength, y), new Point(ScaleX, y));
FormattedText text = Text(
tick.Kilohertz.ToString("0.#", CultureInfo.InvariantCulture),
Opaque(ink, 0.6));
context.DrawText(text, new Point(ScaleX - TickLength - 4 - text.Width, y - (text.Height / 2)));
}
}
private void DrawVfos(DrawingContext context)
{
foreach (VfoMarker vfo in Vfos)
{
double top = scale.YFor(vfo.Low) + Inset;
double bottom = scale.YFor(vfo.High) + Inset;
// a narrow mode would be less than a pixel tall
double height = Math.Max(3, bottom - top);
context.FillRectangle(
BrushFor(vfo.Role),
new Rect(ScaleX - (VfoWidth / 2), top, VfoWidth, height));
}
}
private void DrawLabel(DrawingContext context, BandmapLabel label, IBrush ink)
{
IBrush colour = ColourOf(label.Spot);
context.DrawLine(
new Pen(Opaque(ink, 0.35), 1),
new Point(ScaleX + 2, label.MarkY),
new Point(LabelX - 2, label.LabelY + (LabelHeight / 2)));
context.DrawText(Text(label.Spot.Call.Text, colour), new Point(LabelX, label.LabelY));
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
Point at = e.GetPosition(this);
foreach (BandmapLabel label in placed)
{
if (at.X >= LabelX && at.Y >= label.LabelY && at.Y <= label.LabelY + LabelHeight)
{
SpotPicked?.Invoke(this, label.Spot);
return;
}
}
FrequencyPicked?.Invoke(this, scale.At(at.Y - Inset));
}
private FormattedText Text(string text, IBrush brush) =>
new(text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, TextSize, brush);
private IBrush Foreground() =>
this.FindResource("SystemControlForegroundBaseHighBrush") as IBrush ?? Brushes.Gray;
private static IBrush Opaque(IBrush ink, double opacity) =>
ink is ISolidColorBrush solid
? new SolidColorBrush(solid.Color, opacity)
: ink;
private static IBrush BrushFor(VfoRole role) => role switch
{
VfoRole.Transmit => TransmitBrush,
VfoRole.Second => SecondBrush,
_ => ReceiveBrush,
};
}