Skip to content
Snippets Groups Projects
MaterialEditorUtils.cpp 7.08 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
    //
    
    t.knopff's avatar
    t.knopff committed
    //! @file      GUI/Views/MaterialEditor/MaterialItemUtils.cpp
    
    //! @brief     Implements class MaterialItemUtils
    
    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
    
    
    t.knopff's avatar
    t.knopff committed
    #include "GUI/Views/MaterialEditor/MaterialItemUtils.h"
    #include "GUI/Models/ComboProperty.h"
    
    #include "GUI/Models/Error.h"
    
    t.knopff's avatar
    t.knopff committed
    #include "GUI/Models/LayerItem.h"
    #include "GUI/Models/MaterialDataItems.h"
    #include "GUI/Models/MaterialItemContainer.h"
    #include "GUI/Models/MaterialModel.h"
    #include "GUI/Models/MesoCrystalItem.h"
    #include "GUI/Models/ParticleCompositionItem.h"
    #include "GUI/Models/ParticleCoreShellItem.h"
    #include "GUI/Models/ParticleItem.h"
    #include "GUI/Models/ParticleLayoutItem.h"
    #include "GUI/Views/MaterialEditor/MaterialEditorDialog.h"
    #include "GUI/Views/SampleDesigner/DesignerHelper.h"
    #include "GUI/mainwindow/AppSvc.h"
    #include "GUI/mainwindow/mainwindow.h"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    #include "Sample/Material/Material.h"
    
    #include <QColorDialog>
    
    namespace {
    
    std::map<QString, QString> get_tag_map()
    {
    
        std::map<QString, QString> result = {
    
            {"ParticleComposition", ParticleCompositionItem::T_PARTICLES},
            {"ParticleLayout", ParticleLayoutItem::T_PARTICLES},
            {"MesoCrystal", MesoCrystalItem::T_BASIS_PARTICLE}};
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    } // namespace
    
    QColor MaterialItemUtils::suggestMaterialColor(const QString& name)
    {
    
        if (name.contains("Vacuum")) {
    
            return QColor(179, 242, 255);
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
        } else if (name.contains("Substrate")) {
            return QColor(205, 102, 0);
        } else if (name.contains("Default")) {
    
            return QColor(Qt::green);
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
        } else if (name.contains("Particle")) {
    
            return QColor(146, 198, 255);
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
        } else {
    
            return DesignerHelper::getRandomColor();
        }
    
    ExternalProperty MaterialItemUtils::defaultMaterialProperty()
    {
    
        if (!AppSvc::materialModel())
            return ExternalProperty();
    
    
        auto materials = AppSvc::materialModel()->topItems<MaterialItem>();
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
        return materials.isEmpty() ? ExternalProperty()
                                   : MaterialItemUtils::materialProperty(*materials.front());
    
    std::unique_ptr<Material>
    
    MaterialItemUtils::createDomainMaterial(const ExternalProperty& material_property)
    {
    
        MaterialItem* materialItem = findMaterial(material_property);
        return materialItem->createMaterial();
    }
    
    
    std::unique_ptr<Material>
    MaterialItemUtils::createDomainMaterial(const ExternalProperty& material_property,
    
                                            const MaterialItemContainer& container)
    {
    
        const MaterialItem* material_item = container.findMaterialById(material_property.identifier());
        if (!material_item)
    
            throw Error("MaterialUtils::createDomainMaterial() -> Error. Can't find "
                        "material with name '"
                        + material_property.text() + "'.");
    
        return material_item->createMaterial();
    }
    
    
    MaterialItem* MaterialItemUtils::findMaterial(const ExternalProperty& material_property)
    {
    
        if (!AppSvc::materialModel())
    
            throw Error("MaterialItemUtils::findMaterial() -> Error. "
                        "Attempt to access non-existing material model");
    
        auto material = AppSvc::materialModel()->materialFromIdentifier(material_property.identifier());
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
        if (!material)
    
            throw Error("MaterialUtils::findMaterial() -> Error. Can't find "
                        "material with name '"
                        + material_property.text() + "'.");
    
        return material;
    
    
    //! Returns material tag for given item. Returns empty string, if item doesn't have materials.
    
    
    QString MaterialItemUtils::materialTag(const SessionItem& item)
    {
    
        } else if (item.modelType() == "Layer") {
    
            result = LayerItem::P_MATERIAL;
        }
        return result;
    }
    
    //! Returns list of model types which contains registered MaterialProperty.
    
    
    QStringList MaterialItemUtils::materialRelatedModelTypes()
    {
    
    //! Constructs material property for given material.
    
    ExternalProperty MaterialItemUtils::materialProperty(const MaterialItem& materialItem)
    
        ExternalProperty result;
    
        QColor color = materialItem.color();
    
        result.setIdentifier(materialItem.identifier());
    
        result.setColor(color);
    
        result.setText(materialItem.itemName());
    
    ExternalProperty MaterialItemUtils::colorProperty(const QColor& color)
    {
    
        ExternalProperty result;
        result.setColor(color);
        result.setText(QString("[%1, %2, %3] (%4)")
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
                           .arg(color.red())
                           .arg(color.green())
                           .arg(color.blue())
                           .arg(color.alpha()));
    
    ExternalProperty MaterialItemUtils::selectMaterialProperty(const ExternalProperty& previous)
    {
    
        MaterialEditorDialog dialog(AppSvc::materialModel(), MainWindow::instance());
    
        dialog.setMaterialProperty(previous);
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
        if (dialog.exec() == QDialog::Accepted) {
    
            return dialog.selectedMaterialProperty();
    
        return ExternalProperty();
    
    ExternalProperty MaterialItemUtils::selectColorProperty(const ExternalProperty& previous)
    {
    
    #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
        auto oldColor = previous.color();
        auto newColor = QColorDialog::getColor(oldColor);
        if (oldColor != newColor)
            result = MaterialItemUtils::colorProperty(newColor);
    #else
    
        bool ok = false;
        QRgb oldRgba = previous.color().rgba();
        QRgb newRgba = QColorDialog::getRgba(oldRgba, &ok, nullptr);
    
        if (ok && newRgba != oldRgba)
            result = MaterialItemUtils::colorProperty(QColor::fromRgba(newRgba));
    
    QVector<SessionItem*> MaterialItemUtils::materialPropertyItems(SessionItem* item)
    {
    
        static const std::map<QString, QString> tag_map = get_tag_map();
    
        QVector<SessionItem*> materials;
        QList<SessionItem*> particle_holders{item};
        while (!particle_holders.isEmpty()) {
            auto item = particle_holders.takeFirst();
            if (!item)
                continue;
    
            const QString model_type = item->modelType();
            auto iter = tag_map.find(model_type);
            if (iter != tag_map.end()) {
                particle_holders.append(QList<SessionItem*>::fromVector(item->getItems(iter->second)));
                continue;
            }
    
    
                materials.append(static_cast<ParticleItem*>(item)->materialPropertyItems());
    
            else if (model_type == "ParticleCoreShell")
    
                materials.append(static_cast<ParticleCoreShellItem*>(item)->materialPropertyItems());
            else
    
                    "Error in MaterialItemUtils::materialProperties: cannot handle passed model type '"
                    + model_type + "'");
        }
        return materials;
    }