|
28 | 28 | import os |
29 | 29 | from pathlib import Path |
30 | 30 |
|
31 | | -from PySide6.QtCore import QCoreApplication, QSize, Signal |
| 31 | +from PySide6.QtCore import QCoreApplication, QSize, Qt, Signal |
32 | 32 | from PySide6.QtGui import QFont |
33 | 33 | from PySide6.QtWidgets import ( |
34 | 34 | QComboBox, |
35 | 35 | QGridLayout, |
36 | 36 | QGroupBox, |
37 | 37 | QHBoxLayout, |
38 | 38 | QLabel, |
| 39 | + QListWidgetItem, |
39 | 40 | QPushButton, |
40 | 41 | QSizePolicy, |
41 | 42 | QToolButton, |
@@ -679,31 +680,44 @@ def update_channels(self, channels): |
679 | 680 | """ |
680 | 681 | self.logger.info(f"Updating channels to {channels}") |
681 | 682 |
|
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()) |
684 | 685 | self.logger.debug( |
685 | 686 | f"Current selections before restoration: {current_selections}" |
686 | 687 | ) |
687 | 688 |
|
688 | | - # Clear and add new channels to the MultiSelectComboBox |
689 | 689 | new_channels = [str(i) for i in channels] |
690 | | - self.channel_comboBox.addItems(new_channels) |
691 | | - self.logger.debug(f"Added channels: {new_channels}") |
692 | 690 |
|
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 |
694 | 695 | self.channel_comboBox.listWidget.itemChanged.disconnect( |
695 | 696 | self.channel_comboBox.handleItemChanged |
696 | 697 | ) |
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 | + |
700 | 711 | self.channel_comboBox.listWidget.itemChanged.connect( |
701 | 712 | self.channel_comboBox.handleItemChanged |
702 | 713 | ) |
703 | 714 |
|
| 715 | + # Update display text and Select All button without emitting selectionChanged |
| 716 | + self.channel_comboBox.refreshDisplayText() |
| 717 | + self.channel_comboBox.updateSelectAllButton() |
| 718 | + |
704 | 719 | # Log the final state of selections |
705 | 720 | restored_selections = self.channel_comboBox.getSelectedItems() |
706 | | - self.channel_comboBox.refreshDisplayText() |
707 | 721 | self.logger.debug(f"Selected items after restoration: {restored_selections}") |
708 | 722 |
|
709 | 723 | def update_readers(self, readers: list[str]) -> None: |
|
0 commit comments