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

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

Unit tests for fitting options dialog SASVIEW-514

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[ab9984e]1"""
2Slit Size Calculator Panel
3"""
4import os
5import sys
6
[d1fb22ee]7from PyQt4 import QtGui
8from PyQt4 import QtCore
[cd2cc745]9
10from sas.qtgui.UI import main_resources_rc
[b0c5e8c]11import sas.qtgui.Utilities.GuiUtils as GuiUtils
[cd2cc745]12
[d1fb22ee]13from UI.SlitSizeCalculator import Ui_SlitSizeCalculator
[253e7170]14from sas.sascalc.dataloader.loader import Loader
[d1fb22ee]15from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator
[253e7170]16
[d1fb22ee]17
18class SlitSizeCalculator(QtGui.QDialog, Ui_SlitSizeCalculator):
[debf5c3]19    """
20    Provides the slit length calculator GUI.
21    """
[0532d7c1]22    def __init__(self, parent=None):
[d1fb22ee]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
[0532d7c1]39
[d1fb22ee]40    def onHelp(self):
41        """
[debf5c3]42        Bring up the Slit Size Calculator calculator Documentation whenever
[d1fb22ee]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:
[b0c5e8c]48            location = GuiUtils.HELP_DIRECTORY_LOCATION + \
[a8ec5b1]49                "/user/sasgui/perspectives/calculator/slit_calculator_help.html"
[d1fb22ee]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        """
[debf5c3]59        Browse the file and calculate slit lenght upon loading
[d1fb22ee]60        """
[253e7170]61        path_str = self.chooseFile()
[d1fb22ee]62        if not path_str:
63            return
[253e7170]64        loader = Loader()
65        data = loader.load(path_str)
[d1fb22ee]66
[0532d7c1]67        self.data_file.setText(os.path.basename(path_str))
68        self.calculateSlitSize(data)
[d1fb22ee]69
[253e7170]70    def chooseFile(self):
[d1fb22ee]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)
[253e7170]77        path = QtGui.QFileDialog.getOpenFileName(self, "Choose a file", "",
[ab9984e]78                                                 "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", None,
79                                                 QtGui.QFileDialog.DontUseNativeDialog)
[debf5c3]80
[253e7170]81        if path is None:
[d1fb22ee]82            return
83
[253e7170]84        if isinstance(path, QtCore.QString):
85            path = str(path)
[d1fb22ee]86
[253e7170]87        return path
[d1fb22ee]88
89    def onClose(self):
90        """
91        close the window containing this panel
92        """
93        self.close()
[a8ec5b1]94
[886d2f2c]95    def clearResults(self):
96        """
97        Clear the content of output LineEdits
98        """
99        self.slit_length_out.setText("ERROR!")
100        self.unit_out.clear()
101
[0532d7c1]102    def calculateSlitSize(self, data=None):
[a8ec5b1]103        """
[debf5c3]104        Computes slit lenght from given 1D data
[a8ec5b1]105        """
[253e7170]106
107        if data is None:
[886d2f2c]108            self.clearResults()
[253e7170]109            msg = "ERROR: Data hasn't been loaded correctly"
110            raise RuntimeError, msg
111
[debf5c3]112        if data.__class__.__name__ == 'Data2D':
[886d2f2c]113            self.clearResults()
[a8ec5b1]114            msg = "Slit Length cannot be computed for 2D Data"
[886d2f2c]115            raise RuntimeError, msg
[253e7170]116
[a8ec5b1]117        #compute the slit size
118        try:
[ab9984e]119            xdata = data.x
120            ydata = data.y
121            if xdata == [] or xdata is None or ydata == [] or ydata is None:
122                msg = "The current data is empty please check x and y"
123                raise ValueError, msg
124            slit_length_calculator = SlitlengthCalculator()
125            slit_length_calculator.set_data(x=xdata, y=ydata)
126            slit_length = slit_length_calculator.calculate_slit_length()
[a8ec5b1]127        except:
[886d2f2c]128            self.clearResults()
[ab9984e]129            msg = "Slit Size Calculator: %s" % (sys.exc_value)
130            raise RuntimeError, msg
[253e7170]131
[0532d7c1]132        slit_length_str = "{:.5f}".format(slit_length)
133        self.slit_length_out.setText(slit_length_str)
[debf5c3]134
135        #Display unit, which most likely needs to be 1/Ang but needs to be confirmed
[0532d7c1]136        self.unit_out.setText("[Unknown]")
[a8ec5b1]137
Note: See TracBrowser for help on using the repository browser.