diff --git a/GUI/Model/Sample/SampleValidator.cpp b/GUI/Model/Sample/SampleValidator.cpp index cf9842e817df673efc090b74e3c64f7921a345b6..2e4c18d263e098d4ac0c9f998eb5c8e0592c8bce 100644 --- a/GUI/Model/Sample/SampleValidator.cpp +++ b/GUI/Model/Sample/SampleValidator.cpp @@ -21,46 +21,37 @@ #include "GUI/Model/Sample/ParticleLayoutItem.h" #include "GUI/Model/Sample/SampleItem.h" -void SampleValidator::validateItem(const ItemWithParticles* item) +QString SampleValidator::isValidSample(const SampleItem* sample) { - if (const auto* p = dynamic_cast<const CoreAndShellItem*>(item)) { - if (!p->coreItem()) - addMessage("Sim/shell particle doesn't have core defined."); - if (!p->shellItem()) - addMessage("Sim/shell particle doesn't have shell defined."); - } else if (const auto* p = dynamic_cast<const CompoundItem*>(item)) - if (p->itemsWithParticles().isEmpty()) - addMessage("Particle composition doesn't have any particles."); -} - -void SampleValidator::addMessage(const QString& m) -{ - m_messages += QString("* ") + m + "\n"; -} - -bool SampleValidator::isValidSample(const SampleItem* sample) -{ - m_messages.clear(); + QString messages; + const auto append = [&](const QString& m) { messages.append("* " + m + "\n"); }; QVector<LayerItem*> layers = sample->layerItems(); if (layers.isEmpty()) - addMessage("MultiLayer should contain at least one layer."); + append("MultiLayer should contain at least one layer."); if (layers.size() == 1) if (layers.front()->layoutItems().isEmpty()) - addMessage("The single layer in your MultiLayer should contain a particle layout."); + append("The single layer in your MultiLayer should contain a particle layout."); for (const auto* layer : layers) { for (const auto* layout : layer->layoutItems()) { if (layout->itemsWithParticles().isEmpty()) - addMessage("Particle layout doesn't contain any particles."); + append("Particle layout doesn't contain any particles."); for (const auto* particle : layout->containedItemsWithParticles()) { - validateItem(particle); + if (const auto* p = dynamic_cast<const CoreAndShellItem*>(particle)) { + if (!p->coreItem()) + append("Sim/shell particle doesn't have core defined."); + if (!p->shellItem()) + append("Sim/shell particle doesn't have shell defined."); + } else if (const auto* p = dynamic_cast<const CompoundItem*>(particle)) + if (p->itemsWithParticles().isEmpty()) + append("Particle composition doesn't have any particles."); } } } - if (!m_messages.isEmpty()) - m_messages = "Cannot setup DWBA simulation for given MultiLayer.\n" + m_messages; + if (!messages.isEmpty()) + messages = "Cannot setup DWBA simulation for given MultiLayer.\n" + messages; - return m_messages.isEmpty(); + return messages; } diff --git a/GUI/Model/Sample/SampleValidator.h b/GUI/Model/Sample/SampleValidator.h index a70b32ac2627d09748e4eca299c6de1b3b1359a6..5e801b26a85ede7cb6e1fad11418993d07fb44cb 100644 --- a/GUI/Model/Sample/SampleValidator.h +++ b/GUI/Model/Sample/SampleValidator.h @@ -18,23 +18,11 @@ #include <QString> class SampleItem; -class LayerItem; -class ItemWithParticles; -//! Validates whether SampleItem is suitable for simulation -class SampleValidator { -public: - bool isValidSample(const SampleItem* sample); +namespace SampleValidator { - QString getValidationMessage() const { return m_messages; } +QString isValidSample(const SampleItem* sample); -private: - void validateItem(const ItemWithParticles* item); - - //! Adds this message to the report. - void addMessage(const QString& m); - - QString m_messages; -}; +} #endif // BORNAGAIN_GUI_MODEL_SAMPLE_SAMPLEVALIDATOR_H diff --git a/GUI/View/Project/SimulationView.cpp b/GUI/View/Project/SimulationView.cpp index baeff8d3d193fd9c098bc23636e1294def9e766d..f30105efe9d599413dc491b2cb3fd17acf8d7826 100644 --- a/GUI/View/Project/SimulationView.cpp +++ b/GUI/View/Project/SimulationView.cpp @@ -229,9 +229,9 @@ QString SimulationView::validateSimulationSetup(bool validateRealData) const if (!selectedSampleItem()) append("No sample selected"); else { - SampleValidator sampleValidator; - if (!sampleValidator.isValidSample(selectedSampleItem())) - append(sampleValidator.getValidationMessage()); + QString m = SampleValidator::isValidSample(selectedSampleItem()); + if (!m.isEmpty()) + append(m); } if (!selectedInstrumentItem()) diff --git a/Resample/Processed/ReSample.cpp b/Resample/Processed/ReSample.cpp index 69700a38f29c28439b7c84791bed039a174212d7..eb76a376dea3e1620a0aa00f286b3469d4ff269a 100644 --- a/Resample/Processed/ReSample.cpp +++ b/Resample/Processed/ReSample.cpp @@ -104,7 +104,7 @@ SliceStack slicify(const MultiLayer& sample, bool useAvgMaterials) size_t nLayers = sample.numberOfLayers(); if (nLayers == 0) - return result; + throw std::runtime_error("Sample has no layer"); const bool use_slicing = useAvgMaterials && nLayers > 1; @@ -305,6 +305,7 @@ OwningVector<const ReLayout> collectLayouts(const MultiLayer& sample, const Slic { OwningVector<const ReLayout> result; + ASSERT(!slices.empty()); double z_ref = -slices.front().low(); for (size_t i = 0; i < sample.numberOfLayers(); ++i) {