Skip to content

Commit

Permalink
accurately display DNA database contents in widget
Browse files Browse the repository at this point in the history
Each node ID can only have one entry in the DNA database, so update any
existing row for a particular node ID instead of creating a new one.
  • Loading branch information
tpwrules authored and tridge committed Nov 12, 2024
1 parent 1f377ff commit cedb3c8
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions dronecan_gui_tool/widgets/dynamic_node_id_allocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,10 @@ def _update_table(self):
self._allocation_table.setRowCount(0)
else:
known_entries = [] if self._allocator is None else self._allocator.get_allocation_table()
displayed_entries = set()
nid_row = {} # map from node ID to its row as the DB can only have one entry per ID

for row in range(self._allocation_table.rowCount()):
nid_str = self._allocation_table.item(row, 0).text()
uid_str = self._allocation_table.item(row, 1).text()
displayed_entries.add((uid_str, nid_str))
nid_row[int(self._allocation_table.item(row, 0).text())] = row

def find_insertion_pos_for_node_id(target_nid):
for row in range(self._allocation_table.rowCount()):
Expand All @@ -141,11 +139,21 @@ def find_insertion_pos_for_node_id(target_nid):
return self._allocation_table.rowCount()

for uid, nid in known_entries:
if (unique_id_to_string(uid), str(nid)) in displayed_entries:
continue
row = find_insertion_pos_for_node_id(nid)
self._allocation_table.insertRow(row)
self._allocation_table.set_row(row, (uid, nid))
# compute correct row for this entry
try:
row = nid_row[nid]
except KeyError: # create row if it doesn't exist
row = find_insertion_pos_for_node_id(nid)
self._allocation_table.insertRow(row)
# update later rows
for known_nid, known_row in nid_row.items():
if known_row >= row:
nid_row[known_nid] = known_row + 1

# update row value if necessary
curr_val = self._allocation_table.item(row, 1)
if curr_val is None or curr_val.text() != unique_id_to_string(uid):
self._allocation_table.set_row(row, (uid, nid))

self._allocation_table.setUpdatesEnabled(True)

Expand Down

0 comments on commit cedb3c8

Please sign in to comment.