diff --git a/GUI/Model/Sample/CompoundItem.cpp b/GUI/Model/Sample/CompoundItem.cpp index 375fecd22fde716b1ef50a325145cd03a9bf4731..41c0e49249855d317472d2f0549ba6be504a40a6 100644 --- a/GUI/Model/Sample/CompoundItem.cpp +++ b/GUI/Model/Sample/CompoundItem.cpp @@ -13,8 +13,6 @@ // ************************************************************************************************ #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/MesocrystalItem.h" #include "GUI/Model/Sample/ParticleItem.h" @@ -25,13 +23,10 @@ #include "Sample/Scattering/Rotations.h" namespace { - namespace Tag { -const QString Abundance("Abundance"); -const QString Position("Position"); -const QString Particles("Particles"); -const QString Rotation("Rotation"); +const QString Particle("Particle"); +const QString BaseData("BaseData"); } // namespace Tag @@ -46,25 +41,73 @@ const QString position_tooltip = "Relative position of the particle's reference CompoundItem::CompoundItem(const MaterialModel* materials) : ItemWithParticles(abundance_tooltip, position_tooltip) - , m_materials(materials) + , m_materialModel(materials) { - ASSERT(m_materials); + ASSERT(m_materialModel); } void CompoundItem::serialize(Streamer& s) { - s.assertVersion(0); - Serialize::rwProperty(s, Tag::Abundance, m_abundance); - m_position.rwProperty(s, Tag::Position); - m_rotation.rwSelected(s, Tag::Rotation); - Serialize::rwCatalogized<ItemWithParticlesCatalog>(s, Tag::Particles, m_particles, m_materials); +// s.assertVersion(0); +// Serialize::rwProperty(s, Tag::Abundance, m_abundance); +// m_position.rwProperty(s, Tag::Position); +// m_rotation.rwSelected(s, Tag::Rotation); + +// 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 { auto P_composition = std::make_unique<Compound>(); 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 P_particle = particleItem->createParticle()) P_composition->addParticle(*P_particle); @@ -89,24 +132,31 @@ std::unique_ptr<Compound> CompoundItem::createCompound() 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) { - m_particles.removeAll(particle); - delete particle; + for(size_t i = 0; i<m_particleSelections.size(); i++) + if(m_particleSelections[i].currentItem() == particle) + m_particleSelections.erase(m_particleSelections.begin() + i); } QVector<ItemWithParticles*> CompoundItem::containedItemsWithParticles() const { QVector<ItemWithParticles*> result; - for (auto* particle : m_particles) - result << particle << particle->containedItemsWithParticles(); + for (const auto& sel : m_particleSelections) + result << sel.currentItem() << sel.currentItem()->containedItemsWithParticles(); return result; } diff --git a/GUI/Model/Sample/CompoundItem.h b/GUI/Model/Sample/CompoundItem.h index 6c34fee204fc744dc47677abba343d30d150b844..972c36d24e8f0c1c4cd53fb6831a4704e6f9eac4 100644 --- a/GUI/Model/Sample/CompoundItem.h +++ b/GUI/Model/Sample/CompoundItem.h @@ -15,6 +15,7 @@ #ifndef 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 "Sample/Particle/Compound.h" #include <memory> @@ -25,18 +26,20 @@ class CompoundItem : public ItemWithParticles { public: CompoundItem(const MaterialModel* materials); void serialize(Streamer& s) override; + void writeTo(QXmlStreamWriter* w) const; + void readFrom(QXmlStreamReader* r); std::unique_ptr<Compound> createCompound() const; QVector<ItemWithParticles*> particles() const; - void addParticle(ItemWithParticles* particle); + SelectionProperty<ItemWithParticlesCatalog>& addParticle(ItemWithParticles* particle); void removeParticle(ItemWithParticles* particle); QVector<ItemWithParticles*> containedItemsWithParticles() const override; -private: - QVector<ItemWithParticles*> m_particles; - const MaterialModel* m_materials; +private: + std::vector<SelectionProperty<ItemWithParticlesCatalog>> m_particleSelections; + const MaterialModel* m_materialModel; }; #endif // BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H