Skip to content

Commit 181f530

Browse files
committed
feat(progress): progress is tracked from bec; unified progress backend
1 parent cd150c0 commit 181f530

9 files changed

Lines changed: 529 additions & 626 deletions

File tree

bec_widgets/widgets/containers/main_window/main_window.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ def _add_scan_progress_bar(self):
209209
self._scan_progress_bar_simple.progressbar.label_template = ""
210210
self._scan_progress_bar_simple.progressbar.setFixedHeight(self.SCAN_PROGRESS_HEIGHT)
211211
self._scan_progress_bar_simple.progressbar.setFixedWidth(self.SCAN_PROGRESS_WIDTH)
212+
# This one do not need dynamic styling on hover ScanProgressBar since user will hover on it probably later, when progress bar is big enough
212213
self._scan_progress_bar_full = ScanProgressBar(
213214
self, rpc_exposed=False, rpc_passthrough_children=False, enable_dynamic_stylesheet=False
214215
)
@@ -237,16 +238,16 @@ def _add_separator(self, separate_object: bool = False) -> QWidget | None:
237238

238239
# The actual line
239240
line = QFrame()
240-
line.setFrameShape(QFrame.VLine)
241-
line.setFrameShadow(QFrame.Sunken)
241+
line.setFrameShape(QFrame.Shape.VLine)
242+
line.setFrameShadow(QFrame.Shadow.Sunken)
242243
line.setFixedHeight(status_bar.sizeHint().height() - 2)
243244

244245
# Wrapper to center the line vertically -> work around for QFrame not being able to center itself
245246
wrapper = QWidget()
246247
vbox = QVBoxLayout(wrapper)
247248
vbox.setContentsMargins(0, 0, 0, 0)
248249
vbox.addStretch()
249-
vbox.addWidget(line, alignment=Qt.AlignHCenter)
250+
vbox.addWidget(line, alignment=Qt.AlignmentFlag.AlignHCenter)
250251
vbox.addStretch()
251252
wrapper.setFixedWidth(line.sizeHint().width())
252253

bec_widgets/widgets/progress/bec_progressbar/bec_progressbar.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ class ProgressState(Enum):
2020
@classmethod
2121
def from_bec_status(cls, status: str) -> "ProgressState":
2222
"""
23-
Map a BEC status string (open, paused, aborted, halted, closed)
23+
Map a BEC status string (open, paused, aborted, halt/halted, closed, user_completed)
2424
to the corresponding ProgressState.
2525
Any unknown status falls back to NORMAL.
2626
"""
2727
mapping = {
2828
"open": cls.NORMAL,
2929
"paused": cls.PAUSED,
3030
"aborted": cls.INTERRUPTED,
31+
"halt": cls.PAUSED,
3132
"halted": cls.PAUSED,
3233
"closed": cls.COMPLETED,
34+
"user_completed": cls.PAUSED,
3335
}
3436
return mapping.get(status.lower(), cls.NORMAL)
3537

@@ -104,9 +106,6 @@ def __init__(
104106
self.progressbar.setMinimumHeight(0)
105107
self.progressbar.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Ignored)
106108

107-
# Backwards-compatible alias used by existing tests and downstream code.
108-
self.center_label = self.progressbar
109-
110109
self._layout = QVBoxLayout(self)
111110
self._layout.setContentsMargins(self._padding_left_right, 0, self._padding_left_right, 0)
112111
self._layout.setSpacing(0)
@@ -339,6 +338,7 @@ def _sync_progressbar(self) -> None:
339338

340339
def _setup_style_sheet(self, *, chunk_radius: int) -> None:
341340
radius = int(round(self._corner_radius))
341+
chunk_color = self._state_colors[self._current_visual_state()].name()
342342
self.progressbar.setStyleSheet(f"""
343343
QProgressBar {{
344344
background-color: palette(mid);
@@ -348,7 +348,7 @@ def _setup_style_sheet(self, *, chunk_radius: int) -> None:
348348
text-align: center;
349349
}}
350350
QProgressBar::chunk {{
351-
background-color: palette(highlight);
351+
background-color: {chunk_color};
352352
border-radius: {chunk_radius}px;
353353
}}
354354
""")
@@ -377,6 +377,11 @@ def _initial_chunk_radius(self) -> int:
377377
return 0 if self._enable_dynamic_stylesheet else self._target_chunk_radius()
378378

379379
def _calculate_chunk_radius(self, target_radius: int) -> int:
380+
"""
381+
This whole chunk logic is to calculater radius based on the current size.
382+
If the radius is smaller than size of the progressbar it is just not applied.
383+
The chunk stylesheet logic is smoothing it as much as possible.
384+
"""
380385
if target_radius <= 0 or self._maximum <= 0:
381386
return 0
382387
fill_width = self.progressbar.width() * min(1.0, max(0.0, self._value / self._maximum))
@@ -385,6 +390,16 @@ def _calculate_chunk_radius(self, target_radius: int) -> int:
385390
return min(target_radius, max(1, int(fill_width / 2)))
386391

387392
def _apply_state_style(self) -> None:
393+
chunk_radius = self._chunk_radius
394+
if chunk_radius is None:
395+
target_radius = self._target_chunk_radius()
396+
chunk_radius = (
397+
self._calculate_chunk_radius(target_radius)
398+
if self._enable_dynamic_stylesheet
399+
else target_radius
400+
)
401+
self._chunk_radius = chunk_radius
402+
self._setup_style_sheet(chunk_radius=chunk_radius)
388403
color = self._state_colors[self._current_visual_state()]
389404
palette = self.progressbar.palette()
390405
palette.setColor(QPalette.ColorRole.Highlight, color)
@@ -406,20 +421,23 @@ def _get_label(self) -> str:
406421
if __name__ == "__main__": # pragma: no cover
407422
app = QApplication(sys.argv)
408423

409-
progressBar = BECProgressBar()
410-
progressBar.show()
411-
progressBar.set_minimum(-100)
412-
progressBar.set_maximum(0)
424+
progress_bar = BECProgressBar()
425+
progress_bar.setWindowTitle("BEC Progress Bar")
426+
progress_bar.resize(360, 48)
427+
progress_bar.set_minimum(-100)
428+
progress_bar.set_maximum(0)
429+
progress_bar.set_value(-100)
430+
progress_bar.show()
413431

414432
# Example of setting values
415433
def update_progress():
416-
value = progressBar._user_value + 2.5
417-
if value > progressBar._user_maximum:
418-
value = -100 # progressBar._maximum / progressBar._upsampling_factor
419-
progressBar.set_value(value)
434+
value = progress_bar._user_value + 2.5
435+
if value > progress_bar._user_maximum:
436+
value = progress_bar._user_minimum
437+
progress_bar.set_value(value)
420438

421-
timer = QTimer()
439+
timer = QTimer(progress_bar)
422440
timer.timeout.connect(update_progress)
423-
timer.start(200) # Update every half second
441+
timer.start(200)
424442

425443
sys.exit(app.exec())

0 commit comments

Comments
 (0)