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

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

Update for unit tests and minor functionality quirks

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