From f20a73ec06e121733df64e05204a85b2c5b667cd Mon Sep 17 00:00:00 2001
From: payno <henri.payno@esrf.fr>
Date: Thu, 2 May 2019 11:18:53 +0200
Subject: [PATCH] [axis] link the lock button

---
 .../tomwer/widgets/reconstruction/AxisOW.py   | 23 +++++++----
 .../widgets/test/test_reconstruction.py       |  1 +
 tomwer/core/process/reconstruction/axis.py    | 41 +++++++++++--------
 tomwer/gui/ftserie/axis/axis.py               | 19 +++++++--
 4 files changed, 57 insertions(+), 27 deletions(-)

diff --git a/orangecontrib/tomwer/widgets/reconstruction/AxisOW.py b/orangecontrib/tomwer/widgets/reconstruction/AxisOW.py
index daa438656..420f31f41 100644
--- a/orangecontrib/tomwer/widgets/reconstruction/AxisOW.py
+++ b/orangecontrib/tomwer/widgets/reconstruction/AxisOW.py
@@ -100,18 +100,27 @@ class AxisOW(widget.OWWidget, AxisProcess):
         # connect Signal / Slot
         self._widget.sigComputationRequested.connect(self.__compute)
         self._widget.sigApply.connect(self.__validate)
+        self._widget.sigAxisEditionLocked.connect(self.__lockReconsParams)
+
+        # expose API
+        self.setLocked = self._widget.setLocked
 
     def __compute(self):
-        self.__lastAxisProcessParamsCache = self.recons_params.to_dict()
-        AxisProcess.process(self, self.__scan)
+        if self.__scan:
+            self.__lastAxisProcessParamsCache = self.recons_params.to_dict()
+            AxisProcess.process(self, self.__scan)
 
     def __validate(self):
-        # if parameters has changed since last processing, reprocess axis method
-        if self.recons_params.to_dict() != self.__lastAxisProcessParamsCache:
-            self.__compute()
+        if self.__scan:
+            # if parameters has changed since last processing, reprocess axis method
+            if self.recons_params.to_dict() != self.__lastAxisProcessParamsCache:
+                self.__compute()
+
+            self.signalReady(self.__scan)
+            self.accept()
 
-        self.signalReady(self.__scan)
-        self.accept()
+    def __lockReconsParams(self, lock):
+        AxisProcess.set_editable(self, not lock)
 
     def signalReady(self, scanID):
         assert isinstance(scanID, TomoBase)
diff --git a/orangecontrib/tomwer/widgets/test/test_reconstruction.py b/orangecontrib/tomwer/widgets/test/test_reconstruction.py
index 332139c7e..5bb5ec60b 100644
--- a/orangecontrib/tomwer/widgets/test/test_reconstruction.py
+++ b/orangecontrib/tomwer/widgets/test/test_reconstruction.py
@@ -94,6 +94,7 @@ class TestReconstructionWidgets(OrangeWorflowTest):
         cls.ftSeriesWidget._ftserie.reconsStack.setMockMode(True)
         cls.ftSeriesWidget._ftserie.setForceSync(True)
         cls.darkRefWidget.setForceSync(True)
+        cls.ftAxisWidget.setLocked(True)
 
     @classmethod
     def tearDownClass(cls):
diff --git a/tomwer/core/process/reconstruction/axis.py b/tomwer/core/process/reconstruction/axis.py
index 5623f487d..448042091 100644
--- a/tomwer/core/process/reconstruction/axis.py
+++ b/tomwer/core/process/reconstruction/axis.py
@@ -52,11 +52,17 @@ class AxisProcess(SingleProcess):
         SingleProcess.__init__(self)
         assert recons_params is None or isinstance(recons_params, AxisRP)
         self._recons_params = recons_params or AxisRP()
+        """Axis reconstruction parameters to apply"""
+        self._locked = False
+        """Boolean used to lock reconstruction parameters edition"""
 
     def set_recons_params(self, recons_params):
         assert isinstance(recons_params, AxisRP)
         self._recons_params = recons_params
 
+    def set_editable(self, editable=True):
+        self._locked = not editable
+
     def process(self, scan=None):
         if isinstance(scan, dict):
             _logger.warning('convert scan from a dict')
@@ -65,29 +71,32 @@ class AxisProcess(SingleProcess):
             _scan = scan
         assert isinstance(_scan, TomoBase)
 
-        # if no tomo reconstruction parameters exists, create them
-        # TODO: should be overwrite by GUI to create QReconsParams instead of
-        # simple ReconsParams
+        # if the scan has no tomo reconstruction parameters yet create them
         if _scan.tomo_recons_params is None:
             _scan.tomo_recons_params = self._instanciateReconsParams()
 
-        # copy axis value
+        # copy axis recons parameters
         _scan.tomo_recons_params.copy(self._recons_params)
         assert _scan._tomo_recons_params.axis is not None
 
-        # compute the axis position
-        try:
-            _scan.tomo_recons_params.position = self.compute_axis_position(_scan)
-        except NotImplementedError:
-            _logger.error('Not implemented')
-            _scan.tomo_recons_params.position = None
-        except ValueError as e:
-            _logger.error('Fail to compute axis position for', scan.path,
-                          'reason is', e)
-            _scan.tomo_recons_params.position = None
+        if self._locked is True:
+            # if the reconstruction parameters are lock, simple copy the already
+            # computed axis position.
+            # this has already be done when copying the Axis recons params
+            pass
         else:
-            _logger.info('Compute axis position (',
-                         _scan.tomo_recons_params.position, ') for', _scan.path)
+            try:
+                _scan.tomo_recons_params.position = self.compute_axis_position(_scan)
+            except NotImplementedError:
+                _logger.error('Not implemented')
+                _scan.tomo_recons_params.position = None
+            except ValueError as e:
+                _logger.error('Fail to compute axis position for', scan.path,
+                              'reason is', e)
+                _scan.tomo_recons_params.position = None
+            else:
+                _logger.info('Compute axis position (',
+                             _scan.tomo_recons_params.position, ') for', _scan.path)
 
         if self._return_dict:
             return _scan.to_dict()
diff --git a/tomwer/gui/ftserie/axis/axis.py b/tomwer/gui/ftserie/axis/axis.py
index 8133df165..035d09ef5 100644
--- a/tomwer/gui/ftserie/axis/axis.py
+++ b/tomwer/gui/ftserie/axis/axis.py
@@ -76,6 +76,10 @@ class AxisWindow(qt.QMainWindow):
     sigApply = qt.Signal()
     """signal emitted when the axis reconstruction parameters are validated"""
 
+    sigAxisEditionLocked = qt.Signal(bool)
+    """Signal emitted when the status of the reconstruction parameters edition
+    change"""
+
     def __init__(self, axis, parent=None):
         qt.QMainWindow.__init__(self, parent)
         if isinstance(axis, _QAxisRP):
@@ -135,7 +139,7 @@ class AxisWindow(qt.QMainWindow):
         self._controlWidget.sigShiftChanged.connect(self._updateShift)
         self._controlWidget.sigRoiChanged.connect(self._updateShift)
         self._controlWidget.sigAuto.connect(self._updateAuto)
-        self._lockBut.toggled.connect(self._lockAxisPosition)
+        self._lockBut.toggled.connect(self.setLocked)
         self._computeBut.pressed.connect(self._computationRequested)
         self._applyBut.pressed.connect(self._validated)
 
@@ -158,13 +162,17 @@ class AxisWindow(qt.QMainWindow):
     def _computationRequested(self):
         self.sigComputationRequested.emit()
 
+    def setLocked(self, locked):
+        old = self.blockSignals(True)
+        self._controlWidget.setLocked(locked)
+        self._lockBut.setChecked(locked)
+        self.blockSignals(old)
+        self.sigAxisEditionLocked.emit(locked)
+
     def _validated(self):
         """callback when the validate button is activated"""
         self.sigApply.emit()
 
-    def _lockAxisPosition(self, lock):
-        raise NotImplementedError()
-
     def setReconsParams(self, axis):
         """
         
@@ -439,6 +447,9 @@ class _AxisGUIControl(qt.QWidget):
         self._yShift = 0
         self.sigShiftChanged.emit(self._xShift, self._yShift)
 
+    def setLocked(self, locked):
+        self._mainWidget.setEnabled(not locked)
+
 
 class _AxisManualSelection(qt.QWidget):
     def __init__(self, parent, shift_mode):
-- 
GitLab