source: sasview/src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py @ 2add354

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 2add354 was 2add354, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Code review fixes for SASVIEW-273

  • Property mode set to 100644
File size: 3.9 KB
Line 
1import sys
2import numpy
3
4from PyQt4 import QtCore
5from PyQt4 import QtGui
6
7import sas.qtgui.Utilities.GuiUtils as GuiUtils
8import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary
9
10from FittingWidget import FittingWidget
11#from FitPage import FitPage
12
13class 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        # Invoke fit page events
64        for tab in self.tabs:
65            tab.close()
66        if self._allow_close:
67            # reset the closability flag
68            self.setClosable(value=False)
69            event.accept()
70        else:
71            # Maybe we should just minimize
72            self.setWindowState(QtCore.Qt.WindowMinimized)
73            event.ignore()
74
75    def addFit(self, data):
76        """
77        Add a new tab for passed data
78        """
79        tab     = FittingWidget(parent=self.parent, data=data, id=self.maxIndex+1)
80        # Add this tab to the object library so it can be retrieved by scripting/jupyter
81        ObjectLibrary.addObject(self.tabName(), tab)
82        self.tabs.append(tab)
83        self.maxIndex += 1
84        self.addTab(tab, self.tabName())
85
86    def tabName(self):
87        """
88        Get the new tab name, based on the number of fitting tabs so far
89        """
90        page_name = "FitPage" + str(self.maxIndex)
91        return page_name
92
93    def tabCloses(self, index):
94        """
95        Update local bookkeeping on tab close
96        """
97        assert len(self.tabs) >= index
98        # don't remove the last tab
99        if len(self.tabs) <= 1:
100            return
101        ObjectLibrary.deleteObjectByRef(self.tabs[index])
102        del self.tabs[index]
103        self.removeTab(index)
104
105    def allowBatch(self):
106        """
107        Tell the caller that we accept multiple data instances
108        """
109        return True
110
111    def setData(self, data_item=None):
112        """
113        Assign new dataset to the fitting instance
114        Obtain a QStandardItem object and dissect it to get Data1D/2D
115        Pass it over to the calculator
116        """
117        assert data_item is not None
118
119        if not isinstance(data_item, list):
120            msg = "Incorrect type passed to the Fitting Perspective"
121            raise AttributeError, msg
122
123        if not isinstance(data_item[0], QtGui.QStandardItem):
124            msg = "Incorrect type passed to the Fitting Perspective"
125            raise AttributeError, msg
126
127        for data in data_item:
128            # Find the first unassigned tab.
129            # If none, open a new tab.
130            available_tabs = list(map(lambda tab: tab.acceptsData(), self.tabs))
131
132            if numpy.any(available_tabs):
133                self.tabs[available_tabs.index(True)].data = data
134            else:
135                self.addFit(data)
Note: See TracBrowser for help on using the repository browser.