1 | import sys |
---|
2 | import numpy |
---|
3 | |
---|
4 | from PyQt4 import QtCore |
---|
5 | from PyQt4 import QtGui |
---|
6 | |
---|
7 | import sas.qtgui.GuiUtils as GuiUtils |
---|
8 | |
---|
9 | from FittingWidget import FittingWidget |
---|
10 | |
---|
11 | class FittingWindow(QtGui.QTabWidget): |
---|
12 | """ |
---|
13 | """ |
---|
14 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
15 | def __init__(self, parent=None, data=None): |
---|
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) |
---|
35 | |
---|
36 | self.communicate = self.parent.communicator() |
---|
37 | |
---|
38 | # Initialize the first tab |
---|
39 | self.addFit(None) |
---|
40 | |
---|
41 | # Deal with signals |
---|
42 | self.tabCloseRequested.connect(self.tabCloses) |
---|
43 | |
---|
44 | # Perspective window not allowed to close by default |
---|
45 | self._allow_close = False |
---|
46 | |
---|
47 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
48 | |
---|
49 | def setClosable(self, value=True): |
---|
50 | """ |
---|
51 | Allow outsiders close this widget |
---|
52 | """ |
---|
53 | assert isinstance(value, bool) |
---|
54 | |
---|
55 | self._allow_close = value |
---|
56 | |
---|
57 | def closeEvent(self, event): |
---|
58 | """ |
---|
59 | Overwrite QDialog close method to allow for custom widget close |
---|
60 | """ |
---|
61 | if self._allow_close: |
---|
62 | # reset the closability flag |
---|
63 | self.setClosable(value=False) |
---|
64 | event.accept() |
---|
65 | else: |
---|
66 | event.ignore() |
---|
67 | # Maybe we should just minimize |
---|
68 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
69 | |
---|
70 | def addFit(self, data): |
---|
71 | """ |
---|
72 | Add a new tab for passed data |
---|
73 | """ |
---|
74 | tab = FittingWidget(parent=self.parent, data=data, id=self.maxIndex+1) |
---|
75 | self.tabs.append(tab) |
---|
76 | self.maxIndex += 1 |
---|
77 | self.addTab(tab, self.tabName()) |
---|
78 | |
---|
79 | def tabName(self): |
---|
80 | """ |
---|
81 | Get the new tab name, based on the number of fitting tabs so far |
---|
82 | """ |
---|
83 | page_name = "FitPage" + str(self.maxIndex) |
---|
84 | return page_name |
---|
85 | |
---|
86 | def tabCloses(self, index): |
---|
87 | """ |
---|
88 | Update local bookkeeping on tab close |
---|
89 | """ |
---|
90 | assert len(self.tabs) >= index |
---|
91 | # don't remove the last tab |
---|
92 | if len(self.tabs) <= 1: |
---|
93 | return |
---|
94 | del self.tabs[index] |
---|
95 | self.removeTab(index) |
---|
96 | |
---|
97 | def allowBatch(self): |
---|
98 | """ |
---|
99 | Tell the caller that we accept multiple data instances |
---|
100 | """ |
---|
101 | return True |
---|
102 | |
---|
103 | def setData(self, data_item=None): |
---|
104 | """ |
---|
105 | Assign new dataset to the fitting instance |
---|
106 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
107 | Pass it over to the calculator |
---|
108 | """ |
---|
109 | assert data_item is not None |
---|
110 | |
---|
111 | if not isinstance(data_item, list): |
---|
112 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
113 | raise AttributeError, msg |
---|
114 | |
---|
115 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
116 | msg = "Incorrect type passed to the Fitting Perspective" |
---|
117 | raise AttributeError, msg |
---|
118 | |
---|
119 | self._model_item = data_item[0] |
---|
120 | |
---|
121 | # Find the first unassigned tab. |
---|
122 | # If none, open a new tab. |
---|
123 | available_tabs = list(map(lambda tab: tab.acceptsData(), self.tabs)) |
---|
124 | |
---|
125 | if numpy.any(available_tabs): |
---|
126 | self.tabs[available_tabs.index(True)].data = data_item |
---|
127 | else: |
---|
128 | self.addFit(data_item) |
---|