1 | import numpy |
---|
2 | |
---|
3 | from PyQt4 import QtCore |
---|
4 | from PyQt4 import QtGui |
---|
5 | |
---|
6 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
7 | |
---|
8 | from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget |
---|
9 | |
---|
10 | class FittingWindow(QtGui.QTabWidget): |
---|
11 | """ |
---|
12 | """ |
---|
13 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
14 | def __init__(self, parent=None, data=None): |
---|
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 |
---|
30 | self.optimizer = 'Levenberg-Marquardt' |
---|
31 | |
---|
32 | # The tabs need to be closeable |
---|
33 | self.setTabsClosable(True) |
---|
34 | |
---|
35 | self.communicate = self.parent.communicator() |
---|
36 | |
---|
37 | # Initialize the first tab |
---|
38 | self.addFit(None) |
---|
39 | |
---|
40 | # Deal with signals |
---|
41 | self.tabCloseRequested.connect(self.tabCloses) |
---|
42 | |
---|
43 | # Perspective window not allowed to close by default |
---|
44 | self._allow_close = False |
---|
45 | |
---|
46 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
47 | |
---|
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 | """ |
---|
60 | # Invoke fit page events |
---|
61 | for tab in self.tabs: |
---|
62 | tab.close() |
---|
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) |
---|
70 | event.ignore() |
---|
71 | |
---|
72 | def addFit(self, data): |
---|
73 | """ |
---|
74 | Add a new tab for passed data |
---|
75 | """ |
---|
76 | tab = FittingWidget(parent=self.parent, data=data, tab_id=self.maxIndex+1) |
---|
77 | # Add this tab to the object library so it can be retrieved by scripting/jupyter |
---|
78 | ObjectLibrary.addObject(self.tabName(), tab) |
---|
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 | """ |
---|
94 | assert len(self.tabs) >= index |
---|
95 | # don't remove the last tab |
---|
96 | if len(self.tabs) <= 1: |
---|
97 | return |
---|
98 | ObjectLibrary.deleteObjectByRef(self.tabs[index]) |
---|
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 | |
---|
108 | def setData(self, data_item=None): |
---|
109 | """ |
---|
110 | Assign new dataset to the fitting instance |
---|
111 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
112 | Pass it over to the calculator |
---|
113 | """ |
---|
114 | assert data_item is not None |
---|
115 | |
---|
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 | |
---|
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) |
---|