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