[4c7dd9f] | 1 | import sys |
---|
[6f7f652] | 2 | import numpy |
---|
[0efe791] | 3 | |
---|
[4c7dd9f] | 4 | from PyQt4 import QtCore |
---|
[60af928] | 5 | from PyQt4 import QtGui |
---|
[4c7dd9f] | 6 | |
---|
[5236449] | 7 | import sas.qtgui.GuiUtils as GuiUtils |
---|
| 8 | |
---|
[60af928] | 9 | from FittingWidget import FittingWidget |
---|
[8bdb6f5] | 10 | |
---|
[60af928] | 11 | class FittingWindow(QtGui.QTabWidget): |
---|
[0efe791] | 12 | """ |
---|
| 13 | """ |
---|
[60af928] | 14 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
[811bec1] | 15 | def __init__(self, parent=None, data=None): |
---|
[f46f6dc] | 16 | super(FittingWindow, self).__init__() |
---|
| 17 | |
---|
| 18 | self.parent = parent |
---|
| 19 | self._data = data |
---|
| 20 | |
---|
| 21 | # List of active fits |
---|
| 22 | self.tabs = [] |
---|
| 23 | |
---|
| 24 | # Max index for adding new, non-clashing tab names |
---|
| 25 | self.maxIndex = 0 |
---|
| 26 | |
---|
| 27 | # Index of the current tab |
---|
| 28 | self.currentTab = 0 |
---|
| 29 | |
---|
| 30 | # The current optimizer |
---|
| 31 | self.optimizer = 'DREAM' |
---|
| 32 | |
---|
| 33 | # The tabs need to be closeable |
---|
| 34 | self.setTabsClosable(True) |
---|
[60af928] | 35 | |
---|
[811bec1] | 36 | self.communicate = self.parent.communicator() |
---|
| 37 | |
---|
[60af928] | 38 | # Initialize the first tab |
---|
[f46f6dc] | 39 | self.addFit(None) |
---|
| 40 | |
---|
| 41 | # Deal with signals |
---|
| 42 | self.tabCloseRequested.connect(self.tabCloses) |
---|
| 43 | |
---|
| 44 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
[60af928] | 45 | |
---|
| 46 | def addFit(self, data): |
---|
| 47 | """ |
---|
| 48 | Add a new tab for passed data |
---|
| 49 | """ |
---|
[811bec1] | 50 | tab = FittingWidget(parent=self.parent, data=data, id=self.maxIndex+1) |
---|
[f46f6dc] | 51 | self.tabs.append(tab) |
---|
| 52 | self.maxIndex += 1 |
---|
| 53 | self.addTab(tab, self.tabName()) |
---|
| 54 | |
---|
| 55 | def tabName(self): |
---|
| 56 | """ |
---|
| 57 | Get the new tab name, based on the number of fitting tabs so far |
---|
| 58 | """ |
---|
| 59 | page_name = "FitPage" + str(self.maxIndex) |
---|
| 60 | return page_name |
---|
| 61 | |
---|
| 62 | def tabCloses(self, index): |
---|
| 63 | """ |
---|
| 64 | Update local bookkeeping on tab close |
---|
| 65 | """ |
---|
[cbcdd2c] | 66 | assert len(self.tabs) >= index |
---|
[f46f6dc] | 67 | # don't remove the last tab |
---|
| 68 | if len(self.tabs) <= 1: |
---|
| 69 | return |
---|
| 70 | del self.tabs[index] |
---|
| 71 | self.removeTab(index) |
---|
| 72 | |
---|
| 73 | def allowBatch(self): |
---|
| 74 | """ |
---|
| 75 | Tell the caller that we accept multiple data instances |
---|
| 76 | """ |
---|
| 77 | return True |
---|
| 78 | |
---|
[68c96d3] | 79 | def setData(self, data_item=None): |
---|
[f46f6dc] | 80 | """ |
---|
| 81 | Assign new dataset to the fitting instance |
---|
[5236449] | 82 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
| 83 | Pass it over to the calculator |
---|
[f46f6dc] | 84 | """ |
---|
[cbcdd2c] | 85 | assert data_item is not None |
---|
[68c96d3] | 86 | |
---|
[5236449] | 87 | if not isinstance(data_item, list): |
---|
| 88 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
| 89 | raise AttributeError, msg |
---|
| 90 | |
---|
| 91 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
| 92 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
| 93 | raise AttributeError, msg |
---|
| 94 | |
---|
| 95 | self._model_item = data_item[0] |
---|
| 96 | |
---|
| 97 | # Find the first unassigned tab. |
---|
[68c96d3] | 98 | # If none, open a new tab. |
---|
[cbcdd2c] | 99 | available_tabs = list(map(lambda tab: tab.acceptsData(), self.tabs)) |
---|
[68c96d3] | 100 | |
---|
[6f7f652] | 101 | if numpy.any(available_tabs): |
---|
[68c96d3] | 102 | self.tabs[available_tabs.index(True)].data = data_item |
---|
[f46f6dc] | 103 | else: |
---|
[68c96d3] | 104 | self.addFit(data_item) |
---|