[ab9984e] | 1 | """ |
---|
| 2 | Slit Size Calculator Panel |
---|
| 3 | """ |
---|
| 4 | import os |
---|
| 5 | import sys |
---|
[fbfc488] | 6 | import logging |
---|
[ab9984e] | 7 | |
---|
[4992ff2] | 8 | from PyQt5 import QtCore |
---|
| 9 | from PyQt5 import QtGui |
---|
| 10 | from PyQt5 import QtWidgets |
---|
[cd2cc745] | 11 | |
---|
| 12 | from sas.qtgui.UI import main_resources_rc |
---|
[b0c5e8c] | 13 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[cd2cc745] | 14 | |
---|
[b3e8629] | 15 | from .UI.SlitSizeCalculator import Ui_SlitSizeCalculator |
---|
[253e7170] | 16 | from sas.sascalc.dataloader.loader import Loader |
---|
[d1fb22ee] | 17 | from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator |
---|
[253e7170] | 18 | |
---|
[d1fb22ee] | 19 | |
---|
[4992ff2] | 20 | class SlitSizeCalculator(QtWidgets.QDialog, Ui_SlitSizeCalculator): |
---|
[debf5c3] | 21 | """ |
---|
| 22 | Provides the slit length calculator GUI. |
---|
| 23 | """ |
---|
[0532d7c1] | 24 | def __init__(self, parent=None): |
---|
[d1fb22ee] | 25 | super(SlitSizeCalculator, self).__init__() |
---|
| 26 | self.setupUi(self) |
---|
[33c0561] | 27 | # disable the context help icon |
---|
| 28 | self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint) |
---|
[d1fb22ee] | 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 | |
---|
[0532d7c1] | 43 | |
---|
[d1fb22ee] | 44 | def onHelp(self): |
---|
| 45 | """ |
---|
[debf5c3] | 46 | Bring up the Slit Size Calculator calculator Documentation whenever |
---|
[d1fb22ee] | 47 | the HELP button is clicked. |
---|
| 48 | Calls DocumentationWindow with the path of the location within the |
---|
| 49 | documentation tree (after /doc/ ....". |
---|
| 50 | """ |
---|
[aed0532] | 51 | location = "/user/qtgui/Calculators/slit_calculator_help.html" |
---|
[e90988c] | 52 | self._parent.showHelp(location) |
---|
[d1fb22ee] | 53 | |
---|
| 54 | def onBrowse(self): |
---|
| 55 | """ |
---|
[debf5c3] | 56 | Browse the file and calculate slit lenght upon loading |
---|
[d1fb22ee] | 57 | """ |
---|
[253e7170] | 58 | path_str = self.chooseFile() |
---|
[d1fb22ee] | 59 | if not path_str: |
---|
| 60 | return |
---|
[253e7170] | 61 | loader = Loader() |
---|
[fbfc488] | 62 | try: |
---|
[53c771e] | 63 | data = loader.load(path_str) |
---|
| 64 | data = data[0] |
---|
[fbfc488] | 65 | # Can return multiple exceptions - gather them all under one umbrella and complain |
---|
| 66 | except Exception as ex: |
---|
| 67 | logging.error(ex) |
---|
| 68 | return |
---|
[d1fb22ee] | 69 | |
---|
[0532d7c1] | 70 | self.data_file.setText(os.path.basename(path_str)) |
---|
| 71 | self.calculateSlitSize(data) |
---|
[d1fb22ee] | 72 | |
---|
[253e7170] | 73 | def chooseFile(self): |
---|
[d1fb22ee] | 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) |
---|
[4992ff2] | 80 | path = QtWidgets.QFileDialog.getOpenFileName(self, "Choose a file", "", |
---|
[b3e8629] | 81 | "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", |
---|
[4992ff2] | 82 | None, |
---|
[fbfc488] | 83 | QtWidgets.QFileDialog.DontUseNativeDialog)[0] |
---|
[253e7170] | 84 | return path |
---|
[d1fb22ee] | 85 | |
---|
| 86 | def onClose(self): |
---|
| 87 | """ |
---|
| 88 | close the window containing this panel |
---|
| 89 | """ |
---|
| 90 | self.close() |
---|
[a8ec5b1] | 91 | |
---|
[886d2f2c] | 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 | |
---|
[0532d7c1] | 99 | def calculateSlitSize(self, data=None): |
---|
[a8ec5b1] | 100 | """ |
---|
[debf5c3] | 101 | Computes slit lenght from given 1D data |
---|
[a8ec5b1] | 102 | """ |
---|
[253e7170] | 103 | if data is None: |
---|
[886d2f2c] | 104 | self.clearResults() |
---|
[253e7170] | 105 | msg = "ERROR: Data hasn't been loaded correctly" |
---|
[fbfc488] | 106 | logging.error(msg) |
---|
| 107 | return |
---|
[253e7170] | 108 | |
---|
[debf5c3] | 109 | if data.__class__.__name__ == 'Data2D': |
---|
[886d2f2c] | 110 | self.clearResults() |
---|
[a8ec5b1] | 111 | msg = "Slit Length cannot be computed for 2D Data" |
---|
[fbfc488] | 112 | logging.error(msg) |
---|
| 113 | return |
---|
[253e7170] | 114 | |
---|
[a8ec5b1] | 115 | #compute the slit size |
---|
| 116 | try: |
---|
[ab9984e] | 117 | xdata = data.x |
---|
| 118 | ydata = data.y |
---|
[fa05c6c1] | 119 | if xdata == [] or xdata is None or ydata == [] or ydata is None: |
---|
[ab9984e] | 120 | msg = "The current data is empty please check x and y" |
---|
[fbfc488] | 121 | logging.error(msg) |
---|
| 122 | return |
---|
[ab9984e] | 123 | slit_length_calculator = SlitlengthCalculator() |
---|
| 124 | slit_length_calculator.set_data(x=xdata, y=ydata) |
---|
| 125 | slit_length = slit_length_calculator.calculate_slit_length() |
---|
[a8ec5b1] | 126 | except: |
---|
[886d2f2c] | 127 | self.clearResults() |
---|
[b3e8629] | 128 | msg = "Slit Size Calculator: %s" % (sys.exc_info()[1]) |
---|
[fbfc488] | 129 | logging.error(msg) |
---|
| 130 | return |
---|
[253e7170] | 131 | |
---|
[0532d7c1] | 132 | slit_length_str = "{:.5f}".format(slit_length) |
---|
| 133 | self.slit_length_out.setText(slit_length_str) |
---|
[debf5c3] | 134 | |
---|
| 135 | #Display unit, which most likely needs to be 1/Ang but needs to be confirmed |
---|
[0532d7c1] | 136 | self.unit_out.setText("[Unknown]") |
---|
[a8ec5b1] | 137 | |
---|