Skip to content
Snippets Groups Projects
Commit cb4761c7 authored by Mikhail Svechnikov's avatar Mikhail Svechnikov
Browse files

CompoundItem: add r/w

parent 9c3f4130
No related branches found
No related tags found
1 merge request!1204GUI: serialization refactoring part 6
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
// ************************************************************************************************ // ************************************************************************************************
#include "GUI/Model/Sample/CompoundItem.h" #include "GUI/Model/Sample/CompoundItem.h"
#include "GUI/Model/CatSample/ItemWithParticlesCatalog.h"
#include "GUI/Model/CatSample/RotationItemCatalog.h"
#include "GUI/Model/Sample/CoreAndShellItem.h" #include "GUI/Model/Sample/CoreAndShellItem.h"
#include "GUI/Model/Sample/MesocrystalItem.h" #include "GUI/Model/Sample/MesocrystalItem.h"
#include "GUI/Model/Sample/ParticleItem.h" #include "GUI/Model/Sample/ParticleItem.h"
...@@ -25,13 +23,10 @@ ...@@ -25,13 +23,10 @@
#include "Sample/Scattering/Rotations.h" #include "Sample/Scattering/Rotations.h"
namespace { namespace {
namespace Tag { namespace Tag {
const QString Abundance("Abundance"); const QString Particle("Particle");
const QString Position("Position"); const QString BaseData("BaseData");
const QString Particles("Particles");
const QString Rotation("Rotation");
} // namespace Tag } // namespace Tag
...@@ -46,25 +41,73 @@ const QString position_tooltip = "Relative position of the particle's reference ...@@ -46,25 +41,73 @@ const QString position_tooltip = "Relative position of the particle's reference
CompoundItem::CompoundItem(const MaterialModel* materials) CompoundItem::CompoundItem(const MaterialModel* materials)
: ItemWithParticles(abundance_tooltip, position_tooltip) : ItemWithParticles(abundance_tooltip, position_tooltip)
, m_materials(materials) , m_materialModel(materials)
{ {
ASSERT(m_materials); ASSERT(m_materialModel);
} }
void CompoundItem::serialize(Streamer& s) void CompoundItem::serialize(Streamer& s)
{ {
s.assertVersion(0); // s.assertVersion(0);
Serialize::rwProperty(s, Tag::Abundance, m_abundance); // Serialize::rwProperty(s, Tag::Abundance, m_abundance);
m_position.rwProperty(s, Tag::Position); // m_position.rwProperty(s, Tag::Position);
m_rotation.rwSelected(s, Tag::Rotation); // m_rotation.rwSelected(s, Tag::Rotation);
Serialize::rwCatalogized<ItemWithParticlesCatalog>(s, Tag::Particles, m_particles, m_materials);
// Serialize::rwCatalogized<ItemWithParticlesCatalog>(s, "Particles", m_particles, m_materialModel);
if (s.xmlReader())
readFrom(s.xmlReader());
else if (s.xmlWriter())
writeTo(s.xmlWriter());
}
void CompoundItem::writeTo(QXmlStreamWriter *w) const
{
XML::writeAttribute(w, XML::Attrib::version, uint(1));
// parameters from base class
w->writeStartElement(Tag::BaseData);
ItemWithParticles::writeTo(w);
w->writeEndElement();
// particles
for (const auto& sel : m_particleSelections) {
w->writeStartElement(Tag::Particle);
sel.writeTo(w);
w->writeEndElement();
}
}
void CompoundItem::readFrom(QXmlStreamReader *r)
{
m_particleSelections.clear();
const uint version = XML::readUIntAttribute(r, XML::Attrib::version);
Q_UNUSED(version)
while (r->readNextStartElement()) {
QString tag = r->name().toString();
// parameters from base class
if (tag == Tag::BaseData) {
ItemWithParticles::readFrom(r);
XML::gotoEndElementOfTag(r, tag);
// particle
} else if (tag == Tag::Particle) {
addParticle(nullptr).readFrom(r, m_materialModel);
XML::gotoEndElementOfTag(r, tag);
} else
r->skipCurrentElement();
}
} }
std::unique_ptr<Compound> CompoundItem::createCompound() const std::unique_ptr<Compound> CompoundItem::createCompound() const
{ {
auto P_composition = std::make_unique<Compound>(); auto P_composition = std::make_unique<Compound>();
P_composition->setAbundance(abundance()); P_composition->setAbundance(abundance());
for (auto* p : m_particles) { for (const auto& ps : m_particleSelections) {
auto* p = ps.currentItem();
if (auto* particleItem = dynamic_cast<ParticleItem*>(p)) { if (auto* particleItem = dynamic_cast<ParticleItem*>(p)) {
if (auto P_particle = particleItem->createParticle()) if (auto P_particle = particleItem->createParticle())
P_composition->addParticle(*P_particle); P_composition->addParticle(*P_particle);
...@@ -89,24 +132,31 @@ std::unique_ptr<Compound> CompoundItem::createCompound() const ...@@ -89,24 +132,31 @@ std::unique_ptr<Compound> CompoundItem::createCompound() const
QVector<ItemWithParticles*> CompoundItem::particles() const QVector<ItemWithParticles*> CompoundItem::particles() const
{ {
return m_particles; QVector<ItemWithParticles*> output;
for(const auto& sel : m_particleSelections)
output.append(sel.currentItem());
return output;
} }
void CompoundItem::addParticle(ItemWithParticles* particle) SelectionProperty<ItemWithParticlesCatalog>& CompoundItem::addParticle(ItemWithParticles* particle)
{ {
m_particles << particle; SelectionProperty<ItemWithParticlesCatalog> newParticleSelection;
newParticleSelection.setCurrentItem(particle);
m_particleSelections.push_back(std::move(newParticleSelection));
return m_particleSelections.back();
} }
void CompoundItem::removeParticle(ItemWithParticles* particle) void CompoundItem::removeParticle(ItemWithParticles* particle)
{ {
m_particles.removeAll(particle); for(size_t i = 0; i<m_particleSelections.size(); i++)
delete particle; if(m_particleSelections[i].currentItem() == particle)
m_particleSelections.erase(m_particleSelections.begin() + i);
} }
QVector<ItemWithParticles*> CompoundItem::containedItemsWithParticles() const QVector<ItemWithParticles*> CompoundItem::containedItemsWithParticles() const
{ {
QVector<ItemWithParticles*> result; QVector<ItemWithParticles*> result;
for (auto* particle : m_particles) for (const auto& sel : m_particleSelections)
result << particle << particle->containedItemsWithParticles(); result << sel.currentItem() << sel.currentItem()->containedItemsWithParticles();
return result; return result;
} }
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#ifndef BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H #ifndef BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H
#define BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H #define BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H
#include "GUI/Model/CatSample/ItemWithParticlesCatalog.h"
#include "GUI/Model/Sample/ItemWithParticles.h" #include "GUI/Model/Sample/ItemWithParticles.h"
#include "Sample/Particle/Compound.h" #include "Sample/Particle/Compound.h"
#include <memory> #include <memory>
...@@ -25,18 +26,20 @@ class CompoundItem : public ItemWithParticles { ...@@ -25,18 +26,20 @@ class CompoundItem : public ItemWithParticles {
public: public:
CompoundItem(const MaterialModel* materials); CompoundItem(const MaterialModel* materials);
void serialize(Streamer& s) override; void serialize(Streamer& s) override;
void writeTo(QXmlStreamWriter* w) const;
void readFrom(QXmlStreamReader* r);
std::unique_ptr<Compound> createCompound() const; std::unique_ptr<Compound> createCompound() const;
QVector<ItemWithParticles*> particles() const; QVector<ItemWithParticles*> particles() const;
void addParticle(ItemWithParticles* particle); SelectionProperty<ItemWithParticlesCatalog>& addParticle(ItemWithParticles* particle);
void removeParticle(ItemWithParticles* particle); void removeParticle(ItemWithParticles* particle);
QVector<ItemWithParticles*> containedItemsWithParticles() const override; QVector<ItemWithParticles*> containedItemsWithParticles() const override;
private: private:
QVector<ItemWithParticles*> m_particles; std::vector<SelectionProperty<ItemWithParticlesCatalog>> m_particleSelections;
const MaterialModel* m_materials; const MaterialModel* m_materialModel;
}; };
#endif // BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H #endif // BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H
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