[d1fb22ee] | 1 | from PyQt4 import QtGui |
---|
| 2 | from PyQt4 import QtCore |
---|
| 3 | from UI.SlitSizeCalculator import Ui_SlitSizeCalculator |
---|
[a8ec5b1] | 4 | from twisted.internet import threads |
---|
| 5 | import logging |
---|
[d1fb22ee] | 6 | |
---|
| 7 | # sas-global |
---|
| 8 | from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator |
---|
[a8ec5b1] | 9 | from DataExplorer import DataExplorerWindow |
---|
[d1fb22ee] | 10 | |
---|
| 11 | class SlitSizeCalculator(QtGui.QDialog, Ui_SlitSizeCalculator): |
---|
[a8ec5b1] | 12 | def __init__(self, parent=None, guimanager=None, manager=None): |
---|
[d1fb22ee] | 13 | super(SlitSizeCalculator, self).__init__() |
---|
| 14 | self.setupUi(self) |
---|
| 15 | |
---|
| 16 | self.setWindowTitle("Slit Size Calculator") |
---|
| 17 | self._parent = parent |
---|
[a8ec5b1] | 18 | self._guimanager = guimanager |
---|
| 19 | self._manager = manager |
---|
[d1fb22ee] | 20 | |
---|
| 21 | self.thickness = SlitlengthCalculator() |
---|
| 22 | |
---|
| 23 | # signals |
---|
| 24 | self.helpButton.clicked.connect(self.onHelp) |
---|
| 25 | self.browseButton.clicked.connect(self.onBrowse) |
---|
| 26 | self.closeButton.clicked.connect(self.onClose) |
---|
| 27 | |
---|
| 28 | # no reason to have this widget resizable |
---|
| 29 | self.setFixedSize(self.minimumSizeHint()) |
---|
| 30 | |
---|
| 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 | """ |
---|
| 52 | path_str = self.chooseFiles() |
---|
| 53 | if not path_str: |
---|
| 54 | return |
---|
| 55 | self.loadFromURL(path_str) |
---|
[a8ec5b1] | 56 | #Path_str is a list - it needs to be changed, so that only one file can be uploaded |
---|
| 57 | self.deltaq_in.setText(path_str[0]) |
---|
[d1fb22ee] | 58 | |
---|
| 59 | def loadFromURL(self, url): |
---|
| 60 | """ |
---|
| 61 | Threaded file load |
---|
| 62 | """ |
---|
[a8ec5b1] | 63 | data_explorer = DataExplorerWindow(parent=self._parent, guimanager=self._guimanager) |
---|
| 64 | load_thread = threads.deferToThread(data_explorer.readData, url) |
---|
| 65 | load_thread.addCallback(data_explorer.loadComplete) |
---|
| 66 | #On complete loading |
---|
[d1fb22ee] | 67 | |
---|
| 68 | def chooseFiles(self): |
---|
| 69 | """ |
---|
| 70 | Shows the Open file dialog and returns the chosen path(s) |
---|
| 71 | """ |
---|
| 72 | |
---|
| 73 | # Location is automatically saved - no need to keep track of the last dir |
---|
| 74 | # But only with Qt built-in dialog (non-platform native) |
---|
| 75 | paths = QtGui.QFileDialog.getOpenFileNames(self, "Choose a file", "", |
---|
[a8ec5b1] | 76 | "SAXSess Data 1D (*.txt *.TXT *.dat *.DAT)", None, |
---|
| 77 | QtGui.QFileDialog.DontUseNativeDialog) |
---|
[d1fb22ee] | 78 | if paths is None: |
---|
| 79 | return |
---|
| 80 | |
---|
| 81 | if isinstance(paths, QtCore.QStringList): |
---|
| 82 | paths = [str(f) for f in paths] |
---|
| 83 | |
---|
| 84 | if not isinstance(paths, list): |
---|
| 85 | paths = [paths] |
---|
| 86 | |
---|
| 87 | return paths |
---|
| 88 | |
---|
| 89 | def onClose(self): |
---|
| 90 | """ |
---|
| 91 | close the window containing this panel |
---|
| 92 | """ |
---|
| 93 | self.close() |
---|
[a8ec5b1] | 94 | |
---|
| 95 | def complete_loading(self, data=None): |
---|
| 96 | """ |
---|
| 97 | Complete the loading and compute the slit size |
---|
| 98 | """ |
---|
| 99 | #TODO: Provided we have an access to data then it should be fairly easy |
---|
| 100 | index = self.treeView.selectedIndexes()[0] |
---|
| 101 | model_item = self.model.itemFromIndex(self.data_proxy.mapToSource(index)) |
---|
| 102 | data = GuiUtils.dataFromItem(model_item) |
---|
| 103 | |
---|
| 104 | if data is None or isinstance(data, Data2D): |
---|
| 105 | #I guess this doesn't apply |
---|
| 106 | if self.parent.parent is None: |
---|
| 107 | return |
---|
| 108 | msg = "Slit Length cannot be computed for 2D Data" |
---|
| 109 | logging.info(msg) |
---|
| 110 | return |
---|
| 111 | self.data_name_tcl.SetValue(str(data.filename)) |
---|
| 112 | #compute the slit size |
---|
| 113 | try: |
---|
| 114 | x = data.x |
---|
| 115 | y = data.y |
---|
| 116 | if x == [] or x is None or y == [] or y is None: |
---|
| 117 | msg = "The current data is empty please check x and y" |
---|
| 118 | raise ValueError, msg |
---|
| 119 | slit_length_calculator = SlitlengthCalculator() |
---|
| 120 | slit_length_calculator.set_data(x=x, y=y) |
---|
| 121 | slit_length = slit_length_calculator.calculate_slit_length() |
---|
| 122 | except: |
---|
| 123 | if self.parent.parent is None: |
---|
| 124 | return |
---|
| 125 | msg = "Slit Size Calculator: %s" % (sys.exc_value) |
---|
| 126 | logging.info(msg) |
---|
| 127 | return |
---|
| 128 | self.slit_size_tcl.SetValue(str(slit_length)) |
---|
| 129 | #Display unit |
---|
| 130 | self.slit_size_unit_tcl.SetValue('[Unknown]') |
---|
| 131 | if self.parent.parent is None: |
---|
| 132 | return |
---|
| 133 | msg = "Load Complete" |
---|
| 134 | logging.info(msg) |
---|
| 135 | |
---|