shiftcorrection.py 4.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "02/10/2018"

30
import logging
31
from silx.gui import qt
32

33
from Orange.canvas.registry.description import InputSignal, OutputSignal
34 35 36
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget

37 38
from id06workflow.core.experiment import Experiment
from id06workflow.core.types import _Image
39 40 41
from id06workflow.gui.shifcorrection import ShiftCorrectionWidget

_logger = logging.getLogger(__file__)
42 43 44 45


class ShiftCorrectionOW(OWWidget):
    """
46 47
    Widget used to apply a shift correction. User can define two points which 
     is the same position on browsing dataset.
48
    """
49

50
    name = "Shift correction"
51 52
    id = "orange.widgets.id06workflow.shiftcorrection"
    description = "Apply shift correction on the dataset"
53
    icon = "icons/shift_correction.svg"
54 55 56 57 58

    priority = 10
    category = "esrfWidgets"
    keywords = ["dataset", "shift", "correction"]

59
    inputs = [InputSignal(name="data", type=Experiment, handler='_process')]
60 61
    outputs = [OutputSignal(name="data", type=Experiment),
               OutputSignal(name="image", type=_Image)]
62 63 64 65

    want_main_area = True
    resizing_enabled = True
    compress_signal = False
66 67 68

    def __init__(self):
        super().__init__()
69
        self._editedExperiment = None
70
        self._widget = ShiftCorrectionWidget(parent=self)
71 72
        self._progress = gui.ProgressBar(self, 100)
        self._widget.sigProgress.connect(self._setProgressValue)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        layout = gui.vBox(self.mainArea, 'data selection').layout()
        layout.addWidget(self._widget)
        layout.addWidget(self._widget)

        # buttons
        types = qt.QDialogButtonBox.Ok
        self._buttons = qt.QDialogButtonBox(parent=self)
        self._buttons.setStandardButtons(types)
        self._updateButton = qt.QPushButton("Update")
        self._buttons.addButton(self._updateButton, qt.QDialogButtonBox.ActionRole)
        self._updateButton.pressed.connect(self._widget._updateShift)

        layout.addWidget(self._buttons)

        self._buttons.hide()
        self._buttons.accepted.connect(self.validate)

        # expose API
        self.setShift = self._widget.setShift

93 94 95
    def _setProgressValue(self, value):
        self._progress.widget.progressBarSet(value)

96 97 98 99 100
    def _process(self, experiment):
        assert isinstance(experiment, (Experiment, type(None)))
        if experiment.dataset is None or experiment.dataset.is_valid() is False:
            _logger.warning('cannot process shift correction, need a valid dataset for this')
        else:
101
            _logger.info('Receive some data for shift application')
102
            self._editedExperiment = experiment
103
            self._widget.setExperiment(experiment)
104 105 106 107
            self._buttons.show()
            self.show()

    def validate(self):
108 109 110 111 112
        if self._widget.isProcessing() is True:
            _logger.error('you should wait the end of the processing before'
                          'validation')
            return
        self._progress.finish()
113 114 115 116 117 118 119 120 121 122 123
        if self._editedExperiment is not None:
            self._widget._updateShift()
            if self._widget.getShift() != (0, 0, 0):
                self._widget.getOperation().apply()

            self.send("data", self._editedExperiment)
            image = _Image(self._widget._sumPlot.getActiveImage(just_legend=False),
                           'shift_correction')
            self.send("image",
                      image)
            # TODO: clear widget to avoid reprocessing ?
124
            OWWidget.accept(self)