From e2ebf8ebf02464e91d3a21e40b4a1effe87037f8 Mon Sep 17 00:00:00 2001 From: IgorTatarnikov Date: Fri, 10 Nov 2023 17:01:41 +0000 Subject: [PATCH] wip: chest and drawers classes draft --- brainglobe_utils/qtpy/chest.py | 27 ++++++++++++ brainglobe_utils/qtpy/drawer.py | 41 +++++++++++++++++++ .../tests/test_unit/test_qtpy/test_drawer.py | 22 ++++++++++ 3 files changed, 90 insertions(+) create mode 100644 brainglobe_utils/qtpy/chest.py create mode 100644 brainglobe_utils/qtpy/drawer.py create mode 100644 tests/tests/test_unit/test_qtpy/test_drawer.py diff --git a/brainglobe_utils/qtpy/chest.py b/brainglobe_utils/qtpy/chest.py new file mode 100644 index 0000000..0de99cf --- /dev/null +++ b/brainglobe_utils/qtpy/chest.py @@ -0,0 +1,27 @@ +from qtpy.QtCore import Qt +from qtpy.QtWidgets import QGroupBox, QVBoxLayout + + +class Chest(QGroupBox): + def __init__(self): + super().__init__() + self.setLayout(QVBoxLayout()) + + self.layout().setAlignment(Qt.AlignTop) + self.layout().setSpacing(0) + self.layout().setContentsMargins(0, 0, 0, 0) + self.drawers = [] + + def add_drawer(self, drawer): + self.drawers.append(drawer) + self.layout().addWidget(drawer, 0, Qt.AlignTop) + drawer.toggled_signal.connect(self._update_drawers) + + def _update_drawers(self, signalling_drawer, state): + if state: + for drawer in self.drawers: + if ( + drawer is not signalling_drawer + and drawer.currently_expanded + ): + drawer.toggle_expansion() diff --git a/brainglobe_utils/qtpy/drawer.py b/brainglobe_utils/qtpy/drawer.py new file mode 100644 index 0000000..34af89d --- /dev/null +++ b/brainglobe_utils/qtpy/drawer.py @@ -0,0 +1,41 @@ +from qtpy.QtCore import Qt, Signal +from qtpy.QtWidgets import QPushButton, QVBoxLayout, QWidget + + +class Drawer(QWidget): + """ + LayerList class which acts as collapsable list. + """ + + toggled_signal = Signal(QWidget, bool) + + def __init__(self, name, expand=False): + super().__init__() + self.currently_expanded = expand + self.main_layout = QVBoxLayout() + + self.expand_button = QPushButton(name) + self.expand_button.setToolTip(f"{name}") + + # self.expand_button.setIcon \ + # (QIcon(os.path.join(PATH, 'LayersList_Up.png'))) + + self.main_layout.addWidget(self.expand_button, 0, Qt.AlignTop) + + self.expand_button.clicked.connect(self._on_expand_toggle) + self.setLayout(self.main_layout) + + def _on_expand_toggle(self): + self.toggle_expansion() + self.toggled_signal.emit(self, self.currently_expanded) + + def toggle_expansion(self): + self.currently_expanded = not self.currently_expanded + for i in range(1, self.layout().count()): + self.layout().itemAt(i).widget().setVisible( + self.currently_expanded + ) + + def add(self, widget: QWidget): + widget.setVisible(False) + self.layout().addWidget(widget) diff --git a/tests/tests/test_unit/test_qtpy/test_drawer.py b/tests/tests/test_unit/test_qtpy/test_drawer.py new file mode 100644 index 0000000..ee0bb0b --- /dev/null +++ b/tests/tests/test_unit/test_qtpy/test_drawer.py @@ -0,0 +1,22 @@ +from qtpy.QtWidgets import QLabel + +from brainglobe_utils.qtpy.chest import Chest +from brainglobe_utils.qtpy.drawer import Drawer + + +def test_layers_list(qtbot): + chest = Chest() + drawer = Drawer("test") + drawer_2 = Drawer("test2") + drawer.add(QLabel("test")) + drawer.add(QLabel("test2")) + drawer_2.add(QLabel("test")) + drawer_2.add(QLabel("test2")) + + chest.add_drawer(drawer) + chest.add_drawer(drawer_2) + qtbot.addWidget(chest) + + chest.window().show() + + qtbot.wait(100000)