|
1 | 1 | from psygnal import Signal |
2 | 2 | from qtpy.QtWidgets import ( |
3 | 3 | QButtonGroup, |
| 4 | + QComboBox, |
4 | 5 | QGroupBox, |
5 | 6 | QHBoxLayout, |
6 | 7 | QRadioButton, |
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | class TreeViewFeatureWidget(QWidget): |
13 | | - """Widget to switch between viewing all nodes versus nodes of one or more lineages |
14 | | - in the tree widget |
15 | | - """ |
| 14 | + """Widget to switch between viewing all nodes versus nodes of one or more lineages in the tree widget""" |
16 | 15 |
|
17 | | - change_feature = Signal(str) |
| 16 | + change_plot_type = Signal(str) |
18 | 17 |
|
19 | | - def __init__(self): |
| 18 | + def __init__(self, features: list[str]): |
20 | 19 | super().__init__() |
21 | 20 |
|
22 | | - self.feature = "tree" |
| 21 | + self.plot_type = "tree" |
23 | 22 |
|
24 | | - display_box = QGroupBox("Feature [W]") |
| 23 | + display_box = QGroupBox("Plot [W]") |
25 | 24 | display_layout = QHBoxLayout() |
26 | 25 | button_group = QButtonGroup() |
27 | 26 | self.show_tree_radio = QRadioButton("Lineage Tree") |
28 | 27 | self.show_tree_radio.setChecked(True) |
29 | | - self.show_tree_radio.clicked.connect(lambda: self._set_feature("tree")) |
30 | | - self.show_area_radio = QRadioButton("Object size") |
31 | | - self.show_area_radio.clicked.connect(lambda: self._set_feature("area")) |
| 28 | + self.show_tree_radio.clicked.connect(lambda: self._set_plot_type("tree")) |
| 29 | + self.show_area_radio = QRadioButton("Feature") |
| 30 | + self.show_area_radio.clicked.connect(lambda: self._set_plot_type("feature")) |
32 | 31 | button_group.addButton(self.show_tree_radio) |
33 | 32 | button_group.addButton(self.show_area_radio) |
34 | 33 | display_layout.addWidget(self.show_tree_radio) |
35 | 34 | display_layout.addWidget(self.show_area_radio) |
| 35 | + |
| 36 | + self.feature_dropdown = QComboBox() |
| 37 | + for feature in features: |
| 38 | + self.feature_dropdown.addItem(feature) |
| 39 | + self.feature_dropdown.currentIndexChanged.connect(self._update_feature) |
| 40 | + if len(features) > 0: |
| 41 | + self.current_feature = features[0] |
| 42 | + else: |
| 43 | + self.current_feature = None |
| 44 | + self.show_area_radio.setEnabled(False) |
| 45 | + display_layout.addWidget(self.feature_dropdown) |
| 46 | + |
36 | 47 | display_box.setLayout(display_layout) |
37 | | - display_box.setMaximumWidth(250) |
| 48 | + display_box.setMaximumWidth(400) |
38 | 49 | display_box.setMaximumHeight(60) |
39 | 50 |
|
40 | 51 | layout = QVBoxLayout() |
41 | 52 | layout.addWidget(display_box) |
42 | 53 |
|
43 | 54 | self.setLayout(layout) |
44 | 55 |
|
45 | | - def _toggle_feature_mode(self, event=None) -> None: |
| 56 | + def _toggle_plot_type(self, event=None) -> None: |
46 | 57 | """Toggle display mode""" |
47 | 58 |
|
48 | 59 | if ( |
49 | 60 | self.show_area_radio.isEnabled |
50 | 61 | ): # if button is disabled, toggle is not allowed |
51 | | - if self.feature == "area": |
52 | | - self._set_feature("tree") |
| 62 | + if self.plot_type == "feature": |
| 63 | + self._set_plot_type("tree") |
53 | 64 | self.show_tree_radio.setChecked(True) |
54 | 65 | else: |
55 | | - self._set_feature("area") |
| 66 | + self._set_plot_type("feature") |
56 | 67 | self.show_area_radio.setChecked(True) |
57 | 68 |
|
58 | | - def _set_feature(self, mode: str): |
| 69 | + def _set_plot_type(self, plot_type: str): |
59 | 70 | """Emit signal to change the display mode""" |
60 | 71 |
|
61 | | - self.feature = mode |
62 | | - self.change_feature.emit(mode) |
| 72 | + self.plot_type = plot_type |
| 73 | + self.change_plot_type.emit(plot_type) |
| 74 | + |
| 75 | + def _update_feature(self) -> None: |
| 76 | + """Update the feature to be plotted if the plot_type == 'feature'""" |
| 77 | + |
| 78 | + self.current_feature = self.feature_dropdown.currentText() |
| 79 | + self.change_plot_type.emit(self.plot_type) |
| 80 | + |
| 81 | + def get_current_feature(self): |
| 82 | + """Return the current feature that is being plotted""" |
| 83 | + |
| 84 | + return self.current_feature |
| 85 | + |
| 86 | + def update_feature_dropdown(self, feature_list: list[str]) -> None: |
| 87 | + """Update the list of features in the dropdown""" |
| 88 | + |
| 89 | + self.feature_dropdown.currentIndexChanged.disconnect(self._update_feature) |
| 90 | + self.feature_dropdown.clear() |
| 91 | + self.show_area_radio.setEnabled(True) |
| 92 | + for feature in feature_list: |
| 93 | + self.feature_dropdown.addItem(feature) |
| 94 | + |
| 95 | + if self.current_feature not in feature_list: |
| 96 | + if len(feature_list) > 0: |
| 97 | + self.current_feature = feature_list[0] |
| 98 | + self.feature_dropdown.setCurrentIndex(0) |
| 99 | + else: |
| 100 | + self.current_feature = None |
| 101 | + self.show_area_radio.setEnabled(False) |
| 102 | + else: |
| 103 | + self.feature_dropdown.setCurrentIndex( |
| 104 | + self.feature_dropdown.findText(self.current_feature) |
| 105 | + ) |
| 106 | + |
| 107 | + self.feature_dropdown.currentIndexChanged.connect(self._update_feature) |
0 commit comments