Skip to content
Snippets Groups Projects
Commit 8748bfaf authored by m.puchner's avatar m.puchner
Browse files

Merge branch 'RedSessionModel' into 'develop'

Reduce SessionModel

See merge request !555
parents 741a3e02 cabea2b0
No related branches found
No related tags found
1 merge request!555Reduce SessionModel
Pipeline #52426 passed
Showing
with 166 additions and 32 deletions
......@@ -60,6 +60,11 @@ public:
void setTransformationInfo(IParticle* result) const;
//! Return full hierarchical contained items with particles.
//!
//! For example ParticleCompositionItem contains other items, ParticleItem doesn't.
virtual QVector<ItemWithParticles*> containedItemsWithParticles() const = 0;
protected:
ItemWithParticles(const QString& model_type, const QString& abundance_tooltip,
const QString& position_tooltip);
......
......@@ -87,6 +87,14 @@ QVector<ItemWithMaterial*> LayerItem::itemsWithMaterial()
return result;
}
QVector<ItemWithParticles*> LayerItem::itemsWithParticles() const
{
QVector<ItemWithParticles*> result;
for (auto* layout : layouts())
result << layout->containedItemsWithParticles();
return result;
}
DoubleDescriptor LayerItem::thickness() const
{
DoubleDescriptor d(getItem(P_THICKNESS), Unit::nanometer);
......
......@@ -24,6 +24,7 @@ class LayerBasicRoughnessItem;
class ParticleLayoutItem;
class UIntDescriptor;
class DoubleDescriptor;
class ItemWithParticles;
class BA_CORE_API_ LayerItem : public ItemWithMaterial {
private:
......@@ -41,6 +42,7 @@ public:
QString layerName() const;
QVector<ItemWithMaterial*> itemsWithMaterial();
QVector<ItemWithParticles*> itemsWithParticles() const;
DoubleDescriptor thickness() const;
void setThicknessEnabled(bool enabled);
......
......@@ -169,3 +169,11 @@ VectorDescriptor MesoCrystalItem::vectorC() const
{
return VectorDescriptor(item<VectorItem>(P_VECTOR_C), Unit::nanometer);
}
QVector<ItemWithParticles*> MesoCrystalItem::containedItemsWithParticles() const
{
QVector<ItemWithParticles*> result;
if (basisParticle())
result << basisParticle() << basisParticle()->containedItemsWithParticles();
return result;
}
......@@ -62,6 +62,8 @@ public:
VectorDescriptor vectorA() const;
VectorDescriptor vectorB() const;
VectorDescriptor vectorC() const;
QVector<ItemWithParticles*> containedItemsWithParticles() const override;
};
template <typename T> T* MesoCrystalItem::setOuterShapeType()
......
......@@ -90,3 +90,11 @@ void ParticleCompositionItem::removeParticle(ItemWithParticles* particle)
{
model()->removeItem(particle);
}
QVector<ItemWithParticles*> ParticleCompositionItem::containedItemsWithParticles() const
{
QVector<ItemWithParticles*> result;
for (auto* particle : particles())
result << particle << particle->containedItemsWithParticles();
return result;
}
......@@ -34,6 +34,8 @@ public:
QVector<ItemWithParticles*> particles() const;
void addParticle(ItemWithParticles* particle);
void removeParticle(ItemWithParticles* particle);
QVector<ItemWithParticles*> containedItemsWithParticles() const override;
};
#endif // BORNAGAIN_GUI_MODEL_SAMPLE_PARTICLECOMPOSITIONITEM_H
......@@ -101,3 +101,13 @@ ParticleItem* ParticleCoreShellItem::createShell(MaterialModel* materials)
return p;
}
QVector<ItemWithParticles*> ParticleCoreShellItem::containedItemsWithParticles() const
{
QVector<ItemWithParticles*> result;
if (core())
result << core() << core()->containedItemsWithParticles();
if (shell())
result << shell() << shell()->containedItemsWithParticles();
return result;
}
......@@ -41,6 +41,8 @@ public:
ParticleItem* shell() const;
void clearShell();
ParticleItem* createShell(MaterialModel* materials);
QVector<ItemWithParticles*> containedItemsWithParticles() const override;
};
#endif // BORNAGAIN_GUI_MODEL_SAMPLE_PARTICLECORESHELLITEM_H
......@@ -71,3 +71,8 @@ GroupItem* ParticleItem::formFactorItem() const
{
return item<GroupItem>(P_FORM_FACTOR);
}
QVector<ItemWithParticles*> ParticleItem::containedItemsWithParticles() const
{
return {};
}
......@@ -38,6 +38,7 @@ public:
void setFormFactor(const QString& model_type);
FormFactorItem* formFactor() const;
GroupItem* formFactorItem() const;
QVector<ItemWithParticles*> containedItemsWithParticles() const override;
};
template <typename T> T* ParticleItem::setFormFactorType()
......
......@@ -97,6 +97,14 @@ void ParticleLayoutItem::removeParticle(ItemWithParticles* particle)
model()->removeItem(particle);
}
QVector<ItemWithParticles*> ParticleLayoutItem::containedItemsWithParticles() const
{
QVector<ItemWithParticles*> result;
for (auto* p : particles())
result << p << p->containedItemsWithParticles();
return result;
}
SelectionDescriptor<InterferenceItem*> ParticleLayoutItem::interference() const
{
SelectionDescriptor<InterferenceItem*> d;
......
......@@ -49,10 +49,19 @@ public:
DoubleDescriptor weight() const;
//! The particles this layout contains.
//!
//! Note that this is not a hierarchical search! Only the topmost particles are returned, not
//! their contained particles. For a hierarchical search use containedItemsWithParticles
QVector<ItemWithParticles*> particles() const;
void addParticle(ItemWithParticles* particle);
void removeParticle(ItemWithParticles* particle);
//! Return full hierarchical contained items with particles.
//!
//! \sa particles()
QVector<ItemWithParticles*> containedItemsWithParticles() const;
SelectionDescriptor<InterferenceItem*> interference() const;
template <typename T> T* createInterference();
void setInterference(InterferenceItem* interference);
......
......@@ -68,21 +68,21 @@ RealSpaceBuilder::RealSpaceBuilder(const MaterialModel* materialModel)
m_builderUtils = std::make_unique<RealSpace::BuilderUtils>(materialModel);
}
void RealSpaceBuilder::populate(RealSpace::Model* model, SampleItem item,
void RealSpaceBuilder::populate(RealSpace::Model* model, ItemForRealSpace item,
const SceneGeometry& sceneGeometry,
const RealSpace::Camera::Position& cameraPosition) const
{
// default value of cameraPosition is in RealSpaceBuilder.h
model->defCamPos = cameraPosition;
if (const auto* p = item.get_if<MultiLayerItem*>())
populateMultiLayer(model, *p, sceneGeometry);
else if (const auto* p = item.get_if<LayerItem*>())
populateLayer(model, *p, sceneGeometry);
else if (const auto* p = item.get_if<ParticleLayoutItem*>())
populateLayout(model, *p, sceneGeometry);
else if (const auto* p = item.get_if<ItemWithParticles*>())
populateParticleFromParticleItem(model, *p);
if (const auto* p = std::get_if<MultiLayerItem*>(&item))
populateMultiLayer(model, **p, sceneGeometry);
else if (const auto* p = std::get_if<LayerItem*>(&item))
populateLayer(model, **p, sceneGeometry);
else if (const auto* p = std::get_if<ParticleLayoutItem*>(&item))
populateLayout(model, **p, sceneGeometry);
else if (const auto* p = std::get_if<ItemWithParticles*>(&item))
populateParticleFromParticleItem(model, **p);
}
void RealSpaceBuilder::populateMultiLayer(GUI::RealSpace::Model* model,
......
......@@ -37,9 +37,14 @@ using namespace GUI;
class RealSpaceBuilder {
public:
//! Defines the item types this class can handle
using ItemForRealSpace =
std::variant<MultiLayerItem*, LayerItem*, ParticleLayoutItem*, ItemWithParticles*>;
RealSpaceBuilder(const MaterialModel* materialModel);
void populate(RealSpace::Model* model, SampleItem item, const SceneGeometry& sceneGeometry,
void populate(RealSpace::Model* model, ItemForRealSpace item,
const SceneGeometry& sceneGeometry,
const RealSpace::Camera::Position& cameraPosition =
RealSpace::Camera::Position(RealSpace::Vector3D(0, -200, 120), // eye
RealSpace::Vector3D(0, 0, 0), // center
......
......@@ -39,16 +39,17 @@ RealSpaceCanvas::RealSpaceCanvas(const MaterialModel* materialModel, QWidget* pa
setLayout(layout);
}
void RealSpaceCanvas::setCurrentItem(SampleItem item)
void RealSpaceCanvas::setCurrentItem(ItemForRealSpace item)
{
m_currentItem = item;
updateScene();
defaultView(); // Enforces default view and also sets the zoomLevel to default i.e. 0
}
std::optional<SampleItem> RealSpaceCanvas::currentItem() const
{
return m_currentItem;
bool isValid = false;
std::visit([&](auto&& arg) { isValid = arg != nullptr; }, item);
if (isValid) {
m_currentItem = item;
updateScene();
defaultView(); // Enforces default view and also sets the zoomLevel to default i.e. 0
} else
resetScene();
}
void RealSpaceCanvas::changeLayerSize(double layerSizeChangeScale)
......@@ -135,7 +136,7 @@ void RealSpaceCanvas::resetScene()
{
m_realSpaceModel.reset();
m_view->setModel(nullptr);
m_currentItem = nullptr;
m_currentItem.reset();
}
void RealSpaceCanvas::defaultView()
......
......@@ -15,18 +15,21 @@
#ifndef BORNAGAIN_GUI_VIEW_REALSPACE_REALSPACECANVAS_H
#define BORNAGAIN_GUI_VIEW_REALSPACE_REALSPACECANVAS_H
#include "GUI/Model/Sample/SampleItem.h"
#include <QWidget>
#include <memory>
#include <optional>
#include <variant>
class CautionSign;
class MaterialModel;
namespace GUI::RealSpace {
class MultiLayerItem;
class LayerItem;
class ParticleLayoutItem;
class ItemWithParticles;
namespace GUI::RealSpace {
class Canvas;
class Model;
} // namespace GUI::RealSpace
//! Size and thickness information of layers
......@@ -41,10 +44,14 @@ class RealSpaceCanvas : public QWidget {
Q_OBJECT
public:
//! Defines the item types which can be the "current item"
using ItemForRealSpace =
std::variant<MultiLayerItem*, LayerItem*, ParticleLayoutItem*, ItemWithParticles*>;
RealSpaceCanvas(const MaterialModel* materialModel, QWidget* parent = nullptr);
void setCurrentItem(SampleItem item);
std::optional<SampleItem> currentItem() const;
void setCurrentItem(ItemForRealSpace item);
template <typename T> T currentItemAs() const;
void defaultView();
void sideView();
void topView();
......@@ -64,8 +71,18 @@ private:
std::unique_ptr<GUI::RealSpace::Model> m_realSpaceModel;
SceneGeometry m_sceneGeometry;
CautionSign* m_cautionSign;
std::optional<SampleItem> m_currentItem;
std::optional<ItemForRealSpace> m_currentItem;
bool m_firstView;
};
template <typename T> T RealSpaceCanvas::currentItemAs() const
{
if (!m_currentItem)
return nullptr;
auto currentVariant = m_currentItem.value();
T* ppCurrent = std::get_if<T>(&currentVariant);
return ppCurrent ? *ppCurrent : nullptr;
}
#endif // BORNAGAIN_GUI_VIEW_REALSPACE_REALSPACECANVAS_H
......@@ -14,7 +14,10 @@
#include "GUI/View/SampleDesigner/SampleView.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sample/ItemWithParticles.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/MultiLayerItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
#include "GUI/Model/Sample/SampleModel.h"
#include "GUI/View/Common/DocksController.h"
#include "GUI/View/Common/StyledToolBar.h"
......@@ -113,16 +116,54 @@ void SampleView::onRequestViewInRealSpace(SessionItem* itemToView)
if (!itemToView)
return;
RealSpaceCanvas::ItemForRealSpace itemForRealSpace;
if (auto* p = dynamic_cast<MultiLayerItem*>(itemToView))
itemForRealSpace = p;
else if (auto* p = dynamic_cast<LayerItem*>(itemToView))
itemForRealSpace = p;
else if (auto* p = dynamic_cast<ParticleLayoutItem*>(itemToView))
itemForRealSpace = p;
else if (auto* p = dynamic_cast<ItemWithParticles*>(itemToView))
itemForRealSpace = p;
else
return;
m_docks->setDockVisible(REALSPACEPANEL);
m_realSpacePanel->canvas()->setCurrentItem(itemToView);
m_realSpacePanel->canvas()->setCurrentItem(itemForRealSpace);
}
void SampleView::onAboutToRemoveItem(SampleItem item)
void SampleView::onAboutToRemoveItem(SampleItem itemToRemove)
{
if (m_realSpacePanel->canvas()->currentItem() == item)
m_realSpacePanel->canvas()->resetScene();
// #baMigration ++ check whether canvas shows child of removed item
// If an item shall be deleted from the sample, check whether RealSpace is affected.
// Its current item could be itemToRemmove itself, or it could be a child of itemToRemove.
// -- for convenience access later on
auto* canvas = m_realSpacePanel->canvas();
auto* currentAsLayer = canvas->currentItemAs<LayerItem*>();
auto* currentAsLayout = canvas->currentItemAs<ParticleLayoutItem*>();
auto* currentAsParticle = canvas->currentItemAs<ItemWithParticles*>();
const auto resetIf = [&](bool b) {
if (b)
canvas->resetScene();
};
if (itemToRemove.get_if<MultiLayerItem*>()) {
// -- itemToRemove is a multilayer. In this case: always reset
canvas->resetScene();
} else if (auto* layerToRemove = itemToRemove.get_if<LayerItem*>()) {
// -- itemToRemove is a layer
resetIf(layerToRemove == currentAsLayer);
resetIf(layerToRemove->layouts().contains(currentAsLayout));
resetIf(layerToRemove->itemsWithParticles().contains(currentAsParticle));
} else if (auto* layoutToRemove = itemToRemove.get_if<ParticleLayoutItem*>()) {
// -- itemToRemove is a particle layout
resetIf(layoutToRemove == currentAsLayout);
resetIf(layoutToRemove->containedItemsWithParticles().contains(currentAsParticle));
} else if (auto* particleToRemove = itemToRemove.get_if<ItemWithParticles*>()) {
// -- itemToRemove is a particle
resetIf(particleToRemove == currentAsParticle);
resetIf(particleToRemove->containedItemsWithParticles().contains(currentAsParticle));
}
}
void SampleView::toggleRealSpaceView()
......
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