Skip to content

Commit

Permalink
FitPropertyBrowser now listens for updates
Browse files Browse the repository at this point in the history
This ensures that fit functions can be defined at runtime and not
just when starting Mantid. Refs #970
  • Loading branch information
martyngigg committed Apr 16, 2013
1 parent 0f63629 commit 2396cbb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
6 changes: 5 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/FunctionFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ namespace API
template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<FunctionFactoryImpl>;
#endif /* _WIN32 */
typedef MANTID_API_DLL Mantid::Kernel::SingletonHolder<FunctionFactoryImpl> FunctionFactory;


/// Convenient typedef for an UpdateNotification
typedef FunctionFactoryImpl::UpdateNotification FunctionFactoryUpdateNotification;
/// Convenient typedef for an UpdateNotification AutoPtr
typedef const Poco::AutoPtr<FunctionFactoryUpdateNotification> & FunctionFactoryUpdateNotification_ptr;
} // namespace API
} // namespace Mantid

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#include <QList>

#include "MantidQtAPI/WorkspaceObserver.h"

#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/MatrixWorkspace.h"


Expand Down Expand Up @@ -248,6 +248,11 @@ public slots:
/// signal which can optionally be caught for customization after a fit has
/// been done
void fittingDone(QString);
void functionFactoryUpdateReceived();

protected slots:
/// Get the registered function names
virtual void populateFunctionNames();

private slots:

Expand Down Expand Up @@ -404,8 +409,6 @@ private slots:
void saveFunction(const QString& fnName);
/// Create CompositeFunction
void createCompositeFunction(const QString& str = "");
/// Get the registered function names
virtual void populateFunctionNames();
/// Check if the workspace can be used in the fit
virtual bool isWorkspaceValid(Mantid::API::Workspace_sptr)const;
/// Called when the fit is finished
Expand Down Expand Up @@ -435,6 +438,13 @@ private slots:
/// Returns the tie property for a parameter property, or NULL
QtProperty* getTieProperty(QtProperty* parProp)const;

/// Callback for FunctionFactory update notifications
void handleFactoryUpdate(Mantid::API::FunctionFactoryUpdateNotification_ptr);
/// Observes algorithm factory update notifications
Poco::NObserver<FitPropertyBrowser,
Mantid::API::FunctionFactoryUpdateNotification> m_updateObserver;


/// Make sure m_groupMember belongs to the group
//void validateGroupMember();

Expand Down
87 changes: 54 additions & 33 deletions Code/Mantid/MantidQt/MantidWidgets/src/FitPropertyBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "MantidQtMantidWidgets/SequentialFitDialog.h"
#include "MantidQtMantidWidgets/MultifitSetupDialog.h"

#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/IBackgroundFunction.h"
Expand Down Expand Up @@ -119,6 +118,7 @@ m_logValue(NULL),
m_compositeFunction(),
m_changeSlotsEnabled(false),
m_guessOutputName(true),
m_updateObserver(*this,&FitPropertyBrowser::handleFactoryUpdate),
m_currentHandler(0),
m_defaultFunction("Gaussian"),
m_defaultPeak("Gaussian"),
Expand Down Expand Up @@ -252,6 +252,11 @@ void FitPropertyBrowser::init()
m_settingsGroup = m_browser->addProperty(settingsGroup);

initLayout(w);

using Mantid::API::FunctionFactory;
FunctionFactory::Instance().notificationCenter.addObserver(m_updateObserver);
connect(this, SIGNAL(functionFactoryUpdateReceived()), this, SLOT(populateFunctionNames()));
FunctionFactory::Instance().enableNotifications();
}


Expand Down Expand Up @@ -1101,6 +1106,40 @@ std::string FitPropertyBrowser::costFunction()const
return m_costFunctions[i].toStdString();
}

/// Get the registered function names
void FitPropertyBrowser::populateFunctionNames()
{
const std::vector<std::string> names = Mantid::API::FunctionFactory::Instance().getKeys();
m_registeredFunctions.clear();
m_registeredPeaks.clear();
m_registeredBackgrounds.clear();
m_registeredOther.clear();

for(size_t i=0;i<names.size();i++)
{
std::string fnName = names[i];
QString qfnName = QString::fromStdString(fnName);
if (qfnName == "MultiBG") continue;

auto f = Mantid::API::FunctionFactory::Instance().createFunction(fnName);
m_registeredFunctions << qfnName;
Mantid::API::IPeakFunction* pf = dynamic_cast<Mantid::API::IPeakFunction*>(f.get());
//Mantid::API::CompositeFunction* cf = dynamic_cast<Mantid::API::CompositeFunction*>(f.get());
if (pf)
{
m_registeredPeaks << qfnName;
}
else if (dynamic_cast<Mantid::API::IBackgroundFunction*>(f.get()))
{
m_registeredBackgrounds << qfnName;
}
else
{
m_registeredOther << qfnName;
}
}
}

/** Called when the function name property changed
* @param prop :: A pointer to the function name property m_functionName
*/
Expand Down Expand Up @@ -1385,38 +1424,6 @@ void FitPropertyBrowser::setFwhm(double value)
}
}

/// Get the registered function names
void FitPropertyBrowser::populateFunctionNames()
{
const std::vector<std::string> names = Mantid::API::FunctionFactory::Instance().getKeys();
m_registeredFunctions.clear();
m_registeredPeaks.clear();
m_registeredBackgrounds.clear();
for(size_t i=0;i<names.size();i++)
{
std::string fnName = names[i];
QString qfnName = QString::fromStdString(fnName);
if (qfnName == "MultiBG") continue;

auto f = Mantid::API::FunctionFactory::Instance().createFunction(fnName);
m_registeredFunctions << qfnName;
Mantid::API::IPeakFunction* pf = dynamic_cast<Mantid::API::IPeakFunction*>(f.get());
//Mantid::API::CompositeFunction* cf = dynamic_cast<Mantid::API::CompositeFunction*>(f.get());
if (pf)
{
m_registeredPeaks << qfnName;
}
else if (dynamic_cast<Mantid::API::IBackgroundFunction*>(f.get()))
{
m_registeredBackgrounds << qfnName;
}
else
{
m_registeredOther << qfnName;
}
}
}


/// Get number of functions in CompositeFunction
int FitPropertyBrowser::count()const
Expand Down Expand Up @@ -2007,6 +2014,20 @@ QtProperty* FitPropertyBrowser::getTieProperty(QtProperty* parProp)const
return NULL;
}


/**
* Called when the function factory has been updated
* @param notice A Poco notification object
*/
void FitPropertyBrowser::handleFactoryUpdate(Mantid::API::FunctionFactoryUpdateNotification_ptr notice)
{
Q_UNUSED(notice);
// Don't call populate directly as the updates can come from a different thread
emit functionFactoryUpdateReceived();
std::cerr << "algorithm factory update recieved\n";
}


/** Display a tip
* @param txt :: The text to display
*/
Expand Down

0 comments on commit 2396cbb

Please sign in to comment.