source: sasview/src/sas/qtgui/Perspectives/Fitting/ConstraintWidget.py @ be7c981

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since be7c981 was c4c4957, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 5 years ago

Batch results tabs now have meaningful names SASVIEW-1204

  • Property mode set to 100644
File size: 25.7 KB
Line 
1import logging
2import copy
3
4from twisted.internet import threads
5
6import sas.qtgui.Utilities.GuiUtils as GuiUtils
7import sas.qtgui.Utilities.LocalConfig as LocalConfig
8
9from PyQt5 import QtGui, QtCore, QtWidgets
10
11from sas.sascalc.fit.BumpsFitting import BumpsFit as Fit
12
13import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary
14from sas.qtgui.Perspectives.Fitting.UI.ConstraintWidgetUI import Ui_ConstraintWidgetUI
15from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget
16from sas.qtgui.Perspectives.Fitting.FitThread import FitThread
17from sas.qtgui.Perspectives.Fitting.ConsoleUpdate import ConsoleUpdate
18from sas.qtgui.Perspectives.Fitting.ComplexConstraint import ComplexConstraint
19from sas.qtgui.Perspectives.Fitting.Constraint import Constraint
20
21class ConstraintWidget(QtWidgets.QWidget, Ui_ConstraintWidgetUI):
22    """
23    Constraints Dialog to select the desired parameter/model constraints.
24    """
25
26    def __init__(self, parent=None):
27        super(ConstraintWidget, self).__init__()
28        self.parent = parent
29        self.setupUi(self)
30        self.currentType = "FitPage"
31        # Page id for fitting
32        # To keep with previous SasView values, use 300 as the start offset
33        self.page_id = 301
34
35        # Are we chain fitting?
36        self.is_chain_fitting = False
37
38        # Remember previous content of modified cell
39        self.current_cell = ""
40
41        # Tabs used in simultaneous fitting
42        # tab_name : True/False
43        self.tabs_for_fitting = {}
44
45        # Set up the widgets
46        self.initializeWidgets()
47
48        # Set up signals/slots
49        self.initializeSignals()
50
51        # Create the list of tabs
52        self.initializeFitList()
53
54    def acceptsData(self):
55        """ Tells the caller this widget doesn't accept data """
56        return False
57
58    def initializeWidgets(self):
59        """
60        Set up various widget states
61        """
62        labels = ['FitPage', 'Model', 'Data', 'Mnemonic']
63        # tab widget - headers
64        self.editable_tab_columns = [labels.index('Mnemonic')]
65        self.tblTabList.setColumnCount(len(labels))
66        self.tblTabList.setHorizontalHeaderLabels(labels)
67        self.tblTabList.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
68
69        self.tblTabList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
70        self.tblTabList.customContextMenuRequested.connect(self.showModelContextMenu)
71
72        # Single Fit is the default, so disable chainfit
73        self.chkChain.setVisible(False)
74
75        # disabled constraint
76        labels = ['Constraint']
77        self.tblConstraints.setColumnCount(len(labels))
78        self.tblConstraints.setHorizontalHeaderLabels(labels)
79        self.tblConstraints.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
80        self.tblConstraints.setEnabled(False)
81
82        self.tblConstraints.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
83        self.tblConstraints.customContextMenuRequested.connect(self.showConstrContextMenu)
84
85    def initializeSignals(self):
86        """
87        Set up signals/slots for this widget
88        """
89        # simple widgets
90        self.btnSingle.toggled.connect(self.onFitTypeChange)
91        self.btnBatch.toggled.connect(self.onFitTypeChange)
92        self.cbCases.currentIndexChanged.connect(self.onSpecialCaseChange)
93        self.cmdFit.clicked.connect(self.onFit)
94        self.cmdHelp.clicked.connect(self.onHelp)
95        self.chkChain.toggled.connect(self.onChainFit)
96
97        # QTableWidgets
98        self.tblTabList.cellChanged.connect(self.onTabCellEdit)
99        self.tblTabList.cellDoubleClicked.connect(self.onTabCellEntered)
100        self.tblConstraints.cellChanged.connect(self.onConstraintChange)
101
102        # External signals
103        self.parent.tabsModifiedSignal.connect(self.initializeFitList)
104
105    def updateSignalsFromTab(self, tab=None):
106        """
107        Intercept update signals from fitting tabs
108        """
109        if tab is None:
110            return
111        tab_object = ObjectLibrary.getObject(tab)
112
113        # Disconnect all local slots
114        #tab_object.disconnect()
115
116        # Reconnect tab signals to local slots
117        tab_object.constraintAddedSignal.connect(self.initializeFitList)
118        tab_object.newModelSignal.connect(self.initializeFitList)
119
120    def onFitTypeChange(self, checked):
121        """
122        Respond to the fit type change
123        single fit/batch fit
124        """
125        source = self.sender().objectName()
126        self.currentType = "BatchPage" if source == "btnBatch" else "FitPage"
127        self.chkChain.setVisible(source=="btnBatch")
128        self.initializeFitList()
129
130    def onSpecialCaseChange(self, index):
131        """
132        Respond to the combobox change for special case constraint sets
133        """
134        pass
135
136    def getTabsForFit(self):
137        """
138        Returns list of tab names selected for fitting
139        """
140        return [tab for tab in self.tabs_for_fitting if self.tabs_for_fitting[tab]]
141
142    def onChainFit(self, is_checked):
143        """
144        Respond to selecting the Chain Fit checkbox
145        """
146        self.is_chain_fitting = is_checked
147
148    def onFit(self):
149        """
150        Perform the constrained/simultaneous fit
151        """
152        # Find out all tabs to fit
153        tabs_to_fit = self.getTabsForFit()
154
155        # Single fitter for the simultaneous run
156        fitter = Fit()
157        fitter.fitter_id = self.page_id
158
159        # Notify the parent about fitting started
160        self.parent.fittingStartedSignal.emit(tabs_to_fit)
161
162        # prepare fitting problems for each tab
163        #
164        page_ids = []
165        fitter_id = 0
166        sim_fitter_list=[fitter]
167        # Prepare the fitter object
168        try:
169            for tab in tabs_to_fit:
170                tab_object = ObjectLibrary.getObject(tab)
171                if tab_object is None:
172                    # No such tab!
173                    return
174                sim_fitter_list, fitter_id = \
175                    tab_object.prepareFitters(fitter=sim_fitter_list[0], fit_id=fitter_id)
176                page_ids.append([tab_object.page_id])
177        except ValueError:
178            # No parameters selected in one of the tabs
179            no_params_msg = "Fitting can not be performed.\n" +\
180                            "Not all tabs chosen for fitting have parameters selected for fitting."
181            QtWidgets.QMessageBox.warning(self,
182                                          'Warning',
183                                           no_params_msg,
184                                           QtWidgets.QMessageBox.Ok)
185
186            return
187
188        # Create the fitting thread, based on the fitter
189        completefn = self.onBatchFitComplete if self.currentType=='BatchPage' else self.onFitComplete
190
191        if LocalConfig.USING_TWISTED:
192            handler = None
193            updater = None
194        else:
195            handler = ConsoleUpdate(parent=self.parent,
196                                    manager=self,
197                                    improvement_delta=0.1)
198            updater = handler.update_fit
199
200        batch_inputs = {}
201        batch_outputs = {}
202
203        # new fit thread object
204        calc_fit = FitThread(handler=handler,
205                             fn=sim_fitter_list,
206                             batch_inputs=batch_inputs,
207                             batch_outputs=batch_outputs,
208                             page_id=page_ids,
209                             updatefn=updater,
210                             completefn=completefn,
211                             reset_flag=self.is_chain_fitting)
212
213        if LocalConfig.USING_TWISTED:
214            # start the trhrhread with twisted
215            calc_thread = threads.deferToThread(calc_fit.compute)
216            calc_thread.addCallback(completefn)
217            calc_thread.addErrback(self.onFitFailed)
218        else:
219            # Use the old python threads + Queue
220            calc_fit.queue()
221            calc_fit.ready(2.5)
222
223
224        #disable the Fit button
225        self.cmdFit.setText('Running...')
226        self.parent.communicate.statusBarUpdateSignal.emit('Fitting started...')
227        self.cmdFit.setEnabled(False)
228
229    def onHelp(self):
230        """
231        Show the "Fitting" section of help
232        """
233        tree_location = "/user/qtgui/Perspectives/Fitting/"
234
235        helpfile = "fitting_help.html#simultaneous-fit-mode"
236        help_location = tree_location + helpfile
237
238        # OMG, really? Crawling up the object hierarchy...
239        self.parent.parent.showHelp(help_location)
240
241    def onTabCellEdit(self, row, column):
242        """
243        Respond to check/uncheck and to modify the model moniker actions
244        """
245        item = self.tblTabList.item(row, column)
246        if column == 0:
247            # Update the tabs for fitting list
248            tab_name = item.text()
249            self.tabs_for_fitting[tab_name] = (item.checkState() == QtCore.Qt.Checked)
250            # Enable fitting only when there are models to fit
251            self.cmdFit.setEnabled(any(self.tabs_for_fitting.values()))
252
253        if column not in self.editable_tab_columns:
254            return
255        new_moniker = item.data(0)
256
257        # The new name should be validated on the fly, with QValidator
258        # but let's just assure it post-factum
259        is_good_moniker = self.validateMoniker(new_moniker)
260        if not is_good_moniker:
261            self.tblTabList.blockSignals(True)
262            item.setBackground(QtCore.Qt.red)
263            self.tblTabList.blockSignals(False)
264            self.cmdFit.setEnabled(False)
265            return
266        self.tblTabList.blockSignals(True)
267        item.setBackground(QtCore.Qt.white)
268        self.tblTabList.blockSignals(False)
269        self.cmdFit.setEnabled(True)
270        if not self.current_cell:
271            return
272        # Remember the value
273        if self.current_cell not in self.available_tabs:
274            return
275        temp_tab = self.available_tabs[self.current_cell]
276        # Remove the key from the dictionaries
277        self.available_tabs.pop(self.current_cell, None)
278        # Change the model name
279        model = temp_tab.kernel_module
280        model.name = new_moniker
281        # Replace constraint name
282        temp_tab.replaceConstraintName(self.current_cell, new_moniker)
283        # Replace constraint name in the remaining tabs
284        for tab in self.available_tabs.values():
285            tab.replaceConstraintName(self.current_cell, new_moniker)
286        # Reinitialize the display
287        self.initializeFitList()
288
289    def onConstraintChange(self, row, column):
290        """
291        Modify the constraint's "active" instance variable.
292        """
293        item = self.tblConstraints.item(row, column)
294        if column == 0:
295            # Update the tabs for fitting list
296            constraint = self.available_constraints[row]
297            constraint.active = (item.checkState() == QtCore.Qt.Checked)
298
299    def onTabCellEntered(self, row, column):
300        """
301        Remember the original tab list cell data.
302        Needed for reverting back on bad validation
303        """
304        if column != 3:
305            return
306        self.current_cell = self.tblTabList.item(row, column).data(0)
307
308    def onFitComplete(self, result):
309        """
310        Respond to the successful fit complete signal
311        """
312        #re-enable the Fit button
313        self.cmdFit.setText("Fit")
314        self.cmdFit.setEnabled(True)
315
316        # Notify the parent about completed fitting
317        self.parent.fittingStoppedSignal.emit(self.getTabsForFit())
318
319        # Assure the fitting succeeded
320        if result is None or not result:
321            msg = "Fitting failed. Please ensure correctness of chosen constraints."
322            self.parent.communicate.statusBarUpdateSignal.emit(msg)
323            return
324
325        # get the elapsed time
326        elapsed = result[1]
327
328        # result list
329        results = result[0][0]
330
331        # Find out all tabs to fit
332        tabs_to_fit = [tab for tab in self.tabs_for_fitting if self.tabs_for_fitting[tab]]
333
334        # update all involved tabs
335        for i, tab in enumerate(tabs_to_fit):
336            tab_object = ObjectLibrary.getObject(tab)
337            if tab_object is None:
338                # No such tab. removed while job was running
339                return
340            # Make sure result and target objects are the same (same model moniker)
341            if tab_object.kernel_module.name == results[i].model.name:
342                tab_object.fitComplete(([[results[i]]], elapsed))
343
344        msg = "Fitting completed successfully in: %s s.\n" % GuiUtils.formatNumber(elapsed)
345        self.parent.communicate.statusBarUpdateSignal.emit(msg)
346
347    def onBatchFitComplete(self, result):
348        """
349        Respond to the successful batch fit complete signal
350        """
351        #re-enable the Fit button
352        self.cmdFit.setText("Fit")
353        self.cmdFit.setEnabled(True)
354
355        # Notify the parent about completed fitting
356        self.parent.fittingStoppedSignal.emit(self.getTabsForFit())
357
358        # get the elapsed time
359        elapsed = result[1]
360
361        if result is None:
362            msg = "Fitting failed."
363            self.parent.communicate.statusBarUpdateSignal.emit(msg)
364            return
365
366        # Show the grid panel
367        page_name = "ConstSimulPage"
368        results = copy.deepcopy(result[0])
369        results.append(page_name)
370        self.parent.communicate.sendDataToGridSignal.emit(results)
371
372        msg = "Fitting completed successfully in: %s s.\n" % GuiUtils.formatNumber(elapsed)
373        self.parent.communicate.statusBarUpdateSignal.emit(msg)
374
375    def onFitFailed(self, reason):
376        """
377        Respond to fitting failure.
378        """
379        #re-enable the Fit button
380        self.cmdFit.setText("Fit")
381        self.cmdFit.setEnabled(True)
382
383        # Notify the parent about completed fitting
384        self.parent.fittingStoppedSignal.emit(self.getTabsForFit())
385
386        msg = "Fitting failed: %s s.\n" % reason
387        self.parent.communicate.statusBarUpdateSignal.emit(msg)
388 
389    def isTabImportable(self, tab):
390        """
391        Determines if the tab can be imported and included in the widget
392        """
393        if not isinstance(tab, str): return False
394        if not self.currentType in tab: return False
395        object = ObjectLibrary.getObject(tab)
396        if not isinstance(object, FittingWidget): return False
397        if not object.data_is_loaded : return False
398        return True
399
400    def showModelContextMenu(self, position):
401        """
402        Show context specific menu in the tab table widget.
403        """
404        menu = QtWidgets.QMenu()
405        rows = [s.row() for s in self.tblTabList.selectionModel().selectedRows()]
406        num_rows = len(rows)
407        if num_rows <= 0:
408            return
409        # Select for fitting
410        param_string = "Fit Page " if num_rows==1 else "Fit Pages "
411
412        self.actionSelect = QtWidgets.QAction(self)
413        self.actionSelect.setObjectName("actionSelect")
414        self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting"))
415        # Unselect from fitting
416        self.actionDeselect = QtWidgets.QAction(self)
417        self.actionDeselect.setObjectName("actionDeselect")
418        self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting"))
419
420        self.actionRemoveConstraint = QtWidgets.QAction(self)
421        self.actionRemoveConstraint.setObjectName("actionRemoveConstrain")
422        self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove all constraints on selected models"))
423
424        self.actionMutualMultiConstrain = QtWidgets.QAction(self)
425        self.actionMutualMultiConstrain.setObjectName("actionMutualMultiConstrain")
426        self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of parameters in selected models..."))
427
428        menu.addAction(self.actionSelect)
429        menu.addAction(self.actionDeselect)
430        menu.addSeparator()
431
432        if num_rows >= 2:
433            menu.addAction(self.actionMutualMultiConstrain)
434
435        # Define the callbacks
436        self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint)
437        self.actionSelect.triggered.connect(self.selectModels)
438        self.actionDeselect.triggered.connect(self.deselectModels)
439        try:
440            menu.exec_(self.tblTabList.viewport().mapToGlobal(position))
441        except AttributeError as ex:
442            logging.error("Error generating context menu: %s" % ex)
443        return
444
445    def showConstrContextMenu(self, position):
446        """
447        Show context specific menu in the tab table widget.
448        """
449        menu = QtWidgets.QMenu()
450        rows = [s.row() for s in self.tblConstraints.selectionModel().selectedRows()]
451        num_rows = len(rows)
452        if num_rows <= 0:
453            return
454        # Select for fitting
455        param_string = "constraint " if num_rows==1 else "constraints "
456
457        self.actionSelect = QtWidgets.QAction(self)
458        self.actionSelect.setObjectName("actionSelect")
459        self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting"))
460        # Unselect from fitting
461        self.actionDeselect = QtWidgets.QAction(self)
462        self.actionDeselect.setObjectName("actionDeselect")
463        self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting"))
464
465        self.actionRemoveConstraint = QtWidgets.QAction(self)
466        self.actionRemoveConstraint.setObjectName("actionRemoveConstrain")
467        self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove "+param_string))
468
469        menu.addAction(self.actionSelect)
470        menu.addAction(self.actionDeselect)
471        menu.addSeparator()
472        menu.addAction(self.actionRemoveConstraint)
473
474        # Define the callbacks
475        self.actionRemoveConstraint.triggered.connect(self.deleteConstraint)
476        self.actionSelect.triggered.connect(self.selectConstraints)
477        self.actionDeselect.triggered.connect(self.deselectConstraints)
478        try:
479            menu.exec_(self.tblConstraints.viewport().mapToGlobal(position))
480        except AttributeError as ex:
481            logging.error("Error generating context menu: %s" % ex)
482        return
483
484    def selectConstraints(self):
485        """
486        Selected constraints are chosen for fitting
487        """
488        status = QtCore.Qt.Checked
489        self.setRowSelection(self.tblConstraints, status)
490
491    def deselectConstraints(self):
492        """
493        Selected constraints are removed for fitting
494        """
495        status = QtCore.Qt.Unchecked
496        self.setRowSelection(self.tblConstraints, status)
497
498    def selectModels(self):
499        """
500        Selected models are chosen for fitting
501        """
502        status = QtCore.Qt.Checked
503        self.setRowSelection(self.tblTabList, status)
504
505    def deselectModels(self):
506        """
507        Selected models are removed for fitting
508        """
509        status = QtCore.Qt.Unchecked
510        self.setRowSelection(self.tblTabList, status)
511
512    def selectedParameters(self, widget):
513        """ Returns list of selected (highlighted) parameters """
514        return [s.row() for s in widget.selectionModel().selectedRows()]
515
516    def setRowSelection(self, widget, status=QtCore.Qt.Unchecked):
517        """
518        Selected models are chosen for fitting
519        """
520        # Convert to proper indices and set requested enablement
521        for row in self.selectedParameters(widget):
522            widget.item(row, 0).setCheckState(status)
523
524    def deleteConstraint(self):#, row):
525        """
526        Delete all selected constraints.
527        """
528        # Removing rows from the table we're iterating over,
529        # so prepare a list of data first
530        constraints_to_delete = []
531        for row in self.selectedParameters(self.tblConstraints):
532            constraints_to_delete.append(self.tblConstraints.item(row, 0).data(0))
533        for constraint in constraints_to_delete:
534            moniker = constraint[:constraint.index(':')]
535            param = constraint[constraint.index(':')+1:constraint.index('=')].strip()
536            tab = self.available_tabs[moniker]
537            tab.deleteConstraintOnParameter(param)
538        # Constraints removed - refresh the table widget
539        self.initializeFitList()
540
541    def uneditableItem(self, data=""):
542        """
543        Returns an uneditable Table Widget Item
544        """
545        item = QtWidgets.QTableWidgetItem(data)
546        item.setFlags( QtCore.Qt.ItemIsSelectable |  QtCore.Qt.ItemIsEnabled )
547        return item
548
549    def updateFitLine(self, tab):
550        """
551        Update a single line of the table widget with tab info
552        """
553        fit_page = ObjectLibrary.getObject(tab)
554        model = fit_page.kernel_module
555        if model is None:
556            return
557        tab_name = tab
558        model_name = model.id
559        moniker = model.name
560        model_data = fit_page.data
561        model_filename = model_data.filename
562        self.available_tabs[moniker] = fit_page
563
564        # Update the model table widget
565        pos = self.tblTabList.rowCount()
566        self.tblTabList.insertRow(pos)
567        item = self.uneditableItem(tab_name)
568        item.setFlags(item.flags() ^ QtCore.Qt.ItemIsUserCheckable)
569        if tab_name in self.tabs_for_fitting:
570            state = QtCore.Qt.Checked if self.tabs_for_fitting[tab_name] else QtCore.Qt.Unchecked
571            item.setCheckState(state)
572        else:
573            item.setCheckState(QtCore.Qt.Checked)
574            self.tabs_for_fitting[tab_name] = True
575
576        # Disable signals so we don't get infinite call recursion
577        self.tblTabList.blockSignals(True)
578        self.tblTabList.setItem(pos, 0, item)
579        self.tblTabList.setItem(pos, 1, self.uneditableItem(model_name))
580        self.tblTabList.setItem(pos, 2, self.uneditableItem(model_filename))
581        # Moniker is editable, so no option change
582        item = QtWidgets.QTableWidgetItem(moniker)
583        self.tblTabList.setItem(pos, 3, item)
584        self.tblTabList.blockSignals(False)
585
586        # Check if any constraints present in tab
587        constraint_names = fit_page.getComplexConstraintsForModel()
588        constraints = fit_page.getConstraintObjectsForModel()
589        if not constraints: 
590            return
591        self.tblConstraints.setEnabled(True)
592        self.tblConstraints.blockSignals(True)
593        for constraint, constraint_name in zip(constraints, constraint_names):
594            # Create the text for widget item
595            label = moniker + ":"+ constraint_name[0] + " = " + constraint_name[1]
596            pos = self.tblConstraints.rowCount()
597            self.available_constraints[pos] = constraint
598
599            # Show the text in the constraint table
600            item = self.uneditableItem(label)
601            item.setFlags(item.flags() ^ QtCore.Qt.ItemIsUserCheckable)
602            item.setCheckState(QtCore.Qt.Checked)
603            self.tblConstraints.insertRow(pos)
604            self.tblConstraints.setItem(pos, 0, item)
605        self.tblConstraints.blockSignals(False)
606
607    def initializeFitList(self):
608        """
609        Fill the list of model/data sets for fitting/constraining
610        """
611        # look at the object library to find all fit tabs
612        # Show the content of the current "model"
613        objects = ObjectLibrary.listObjects()
614
615        # Tab dict
616        # moniker -> (kernel_module, data)
617        self.available_tabs = {}
618        # Constraint dict
619        # moniker -> [constraints]
620        self.available_constraints = {}
621
622        # Reset the table widgets
623        self.tblTabList.setRowCount(0)
624        self.tblConstraints.setRowCount(0)
625
626        # Fit disabled
627        self.cmdFit.setEnabled(False)
628
629        if not objects:
630            return
631
632        tabs = [tab for tab in ObjectLibrary.listObjects() if self.isTabImportable(tab)]
633        for tab in tabs:
634            self.updateFitLine(tab)
635            self.updateSignalsFromTab(tab)
636            # We have at least 1 fit page, allow fitting
637            self.cmdFit.setEnabled(True)
638
639    def validateMoniker(self, new_moniker=None):
640        """
641        Check new_moniker for correctness.
642        It must be non-empty.
643        It must not be the same as other monikers.
644        """
645        if not new_moniker:
646            return False
647
648        for existing_moniker in self.available_tabs:
649            if existing_moniker == new_moniker and existing_moniker != self.current_cell:
650                return False
651
652        return True
653
654    def getObjectByName(self, name):
655        """
656        Given name of the fit, returns associated fit object
657        """
658        for object_name in ObjectLibrary.listObjects():
659            object = ObjectLibrary.getObject(object_name)
660            if isinstance(object, FittingWidget):
661                try:
662                    if object.kernel_module.name == name:
663                        return object
664                except AttributeError:
665                    # Disregard atribute errors - empty fit widgets
666                    continue
667        return None
668
669    def showMultiConstraint(self):
670        """
671        Invoke the complex constraint editor
672        """
673        selected_rows = self.selectedParameters(self.tblTabList)
674        assert(len(selected_rows)==2)
675
676        tab_list = [ObjectLibrary.getObject(self.tblTabList.item(s, 0).data(0)) for s in selected_rows]
677        # Create and display the widget for param1 and param2
678        cc_widget = ComplexConstraint(self, tabs=tab_list)
679        if cc_widget.exec_() != QtWidgets.QDialog.Accepted:
680            return
681
682        constraint = Constraint()
683        model1, param1, operator, constraint_text = cc_widget.constraint()
684
685        constraint.func = constraint_text
686        # param1 is the parameter we're constraining
687        constraint.param = param1
688
689        # Find the right tab
690        constrained_tab = self.getObjectByName(model1)
691        if constrained_tab is None:
692            return
693
694        # Find the constrained parameter row
695        constrained_row = constrained_tab.getRowFromName(param1)
696
697        # Update the tab
698        constrained_tab.addConstraintToRow(constraint, constrained_row)
Note: See TracBrowser for help on using the repository browser.