source: sasview/src/sas/qtgui/SlitSizeCalculator.py @ 0532d7c1

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

Unit tests added

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