[6f7f652] | 1 | import numpy |
---|
[0efe791] | 2 | |
---|
[4c7dd9f] | 3 | from PyQt4 import QtCore |
---|
[60af928] | 4 | from PyQt4 import QtGui |
---|
[4c7dd9f] | 5 | |
---|
[61a92d4] | 6 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
[5236449] | 7 | |
---|
[1bc27f1] | 8 | from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget |
---|
[8bdb6f5] | 9 | |
---|
[60af928] | 10 | class FittingWindow(QtGui.QTabWidget): |
---|
[0efe791] | 11 | """ |
---|
| 12 | """ |
---|
[60af928] | 13 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
[811bec1] | 14 | def __init__(self, parent=None, data=None): |
---|
[f46f6dc] | 15 | super(FittingWindow, self).__init__() |
---|
| 16 | |
---|
| 17 | self.parent = parent |
---|
| 18 | self._data = data |
---|
| 19 | |
---|
| 20 | # List of active fits |
---|
| 21 | self.tabs = [] |
---|
| 22 | |
---|
| 23 | # Max index for adding new, non-clashing tab names |
---|
| 24 | self.maxIndex = 0 |
---|
| 25 | |
---|
| 26 | # Index of the current tab |
---|
| 27 | self.currentTab = 0 |
---|
| 28 | |
---|
| 29 | # The current optimizer |
---|
[1bc27f1] | 30 | self.optimizer = 'Levenberg-Marquardt' |
---|
[f46f6dc] | 31 | |
---|
| 32 | # The tabs need to be closeable |
---|
| 33 | self.setTabsClosable(True) |
---|
[60af928] | 34 | |
---|
[811bec1] | 35 | self.communicate = self.parent.communicator() |
---|
| 36 | |
---|
[60af928] | 37 | # Initialize the first tab |
---|
[f46f6dc] | 38 | self.addFit(None) |
---|
| 39 | |
---|
| 40 | # Deal with signals |
---|
| 41 | self.tabCloseRequested.connect(self.tabCloses) |
---|
| 42 | |
---|
[b1e36a3] | 43 | # Perspective window not allowed to close by default |
---|
| 44 | self._allow_close = False |
---|
| 45 | |
---|
[f46f6dc] | 46 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
[60af928] | 47 | |
---|
[b1e36a3] | 48 | def setClosable(self, value=True): |
---|
| 49 | """ |
---|
| 50 | Allow outsiders close this widget |
---|
| 51 | """ |
---|
| 52 | assert isinstance(value, bool) |
---|
| 53 | |
---|
| 54 | self._allow_close = value |
---|
| 55 | |
---|
| 56 | def closeEvent(self, event): |
---|
| 57 | """ |
---|
| 58 | Overwrite QDialog close method to allow for custom widget close |
---|
| 59 | """ |
---|
[2add354] | 60 | # Invoke fit page events |
---|
| 61 | for tab in self.tabs: |
---|
| 62 | tab.close() |
---|
[b1e36a3] | 63 | if self._allow_close: |
---|
| 64 | # reset the closability flag |
---|
| 65 | self.setClosable(value=False) |
---|
| 66 | event.accept() |
---|
| 67 | else: |
---|
| 68 | # Maybe we should just minimize |
---|
| 69 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
[2add354] | 70 | event.ignore() |
---|
[b1e36a3] | 71 | |
---|
[60af928] | 72 | def addFit(self, data): |
---|
| 73 | """ |
---|
| 74 | Add a new tab for passed data |
---|
| 75 | """ |
---|
[1bc27f1] | 76 | tab = FittingWidget(parent=self.parent, data=data, tab_id=self.maxIndex+1) |
---|
[61a92d4] | 77 | # Add this tab to the object library so it can be retrieved by scripting/jupyter |
---|
| 78 | ObjectLibrary.addObject(self.tabName(), tab) |
---|
[f46f6dc] | 79 | self.tabs.append(tab) |
---|
| 80 | self.maxIndex += 1 |
---|
| 81 | self.addTab(tab, self.tabName()) |
---|
| 82 | |
---|
| 83 | def tabName(self): |
---|
| 84 | """ |
---|
| 85 | Get the new tab name, based on the number of fitting tabs so far |
---|
| 86 | """ |
---|
| 87 | page_name = "FitPage" + str(self.maxIndex) |
---|
| 88 | return page_name |
---|
| 89 | |
---|
| 90 | def tabCloses(self, index): |
---|
| 91 | """ |
---|
| 92 | Update local bookkeeping on tab close |
---|
| 93 | """ |
---|
[cbcdd2c] | 94 | assert len(self.tabs) >= index |
---|
[f46f6dc] | 95 | # don't remove the last tab |
---|
| 96 | if len(self.tabs) <= 1: |
---|
| 97 | return |
---|
[61a92d4] | 98 | ObjectLibrary.deleteObjectByRef(self.tabs[index]) |
---|
[f46f6dc] | 99 | del self.tabs[index] |
---|
| 100 | self.removeTab(index) |
---|
| 101 | |
---|
| 102 | def allowBatch(self): |
---|
| 103 | """ |
---|
| 104 | Tell the caller that we accept multiple data instances |
---|
| 105 | """ |
---|
| 106 | return True |
---|
| 107 | |
---|
[68c96d3] | 108 | def setData(self, data_item=None): |
---|
[f46f6dc] | 109 | """ |
---|
| 110 | Assign new dataset to the fitting instance |
---|
[5236449] | 111 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
| 112 | Pass it over to the calculator |
---|
[f46f6dc] | 113 | """ |
---|
[cbcdd2c] | 114 | assert data_item is not None |
---|
[68c96d3] | 115 | |
---|
[5236449] | 116 | if not isinstance(data_item, list): |
---|
| 117 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
| 118 | raise AttributeError, msg |
---|
| 119 | |
---|
| 120 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
| 121 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
| 122 | raise AttributeError, msg |
---|
| 123 | |
---|
[454670d] | 124 | for data in data_item: |
---|
| 125 | # Find the first unassigned tab. |
---|
| 126 | # If none, open a new tab. |
---|
| 127 | available_tabs = list(map(lambda tab: tab.acceptsData(), self.tabs)) |
---|
| 128 | |
---|
| 129 | if numpy.any(available_tabs): |
---|
| 130 | self.tabs[available_tabs.index(True)].data = data |
---|
| 131 | else: |
---|
| 132 | self.addFit(data) |
---|