mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 19:21:50 +02:00
fix(main_window): added expiration timer for scroll label for ClientInfoMessage
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from bec_lib.endpoints import MessageEndpoints
|
from bec_lib.endpoints import MessageEndpoints
|
||||||
from qtpy.QtCore import QEvent, QSize, Qt
|
from qtpy.QtCore import QEvent, QSize, Qt, QTimer
|
||||||
from qtpy.QtGui import QAction, QActionGroup, QIcon
|
from qtpy.QtGui import QAction, QActionGroup, QIcon
|
||||||
from qtpy.QtWidgets import QApplication, QFrame, QLabel, QMainWindow, QStyle, QVBoxLayout, QWidget
|
from qtpy.QtWidgets import QApplication, QFrame, QLabel, QMainWindow, QStyle, QVBoxLayout, QWidget
|
||||||
|
|
||||||
@ -80,6 +80,11 @@ class BECMainWindow(BECWidget, QMainWindow):
|
|||||||
)
|
)
|
||||||
status_bar.addWidget(self._client_info_label, 1)
|
status_bar.addWidget(self._client_info_label, 1)
|
||||||
|
|
||||||
|
# Timer to automatically clear client messages once they expire
|
||||||
|
self._client_info_expire_timer = QTimer(self)
|
||||||
|
self._client_info_expire_timer.setSingleShot(True)
|
||||||
|
self._client_info_expire_timer.timeout.connect(lambda: self._client_info_label.setText(""))
|
||||||
|
|
||||||
def _add_separator(self):
|
def _add_separator(self):
|
||||||
"""
|
"""
|
||||||
Add a vertically centred separator to the status bar.
|
Add a vertically centred separator to the status bar.
|
||||||
@ -222,9 +227,24 @@ class BECMainWindow(BECWidget, QMainWindow):
|
|||||||
|
|
||||||
@SafeSlot(dict, dict)
|
@SafeSlot(dict, dict)
|
||||||
def display_client_message(self, msg: dict, meta: dict):
|
def display_client_message(self, msg: dict, meta: dict):
|
||||||
|
"""
|
||||||
|
Display a client message in the status bar.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
msg(dict): The message to display, should contain:
|
||||||
|
meta(dict): Metadata about the message, usually empty.
|
||||||
|
"""
|
||||||
|
# self._client_info_label.setText("")
|
||||||
message = msg.get("message", "")
|
message = msg.get("message", "")
|
||||||
|
expiration = msg.get("expire", 0) # 0 → never expire
|
||||||
self._client_info_label.setText(message)
|
self._client_info_label.setText(message)
|
||||||
|
|
||||||
|
# Restart the expiration timer if necessary
|
||||||
|
if hasattr(self, "_client_info_expire_timer") and self._client_info_expire_timer.isActive():
|
||||||
|
self._client_info_expire_timer.stop()
|
||||||
|
if expiration and expiration > 0:
|
||||||
|
self._client_info_expire_timer.start(int(expiration * 1000))
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# General and Cleanup Methods
|
# General and Cleanup Methods
|
||||||
################################################################################
|
################################################################################
|
||||||
@ -259,6 +279,8 @@ class BECMainWindow(BECWidget, QMainWindow):
|
|||||||
child.close()
|
child.close()
|
||||||
child.deleteLater()
|
child.deleteLater()
|
||||||
|
|
||||||
|
if hasattr(self, "_client_info_expire_timer") and self._client_info_expire_timer.isActive():
|
||||||
|
self._client_info_expire_timer.stop()
|
||||||
# Status bar widgets cleanup
|
# Status bar widgets cleanup
|
||||||
self._client_info_label.cleanup()
|
self._client_info_label.cleanup()
|
||||||
super().cleanup()
|
super().cleanup()
|
||||||
@ -266,3 +288,12 @@ class BECMainWindow(BECWidget, QMainWindow):
|
|||||||
|
|
||||||
class UILaunchWindow(BECMainWindow):
|
class UILaunchWindow(BECMainWindow):
|
||||||
RPC = True
|
RPC = True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
main_window = UILaunchWindow()
|
||||||
|
main_window.show()
|
||||||
|
sys.exit(app.exec())
|
||||||
|
@ -112,6 +112,60 @@ def test_scroll_label_paint_event(qtbot):
|
|||||||
assert not pixmap.isNull()
|
assert not pixmap.isNull()
|
||||||
|
|
||||||
|
|
||||||
|
def test_display_client_message_with_expiration(qtbot, bec_main_window):
|
||||||
|
"""
|
||||||
|
A message with a finite 'expire' value should disappear once the timer
|
||||||
|
fires.
|
||||||
|
"""
|
||||||
|
test_msg = "This message should vanish fast"
|
||||||
|
expire_sec = 0.2
|
||||||
|
|
||||||
|
bec_main_window.display_client_message({"message": test_msg, "expire": expire_sec}, {})
|
||||||
|
|
||||||
|
assert bec_main_window._client_info_expire_timer.isActive()
|
||||||
|
assert bec_main_window._client_info_label.text() == test_msg
|
||||||
|
|
||||||
|
qtbot.waitUntil(lambda: not bec_main_window._client_info_expire_timer.isActive(), timeout=1000)
|
||||||
|
|
||||||
|
assert bec_main_window._client_info_label.text() == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_display_client_message_no_expiration(qtbot, bec_main_window):
|
||||||
|
"""
|
||||||
|
A message with 'expire' == 0 must persist and never start the timer.
|
||||||
|
"""
|
||||||
|
test_msg = "Persistent status message"
|
||||||
|
|
||||||
|
bec_main_window.display_client_message({"message": test_msg, "expire": 0}, {})
|
||||||
|
|
||||||
|
assert not bec_main_window._client_info_expire_timer.isActive()
|
||||||
|
assert bec_main_window._client_info_label.text() == test_msg
|
||||||
|
|
||||||
|
qtbot.wait(500)
|
||||||
|
assert bec_main_window._client_info_label.text() == test_msg
|
||||||
|
|
||||||
|
|
||||||
|
def test_display_client_message_overwrite_resets_timer(qtbot, bec_main_window):
|
||||||
|
"""
|
||||||
|
Sending a second message while the expiration timer is active should
|
||||||
|
overwrite the first and stop the timer if the second one is persistent.
|
||||||
|
"""
|
||||||
|
first_msg = "First (temporary)"
|
||||||
|
second_msg = "Second (persistent)"
|
||||||
|
|
||||||
|
bec_main_window.display_client_message({"message": first_msg, "expire": 0.3}, {})
|
||||||
|
qtbot.wait(200)
|
||||||
|
assert bec_main_window._client_info_expire_timer.isActive()
|
||||||
|
|
||||||
|
bec_main_window.display_client_message({"message": second_msg, "expire": 0}, {})
|
||||||
|
|
||||||
|
assert not bec_main_window._client_info_expire_timer.isActive()
|
||||||
|
assert bec_main_window._client_info_label.text() == second_msg
|
||||||
|
|
||||||
|
qtbot.wait(400)
|
||||||
|
assert bec_main_window._client_info_label.text() == second_msg
|
||||||
|
|
||||||
|
|
||||||
#################################################################
|
#################################################################
|
||||||
# Tests for BECWebLinksMixin (webbrowser opening)
|
# Tests for BECWebLinksMixin (webbrowser opening)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user