Skip to content

Commit

Permalink
Support sorting columns in the view
Browse files Browse the repository at this point in the history
This is different from the existing "sort memories" function, which
changes the ordering in the radio/image. This feature changes only the
"view" in the UI.

Fixes: #11338
  • Loading branch information
kk7ds committed May 16, 2024
1 parent afec83c commit 27fbd8e
Showing 1 changed file with 75 additions and 9 deletions.
84 changes: 75 additions & 9 deletions chirp/wxui/memedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,54 @@
WX_GTK = 'gtk' in wx.version().lower()


class ChirpGridTable(wx.grid.GridStringTable):
def __init__(self, features, num_cols):
self._features = features
num_rows = (self._features.memory_bounds[1] -
self._features.memory_bounds[0] +
len(self._features.valid_special_chans) + 1)
super().__init__(num_rows, num_cols)
self._rowmap = {x: x for x in range(0, self.GetRowsCount())}
self._rowmap_rev = {v: k for k, v in self._rowmap.items()}

@property
def rowmap(self):
return self._rowmap

@property
def rowmap_rev(self):
return self._rowmap_rev

def sort_via(self, col, asc=True):
class SortItem:
"""String sort, but sort blanks to the end"""
def __init__(self, value):
self.value = value

def __lt__(self, other):
if not self.value.strip():
return False
else:
return self.value < other.value

sorted_values = sorted((
(asc != bool(
super(ChirpGridTable, self).GetValue(realrow, col).strip()),
super(ChirpGridTable, self).GetValue(realrow, col),
realrow)
for realrow in range(0, self.GetRowsCount())),
reverse=not asc)
self._rowmap = dict((i, mapping[-1])
for i, mapping in enumerate(sorted_values))
self._rowmap_rev = {v: k for k, v in self._rowmap.items()}

def GetValue(self, row, col):
return super().GetValue(self._rowmap[row], col)

def SetValue(self, row, col, value):
return super().SetValue(self._rowmap[row], col, value)


class ChirpMemoryGrid(wx.grid.Grid, glr.GridWithLabelRenderersMixin):
def __init__(self, *a, **k):
wx.grid.Grid.__init__(self, *a, **k)
Expand Down Expand Up @@ -677,10 +725,8 @@ def __init__(self, radio, *a, **k):
self.bandplan = bandplan.BandPlans(CONF)

self._grid = ChirpMemoryGrid(self)
self._grid.CreateGrid(
self._features.memory_bounds[1] - self._features.memory_bounds[0] +
len(self._features.valid_special_chans) + 1,
len(self._col_defs))
self._table = ChirpGridTable(self._features, len(self._col_defs))
self._grid.AssignTable(self._table)
self._grid.SetSelectionMode(wx.grid.Grid.SelectRows)
self._grid.DisableDragRowSize()
self._grid.EnableDragCell()
Expand All @@ -698,6 +744,8 @@ def __init__(self, radio, *a, **k):
self._variable_font = self._grid.GetDefaultCellFont()
self.update_font(False)

self._grid.Bind(wx.grid.EVT_GRID_COL_SORT,
self._sort_column)
self._grid.Bind(wx.grid.EVT_GRID_CELL_CHANGING,
self._memory_edited)
self._grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED,
Expand Down Expand Up @@ -828,7 +876,11 @@ def set_cell_attrs(self):
if not col_def.valid:
self._grid.HideCol(col)
else:
self._grid.SetColLabelValue(col, col_def.label)
label = col_def.label
if self._grid.GetSortingColumn() == col:
asc = self._grid.IsSortOrderAscending()
label += ' ' + (asc and '▲' or '▼')
self._grid.SetColLabelValue(col, label)
attr = wx.grid.GridCellAttr()
attr.SetEditor(col_def.get_editor())
self._grid.SetColAttr(col, attr)
Expand Down Expand Up @@ -953,12 +1005,15 @@ def _col_def_by_name(self, name):

def mem2row(self, number):
if isinstance(number, str):
return self._special_rows[number]
if number in self._special_numbers:
return self._special_rows[self._special_numbers[number]]
return number - self._features.memory_bounds[0]
row = self._special_rows[number]
elif number in self._special_numbers:
row = self._special_rows[self._special_numbers[number]]
else:
row = number - self._features.memory_bounds[0]
return self._table.rowmap_rev[row]

def row2mem(self, row):
row = self._table.rowmap[row]
if row in self._special_rows.values():
row2number = {v: k for k, v in self._special_rows.items()}
return row2number[row]
Expand Down Expand Up @@ -1350,6 +1405,17 @@ def _resolve_offset(self, mem):
mem.duplex = duplex
return True

def _sort_column(self, event):
col = event.GetCol()
cur = self._grid.GetSortingColumn()
asc = not self._grid.IsSortOrderAscending() if col == cur else True
LOG.debug('Sorting col %s asc %s', col, asc)
self._table.sort_via(col, asc)
self.refresh()
# This needs to be called after we're done here so that the grid's
# sort/asc attributes are updated
wx.CallAfter(self.set_cell_attrs)

@common.error_proof()
def _memory_edited(self, event):
"""
Expand Down

0 comments on commit 27fbd8e

Please sign in to comment.