Skip to content

Commit 6ed8f31

Browse files
committed
Added single operand mode test
(multiple selection operation mode)
1 parent d11b923 commit 6ed8f31

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

cdl/core/gui/objectmodel.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,16 @@ def __len__(self) -> int:
135135
"""Return number of objects in group"""
136136
return len(self.__objects)
137137

138-
def __getitem__(self, index: int) -> SignalObj | ImageObj:
138+
def __getitem__(
139+
self, index: int | slice
140+
) -> SignalObj | ImageObj | list[SignalObj | ImageObj]:
139141
"""Return object at index"""
142+
if isinstance(index, slice):
143+
return [
144+
self.model[self.__objects[i]]
145+
for i in range(*index.indices(len(self)))
146+
if i < len(self)
147+
]
140148
return self.model[self.__objects[index]]
141149

142150
def __contains__(self, obj: SignalObj | ImageObj) -> bool:

cdl/core/gui/panel/base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,14 @@ def paste_metadata(self) -> None:
521521
obj.metadata.update(self.__metadata_clipboard)
522522
self.SIG_REFRESH_PLOT.emit("selected", True)
523523

524-
def remove_object(self) -> None:
525-
"""Remove signal/image object"""
524+
def remove_object(self, force: bool = False) -> None:
525+
"""Remove signal/image object
526+
527+
Args:
528+
force: if True, remove object without confirmation. Defaults to False.
529+
"""
526530
sel_groups = self.objview.get_sel_groups()
527-
if sel_groups and not execenv.unattended:
531+
if sel_groups and not force and not execenv.unattended:
528532
answer = QW.QMessageBox.warning(
529533
self,
530534
_("Delete group(s)"),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
2+
3+
"""
4+
Operation modes test
5+
--------------------
6+
7+
"""
8+
9+
# guitest: show
10+
11+
from __future__ import annotations
12+
13+
from cdl import app
14+
from cdl.utils.qthelpers import cdl_app_context
15+
from cdl.utils.tests import get_test_fnames
16+
17+
18+
def test_single_operand_mode():
19+
"""Run single operand mode test scenario"""
20+
with cdl_app_context(exec_loop=True):
21+
win = app.create(h5files=[get_test_fnames("reorder*")[0]], console=False)
22+
panel = win.signalpanel
23+
view, model = panel.objview, panel.objmodel
24+
25+
# Store the number of groups before the operations
26+
n_groups = len(model.get_groups())
27+
28+
# Select the two first groups
29+
groups = [model.get_group_from_number(idx) for idx in (1, 2)]
30+
view.select_groups(groups)
31+
32+
# Perform a sum operation
33+
panel.processor.compute_sum()
34+
35+
# Default operation mode is single operand mode, so the sum operation
36+
# is applied to the selected groups, and we should have a new group
37+
# with two signals being the sum of signals from each group:
38+
# - signal 1: group 1 signal 1 + group 1 signal 2
39+
# - signal 2: group 2 signal 1 + group 2 signal 2
40+
assert len(model.get_groups()) == n_groups + 1
41+
new_group = model.get_group_from_number(n_groups + 1)
42+
assert len(new_group.get_objects()) == 2
43+
for idx, obj in enumerate(new_group.get_objects()):
44+
pfx_orig = ", ".join(obj.short_id for obj in groups[idx].get_objects())
45+
assert obj.title == f"Σ({pfx_orig})"
46+
47+
# Remove new group
48+
view.select_groups([new_group])
49+
panel.remove_object(force=True)
50+
51+
# Store the number of groups before the operations
52+
n_groups = len(model.get_groups())
53+
54+
# Select the two first signals of the first two groups
55+
groups = [model.get_group_from_number(idx) for idx in (1, 2)]
56+
objs = groups[0][:2] + groups[1][:2]
57+
view.select_objects(objs)
58+
59+
# Perform a sum operation
60+
panel.processor.compute_sum()
61+
62+
# Default operation mode is single operand mode, so the sum operation
63+
# is applied to the selected signals, and we should have a new resulting
64+
# signal being the sum of the selected signals added in each group:
65+
# - signal added to group 1: group 1 signal 1 + group 2 signal 1
66+
# - signal added to group 2: group 1 signal 2 + group 2 signal 2
67+
assert len(model.get_groups()) == n_groups # no new group
68+
for idx in range(1):
69+
pfx_orig = ", ".join(obj.short_id for obj in groups[idx][:2])
70+
assert groups[idx][-1].title == f"Σ({pfx_orig})"
71+
72+
# Removing resulting signals
73+
view.select_objects([groups[0][-1], groups[1][-1]])
74+
panel.remove_object()
75+
76+
77+
if __name__ == "__main__":
78+
test_single_operand_mode()

cdl/tests/features/common/reorder_app_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
def test_reorder():
2525
"""Run signals/images reorder test scenario"""
2626
with cdl_app_context(exec_loop=True):
27-
win = app.create(h5files=get_test_fnames("reorder*"))
27+
win = app.create(h5files=[get_test_fnames("reorder*")[0]])
2828
panel = win.signalpanel
2929
view, model = panel.objview, panel.objmodel
3030

0 commit comments

Comments
 (0)