Skip to content
Snippets Groups Projects
ParameterTreeUtils.cpp 3.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • //  ************************************************************************************************
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //
    
    //  BornAgain: simulate and fit reflection and scattering
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //
    
    //! @file      GUI/Model/Job/ParameterTreeUtils.cpp
    
    //! @brief     Implements ParameterTreeUtils namespace
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //!
    
    //! @homepage  http://www.bornagainproject.org
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //! @license   GNU General Public License v3 or higher (see COPYING)
    
    //! @copyright Forschungszentrum Jülich GmbH 2018
    //! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    //
    
    //  ************************************************************************************************
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    
    
    #include "GUI/Model/Job/ParameterTreeUtils.h"
    #include "GUI/Model/Fit/ParameterTreeItems.h"
    #include "GUI/Model/Group/GroupItem.h"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    #include "GUI/Model/Group/PropertyItem.h"
    
    #include "GUI/Model/Instrument/InstrumentItems.h"
    #include "GUI/Model/Job/JobItem.h"
    #include "GUI/Model/Material/MaterialItemContainer.h"
    #include "GUI/Model/Sample/MultiLayerItem.h"
    
    #include "GUI/Model/Session/ModelPath.h"
    
    #include "GUI/Util/Error.h"
    
    #include <boost/polymorphic_cast.hpp>
    using boost::polymorphic_downcast;
    
    
    namespace {
    
    void handleItem(ParameterContainerItem* container, SessionItem* tree, const SessionItem* source)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        if (tree->hasModelType<ParameterLabelItem>())
    
            tree->setDisplayName(source->itemName());
    
    
        else if (tree->hasModelType<ParameterItem>()) {
    
    David Li's avatar
    David Li committed
            tree->setDisplayName(source->itemName());
    
    
            double sourceValue = source->value().toDouble();
            tree->setValue(QVariant(sourceValue));
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            QString path = GUI::Model::Path::getPathFromIndex(source->index());
    
            int firstSlash = path.indexOf('/');
            path = path.mid(firstSlash + 1);
    
            auto* parItem = polymorphic_downcast<ParameterItem*>(tree);
    
            parItem->setLink(path);
    
            container->setBackupValue(parItem->link(), sourceValue);
    
    David Li's avatar
    David Li committed
            return;
    
    David Li's avatar
    David Li committed
            return;
    
        for (SessionItem* child : source->children()) {
    
    David Li's avatar
    David Li committed
            if (child->isVisible() && child->isEnabled()) {
    
                if (child->hasModelType<PropertyItem>()) {
    
    David Li's avatar
    David Li committed
                    if (child->value().type() == QVariant::Double) {
    
                        auto* branch = tree->model()->insertItem<ParameterItem>(tree);
    
                        handleItem(container, branch, child);
    
                } else if (child->hasModelType<GroupItem>()) {
    
                    SessionItem* currentItem = dynamic_cast<GroupItem*>(child)->currentItem();
    
                    if (currentItem && currentItem->numberOfChildren() > 0) {
    
                        auto* branch = tree->model()->insertItem<ParameterLabelItem>(tree);
    
                        handleItem(container, branch, currentItem);
    
    David Li's avatar
    David Li committed
                } else {
    
                    auto* branch = tree->model()->insertItem<ParameterLabelItem>(tree);
    
                    handleItem(container, branch, child);
    
    //! Populates ParameterContainer with ParameterItem's corresponding to all properties found
    //! in a source item.
    
    
    void populateParameterContainer(ParameterContainerItem* container, const SessionItem* source)
    
        auto* sourceLabel = container->model()->insertItem<ParameterLabelItem>(container);
    
        handleItem(container, sourceLabel, source);
    
    void GUI::Model::ParameterTreeUtils::createParameterTree(JobItem* jobItem)
    
        auto* container = jobItem->parameterContainerItem();
        if (!container)
            container = jobItem->createParameterContainerItem();
    
        populateParameterContainer(container, jobItem->materialContainerItem());
    
        populateParameterContainer(container, jobItem->sampleItem());
    
        populateParameterContainer(container, jobItem->instrumentItem());