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

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 4992ff2 was 4992ff2, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Initial, in-progress version. Not really working atm. SASVIEW-787

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