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