Skip to content
Snippets Groups Projects
SampleEditorController.cpp 16.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • //  ************************************************************************************************
    //
    //  BornAgain: simulate and fit reflection and scattering
    //
    
    //! @file      GUI/View/SampleDesigner/SampleEditorController.cpp
    
    //! @brief     Implements class SampleEditorController
    //!
    //! @homepage  http://www.bornagainproject.org
    //! @license   GNU General Public License v3 or higher (see COPYING)
    //! @copyright Forschungszentrum Jülich GmbH 2021
    //! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
    //
    //  ************************************************************************************************
    
    
    #include "GUI/View/SampleDesigner/SampleEditorController.h"
    
    #include "GUI/Model/Item/InterferenceItems.h"
    #include "GUI/Model/Item/LayerItem.h"
    #include "GUI/Model/Item/MesoCrystalItem.h"
    #include "GUI/Model/Item/MultiLayerItem.h"
    #include "GUI/Model/Item/ParticleCompositionItem.h"
    #include "GUI/Model/Item/ParticleCoreShellItem.h"
    #include "GUI/Model/Item/ParticleItem.h"
    #include "GUI/Model/Item/ParticleLayoutItem.h"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    #include "GUI/Model/MakeItem/FormFactorItemCatalog.h"
    #include "GUI/Model/Model/MaterialModel.h"
    #include "GUI/Model/Project/ProjectDocument.h"
    
    #include "GUI/Model/Types/UIntDescriptor.h"
    #include "GUI/View/Edit/DoubleSpinBox.h"
    #include "GUI/View/SampleDesigner/InterferenceForm.h"
    
    #include "GUI/View/SampleDesigner/LatticeTypeSelectionForm.h"
    
    #include "GUI/View/SampleDesigner/LayerForm.h"
    #include "GUI/View/SampleDesigner/MaterialInplaceForm.h"
    #include "GUI/View/SampleDesigner/MesoCrystalForm.h"
    #include "GUI/View/SampleDesigner/MultiLayerForm.h"
    #include "GUI/View/SampleDesigner/ParticleCompositionForm.h"
    #include "GUI/View/SampleDesigner/ParticleCoreShellForm.h"
    #include "GUI/View/SampleDesigner/ParticleLayoutForm.h"
    #include "GUI/View/SampleDesigner/SampleEditorCommands.h"
    
    #include "Sample/HardParticle/FormFactorCylinder.h"
    
    SampleEditorController::SampleEditorController(ProjectDocument* document, MultiLayerItem* multi)
    
        : m_multiLayerItem(multi)
        , m_multiLayerForm(nullptr)
        , m_document(document)
    
    {
    }
    
    void SampleEditorController::setMultiLayerForm(MultiLayerForm* view)
    {
        m_multiLayerForm = view;
    }
    
    MultiLayerForm* SampleEditorController::multiLayerForm() const
    {
        return m_multiLayerForm;
    }
    
    MultiLayerItem* SampleEditorController::multiLayerItem() const
    {
        return m_multiLayerItem;
    }
    
    void SampleEditorController::addLayer(LayerItem* before)
    {
        const int rowInMultiLayer = (before != nullptr) ? m_multiLayerItem->layers().indexOf(before)
                                                        : m_multiLayerItem->layers().size();
    
    
        // -- find a color for the new layer
        QColor color;
        auto unusedColors = LayerEditorUtils::predefinedLayerColors();
    
        for (auto* l : m_multiLayerItem->layers())
    
            unusedColors.removeAll(l->color());
        if (!unusedColors.isEmpty())
            color = unusedColors.first();
        else {
            // search for a color which has been used the less, and which is not the same as in the
            // layers above and below
            QMap<QString, int> usage;
    
            for (auto* l : m_multiLayerItem->layers())
    
                usage[l->color().name()] += 1;
    
            auto sortedByUsage = LayerEditorUtils::predefinedLayerColors();
            std::stable_sort(
                sortedByUsage.begin(), sortedByUsage.end(),
                [&](const QColor& a, const QColor& b) { return usage[a.name()] < usage[b.name()]; });
    
    
            const QColor above = (rowInMultiLayer > 0)
                                     ? m_multiLayerItem->layers()[rowInMultiLayer - 1]->color()
                                     : QColor();
            const QColor below = (rowInMultiLayer < m_multiLayerItem->layers().size())
                                     ? m_multiLayerItem->layers()[rowInMultiLayer]->color()
                                     : QColor();
    
    
                if (col != above && col != below) {
                    color = col;
                    break;
                }
        }
    
        // - create new layer
    
        LayerItem* layer = m_multiLayerItem->addLayer(rowInMultiLayer);
    
        layer->setMaterial(materialModel()->defaultMaterial());
    
        layer->setColor(color);
    
    
        ASSERT(m_multiLayerForm);
        m_multiLayerForm->onLayerAdded(layer);
    
        m_multiLayerForm->updateUnits();
    
    
        // expand the new layer's form for better workflow
        for (auto* c : m_multiLayerForm->findChildren<LayerForm*>())
            if (c->layerItem() == layer)
                c->expand();
    
    }
    
    void SampleEditorController::addLayout(LayerForm* layerItemWidget)
    {
    
        auto* newLayoutItem = layerItemWidget->layerItem()->addLayout();
    
        layerItemWidget->onLayoutAdded(newLayoutItem);
    
        m_multiLayerForm->updateUnits();
    
    
        for (auto* layoutForms : layerItemWidget->findChildren<ParticleLayoutForm*>())
            layoutForms->updateTitle(layerItemWidget->layerItem());
    
    }
    
    void SampleEditorController::removeLayer(LayerItem* layerItem)
    {
        m_undoStack.push(new CommandRemoveLayer(this, layerItem));
    }
    
    void SampleEditorController::removeLayout(LayerForm* layerItemWidget, ParticleLayoutItem* layout)
    {
    
        emit aboutToRemoveItem(layout);
    
        layerItemWidget->onAboutToRemoveLayout(layout);
        layerItemWidget->layerItem()->removeLayout(layout);
    
    
        for (auto* layoutForms : layerItemWidget->findChildren<ParticleLayoutForm*>())
            layoutForms->updateTitle(layerItemWidget->layerItem());
    
    void SampleEditorController::addParticle(ParticleLayoutItem* layoutItem,
                                             FormFactorItemCatalog::Type formFactorType)
    
        auto* newParticle = createAndInitParticle(formFactorType);
    
        layoutItem->addParticle(newParticle);
    
        emit modified();
    
        //  search for particle layout widget for notification
        ASSERT(m_multiLayerForm);
        for (auto* w : m_multiLayerForm->findChildren<ParticleLayoutForm*>())
            if (w->layoutItem() == layoutItem)
                w->onParticleAdded(newParticle);
        m_multiLayerForm->updateUnits();
    }
    
    void SampleEditorController::addParticle(ParticleLayoutItem* layoutItem,
                                             ItemWithParticlesCatalog::Type type)
    {
    
        auto* newItem = createAndInitParticle(type);
    
        layoutItem->addParticle(newItem);
    
        //  search for particle layout widget for notification
        ASSERT(m_multiLayerForm);
    
        for (auto* w : m_multiLayerForm->findChildren<ParticleLayoutForm*>())
    
            if (w->layoutItem() == layoutItem)
                w->onParticleAdded(newItem);
    
        m_multiLayerForm->updateUnits();
    
    }
    
    void SampleEditorController::addParticle(ParticleCompositionItem* compositionItem,
    
                                             ItemWithParticlesCatalog::Type type)
    
        auto* newItem = createAndInitParticle(type);
    
        compositionItem->addParticle(newItem);
    
        //  search for composition widget for notification
        ASSERT(m_multiLayerForm);
    
        for (auto* c : m_multiLayerForm->findChildren<ParticleCompositionForm*>())
    
            if (c->compositionItem() == compositionItem)
                c->onParticleAdded(newItem);
    
        m_multiLayerForm->updateUnits();
    
    void SampleEditorController::addParticle(ParticleCompositionItem* compositionItem,
                                             FormFactorItemCatalog::Type formFactorType)
    {
    
        auto* newParticle = createAndInitParticle(formFactorType);
    
        compositionItem->addParticle(newParticle);
    
        emit modified();
    
        //  search for composition widget for notification
        ASSERT(m_multiLayerForm);
        for (auto* c : m_multiLayerForm->findChildren<ParticleCompositionForm*>())
            if (c->compositionItem() == compositionItem)
                c->onParticleAdded(newParticle);
        m_multiLayerForm->updateUnits();
    }
    
    
    ItemWithParticles*
    SampleEditorController::createAndInitParticle(FormFactorItemCatalog::Type formFactorType) const
    {
    
        auto* newParticle = new ParticleItem(materialModel());
    
        newParticle->setFormFactor(FormFactorItemCatalog::create(formFactorType));
        newParticle->setMaterial(materialModel()->defaultMaterial());
        return newParticle;
    }
    
    ItemWithParticles*
    SampleEditorController::createAndInitParticle(ItemWithParticlesCatalog::Type itemType) const
    {
    
        auto* newItem = ItemWithParticlesCatalog::create(itemType, materialModel());
    
        if (auto* p = dynamic_cast<ItemWithMaterial*>(newItem))
    
            p->setMaterial(materialModel()->defaultMaterial());
    
        if (auto* cs = dynamic_cast<ParticleCoreShellItem*>(newItem)) {
            cs->createCore(materialModel());
            cs->createShell(materialModel());
            cs->core()->setFormFactor(new CylinderItem());
            cs->shell()->setFormFactor(new CylinderItem());
        }
    
        if (auto* meso = dynamic_cast<MesoCrystalItem*>(newItem); meso && meso->basisParticle())
    
            if (auto* p = dynamic_cast<ItemWithMaterial*>(meso->basisParticle()))
    
                p->setMaterial(materialModel()->defaultMaterial());
    
        return newItem;
    }
    
    
    void SampleEditorController::setCoreFormFactor(ParticleCoreShellForm* widget,
    
                                                   FormFactorItemCatalog::Type type)
    
    {
        auto* particleCoreShell = widget->coreShellItem();
    
        if (particleCoreShell->core() == nullptr)
    
            particleCoreShell->createCore(materialModel());
    
        particleCoreShell->core()->setFormFactor(FormFactorItemCatalog::create(type));
    
        widget->createCoreWidgets();
    
        m_multiLayerForm->updateUnits();
    
        emit modified();
    
    }
    
    void SampleEditorController::setShellFormFactor(ParticleCoreShellForm* widget,
    
                                                    FormFactorItemCatalog::Type type)
    
    {
        auto* particleCoreShell = widget->coreShellItem();
    
        if (particleCoreShell->shell() == nullptr)
    
            particleCoreShell->createShell(materialModel());
    
        particleCoreShell->shell()->setFormFactor(FormFactorItemCatalog::create(type));
    
        widget->createShellWidgets();
    
        m_multiLayerForm->updateUnits();
    
        emit modified();
    
    void SampleEditorController::removeParticle(ItemWithParticles* itemToRemove)
    
        ASSERT(m_multiLayerForm);
    
        for (auto* layoutForm : m_multiLayerForm->findChildren<ParticleLayoutForm*>())
            if (layoutForm->layoutItem()->particles().contains(itemToRemove)) {
                layoutForm->onAboutToRemoveParticle(itemToRemove);
    
                emit aboutToRemoveItem(itemToRemove);
                layoutForm->layoutItem()->removeParticle(itemToRemove);
                emit modified();
                return;
            }
    
        for (auto* c : m_multiLayerForm->findChildren<ParticleCompositionForm*>())
            if (c->compositionItem()->particles().contains(itemToRemove)) {
                c->onAboutToRemoveParticle(itemToRemove);
    
                emit aboutToRemoveItem(itemToRemove);
                c->compositionItem()->removeParticle(itemToRemove);
                emit modified();
                return;
            }
    
    }
    
    void SampleEditorController::setDouble(double newValue, DoubleDescriptor d)
    {
        m_undoStack.push(new CommandChangeValue(d.label, this, d.get(), newValue, d.path()));
        d.set(newValue);
    
        emit modified();
    
    }
    
    void SampleEditorController::setDoubleFromUndo(double newValue, const QString& path)
    {
        ASSERT(m_multiLayerForm);
    
        DoubleSpinBox* spinBox = nullptr;
    
        for (auto* s : m_multiLayerForm->findChildren<DoubleSpinBox*>()) {
    
            if (s->valueDescriptor().path() == path) {
                spinBox = s;
                break;
            }
        }
    
        if (!spinBox)
            return;
    
        spinBox->valueDescriptor().set(newValue);
    
        m_multiLayerForm->ensureVisible(spinBox);
        QSignalBlocker b(spinBox);
        spinBox->setBaseValue(newValue);
        spinBox->setFocus();
        spinBox->selectAll();
    
    }
    
    void SampleEditorController::setInt(int newValue, UIntDescriptor d)
    {
        d.set(newValue);
    
        emit modified();
    
    void SampleEditorController::setCurrentIndex(AbstractSelectionContainerForm* widget, int index,
    
                                                 const AbstractSelectionDescriptor& d)
    
    {
        d.setCurrentIndex(index);
    
        widget->createContent();
    
        m_multiLayerForm->updateUnits();
    
        emit modified();
    
    QUndoStack* SampleEditorController::undoStack()
    {
        return &m_undoStack;
    }
    
    
    MaterialModel* SampleEditorController::materialModel() const
    {
    
        return &m_multiLayerItem->materialItems();
    
    }
    
    ProjectDocument* SampleEditorController::projectDocument() const
    {
        return m_document;
    
    void SampleEditorController::selectMaterial(ItemWithMaterial* item,
                                                const QString& newMaterialIdentifier)
    {
        item->setMaterial(newMaterialIdentifier);
    
        //  update Layer title
        ASSERT(m_multiLayerForm);
    
        for (auto* c : m_multiLayerForm->findChildren<LayerForm*>())
    
            if (c->layerItem() == item)
                c->updateTitle();
    
        // #baLayerEditor notify all material users (update link info)
    
        emit modified();
    
    void SampleEditorController::setMaterialValue(ItemWithMaterial* item, double newValue,
    
                                                  DoubleDescriptor d)
    {
        setDouble(newValue, d);
    
    
        // -- notify all other users of this material (update values in the UI)
        ASSERT(m_multiLayerForm);
    
        for (auto* c : m_multiLayerForm->findChildren<MaterialInplaceForm*>())
    
            if (c->itemWithMaterial() != item
                && c->itemWithMaterial()->materialIdentifier() == item->materialIdentifier())
                c->updateValues();
    
        emit modified();
    
    void SampleEditorController::setDensityRelatedValue(InterferenceItem* interferenceItem,
                                                        double newValue, DoubleDescriptor d)
    {
        setDouble(newValue, d);
    
    
        // -- notify the containing particle layout UI about changed value
        ASSERT(m_multiLayerForm);
        for (auto* c : m_multiLayerForm->findChildren<ParticleLayoutForm*>())
            if (c->layoutItem()->interference() == interferenceItem) {
                c->updateDensityValue();
                break;
            }
    
    void SampleEditorController::onStartingToMoveLayer()
    {
        ASSERT(m_multiLayerForm);
        m_multiLayerForm->showAddLayerButtons(false);
    }
    
    void SampleEditorController::onStoppedToMoveLayer(QWidget* widgetToMove,
                                                      QWidget* moveAboveThisWidget)
    {
        ASSERT(m_multiLayerForm);
        m_multiLayerForm->showAddLayerButtons(true);
        const auto* moveAboveThisLayerForm = m_multiLayerForm->findNextLayerForm(moveAboveThisWidget);
        auto* itemToMove = dynamic_cast<LayerForm*>(widgetToMove)->layerItem();
        auto* moveBeforeThisItem =
            moveAboveThisLayerForm != nullptr ? moveAboveThisLayerForm->layerItem() : nullptr;
    
        m_multiLayerItem->moveLayer(itemToMove, moveBeforeThisItem);
        m_multiLayerForm->onLayerMoved(itemToMove);
    
        // #baLayerEditor: tab order!
    
    void SampleEditorController::setMesoCrystalBasis(MesoCrystalForm* widget,
                                                     ItemWithParticlesCatalog::Type type)
    
    {
        auto* meso = widget->mesoCrystalItem();
    
        meso->setBasis(createAndInitParticle(type));
    
        widget->createBasisWidgets();
        m_multiLayerForm->updateUnits();
        emit modified();
    }
    
    void SampleEditorController::setMesoCrystalBasis(MesoCrystalForm* widget,
                                                     FormFactorItemCatalog::Type type)
    {
        auto* meso = widget->mesoCrystalItem();
    
        meso->setBasis(createAndInitParticle(type));
    
        widget->createBasisWidgets();
    
        m_multiLayerForm->updateUnits();
    
        emit modified();
    
    void SampleEditorController::selectInterference(InterferenceForm* widget, int newIndex)
    
        widget->layoutItem()->interference().setCurrentIndex(newIndex);
        widget->onInterferenceTypeChanged();
    
        m_multiLayerForm->updateUnits();
    
    
        // Disable/enable total density property in the particle layout, depending on type of
        // interference function.
        QWidget* parent = widget->parentWidget();
        while (parent != nullptr && dynamic_cast<ParticleLayoutForm*>(parent) == nullptr)
            parent = parent->parentWidget();
    
    
        if (auto* particleLayoutForm = dynamic_cast<ParticleLayoutForm*>(parent)) {
    
            particleLayoutForm->updateDensityEnabling();
    
            particleLayoutForm->updateDensityValue();
    
    
    void SampleEditorController::setIntegrateOverXi(LatticeTypeSelectionForm* widget, bool newValue)
    {
        widget->interferenceItem()->setXiIntegration(newValue);
        widget->onIntegrateOverXiChanged();
    
        emit modified();