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
14 changes: 13 additions & 1 deletion data_file_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ def update_checkbox_state():
# Set callback in the model so it can update the checkbox
var_list.model()._checkbox_update_callback = update_checkbox_state

tab_layout.addWidget(derived_checkbox)
checkbox_layout = QHBoxLayout()
checkbox_layout.addWidget(derived_checkbox)

show_values_checkbox = QCheckBox("Show values")
show_values_checkbox.toggled.connect(var_list.set_show_values)
checkbox_layout.addWidget(show_values_checkbox)

tab_layout.addLayout(checkbox_layout)
tab_layout.addWidget(var_list)

tab_name = os.path.basename(filepath)
Expand Down Expand Up @@ -158,6 +165,11 @@ def get_time(self, idx=0):
return None
return self.get_data_file(idx).time

def update_tick(self, tick):
active_widget = self.get_active_data_file()
if active_widget:
active_widget.update_tick(tick)

@pyqtSlot(QPoint)
def on_context_menu_request(self, pos):
# We only want to bring up the context menu when an actual tab is right-clicked. Check that
Expand Down
25 changes: 22 additions & 3 deletions data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def __init__(self, data_loader, parent=None):
logger.info(f"Loaded {data_loader.source} which has a dt of {self._avg_dt:.6f} sec and a sampling rate of {freq} Hz")

self._time_offset = 0
self._current_tick = 0
self._show_values = False

@property
def time(self):
Expand Down Expand Up @@ -103,16 +105,33 @@ def avg_dt(self):
def set_time_offset(self, time_offset):
self._time_offset = time_offset

def set_current_tick(self, tick):
self._current_tick = tick

def set_show_values(self, show):
self._show_values = show

def rowCount(self, parent=QModelIndex()):
return len(self._data)

def data(self, index, role):
if role == Qt.DisplayRole:
item = self._data[index.row()]
return QVariant(item.var_name)
var_name = item.var_name

if self._show_values and not isinstance(item, SeparatorItem):
try:
value = item.data[self._current_tick]
if isinstance(value, (float, np.floating)):
display_text = f"{var_name}: {value:.4f}"
else:
display_text = f"{var_name}: {value}"
return QVariant(display_text)
except IndexError:
return QVariant(f"{var_name}: N/A")
Comment on lines +123 to +131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for creating the display text can be refactored to improve readability and reduce repetition. By preparing the value as a string first and then constructing the final display text in one place, you can avoid repeating the f"{var_name}: ..." pattern and have a single return statement for this part of the logic.

Suggested change
try:
value = item.data[self._current_tick]
if isinstance(value, (float, np.floating)):
display_text = f"{var_name}: {value:.4f}"
else:
display_text = f"{var_name}: {value}"
return QVariant(display_text)
except IndexError:
return QVariant(f"{var_name}: N/A")
try:
value = item.data[self._current_tick]
if isinstance(value, (float, np.floating)):
value_str = f"{value:.4f}"
else:
value_str = f"{value}"
display_text = f"{var_name}: {value_str}"
except IndexError:
display_text = f"{var_name}: N/A"
return QVariant(display_text)


return QVariant(var_name)
elif role == Qt.UserRole:
# print(type(index))
# print(type(index.row()))
return self._data[index.row()]
elif role == Qt.ForegroundRole:
item = self._data[index.row()]
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def __init__(self):
tick_time_indicator = TimeTickWidget()
self.plot_manager.tickValueChanged.connect(tick_time_indicator.update_tick)
self.plot_manager.timeValueChanged.connect(tick_time_indicator.update_time)
self.plot_manager.tickValueChanged.connect(self.data_file_widget.update_tick)
self.statusBar().addPermanentWidget(tick_time_indicator)

self._settings = QSettings()
Expand Down
8 changes: 8 additions & 0 deletions var_list_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def set_time_offset(self, time_offset):
self.model().set_time_offset(time_offset)
self.timeChanged.emit()

def set_show_values(self, show):
self.model().set_show_values(show)
self.model().layoutChanged.emit()

def update_tick(self, tick):
self.model().set_current_tick(tick)
self.model().layoutChanged.emit()
Comment on lines +62 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better performance and adherence to Qt's model/view architecture, it's preferable to use dataChanged instead of layoutChanged when only the data for items is changing, not the structure of the model (like adding or removing rows).

dataChanged is more specific and can be more efficient as it tells the view to only update the data of existing items. layoutChanged is a stronger signal that may cause the view to perform more work than necessary, such as recomputing layout hints.

Suggested change
def set_show_values(self, show):
self.model().set_show_values(show)
self.model().layoutChanged.emit()
def update_tick(self, tick):
self.model().set_current_tick(tick)
self.model().layoutChanged.emit()
def set_show_values(self, show):
self.model().set_show_values(show)
if self.model().rowCount() > 0:
self.model().dataChanged.emit(self.model().index(0), self.model().index(self.model().rowCount() - 1))
def update_tick(self, tick):
self.model().set_current_tick(tick)
if self.model().rowCount() > 0:
self.model().dataChanged.emit(self.model().index(0), self.model().index(self.model().rowCount() - 1))


def close(self):
self.onClose.emit()
return super().close()
Expand Down