source: sasview/src/sas/qtgui/SlitSizeCalculator.py @ 3a0ce4f

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

Added a few unit tests

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