Skip to content

Commit

Permalink
Re #5261. This should fix the issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed May 4, 2012
1 parent fe783e4 commit 38e17f8
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget
bool isDoubleAttribute(QtProperty* prop) const;
/// Check if property is a function attribute
bool isIntAttribute(QtProperty* prop) const;
/// Get string attribute
std::string getStringAttribute(QtProperty* prop) const;
/// Get double attribute
double getDoubleAttribute(QtProperty* prop) const;
/// Get int attribute
int getIntAttribute(QtProperty* prop) const;
/// Check if property is a function paramater
bool isParameter(QtProperty* prop) const;
/// Get attribute as a string
Expand Down Expand Up @@ -214,6 +208,8 @@ protected slots:
QtStringPropertyManager *m_tieManager;
/// Manager for parameter constraint properties
QtStringPropertyManager *m_constraintManager;
/// Manager for Formula attributes
QtStringPropertyManager *m_formulaManager;


/// Qt property browser which displays properties
Expand Down Expand Up @@ -253,6 +249,7 @@ protected slots:
QAction *m_actionRemoveConstraint;

friend class CreateAttributePropertyForFunctionBrowser;
friend class SetAttributeFromProperty;
};


Expand Down
180 changes: 96 additions & 84 deletions Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void FunctionBrowser::createBrowser()
m_indexManager = new QtStringPropertyManager(this);
m_tieManager = new QtStringPropertyManager(this);
m_constraintManager = new QtStringPropertyManager(this);
m_formulaManager = new QtStringPropertyManager(this);

//m_filenameManager = new QtStringPropertyManager(this);
//m_formulaManager = new QtStringPropertyManager(this);
Expand All @@ -152,7 +153,7 @@ void FunctionBrowser::createBrowser()
DoubleEditorFactory *doubleEditorFactory = new DoubleEditorFactory(this);
QtLineEditFactory *lineEditFactory = new QtLineEditFactory(this);
//StringDialogEditorFactory* stringDialogEditFactory = new StringDialogEditorFactory(this);
//FormulaDialogEditorFactory* formulaDialogEditFactory = new FormulaDialogEditorFactory(this);
FormulaDialogEditorFactory* formulaDialogEditFactory = new FormulaDialogEditorFactory(this);

m_browser = new QtTreePropertyBrowser();
// assign factories to property managers
Expand All @@ -163,6 +164,7 @@ void FunctionBrowser::createBrowser()
m_browser->setFactoryForManager(m_indexManager, lineEditFactory);
m_browser->setFactoryForManager(m_tieManager, lineEditFactory);
m_browser->setFactoryForManager(m_constraintManager, lineEditFactory);
m_browser->setFactoryForManager(m_formulaManager, formulaDialogEditFactory);

m_browser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_browser, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(popupMenu(const QPoint &)));
Expand Down Expand Up @@ -425,8 +427,8 @@ void FunctionBrowser::addFunction(QtProperty* prop, Mantid::API::IFunction_sptr
}
else
{
std::cerr << "add to: " << prop->propertyName().toStdString() << std::endl;
Mantid::API::IFunction_sptr parentFun = getFunction(prop);
if ( !parentFun ) return;
auto cf = boost::dynamic_pointer_cast<Mantid::API::CompositeFunction>(parentFun);
if ( !cf )
{
Expand Down Expand Up @@ -457,8 +459,17 @@ class CreateAttributePropertyForFunctionBrowser: public Mantid::API::IFunction::
/// Create string property
FunctionBrowser::AProperty apply(const std::string& str)const
{
QtProperty* prop = m_browser->m_attributeStringManager->addProperty(m_attName);
m_browser->m_attributeStringManager->setValue(prop, QString::fromStdString(str));
QtProperty* prop = NULL;
if ( m_attName == "Formula" )
{
prop = m_browser->m_formulaManager->addProperty(m_attName);
m_browser->m_formulaManager->setValue(prop, QString::fromStdString(str));
}
else
{
prop = m_browser->m_attributeStringManager->addProperty(m_attName);
m_browser->m_attributeStringManager->setValue(prop, QString::fromStdString(str));
}
return m_browser->addProperty(m_parent,prop);
}
/// Create double property
Expand All @@ -481,6 +492,46 @@ class CreateAttributePropertyForFunctionBrowser: public Mantid::API::IFunction::
QString m_attName;
};

/**
* Attribute visitor to set an attribute from a QtProperty. Depending on the attribute type
* the appropriate apply() method is used.
*/
class SetAttributeFromProperty: public Mantid::API::IFunction::AttributeVisitor<>
{
public:
SetAttributeFromProperty(FunctionBrowser* browser, QtProperty* prop)
:m_browser(browser),m_prop(prop)
{
}
protected:
/// Set string attribute
void apply(std::string& str)const
{
QString attName = m_prop->propertyName();
if ( attName == "Formula" )
{
str = m_browser->m_formulaManager->value(m_prop).toStdString();
}
else
{
str = m_browser->m_attributeStringManager->value(m_prop).toStdString();
}
}
/// Set double attribute
void apply(double& d)const
{
d = m_browser->m_attributeDoubleManager->value(m_prop);
}
/// Set int attribute
void apply(int& i)const
{
i = m_browser->m_attributeIntManager->value(m_prop);
}
private:
FunctionBrowser* m_browser;
QtProperty* m_prop;
};

/**
* Add a attribute property
* @param parent :: Parent function property
Expand Down Expand Up @@ -633,7 +684,10 @@ bool FunctionBrowser::isFunction(QtProperty* prop) const
*/
bool FunctionBrowser::isStringAttribute(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_attributeStringManager) == prop->propertyManager();
return prop && (
dynamic_cast<QtAbstractPropertyManager*>(m_attributeStringManager) == prop->propertyManager() ||
dynamic_cast<QtAbstractPropertyManager*>(m_formulaManager) == prop->propertyManager()
);
}

/**
Expand Down Expand Up @@ -663,49 +717,6 @@ bool FunctionBrowser::isAttribute(QtProperty* prop) const
return isStringAttribute(prop) || isDoubleAttribute(prop) || isIntAttribute(prop);
}

/**
* Get string attribute
* @param prop :: An attribute property
*/
std::string FunctionBrowser::getStringAttribute(QtProperty* prop) const
{
if (isStringAttribute(prop))
{
return m_attributeStringManager->value(prop).toStdString();
}
throw std::runtime_error("FunctionBrowser: attribute " + prop->propertyName().toStdString() +
" is not string.");
}

/**
* Get double attribute
* @param prop :: An attribute property
*/
double FunctionBrowser::getDoubleAttribute(QtProperty* prop) const
{
if (isDoubleAttribute(prop))
{
return m_attributeDoubleManager->value(prop);
}
throw std::runtime_error("FunctionBrowser: attribute " + prop->propertyName().toStdString() +
" is not double.");
}

/**
* Get int attribute
* @param prop :: An attribute property
*/
int FunctionBrowser::getIntAttribute(QtProperty* prop) const
{
if (isIntAttribute(prop))
{
return m_attributeIntManager->value(prop);
}
throw std::runtime_error("FunctionBrowser: attribute " + prop->propertyName().toStdString() +
" is not int.");
}


/**
* Check if property is a function parameter
* @param prop :: Property to check
Expand Down Expand Up @@ -1027,11 +1038,6 @@ bool FunctionBrowser::hasUpperBound(QtProperty* prop) const
*/
void FunctionBrowser::popupMenu(const QPoint &)
{
auto fun = getFunction();
if (fun)
{
std::cerr << fun->asString() << std::endl;
}
auto item = m_browser->currentItem();
if (!item)
{
Expand Down Expand Up @@ -1159,7 +1165,11 @@ void FunctionBrowser::addFunction()
else
{
cf.reset(new Mantid::API::CompositeFunction);
cf->addFunction(getFunction(prop));
auto f0 = getFunction(prop);
if ( f0 )
{
cf->addFunction(f0);
}
cf->addFunction(f);
setFunction(cf);
}
Expand Down Expand Up @@ -1196,26 +1206,43 @@ Mantid::API::IFunction_sptr FunctionBrowser::getFunction(QtProperty* prop)
if (isFunction(child))
{
auto f = getFunction(child);
cf->addFunction(f);
// if f is null ignore that function
if ( f )
{
cf->addFunction(f);
}
}
}
}
else
{
// loop over the children properties and set parameters and attributes
auto children = prop->subProperties();
foreach(QtProperty* child, children)
{
if (isStringAttribute(child))
{
fun->setAttributeValue(child->propertyName().toStdString(),getStringAttribute(child));
}
if (isDoubleAttribute(child))
{
fun->setAttributeValue(child->propertyName().toStdString(),getDoubleAttribute(child));
}
if (isIntAttribute(child))
if (isAttribute(child))
{
fun->setAttributeValue(child->propertyName().toStdString(),getIntAttribute(child));
std::string attName = child->propertyName().toStdString();
SetAttributeFromProperty setter(this,child);
Mantid::API::IFunction::Attribute attr = fun->getAttribute(attName);
attr.apply(setter);
try
{
fun->setAttribute(attName,attr);
}
catch(std::exception& e)
{
if ( attName == "Formula" && attr.asString().empty() )
{
fun->setAttributeValue("Formula","x");
}
else
{
QMessageBox::warning(this,"Mantid - Warning","Attribute " + child->propertyName() +
" cannot be set for function " + prop->propertyName() + ":\n" + e.what() );
return Mantid::API::IFunction_sptr();
}
}
}
else if (isParameter(child))
{
Expand Down Expand Up @@ -1473,25 +1500,10 @@ void FunctionBrowser::attributeChanged(QtProperty* prop)
auto fun = Mantid::API::FunctionFactory::Instance().createFunction(funProp->propertyName().toStdString());

std::string attName = prop->propertyName().toStdString();
if ( isStringAttribute(prop) )
{
std::string value = getStringAttribute(prop);
fun->setAttributeValue(attName, value);
}
else if ( isDoubleAttribute(prop) )
{
double value = getDoubleAttribute(prop);
fun->setAttributeValue(attName, value);
}
else if ( isIntAttribute(prop) )
{
int value = getIntAttribute(prop);
fun->setAttributeValue(attName, value);
}
else
{
throw std::runtime_error("FunctionBrowser: unknown attribute type.");
}
SetAttributeFromProperty setter(this,prop);
Mantid::API::IFunction::Attribute attr = fun->getAttribute(attName);
attr.apply(setter);
fun->setAttribute(attName,attr);

setFunction(funProp, fun);
updateFunctionIndices();
Expand Down

0 comments on commit 38e17f8

Please sign in to comment.