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 | # disable the context help icon |
---|
28 | self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint) |
---|
29 | |
---|
30 | self.setWindowTitle("Slit Size Calculator") |
---|
31 | self._parent = parent |
---|
32 | |
---|
33 | self.thickness = SlitlengthCalculator() |
---|
34 | |
---|
35 | # signals |
---|
36 | self.helpButton.clicked.connect(self.onHelp) |
---|
37 | self.browseButton.clicked.connect(self.onBrowse) |
---|
38 | self.closeButton.clicked.connect(self.onClose) |
---|
39 | |
---|
40 | # no reason to have this widget resizable |
---|
41 | self.setFixedSize(self.minimumSizeHint()) |
---|
42 | |
---|
43 | |
---|
44 | def onHelp(self): |
---|
45 | """ |
---|
46 | Bring up the Slit Size Calculator calculator Documentation whenever |
---|
47 | the HELP button is clicked. |
---|
48 | Calls DocumentationWindow with the path of the location within the |
---|
49 | documentation tree (after /doc/ ....". |
---|
50 | """ |
---|
51 | location = "/user/qtgui/Calculators/slit_calculator_help.html" |
---|
52 | self._parent.showHelp(location) |
---|
53 | |
---|
54 | def onBrowse(self): |
---|
55 | """ |
---|
56 | Browse the file and calculate slit lenght upon loading |
---|
57 | """ |
---|
58 | path_str = self.chooseFile() |
---|
59 | if not path_str: |
---|
60 | return |
---|
61 | loader = Loader() |
---|
62 | try: |
---|
63 | data = loader.load(path_str) |
---|
64 | data = data[0] |
---|
65 | # Can return multiple exceptions - gather them all under one umbrella and complain |
---|
66 | except Exception as ex: |
---|
67 | logging.error(ex) |
---|
68 | return |
---|
69 | |
---|
70 | self.data_file.setText(os.path.basename(path_str)) |
---|
71 | self.calculateSlitSize(data) |
---|
72 | |
---|
73 | def chooseFile(self): |
---|
74 | """ |
---|
75 | Shows the Open file dialog and returns the chosen path(s) |
---|
76 | """ |
---|
77 | |
---|
78 | # Location is automatically saved - no need to keep track of the last dir |
---|
79 | # But only with Qt built-in dialog (non-platform native) |
---|
80 | path = QtWidgets.QFileDialog.getOpenFileName(self, "Choose a file", "", |
---|
81 | "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", |
---|
82 | None, |
---|
83 | QtWidgets.QFileDialog.DontUseNativeDialog)[0] |
---|
84 | return path |
---|
85 | |
---|
86 | def onClose(self): |
---|
87 | """ |
---|
88 | close the window containing this panel |
---|
89 | """ |
---|
90 | self.close() |
---|
91 | |
---|
92 | def clearResults(self): |
---|
93 | """ |
---|
94 | Clear the content of output LineEdits |
---|
95 | """ |
---|
96 | self.slit_length_out.setText("ERROR!") |
---|
97 | self.unit_out.clear() |
---|
98 | |
---|
99 | def calculateSlitSize(self, data=None): |
---|
100 | """ |
---|
101 | Computes slit lenght from given 1D data |
---|
102 | """ |
---|
103 | if data is None: |
---|
104 | self.clearResults() |
---|
105 | msg = "ERROR: Data hasn't been loaded correctly" |
---|
106 | logging.error(msg) |
---|
107 | return |
---|
108 | |
---|
109 | if data.__class__.__name__ == 'Data2D': |
---|
110 | self.clearResults() |
---|
111 | msg = "Slit Length cannot be computed for 2D Data" |
---|
112 | logging.error(msg) |
---|
113 | return |
---|
114 | |
---|
115 | #compute the slit size |
---|
116 | try: |
---|
117 | xdata = data.x |
---|
118 | ydata = data.y |
---|
119 | if xdata == [] or xdata is None or ydata == [] or ydata is None: |
---|
120 | msg = "The current data is empty please check x and y" |
---|
121 | logging.error(msg) |
---|
122 | return |
---|
123 | slit_length_calculator = SlitlengthCalculator() |
---|
124 | slit_length_calculator.set_data(x=xdata, y=ydata) |
---|
125 | slit_length = slit_length_calculator.calculate_slit_length() |
---|
126 | except: |
---|
127 | self.clearResults() |
---|
128 | msg = "Slit Size Calculator: %s" % (sys.exc_info()[1]) |
---|
129 | logging.error(msg) |
---|
130 | return |
---|
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 | |
---|