1 | import sys |
---|
2 | import numpy |
---|
3 | |
---|
4 | from PyQt4 import QtCore |
---|
5 | from PyQt4 import QtGui |
---|
6 | |
---|
7 | from FittingWidget import FittingWidget |
---|
8 | |
---|
9 | class FittingWindow(QtGui.QTabWidget): |
---|
10 | """ |
---|
11 | """ |
---|
12 | name = "Fitting" # For displaying in the combo box in DataExplorer |
---|
13 | def __init__(self, manager=None, parent=None, data=None): |
---|
14 | super(FittingWindow, self).__init__() |
---|
15 | |
---|
16 | self.manager = manager |
---|
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 = 'DREAM' |
---|
31 | |
---|
32 | # The tabs need to be closeable |
---|
33 | self.setTabsClosable(True) |
---|
34 | |
---|
35 | # Initialize the first tab |
---|
36 | self.addFit(None) |
---|
37 | |
---|
38 | # Deal with signals |
---|
39 | self.tabCloseRequested.connect(self.tabCloses) |
---|
40 | |
---|
41 | self.setWindowTitle('Fit panel - Active Fitting Optimizer: %s' % self.optimizer) |
---|
42 | |
---|
43 | def addFit(self, data): |
---|
44 | """ |
---|
45 | Add a new tab for passed data |
---|
46 | """ |
---|
47 | tab = FittingWidget(manager=self.manager, parent=self.parent, data=data) |
---|
48 | self.tabs.append(tab) |
---|
49 | self.maxIndex += 1 |
---|
50 | self.addTab(tab, self.tabName()) |
---|
51 | |
---|
52 | def tabName(self): |
---|
53 | """ |
---|
54 | Get the new tab name, based on the number of fitting tabs so far |
---|
55 | """ |
---|
56 | page_name = "FitPage" + str(self.maxIndex) |
---|
57 | return page_name |
---|
58 | |
---|
59 | def tabCloses(self, index): |
---|
60 | """ |
---|
61 | Update local bookkeeping on tab close |
---|
62 | """ |
---|
63 | assert(len(self.tabs) >= index) |
---|
64 | # don't remove the last tab |
---|
65 | if len(self.tabs) <= 1: |
---|
66 | return |
---|
67 | del self.tabs[index] |
---|
68 | self.removeTab(index) |
---|
69 | |
---|
70 | def allowBatch(self): |
---|
71 | """ |
---|
72 | Tell the caller that we accept multiple data instances |
---|
73 | """ |
---|
74 | return True |
---|
75 | |
---|
76 | def setData(self, data_item=None): |
---|
77 | """ |
---|
78 | Assign new dataset to the fitting instance |
---|
79 | """ |
---|
80 | assert(data_item is not None) |
---|
81 | |
---|
82 | # Find an unassigned tab. |
---|
83 | # If none, open a new tab. |
---|
84 | available_tabs = list(map(lambda tab:tab.acceptsData(), self.tabs)) |
---|
85 | |
---|
86 | if numpy.any(available_tabs): |
---|
87 | self.tabs[available_tabs.index(True)].data = data_item |
---|
88 | else: |
---|
89 | self.addFit(data_item) |
---|
90 | |
---|
91 | |
---|
92 | if __name__ == "__main__": |
---|
93 | app = QtGui.QApplication([]) |
---|
94 | dlg = FittingWindow() |
---|
95 | dlg.show() |
---|
96 | sys.exit(app.exec_()) |
---|