Commit 47f58bfd authored by Valentin Valls's avatar Valentin Valls
Browse files

Improve copyItemsFromChannelNames to include missing dest items

parent 4874e4b1
......@@ -648,9 +648,16 @@ def updateDisplayedChannelNames(
def copyItemsFromChannelNames(
sourcePlot: plot_model.Plot, destinationPlot: plot_model.Plot
sourcePlot: plot_model.Plot,
destinationPlot: plot_model.Plot,
scan: scan_model.Scan = None,
):
"""Copy from the source plot the item which was setup into the destination plot"""
"""Copy from the source plot the item which was setup into the destination plot.
If the destination plot do not contain the expected items, the scan is used
to know if they are available, and are then created. Else source item is
skipped.
"""
if not isinstance(sourcePlot, plot_item_model.CurvePlot):
raise TypeError("Only available for curve plot. Found %s" % type(sourcePlot))
if not isinstance(destinationPlot, type(sourcePlot)):
......@@ -674,10 +681,26 @@ def copyItemsFromChannelNames(
if channel is None:
continue
name = channel.name()
sourceItem = availableItems.get(name)
sourceItem = availableItems.pop(name, None)
if sourceItem is not None:
copyItemConfig(sourceItem, item)
if len(availableItems) > 0 and scan is not None:
# Some items could be created
for name, sourceItem in availableItems.items():
channel = scan.getChannelByName(name)
if channel is None:
# Not part of the scan
continue
item, _updated = createCurveItem(
destinationPlot,
channel,
yAxis=sourceItem.yAxis(),
allowIndexed=True,
)
copyItemConfig(sourceItem, item)
def copyItemConfig(sourceItem: plot_model.Item, destinationItem: plot_model.Item):
"""Copy the configuration and the item tree from a source item to a
......
......@@ -706,7 +706,21 @@ def test_remove_channels__no_value():
assert item.yChannel() is None
def test_copy_config_tree():
def test_copy_config_tree__updated_root_item():
"""The destination plot already contains the plotted channel"""
scan = scan_model.Scan()
master1 = scan_model.Device(scan)
channel2 = scan_model.Channel(master1)
channel2.setName("y1")
master2 = scan_model.Device(scan)
channel3 = scan_model.Channel(master2)
channel3.setName("x")
channel4 = scan_model.Channel(master2)
channel4.setName("y2")
channel5 = scan_model.Channel(master2)
channel5.setName("y3")
scan.seal()
source = plot_item_model.CurvePlot()
destination = plot_item_model.CurvePlot()
......@@ -722,15 +736,59 @@ def test_copy_config_tree():
source.addItem(item3)
add_item(source, "x", "y2")
destItem = add_item(destination, "x", "y1")
add_item(destination, "x", "y3")
model_helper.copyItemsFromChannelNames(source, destination)
model_helper.copyItemsFromChannelNames(source, destination, scan)
assert destItem.yAxis() == "right"
items = list(destination.items())
assert len(items) == 5
gaussianItem = [i for i in items if type(i) == plot_state_model.GaussianFitItem]
assert len(gaussianItem) == 1
assert gaussianItem[0].source().source().yChannel().name() == "y1"
def test_copy_config_tree__created_root_item():
"""The destination plot is empty, but it is feed"""
scan = scan_model.Scan()
master1 = scan_model.Device(scan)
channel2 = scan_model.Channel(master1)
channel2.setName("y1")
master2 = scan_model.Device(scan)
channel3 = scan_model.Channel(master2)
channel3.setName("x")
channel4 = scan_model.Channel(master2)
channel4.setName("y2")
channel5 = scan_model.Channel(master2)
channel5.setName("y3")
scan.seal()
source = plot_item_model.CurvePlot()
destination = plot_item_model.CurvePlot()
item = add_item(source, "x", "y1")
item.setYAxis("right")
item2 = plot_state_model.DerivativeItem(source)
item2.setSource(item)
source.addItem(item2)
item3 = plot_state_model.GaussianFitItem(source)
item3.setSource(item2)
source.addItem(item3)
item = add_item(source, "x", "y3")
item = add_item(source, "x", "y4")
model_helper.copyItemsFromChannelNames(source, destination, scan)
destItem = destination.items()[0]
assert destItem.yAxis() == "right"
items = list(destination.items())
assert len(items) == 4
assert type(items[-1]) == plot_state_model.GaussianFitItem
assert items[-1].source() is items[-2]
gaussianItem = [i for i in items if type(i) == plot_state_model.GaussianFitItem]
assert len(gaussianItem) == 1
assert gaussianItem[0].source().source().yChannel().name() == "y1"
def test_update_xaxis():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment