xas_output.py 4.37 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
30
31
32
33
34
# 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/11/2019"


from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from silx.gui import qt
from Orange.widgets.settings import Setting
35
from xas.core.types import XASObject
36
import xas.core.io
37
38
39
40
import logging
_logger = logging.getLogger(__file__)


41
class XASOutputOW(OWWidget):
42
43
44
45
    """
    Widget used for signal extraction
    """
    name = "output"
46
    id = "orange.widgets.xas.utils.xas_output"
47
48
49
50
51
52
53
54
55
    description = "Store process result (configuration)"
    icon = "icons/output.png"
    priority = 5
    category = "esrfWidgets"
    keywords = ["spectroscopy", "signal", "output", "file"]

    want_main_area = True
    resizing_enabled = True

56
    inputs = [("spectra", XASObject, 'process')]
57
58

    _output_file_setting = Setting(str())
59
    process_function = xas.core.io.XASWriter
60
61
62
63
64
65
66
67
68
69
70
71

    def __init__(self):
        super().__init__()
        self._outputWindow = qt.QWidget(parent=self)
        self._outputWindow.setLayout(qt.QGridLayout())

        self._outputWindow.layout().addWidget(qt.QLabel('file', parent=self))
        self._inputLe = qt.QLineEdit('', parent=self)
        self._outputWindow.layout().addWidget(self._inputLe, 0, 0)
        self._selectPB = qt.QPushButton('select', parent=self)
        self._outputWindow.layout().addWidget(self._selectPB, 0, 1)

72
73
74
        spacer = qt.QWidget(parent=self)
        spacer.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Expanding)
        self._outputWindow.layout().addWidget(spacer, 2, 0)
75
76
77
78
79

        layout = gui.vBox(self.mainArea, 'output').layout()
        layout.addWidget(self._outputWindow)

        # deal with settings
80
        self.setFileSelected(self._output_file_setting)
81
82
83
84
85
86
87
88
89
90
91

        # signal / slot connection
        self._selectPB.pressed.connect(self._selectFile)

    def _selectFile(self, *args, **kwargs):
        old = self.blockSignals(True)
        dialog = qt.QFileDialog(self)
        dialog.setFileMode(qt.QFileDialog.AnyFile)

        if not dialog.exec_():
            dialog.close()
92
93
            return False
        return True
94
95
96
97
98
99

        fileSelected = dialog.selectedFiles()
        if len(fileSelected) == 0:
            return
        else:
            assert len(fileSelected) == 1
100
            self.setFileSelected(fileSelected[0])
101
102
103

        self.blockSignals(old)

104
    def setFileSelected(self, file_path):
105
106
107
        self._output_file_setting = file_path
        self._inputLe.setText(file_path)

108
    def _getFileSelected(self):
109
110
        return self._inputLe.text()

111
    def process(self, xas_obj):
112
        if isinstance(xas_obj, dict):
113
            _xas_obj = XASObject.from_dict(xas_obj)
114
115
116
117
118
        else:
            _xas_obj = xas_obj

        has_file = self._getFileSelected() != ''
        if not has_file:
119
120
121
122
            mess = qt.QMessageBox(parent=self)
            mess.setIcon(qt.QMessageBox.Warning)
            mess.setText('No output file defined, please give a file path')
            mess.exec_()
123
124
            has_file = self._selectFile()
        if not has_file:
125
126
            _logger.error('no valid output file given, skip save')
        else:
127
            _xas_obj.dump(self._getFileSelected())