k_weight.py 4.92 KB
Newer Older
payno's avatar
payno committed
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__ = "06/07/2019"

30
31
import functools
import logging
payno's avatar
payno committed
32
33

from Orange.widgets import gui
payno's avatar
payno committed
34
from Orange.widgets.settings import Setting
35
from Orange.widgets.widget import OWWidget
36
37
from Orange.widgets.widget import Input, Output
import Orange.data
payno's avatar
payno committed
38
from silx.gui import qt
payno's avatar
payno committed
39
import est.core.process.pymca.k_weight
40
from orangecontrib.est.process import _ProcessForOrangeMixIn
payno's avatar
payno committed
41
42
43
from est.core.types import XASObject
from orangecontrib.est.progress import QProgress
from orangecontrib.est.utils import Converter
payno's avatar
payno committed
44

payno's avatar
payno committed
45
46
47
_logger = logging.getLogger(__file__)


payno's avatar
payno committed
48
49
50
51
class KWeightWindow(qt.QMainWindow):
    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

52
        # k wright widget
payno's avatar
payno committed
53
        self._k_widget = qt.QWidget(parent=self)
payno's avatar
payno committed
54
        self._k_widget.setLayout(qt.QHBoxLayout())
payno's avatar
payno committed
55
        self._k_widget.layout().addWidget(qt.QLabel("k weight"))
payno's avatar
payno committed
56
57
58
59
60
61
62
63
        self._k_spin_box = qt.QSpinBox(parent=self)
        self._k_spin_box.setRange(0, 3)
        self._k_widget.layout().addWidget(self._k_spin_box)
        dockWidget = qt.QDockWidget(parent=self)
        dockWidget.setWidget(self._k_widget)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, dockWidget)
        dockWidget.setAllowedAreas(qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea)
        dockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
64

65
66
        self.setWindowFlags(qt.Qt.Widget)

payno's avatar
payno committed
67

68
69
class KWeightOW(
    _ProcessForOrangeMixIn,
70
    ewokstaskclass=est.core.process.pymca.k_weight.PyMca_k_weight,
71
):
payno's avatar
payno committed
72
73
74
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
75

payno's avatar
payno committed
76
    name = "k weight"
payno's avatar
payno committed
77
    id = "orange.widgets.est.pymca.k_weight"
78
    description = "Progress k weight"
payno's avatar
payno committed
79
    icon = "icons/k_weight.png"
80
    priority = 2
payno's avatar
payno committed
81
82
83
84
85
    category = "esrfWidgets"
    keywords = ["spectroscopy", "signal", "k", "weight"]

    want_main_area = True
    resizing_enabled = True
86
    allows_cycle = False
payno's avatar
payno committed
87

payno's avatar
payno committed
88
    _kWeightSetting = Setting(int(3))
89
90
    # kept for compatibility
    static_input = Setting({"xas_obj": None, "k_weight": 3})
payno's avatar
payno committed
91
    """Store the configuration of the PyMca XASClass"""
92

payno's avatar
payno committed
93
94
    def __init__(self):
        super().__init__()
95
        self._latest_xas_obj = None
payno's avatar
payno committed
96
        layout = gui.vBox(self.mainArea, "k weight").layout()
payno's avatar
payno committed
97
98
        self._window = KWeightWindow(parent=self)
        layout.addWidget(self._window)
payno's avatar
payno committed
99

payno's avatar
payno committed
100
        # manage settings
101
102
103
104
105
        k_weight = self.static_input.get("k_weight")
        if k_weight == 3:
            k_weight = self._kWeightSetting
        if k_weight != 3:
            self._window._k_spin_box.setValue(k_weight)
payno's avatar
payno committed
106

payno's avatar
payno committed
107
108
109
110
        # signal / slot connection
        self._window._k_spin_box.valueChanged.connect(self._updateProcess)

    def _updateProcess(self, *arv, **kwargs):
payno's avatar
payno committed
111
        self._update_settings()
112
113
        if self._latest_xas_obj:
            self.process(self._latest_xas_obj)
payno's avatar
payno committed
114

payno's avatar
payno committed
115
116
    def _update_settings(self):
        self._kWeightSetting = self._window._k_spin_box.value()
117
118
119
120
        self.static_input = {
            "xas_obj": None,
            "k_weight": self._window._k_spin_box.value(),
        }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

    def inputsChanged(self):
        pass

    def outputsChanged(self):
        if "xas_obj" in self.task_inputs:
            xas_obj = self.output_variables["xas_obj"].value
            if isinstance(xas_obj, dict):
                xas_obj = XASObject.from_dict(xas_obj)
            if hasattr(self, "_window") and hasattr(self._window, "setXASObj"):
                self._window.setXASObj(xas_obj=xas_obj)
            elif hasattr(self, "_window") and hasattr(self._window, "xasObjViewer"):
                if hasattr(self._window.xasObjViewer, "setXASObj"):
                    self._window.xasObjViewer.setXASObj(xas_obj=xas_obj)
            self.Outputs.xas_obj.send(xas_obj)