xas_output.py 4.48 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
# 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.settings import Setting
33
from silx.gui import qt
34
from ewoksorange.bindings.owwidgets import OWEwoksWidgetNoThread
35
import est.core.process.io
36
import logging
37
import h5py
payno's avatar
payno committed
38

39
40
41
_logger = logging.getLogger(__file__)


42
class XASOutputOW(
43
44
    OWEwoksWidgetNoThread,
    ewokstaskclass=est.core.process.io.DumpXasObject,
45
):
46
47
48
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
49

50
    name = "xas output"
51
    id = "orange.widgets.xas.utils.xas_output"
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    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

    _output_file_setting = Setting(str())

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

payno's avatar
payno committed
68
69
        self._outputWindow.layout().addWidget(qt.QLabel("file", parent=self))
        self._inputLe = qt.QLineEdit("", parent=self)
70
        self._outputWindow.layout().addWidget(self._inputLe, 0, 0)
payno's avatar
payno committed
71
        self._selectPB = qt.QPushButton("select", parent=self)
72
73
        self._outputWindow.layout().addWidget(self._selectPB, 0, 1)

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

payno's avatar
payno committed
78
        layout = gui.vBox(self.mainArea, "output").layout()
79
80
81
        layout.addWidget(self._outputWindow)

        # deal with settings
82
        self.setFileSelected(self._output_file_setting)
83
84
85
86
87
88

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

    def _selectFile(self, *args, **kwargs):
        dialog = qt.QFileDialog(self)
89
        dialog.setAcceptMode(qt.QFileDialog.AcceptSave)
90
        dialog.setFileMode(qt.QFileDialog.AnyFile)
payno's avatar
payno committed
91
        dialog.setNameFilters(["hdf5 files (*.hdf5, *.hdf, *.h5)"])
92

93
        if dialog.exec_() is qt.QDialog.Rejected:
94
            dialog.close()
95
            return None
96
97
        fileSelected = dialog.selectedFiles()
        if len(fileSelected) == 0:
98
            return None
99
100
        else:
            assert len(fileSelected) == 1
101
102
            file_ = fileSelected[0]
            if not h5py.is_hdf5(file_):
103
104
105
                if not file_.lower().endswith((".h5", ".hdf5", ".nx", ".nexus")):
                    file_ += ".h5"
            old = self.blockSignals(True)
106
            self.setFileSelected(file_)
107
108
            self.blockSignals(old)
            return str(file_)
109

110
    def setFileSelected(self, file_path):
111
112
113
        self._output_file_setting = file_path
        self._inputLe.setText(file_path)

114
    def _getFileSelected(self):
115
116
        return self._inputLe.text()

117
118
119
120
121
122
123
124
125
126
127
    def _missing_file_getter(self):
        mess = qt.QMessageBox(self)
        mess.setIcon(qt.QMessageBox.Warning)
        mess.setText("No output file defined, please give a file path")
        mess.exec_()
        return self._selectFile()

    def _getTaskArguments(self):
        adict = super()._getTaskArguments()
        adict["missing_file_callback"] = self._missing_file_getter
        return adict