1 | import sys |
---|
2 | import numpy |
---|
3 | |
---|
4 | from PyQt4 import QtCore |
---|
5 | from PyQt4 import QtGui |
---|
6 | |
---|
7 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
8 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
9 | |
---|
10 | from FittingWidget import FittingWidget |
---|
11 | from FitPage import FitPage |
---|
12 | |
---|
13 | class FittingWindow(QtGui.QTabWidget): |
---|
14 | """ |
---|
15 | """ |
---|
16 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
17 | def __init__(self, parent=None, data=None): |
---|
18 | super(FittingWindow, self).__init__() |
---|
19 | |
---|
20 | self.parent = parent |
---|
21 | self._data = data |
---|
22 | |
---|
23 | # List of active fits |
---|
24 | self.tabs = [] |
---|
25 | |
---|
26 | # Max index for adding new, non-clashing tab names |
---|
27 | self.maxIndex = 0 |
---|
28 | |
---|
29 | # Index of the current tab |
---|
30 | self.currentTab = 0 |
---|
31 | |
---|
32 | # The current optimizer |
---|
33 | self.optimizer = 'DREAM' |
---|
34 | |
---|
35 | # The tabs need to be closeable |
---|
36 | self.setTabsClosable(True) |
---|
37 | |
---|
38 | self.communicate = self.parent.communicator() |
---|
39 | |
---|
40 | # Initialize the first tab |
---|
41 | self.addFit(None) |
---|
42 | |
---|
43 | # Deal with signals |
---|
44 | self.tabCloseRequested.connect(self.tabCloses) |
---|
45 | |
---|
46 | # Perspective window not allowed to close by default |
---|
47 | self._allow_close = False |
---|
48 | |
---|
49 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
50 | |
---|
51 | def setClosable(self, value=True): |
---|
52 | """ |
---|
53 | Allow outsiders close this widget |
---|
54 | """ |
---|
55 | assert isinstance(value, bool) |
---|
56 | |
---|
57 | self._allow_close = value |
---|
58 | |
---|
59 | def closeEvent(self, event): |
---|
60 | """ |
---|
61 | Overwrite QDialog close method to allow for custom widget close |
---|
62 | """ |
---|
63 | if self._allow_close: |
---|
64 | # reset the closability flag |
---|
65 | self.setClosable(value=False) |
---|
66 | event.accept() |
---|
67 | else: |
---|
68 | event.ignore() |
---|
69 | # Maybe we should just minimize |
---|
70 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
71 | |
---|
72 | def addFit(self, data): |
---|
73 | """ |
---|
74 | Add a new tab for passed data |
---|
75 | """ |
---|
76 | tab = FittingWidget(parent=self.parent, data=data, 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) |
---|