Skip to content

Commit

Permalink
Refs #9213. Extend FunctionBrowser functionality
Browse files Browse the repository at this point in the history
This allows for finer-grain function retrieval and updating, plus a few
signals to indicate basic changes.
  • Loading branch information
arturbekasov committed May 13, 2014
1 parent b1c8656 commit 2abc97a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
Expand Up @@ -8,6 +8,8 @@
#include <QWidget>
#include <QMap>

#include <boost/optional.hpp>

/* Forward declarations */

class QtTreePropertyBrowser;
Expand Down Expand Up @@ -92,6 +94,24 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget
/// Return the function
Mantid::API::IFunction_sptr getFunction(QtProperty* prop = NULL, bool attributesOnly = false);

/// Return a function with specified index
Mantid::API::IFunction_sptr getFunctionByIndex(const QString& index);

/// Return index of the current function, if one is selected
boost::optional<QString> currentFunctionIndex() { return m_currentFunctionIndex; }

/// Update the function parameter value
void setParameter(const QString& funcIndex, const QString& paramName, double value);

signals:
/// User selects a different function (or one of it's sub-properties)
void currentFunctionChanged();

/// Function parameter gets changed
/// @param funcIndex :: Index of the changed function
/// @param paramName :: Name of the changed parameter
void parameterChanged(const QString& funcIndex, const QString& paramName);

protected:
/// Create the Qt property browser
void createBrowser();
Expand Down Expand Up @@ -141,6 +161,8 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FunctionBrowser: public QWidget
bool isIndex(QtProperty* prop) const;
/// Get the function index for a property
QString getIndex(QtProperty* prop) const;
/// Get function property for the index
QtProperty* getFunctionProperty(const QString& index);

/// Add a tie property
AProperty addTieProperty(QtProperty* prop, QString tie);
Expand Down Expand Up @@ -189,13 +211,17 @@ protected slots:
void addConstraints50();
/// Remove one of the constraints
void removeConstraint();
/// Update current function index depending on currently selected item
void updateCurrentFunctionIndex();

// Property change slots

/// Called when a function attribute property is changed
void attributeChanged(QtProperty*);
/// Called when a member of a vector attribute is changed
void attributeVectorDoubleChanged(QtProperty*);
/// Called when a function parameter property is changed
void parameterChanged(QtProperty*);

protected:
/// Manager for function group properties
Expand Down Expand Up @@ -266,6 +292,9 @@ protected slots:
/// Remove one constraints from current parameter
QAction *m_actionRemoveConstraint;

/// Index of currently selected function. Gets updated in updateCurrentFunctionIndex()
boost::optional<QString> m_currentFunctionIndex;

friend class CreateAttributePropertyForFunctionBrowser;
friend class SetAttributeFromProperty;
};
Expand Down
90 changes: 88 additions & 2 deletions Code/Mantid/MantidQt/MantidWidgets/src/FunctionBrowser.cpp
Expand Up @@ -145,7 +145,6 @@ void FunctionBrowser::createBrowser()

m_browser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_browser, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(popupMenu(const QPoint &)));
//connect(m_browser, SIGNAL(currentItemChanged(QtBrowserItem*)), this, SLOT(currentItemChanged(QtBrowserItem*)));

connect(m_attributeStringManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_attributeDoubleManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
Expand All @@ -154,6 +153,11 @@ void FunctionBrowser::createBrowser()
connect(m_formulaManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_filenameManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_attributeVectorDoubleManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeVectorDoubleChanged(QtProperty*)));

connect(m_parameterManager, SIGNAL(valueChanged(QtProperty*,double)),
SLOT(parameterChanged(QtProperty*)));

connect(m_browser, SIGNAL(currentItemChanged(QtBrowserItem*)), SLOT(updateCurrentFunctionIndex()));
}

/**
Expand Down Expand Up @@ -840,7 +844,28 @@ QString FunctionBrowser::getIndex(QtProperty* prop) const
}

auto ap = m_properties[prop];
return getIndex(ap.parent);
return getIndex(ap.parent);
}

/**
* Return function property for a function with given index.
* @param index :: Function index to search, or empty string for top-level function
* @return Function property, or NULL if not found
*/
QtProperty* FunctionBrowser::getFunctionProperty(const QString& index)
{
// Might not be the most efficient way to do it. m_functionManager might be searched instead,
// but it is not being kept up-to-date at the moment (is not cleared).
foreach (auto property, m_properties.keys())
{
if(isFunction(property) && getIndex(property) == index)
{
return property;
}
}

// No function with such index
return NULL;
}


Expand Down Expand Up @@ -1380,6 +1405,45 @@ Mantid::API::IFunction_sptr FunctionBrowser::getFunction(QtProperty* prop, bool
return fun;
}

/**
* Return function at specified function index (e.g. f0.)
* @param index :: Index of the function, or empty string for top-level function
* @return Function at index, or null pointer if not found
*/
Mantid::API::IFunction_sptr FunctionBrowser::getFunctionByIndex(const QString& index)
{
if(auto prop = getFunctionProperty(index))
{
return getFunction(prop);
}
else
{
return Mantid::API::IFunction_sptr();
}
}

/**
* Updates the function parameter value
* @param funcIndex :: Index of the function
* @param paramName :: Parameter name
* @param value :: New value
*/
void FunctionBrowser::setParameter(const QString& funcIndex, const QString& paramName, double value)
{
if (auto prop = getFunctionProperty(funcIndex))
{
auto children = prop->subProperties();
foreach(QtProperty* child, children)
{
if (isParameter(child) && child->propertyName() == paramName)
{
m_parameterManager->setValue(child, value);
break;
}
}
}
}

/**
* Return FunctionFactory function string
*/
Expand Down Expand Up @@ -1560,6 +1624,23 @@ void FunctionBrowser::removeConstraint()
removeProperty( prop );
}

void FunctionBrowser::updateCurrentFunctionIndex()
{
boost::optional<QString> newIndex;

if (auto item = m_browser->currentItem())
{
auto prop = item->property();
newIndex = getIndex(prop);
}

if (m_currentFunctionIndex != newIndex)
{
m_currentFunctionIndex = newIndex;
emit currentFunctionChanged();
}
}

/**
* Slot connected to all function attribute managers. Update the corresponding function.
* @param prop :: An attribute property that was changed
Expand Down Expand Up @@ -1587,6 +1668,11 @@ void FunctionBrowser::attributeVectorDoubleChanged(QtProperty *prop)
attributeChanged( vectorProp );
}

void FunctionBrowser::parameterChanged(QtProperty* prop)
{
emit parameterChanged(getIndex(prop), prop->propertyName());
}


} // MantidWidgets
} // MantidQt

0 comments on commit 2abc97a

Please sign in to comment.