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