source: sasview/src/sas/qtgui/SlitSizeCalculator.py @ debf5c3

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 debf5c3 was debf5c3, checked in by wojciech, 7 years ago

Addded tests for raised exceptions

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