Commit 6ec913b6 authored by Valentin Valls's avatar Valentin Valls

Rework item inheritance

parent 26458223
......@@ -124,8 +124,8 @@ class DefaultStyleStrategy(plot_model.StyleStrategy):
for item in plot.items():
if isinstance(item, plot_item_model.ScanItem):
continue
if isinstance(item, plot_model.AbstractComputableItem):
if isinstance(item, plot_state_model.CurveStatisticMixIn):
if isinstance(item, plot_model.ComputableMixIn):
if isinstance(item, plot_state_model.CurveStatisticItem):
source = item.source()
baseStyle = self.getStyleFromItem(source, scan)
style = plot_model.Style(
......
......@@ -17,6 +17,8 @@ Here is a list of plot and item inheritance.
"""
from __future__ import annotations
from typing import Optional
from typing import Dict
from typing import Any
import numpy
......@@ -68,10 +70,20 @@ class CurveMixIn:
def __init__(self):
self.__yAxis = "left"
def __getstate__(self):
state = {}
state["y_axis"] = self.yAxis()
return state
def __setstate__(self, state):
self.setYAxis(state.pop("y_axis"))
def yAxis(self) -> str:
return self.__yAxis
def setYAxis(self, yAxis: str):
if self.__yAxis == yAxis:
return
self.__yAxis = yAxis
self._emitValueChanged(plot_model.ChangeEventType.YAXIS)
......@@ -105,29 +117,26 @@ class CurveItem(plot_model.Item, CurveMixIn):
"""
def __init__(self, parent: plot_model.Plot = None):
super(CurveItem, self).__init__(parent=parent)
plot_model.Item.__init__(self, parent=parent)
CurveMixIn.__init__(self)
self.__x: Optional[plot_model.ChannelRef] = None
self.__y: Optional[plot_model.ChannelRef] = None
self.__yAxis: str = "left"
def __reduce__(self):
return (self.__class__, (), self.__getstate__())
def __getstate__(self):
state = super(CurveItem, self).__getstate__()
state: Dict[str, Any] = {}
state.update(plot_model.Item.__getstate__(self))
state.update(CurveMixIn.__getstate__(self))
assert "x" not in state
assert "y" not in state
assert "y-axis" not in state
state["x"] = self.__x
state["y"] = self.__y
state["y_axis"] = self.__yAxis
return state
def __setstate__(self, state):
super(CurveItem, self).__setstate__(state)
plot_model.Item.__setstate__(self, state)
CurveMixIn.__setstate__(self, state)
self.__x = state.pop("x")
self.__y = state.pop("y")
self.__yAxis = state.pop("y_axis")
def isValid(self):
return self.__x is not None and self.__y is not None
......@@ -167,15 +176,6 @@ class CurveItem(plot_model.Item, CurveMixIn):
self.__y = channel
self._emitValueChanged(plot_model.ChangeEventType.Y_CHANNEL)
def yAxis(self) -> str:
return self.__yAxis
def setYAxis(self, yAxis: str):
if self.__yAxis == yAxis:
return
self.__yAxis = yAxis
self._emitValueChanged(plot_model.ChangeEventType.YAXIS)
def xData(self, scan: scan_model.Scan) -> Optional[scan_model.Data]:
channel = self.xChannel()
if channel is None:
......
......@@ -28,6 +28,7 @@ of the implementation.
from __future__ import annotations
from typing import List
from typing import Any
from typing import Dict
from typing import Optional
import numpy
......@@ -91,7 +92,7 @@ class Plot(qt.QObject):
def __init__(self, parent=None):
super(Plot, self).__init__(parent=parent)
self.__items: List[Item] = []
self.__styleStrategy: StyleStrategy = None
self.__styleStrategy: Optional[StyleStrategy] = None
self.__inTransaction: int = 0
def __reduce__(self):
......@@ -383,8 +384,8 @@ _NotComputed = object()
class ComputeError(Exception):
"""Raised when the `compute` method of AbstractComputableItem can't compute
any output"""
"""Raised when the `compute` method of ComputableMixIn or
IncrementalComputableMixIn can't compute any output"""
def __init__(self, msg: str, result=None):
super(ComputeError, self).__init__(self, msg)
......@@ -392,26 +393,22 @@ class ComputeError(Exception):
self.result = result
class AbstractComputableItem(Item):
"""This item use the scan data to process result before displaying it."""
resultAvailable = qt.Signal(object)
class ChildItem(Item):
"""An item with a source"""
def __init__(self, parent=None):
Item.__init__(self, parent=parent)
super(ChildItem, self).__init__(parent=parent)
self.__source: Optional[Item] = None
def __reduce__(self):
return (self.__class__, (), self.__getstate__())
def __getstate__(self):
state = super(AbstractComputableItem, self).__getstate__()
state: Dict[str, Any] = {}
state.update(Item.__getstate__(self))
assert "source" not in state
state["source"] = self.__source
return state
def __setstate__(self, state):
super(AbstractComputableItem, self).__setstate__(state)
Item.__setstate__(self, state)
self.__source = state.pop("source")
def isChildOf(self, parent: Item) -> bool:
......@@ -429,6 +426,12 @@ class AbstractComputableItem(Item):
def source(self) -> Optional[Item]:
return self.__source
class ComputableMixIn:
"""This item use the scan data to process result before displaying it."""
resultAvailable = qt.Signal(object)
def isResultComputed(self, scan: scan_model.Scan) -> bool:
return scan.hasCachedResult(self)
......@@ -461,7 +464,7 @@ class AbstractComputableItem(Item):
raise NotImplementedError()
class AbstractIncrementalComputableItem(AbstractComputableItem):
class IncrementalComputableMixIn(ComputableMixIn):
def incrementalCompute(self, previousResult: Any, scan: scan_model.Scan) -> Any:
"""Compute a data using the previous value as basis"""
raise NotImplementedError()
......
......@@ -18,6 +18,8 @@ Here is a list of plot and item inheritance.
from __future__ import annotations
from typing import Optional
from typing import NamedTuple
from typing import Dict
from typing import Any
import numpy
import logging
......@@ -30,8 +32,8 @@ from ..utils import mathutils
_logger = logging.getLogger(__name__)
class CurveStatisticMixIn:
"""This item use the scan data to process result before displaying it."""
class CurveStatisticItem(plot_model.ChildItem):
"""Statistic displayed on a source item, depending on it y-axis."""
def yAxis(self) -> str:
"""Returns the name of the y-axis in which the statistic have to be displayed"""
......@@ -42,7 +44,7 @@ class CurveStatisticMixIn:
previousSource = self.source()
if previousSource is not None:
previousSource.valueChanged.disconnect(self.__sourceChanged)
plot_model.AbstractIncrementalComputableItem.setSource(self, source)
plot_model.ChildItem.setSource(self, source)
if source is not None:
source.valueChanged.connect(self.__sourceChanged)
self.__sourceChanged(plot_model.ChangeEventType.YAXIS)
......@@ -59,25 +61,28 @@ class DerivativeData(NamedTuple):
class DerivativeItem(
plot_model.AbstractIncrementalComputableItem, plot_item_model.CurveMixIn
plot_model.ChildItem,
plot_model.IncrementalComputableMixIn,
plot_item_model.CurveMixIn,
):
"""This item use the scan data to process result before displaying it."""
EXTRA_POINTS = 5
"""Extra points needed before and after a single point to compute a result"""
def __reduce__(self):
return (self.__class__, (), self.__getstate__())
def __init__(self, parent=None):
plot_model.ChildItem.__init__(self, parent=parent)
plot_item_model.CurveMixIn.__init__(self)
def __getstate__(self):
state = super(DerivativeItem, self).__getstate__()
assert "y_axis" not in state
state["y_axis"] = self.yAxis()
state: Dict[str, Any] = {}
state.update(plot_model.ChildItem.__getstate__(self))
state.update(plot_item_model.CurveMixIn.__getstate__(self))
return state
def __setstate__(self, state):
super(DerivativeItem, self).__setstate__(state)
self.setYAxis(state.pop("y_axis"))
plot_model.ChildItem.__setstate__(self, state)
plot_item_model.CurveMixIn.__setstate__(self, state)
def isResultValid(self, result):
return result is not None
......@@ -178,21 +183,24 @@ class GaussianFitData(NamedTuple):
fit: mathutils.GaussianFitResult
class GaussianFitItem(plot_model.AbstractComputableItem, plot_item_model.CurveMixIn):
class GaussianFitItem(
plot_model.ChildItem, plot_model.ComputableMixIn, plot_item_model.CurveMixIn
):
"""This item use the scan data to process result before displaying it."""
def __reduce__(self):
return (self.__class__, (), self.__getstate__())
def __init__(self, parent=None):
plot_model.ChildItem.__init__(self, parent=parent)
plot_item_model.CurveMixIn.__init__(self)
def __getstate__(self):
state = super(GaussianFitItem, self).__getstate__()
assert "y_axis" not in state
state["y_axis"] = self.yAxis()
state: Dict[str, Any] = {}
state.update(plot_model.ChildItem.__getstate__(self))
state.update(plot_item_model.CurveMixIn.__getstate__(self))
return state
def __setstate__(self, state):
super(GaussianFitItem, self).__setstate__(state)
self.setYAxis(state.pop("y_axis"))
plot_model.ChildItem.__setstate__(self, state)
plot_item_model.CurveMixIn.__setstate__(self, state)
def isResultValid(self, result):
return result is not None
......@@ -250,7 +258,7 @@ class MaxData(NamedTuple):
nb_points: int
class MaxCurveItem(plot_model.AbstractIncrementalComputableItem, CurveStatisticMixIn):
class MaxCurveItem(CurveStatisticItem, plot_model.IncrementalComputableMixIn):
"""Statistic identifying the maximum location of a curve."""
def isResultValid(self, result):
......@@ -324,7 +332,7 @@ class MinData(NamedTuple):
nb_points: int
class MinCurveItem(plot_model.AbstractIncrementalComputableItem, CurveStatisticMixIn):
class MinCurveItem(CurveStatisticItem, plot_model.IncrementalComputableMixIn):
"""Statistic identifying the minimum location of a curve."""
def isResultValid(self, result):
......
......@@ -87,10 +87,10 @@ class ScanRowItem(StandardRowItem):
icon = icons.getQIcon("flint:icons/channel-image")
elif isinstance(plotItem, plot_item_model.ScatterItem):
icon = icons.getQIcon("flint:icons/channel-curve")
elif isinstance(plotItem, plot_state_model.CurveStatisticItem):
icon = icons.getQIcon("flint:icons/item-stats")
elif isinstance(plotItem, plot_item_model.CurveMixIn):
icon = icons.getQIcon("flint:icons/item-func")
elif isinstance(plotItem, plot_state_model.CurveStatisticMixIn):
icon = icons.getQIcon("flint:icons/item-stats")
else:
icon = icons.getQIcon("flint:icons/item-channel")
self.setIcon(icon)
......
......@@ -484,7 +484,7 @@ class CurvePlotWidget(ExtendedDockWidget):
plot._add(curveItem)
plotItems.append((legend, "curve"))
elif isinstance(item, plot_state_model.CurveStatisticMixIn):
elif isinstance(item, plot_state_model.CurveStatisticItem):
if isinstance(item, plot_state_model.MaxCurveItem):
legend = str(item) + "/" + str(scan)
result = item.reachResult(scan)
......
......@@ -552,7 +552,7 @@ class _DataItem(_property_tree_helper.ScanRowItem):
# self.__updateXAxisStyle(False, None)
useXAxis = False
self.__updateXAxisStyle(False)
elif isinstance(plotItem, plot_state_model.CurveStatisticMixIn):
elif isinstance(plotItem, plot_state_model.CurveStatisticItem):
useXAxis = False
self.__updateXAxisStyle(False)
......@@ -832,7 +832,7 @@ class CurvePlotPropertyWidget(qt.QWidget):
if isinstance(plotItem, plot_item_model.MotorPositionMarker):
continue
if isinstance(plotItem, plot_model.AbstractComputableItem):
if isinstance(plotItem, plot_model.ComputableMixIn):
source = plotItem.source()
if source is None:
parent = itemWithoutLocation
......
......@@ -29,7 +29,9 @@ from bliss.flint.helper import style_helper
plot_item_model.McaItem,
plot_item_model.ImageItem,
plot_state_model.DerivativeItem,
plot_state_model.CurveStatisticMixIn,
plot_state_model.GaussianFitItem,
plot_state_model.CurveStatisticItem,
plot_state_model.MinCurveItem,
plot_state_model.MaxCurveItem,
plot_item_model.MotorPositionMarker,
style_helper.DefaultStyleStrategy,
......
Markdown is supported
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