- Timestamp:
- Dec 24, 2017 10:42:41 AM (7 years ago)
- 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:
- 2109350
- Parents:
- 378e808
- Location:
- src/sas/qtgui
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/GUITests.py
r0261bc1 r6b0c2f6 56 56 from Perspectives.Fitting.UnitTesting import FitPageTest 57 57 from Perspectives.Fitting.UnitTesting import FittingOptionsTest 58 from Perspectives.Fitting.UnitTesting import MultiConstraintTest 59 58 60 # Invariant 59 61 from Perspectives.Invariant.UnitTesting import InvariantPerspectiveTest 60 from Perspectives.Invariant.UnitTesting import InvariantDetailsTest61 62 62 63 … … 112 113 unittest.makeSuite(FitPageTest.FitPageTest, 'test'), 113 114 unittest.makeSuite(FittingOptionsTest.FittingOptionsTest, 'test'), 115 unittest.makeSuite(MultiConstraintTest.MultiConstraintTest, 'test'), 114 116 # Invariant 115 117 unittest.makeSuite(InvariantPerspectiveTest.InvariantPerspectiveTest, 'test'), -
src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
r378e808 r6b0c2f6 84 84 Main widget for selecting form and structure factor models 85 85 """ 86 constraintAddedSignal = QtCore.pyqtSignal(list) 86 87 def __init__(self, parent=None, data=None, tab_id=1): 87 88 … … 456 457 # Respond to change in parameters from the UI 457 458 self._model_model.itemChanged.connect(self.onMainParamsChange) 459 self.constraintAddedSignal.connect(self.modifyViewOnConstraint) 458 460 self._poly_model.itemChanged.connect(self.onPolyModelChange) 459 461 self._magnet_model.itemChanged.connect(self.onMagnetModelChange) … … 510 512 511 513 # Define the callbacks 512 self.actionConstrain.triggered.connect(self.add Constraint)513 self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstrain )514 self.actionConstrain.triggered.connect(self.addSimpleConstraint) 515 self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint) 514 516 self.actionSelect.triggered.connect(self.selectParameters) 515 517 self.actionDeselect.triggered.connect(self.deselectParameters) 516 518 return menu 517 519 518 def showMultiConstrain (self):520 def showMultiConstraint(self): 519 521 """ 520 522 Show the constraint widget and receive the expression 521 523 """ 522 524 from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint 523 params_list = [s.data() for s in self.lstParams.selectionModel().selectedRows()] 525 from .Constraints import Constraint 526 selected_rows = self.lstParams.selectionModel().selectedRows() 527 528 params_list = [s.data() for s in selected_rows] 524 529 mc_widget = MultiConstraint(self, params=params_list) 525 530 mc_widget.exec_() 526 constraint = mc_widget.txtConstraint.text() 531 constraint = Constraint() 532 c_text = mc_widget.txtConstraint.text() 527 533 # Pass the constraint to the parser 534 528 535 self.communicate.statusBarUpdateSignal.emit('Constraints added') 536 # Change the colour of the row 529 537 pass 530 538 531 def addConstraint(self): 539 def modifyViewOnConstraint(self, row): 540 """ 541 Add visual cues that the parameter is constrained 542 """ 543 value = self._model_model.item(row, 1).text() 544 # Set min/max to the value constrained 545 self._model_model.item(row,2).setText(value) 546 self._model_model.item(row,3).setText(value) 547 font = QtGui.QFont() 548 font.setItalic(True) 549 brush = QtGui.QBrush(QtGui.QColor('blue')) 550 self._model_model.blockSignals(True) 551 # Modify font and foreground of affected rows 552 for column in range(0, self._model_model.columnCount()): 553 self._model_model.item(row, column).setForeground(brush) 554 self._model_model.item(row, column).setFont(font) 555 self._model_model.item(row, column).setEditable(False) 556 self._model_model.blockSignals(False) 557 558 def addSimpleConstraint(self): 532 559 """ 533 560 Adds a constraint on a single parameter. 534 561 """ 562 from .Constraints import Constraint 563 for row in self.selectedParameters(): 564 param = self._model_model.item(row, 0).text() 565 value = self._model_model.item(row, 1).text() 566 constraint = Constraint(param=param, value=value) 567 item = QtGui.QStandardItem() 568 item.setData(constraint) 569 self._model_model.item(row, 1).setChild(0, item) 570 #self.constraintAddedSignal.emit([row]) 571 self.modifyViewOnConstraint(row) 535 572 self.communicate.statusBarUpdateSignal.emit('Constraint added') 536 573 pass -
src/sas/qtgui/Perspectives/Fitting/MultiConstraint.py
- Property mode changed from 100755 to 100644
r378e808 r6b0c2f6 2 2 Widget for parameter constraints. 3 3 """ 4 from numpy import * 5 4 6 from PyQt5 import QtCore 5 7 from PyQt5 import QtGui … … 23 25 self.setupTooltip() 24 26 27 # Set param text control to the second parameter passed 28 self.txtConstraint.setText(self.params[1]) 29 25 30 self.cmdOK.clicked.connect(self.accept) 26 31 self.cmdRevert.clicked.connect(self.revert) 32 self.txtConstraint.editingFinished.connect(self.validateFormula) 27 33 28 34 def revert(self): … … 52 58 self.txtConstraint.setToolTip(tooltip) 53 59 54 pass 60 def validateFormula(self): 61 """ 62 Add visual cues when formula is incorrect 63 """ 64 formula_is_valid = False 65 formula_is_valid = self.validateConstraint(self.txtConstraint.text()) 66 if not formula_is_valid: 67 self.cmdOK.setEnabled(False) 68 self.txtConstraint.setStyleSheet("QLineEdit {background-color: red;}") 69 else: 70 self.cmdOK.setEnabled(True) 71 self.txtConstraint.setStyleSheet("QLineEdit {background-color: white;}") 55 72 73 def validateConstraint(self, constraint_text): 74 """ 75 Ensure the constraint has proper form 76 """ 77 # 0. none or empty 78 if not constraint_text or not isinstance(constraint_text, str): 79 return False 80 81 param_str = str(self.params[1]) 82 constraint_text = constraint_text.strip() 83 84 # 1. just the parameter 85 if param_str == constraint_text: 86 return True 87 88 # 2. ensure the text contains parameter name 89 parameter_string_start = constraint_text.find(param_str) 90 has_parameter_name = (parameter_string_start > -1) 91 if not has_parameter_name: 92 return False 93 parameter_string_end = parameter_string_start + len(param_str) 94 95 # 3. parameter name should be a separate word, but can have "()[]*+-/" around 96 valid_neighbours = "()[]*+-/ " 97 has_only_parameter = False 98 start_loc = parameter_string_start -1 99 end_loc = parameter_string_end 100 if not any([constraint_text[start_loc] == char for char in valid_neighbours]): 101 return False 102 if end_loc < len(constraint_text): 103 if not any([constraint_text[end_loc] == char for char in valid_neighbours]): 104 return False 105 106 # 4. replace parameter name with "1" and try to evaluate the expression 107 try: 108 expression_to_evaluate = constraint_text.replace(param_str, "1.0") 109 eval(expression_to_evaluate) 110 except Exception: 111 # Too many cases to cover individually, just a blanket 112 # Exception should be sufficient 113 # Note that in current numpy things like sqrt(-1) don't 114 # raise but just return warnings 115 return False 116 117 return True 118 119 120
Note: See TracChangeset
for help on using the changeset viewer.