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 | |
---|
10 | from sas.qtgui.UI import main_resources_rc |
---|
11 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
12 | |
---|
13 | from UI.SlitSizeCalculator import Ui_SlitSizeCalculator |
---|
14 | from sas.sascalc.dataloader.loader import Loader |
---|
15 | from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator |
---|
16 | |
---|
17 | |
---|
18 | class SlitSizeCalculator(QtGui.QDialog, Ui_SlitSizeCalculator): |
---|
19 | """ |
---|
20 | Provides the slit length calculator GUI. |
---|
21 | """ |
---|
22 | def __init__(self, parent=None): |
---|
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 | |
---|
39 | |
---|
40 | def onHelp(self): |
---|
41 | """ |
---|
42 | Bring up the Slit Size Calculator calculator Documentation whenever |
---|
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: |
---|
48 | location = GuiUtils.HELP_DIRECTORY_LOCATION + \ |
---|
49 | "/user/sasgui/perspectives/calculator/slit_calculator_help.html" |
---|
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 | """ |
---|
59 | Browse the file and calculate slit lenght upon loading |
---|
60 | """ |
---|
61 | path_str = self.chooseFile() |
---|
62 | if not path_str: |
---|
63 | return |
---|
64 | loader = Loader() |
---|
65 | data = loader.load(path_str) |
---|
66 | |
---|
67 | self.data_file.setText(os.path.basename(path_str)) |
---|
68 | self.calculateSlitSize(data) |
---|
69 | |
---|
70 | def chooseFile(self): |
---|
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) |
---|
77 | path = QtGui.QFileDialog.getOpenFileName(self, "Choose a file", "", |
---|
78 | "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", None, |
---|
79 | QtGui.QFileDialog.DontUseNativeDialog) |
---|
80 | |
---|
81 | if path is None: |
---|
82 | return |
---|
83 | |
---|
84 | if isinstance(path, QtCore.QString): |
---|
85 | path = str(path) |
---|
86 | |
---|
87 | return path |
---|
88 | |
---|
89 | def onClose(self): |
---|
90 | """ |
---|
91 | close the window containing this panel |
---|
92 | """ |
---|
93 | self.close() |
---|
94 | |
---|
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 | |
---|
102 | def calculateSlitSize(self, data=None): |
---|
103 | """ |
---|
104 | Computes slit lenght from given 1D data |
---|
105 | """ |
---|
106 | |
---|
107 | if data is None: |
---|
108 | self.clearResults() |
---|
109 | msg = "ERROR: Data hasn't been loaded correctly" |
---|
110 | raise RuntimeError, msg |
---|
111 | |
---|
112 | if data.__class__.__name__ == 'Data2D': |
---|
113 | self.clearResults() |
---|
114 | msg = "Slit Length cannot be computed for 2D Data" |
---|
115 | raise RuntimeError, msg |
---|
116 | |
---|
117 | #compute the slit size |
---|
118 | try: |
---|
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() |
---|
127 | except: |
---|
128 | self.clearResults() |
---|
129 | msg = "Slit Size Calculator: %s" % (sys.exc_value) |
---|
130 | raise RuntimeError, msg |
---|
131 | |
---|
132 | slit_length_str = "{:.5f}".format(slit_length) |
---|
133 | self.slit_length_out.setText(slit_length_str) |
---|
134 | |
---|
135 | #Display unit, which most likely needs to be 1/Ang but needs to be confirmed |
---|
136 | self.unit_out.setText("[Unknown]") |
---|
137 | |
---|