diff --git a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h index 555844c911cea502273e812e2b6df2ffd08c4010..17203c637ac382337d0eb478bd32a5c9af715010 100644 --- a/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h +++ b/qt/widgets/common/inc/MantidQtWidgets/Common/FitScriptGeneratorPresenter.h @@ -100,6 +100,14 @@ private: WorkspaceIndex workspaceIndex, std::string const ¶meter, std::string const &tie); + void removeParameterConstraint(std::string const &workspaceName, + WorkspaceIndex workspaceIndex, + std::string const ¶meter); + void updateParameterConstraint(std::string const &workspaceName, + WorkspaceIndex workspaceIndex, + std::string const &functionIndex, + std::string const &constraint); + void updateFunctionInViewFromModel(FitDomainIndex domainIndex); template <void (FitScriptGeneratorPresenter::*func)( @@ -121,12 +129,8 @@ private: void updateFunctionForDomainInModel(FitDomainIndex domainIndex, std::string const &function); - template <typename T, - void (FitScriptGeneratorPresenter::*func)( - std::string const &workspaceName, WorkspaceIndex workspaceIndex, - std::string const &functionComponent, T newValue)> - void updateFunctionComponent(std::string const &functionComponent, - T newValue); + template <typename Function, typename... Args> + void updateFunctionComponent(Function &&func, Args... arguments); [[nodiscard]] std::vector<FitDomainIndex> getRowIndices() const; diff --git a/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp b/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp index 3b0d0bdc17b279198ee29c0160063c0ace13c310..6a3099a079cd0836d27ed19740c883e28ed98f38 100644 --- a/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp +++ b/qt/widgets/common/src/FitScriptGeneratorPresenter.cpp @@ -174,27 +174,21 @@ void FitScriptGeneratorPresenter::handleFunctionReplaced( void FitScriptGeneratorPresenter::handleParameterChanged( std::string const ¶meter) { - updateFunctionComponent<double, - &FitScriptGeneratorPresenter::updateParameterValue>( - parameter, m_view->parameterValue(parameter)); - + updateFunctionComponent(&FitScriptGeneratorPresenter::updateParameterValue, + parameter, m_view->parameterValue(parameter)); handleSelectionChanged(); } void FitScriptGeneratorPresenter::handleAttributeChanged( std::string const &attribute) { - updateFunctionComponent<IFunction::Attribute const &, - &FitScriptGeneratorPresenter::updateAttributeValue>( - attribute, m_view->attributeValue(attribute)); + updateFunctionComponent(&FitScriptGeneratorPresenter::updateAttributeValue, + attribute, m_view->attributeValue(attribute)); } void FitScriptGeneratorPresenter::handleParameterTieChanged( std::string const ¶meter, std::string const &tie) { - for (auto const &rowIndex : getRowIndices()) { - auto const workspaceName = m_view->workspaceName(rowIndex); - auto const workspaceIndex = m_view->workspaceIndex(rowIndex); - updateParameterTie(workspaceName, workspaceIndex, parameter, tie); - } + updateFunctionComponent(&FitScriptGeneratorPresenter::updateParameterTie, + parameter, tie); checkForWarningMessages(); setGlobalTies(m_model->getGlobalTies()); @@ -203,28 +197,16 @@ void FitScriptGeneratorPresenter::handleParameterTieChanged( void FitScriptGeneratorPresenter::handleParameterConstraintRemoved( std::string const ¶meter) { - for (auto const &rowIndex : getRowIndices()) { - auto const workspaceName = m_view->workspaceName(rowIndex); - auto const workspaceIndex = m_view->workspaceIndex(rowIndex); - m_model->removeParameterConstraint(workspaceName, workspaceIndex, - parameter); - } - + updateFunctionComponent( + &FitScriptGeneratorPresenter::removeParameterConstraint, parameter); handleSelectionChanged(); } void FitScriptGeneratorPresenter::handleParameterConstraintChanged( std::string const &functionIndex, std::string const &constraint) { - for (auto const &rowIndex : getRowIndices()) { - auto const workspaceName = m_view->workspaceName(rowIndex); - auto const workspaceIndex = m_view->workspaceIndex(rowIndex); - auto const equivalentFunctionIndex = - m_model->getEquivalentFunctionIndexForDomain( - workspaceName, workspaceIndex, functionIndex); - m_model->updateParameterConstraint(workspaceName, workspaceIndex, - equivalentFunctionIndex, constraint); - } - + updateFunctionComponent( + &FitScriptGeneratorPresenter::updateParameterConstraint, functionIndex, + constraint); handleSelectionChanged(); } @@ -354,6 +336,22 @@ void FitScriptGeneratorPresenter::updateParameterTie( } } +void FitScriptGeneratorPresenter::removeParameterConstraint( + std::string const &workspaceName, WorkspaceIndex workspaceIndex, + std::string const ¶meter) { + m_model->removeParameterConstraint(workspaceName, workspaceIndex, parameter); +} + +void FitScriptGeneratorPresenter::updateParameterConstraint( + std::string const &workspaceName, WorkspaceIndex workspaceIndex, + std::string const &functionIndex, std::string const &constraint) { + auto const equivalentFunctionIndex = + m_model->getEquivalentFunctionIndexForDomain( + workspaceName, workspaceIndex, functionIndex); + m_model->updateParameterConstraint(workspaceName, workspaceIndex, + equivalentFunctionIndex, constraint); +} + void FitScriptGeneratorPresenter::updateFunctionInViewFromModel( FitDomainIndex domainIndex) { auto const workspaceName = m_view->workspaceName(domainIndex); @@ -407,17 +405,15 @@ void FitScriptGeneratorPresenter::updateFunctionForDomainInModel( (m_model->*func)(workspaceName, workspaceIndex, function); } -template <typename T, - void (FitScriptGeneratorPresenter::*func)( - std::string const &workspaceName, WorkspaceIndex workspaceIndex, - std::string const &functionComponent, T newValue)> -void FitScriptGeneratorPresenter::updateFunctionComponent( - std::string const &functionComponent, T newValue) { +template <typename Function, typename... Args> +void FitScriptGeneratorPresenter::updateFunctionComponent(Function &&func, + Args... arguments) { for (auto const &rowIndex : getRowIndices()) { auto const workspaceName = m_view->workspaceName(rowIndex); auto const workspaceIndex = m_view->workspaceIndex(rowIndex); - (this->*func)(workspaceName, workspaceIndex, functionComponent, newValue); + std::invoke(std::forward<Function>(func), this, workspaceName, + workspaceIndex, arguments...); } }