Ignore:
Timestamp:
Apr 25, 2018 4:32:11 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:
27689dc
Parents:
01ef3f7
Message:

Code review changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Utilities/AddMultEditor.py

    r01ef3f7 r3b8cc00  
    4545       (add or multiply) the resulting model will add a scale parameter and a 
    4646       background parameter. 
    47        The user also gives a brief help for the model in a description box and 
    48        must provide a unique name which is verified as unique before the new 
     47       The user can also give a brief help for the model in the description box and 
     48       must provide a unique name which is verified before the new model is saved. 
    4949    """ 
    5050    def __init__(self, parent=None): 
    51         super(AddMultEditor, self).__init__() 
     51        super(AddMultEditor, self).__init__(parent._parent) 
    5252 
    5353        self.parent = parent 
     
    5757        #  uncheck self.chkOverwrite 
    5858        self.chkOverwrite.setChecked(False) 
     59        self.canOverwriteName = False 
    5960 
    6061        # Disabled Apply button until input of valid output plugin name 
     
    117118        # or when overwriting not allowed 
    118119        self.txtName.editingFinished.connect(self.onNameCheck) 
    119         self.chkOverwrite.stateChanged.connect(self.onNameCheck) 
     120        self.chkOverwrite.stateChanged.connect(self.onOverwrite) 
    120121 
    121122        self.buttonBox.button(QtWidgets.QDialogButtonBox.Apply).clicked.connect(self.onApply) 
     
    126127        self.cbOperator.currentIndexChanged.connect(self.onOperatorChange) 
    127128 
     129    def onOverwrite(self): 
     130        """ 
     131        Modify state on checkbox change 
     132        """ 
     133        self.canOverwriteName = self.chkOverwrite.isChecked() 
     134 
    128135    def onNameCheck(self): 
    129136        """ 
     
    138145        filename = title + '.py' 
    139146 
    140         if self.chkOverwrite.isChecked(): 
     147        if self.canOverwriteName: 
    141148            # allow overwriting -> only valid name needs to be checked 
    142149            # (done with validator in __init__ above) 
     
    170177            QtWidgets.QDialogButtonBox.Apply).setEnabled(self.good_name) 
    171178 
    172         return self.good_name 
    173  
    174179    def onOperatorChange(self, index): 
    175180        """ Respond to operator combo box changes """ 
     
    182187        """ Validity check, save model to file """ 
    183188 
    184         # if name OK write file and test 
    185         self.buttonBox.button( 
    186             QtWidgets.QDialogButtonBox.Apply).setEnabled(False) 
     189        # Set the button enablement, so no double clicks can be made 
     190        self.buttonBox.button(QtWidgets.QDialogButtonBox.Apply).setEnabled(False) 
     191 
     192        # Check the name/overwrite combination again, in case we managed to confuse the UI 
     193        self.onNameCheck() 
     194        if not self.good_name: 
     195            return 
    187196 
    188197        self.write_new_model_to_file(self.plugin_filename, 
     
    191200                                     self.cbOperator.currentText()) 
    192201 
    193         success = self.checkModel(self.plugin_filename) 
    194  
    195         if success: 
    196             # Update list of models in FittingWidget and AddMultEditor 
    197             self.parent.communicate.customModelDirectoryChanged.emit() 
    198             self.updateModels() 
     202        success = GuiUtils.checkModel(self.plugin_filename) 
     203 
     204        if not success: 
     205            return 
     206 
     207        # Update list of models in FittingWidget and AddMultEditor 
     208        self.parent.communicate.customModelDirectoryChanged.emit() 
     209        # Re-read the model list so the new model is included 
     210        self.list_models = self.readModels() 
     211        self.updateModels() 
     212 
     213        # Notify the user 
     214        title = self.txtName.text().lstrip().rstrip() 
     215        msg = "Custom model "+title + " successfully created." 
     216        self.parent.communicate.statusBarUpdateSignal.emit(msg) 
     217        logging.info(msg) 
     218 
    199219 
    200220    def write_new_model_to_file(self, fname, model1_name, model2_name, operator): 
     
    221241            out_f.write(output) 
    222242 
    223     # same version as in TabbedModelEditor 
    224     @classmethod 
    225     def checkModel(cls, path): 
    226         """ Check that the model saved in file 'path' can run. """ 
    227  
    228         # try running the model 
    229         from sasmodels.sasview_model import load_custom_model 
    230         Model = load_custom_model(path) 
    231         model = Model() 
    232         q = np.array([0.01, 0.1]) 
    233         _ = model.evalDistribution(q) 
    234         qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) 
    235         _ = model.evalDistribution([qx, qy]) 
    236         # check the model's unit tests run 
    237         from sasmodels.model_test import run_one 
    238         # TODO see comments in TabbedModelEditor 
    239         # result = run_one(path) 
    240         result = True  # to be commented out 
    241         return result 
    242  
    243243    def updateModels(self): 
    244244        """ Update contents of comboboxes with new plugin models """ 
     245 
     246        # Keep pointers to the current indices so we can show the comboboxes with 
     247        # original selection 
     248        model_1 = self.cbModel1.currentText() 
     249        model_2 = self.cbModel2.currentText() 
    245250 
    246251        self.cbModel1.blockSignals(True) 
     
    257262        self.cbModel2.addItems(model_list) 
    258263 
     264        # Scroll back to the user chosen models 
     265        self.cbModel1.setCurrentIndex(self.cbModel1.findText(model_1)) 
     266        self.cbModel2.setCurrentIndex(self.cbModel2.findText(model_2)) 
     267 
    259268    def onHelp(self): 
    260269        """ Display related help section """ 
Note: See TracChangeset for help on using the changeset viewer.