source: sasview/src/sas/qtgui/Calculators/SlitSizeCalculator.py @ fbfc488

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since fbfc488 was fbfc488, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More Qt5 related fixes.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1"""
2Slit Size Calculator Panel
3"""
4import os
5import sys
6import logging
7
8from PyQt5 import QtCore
9from PyQt5 import QtGui
10from PyQt5 import QtWidgets
11
12from sas.qtgui.UI import main_resources_rc
13import sas.qtgui.Utilities.GuiUtils as GuiUtils
14
15from .UI.SlitSizeCalculator import Ui_SlitSizeCalculator
16from sas.sascalc.dataloader.loader import Loader
17from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator
18
19
20class SlitSizeCalculator(QtWidgets.QDialog, Ui_SlitSizeCalculator):
21    """
22    Provides the slit length calculator GUI.
23    """
24    def __init__(self, parent=None):
25        super(SlitSizeCalculator, self).__init__()
26        self.setupUi(self)
27
28        self.setWindowTitle("Slit Size Calculator")
29        self._parent = parent
30
31        self.thickness = SlitlengthCalculator()
32
33        # signals
34        self.helpButton.clicked.connect(self.onHelp)
35        self.browseButton.clicked.connect(self.onBrowse)
36        self.closeButton.clicked.connect(self.onClose)
37
38        # no reason to have this widget resizable
39        self.setFixedSize(self.minimumSizeHint())
40
41
42    def onHelp(self):
43        """
44        Bring up the Slit Size Calculator calculator Documentation whenever
45        the HELP button is clicked.
46        Calls DocumentationWindow with the path of the location within the
47        documentation tree (after /doc/ ....".
48        """
49        try:
50            location = GuiUtils.HELP_DIRECTORY_LOCATION + \
51                "/user/sasgui/perspectives/calculator/slit_calculator_help.html"
52
53            self._parent._helpView.load(QtCore.QUrl(location))
54            self._parent._helpView.show()
55        except AttributeError:
56            # No manager defined - testing and standalone runs
57            pass
58
59    def onBrowse(self):
60        """
61        Browse the file and calculate slit lenght upon loading
62        """
63        path_str = self.chooseFile()
64        if not path_str:
65            return
66        loader = Loader()
67        try:
68            data = loader.load(path_str)[0]
69        # Can return multiple exceptions - gather them all under one umbrella and complain
70        except Exception as ex:
71            logging.error(ex)
72            return
73
74        self.data_file.setText(os.path.basename(path_str))
75        self.calculateSlitSize(data)
76
77    def chooseFile(self):
78        """
79        Shows the Open file dialog and returns the chosen path(s)
80        """
81
82        # Location is automatically saved - no need to keep track of the last dir
83        # But only with Qt built-in dialog (non-platform native)
84        path = QtWidgets.QFileDialog.getOpenFileName(self, "Choose a file", "",
85                                                 "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)",
86                                                 None,
87                                                 QtWidgets.QFileDialog.DontUseNativeDialog)[0]
88        return path
89
90    def onClose(self):
91        """
92        close the window containing this panel
93        """
94        self.close()
95
96    def clearResults(self):
97        """
98        Clear the content of output LineEdits
99        """
100        self.slit_length_out.setText("ERROR!")
101        self.unit_out.clear()
102
103    def calculateSlitSize(self, data=None):
104        """
105        Computes slit lenght from given 1D data
106        """
107        if data is None:
108            self.clearResults()
109            msg = "ERROR: Data hasn't been loaded correctly"
110            logging.error(msg)
111            return
112
113        if data.__class__.__name__ == 'Data2D':
114            self.clearResults()
115            msg = "Slit Length cannot be computed for 2D Data"
116            logging.error(msg)
117            return
118
119        #compute the slit size
120        try:
121            xdata = data.x
122            ydata = data.y
123            if xdata == [] or xdata is None or ydata == [] or ydata is None:
124                msg = "The current data is empty please check x and y"
125                logging.error(msg)
126                return
127            slit_length_calculator = SlitlengthCalculator()
128            slit_length_calculator.set_data(x=xdata, y=ydata)
129            slit_length = slit_length_calculator.calculate_slit_length()
130        except:
131            self.clearResults()
132            msg = "Slit Size Calculator: %s" % (sys.exc_info()[1])
133            logging.error(msg)
134            return
135
136        slit_length_str = "{:.5f}".format(slit_length)
137        self.slit_length_out.setText(slit_length_str)
138
139        #Display unit, which most likely needs to be 1/Ang but needs to be confirmed
140        self.unit_out.setText("[Unknown]")
141
Note: See TracBrowser for help on using the repository browser.