source: sasview/src/sas/qtgui/Perspectives/Fitting/FittingPerspective.py @ 1bc27f1

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 1bc27f1 was 1bc27f1, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Code review fixes SASVIEW-588
Pylint related fixes in Perspectives/Fitting?

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