Skip to content

Commit 6133736

Browse files
committed
fix: default to select all on first load, preserve selection on update
1 parent 5e073d9 commit 6133736

2 files changed

Lines changed: 66 additions & 19 deletions

File tree

poriscope/plugins/analysistabs/utils/eventAnalysisControls.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
QGroupBox,
4444
QHBoxLayout,
4545
QLabel,
46+
QListWidgetItem,
4647
QPushButton,
4748
QSizePolicy,
4849
QToolButton,
@@ -612,18 +613,50 @@ def on_button_clicked(self, button_type):
612613
button_mapping.get(button_type, lambda: None).setChecked(False)
613614

614615
def update_channels(self, channels):
616+
"""
617+
Updates the channels displayed in the MultiSelectComboBox widget and restores previous selections.
618+
"""
615619
self.logger.info(f"Updating channels to {channels}")
616620

617-
# Store the current selection(s)
618-
current_selections = self.channel_comboBox.getSelectedItems()
621+
# Get current selections from the MultiSelectComboBox BEFORE clearing
622+
current_selections = set(self.channel_comboBox.getSelectedItems())
623+
self.logger.debug(
624+
f"Current selections before restoration: {current_selections}"
625+
)
626+
627+
new_channels = [str(i) for i in channels]
628+
629+
# Check if this is a first load (no items exist yet) to default to Select All
630+
is_first_load = self.channel_comboBox.listWidget.count() == 0
631+
632+
# Block signals during the entire rebuild to avoid emitting incorrect states
633+
self.channel_comboBox.listWidget.itemChanged.disconnect(
634+
self.channel_comboBox.handleItemChanged
635+
)
636+
637+
# Clear and rebuild items, preserving checked state from previous selections.
638+
# On first load, default all channels to checked (Select All behavior).
639+
self.channel_comboBox.listWidget.clear()
640+
for text in new_channels:
641+
item = QListWidgetItem(text, self.channel_comboBox.listWidget)
642+
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
643+
# On first load select all by default, otherwise restore previous selection
644+
state = Qt.Checked if (is_first_load or text in current_selections) else Qt.Unchecked
645+
item.setCheckState(state)
646+
647+
self.logger.debug(f"Added channels: {new_channels}")
648+
649+
self.channel_comboBox.listWidget.itemChanged.connect(
650+
self.channel_comboBox.handleItemChanged
651+
)
619652

620-
self.channel_comboBox.clear()
621-
self.channel_comboBox.addItems([str(i) for i in channels])
653+
# Update display text and Select All button without emitting selectionChanged
654+
self.channel_comboBox.refreshDisplayText()
655+
self.channel_comboBox.updateSelectAllButton()
622656

623-
# Restore selections if they still exist
624-
for selection in current_selections:
625-
if selection in [str(i) for i in channels]:
626-
self.channel_comboBox.selectItem(selection)
657+
# Log the final state of selections
658+
restored_selections = self.channel_comboBox.getSelectedItems()
659+
self.logger.debug(f"Selected items after restoration: {restored_selections}")
627660

628661
def update_loaders(self, loaders: list[str]) -> None:
629662
self.logger.info(f"Updating loaders: {loaders}")

poriscope/plugins/analysistabs/utils/rawdatacontrols.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
import os
2929
from pathlib import Path
3030

31-
from PySide6.QtCore import QCoreApplication, QSize, Signal
31+
from PySide6.QtCore import QCoreApplication, QSize, Qt, Signal
3232
from PySide6.QtGui import QFont
3333
from PySide6.QtWidgets import (
3434
QComboBox,
3535
QGridLayout,
3636
QGroupBox,
3737
QHBoxLayout,
3838
QLabel,
39+
QListWidgetItem,
3940
QPushButton,
4041
QSizePolicy,
4142
QToolButton,
@@ -679,31 +680,44 @@ def update_channels(self, channels):
679680
"""
680681
self.logger.info(f"Updating channels to {channels}")
681682

682-
# Get current selections from the MultiSelectComboBox
683-
current_selections = self.channel_comboBox.getSelectedItems()
683+
# Get current selections from the MultiSelectComboBox BEFORE clearing
684+
current_selections = set(self.channel_comboBox.getSelectedItems())
684685
self.logger.debug(
685686
f"Current selections before restoration: {current_selections}"
686687
)
687688

688-
# Clear and add new channels to the MultiSelectComboBox
689689
new_channels = [str(i) for i in channels]
690-
self.channel_comboBox.addItems(new_channels)
691-
self.logger.debug(f"Added channels: {new_channels}")
692690

693-
# Restore previous selections that are still valid
691+
# Check if this is a first load (no items exist yet) to default to Select All
692+
is_first_load = self.channel_comboBox.listWidget.count() == 0
693+
694+
# Block signals during the entire rebuild to avoid emitting incorrect states
694695
self.channel_comboBox.listWidget.itemChanged.disconnect(
695696
self.channel_comboBox.handleItemChanged
696697
)
697-
for selection in current_selections:
698-
if selection in new_channels:
699-
self.channel_comboBox.selectItem(selection)
698+
699+
# Clear and rebuild items, preserving checked state from previous selections.
700+
# On first load, default all channels to checked (Select All behavior).
701+
self.channel_comboBox.listWidget.clear()
702+
for text in new_channels:
703+
item = QListWidgetItem(text, self.channel_comboBox.listWidget)
704+
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
705+
# On first load select all by default, otherwise restore previous selection
706+
state = Qt.Checked if (is_first_load or text in current_selections) else Qt.Unchecked
707+
item.setCheckState(state)
708+
709+
self.logger.debug(f"Added channels: {new_channels}")
710+
700711
self.channel_comboBox.listWidget.itemChanged.connect(
701712
self.channel_comboBox.handleItemChanged
702713
)
703714

715+
# Update display text and Select All button without emitting selectionChanged
716+
self.channel_comboBox.refreshDisplayText()
717+
self.channel_comboBox.updateSelectAllButton()
718+
704719
# Log the final state of selections
705720
restored_selections = self.channel_comboBox.getSelectedItems()
706-
self.channel_comboBox.refreshDisplayText()
707721
self.logger.debug(f"Selected items after restoration: {restored_selections}")
708722

709723
def update_readers(self, readers: list[str]) -> None:

0 commit comments

Comments
 (0)