Ignore:
Timestamp:
Jun 15, 2017 6:24:49 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
72f4834
Parents:
144ec831
Message:

Unit tests for fitting options dialog SASVIEW-514

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Fitting/FittingOptions.py

    r2d0e0c1 rb0c5e8c  
    22import sys 
    33import os 
     4import types 
     5 
    46from PyQt4 import QtCore 
    57from PyQt4 import QtGui 
     
    810from sas.qtgui.UI import images_rc 
    911from sas.qtgui.UI import main_resources_rc 
    10 #from bumps.fitters import FITTERS, FIT_AVAILABLE_IDS, FIT_ACTIVE_IDS, FIT_DEFAULT_ID 
     12import sas.qtgui.Utilities.GuiUtils as GuiUtils 
     13 
    1114from bumps import fitters 
    1215import bumps.options 
     
    3134        settings = [('steps', 1000), ('starts', 1), ('radius', 0.15), ('xtol', 1e-6), ('ftol', 1e-8)] 
    3235    """ 
    33     fit_option_changed = QtCore.pyqtSignal(str, dict) 
     36    fit_option_changed = QtCore.pyqtSignal(str) 
    3437 
    3538    def __init__(self, parent=None, config=None): 
     
    4952        # Handle the Apply button click 
    5053        self.buttonBox.button(QtGui.QDialogButtonBox.Ok).clicked.connect(self.onApply) 
     54        # handle the Help button click 
     55        self.buttonBox.button(QtGui.QDialogButtonBox.Help).clicked.connect(self.onHelp) 
    5156 
    5257        # Handle the combo box changes 
     
    5762        default_index = self.cbAlgorithm.findText(default_name) 
    5863        self.cbAlgorithm.setCurrentIndex(default_index) 
     64 
     65        # Assign appropriate validators 
     66        self.assignValidators() 
     67 
    5968        self.current_fitter_id = fitters.FIT_DEFAULT_ID 
    6069 
    61         # Fill in options for all active fitters 
    62         #[self.updateWidgetFromBumps(fitter_id) for fitter_id in fitters.FIT_ACTIVE_IDS] 
     70        # Display HTML content 
     71        self.helpView = QtWebKit.QWebView() 
     72 
     73    def assignValidators(self): 
     74        """ 
     75        Use options.FIT_FIELDS to assert which line edit gets what validator 
     76        """ 
     77        for option in bumps.options.FIT_FIELDS.iterkeys(): 
     78            (f_name, f_type) = bumps.options.FIT_FIELDS[option] 
     79            validator = None 
     80            if type(f_type) == types.FunctionType: 
     81                validator = QtGui.QIntValidator() 
     82            elif f_type == types.FloatType: 
     83                validator = QtGui.QDoubleValidator() 
     84            else: 
     85                continue 
     86            for fitter_id in fitters.FIT_ACTIVE_IDS: 
     87                line_edit = self.widgetFromOption(str(option), current_fitter=str(fitter_id)) 
     88                if hasattr(line_edit, 'setValidator') and validator is not None: 
     89                    line_edit.setValidator(validator) 
    6390 
    6491    def onAlgorithmChange(self, index): 
     
    75102        # Convert the name into widget instance 
    76103        widget_to_activate = eval(widget_name) 
     104        index_for_this_id = self.stackedWidget.indexOf(widget_to_activate) 
    77105 
    78106        # Select the requested widget 
    79         self.stackedWidget.setCurrentIndex(self.stackedWidget.indexOf(widget_to_activate)) 
     107        self.stackedWidget.setCurrentIndex(index_for_this_id) 
    80108 
    81109        self.updateWidgetFromBumps(self.current_fitter_id) 
    82110 
     111        self.assignValidators() 
     112 
    83113    def onApply(self): 
    84114        """ 
    85         Update the fitter object in perspective 
     115        Update the fitter object 
    86116        """ 
    87         self.fit_option_changed.emit(self.cbAlgorithm.currentText(), {}) 
     117        # Notify the perspective, so the window title is updated 
     118        self.fit_option_changed.emit(self.cbAlgorithm.currentText()) 
    88119 
    89         # or just do it locally 
    90         for option in self.config.values[self.current_fitter_id].iterkeys(): 
     120        def bumpsUpdate(option): 
     121            """ 
     122            Utility method for bumps state update 
     123            """ 
    91124            widget = self.widgetFromOption(option) 
    92             new_value = "" 
    93             if isinstance(widget, QtGui.QComboBox): 
    94                 new_value = widget.currentText() 
    95             else: 
    96                 new_value = float(widget.text()) 
     125            new_value = widget.currentText() if isinstance(widget, QtGui.QComboBox) \ 
     126                else float(widget.text()) 
    97127            self.config.values[self.current_fitter_id][option] = new_value 
    98128 
    99     def widgetFromOption(self, option_id): 
     129        # Update the BUMPS singleton 
     130        [bumpsUpdate(o) for o in self.config.values[self.current_fitter_id].iterkeys()] 
     131 
     132    def onHelp(self): 
     133        """ 
     134        Show the "Fitting options" section of help 
     135        """ 
     136        tree_location = GuiUtils.HELP_DIRECTORY_LOCATION + "/user/sasgui/perspectives/fitting/" 
     137 
     138        # Actual file anchor will depend on the combo box index 
     139        # Note that we can be clusmy here, since bad current_fitter_id 
     140        # will just make the page displayed from the top 
     141        helpfile = "optimizer.html#fit-" + self.current_fitter_id  
     142        help_location = tree_location + helpfile 
     143        self.helpView.load(QtCore.QUrl(help_location)) 
     144        self.helpView.show() 
     145 
     146    def widgetFromOption(self, option_id, current_fitter=None): 
    100147        """ 
    101148        returns widget's element linked to the given option_id 
    102149        """ 
    103         return eval('self.' + option_id + '_' + self.current_fitter_id) 
     150        if current_fitter is None: 
     151            current_fitter = self.current_fitter_id 
     152        if option_id not in bumps.options.FIT_FIELDS.keys(): return None 
     153        option = option_id + '_' + current_fitter 
     154        if not hasattr(self, option): return None 
     155        return eval('self.' + option) 
    104156 
    105157    def getResults(self): 
     
    127179 
    128180        pass 
    129  
    130     def updateBumpsOptions(self, optimizer_id): 
    131         """ 
    132         Given the ID of the optimizer, gather widget's values 
    133         and update the bumps options 
    134         """ 
    135         pass 
    136  
Note: See TracChangeset for help on using the changeset viewer.