|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from funtracks.import_export.import_from_geff import import_from_geff |
| 4 | +from qtpy.QtCore import Qt |
| 5 | +from qtpy.QtWidgets import ( |
| 6 | + QApplication, |
| 7 | + QDialog, |
| 8 | + QHBoxLayout, |
| 9 | + QMessageBox, |
| 10 | + QPushButton, |
| 11 | + QScrollArea, |
| 12 | + QSizePolicy, |
| 13 | + QVBoxLayout, |
| 14 | + QWidget, |
| 15 | +) |
| 16 | + |
| 17 | +from finn.track_import_export.menus.import_from_geff.geff_import_widget import ( |
| 18 | + ImportGeffWidget, |
| 19 | +) |
| 20 | +from finn.track_import_export.menus.import_from_geff.geff_prop_map_widget import ( |
| 21 | + StandardFieldMapWidget, |
| 22 | +) |
| 23 | +from finn.track_import_export.menus.import_from_geff.geff_scale_widget import ScaleWidget |
| 24 | +from finn.track_import_export.menus.import_from_geff.geff_segmentation_widgets import ( |
| 25 | + SegmentationWidget, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class ImportGeffDialog(QDialog): |
| 30 | + """Dialgo for importing external tracks from a geff file""" |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + super().__init__() |
| 34 | + self.setWindowTitle("Import external tracks from geff") |
| 35 | + self.name = "Tracks from Geff" |
| 36 | + |
| 37 | + # cancel and finish buttons |
| 38 | + self.button_layout = QHBoxLayout() |
| 39 | + self.cancel_button = QPushButton("Cancel") |
| 40 | + self.finish_button = QPushButton("Finish") |
| 41 | + self.finish_button.setEnabled(False) |
| 42 | + self.button_layout.addWidget(self.cancel_button) |
| 43 | + self.button_layout.addWidget(self.finish_button) |
| 44 | + |
| 45 | + # Connect button signals |
| 46 | + self.cancel_button.clicked.connect(self._cancel) |
| 47 | + self.finish_button.clicked.connect(self._finish) |
| 48 | + self.cancel_button.setDefault(False) |
| 49 | + self.cancel_button.setAutoDefault(False) |
| 50 | + self.finish_button.setDefault(False) |
| 51 | + self.finish_button.setAutoDefault(False) |
| 52 | + |
| 53 | + # Initialize widgets and connect to update signals |
| 54 | + self.geff_widget = ImportGeffWidget() |
| 55 | + self.geff_widget.update_buttons.connect(self._update_segmentation_widget) |
| 56 | + self.segmentation_widget = SegmentationWidget(root=self.geff_widget.root) |
| 57 | + self.segmentation_widget.none_radio.toggled.connect( |
| 58 | + self._toggle_scale_widget_and_seg_id |
| 59 | + ) |
| 60 | + self.segmentation_widget.segmentation_widget.seg_path_updated.connect( |
| 61 | + self._update_finish_button |
| 62 | + ) |
| 63 | + self.prop_map_widget = StandardFieldMapWidget() |
| 64 | + self.geff_widget.update_buttons.connect(self._update_field_map_widget) |
| 65 | + self.scale_widget = ScaleWidget() |
| 66 | + |
| 67 | + self.content_widget = QWidget() |
| 68 | + main_layout = QVBoxLayout(self.content_widget) |
| 69 | + main_layout.addWidget(self.geff_widget) |
| 70 | + main_layout.addWidget(self.segmentation_widget) |
| 71 | + main_layout.addWidget(self.prop_map_widget) |
| 72 | + main_layout.addWidget(self.scale_widget) |
| 73 | + main_layout.addLayout(self.button_layout) |
| 74 | + self.content_widget.setLayout(main_layout) |
| 75 | + self.content_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) |
| 76 | + |
| 77 | + # Create scroll area |
| 78 | + self.scroll_area = QScrollArea() |
| 79 | + self.scroll_area.setWidget(self.content_widget) |
| 80 | + self.scroll_area.setWidgetResizable(True) |
| 81 | + self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) |
| 82 | + self.scroll_area.setMinimumWidth(500) |
| 83 | + self.scroll_area.setSizePolicy( |
| 84 | + QSizePolicy.Preferred, QSizePolicy.MinimumExpanding |
| 85 | + ) |
| 86 | + self.content_widget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum) |
| 87 | + |
| 88 | + dialog_layout = QVBoxLayout() |
| 89 | + dialog_layout.addWidget(self.scroll_area) |
| 90 | + self.setLayout(dialog_layout) |
| 91 | + self.setSizePolicy(self.sizePolicy().horizontalPolicy(), QSizePolicy.Minimum) |
| 92 | + |
| 93 | + def _resize_dialog(self): |
| 94 | + """Dynamic widget resizing depending on the visible contents""" |
| 95 | + |
| 96 | + self.content_widget.adjustSize() |
| 97 | + self.content_widget.updateGeometry() |
| 98 | + |
| 99 | + content_hint = self.content_widget.sizeHint() |
| 100 | + screen_geometry = QApplication.primaryScreen().availableGeometry() |
| 101 | + |
| 102 | + max_height = int(screen_geometry.height() * 0.85) |
| 103 | + new_height = min(content_hint.height(), max_height) |
| 104 | + new_width = max(content_hint.width(), 500) |
| 105 | + |
| 106 | + self.resize(new_width, new_height) |
| 107 | + |
| 108 | + # Center horizontally, but upwards if too tall |
| 109 | + screen_center = screen_geometry.center() |
| 110 | + x = screen_center.x() - self.width() // 2 |
| 111 | + |
| 112 | + if new_height < screen_geometry.height(): |
| 113 | + y = screen_center.y() - new_height // 2 |
| 114 | + else: |
| 115 | + y = screen_geometry.top() + 50 |
| 116 | + |
| 117 | + self.move(x, y) |
| 118 | + |
| 119 | + def _update_segmentation_widget(self) -> None: |
| 120 | + """Refresh the segmentation widget based on the geff root group.""" |
| 121 | + |
| 122 | + if self.geff_widget.root is not None: |
| 123 | + self.segmentation_widget.update_root(self.geff_widget.root) |
| 124 | + else: |
| 125 | + self.segmentation_widget.setVisible(False) |
| 126 | + self._update_finish_button() |
| 127 | + self._resize_dialog() |
| 128 | + |
| 129 | + def _update_field_map_widget(self) -> None: |
| 130 | + """Prefill the field map widget with the geff metadata and graph attributes.""" |
| 131 | + |
| 132 | + if self.geff_widget.root is not None: |
| 133 | + self.prop_map_widget.update_mapping( |
| 134 | + self.geff_widget.root, self.segmentation_widget.include_seg() |
| 135 | + ) |
| 136 | + |
| 137 | + self.scale_widget._prefill_from_metadata( |
| 138 | + dict(self.geff_widget.root.attrs.get("geff", {})) |
| 139 | + ) |
| 140 | + self.scale_widget.setVisible(self.segmentation_widget.include_seg()) |
| 141 | + else: |
| 142 | + self.prop_map_widget.setVisible(False) |
| 143 | + self.scale_widget.setVisible(False) |
| 144 | + |
| 145 | + self._update_finish_button() |
| 146 | + self._resize_dialog() |
| 147 | + |
| 148 | + def _update_finish_button(self): |
| 149 | + """Update the finish button status depending on whether a segmentation is required |
| 150 | + and whether a valid geff root is present.""" |
| 151 | + |
| 152 | + include_seg = self.segmentation_widget.include_seg() |
| 153 | + has_seg = self.segmentation_widget.get_segmentation() is not None |
| 154 | + valid_seg = not (include_seg and not has_seg) |
| 155 | + self.finish_button.setEnabled(self.geff_widget.root is not None and valid_seg) |
| 156 | + |
| 157 | + def _toggle_scale_widget_and_seg_id(self, checked: bool) -> None: |
| 158 | + """Toggle visibility of the scale widget based on the 'None' radio button state, |
| 159 | + and update the visibility of the 'seg_id' combobox in the prop map widget.""" |
| 160 | + |
| 161 | + self.scale_widget.setVisible(not checked) |
| 162 | + |
| 163 | + # Also remove the seg_id from the fields widget |
| 164 | + if len(self.prop_map_widget.mapping_widgets) > 0: |
| 165 | + self.prop_map_widget.mapping_widgets["seg_id"].setVisible(not checked) |
| 166 | + self.prop_map_widget.mapping_labels["seg_id"].setVisible(not checked) |
| 167 | + self.prop_map_widget.optional_features["area"]["recompute"].setEnabled( |
| 168 | + not checked |
| 169 | + ) |
| 170 | + |
| 171 | + self._update_finish_button() |
| 172 | + self._resize_dialog() |
| 173 | + |
| 174 | + def _cancel(self) -> None: |
| 175 | + """Close the dialog without loading tracks.""" |
| 176 | + self.reject() |
| 177 | + |
| 178 | + def _finish(self) -> None: |
| 179 | + """Tries to read the geff file and optional segmentation image and apply the |
| 180 | + attribute to column mapping to construct a Tracks object""" |
| 181 | + |
| 182 | + if self.geff_widget.root is not None: |
| 183 | + store_path = Path(self.geff_widget.root.store.path) # e.g. /.../my_store.zarr |
| 184 | + group_path = Path(self.geff_widget.root.path) # e.g. 'tracks' |
| 185 | + geff_dir = store_path / group_path |
| 186 | + |
| 187 | + self.name = self.geff_widget.dir_name |
| 188 | + scale = self.scale_widget.get_scale() |
| 189 | + |
| 190 | + segmentation = self.segmentation_widget.get_segmentation() |
| 191 | + name_map = self.prop_map_widget.get_name_map() |
| 192 | + name_map = {k: (None if v == "None" else v) for k, v in name_map.items()} |
| 193 | + extra_features = self.prop_map_widget.get_optional_props() |
| 194 | + |
| 195 | + try: |
| 196 | + self.tracks = import_from_geff( |
| 197 | + geff_dir, |
| 198 | + name_map, |
| 199 | + segmentation_path=segmentation, |
| 200 | + extra_features=extra_features, |
| 201 | + scale=scale, |
| 202 | + ) |
| 203 | + except (ValueError, OSError, FileNotFoundError, AssertionError) as e: |
| 204 | + QMessageBox.critical(self, "Error", f"Failed to load tracks: {e}") |
| 205 | + return |
| 206 | + self.accept() |
0 commit comments