Skip to content
Snippets Groups Projects
Commit a56fc0c8 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

ModelPath cleanup from remnants of old translators.

parent b3eff3c6
No related branches found
No related tags found
No related merge requests found
......@@ -15,55 +15,10 @@
// ************************************************************************** //
#include "ModelPath.h"
#include "GroupItem.h"
#include "ParticleItem.h"
#include "ParticleLayoutItem.h"
#include "SessionModel.h"
#include "JobItem.h"
using std::string;
std::vector<std::unique_ptr<IParameterTranslator>> ModelPath::m_special_translators {};
double ModelPath::getParameterValue(const SessionItem *item, const QString &name)
{
QString head = getFirstField(name);
auto p_child = item->getChildByName(head);
if (p_child) {
return getParameterValue(p_child, stripFirstField(name));
}
if (item->isTag(head)) {
return item->getItemValue(head).toDouble();
} else {
return 0.0;
}
}
string ModelPath::translateParameterName(const SessionItem* item, const QString& par_name)
{
std::ostringstream result;
QStringList list = splitParameterName(par_name);
if (list.isEmpty()) {
return {};
}
QString first_field = list[0];
result << "/" << translateSingleName(first_field);
if (list.size() > 1) {
QString remainder = list[1];
const SessionItem* p_child = findChild(item, first_field);
if (p_child) {
result << translateParameterName(p_child, remainder);
}
}
return stripDistributionNone(result.str());
}
void ModelPath::addParameterTranslator(const IParameterTranslator &translator)
{
m_special_translators.emplace_back(translator.clone());
}
QString ModelPath::getPathFromIndex(const QModelIndex &index)
QString ModelPath::getPathFromIndex(const QModelIndex& index)
{
if (index.isValid()) {
QStringList namePath;
......@@ -78,15 +33,15 @@ QString ModelPath::getPathFromIndex(const QModelIndex &index)
return QString();
}
// FIXME TODO refactor this hell, item #1623, item #1624
// TODO cover with unit tests and simplify
QModelIndex ModelPath::getIndexFromPath(const SessionModel *model, const QString &path)
QModelIndex ModelPath::getIndexFromPath(const SessionModel* model, const QString& path)
{
if (model) {
QStringList parts = path.split("/");
SessionItem *t = model->rootItem();
for(int i = 0; i < parts.length(); i++) {
if (t->modelType()==Constants::JobItemType && parts[i]==Constants::InstrumentType) {
SessionItem* t = model->rootItem();
for (int i = 0; i < parts.length(); i++) {
if (t->modelType() == Constants::JobItemType && parts[i] == Constants::InstrumentType) {
t = t->getItem(JobItem::T_INSTRUMENT);
continue;
}
......@@ -103,7 +58,8 @@ QModelIndex ModelPath::getIndexFromPath(const SessionModel *model, const QString
}
//! returns an item from relative path wrt to given parent
SessionItem *ModelPath::getItemFromPath(const QString &relPath, const SessionItem *parent)
SessionItem* ModelPath::getItemFromPath(const QString& relPath, const SessionItem* parent)
{
Q_ASSERT(parent);
QString fullPath = getPathFromIndex(parent->index()) + "/" + relPath;
......@@ -112,109 +68,29 @@ SessionItem *ModelPath::getItemFromPath(const QString &relPath, const SessionIte
//! Iterates through all the model and returns true if item is found. This is to
bool ModelPath::isValidItem(SessionModel *model, SessionItem *item, const QModelIndex &parent)
bool ModelPath::isValidItem(SessionModel* model, SessionItem* item, const QModelIndex& parent)
{
for(int i_row=0; i_row<model->rowCount(parent); ++i_row) {
for (int i_row = 0; i_row < model->rowCount(parent); ++i_row) {
QModelIndex index = model->index(i_row, 0, parent);
SessionItem *curr = model->itemForIndex(index);
if(curr == item) return true;
SessionItem* curr = model->itemForIndex(index);
if (curr == item)
return true;
bool isvalid = isValidItem(model, item, index);
if(isvalid) return isvalid;
if (isvalid)
return isvalid;
}
return false;
}
QStringList ModelPath::splitParameterName(const QString &par_name)
{
QStringList result;
for (auto& translator : m_special_translators) {
result = translator->split(par_name);
if (result.size() > 0) {
return result;
}
}
// TODO: extract as default split method
result << getFirstField(par_name);
QString remainder = stripFirstField(par_name);
if (!remainder.isEmpty()) {
result << remainder;
}
return result;
}
QString ModelPath::getFirstField(const QString &par_name)
{
QStringList par_list = par_name.split("/");
if (par_list.size()==0) return QString {};
return par_list.front();
}
QString ModelPath::stripFirstField(const QString &par_name)
{
QStringList par_list = par_name.split("/");
if (par_list.size()<2) return QString {};
par_list.removeFirst();
return par_list.join("/");
}
string ModelPath::translateSingleName(const QString &name)
{
for (auto& translator : m_special_translators) {
auto result = translator->translate(name);
if (!result.empty()) {
return result;
}
}
return name.toStdString();
}
SessionItem* ModelPath::findChild(const SessionItem *item, const QString& first_field)
{
if (item->modelType()==Constants::JobItemType) {
if (first_field==Constants::MultiLayerType) {
return item->getItem(JobItem::T_SAMPLE);
} else if (first_field==Constants::InstrumentType) {
return item->getItem(JobItem::T_INSTRUMENT);
}
}
SessionItem* p_child = item->getChildByName(first_field);
if (!p_child) { //search through group items
auto groupItems = item->getChildrenOfType(Constants::GroupItemType);
for (SessionItem* groupItem : groupItems) {
if (GroupItem *gItem = dynamic_cast<GroupItem*>(groupItem)) {
if (gItem->currentType() == first_field) {
p_child = gItem->currentItem();
break;
}
}
}
}
return p_child;
}
// TODO: item #1623, remove this hack when refactoring name translations
string ModelPath::stripDistributionNone(const string &name)
{
const string distribution_none { "/DistributionNone/Value" };
if (name.length() >= distribution_none.length() &&
name.compare(name.length()-distribution_none.length(), distribution_none.length(),
distribution_none)==0) {
return name.substr(0, name.length()-distribution_none.length());
}
return name;
}
//! Returns true when we know how to translate ParameterItem link to domain name.
// TODO: item #1623, item #1624 remove this hack when refactoring name translations
// Function is intended to disalow drag-and-drop of ParameterItem onto FitParameterItem
// for non-implemented or senseless translations (in GUI)
bool ModelPath::isTranslatable(const SessionItem *item, const QString &par_name)
bool ModelPath::isTranslatable(const SessionItem* item, const QString& par_name)
{
Q_UNUSED(item);
if(par_name.contains(Constants::DetectorType))
if (par_name.contains(Constants::DetectorType))
return false;
if(par_name.contains(Constants::DistributionSigmaFactor))
if (par_name.contains(Constants::DistributionSigmaFactor))
return false;
return true;
}
......@@ -222,9 +98,9 @@ bool ModelPath::isTranslatable(const SessionItem *item, const QString &par_name)
//! Returns ancestor of given modelType for given item.
//! For example, returns corresponding jobItem owning ParameterItem via ParameterContainer.
const SessionItem* ModelPath::ancestor(const SessionItem *item, const QString &requiredModelType)
const SessionItem* ModelPath::ancestor(const SessionItem* item, const QString& requiredModelType)
{
const SessionItem *cur = item;
const SessionItem* cur = item;
while (cur && cur->modelType() != requiredModelType)
cur = cur->parent();
......@@ -236,8 +112,8 @@ const SessionItem* ModelPath::ancestor(const SessionItem *item, const QString &r
QString ModelPath::itemPathTranslation(const SessionItem& item, const SessionItem* topItem)
{
QStringList pathList;
const SessionItem *current(&item);
while (current && current!=topItem) {
const SessionItem* current(&item);
while (current && current != topItem) {
pathList = current->translateList(pathList);
current = current->parent();
}
......
......@@ -3,7 +3,7 @@
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file GUI/coregui/Models/ModelPath.h
//! @brief Defines class ModelPath
//! @brief Defines ModelPath namespace
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
......@@ -26,47 +26,21 @@ class SessionItem;
class QModelIndex;
class SessionModel;
class BA_CORE_API_ ModelPath
namespace ModelPath
{
public:
//! retrieve value of given parameter name
static double getParameterValue(const SessionItem *item, const QString &name);
QString getPathFromIndex(const QModelIndex &index);
//! translates the given parameter name to a domain parameter name
//! name should start with a child/subitem name or be a direct parameter name
static std::string translateParameterName(const SessionItem *item, const QString &par_name);
QModelIndex getIndexFromPath(const SessionModel *model, const QString &path);
static void addParameterTranslator(const IParameterTranslator &translator);
SessionItem* getItemFromPath(const QString &relPath, const SessionItem* parent);
static QString getPathFromIndex(const QModelIndex &index);
bool isValidItem(SessionModel *model, SessionItem *item, const QModelIndex &parent);
static QModelIndex getIndexFromPath(const SessionModel *model, const QString &path);
bool isTranslatable(const SessionItem *item, const QString &par_name);
static SessionItem* getItemFromPath(const QString &relPath, const SessionItem* parent);
const SessionItem *ancestor(const SessionItem *item, const QString& requiredModelType);
static bool isValidItem(SessionModel *model, SessionItem *item, const QModelIndex &parent);
QString itemPathTranslation(const SessionItem& item, const SessionItem* topItem=0);
}
static bool isTranslatable(const SessionItem *item, const QString &par_name);
static const SessionItem *ancestor(const SessionItem *item, const QString& requiredModelType);
static QString itemPathTranslation(const SessionItem& item, const SessionItem* topItem=0);
private:
static QStringList splitParameterName(const QString& par_name);
static QString getFirstField(const QString& par_name);
static QString stripFirstField(const QString& par_name);
static std::string translateSingleName(const QString& name);
static SessionItem* findChild(const SessionItem* item, const QString& first_field);
static std::string stripDistributionNone(const std::string& name);
static std::vector<std::unique_ptr<IParameterTranslator>> m_special_translators;
};
#endif // MODELPATH_H
#endif // MODELPATH_H namespace
......@@ -17,13 +17,8 @@
#include "ParameterTranslators.h"
#include "BornAgainNamespace.h"
#include "ParticleItem.h"
#include "RotationItems.h"
#include "VectorItem.h"
#include "InterferenceFunctionItems.h"
#include "Lattice2DItems.h"
#include "GUIHelpers.h"
#include "MultiLayerItem.h"
#include <QDebug>
namespace {
const QStringList expectedRoughnessPars = QStringList() << QString::fromStdString(BornAgain::Sigma)
......@@ -31,64 +26,7 @@ const QStringList expectedRoughnessPars = QStringList() << QString::fromStdStrin
<< QString::fromStdString(BornAgain::CorrelationLength);
}
QStringList IParameterTranslator::split(const QString &par_name) const
{
QStringList result;
std::string translated_name = translate(par_name);
if (!translated_name.empty())
result << par_name;
return result;
}
std::string PositionTranslator::translate(const QString& name) const
{
QStringList name_list = name.split("/");
if (name_list.size() > 2) return {};
if (name_list.size() > 1 && name_list[0] == ParticleItem::P_POSITION) {
if (name_list[1] == VectorItem::P_X) {
return BornAgain::PositionX;
}
if (name_list[1] == VectorItem::P_Y) {
return BornAgain::PositionY;
}
if (name_list[1] == VectorItem::P_Z) {
return BornAgain::PositionZ;
}
}
return {};
}
std::string RotationTranslator::translate(const QString& name) const
{
QStringList name_list = name.split("/");
std::string separator {"/"};
if (name_list.size() > 3) return {};
if (name_list.size() == 3 && name_list[0] == Constants::TransformationType) {
if (name_list[1] == Constants::XRotationType) {
return BornAgain::XRotationType + separator + BornAgain::Angle;
}
if (name_list[1] == Constants::YRotationType) {
return BornAgain::YRotationType + separator + BornAgain::Angle;
}
if (name_list[1] == Constants::ZRotationType) {
return BornAgain::ZRotationType + separator + BornAgain::Angle;
}
if (name_list[1] == Constants::EulerRotationType) {
if (name_list[2] == EulerRotationItem::P_ALPHA) {
return BornAgain::EulerRotationType + separator + BornAgain::Alpha;
} else if (name_list[2] == EulerRotationItem::P_BETA) {
return BornAgain::EulerRotationType + separator + BornAgain::Beta;
} else if (name_list[2] == EulerRotationItem::P_GAMMA) {
return BornAgain::EulerRotationType + separator + BornAgain::Gamma;
}
}
}
return {};
}
QStringList NewPositionTranslator::translate(const QStringList& list) const
QStringList PositionTranslator::translate(const QStringList& list) const
{
if(list.back() != ParticleItem::P_POSITION)
return list;
......@@ -109,7 +47,7 @@ QStringList NewPositionTranslator::translate(const QStringList& list) const
return result;
}
QStringList NewRotationTranslator::translate(const QStringList& list) const
QStringList RotationTranslator::translate(const QStringList& list) const
{
if(list.back() != Constants::TransformationType)
return list;
......
......@@ -20,40 +20,8 @@
class SessionItem;
class IParameterTranslator {
public:
virtual ~IParameterTranslator()=default;
virtual IParameterTranslator* clone() const=0;
virtual QStringList split(const QString &par_name) const;
virtual std::string translate(const QString &name) const=0;
};
class PositionTranslator final : public IParameterTranslator {
public:
~PositionTranslator() override {}
PositionTranslator* clone() const override { return new PositionTranslator {}; }
std::string translate(const QString &name) const override;
};
class RotationTranslator final : public IParameterTranslator {
public:
~RotationTranslator() override {}
RotationTranslator* clone() const override { return new RotationTranslator {}; }
std::string translate(const QString &name) const override;
};
class IPathTranslator {
public:
// IPathTranslator(const IPathTranslator&) = delete;
// IPathTranslator& operator=(const IPathTranslator&) = delete;
virtual ~IPathTranslator()=default;
virtual IPathTranslator* clone() const=0;
......@@ -61,20 +29,20 @@ public:
virtual QStringList translate(const QStringList& list) const=0;
};
class NewPositionTranslator : public IPathTranslator {
class PositionTranslator : public IPathTranslator {
public:
~NewPositionTranslator() override {}
~PositionTranslator() override {}
NewPositionTranslator* clone() const override { return new NewPositionTranslator; }
PositionTranslator* clone() const override { return new PositionTranslator; }
virtual QStringList translate(const QStringList& list) const;
};
class NewRotationTranslator : public IPathTranslator {
class RotationTranslator : public IPathTranslator {
public:
~NewRotationTranslator() override {}
~RotationTranslator() override {}
NewRotationTranslator* clone() const override { return new NewRotationTranslator; }
RotationTranslator* clone() const override { return new RotationTranslator; }
virtual QStringList translate(const QStringList& list) const;
};
......@@ -99,5 +67,4 @@ private:
int getLayerIndex(QString layerName) const;
};
#endif // PARAMETERTRANSLATORS_H
......@@ -85,19 +85,12 @@ void ParameterItem::restoreFromBackup()
//! Returns true if item can be used to drag-and-drop to FitParameterContainer.
//! In other words, if translation to domain name is implemented and valid.
// TODO, item #1623, consider the necessity of method after all fit parameter translations
// are fixed
bool ParameterItem::isFittable() const
{
const SessionItem *jobItem = ModelPath::ancestor(this, Constants::JobItemType);
Q_ASSERT(jobItem);
return ModelPath::isTranslatable(jobItem, FitParameterHelper::getParameterItemPath(this));
// TODO, item #1623, consider equivalent implementation instead
// if(getItemValue(P_DOMAIN).toString() == Constants::FITPAR_UNFITTABLE)
// return false;
// return true;
}
// ----------------------------------------------------------------------------
......
......@@ -36,6 +36,9 @@ QString removeLeadingSlash(const QString& name )
}
void handleItem(SessionItem* tree, const SessionItem* source);
void populateDomainLinks(SessionItem* container);
}
void ParameterTreeUtils::createParameterTree(JobItem* jobItem)
......@@ -51,7 +54,7 @@ void ParameterTreeUtils::createParameterTree(JobItem* jobItem)
// Provides all items in "JobItem/Parameter Tree Container" with domain links already
// at the stage of ParameterTree creation. It is necessary for validation, in Release mode
// it will lead for unnecessary large project files.
ParameterTreeUtils::populateDomainLinks(container);
populateDomainLinks(container);
#endif
}
......@@ -178,15 +181,17 @@ QString ParameterTreeUtils::parameterNameToDomainName(const QString& parName,
return {};
}
namespace {
//! For every ParameterItem in a container creates a link to the domain.
void ParameterTreeUtils::populateDomainLinks(SessionItem* container)
void populateDomainLinks(SessionItem* container)
{
if(container->modelType() != Constants::ParameterContainerType)
throw GUIHelpers::Error("ParameterTreeUtils::populateParameterContainer() -> Error. "
"Not a ParameterContainerType.");
visitParameterContainer(container, [container](ParameterItem* parItem)
ParameterTreeUtils::visitParameterContainer(container, [container](ParameterItem* parItem)
{
QString translation = "*/" + ModelPath::itemPathTranslation(*parItem->linkedItem(),
container->parent());
......@@ -194,8 +199,6 @@ void ParameterTreeUtils::populateDomainLinks(SessionItem* container)
});
}
namespace {
void handleItem(SessionItem* tree, const SessionItem* source)
{
if (tree->modelType() == Constants::ParameterLabelType) {
......
......@@ -52,8 +52,6 @@ BA_CORE_API_ QString domainNameToParameterName(const QString& domainName, const
BA_CORE_API_ QString parameterNameToDomainName(const QString& parName, const SessionItem* source);
BA_CORE_API_ void populateDomainLinks(SessionItem* container);
}
#endif // PARAMETERTREEUTILS_H
......@@ -30,18 +30,14 @@ ParticleCompositionItem::ParticleCompositionItem()
getItem(ParticleItem::P_ABUNDANCE)->setLimits(RealLimits::limited(0.0, 1.0));
getItem(ParticleItem::P_ABUNDANCE)->setDecimals(3);
addGroupProperty(ParticleItem::P_POSITION, Constants::VectorType);
PositionTranslator position_translator;
ModelPath::addParameterTranslator(position_translator);
registerTag(T_PARTICLES, 0, -1, QStringList() << Constants::ParticleType <<
Constants::ParticleCoreShellType << Constants::ParticleCompositionType);
setDefaultTag(T_PARTICLES);
registerTag(ParticleItem::T_TRANSFORMATION, 0, 1, QStringList() << Constants::TransformationType);
RotationTranslator rotation_translator;
ModelPath::addParameterTranslator(rotation_translator);
addTranslator(NewPositionTranslator());
addTranslator(NewRotationTranslator());
addTranslator(PositionTranslator());
addTranslator(RotationTranslator());
mapper()->setOnParentChange(
[this](SessionItem *parent) {
......
......@@ -30,17 +30,13 @@ ParticleCoreShellItem::ParticleCoreShellItem()
getItem(ParticleItem::P_ABUNDANCE)->setLimits(RealLimits::limited(0.0, 1.0));
getItem(ParticleItem::P_ABUNDANCE)->setDecimals(3);
addGroupProperty(ParticleItem::P_POSITION, Constants::VectorType);
PositionTranslator position_translator;
ModelPath::addParameterTranslator(position_translator);
registerTag(T_CORE, 0, 1, QStringList() << Constants::ParticleType);
registerTag(T_SHELL, 0, 1, QStringList() << Constants::ParticleType);
registerTag(ParticleItem::T_TRANSFORMATION, 0, 1, QStringList() << Constants::TransformationType);
RotationTranslator rotation_translator;
ModelPath::addParameterTranslator(rotation_translator);
addTranslator(NewPositionTranslator());
addTranslator(NewRotationTranslator());
addTranslator(PositionTranslator());
addTranslator(RotationTranslator());
mapper()->setOnParentChange(
[this](SessionItem*)
......
......@@ -39,16 +39,12 @@ ParticleItem::ParticleItem()
addProperty(P_ABUNDANCE, 1.0)->setLimits(RealLimits::limited(0.0, 1.0));
getItem(P_ABUNDANCE)->setDecimals(3);
addGroupProperty(P_POSITION, Constants::VectorType);
PositionTranslator position_translator;
ModelPath::addParameterTranslator(position_translator);
registerTag(T_TRANSFORMATION, 0, 1, QStringList() << Constants::TransformationType);
setDefaultTag(T_TRANSFORMATION);
RotationTranslator rotation_translator;
ModelPath::addParameterTranslator(rotation_translator);
addTranslator(NewPositionTranslator());
addTranslator(NewRotationTranslator());
addTranslator(PositionTranslator());
addTranslator(RotationTranslator());
mapper()->setOnParentChange(
[this](SessionItem* parentItem) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment