Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 74 additions & 11 deletions dronecan_gui_tool/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import os
import re
import queue
import collections
import importlib.resources
from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem, QAbstractItemView, QHeaderView, QApplication, QWidget, \
QComboBox, QCompleter, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox, QLineEdit
QComboBox, QCompleter, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox, QLineEdit, QSpinBox
from PyQt6.QtCore import Qt, QTimer, QStringListModel
from PyQt6.QtGui import QColor, QKeySequence, QFont, QFontInfo, QIcon
from logging import getLogger
Expand Down Expand Up @@ -454,8 +454,38 @@ def __init__(self, icon, text, parent):
self.setEnabled(False)


class SteppableSpinBox(QSpinBox):
"""
A spin box whose arrows (and Page Up/Down) move the value to the next or previous multiple of the step size,
instead of adding the step size to the value; the result is then clamped to the range.

Args:
parent: Parent widget.
min: Minimum value.
max: Maximum value.
step_size: The arrows step to multiples of this value.
"""
def __init__(self, parent=None, min=0, max=99, step_size=1):
super(SteppableSpinBox, self).__init__(parent)
self.setRange(min, max)
self.setSingleStep(step_size)

def stepBy(self, steps):
step = self.singleStep()
if steps > 0:
value = (self.value() // step + steps) * step
else:
value = -(-self.value() // step) * step + steps * step # Rounds up to a multiple, then steps down
self.setValue(max(self.minimum(), min(self.maximum(), value)))


class RealtimeLogWidget(QWidget):
def __init__(self, parent, started_by_default=False, pre_redraw_hook=None, **table_options):
def __init__(self, parent, started_by_default=False, pre_redraw_hook=None, max_rows=None, **table_options):
"""
Args:
max_rows: If set, the oldest rows are removed once the table grows beyond this many rows, and a
control to edit the limit is shown. Unlimited by default.
"""
super(RealtimeLogWidget, self).__init__(parent)

self.on_selection_changed = None
Expand All @@ -467,8 +497,10 @@ def __init__(self, parent, started_by_default=False, pre_redraw_hook=None, **tab

self._clear_button = make_icon_button('fa6s.trash', 'Clear', self, on_clicked=self._clear)

self._pause = make_icon_button('fa6s.pause', 'Pause updates; data received while paused will not be lost', self,
checkable=True)
pause_tool_tip = 'Pause updates; data received while paused will not be lost'
if max_rows is not None:
pause_tool_tip += ', up to the row limit'
self._pause = make_icon_button('fa6s.pause', pause_tool_tip, self, checkable=True)

self._start_button = make_icon_button('fa6s.video', 'Start/stop capturing', self,
checkable=True,
Expand All @@ -484,12 +516,26 @@ def __init__(self, parent, started_by_default=False, pre_redraw_hook=None, **tab
self._row_count = LabelWithIcon(get_icon('fa6s.list'), '0', self)
self._row_count.setToolTip('Row count')

self._max_rows = None
if max_rows is not None:
self._max_rows_spinbox = SteppableSpinBox(self, min=1000, max=10 ** 8, step_size=10000)
self._max_rows_spinbox.setToolTip('Maximum number of rows; the oldest rows are removed beyond this limit')
self._max_rows_spinbox.setPrefix('max ')
self._max_rows_spinbox.setGroupSeparatorShown(True)
self._max_rows_spinbox.setKeyboardTracking(False) # Apply when editing is finished, not per keystroke
self._max_rows_spinbox.setValue(max_rows)
self._max_rows_spinbox.valueChanged.connect(self._set_max_rows)
else:
self._max_rows_spinbox = None

self._redraw_timer = QTimer(self)
self._redraw_timer.setSingleShot(False)
self._redraw_timer.timeout.connect(self._redraw)
self._redraw_timer.start(100)

self._queue = queue.Queue()
# Items beyond the row limit would be removed from the table anyway, so the queue drops the oldest of them
self._queue = collections.deque()
self._set_max_rows(max_rows)

layout = QVBoxLayout(self)

Expand All @@ -506,6 +552,8 @@ def __init__(self, parent, started_by_default=False, pre_redraw_hook=None, **tab
controls_layout.addStretch()

controls_layout.addWidget(self._row_count)
if self._max_rows_spinbox is not None:
controls_layout.addWidget(self._max_rows_spinbox)

layout.addLayout(controls_layout)
layout.addWidget(self._search_bar)
Expand All @@ -527,6 +575,20 @@ def _clear(self):
self._table.setRowCount(0)
self._row_count.setText(str(self._table.rowCount()))

def _set_max_rows(self, max_rows):
self._max_rows = max_rows
self._queue = collections.deque(self._queue, maxlen=max_rows)
self._trim_rows()
self._row_count.setText(str(self._table.rowCount()))

def _trim_rows(self):
if self._max_rows is None:
return
excess = self._table.rowCount() - self._max_rows
if excess > 0:
# One bulk removal is much cheaper than removing the rows one by one
self._table.model().removeRows(0, excess)

def _call_on_selection_changed(self):
if not self.on_selection_changed:
return
Expand All @@ -544,15 +606,17 @@ def _redraw(self):
if not self.paused:
while True:
try:
item = self._queue.get_nowait()
except queue.Empty:
item = self._queue.popleft()
except IndexError:
break

row = self._table.rowCount()
self._table.insertRow(row)
self._table.set_row(row, item)
do_scroll = True

self._trim_rows()

self._table.setUpdatesEnabled(True)

if do_scroll:
Expand All @@ -561,14 +625,13 @@ def _redraw(self):
self._row_count.setText(str(self._table.rowCount()))
else:
# Discarding inputs
while self._queue.qsize() > 0:
self._queue.get_nowait()
self._queue.clear()

def _on_start_button_clicked(self):
self._pause.setChecked(False)

def add_item_async(self, item):
self._queue.put_nowait(item)
self._queue.append(item)

@property
def table(self):
Expand Down
4 changes: 3 additions & 1 deletion dronecan_gui_tool/widgets/bus_monitor/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def row_to_frame(table, row_index):
class BusMonitorWindow(QMainWindow):
DEFAULT_PLOT_X_RANGE = 120
BUS_LOAD_PLOT_MAX_SAMPLES = 50000
DEFAULT_MAX_ROWS = 200000

def __init__(self, get_frame, iface_name):
super(BusMonitorWindow, self).__init__()
Expand All @@ -261,7 +262,8 @@ def __init__(self, get_frame, iface_name):
self._get_frame = get_frame

self._log_widget = RealtimeLogWidget(self, columns=COLUMNS, font=get_monospace_font(),
pre_redraw_hook=self._redraw_hook, multi_line_rows=True)
pre_redraw_hook=self._redraw_hook, multi_line_rows=True,
max_rows=self.DEFAULT_MAX_ROWS)
self._log_widget.on_selection_changed = self._update_measurement_display

self._log_widget.table.cellClicked.connect(lambda row, col: self._decode_transfer_at_row(row))
Expand Down
Loading