diff --git a/GUI/coregui/Models/ModelPath.cpp b/GUI/coregui/Models/ModelPath.cpp index ceccf3258cb01c07ed120adab746eb620d603ba2..934265c577fa6cd4d974cbc6d936d17d620aebda 100644 --- a/GUI/coregui/Models/ModelPath.cpp +++ b/GUI/coregui/Models/ModelPath.cpp @@ -25,38 +25,6 @@ using std::string; std::vector<std::unique_ptr<IParameterTranslator>> ModelPath::m_special_translators {}; -QStringList ModelPath::getParameterTreeList(const SessionItem *item, QString prefix) -{ - QStringList result; - if (item->modelType() == Constants::PropertyType - && item->value().type() == QVariant::Double && item->itemName() != ParticleItem::P_ABUNDANCE) { - if (prefix.endsWith("/")) - prefix = prefix.mid(0, prefix.size()-1); - result << prefix; - } - else { - if (item->hasChildren()) { - for (auto p_child : item->childItems()) { - if(p_child->isVisible()) { - if (p_child->modelType() == Constants::GroupItemType) { - if (const GroupItem *groupItem = dynamic_cast<const GroupItem*>(p_child)) { - if (const SessionItem *subItem = groupItem->currentItem()) { - QString child_prefix = prefix + subItem->itemName() + QString("/"); - result << getParameterTreeList(subItem, child_prefix); - } - } - } else { - QString child_name = p_child->itemName(); - QString child_prefix = prefix + child_name + QString("/"); - result << getParameterTreeList(p_child, child_prefix); - } - } - } - } - } - return result; -} - double ModelPath::getParameterValue(const SessionItem *item, const QString &name) { QString head = getFirstField(name); diff --git a/GUI/coregui/Models/ModelPath.h b/GUI/coregui/Models/ModelPath.h index fef0fad55cf3cf345be2ae3ab601377542056e0f..fec030aed229039d27f89d62af4bc65525ad1402 100644 --- a/GUI/coregui/Models/ModelPath.h +++ b/GUI/coregui/Models/ModelPath.h @@ -29,10 +29,6 @@ class SessionModel; class BA_CORE_API_ ModelPath { public: - //! retrieves a list of all parameter names in the SessionItem tree starting - //! with this node and prefixes them - static QStringList getParameterTreeList(const SessionItem *item, QString prefix = ""); - //! retrieve value of given parameter name static double getParameterValue(const SessionItem *item, const QString &name); diff --git a/GUI/coregui/Models/ParameterTreeUtils.cpp b/GUI/coregui/Models/ParameterTreeUtils.cpp index 27c2f76732449542ace9e2ed599e17b35439b921..a69834d7d69b221dcaab7c1d71be71d2d722df50 100644 --- a/GUI/coregui/Models/ParameterTreeUtils.cpp +++ b/GUI/coregui/Models/ParameterTreeUtils.cpp @@ -27,10 +27,15 @@ #include "FitParameterHelper.h" #include "SampleModel.h" #include <QStack> -#include <QDebug> namespace { - void handleItem(SessionItem* tree, const SessionItem* source); + +QString removeLeadingSlash(const QString& name ) +{ + return name.indexOf('/') == 0 ? name.mid(1) : name; +} + +void handleItem(SessionItem* tree, const SessionItem* source); } void ParameterTreeUtils::createParameterTree(JobItem* jobItem) @@ -91,50 +96,87 @@ void ParameterTreeUtils::visitParameterContainer(SessionItem* container, QStringList ParameterTreeUtils::parameterTreeNames(const SessionItem* source) { - Q_ASSERT(source); QStringList result; - SampleModel model; - SessionItem* container = model.insertNewItem(Constants::ParameterContainerType); + for(auto pair : parameterNameTranslation(source)) + result << pair.first; - populateParameterContainer(container, source); + return result; +} - visitParameterContainer(container, [&](ParameterItem* parItem) - { - result.push_back(FitParameterHelper::getParameterItemPath(parItem)); - }); +//! Creates domain translated list of parameter names for source item. - std::reverse(result.begin(), result.end()); +QStringList ParameterTreeUtils::translatedParameterTreeNames(const SessionItem* source) +{ + QStringList result; + + for(auto pair : parameterNameTranslation(source)) + result << pair.second; return result; } -//! Creates domain translated list of parameter names for source item. +//! Correspondance of parameter name to translated name for all properties found in source +//! in its children. -QStringList ParameterTreeUtils::translatedParameterTreeNames(const SessionItem* source) +QVector<QPair<QString, QString>> +ParameterTreeUtils::parameterNameTranslation(const SessionItem* source) { Q_ASSERT(source); - QStringList result; + QVector<QPair<QString, QString>> result; + + // Create container with ParameterItem's of given source item SampleModel model; SessionItem* container = model.insertNewItem(Constants::ParameterContainerType); - populateParameterContainer(container, source); + // Iterate through all ParameterItems and retrieve necessary data. visitParameterContainer(container, [&](ParameterItem* parItem) { + // TODO replace with the method from ModelPath + QString parPath = FitParameterHelper::getParameterItemPath(parItem); + QString relPath = source->displayName() + "/" + parItem->getItemValue(ParameterItem::P_LINK).toString(); SessionItem *linkedItem = ModelPath::getItemFromPath(relPath, source); - result.push_back(ModelPath::itemPathTranslation(*linkedItem, source->parent())); - }); + QString translation = ModelPath::itemPathTranslation(*linkedItem, source->parent()); + + result.push_back(QPair<QString, QString>(parPath, translation)); + }); std::reverse(result.begin(), result.end()); return result; +} + +//! Converts domain name to parameterItem name. Parameter name should belong to item or +//! one of its children. +QString ParameterTreeUtils::domainNameToParameterName(const QString& domainName, + const SessionItem* source) +{ + QString domain = removeLeadingSlash(domainName); + for(auto pair : parameterNameTranslation(source)) {// parName, domainName + if(pair.second == domain) + return pair.first; + } + + return {}; } +//! Converts parameter name to domain name. Parameter name should belong to item or +//! one of its children. + +QString ParameterTreeUtils::parameterNameToDomainName(const QString& parName, + const SessionItem* source) +{ + for(auto pair : parameterNameTranslation(source)) // parName, domainName + if(pair.first == parName) + return "/"+pair.second; + + return {}; +} //! For every ParameterItem in a container creates a link to the domain. @@ -213,6 +255,3 @@ void handleItem(SessionItem* tree, const SessionItem* source) } } // namespace - - - diff --git a/GUI/coregui/Models/ParameterTreeUtils.h b/GUI/coregui/Models/ParameterTreeUtils.h index d1a24cd5983427d1ee7689169dee918c504e1b3c..d27a1a5498965bbf9bfc2fa9ce7c4f22cac42f08 100644 --- a/GUI/coregui/Models/ParameterTreeUtils.h +++ b/GUI/coregui/Models/ParameterTreeUtils.h @@ -20,6 +20,7 @@ #include "WinDllMacros.h" #include <functional> #include <QString> +#include <QPair> class JobItem; class SessionItem; @@ -43,6 +44,14 @@ BA_CORE_API_ QStringList parameterTreeNames(const SessionItem* source); BA_CORE_API_ QStringList translatedParameterTreeNames(const SessionItem* source); +BA_CORE_API_ QStringList translateParameterName(const QString& name, const SessionItem* source); + +BA_CORE_API_ QVector<QPair<QString, QString>> parameterNameTranslation(const SessionItem* source); + +BA_CORE_API_ QString domainNameToParameterName(const QString& domainName, const SessionItem* source); + +BA_CORE_API_ QString parameterNameToDomainName(const QString& parName, const SessionItem* source); + BA_CORE_API_ void populateDomainLinks(SessionItem* container); } diff --git a/GUI/coregui/Models/ParticleDistributionItem.cpp b/GUI/coregui/Models/ParticleDistributionItem.cpp index 6969f6d37d41f6da5968ad1af06764c7de4929c0..c854bc010dfc51e79fe88c6dcfd38d4c039b034f 100644 --- a/GUI/coregui/Models/ParticleDistributionItem.cpp +++ b/GUI/coregui/Models/ParticleDistributionItem.cpp @@ -56,10 +56,6 @@ ParticleDistributionItem::ParticleDistributionItem() } ); } -ParticleDistributionItem::~ParticleDistributionItem() -{ -} - std::unique_ptr<ParticleDistribution> ParticleDistributionItem::createParticleDistribution() const { auto children = childItems(); @@ -77,7 +73,10 @@ std::unique_ptr<ParticleDistribution> ParticleDistributionItem::createParticleDi auto prop = getItemValue(ParticleDistributionItem::P_DISTRIBUTED_PARAMETER) .value<ComboProperty>(); QString par_name = prop.getValue(); - std::string domain_par = ModelPath::translateParameterName(this, par_name); + + std::string domain_par = ParameterTreeUtils::parameterNameToDomainName( + par_name, getItems(T_PARTICLES).front()).toStdString(); + int nbr_samples = distr_item->getItemValue(DistributionItem::P_NUMBER_OF_SAMPLES).toInt(); double sigma_factor @@ -97,7 +96,7 @@ void ParticleDistributionItem::updateParameterList() auto combo_prop = par_prop.value<ComboProperty>(); QString cached_par = combo_prop.getCachedValue(); if (!combo_prop.cacheContainsGUIValue()) { - auto gui_name = translateParameterNameToGUI_V2(cached_par); + auto gui_name = translateParameterNameToGUI(cached_par); if (!gui_name.isEmpty()) { cached_par = gui_name; combo_prop.setCachedValue(cached_par); @@ -105,7 +104,7 @@ void ParticleDistributionItem::updateParameterList() } } QString selected_par = combo_prop.getValue(); - QStringList par_names = childParameterNames(); + QStringList par_names = QStringList() << NO_SELECTION << childParameterNames(); par_names.removeAll(ParticleItem::P_ABUNDANCE); auto updated_prop = ComboProperty(par_names); updated_prop.setCachedValue(cached_par); @@ -122,59 +121,21 @@ void ParticleDistributionItem::updateParameterList() QStringList ParticleDistributionItem::childParameterNames() const { - QStringList result; - QVector<SessionItem*> children = getItems(); - if (children.size() > 1) { - Q_ASSERT(0); - return result; - } - if (children.size() == 0) { - result << NO_SELECTION; - return result; - } -// QString prefix = children.front()->displayName() + QString("/"); -// result = ModelPath::getParameterTreeList(children.front(), prefix); - result = ParameterTreeUtils::parameterTreeNames(children.front()); + if(getItems(T_PARTICLES).size() == 0) + return {}; - result.prepend(NO_SELECTION); - return result; -} + Q_ASSERT(getItems(T_PARTICLES).size() == 1); -QString ParticleDistributionItem::translateParameterNameToGUI(const QString& par_name) -{ - QStringList gui_par_list = ModelPath::getParameterTreeList(this); - for (QString gui_par_name : gui_par_list) { - QString domain_par_name = QString::fromStdString( - ModelPath::translateParameterName(this, gui_par_name)); - if (domain_par_name == par_name) { - return gui_par_name; - } - } - return {}; + return ParameterTreeUtils::parameterTreeNames(getItems(T_PARTICLES).front()); } -QString ParticleDistributionItem::translateParameterNameToGUI_V2(const QString& par_name) +QString ParticleDistributionItem::translateParameterNameToGUI(const QString& domainName) { - QString domainName = par_name; - int firstSlash = par_name.indexOf('/'); - if(firstSlash==0) - domainName = domainName.mid(firstSlash + 1); - - QStringList parameterNames = childParameterNames(); - parameterNames.removeAll(NO_SELECTION); - if(parameterNames.isEmpty()) + if(getItems(T_PARTICLES).size() == 0) return {}; - QStringList translatedNames - = ParameterTreeUtils::translatedParameterTreeNames(getItems(T_PARTICLES).front()); - Q_ASSERT(parameterNames.size() == translatedNames.size()); + Q_ASSERT(getItems(T_PARTICLES).size() > 0); - int index(0); - for(QString translation : translatedNames) { - if(translation == domainName) - return parameterNames.at(index); - ++index; - } - return {}; + return ParameterTreeUtils::domainNameToParameterName(domainName, getItems(T_PARTICLES).front()); } diff --git a/GUI/coregui/Models/ParticleDistributionItem.h b/GUI/coregui/Models/ParticleDistributionItem.h index 24d31f3a47b9ba61e04aba52d257e82c6e53190a..e0602dc37eec4f67756cb9c2c2ce88eb6230ea8d 100644 --- a/GUI/coregui/Models/ParticleDistributionItem.h +++ b/GUI/coregui/Models/ParticleDistributionItem.h @@ -28,16 +28,14 @@ public: static const QString NO_SELECTION; static const QString T_PARTICLES; ParticleDistributionItem(); - virtual ~ParticleDistributionItem(); std::unique_ptr<ParticleDistribution> createParticleDistribution() const; - QString translateParameterNameToGUI(const QString& par_name); - QString translateParameterNameToGUI_V2(const QString& par_name); void updateParameterList(); private: QStringList childParameterNames() const; + QString translateParameterNameToGUI(const QString& domainName); }; #endif // PARTICLEDISTRIBUTIONITEM_H