From 171f6e64de52e74badc1be162cab46ce6ae60355 Mon Sep 17 00:00:00 2001 From: Roman Tolchenov Date: Thu, 26 Mar 2015 14:56:16 +0000 Subject: [PATCH] Re #10576. Moved local parameter values to FunctionBrowser. --- .../MultiDatasetFit.h | 8 +- .../CustomInterfaces/src/MultiDatasetFit.cpp | 76 +++++------------- .../MantidQtMantidWidgets/FunctionBrowser.h | 28 ++++++- .../MantidWidgets/src/FunctionBrowser.cpp | 80 ++++++++++++++++++- 4 files changed, 125 insertions(+), 67 deletions(-) diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h index bc4f63f26f70..29d467de4ea7 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/MultiDatasetFit.h @@ -84,12 +84,12 @@ class MANTIDQT_CUSTOMINTERFACES_DLL MultiDatasetFit: public API::UserSubWindow std::pair getFittingRange(int i) const; /// Total number of spectra (datasets). int getNumberOfSpectra() const; - /// Get value of a local parameter - double getLocalParameterValue(const QString& parName, int i) const; /// Display info about the plot. void showPlotInfo(); /// Check that the data sets in the table are valid void checkDataSets(); + /// Get value of a local parameter + double getLocalParameterValue(const QString& parName, int i) const; public slots: void setLocalParameterValue(const QString& parName, int i, double value); @@ -99,7 +99,6 @@ private slots: void fit(); void editLocalParameterValues(const QString& parName); void finishFit(bool); - void updateLocalParameters(int index); void enableZoom(); void enablePan(); void enableRange(); @@ -111,7 +110,6 @@ private slots: private: void createPlotToolbar(); boost::shared_ptr createFunction() const; - void initLocalParameter(const QString& parName)const; void updateParameters(const Mantid::API::IFunction& fun); void showInfo(const QString& text); bool eventFilter(QObject *widget, QEvent *evn); @@ -134,8 +132,6 @@ private slots: MantidWidgets::FitOptionsBrowser *m_fitOptionsBrowser; /// Name of the output workspace std::string m_outputWorkspaceName; - /// Storage for local paramtere values - mutable QMap> m_localParameterValues; /// Fit algorithm runner boost::shared_ptr m_fitRunner; }; diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp index 29d32eee65a1..0c582a1f0c6f 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp @@ -834,7 +834,6 @@ void MultiDatasetFit::initLayout() connect(m_dataController,SIGNAL(dataTableUpdated()),m_plotController,SLOT(tableUpdated())); connect(m_dataController,SIGNAL(dataSetUpdated(int)),m_plotController,SLOT(updateRange(int))); connect(m_plotController,SIGNAL(fittingRangeChanged(int, double, double)),m_dataController,SLOT(setFittingRange(int, double, double))); - connect(m_plotController,SIGNAL(currentIndexChanged(int)),this,SLOT(updateLocalParameters(int))); QSplitter* splitter = new QSplitter(Qt::Vertical,this); @@ -842,6 +841,7 @@ void MultiDatasetFit::initLayout() splitter->addWidget( m_functionBrowser ); connect(m_functionBrowser,SIGNAL(localParameterButtonClicked(const QString&)),this,SLOT(editLocalParameterValues(const QString&))); connect(m_functionBrowser,SIGNAL(functionStructureChanged()),this,SLOT(reset())); + connect(m_plotController,SIGNAL(currentIndexChanged(int)),m_functionBrowser,SLOT(setCurrentDataset(int))); m_fitOptionsBrowser = new MantidQt::MantidWidgets::FitOptionsBrowser(NULL); splitter->addWidget( m_fitOptionsBrowser ); @@ -1112,45 +1112,6 @@ void MultiDatasetFit::editLocalParameterValues(const QString& parName) dialog.exec(); } -/** - * Get value of a local parameter - * @param parName :: Name of a parameter. - * @param i :: Data set index. - */ -double MultiDatasetFit::getLocalParameterValue(const QString& parName, int i) const -{ - if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfSpectra() ) - { - initLocalParameter(parName); - } - return m_localParameterValues[parName][i]; -} - -void MultiDatasetFit::setLocalParameterValue(const QString& parName, int i, double value) -{ - if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfSpectra() ) - { - initLocalParameter(parName); - } - m_localParameterValues[parName][i] = value; -} - -/** - * Init a local parameter. Define initial values for all datasets. - * @param parName :: Name of parametere to init. - */ -void MultiDatasetFit::initLocalParameter(const QString& parName)const -{ - double value = m_functionBrowser->getParameter(parName); - QVector values( static_cast(getNumberOfSpectra()), value ); - m_localParameterValues[parName] = values; -} - -void MultiDatasetFit::reset() -{ - m_localParameterValues.clear(); -} - /** * Slot called on completion of the Fit algorithm. * @param error :: Set to true if Fit finishes with an error. @@ -1171,7 +1132,7 @@ void MultiDatasetFit::finishFit(bool error) */ void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun) { - m_localParameterValues.clear(); + m_functionBrowser->resetLocalParameters(); auto cfun = dynamic_cast( &fun ); if ( cfun && cfun->nFunctions() > 0 ) { @@ -1191,7 +1152,7 @@ void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun) } for(int j = 0; j < qLocalParameters.size(); ++j) { - setLocalParameterValue( qLocalParameters[j], static_cast(i), sfun->getParameter(localParameters[j]) ); + m_functionBrowser->setLocalParameterValue( qLocalParameters[j], static_cast(i), sfun->getParameter(localParameters[j]) ); } } } @@ -1201,20 +1162,6 @@ void MultiDatasetFit::updateParameters(const Mantid::API::IFunction& fun) } } -/** - * Update the local parameters in the function browser to show values corresponding - * to a particular dataset. - * @param index :: Index of a dataset. - */ -void MultiDatasetFit::updateLocalParameters(int index) -{ - auto localParameters = m_functionBrowser->getLocalParameters(); - foreach(QString par, localParameters) - { - m_functionBrowser->setParameter( par, getLocalParameterValue( par, index ) ); - } -} - /** * Show a message in the info bar at the bottom of the interface. */ @@ -1335,6 +1282,23 @@ void MultiDatasetFit::enableRange() m_uiForm.toolOptions->setCurrentIndex(rangeToolPage); } +void MultiDatasetFit::setLocalParameterValue(const QString& parName, int i, double value) +{ + m_functionBrowser->setLocalParameterValue(parName, i, value); +} + +/// Get value of a local parameter +double MultiDatasetFit::getLocalParameterValue(const QString& parName, int i) const +{ + return m_functionBrowser->getLocalParameterValue(parName, i); +} + +void MultiDatasetFit::reset() +{ + m_functionBrowser->resetLocalParameters(); + m_functionBrowser->setNumberOfDatasets( getNumberOfSpectra() ); +} + /*==========================================================================================*/ /* DataController */ /*==========================================================================================*/ diff --git a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h index 1f2b9aba85fc..f77531cf100c 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h +++ b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/FunctionBrowser.h @@ -95,10 +95,6 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget Mantid::API::IFunction_sptr getFunction(QtProperty* prop = NULL, bool attributesOnly = false); /// Check if a function is set bool hasFunction() const; - /// Get a list of names of global parameters - QStringList getGlobalParameters() const; - /// Get a list of names of local parameters - QStringList getLocalParameters() const; /// Return a function with specified index Mantid::API::IFunction_sptr getFunctionByIndex(const QString& index); @@ -117,6 +113,15 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget /// Update parameter values in the browser to match those of a function. void updateParameters(const Mantid::API::IFunction& fun); + /// Get a list of names of global parameters + QStringList getGlobalParameters() const; + /// Get a list of names of local parameters + QStringList getLocalParameters() const; + /// Get the number of datasets + size_t getNumberOfDatasets() const; + /// Get value of a local parameter + double getLocalParameterValue(const QString& parName, int i) const; + signals: /// User selects a different function (or one of it's sub-properties) void currentFunctionChanged(); @@ -131,6 +136,13 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget void functionStructureChanged(); +public slots: + + void setNumberOfDatasets(size_t n); + void setLocalParameterValue(const QString& parName, int i, double value); + void resetLocalParameters(); + void setCurrentDataset(int i); + protected: /// Create the Qt property browser void createBrowser(); @@ -205,6 +217,8 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget /// Check if a parameter property has a upper bound bool hasUpperBound(QtProperty* prop) const; + void initLocalParameter(const QString& parName)const; + protected slots: /// Show the context menu void popupMenu(const QPoint &); @@ -319,6 +333,12 @@ protected slots: /// Set true if the constructed function is intended to be used in a multi-dataset fit bool m_multiDataset; + /// Number of datasets this function is used for + size_t m_numberOfDatasets; + /// Storage for local paramtere values + mutable QMap> m_localParameterValues; + /// Index of a dataset for which the parameters are currently displayed + int m_currentDataset; friend class CreateAttributePropertyForFunctionBrowser; friend class SetAttributeFromProperty; diff --git a/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp b/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp index 874e0423532f..d74e1b424df7 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp +++ b/Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp @@ -82,7 +82,10 @@ namespace MantidWidgets * @param multi :: Option to use the browser for multi-dataset fitting. */ FunctionBrowser::FunctionBrowser(QWidget *parent, bool multi) - :QWidget(parent),m_multiDataset(multi) + :QWidget(parent),m_multiDataset(multi), + m_numberOfDatasets(0), + m_currentDataset(0) + { // create m_browser createBrowser(); @@ -1829,5 +1832,80 @@ bool FunctionBrowser::hasFunction() const return ! m_functionManager->properties().isEmpty(); } +/// Get the number of datasets +size_t FunctionBrowser::getNumberOfDatasets() const +{ + return m_numberOfDatasets; +} + +/// Set new number of the datasets +/// @param n :: New value for the number of datasets. +void FunctionBrowser::setNumberOfDatasets(size_t n) +{ + if ( !m_multiDataset ) + { + throw std::runtime_error("Function browser wasn't set up for multi-dataset fitting."); + } + m_numberOfDatasets = n; +} + +/** + * Get value of a local parameter + * @param parName :: Name of a parameter. + * @param i :: Data set index. + */ +double FunctionBrowser::getLocalParameterValue(const QString& parName, int i) const +{ + if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfDatasets() ) + { + initLocalParameter(parName); + } + return m_localParameterValues[parName][i]; +} + +void FunctionBrowser::setLocalParameterValue(const QString& parName, int i, double value) +{ + if ( !m_localParameterValues.contains(parName) || m_localParameterValues[parName].size() != getNumberOfDatasets() ) + { + initLocalParameter(parName); + } + m_localParameterValues[parName][i] = value; + if ( i == m_currentDataset ) + { + setParameter( parName, value ); + } +} + +/** + * Init a local parameter. Define initial values for all datasets. + * @param parName :: Name of parametere to init. + */ +void FunctionBrowser::initLocalParameter(const QString& parName)const +{ + double value = getParameter(parName); + QVector values( static_cast(getNumberOfDatasets()), value ); + m_localParameterValues[parName] = values; +} + +void FunctionBrowser::resetLocalParameters() +{ + m_localParameterValues.clear(); +} + +/// Set current dataset. +void FunctionBrowser::setCurrentDataset(int i) +{ + m_currentDataset = i; + if ( m_currentDataset >= m_numberOfDatasets ) + { + throw std::runtime_error("Dataset index is outside the range"); + } + auto localParameters = getLocalParameters(); + foreach(QString par, localParameters) + { + setParameter( par, getLocalParameterValue( par, m_currentDataset ) ); + } +} + } // MantidWidgets } // MantidQt