|
2 | 2 |
|
3 | 3 | import glob |
4 | 4 | import os |
| 5 | +import re |
5 | 6 |
|
6 | 7 | from qtpy.QtWidgets import ( |
7 | 8 | QAbstractItemView, |
|
16 | 17 | QWidget, |
17 | 18 | ) |
18 | 19 |
|
19 | | -from .invalid_styles import INVALID_QLISTWIDGET |
| 20 | +from .invalid_styles import INVALID_QLINEEDIT, INVALID_QLISTWIDGET |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class RawData(QGroupBox): |
@@ -48,16 +49,36 @@ def __init__(self, parent=None): |
48 | 49 | self.files.setSelectionMode(QAbstractItemView.ExtendedSelection) |
49 | 50 | self.files.setSortingEnabled(True) |
50 | 51 |
|
| 52 | + self.manual_selection = QLineEdit() |
| 53 | + self.manual_selection.setToolTip( |
| 54 | + "Select runs by number pattern, e.g. '1-3, 5:20:3' selects runs 1,2,3,5,8,11,14,17,20." |
| 55 | + ) |
| 56 | + manual_layout = QHBoxLayout() |
| 57 | + manual_layout.addWidget(QLabel("Manual selection")) |
| 58 | + manual_layout.addWidget(self.manual_selection) |
| 59 | + self.manual_select = QPushButton("Select") |
| 60 | + self.manual_select.setEnabled(False) |
| 61 | + self.manual_select.clicked.connect(self._manual_selection_apply) |
| 62 | + manual_layout.addWidget(self.manual_select) |
| 63 | + self.manual_deselect = QPushButton("Deselect") |
| 64 | + self.manual_deselect.setEnabled(False) |
| 65 | + self.manual_deselect.clicked.connect(self._manual_selection_deselect) |
| 66 | + manual_layout.addWidget(self.manual_deselect) |
| 67 | + manual_selection_widget = QWidget() |
| 68 | + manual_selection_widget.setLayout(manual_layout) |
| 69 | + |
51 | 70 | layout = QVBoxLayout() |
52 | 71 | layout.addWidget(path_line) |
53 | 72 | layout.addWidget(self.files) |
| 73 | + layout.addWidget(manual_selection_widget) |
54 | 74 | self.setLayout(layout) |
55 | 75 |
|
56 | 76 | self.selected_list_from_oncat = None |
57 | 77 |
|
58 | 78 | # mandatory field check its state |
59 | 79 | # at least one item should be selected |
60 | 80 | self.files.itemSelectionChanged.connect(self.check_file_input) |
| 81 | + self.manual_selection.textChanged.connect(self._on_manual_selection_changed) |
61 | 82 |
|
62 | 83 | def _path_edited(self): |
63 | 84 | if self.path.text() != self.directory: |
@@ -102,6 +123,46 @@ def check_file_input(self): |
102 | 123 | self.set_field_invalid_state(self.files) |
103 | 124 | return state |
104 | 125 |
|
| 126 | + def _on_manual_selection_changed(self): |
| 127 | + text = self.manual_selection.text().strip() |
| 128 | + if not text: |
| 129 | + self.manual_select.setEnabled(False) |
| 130 | + self.manual_deselect.setEnabled(False) |
| 131 | + self.manual_selection.setStyleSheet("") |
| 132 | + return |
| 133 | + valid, _ = parse_run_numbers(text) |
| 134 | + self.manual_select.setEnabled(valid) |
| 135 | + self.manual_deselect.setEnabled(valid) |
| 136 | + if valid: |
| 137 | + self.manual_selection.setStyleSheet("") |
| 138 | + else: |
| 139 | + self.manual_selection.setStyleSheet(INVALID_QLINEEDIT) |
| 140 | + |
| 141 | + def _manual_selection_apply(self): |
| 142 | + text = self.manual_selection.text().strip() |
| 143 | + valid, run_numbers = parse_run_numbers(text) |
| 144 | + if not valid: |
| 145 | + return |
| 146 | + run_set = set(run_numbers) |
| 147 | + self.files.clearSelection() |
| 148 | + for i in range(self.files.count()): |
| 149 | + item = self.files.item(i) |
| 150 | + run_number = _extract_run_number(item.text()) |
| 151 | + if run_number is not None and run_number in run_set: |
| 152 | + item.setSelected(True) |
| 153 | + |
| 154 | + def _manual_selection_deselect(self): |
| 155 | + text = self.manual_selection.text().strip() |
| 156 | + valid, run_numbers = parse_run_numbers(text) |
| 157 | + if not valid: |
| 158 | + return |
| 159 | + run_set = set(run_numbers) |
| 160 | + for i in range(self.files.count()): |
| 161 | + item = self.files.item(i) |
| 162 | + run_number = _extract_run_number(item.text()) |
| 163 | + if run_number is not None and run_number in run_set: |
| 164 | + item.setSelected(False) |
| 165 | + |
105 | 166 | def get_selected(self, use_grouped=False): |
106 | 167 | """Return a list of the full path of the files selected""" |
107 | 168 | # if the selection is from oncat, use the oncat one |
@@ -171,6 +232,56 @@ def populate_from_dict(self, config_dict: dict): |
171 | 232 | self.set_selected(filename_list_flattened) |
172 | 233 |
|
173 | 234 |
|
| 235 | +def _extract_run_number(filename): |
| 236 | + """Return the integer run number from a filename like INST_12345.nxs.h5, or None. |
| 237 | +
|
| 238 | + Strips all extensions first (e.g. .nxs.h5), then returns the last underscore- |
| 239 | + delimited digit group, which is the run number in names like INST_12345 or |
| 240 | + INST_12345_event. |
| 241 | + """ |
| 242 | + stem = filename |
| 243 | + while "." in stem: |
| 244 | + stem = stem.rsplit(".", 1)[0] |
| 245 | + matches = re.findall(r"_(\d+)", stem) |
| 246 | + return int(matches[-1]) if matches else None |
| 247 | + |
| 248 | + |
| 249 | +def parse_run_numbers(text): |
| 250 | + """Parse a run-number pattern string. |
| 251 | +
|
| 252 | + Supports single numbers, dash ranges (1-3), colon ranges with optional step |
| 253 | + (1:10:2), and comma-separated combinations. |
| 254 | +
|
| 255 | + Returns (is_valid: bool, run_numbers: list[int]). |
| 256 | + """ |
| 257 | + try: |
| 258 | + result = [] |
| 259 | + for token in text.split(","): |
| 260 | + token = token.strip() |
| 261 | + if not token: |
| 262 | + continue |
| 263 | + if ":" in token: |
| 264 | + parts = [int(p) for p in token.split(":")] |
| 265 | + if len(parts) == 2: |
| 266 | + result.extend(range(parts[0], parts[1] + 1)) |
| 267 | + elif len(parts) == 3: |
| 268 | + result.extend(range(parts[0], parts[1] + 1, parts[2])) |
| 269 | + else: |
| 270 | + return False, [] |
| 271 | + elif "-" in token: |
| 272 | + parts = [int(p) for p in token.split("-")] |
| 273 | + if len(parts) != 2: |
| 274 | + return False, [] |
| 275 | + result.extend(range(parts[0], parts[1] + 1)) |
| 276 | + else: |
| 277 | + result.append(int(token)) |
| 278 | + if not result: |
| 279 | + return False, [] |
| 280 | + return True, result |
| 281 | + except (ValueError, TypeError): |
| 282 | + return False, [] |
| 283 | + |
| 284 | + |
174 | 285 | def filename_str_to_list(filenames_str): |
175 | 286 | """Convert a string of filenames to a list. |
176 | 287 |
|
|
0 commit comments