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 | location = "/user/sasgui/perspectives/calculator/slit_calculator_help.html" |
---|
50 | self._parent.showHelp(location) |
---|
51 | |
---|
52 | def onBrowse(self): |
---|
53 | """ |
---|
54 | Browse the file and calculate slit lenght upon loading |
---|
55 | """ |
---|
56 | path_str = self.chooseFile() |
---|
57 | if not path_str: |
---|
58 | return |
---|
59 | loader = Loader() |
---|
60 | try: |
---|
61 | data = loader.load(path_str) |
---|
62 | data = data[0] |
---|
63 | # Can return multiple exceptions - gather them all under one umbrella and complain |
---|
64 | except Exception as ex: |
---|
65 | logging.error(ex) |
---|
66 | return |
---|
67 | |
---|
68 | self.data_file.setText(os.path.basename(path_str)) |
---|
69 | self.calculateSlitSize(data) |
---|
70 | |
---|
71 | def chooseFile(self): |
---|
72 | """ |
---|
73 | Shows the Open file dialog and returns the chosen path(s) |
---|
74 | """ |
---|
75 | |
---|
76 | # Location is automatically saved - no need to keep track of the last dir |
---|
77 | # But only with Qt built-in dialog (non-platform native) |
---|
78 | path = QtWidgets.QFileDialog.getOpenFileName(self, "Choose a file", "", |
---|
79 | "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", |
---|
80 | None, |
---|
81 | QtWidgets.QFileDialog.DontUseNativeDialog)[0] |
---|
82 | return path |
---|
83 | |
---|
84 | def onClose(self): |
---|
85 | """ |
---|
86 | close the window containing this panel |
---|
87 | """ |
---|
88 | self.close() |
---|
89 | |
---|
90 | def clearResults(self): |
---|
91 | """ |
---|
92 | Clear the content of output LineEdits |
---|
93 | """ |
---|
94 | self.slit_length_out.setText("ERROR!") |
---|
95 | self.unit_out.clear() |
---|
96 | |
---|
97 | def calculateSlitSize(self, data=None): |
---|
98 | """ |
---|
99 | Computes slit lenght from given 1D data |
---|
100 | """ |
---|
101 | if data is None: |
---|
102 | self.clearResults() |
---|
103 | msg = "ERROR: Data hasn't been loaded correctly" |
---|
104 | logging.error(msg) |
---|
105 | return |
---|
106 | |
---|
107 | if data.__class__.__name__ == 'Data2D': |
---|
108 | self.clearResults() |
---|
109 | msg = "Slit Length cannot be computed for 2D Data" |
---|
110 | logging.error(msg) |
---|
111 | return |
---|
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 | if (not xdata or xdata is None) or (not ydata or ydata is None): |
---|
119 | msg = "The current data is empty please check x and y" |
---|
120 | logging.error(msg) |
---|
121 | return |
---|
122 | slit_length_calculator = SlitlengthCalculator() |
---|
123 | slit_length_calculator.set_data(x=xdata, y=ydata) |
---|
124 | slit_length = slit_length_calculator.calculate_slit_length() |
---|
125 | except: |
---|
126 | self.clearResults() |
---|
127 | msg = "Slit Size Calculator: %s" % (sys.exc_info()[1]) |
---|
128 | logging.error(msg) |
---|
129 | return |
---|
130 | |
---|
131 | slit_length_str = "{:.5f}".format(slit_length) |
---|
132 | self.slit_length_out.setText(slit_length_str) |
---|
133 | |
---|
134 | #Display unit, which most likely needs to be 1/Ang but needs to be confirmed |
---|
135 | self.unit_out.setText("[Unknown]") |
---|
136 | |
---|