mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-04-08 01:37:53 +02:00
Compare commits
2 Commits
fix/temp-d
...
fix/temp-d
| Author | SHA1 | Date | |
|---|---|---|---|
| 65dceead0c | |||
| 20125139f0 |
@@ -336,6 +336,13 @@ class BECDock(RPCBase):
|
||||
Detach the dock from the parent dock area.
|
||||
"""
|
||||
|
||||
@rpc_call
|
||||
def close(self):
|
||||
"""
|
||||
Close the dock area and cleanup.
|
||||
Has to be implemented to overwrite pyqtgraph event accept in Container close.
|
||||
"""
|
||||
|
||||
|
||||
class BECDockArea(RPCBase, BECGuiClientMixin):
|
||||
@property
|
||||
@@ -394,9 +401,9 @@ class BECDockArea(RPCBase, BECGuiClientMixin):
|
||||
name: "str" = None,
|
||||
position: "Literal['bottom', 'top', 'left', 'right', 'above', 'below']" = None,
|
||||
relative_to: "BECDock | None" = None,
|
||||
temporary: "bool" = False,
|
||||
closable: "bool" = True,
|
||||
floating: "bool" = False,
|
||||
temporary: "bool" = False,
|
||||
prefix: "str" = "dock",
|
||||
widget: "str | QWidget | None" = None,
|
||||
row: "int" = None,
|
||||
@@ -411,9 +418,9 @@ class BECDockArea(RPCBase, BECGuiClientMixin):
|
||||
name(str): The name of the dock to be displayed and for further references. Has to be unique.
|
||||
position(Literal["bottom", "top", "left", "right", "above", "below"]): The position of the dock.
|
||||
relative_to(BECDock): The dock to which the new dock should be added relative to.
|
||||
temp(bool): Whether the dock is temporary. Upon closing the dock is not returned to the parent dock area.
|
||||
closable(bool): Whether the dock is closable.
|
||||
floating(bool): Whether the dock is detached after creating.
|
||||
temporary(bool): Whether the dock is temporary. After closing the dock do not return to the parent DockArea.
|
||||
prefix(str): The prefix for the dock name if no name is provided.
|
||||
widget(str|QWidget|None): The widget to be added to the dock. While using RPC, only BEC RPC widgets from RPCWidgetHandler are allowed.
|
||||
row(int): The row of the added widget.
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any, Literal, Optional
|
||||
|
||||
from qtpy.QtCore import QSize
|
||||
from pydantic import Field
|
||||
from pyqtgraph.dockarea import Dock, DockLabel
|
||||
from qtpy import QtCore, QtGui
|
||||
@@ -116,6 +117,7 @@ class BECDock(BECWidget, Dock):
|
||||
"remove",
|
||||
"attach",
|
||||
"detach",
|
||||
"close",
|
||||
]
|
||||
|
||||
def __init__(
|
||||
@@ -126,6 +128,7 @@ class BECDock(BECWidget, Dock):
|
||||
name: str | None = None,
|
||||
client=None,
|
||||
gui_id: str | None = None,
|
||||
temp: bool = False,
|
||||
closable: bool = True,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
@@ -147,6 +150,8 @@ class BECDock(BECWidget, Dock):
|
||||
# Layout Manager
|
||||
self.layout_manager = GridLayoutManager(self.layout)
|
||||
|
||||
self.temp = temp
|
||||
|
||||
def dropEvent(self, event):
|
||||
source = event.source()
|
||||
old_area = source.area
|
||||
@@ -172,6 +177,24 @@ class BECDock(BECWidget, Dock):
|
||||
else:
|
||||
super().float()
|
||||
|
||||
if self.temp:
|
||||
self.make_dock_temporary()
|
||||
|
||||
def make_dock_temporary(self):
|
||||
"""
|
||||
Make the dock temporary.
|
||||
"""
|
||||
|
||||
from bec_widgets.widgets.dock import BECDockArea
|
||||
|
||||
self.orig_area.docks.pop(self.name(), None)
|
||||
self.orig_area = BECDockArea()
|
||||
self.area = self.orig_area
|
||||
self.area.panels[self.name()] = self
|
||||
self.config.parent_dock_area = self.area.gui_id
|
||||
self.area.temporary = False
|
||||
self.hide_title_bar()
|
||||
|
||||
@property
|
||||
def widget_list(self) -> list[BECWidget]:
|
||||
"""
|
||||
@@ -333,3 +356,7 @@ class BECDock(BECWidget, Dock):
|
||||
self.cleanup()
|
||||
super().close()
|
||||
self.parent_dock_area.dock_area.docks.pop(self.name(), None)
|
||||
|
||||
if self.temp:
|
||||
self.area.deleteLater()
|
||||
self.deleteLater()
|
||||
|
||||
@@ -278,9 +278,9 @@ class BECDockArea(BECWidget, QWidget):
|
||||
name: str = None,
|
||||
position: Literal["bottom", "top", "left", "right", "above", "below"] = None,
|
||||
relative_to: BECDock | None = None,
|
||||
temporary: bool = False,
|
||||
closable: bool = True,
|
||||
floating: bool = False,
|
||||
temporary: bool = False,
|
||||
prefix: str = "dock",
|
||||
widget: str | QWidget | None = None,
|
||||
row: int = None,
|
||||
@@ -295,9 +295,9 @@ class BECDockArea(BECWidget, QWidget):
|
||||
name(str): The name of the dock to be displayed and for further references. Has to be unique.
|
||||
position(Literal["bottom", "top", "left", "right", "above", "below"]): The position of the dock.
|
||||
relative_to(BECDock): The dock to which the new dock should be added relative to.
|
||||
temp(bool): Whether the dock is temporary. Upon closing the dock is not returned to the parent dock area.
|
||||
closable(bool): Whether the dock is closable.
|
||||
floating(bool): Whether the dock is detached after creating.
|
||||
temporary(bool): Whether the dock is temporary. After closing the dock do not return to the parent DockArea.
|
||||
prefix(str): The prefix for the dock name if no name is provided.
|
||||
widget(str|QWidget|None): The widget to be added to the dock. While using RPC, only BEC RPC widgets from RPCWidgetHandler are allowed.
|
||||
row(int): The row of the added widget.
|
||||
@@ -319,21 +319,16 @@ class BECDockArea(BECWidget, QWidget):
|
||||
if position is None:
|
||||
position = "bottom"
|
||||
|
||||
if temporary:
|
||||
target_area = self.dock_area.addTempArea()
|
||||
else:
|
||||
target_area = self.dock_area
|
||||
|
||||
dock = BECDock(name=name, parent_dock_area=self, closable=closable)
|
||||
dock = BECDock(name=name, parent_dock_area=self, closable=closable, temp=temporary)
|
||||
dock.config.position = position
|
||||
self.config.docks[name] = dock.config
|
||||
|
||||
target_area.addDock(dock=dock, position=position, relativeTo=relative_to)
|
||||
self.dock_area.addDock(dock=dock, position=position, relativeTo=relative_to)
|
||||
|
||||
if len(target_area.docks) <= 1:
|
||||
if len(self.dock_area.docks) <= 1:
|
||||
dock.hide_title_bar()
|
||||
elif len(target_area.docks) > 1:
|
||||
for dock in target_area.docks.values():
|
||||
elif len(self.dock_area.docks) > 1:
|
||||
for dock in self.dock_area.docks.values():
|
||||
dock.show_title_bar()
|
||||
|
||||
if widget is not None and isinstance(widget, str):
|
||||
@@ -345,10 +340,19 @@ class BECDockArea(BECWidget, QWidget):
|
||||
): # TODO still decide how initial instructions should be handled
|
||||
self._instructions_visible = False
|
||||
self.update()
|
||||
if floating:
|
||||
if floating or temporary:
|
||||
dock.detach()
|
||||
print("dock added")
|
||||
return dock
|
||||
|
||||
# def add_temp_dock(self):
|
||||
# area = BECDockArea()
|
||||
# area.show()
|
||||
# area.add_dock("dock1", widget="BECFigure")
|
||||
|
||||
def addDock(self, *args, **kwargs):
|
||||
return self.add_dock(*args, **kwargs)
|
||||
|
||||
def detach_dock(self, dock_name: str) -> BECDock:
|
||||
"""
|
||||
Undock a dock from the dock area.
|
||||
@@ -409,6 +413,11 @@ class BECDockArea(BECWidget, QWidget):
|
||||
self.cleanup()
|
||||
super().close()
|
||||
|
||||
def closeEvent(self, event):
|
||||
print("close event called")
|
||||
self.cleanup()
|
||||
super().closeEvent(event)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from qtpy.QtWidgets import QApplication
|
||||
@@ -419,4 +428,5 @@ if __name__ == "__main__":
|
||||
set_theme("auto")
|
||||
dock_area = BECDockArea()
|
||||
dock_area.show()
|
||||
dock_area.add_dock("dock1", widget="BECFigure", temporary=True)
|
||||
app.exec_()
|
||||
|
||||
@@ -159,94 +159,3 @@ def test_toolbar_add_utils_progress_bar(bec_dock_area):
|
||||
assert (
|
||||
bec_dock_area.panels["progress_bar_1"].widgets[0].config.widget_class == "RingProgressBar"
|
||||
)
|
||||
|
||||
|
||||
###################################
|
||||
# Tests for Temporary Docks
|
||||
###################################
|
||||
def test_add_temporary_dock(bec_dock_area, qtbot):
|
||||
# Add a temporary dock
|
||||
temp_dock = bec_dock_area.add_dock(name="temp_dock_1", temporary=True)
|
||||
|
||||
# Check that tempAreas has one area
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 1
|
||||
|
||||
# Check that the dock is in the tempArea's docks
|
||||
temp_area = bec_dock_area.dock_area.tempAreas[0]
|
||||
assert temp_dock.name() in temp_area.docks
|
||||
|
||||
# Close the temporary dock
|
||||
temp_dock.close()
|
||||
qtbot.wait(100)
|
||||
|
||||
# Check that tempAreas is now empty
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 0
|
||||
|
||||
# Check that the dock is removed from docks
|
||||
assert temp_dock.name() not in bec_dock_area.dock_area.docks
|
||||
|
||||
bec_dock_area.close()
|
||||
|
||||
|
||||
def test_clear_all_with_temporary_docks(bec_dock_area, qtbot):
|
||||
# Add normal docks
|
||||
d0 = bec_dock_area.add_dock(name="dock_0")
|
||||
d1 = bec_dock_area.add_dock(name="dock_1")
|
||||
# Add temporary docks
|
||||
temp_dock_1 = bec_dock_area.add_dock(name="temp_dock_1", temporary=True)
|
||||
temp_dock_2 = bec_dock_area.add_dock(name="temp_dock_2", temporary=True)
|
||||
|
||||
# Check that tempAreas have 2 areas
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 2
|
||||
|
||||
# Clear all docks
|
||||
bec_dock_area.clear_all()
|
||||
qtbot.wait(100)
|
||||
|
||||
# Check that all docks are removed
|
||||
assert len(bec_dock_area.dock_area.docks) == 0
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 0
|
||||
|
||||
|
||||
def test_attach_all_removes_temporary_docks(bec_dock_area, qtbot):
|
||||
# Add normal dock
|
||||
d0 = bec_dock_area.add_dock(name="dock_0")
|
||||
# Add temporary dock
|
||||
temp_dock = bec_dock_area.add_dock(name="temp_dock", temporary=True)
|
||||
|
||||
# Detach normal dock
|
||||
d0.detach()
|
||||
|
||||
# Check tempAreas
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 2 # One for temp_dock, one for detached d0
|
||||
|
||||
# Call attach_all
|
||||
bec_dock_area.attach_all()
|
||||
qtbot.wait(100)
|
||||
|
||||
# Check that tempAreas is now empty
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 0
|
||||
|
||||
# Check that temp_dock is not in docks
|
||||
assert temp_dock.name() not in bec_dock_area.dock_area.docks
|
||||
|
||||
|
||||
def test_close_temporary_dock(bec_dock_area, qtbot):
|
||||
# Add temporary dock
|
||||
temp_dock = bec_dock_area.add_dock(name="temp_dock", temporary=True)
|
||||
|
||||
# Check tempAreas
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 1
|
||||
|
||||
# Close the temporary dock
|
||||
temp_dock.close()
|
||||
qtbot.wait(100)
|
||||
|
||||
# Check that tempAreas is now empty
|
||||
assert len(bec_dock_area.dock_area.tempAreas) == 0
|
||||
|
||||
# Check that the dock is removed from docks
|
||||
assert temp_dock.name() not in bec_dock_area.dock_area.docks
|
||||
|
||||
# Close parent area
|
||||
bec_dock_area.close()
|
||||
|
||||
Reference in New Issue
Block a user