- Timestamp:
- Jan 9, 2018 3:44:55 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:
- a3c94b54
- Parents:
- 6b0c2f6
- Location:
- src/sas/qtgui/Perspectives/Fitting
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Perspectives/Fitting/FittingWidget.py
r6b0c2f6 r2109350 45 45 from sas.qtgui.Perspectives.Fitting.ViewDelegate import PolyViewDelegate 46 46 from sas.qtgui.Perspectives.Fitting.ViewDelegate import MagnetismViewDelegate 47 from sas.qtgui.Perspectives.Fitting.Constraints import Constraint 48 from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint 47 49 48 50 … … 84 86 Main widget for selecting form and structure factor models 85 87 """ 86 constraintAddedSignal = QtCore.pyqtSignal(list)88 #constraintAddedSignal = QtCore.pyqtSignal(list) 87 89 def __init__(self, parent=None, data=None, tab_id=1): 88 90 … … 457 459 # Respond to change in parameters from the UI 458 460 self._model_model.itemChanged.connect(self.onMainParamsChange) 459 self.constraintAddedSignal.connect(self.modifyViewOnConstraint)461 #self.constraintAddedSignal.connect(self.modifyViewOnConstraint) 460 462 self._poly_model.itemChanged.connect(self.onPolyModelChange) 461 463 self._magnet_model.itemChanged.connect(self.onMagnetModelChange) … … 466 468 def showModelContextMenu(self, position): 467 469 468 rows = len([s.row() for s in self.lstParams.selectionModel().selectedRows()]) 470 #rows = len([s.row() for s in self.lstParams.selectionModel().selectedRows()]) 471 rows = [s.row() for s in self.lstParams.selectionModel().selectedRows()] 469 472 menu = self.showModelDescription() if not rows else self.modelContextMenu(rows) 470 473 try: … … 475 478 476 479 def modelContextMenu(self, rows): 480 """ 481 Create context menu for the given model 482 """ 477 483 menu = QtWidgets.QMenu() 478 484 num_rows = len(rows) 479 485 # Select for fitting 486 param_string = "parameter " if num_rows==1 else "parameters " 487 to_string = "to its current value" if num_rows==1 else "to their current values" 488 480 489 self.actionSelect = QtWidgets.QAction(self) 481 490 self.actionSelect.setObjectName("actionSelect") 482 self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select parameterfor fitting"))491 self.actionSelect.setText(QtCore.QCoreApplication.translate("self", "Select "+param_string+" for fitting")) 483 492 # Unselect from fitting 484 493 self.actionDeselect = QtWidgets.QAction(self) 485 494 self.actionDeselect.setObjectName("actionDeselect") 486 self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select parameterfrom fitting"))495 self.actionDeselect.setText(QtCore.QCoreApplication.translate("self", "De-select "+param_string+" from fitting")) 487 496 488 497 self.actionConstrain = QtWidgets.QAction(self) 489 498 self.actionConstrain.setObjectName("actionConstrain") 490 self.actionConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain parameter to current value")) 499 self.actionConstrain.setText(QtCore.QCoreApplication.translate("self", "Constrain "+param_string + to_string)) 500 501 self.actionRemoveConstraint = QtWidgets.QAction(self) 502 self.actionRemoveConstraint.setObjectName("actionRemoveConstrain") 503 self.actionRemoveConstraint.setText(QtCore.QCoreApplication.translate("self", "Remove constraint")) 491 504 492 505 self.actionMultiConstrain = QtWidgets.QAction(self) … … 498 511 self.actionMutualMultiConstrain.setText(QtCore.QCoreApplication.translate("self", "Mutual constrain of selected parameters...")) 499 512 500 #action.setDefaultWidget(label)501 513 menu.addAction(self.actionSelect) 502 514 menu.addAction(self.actionDeselect) 503 515 menu.addSeparator() 504 516 505 if rows == 1: 517 if self.rowHasConstraint(rows[0]): 518 menu.addAction(self.actionRemoveConstraint) 519 else: 506 520 menu.addAction(self.actionConstrain) 507 elif rows == 2: 508 menu.addAction(self.actionMultiConstrain) 521 if num_rows == 2: 509 522 menu.addAction(self.actionMutualMultiConstrain) 510 elif rows > 2:511 menu.addAction(self.actionMultiConstrain)512 523 513 524 # Define the callbacks 514 525 self.actionConstrain.triggered.connect(self.addSimpleConstraint) 526 self.actionRemoveConstraint.triggered.connect(self.deleteConstraint) 515 527 self.actionMutualMultiConstrain.triggered.connect(self.showMultiConstraint) 516 528 self.actionSelect.triggered.connect(self.selectParameters) … … 522 534 Show the constraint widget and receive the expression 523 535 """ 524 from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint525 from .Constraints import Constraint526 536 selected_rows = self.lstParams.selectionModel().selectedRows() 537 assert(len(selected_rows), 2) 527 538 528 539 params_list = [s.data() for s in selected_rows] 540 # Create and display the widget for param1 and param2 529 541 mc_widget = MultiConstraint(self, params=params_list) 530 mc_widget.exec_() 542 if mc_widget.exec_() != QtWidgets.QDialog.Accepted: 543 return 544 531 545 constraint = Constraint() 532 546 c_text = mc_widget.txtConstraint.text() 533 # Pass the constraint to the parser 534 535 self.communicate.statusBarUpdateSignal.emit('Constraints added')536 # Change the colour of the row537 pass 538 539 def modifyViewOnConstraint(self, row):540 """541 Add visual cues that the parameter is constrained542 """ 543 value = self._model_model.item(row, 1).text()544 # Set min/max to the value constrained545 self._model_model.item(row,2).setText(value)546 self._model_model.item(row,3).setText(value)547 548 # widget.params[0] is the parameter we're constraining 549 constraint.param = mc_widget.params[0] 550 constraint.func = c_text 551 552 # Create a new item and add the Constraint object as a child 553 item = QtGui.QStandardItem() 554 item.setData(constraint) 555 # Which row is the constrained parameter in? 556 557 row = self.rowFromName(constraint.param) 558 self._model_model.item(row, 1).setChild(0, item) 559 #self.constraintAddedSignal.emit([row]) 560 # Show visual hints for the constraint 547 561 font = QtGui.QFont() 548 562 font.setItalic(True) 549 563 brush = QtGui.QBrush(QtGui.QColor('blue')) 564 self.modifyViewOnRow(row, font=font, brush=brush) 565 566 # Pass the constraint to the parser 567 self.communicate.statusBarUpdateSignal.emit('Constraints added') 568 569 def rowFromName(self, name): 570 """ 571 given parameter name get the row number in self._model_model 572 """ 573 for row in range(self._model_model.rowCount()): 574 row_name = self._model_model.item(row).text() 575 if row_name == name: 576 return row 577 return None 578 579 def modifyViewOnRow(self, row, font=None, brush=None): 580 """ 581 Chage how the given row of the main model is shown 582 """ 583 fields_enabled = False 584 if font is None: 585 font = QtGui.QFont() 586 fields_enabled = True 587 if brush is None: 588 brush = QtGui.QBrush() 589 fields_enabled = True 550 590 self._model_model.blockSignals(True) 551 591 # Modify font and foreground of affected rows … … 553 593 self._model_model.item(row, column).setForeground(brush) 554 594 self._model_model.item(row, column).setFont(font) 555 self._model_model.item(row, column).setEditable( False)595 self._model_model.item(row, column).setEditable(fields_enabled) 556 596 self._model_model.blockSignals(False) 557 597 … … 560 600 Adds a constraint on a single parameter. 561 601 """ 562 from .Constraints import Constraint563 602 for row in self.selectedParameters(): 564 603 param = self._model_model.item(row, 0).text() 565 604 value = self._model_model.item(row, 1).text() 566 constraint = Constraint(param=param, value=value) 605 min = self._model_model.item(row, 2).text() 606 max = self._model_model.item(row, 3).text() 607 # Create a Constraint object 608 constraint = Constraint(param=param, value=value, min=min, max=max) 609 # Create a new item and add the Constraint object as a child 567 610 item = QtGui.QStandardItem() 568 611 item.setData(constraint) 569 612 self._model_model.item(row, 1).setChild(0, item) 613 # Set min/max to the value constrained 614 self._model_model.item(row,2).setText(value) 615 self._model_model.item(row,3).setText(value) 570 616 #self.constraintAddedSignal.emit([row]) 571 self.modifyViewOnConstraint(row) 617 # Show visual hints for the constraint 618 font = QtGui.QFont() 619 font.setItalic(True) 620 brush = QtGui.QBrush(QtGui.QColor('blue')) 621 self.modifyViewOnRow(row, font=font, brush=brush) 572 622 self.communicate.statusBarUpdateSignal.emit('Constraint added') 573 623 pass 624 625 def deleteConstraint(self): 626 """ 627 Delete constraints from selected parameters. 628 """ 629 for row in self.selectedParameters(): 630 # Get the Constraint object from of the model item 631 item = self._model_model.item(row, 1) 632 constraint = item.child(0).data() 633 # Retrieve old values and put them on the model 634 if constraint.min is not None: 635 self._model_model.item(row, 2).setText(constraint.min) 636 if constraint.max is not None: 637 self._model_model.item(row, 3).setText(constraint.max) 638 # Remove constraint item 639 item.removeRow(0) 640 #self.constraintAddedSignal.emit([row]) 641 self.modifyViewOnRow(row) 642 self.communicate.statusBarUpdateSignal.emit('Constraint removed') 643 pass 644 645 def rowHasConstraint(self, row): 646 """ 647 Finds out if row of the main model has a constraint child 648 """ 649 item = self._model_model.item(row,1) 650 return True if (item.hasChildren() and isinstance(item.child(0).data(), Constraint)) else False 574 651 575 652 def selectParameters(self): … … 964 1041 np.any(res.pvec is None) or \ 965 1042 not np.all(np.isfinite(res.pvec)): 966 msg = "Fitting did not converge! !!"1043 msg = "Fitting did not converge!" 967 1044 self.communicate.statusBarUpdateSignal.emit(msg) 968 1045 logging.error(msg) -
src/sas/qtgui/Perspectives/Fitting/MultiConstraint.py
r6b0c2f6 r2109350 36 36 switch M1 <-> M2 37 37 """ 38 # Switch parameters 38 39 self.params[1], self.params[0] = self.params[0], self.params[1] 40 # Try to swap parameter names in the line edit 41 current_text = self.txtConstraint.text() 42 new_text = current_text.replace(self.params[0], self.params[1]) 43 self.txtConstraint.setText(new_text) 44 # Update labels and tooltips 39 45 self.setupLabels() 40 46 self.setupTooltip() … … 88 94 # 2. ensure the text contains parameter name 89 95 parameter_string_start = constraint_text.find(param_str) 90 has_parameter_name = (parameter_string_start > -1) 91 if not has_parameter_name: 96 if parameter_string_start < 0: 92 97 return False 93 98 parameter_string_end = parameter_string_start + len(param_str) 94 99 95 # 3. parameter name should be a separate word, but can have "()[]*+-/ " around100 # 3. parameter name should be a separate word, but can have "()[]*+-/ " around 96 101 valid_neighbours = "()[]*+-/ " 97 102 has_only_parameter = False -
src/sas/qtgui/Perspectives/Fitting/UI/MultiConstraintUI.ui
r378e808 r2109350 10 10 <x>0</x> 11 11 <y>0</y> 12 <width>3 35</width>12 <width>369</width> 13 13 <height>143</height> 14 14 </rect> … … 158 158 </widget> 159 159 <resources/> 160 <connections/> 160 <connections> 161 <connection> 162 <sender>cmdOK</sender> 163 <signal>clicked()</signal> 164 <receiver>MultiConstraintUI</receiver> 165 <slot>accept()</slot> 166 <hints> 167 <hint type="sourcelabel"> 168 <x>321</x> 169 <y>121</y> 170 </hint> 171 <hint type="destinationlabel"> 172 <x>184</x> 173 <y>71</y> 174 </hint> 175 </hints> 176 </connection> 177 </connections> 161 178 </ui>
Note: See TracChangeset
for help on using the changeset viewer.