Skip to content

Commit

Permalink
use the table's selection to track the selected entry, show marks for…
Browse files Browse the repository at this point in the history
… special entries in the first column
  • Loading branch information
ftl committed Aug 25, 2024
1 parent dab09c2 commit 005a236
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 42 deletions.
41 changes: 25 additions & 16 deletions core/bandmap/bandmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,38 @@ func (m *Bandmap) update() {
})
m.entries.emitEntryOnFrequency(entryOnFrequency, entryOnFrequencyAvailable)

nearestEntry, nearestEntryFound := m.nextVisibleEntry(m.activeFrequency, 0, func(entry core.BandmapEntry) bool {
return entry.Frequency != m.activeFrequency && entry.Source != core.WorkedSpot
})

bands := m.entries.Bands(m.activeBand, m.visibleBand)
entries := m.entries.Query(nil, m.entryVisible)
index := core.NewFrameIndex(entries)
frame := core.BandmapFrame{
Frequency: m.activeFrequency,
ActiveBand: m.activeBand,
VisibleBand: m.visibleBand,
Mode: m.activeMode,
Bands: bands,
Entries: entries,
Index: index,
NearestEntry: nearestEntry,
RevealNearestEntry: nearestEntryFound,
Frequency: m.activeFrequency,
ActiveBand: m.activeBand,
VisibleBand: m.visibleBand,
Mode: m.activeMode,
Bands: bands,
Entries: entries,
Index: index,
}

selectedEntry, selected := m.entries.SelectedEntry()
if selected && m.entryVisible(selectedEntry) {
if selected && m.entryVisible(selectedEntry) && selectedEntry.OnFrequency(m.activeFrequency) {
frame.SelectedEntry = selectedEntry
}

nearestEntry, nearestEntryFound := m.nextVisibleEntry(m.activeFrequency, 0, func(entry core.BandmapEntry) bool {
return !entry.OnFrequency(m.activeFrequency) && entry.Source != core.WorkedSpot
})
if nearestEntryFound {
frame.NearestEntry = nearestEntry
}

highestValueEntry, highestValueEntryFound := m.nextVisibleEntryBy(core.BandmapByDescendingValue, 0, func(entry core.BandmapEntry) bool {
return entry.Source != core.WorkedSpot
})
if highestValueEntryFound {
frame.HighestValueEntry = highestValueEntry
}

m.view.ShowFrame(frame)
}

Expand Down Expand Up @@ -314,13 +323,13 @@ func (m *Bandmap) SelectByCallsign(call callsign.Callsign) bool {

func (m *Bandmap) GotoHighestValueEntry() {
m.findAndSelectNextVisibleEntryBy(core.BandmapByDescendingValue, func(entry core.BandmapEntry) bool {
return entry.Frequency != m.activeFrequency && entry.Source != core.WorkedSpot
return entry.Source != core.WorkedSpot
})
}

func (m *Bandmap) GotoNearestEntry() {
m.findAndSelectNextVisibleEntry(func(entry core.BandmapEntry) bool {
return entry.Frequency != m.activeFrequency && entry.Source != core.WorkedSpot
return !entry.OnFrequency(m.activeFrequency) && entry.Source != core.WorkedSpot
})
}

Expand Down
20 changes: 10 additions & 10 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,16 +854,16 @@ type Spot struct {
}

type BandmapFrame struct {
Frequency Frequency
ActiveBand Band
VisibleBand Band
Mode Mode
Bands []BandSummary
Entries []BandmapEntry
Index BandmapFrameIndex
NearestEntry BandmapEntry
RevealNearestEntry bool
SelectedEntry BandmapEntry
Frequency Frequency
ActiveBand Band
VisibleBand Band
Mode Mode
Bands []BandSummary
Entries []BandmapEntry
Index BandmapFrameIndex
SelectedEntry BandmapEntry
NearestEntry BandmapEntry
HighestValueEntry BandmapEntry
}

func (f BandmapFrame) IndexOf(id BandmapEntryID) (int, bool) {
Expand Down
71 changes: 59 additions & 12 deletions ui/spotsTableView.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

const (
spotColumnFrequency = iota
spotColumnMark = iota
spotColumnFrequency
spotColumnCallsign
spotColumnQualityTag
spotColumnPredictedExchange
Expand All @@ -36,6 +37,7 @@ func setupSpotsTableView(v *spotsView, builder *gtk.Builder, controller SpotsCon
v.table = getUI(builder, "entryTable").(*gtk.TreeView)
v.table.Connect("button-press-event", v.activateTableSelection)

v.table.AppendColumn(createSpotMarkupColumn("", spotColumnMark))
v.table.AppendColumn(createSpotMarkupColumn("Frequency", spotColumnFrequency))
v.table.AppendColumn(createSpotMarkupColumn("Callsign", spotColumnCallsign))
v.table.AppendColumn(createSpotTextColumn("T", spotColumnQualityTag))
Expand All @@ -56,7 +58,7 @@ func setupSpotsTableView(v *spotsView, builder *gtk.Builder, controller SpotsCon
log.Printf("no tree selection: %v", err)
return
}
selection.SetMode(gtk.SELECTION_NONE)
selection.SetMode(gtk.SELECTION_SINGLE)
selection.Connect("changed", v.onTableSelectionChanged)
}

Expand Down Expand Up @@ -124,6 +126,7 @@ func (v *spotsView) fillEntryToTableRow(row *gtk.TreeIter, entry core.BandmapEnt

return v.tableContent.Set(row,
[]int{
spotColumnMark,
spotColumnFrequency,
spotColumnCallsign,
spotColumnQualityTag,
Expand All @@ -138,8 +141,9 @@ func (v *spotsView) fillEntryToTableRow(row *gtk.TreeIter, entry core.BandmapEnt
spotColumnBackground,
},
[]any{
formatSpotMark(entry, v.currentFrame),
formatSpotFrequency(entry.Frequency),
formatSpotCall(entry.Call, entry.ID == v.currentFrame.SelectedEntry.ID),
formatSpotCall(entry.Call),
entry.Quality.Tag(),
entry.Info.ExchangeText,
formatPoints(entry.Info.Points, entry.Info.Duplicate, 1),
Expand All @@ -154,14 +158,27 @@ func (v *spotsView) fillEntryToTableRow(row *gtk.TreeIter, entry core.BandmapEnt
)
}

func formatSpotMark(entry core.BandmapEntry, frame core.BandmapFrame) string {
if entry.ID == frame.HighestValueEntry.ID {
return "H"
}
if entry.ID == frame.SelectedEntry.ID {
return ">"
}
if entry.OnFrequency(frame.Frequency) {
return "|"
}
if entry.ID == frame.NearestEntry.ID {
return "N"
}
return ""
}

func formatSpotFrequency(frequency core.Frequency) string {
return fmt.Sprintf("%.2f kHz", frequency/1000)
}

func formatSpotCall(call callsign.Callsign, selected bool) string {
if selected {
return ">" + call.String()
}
func formatSpotCall(call callsign.Callsign) string {
return call.String()
}

Expand Down Expand Up @@ -215,29 +232,61 @@ func (v *spotsView) showFrameInTable(frame core.BandmapFrame) {
}
}

func (v *spotsView) revealTableEntry(entry core.BandmapEntry) {
func (v *spotsView) getTablePathForEntry(entry core.BandmapEntry) (*gtk.TreePath, bool) {
index, found := v.currentFrame.IndexOf(entry.ID)
if !found {
log.Printf("cannot find index for entry with ID %d", entry.ID)
return
return nil, false
}

row, err := v.tableContent.GetIterFromString(fmt.Sprintf("%d", index))
if err != nil {
log.Printf("cannot find table row with ID %d", entry.ID)
return
return nil, false
}

path, err := v.tableContent.GetPath(row)
if err != nil {
log.Printf("no table path found for index with ID %d: %v", entry.ID, err)
return nil, false
}

return path, true
}

func (v *spotsView) revealTableEntry(entry core.BandmapEntry) {
path, found := v.getTablePathForEntry(entry)
if !found {
return
}

column := v.table.GetColumn(1)
v.table.ScrollToCell(path, column, false, 0, 0)
}

func (v *spotsView) setSelectedTableEntry(entry core.BandmapEntry) {
path, found := v.getTablePathForEntry(entry)
if !found {
return
}

selection, err := v.table.GetSelection()
if err != nil {
log.Printf("no table selection available: %v", err)
}
selection.SelectPath(path)
column := v.table.GetColumn(1)
v.table.ScrollToCell(path, column, false, 0, 0)
}

func (v *spotsView) clearSelection() {
selection, err := v.table.GetSelection()
if err != nil {
log.Printf("no table selection available: %v", err)
}
selection.UnselectAll()
}

func (v *spotsView) activateTableSelection(_ *gtk.TreeView, event *gdk.Event) {
buttonEvent := gdk.EventButtonNewFromEvent(event)
if buttonEvent.Button() != gdk.BUTTON_PRIMARY {
Expand All @@ -259,8 +308,6 @@ func (v *spotsView) onTableSelectionChanged(selection *gtk.TreeSelection) bool {
return true
}
v.tableSelectionActive = false
selection.UnselectAll()
selection.SetMode(gtk.SELECTION_NONE)

if !selected {
return true
Expand Down
27 changes: 23 additions & 4 deletions ui/spotsView.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,39 @@ func setupSpotsView(builder *gtk.Builder, colors colorProvider, controller Spots

func (v *spotsView) ShowFrame(frame core.BandmapFrame) {
runAsync(func() {
frequencyChanged := v.currentFrame.Frequency != frame.Frequency
// frequencyChanged := v.currentFrame.Frequency != frame.Frequency
bandChanged := (v.currentFrame.ActiveBand != frame.ActiveBand) || (v.currentFrame.VisibleBand != frame.VisibleBand)
selectionChanged := v.currentFrame.SelectedEntry.ID != frame.SelectedEntry.ID

oldFrame := v.currentFrame
v.currentFrame = frame
v.setupBands(frame.Bands)
v.updateBands(frame.Bands)
v.showFrameInTable(frame)

if (bandChanged || frequencyChanged) && v.currentFrame.RevealNearestEntry {
v.revealTableEntry(v.currentFrame.NearestEntry)
if bandChanged {
v.showFrameInTable(frame)
} else {
v.updateFrame(oldFrame, frame)
}

if selectionChanged {
if frame.SelectedEntry.ID != core.NoEntryID {
v.setSelectedTableEntry(frame.SelectedEntry)
} else {
v.clearSelection()
}
}

// if (bandChanged || frequencyChanged) && v.currentFrame.RevealNearestEntry {
// v.revealTableEntry(v.currentFrame.NearestEntry)
// }
})
}

func (v *spotsView) updateFrame(oldFrame core.BandmapFrame, newFrame core.BandmapFrame) {
v.showFrameInTable(newFrame)
}

func (v *spotsView) setupBands(bands []core.BandSummary) {
bandsID := toBandsID(bands)
if bandsID == v.bandsID {
Expand Down

0 comments on commit 005a236

Please sign in to comment.