1
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2026-04-18 22:35:38 +02:00

Compare commits

...

8 Commits

Author SHA1 Message Date
semantic-release
8c3d6334f6 2.15.0
Automatically generated by python-semantic-release
2025-06-15 10:39:36 +00:00
30acc4c236 test(main_window): BECMainWindow tests extended 2025-06-15 12:38:56 +02:00
0dec78afba feat(main_window): main window can display the messages from the send_client_info as a scrolling horizontal text; closes #700 2025-06-15 12:38:56 +02:00
57b9a57a63 refactor(main_window): app id is displayed as QLabel instead of message 2025-06-15 12:38:56 +02:00
644be621f2 fix(main_window): central widget cleanup check to not remove None 2025-06-15 12:38:56 +02:00
semantic-release
d07265b86d 2.14.0
Automatically generated by python-semantic-release
2025-06-13 16:21:17 +00:00
f0d48a0508 refactor(image_roi_tree): shape switch logic adjusted to reduce code repetition 2025-06-13 18:20:37 +02:00
af8db0bede feat(image_roi): added EllipticalROI 2025-06-13 18:20:37 +02:00
10 changed files with 634 additions and 50 deletions

View File

@@ -1,6 +1,43 @@
# CHANGELOG
## v2.15.0 (2025-06-15)
### Bug Fixes
- **main_window**: Central widget cleanup check to not remove None
([`644be62`](https://github.com/bec-project/bec_widgets/commit/644be621f20cf09037da763f6217df9d1e4642bc))
### Features
- **main_window**: Main window can display the messages from the send_client_info as a scrolling
horizontal text; closes #700
([`0dec78a`](https://github.com/bec-project/bec_widgets/commit/0dec78afbaddbef98d20949d3a0ba4e0dc8529df))
### Refactoring
- **main_window**: App id is displayed as QLabel instead of message
([`57b9a57`](https://github.com/bec-project/bec_widgets/commit/57b9a57a631f267a8cb3622bf73035ffb15510e6))
### Testing
- **main_window**: Becmainwindow tests extended
([`30acc4c`](https://github.com/bec-project/bec_widgets/commit/30acc4c236bfbfed19f56512b264a52b4359e6c1))
## v2.14.0 (2025-06-13)
### Features
- **image_roi**: Added EllipticalROI
([`af8db0b`](https://github.com/bec-project/bec_widgets/commit/af8db0bede32dd10ad72671a8c2978ca884f4994))
### Refactoring
- **image_roi_tree**: Shape switch logic adjusted to reduce code repetition
([`f0d48a0`](https://github.com/bec-project/bec_widgets/commit/f0d48a05085bb8c628e516d4a976d776ee63c7c3))
## v2.13.2 (2025-06-13)
### Bug Fixes

View File

@@ -1044,6 +1044,128 @@ class DeviceLineEdit(RPCBase):
"""
class EllipticalROI(RPCBase):
"""Elliptical Region of Interest with centre/width/height tracking and auto-labelling."""
@property
@rpc_call
def label(self) -> "str":
"""
Gets the display name of this ROI.
Returns:
str: The current name of the ROI.
"""
@label.setter
@rpc_call
def label(self) -> "str":
"""
Gets the display name of this ROI.
Returns:
str: The current name of the ROI.
"""
@property
@rpc_call
def movable(self) -> "bool":
"""
Gets whether this ROI is movable.
Returns:
bool: True if the ROI can be moved, False otherwise.
"""
@movable.setter
@rpc_call
def movable(self) -> "bool":
"""
Gets whether this ROI is movable.
Returns:
bool: True if the ROI can be moved, False otherwise.
"""
@property
@rpc_call
def line_color(self) -> "str":
"""
Gets the current line color of the ROI.
Returns:
str: The current line color as a string (e.g., hex color code).
"""
@line_color.setter
@rpc_call
def line_color(self) -> "str":
"""
Gets the current line color of the ROI.
Returns:
str: The current line color as a string (e.g., hex color code).
"""
@property
@rpc_call
def line_width(self) -> "int":
"""
Gets the current line width of the ROI.
Returns:
int: The current line width in pixels.
"""
@line_width.setter
@rpc_call
def line_width(self) -> "int":
"""
Gets the current line width of the ROI.
Returns:
int: The current line width in pixels.
"""
@rpc_call
def get_coordinates(self, typed: "bool | None" = None) -> "dict | tuple":
"""
Return the ellipse's centre and size.
Args:
typed (bool | None): If True returns dict; otherwise tuple.
"""
@rpc_call
def get_data_from_image(
self, image_item: "pg.ImageItem | None" = None, returnMappedCoords: "bool" = False, **kwargs
):
"""
Wrapper around `pyqtgraph.ROI.getArrayRegion`.
Args:
image_item (pg.ImageItem or None): The ImageItem to sample. If None, auto-detects
the first `ImageItem` in the same GraphicsScene as this ROI.
returnMappedCoords (bool): If True, also returns the coordinate array generated by
*getArrayRegion*.
**kwargs: Additional keyword arguments passed to *getArrayRegion* or *affineSlice*,
such as `axes`, `order`, `shape`, etc.
Returns:
ndarray: Pixel data inside the ROI, or (data, coords) if *returnMappedCoords* is True.
"""
@rpc_call
def set_position(self, x: "float", y: "float"):
"""
Sets the position of the ROI.
Args:
x (float): The x-coordinate of the new position.
y (float): The y-coordinate of the new position.
"""
class Image(RPCBase):
"""Image widget for displaying 2D data."""
@@ -1529,7 +1651,7 @@ class Image(RPCBase):
@rpc_call
def add_roi(
self,
kind: "Literal['rect', 'circle']" = "rect",
kind: "Literal['rect', 'circle', 'ellipse']" = "rect",
name: "str | None" = None,
line_width: "int | None" = 5,
pos: "tuple[float, float] | None" = (10, 10),

View File

@@ -0,0 +1,89 @@
from qtpy.QtCore import QTimer
from qtpy.QtGui import QFontMetrics, QPainter
from qtpy.QtWidgets import QLabel
class ScrollLabel(QLabel):
"""A QLabel that scrolls its text horizontally across the widget."""
def __init__(self, parent=None, speed_ms=30, step_px=1, delay_ms=2000):
super().__init__(parent=parent)
self._offset = 0
self._text_width = 0
# scrolling timer (runs continuously once started)
self._timer = QTimer(self)
self._timer.setInterval(speed_ms)
self._timer.timeout.connect(self._scroll)
# delaybeforescroll timer (singleshot)
self._delay_timer = QTimer(self)
self._delay_timer.setSingleShot(True)
self._delay_timer.setInterval(delay_ms)
self._delay_timer.timeout.connect(self._timer.start)
self._step_px = step_px
def setText(self, text):
super().setText(text)
fm = QFontMetrics(self.font())
self._text_width = fm.horizontalAdvance(text)
self._offset = 0
self._update_timer()
def resizeEvent(self, event):
super().resizeEvent(event)
self._update_timer()
def _update_timer(self):
"""
Decide whether to start or stop scrolling.
If the text is wider than the visible area, start a singleshot
delay timer (2s by default). Scrolling begins only after this
delay. Any change (resize or new text) restarts the logic.
"""
needs_scroll = self._text_width > self.width()
if needs_scroll:
if self._timer.isActive():
self._timer.stop()
self._offset = 0
if not self._delay_timer.isActive():
self._delay_timer.start()
else:
if self._delay_timer.isActive():
self._delay_timer.stop()
if self._timer.isActive():
self._timer.stop()
self.update()
def _scroll(self):
self._offset += self._step_px
if self._offset >= self._text_width:
self._offset = 0
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.TextAntialiasing)
text = self.text()
if not text:
return
fm = QFontMetrics(self.font())
y = (self.height() + fm.ascent() - fm.descent()) // 2
if self._text_width <= self.width():
painter.drawText(0, y, text)
else:
x = -self._offset
gap = 50 # space between repeating text blocks
while x < self.width():
painter.drawText(x, y, text)
x += self._text_width + gap
def cleanup(self):
"""Stop all timers to prevent memory leaks."""
if self._timer.isActive():
self._timer.stop()
if self._delay_timer.isActive():
self._delay_timer.stop()

View File

@@ -1,16 +1,17 @@
import os
from qtpy.QtCore import QEvent, QSize
from bec_lib.endpoints import MessageEndpoints
from qtpy.QtCore import QEvent, QSize, Qt
from qtpy.QtGui import QAction, QActionGroup, QIcon
from qtpy.QtWidgets import QApplication, QMainWindow, QStyle
from qtpy.QtWidgets import QApplication, QFrame, QLabel, QMainWindow, QStyle, QVBoxLayout, QWidget
import bec_widgets
from bec_widgets.utils import UILoader
from bec_widgets.utils.bec_widget import BECWidget
from bec_widgets.utils.colors import apply_theme
from bec_widgets.utils.container_utils import WidgetContainerUtils
from bec_widgets.utils.error_popups import SafeSlot
from bec_widgets.utils.widget_io import WidgetHierarchy
from bec_widgets.widgets.containers.main_window.addons.scroll_label import ScrollLabel
from bec_widgets.widgets.containers.main_window.addons.web_links import BECWebLinksMixin
MODULE_PATH = os.path.dirname(bec_widgets.__file__)
@@ -36,6 +37,14 @@ class BECMainWindow(BECWidget, QMainWindow):
self._init_ui()
self._connect_to_theme_change()
# Connections to BEC Notifications
self.bec_dispatcher.connect_slot(
self.display_client_message, MessageEndpoints.client_info()
)
################################################################################
# MainWindow Elements Initialization
################################################################################
def _init_ui(self):
# Set the icon
@@ -43,40 +52,72 @@ class BECMainWindow(BECWidget, QMainWindow):
# Set Menu and Status bar
self._setup_menu_bar()
self._init_status_bar_widgets()
# BEC Specific UI
self.display_app_id()
def _init_status_bar_widgets(self):
"""
Prepare the BEC specific widgets in the status bar.
"""
status_bar = self.statusBar()
# Left: AppID label
self._app_id_label = QLabel()
self._app_id_label.setAlignment(
Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
)
status_bar.addWidget(self._app_id_label)
# Add a separator after the app ID label
self._add_separator()
# Centre: Clientinfo label (stretch=1 so it expands)
self._client_info_label = ScrollLabel()
self._client_info_label.setAlignment(
Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
)
status_bar.addWidget(self._client_info_label, 1)
def _add_separator(self):
"""
Add a vertically centred separator to the status bar.
"""
status_bar = self.statusBar()
# The actual line
line = QFrame()
line.setFrameShape(QFrame.VLine)
line.setFrameShadow(QFrame.Sunken)
line.setFixedHeight(status_bar.sizeHint().height() - 2)
# Wrapper to center the line vertically -> work around for QFrame not being able to center itself
wrapper = QWidget()
vbox = QVBoxLayout(wrapper)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.addStretch()
vbox.addWidget(line, alignment=Qt.AlignHCenter)
vbox.addStretch()
wrapper.setFixedWidth(line.sizeHint().width())
status_bar.addWidget(wrapper)
def _init_bec_icon(self):
icon = self.app.windowIcon()
if icon.isNull():
print("No icon is set, setting default icon")
icon = QIcon()
icon.addFile(
os.path.join(MODULE_PATH, "assets", "app_icons", "bec_widgets_icon.png"),
size=QSize(48, 48),
)
self.app.setWindowIcon(icon)
else:
print("An icon is set")
def load_ui(self, ui_file):
loader = UILoader(self)
self.ui = loader.loader(ui_file)
self.setCentralWidget(self.ui)
def display_app_id(self):
"""
Display the app ID in the status bar.
"""
if self.bec_dispatcher.cli_server is None:
status_message = "Not connected"
else:
# Get the server ID from the dispatcher
server_id = self.bec_dispatcher.cli_server.gui_id
status_message = f"App ID: {server_id}"
self.statusBar().showMessage(status_message)
def _fetch_theme(self) -> str:
return self.app.theme.theme
@@ -164,8 +205,37 @@ class BECMainWindow(BECWidget, QMainWindow):
help_menu.addAction(widgets_docs)
help_menu.addAction(bug_report)
################################################################################
# Status Bar Addons
################################################################################
def display_app_id(self):
"""
Display the app ID in the status bar.
"""
if self.bec_dispatcher.cli_server is None:
status_message = "Not connected"
else:
# Get the server ID from the dispatcher
server_id = self.bec_dispatcher.cli_server.gui_id
status_message = f"App ID: {server_id}"
self._app_id_label.setText(status_message)
@SafeSlot(dict, dict)
def display_client_message(self, msg: dict, meta: dict):
message = msg.get("message", "")
self._client_info_label.setText(message)
################################################################################
# General and Cleanup Methods
################################################################################
@SafeSlot(str)
def change_theme(self, theme: str):
"""
Change the theme of the application.
Args:
theme(str): The theme to apply, either "light" or "dark".
"""
apply_theme(theme)
def event(self, event):
@@ -175,8 +245,9 @@ class BECMainWindow(BECWidget, QMainWindow):
def cleanup(self):
central_widget = self.centralWidget()
central_widget.close()
central_widget.deleteLater()
if central_widget is not None:
central_widget.close()
central_widget.deleteLater()
if not isinstance(central_widget, BECWidget):
# if the central widget is not a BECWidget, we need to call the cleanup method
# of all widgets whose parent is the current BECMainWindow
@@ -187,6 +258,9 @@ class BECMainWindow(BECWidget, QMainWindow):
child.cleanup()
child.close()
child.deleteLater()
# Status bar widgets cleanup
self._client_info_label.cleanup()
super().cleanup()

View File

@@ -24,6 +24,7 @@ from bec_widgets.widgets.plots.plot_base import PlotBase
from bec_widgets.widgets.plots.roi.image_roi import (
BaseROI,
CircularROI,
EllipticalROI,
RectangularROI,
ROIController,
)
@@ -554,7 +555,7 @@ class ImageBase(PlotBase):
def add_roi(
self,
kind: Literal["rect", "circle"] = "rect",
kind: Literal["rect", "circle", "ellipse"] = "rect",
name: str | None = None,
line_width: int | None = 5,
pos: tuple[float, float] | None = (10, 10),
@@ -599,6 +600,16 @@ class ImageBase(PlotBase):
movable=movable,
**pg_kwargs,
)
elif kind == "ellipse":
roi = EllipticalROI(
pos=pos,
size=size,
parent_image=self,
line_width=line_width,
label=name,
movable=movable,
**pg_kwargs,
)
else:
raise ValueError("kind must be 'rect' or 'circle'")

View File

@@ -24,6 +24,7 @@ from bec_widgets.utils.toolbar import MaterialIconAction, ModularToolBar
from bec_widgets.widgets.plots.roi.image_roi import (
BaseROI,
CircularROI,
EllipticalROI,
RectangularROI,
ROIController,
)
@@ -121,11 +122,21 @@ class ROIPropertyTree(BECWidget, QWidget):
# --------------------------------------------------------------------- UI
def _init_toolbar(self):
tb = ModularToolBar(self, self, orientation="horizontal")
self._draw_actions: dict[str, MaterialIconAction] = {}
# --- ROI draw actions (toggleable) ---
self.add_rect_action = MaterialIconAction("add_box", "Add Rect ROI", True, self)
self.add_circle_action = MaterialIconAction("add_circle", "Add Circle ROI", True, self)
tb.add_action("Add Rect ROI", self.add_rect_action, self)
self._draw_actions["rect"] = self.add_rect_action
self.add_circle_action = MaterialIconAction("add_circle", "Add Circle ROI", True, self)
tb.add_action("Add Circle ROI", self.add_circle_action, self)
self._draw_actions["circle"] = self.add_circle_action
# --- Ellipse ROI draw action ---
self.add_ellipse_action = MaterialIconAction("vignette", "Add Ellipse ROI", True, self)
tb.add_action("Add Ellipse ROI", self.add_ellipse_action, self)
self._draw_actions["ellipse"] = self.add_ellipse_action
for mode, act in self._draw_actions.items():
act.action.toggled.connect(lambda on, m=mode: self._on_draw_action_toggled(m, on))
# Expand/Collapse toggle
self.expand_toggle = MaterialIconAction(
@@ -174,17 +185,9 @@ class ROIPropertyTree(BECWidget, QWidget):
self.controller.paletteChanged.connect(lambda cmap: setattr(self.cmap, "colormap", cmap))
# ROI drawing state
self._roi_draw_mode = None # 'rect' | 'circle' | None
self._roi_draw_mode = None # 'rect' | 'circle' | 'ellipse' | None
self._roi_start_pos = None # QPointF in image coords
self._temp_roi = None # live ROI being resized while dragging
# toggle handlers
self.add_rect_action.action.toggled.connect(
lambda on: self._set_roi_draw_mode("rect" if on else None)
)
self.add_circle_action.action.toggled.connect(
lambda on: self._set_roi_draw_mode("circle" if on else None)
)
# capture mouse events on the plot scene
self.plot.scene().installEventFilter(self)
@@ -214,16 +217,12 @@ class ROIPropertyTree(BECWidget, QWidget):
return str(value)
def _set_roi_draw_mode(self, mode: str | None):
# Ensure only the selected action is toggled on
if mode == "rect":
self.add_rect_action.action.setChecked(True)
self.add_circle_action.action.setChecked(False)
elif mode == "circle":
self.add_rect_action.action.setChecked(False)
self.add_circle_action.action.setChecked(True)
else:
self.add_rect_action.action.setChecked(False)
self.add_circle_action.action.setChecked(False)
# Update toolbar actions so that only the selected mode is checked
for m, act in self._draw_actions.items():
act.action.blockSignals(True)
act.action.setChecked(m == mode)
act.action.blockSignals(False)
self._roi_draw_mode = mode
self._roi_start_pos = None
# remove any unfinished temp ROI
@@ -231,6 +230,15 @@ class ROIPropertyTree(BECWidget, QWidget):
self.plot.removeItem(self._temp_roi)
self._temp_roi = None
def _on_draw_action_toggled(self, mode: str, checked: bool):
if checked:
# Activate selected mode
self._set_roi_draw_mode(mode)
else:
# If the active mode is being unchecked, clear mode
if self._roi_draw_mode == mode:
self._set_roi_draw_mode(None)
def eventFilter(self, obj, event):
if self._roi_draw_mode is None:
return super().eventFilter(obj, event)
@@ -243,12 +251,18 @@ class ROIPropertyTree(BECWidget, QWidget):
parent_image=self.image_widget,
resize_handles=False,
)
if self._roi_draw_mode == "circle":
elif self._roi_draw_mode == "circle":
self._temp_roi = CircularROI(
pos=[self._roi_start_pos.x() - 2.5, self._roi_start_pos.y() - 2.5],
size=[5, 5],
parent_image=self.image_widget,
)
elif self._roi_draw_mode == "ellipse":
self._temp_roi = EllipticalROI(
pos=[self._roi_start_pos.x() - 2.5, self._roi_start_pos.y() - 2.5],
size=[5, 5],
parent_image=self.image_widget,
)
self.plot.addItem(self._temp_roi)
return True
elif event.type() == QEvent.GraphicsSceneMouseMove and self._temp_roi is not None:
@@ -258,13 +272,19 @@ class ROIPropertyTree(BECWidget, QWidget):
if self._roi_draw_mode == "rect":
self._temp_roi.setSize([dx, dy])
if self._roi_draw_mode == "circle":
elif self._roi_draw_mode == "circle":
r = max(
1, math.hypot(dx, dy)
) # radius never smaller than 1 for safety of handle mapping, otherwise SEGFAULT
d = 2 * r # diameter
self._temp_roi.setPos(self._roi_start_pos.x() - r, self._roi_start_pos.y() - r)
self._temp_roi.setSize([d, d])
elif self._roi_draw_mode == "ellipse":
# Safeguard: enforce a minimum ellipse width/height of 2 px
min_dim = 2.0
w = dx if abs(dx) >= min_dim else math.copysign(min_dim, dx or 1.0)
h = dy if abs(dy) >= min_dim else math.copysign(min_dim, dy or 1.0)
self._temp_roi.setSize([w, h])
return True
elif (
event.type() == QEvent.GraphicsSceneMouseRelease

View File

@@ -750,6 +750,92 @@ class CircularROI(BaseROI, pg.CircleROI):
return None
class EllipticalROI(BaseROI, pg.EllipseROI):
"""
Elliptical Region of Interest with centre/width/height tracking and auto-labelling.
Mirrors the behaviour of ``CircularROI`` but supports independent
horizontal and vertical radii.
"""
centerChanged = Signal(float, float, float, float) # cx, cy, width, height
centerReleased = Signal(float, float, float, float)
def __init__(
self,
*,
pos,
size,
pen=None,
config: ConnectionConfig | None = None,
gui_id: str | None = None,
parent_image: Image | None = None,
label: str | None = None,
line_color: str | None = None,
line_width: int = 5,
movable: bool = True,
**extra_pg,
):
super().__init__(
config=config,
gui_id=gui_id,
parent_image=parent_image,
label=label,
line_color=line_color,
line_width=line_width,
pos=pos,
size=size,
pen=pen,
movable=movable,
**extra_pg,
)
self.sigRegionChanged.connect(self._on_region_changed)
self._adorner = LabelAdorner(self)
self.hoverPen = fn.mkPen(color=(255, 0, 0), width=3, style=QtCore.Qt.DashLine)
self.handleHoverPen = fn.mkPen("lime", width=4)
def add_scale_handle(self):
"""Add scale handles to the elliptical ROI."""
self._addHandles() # delegates to pg.EllipseROI
def _on_region_changed(self):
w = abs(self.state["size"][0])
h = abs(self.state["size"][1])
cx = self.pos().x() + w / 2
cy = self.pos().y() + h / 2
self.centerChanged.emit(cx, cy, w, h)
self.parent_plot_item.vb.update()
def mouseDragEvent(self, ev):
super().mouseDragEvent(ev)
if ev.isFinish():
w = abs(self.state["size"][0])
h = abs(self.state["size"][1])
cx = self.pos().x() + w / 2
cy = self.pos().y() + h / 2
self.centerReleased.emit(cx, cy, w, h)
def get_coordinates(self, typed: bool | None = None) -> dict | tuple:
"""
Return the ellipse's centre and size.
Args:
typed (bool | None): If True returns dict; otherwise tuple.
"""
if typed is None:
typed = self.description
w, h = map(abs, self.state["size"]) # raw diameters
major, minor = (w, h) if w >= h else (h, w)
cx = self.pos().x() + w / 2
cy = self.pos().y() + h / 2
if typed:
return {"center_x": cx, "center_y": cy, "major_axis": major, "minor_axis": minor}
return (cx, cy, major, minor)
class ROIController(QObject):
"""Manages a collection of ROIs (Regions of Interest) with palette-assigned colors.

View File

@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "bec_widgets"
version = "2.13.2"
version = "2.15.0"
description = "BEC Widgets"
requires-python = ">=3.10"
classifiers = [

View File

@@ -6,16 +6,21 @@ import numpy as np
import pytest
from bec_widgets.widgets.plots.image.image import Image
from bec_widgets.widgets.plots.roi.image_roi import CircularROI, RectangularROI, ROIController
from bec_widgets.widgets.plots.roi.image_roi import (
CircularROI,
EllipticalROI,
RectangularROI,
ROIController,
)
from tests.unit_tests.client_mocks import mocked_client
from tests.unit_tests.conftest import create_widget
@pytest.fixture(params=["rect", "circle"])
@pytest.fixture(params=["rect", "circle", "ellipse"])
def bec_image_widget_with_roi(qtbot, request, mocked_client):
"""Return (widget, roi, shape_label) for each ROI class."""
roi_type: Literal["rect", "circle"] = request.param
roi_type: Literal["rect", "circle", "ellipse"] = request.param
# Build an Image widget with a trivial 100×100 zeros array
widget: Image = create_widget(qtbot, Image, client=mocked_client)
@@ -39,7 +44,12 @@ def test_default_properties(bec_image_widget_with_roi):
assert roi.line_width == 5
# concrete subclass type
assert isinstance(roi, RectangularROI) if roi_type == "rect" else isinstance(roi, CircularROI)
if roi_type == "rect":
assert isinstance(roi, RectangularROI)
elif roi_type == "circle":
assert isinstance(roi, CircularROI)
elif roi_type == "ellipse":
assert isinstance(roi, EllipticalROI)
def test_coordinate_structures(bec_image_widget_with_roi):
@@ -98,7 +108,7 @@ def test_color_uniqueness_across_multiple_rois(qtbot, mocked_client):
widget: Image = create_widget(qtbot, Image, client=mocked_client)
# add two of each ROI type
for _kind in ("rect", "circle"):
for _kind in ("rect", "circle", "ellipse"):
widget.add_roi(kind=_kind)
widget.add_roi(kind=_kind)

View File

@@ -0,0 +1,135 @@
import webbrowser
import pytest
from qtpy.QtWidgets import QFrame
from bec_widgets.widgets.containers.main_window.addons.scroll_label import ScrollLabel
from bec_widgets.widgets.containers.main_window.addons.web_links import BECWebLinksMixin
from bec_widgets.widgets.containers.main_window.main_window import BECMainWindow
from .client_mocks import mocked_client
from .conftest import create_widget
@pytest.fixture
def bec_main_window(qtbot, mocked_client):
widget = BECMainWindow(client=mocked_client)
qtbot.addWidget(widget)
qtbot.waitExposed(widget)
yield widget
#################################################################
# Tests for BECMainWindow Initialization and Functionality
#################################################################
def test_bec_main_window_initialization(bec_main_window):
assert isinstance(bec_main_window, BECMainWindow)
assert bec_main_window.windowTitle() == "BEC"
assert bec_main_window.app is not None
assert bec_main_window.statusBar() is not None
assert bec_main_window._app_id_label is not None
def test_bec_main_window_display_client_message(qtbot, bec_main_window):
"""
Verify that display_client_message updates the clientinfo label.
"""
test_msg = "Client connected successfully"
bec_main_window.display_client_message({"message": test_msg}, {})
qtbot.wait(200)
assert bec_main_window._client_info_label.text() == test_msg
def test_status_bar_has_separator(bec_main_window):
"""Ensure the status bar contains at least one vertical separator."""
status_bar = bec_main_window.statusBar()
separators = [w for w in status_bar.findChildren(QFrame) if w.frameShape() == QFrame.VLine]
assert separators, "Expected at least one QFrame separator in the status bar."
#################################################################
# Tests for BECMainWindow Addons
#################################################################
#################################################################
# Tests for ScrollLabel behaviour
def test_scroll_label_does_not_scroll_when_text_fits(qtbot):
"""Label with short text should not activate scrolling timer."""
lbl = create_widget(qtbot, ScrollLabel) # shorten delay for test speed
qtbot.addWidget(lbl)
lbl.resize(200, 20)
lbl.setText("Short text")
# Process events to allow timer logic to run
qtbot.wait(200)
assert not lbl._timer.isActive()
assert not lbl._delay_timer.isActive()
def test_scroll_label_starts_scrolling(qtbot):
"""Label with long text should start _delay_timer; later _timer becomes active."""
lbl = create_widget(qtbot, ScrollLabel, delay_ms=100)
lbl.resize(150, 20)
long_text = "This is a very long piece of text that should definitely overflow the label width"
lbl.setText(long_text)
# Immediately after setText, only delaytimer should be active
assert lbl._delay_timer.isActive()
assert not lbl._timer.isActive()
# Wait until scrolling timer becomes active
qtbot.waitUntil(lambda: lbl._timer.isActive(), timeout=2000)
assert lbl._timer.isActive()
def test_scroll_label_scroll_method(qtbot):
"""Directly exercise _scroll to ensure offset advances and paintEvent is invoked."""
lbl = create_widget(qtbot, ScrollLabel, step_px=5) # shorten delay for test speed
qtbot.addWidget(lbl)
lbl.resize(120, 20)
lbl.setText("x" * 200) # long text to guarantee overflow
qtbot.wait(200) # let timers configure themselves
# Capture current offset and force a manual scroll tick
old_offset = lbl._offset
lbl._scroll()
assert lbl._offset == old_offset + 5
def test_scroll_label_paint_event(qtbot):
"""
Grab the widget as a pixmap; this calls paintEvent under the hood
and ensures no exceptions occur during rendering.
"""
lbl = create_widget(qtbot, ScrollLabel) # shorten delay for test speed
qtbot.addWidget(lbl)
lbl.resize(180, 20)
lbl.setText("Rendering check")
lbl.show()
qtbot.wait(200) # allow Qt to schedule a paint
pixmap = lbl.grab()
assert not pixmap.isNull()
#################################################################
# Tests for BECWebLinksMixin (webbrowser opening)
def test_bec_weblinks(monkeypatch):
opened_urls = []
def fake_open(url):
opened_urls.append(url)
monkeypatch.setattr(webbrowser, "open", fake_open)
BECWebLinksMixin.open_bec_docs()
BECWebLinksMixin.open_bec_widgets_docs()
BECWebLinksMixin.open_bec_bug_report()
assert opened_urls == [
"https://beamline-experiment-control.readthedocs.io/en/latest/",
"https://bec.readthedocs.io/projects/bec-widgets/en/latest/",
"https://gitlab.psi.ch/groups/bec/-/issues/",
]