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