1 | """ |
---|
2 | Slit Size Calculator Panel |
---|
3 | """ |
---|
4 | import os |
---|
5 | import sys |
---|
6 | |
---|
7 | from PyQt4 import QtGui |
---|
8 | from PyQt4 import QtCore |
---|
9 | from UI.SlitSizeCalculator import Ui_SlitSizeCalculator |
---|
10 | from sas.sascalc.dataloader.loader import Loader |
---|
11 | from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator |
---|
12 | |
---|
13 | |
---|
14 | class SlitSizeCalculator(QtGui.QDialog, Ui_SlitSizeCalculator): |
---|
15 | """ |
---|
16 | Provides the slit length calculator GUI. |
---|
17 | """ |
---|
18 | def __init__(self, parent=None): |
---|
19 | super(SlitSizeCalculator, self).__init__() |
---|
20 | self.setupUi(self) |
---|
21 | |
---|
22 | self.setWindowTitle("Slit Size Calculator") |
---|
23 | self._parent = parent |
---|
24 | |
---|
25 | self.thickness = SlitlengthCalculator() |
---|
26 | |
---|
27 | # signals |
---|
28 | self.helpButton.clicked.connect(self.onHelp) |
---|
29 | self.browseButton.clicked.connect(self.onBrowse) |
---|
30 | self.closeButton.clicked.connect(self.onClose) |
---|
31 | |
---|
32 | # no reason to have this widget resizable |
---|
33 | self.setFixedSize(self.minimumSizeHint()) |
---|
34 | |
---|
35 | |
---|
36 | def onHelp(self): |
---|
37 | """ |
---|
38 | Bring up the Slit Size Calculator calculator Documentation whenever |
---|
39 | the HELP button is clicked. |
---|
40 | Calls DocumentationWindow with the path of the location within the |
---|
41 | documentation tree (after /doc/ ....". |
---|
42 | """ |
---|
43 | try: |
---|
44 | location = self._parent.HELP_DIRECTORY_LOCATION + \ |
---|
45 | "/user/sasgui/perspectives/calculator/slit_calculator_help.html" |
---|
46 | |
---|
47 | self._parent._helpView.load(QtCore.QUrl(location)) |
---|
48 | self._parent._helpView.show() |
---|
49 | except AttributeError: |
---|
50 | # No manager defined - testing and standalone runs |
---|
51 | pass |
---|
52 | |
---|
53 | def onBrowse(self): |
---|
54 | """ |
---|
55 | Browse the file and calculate slit lenght upon loading |
---|
56 | """ |
---|
57 | path_str = self.chooseFile() |
---|
58 | if not path_str: |
---|
59 | return |
---|
60 | loader = Loader() |
---|
61 | data = loader.load(path_str) |
---|
62 | |
---|
63 | self.data_file.setText(os.path.basename(path_str)) |
---|
64 | self.calculateSlitSize(data) |
---|
65 | |
---|
66 | def chooseFile(self): |
---|
67 | """ |
---|
68 | Shows the Open file dialog and returns the chosen path(s) |
---|
69 | """ |
---|
70 | |
---|
71 | # Location is automatically saved - no need to keep track of the last dir |
---|
72 | # But only with Qt built-in dialog (non-platform native) |
---|
73 | path = QtGui.QFileDialog.getOpenFileName(self, "Choose a file", "", |
---|
74 | "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", None, |
---|
75 | QtGui.QFileDialog.DontUseNativeDialog) |
---|
76 | |
---|
77 | if path is None: |
---|
78 | return |
---|
79 | |
---|
80 | if isinstance(path, QtCore.QString): |
---|
81 | path = str(path) |
---|
82 | |
---|
83 | return path |
---|
84 | |
---|
85 | def onClose(self): |
---|
86 | """ |
---|
87 | close the window containing this panel |
---|
88 | """ |
---|
89 | self.close() |
---|
90 | |
---|
91 | def clearResults(self): |
---|
92 | """ |
---|
93 | Clear the content of output LineEdits |
---|
94 | """ |
---|
95 | self.slit_length_out.setText("ERROR!") |
---|
96 | self.unit_out.clear() |
---|
97 | |
---|
98 | def calculateSlitSize(self, data=None): |
---|
99 | """ |
---|
100 | Computes slit lenght from given 1D data |
---|
101 | """ |
---|
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_value) |
---|
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 | |
---|