Skip to content
Snippets Groups Projects
Commit a71cff88 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

simplify sample validator; reduce SessionItem usage

parent 7d5bf210
No related branches found
No related tags found
1 merge request!513Refactor sample model
...@@ -19,19 +19,6 @@ ...@@ -19,19 +19,6 @@
#include "GUI/Model/Sample/ParticleCoreShellItem.h" #include "GUI/Model/Sample/ParticleCoreShellItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h" #include "GUI/Model/Sample/ParticleLayoutItem.h"
#include <boost/polymorphic_cast.hpp>
using boost::polymorphic_cast;
using boost::polymorphic_downcast;
SampleValidator::SampleValidator() : m_valid_sample(true) {}
void SampleValidator::initValidator()
{
m_validation_message.clear();
m_valid_sample = true;
}
void SampleValidator::iterateItems(const SessionItem* parentItem) void SampleValidator::iterateItems(const SessionItem* parentItem)
{ {
for (const SessionItem* child : parentItem->children()) { for (const SessionItem* child : parentItem->children()) {
...@@ -42,87 +29,45 @@ void SampleValidator::iterateItems(const SessionItem* parentItem) ...@@ -42,87 +29,45 @@ void SampleValidator::iterateItems(const SessionItem* parentItem)
void SampleValidator::validateItem(const SessionItem* item) void SampleValidator::validateItem(const SessionItem* item)
{ {
if (!item) if (auto* p = dynamic_cast<const ParticleCoreShellItem*>(item)) {
return; if (!p->core())
addMessage("Core/shell particle doesn't have core defined.");
QString diagnosis; if (!p->shell())
addMessage("Core/shell particle doesn't have shell defined.");
if (item->hasModelType<MultiLayerItem>()) } else if (auto* p = dynamic_cast<const ParticleCompositionItem*>(item))
diagnosis = validateMultiLayerItem(polymorphic_downcast<const MultiLayerItem*>(item)); if (p->particles().isEmpty())
else if (item->hasModelType<ParticleLayoutItem>()) { addMessage("Particle composition doesn't have any particles.");
diagnosis =
validateParticleLayoutItem(polymorphic_downcast<const ParticleLayoutItem*>(item));
} else if (item->hasModelType<ParticleCoreShellItem>()) {
diagnosis =
validateParticleCoreShellItem(polymorphic_cast<const ParticleCoreShellItem*>(item));
} else if (item->hasModelType<ParticleCompositionItem>()) {
diagnosis =
validateParticleCompositionItem(polymorphic_cast<const ParticleCompositionItem*>(item));
}
if (!diagnosis.isEmpty()) {
m_valid_sample = false;
m_validation_message += QString("* ") + diagnosis + "\n";
}
} }
QString SampleValidator::validateMultiLayerItem(const MultiLayerItem* item) void SampleValidator::addMessage(const QString& m)
{ {
QString result; m_messages += QString("* ") + m + "\n";
QVector<LayerItem*> layers = item->layers();
if (layers.isEmpty())
result = "MultiLayer should contain at least one layer.";
else if (layers.size() == 1) {
if (layers.front()->layouts().isEmpty())
result = "The single layer in your MultiLayer should contain ParticleLayout.";
}
return result;
}
QString SampleValidator::validateParticleLayoutItem(const ParticleLayoutItem* item)
{
QString result;
QVector<ItemWithParticles*> particles = item->particles();
if (particles.isEmpty())
result = "ParticleLayout doesn't contain any particles.";
return result;
}
QString SampleValidator::validateParticleCoreShellItem(const ParticleCoreShellItem* item)
{
QString result;
const ParticleItem* core = item->core();
const ParticleItem* shell = item->shell();
if (core == nullptr || shell == nullptr)
result = "ParticleCoreShell doesn't have either core or shell defined.";
return result;
}
QString SampleValidator::validateParticleCompositionItem(const ParticleCompositionItem* item)
{
QString result;
if (item->particles().isEmpty())
result = "ParticleComposition doesn't have any particles.";
return result;
} }
bool SampleValidator::isValidMultiLayer(const MultiLayerItem* multilayer) bool SampleValidator::isValidMultiLayer(const MultiLayerItem* multilayer)
{ {
initValidator(); m_messages.clear();
validateItem(multilayer); QVector<LayerItem*> layers = multilayer->layers();
iterateItems(multilayer);
if (!m_valid_sample) { if (layers.isEmpty())
m_validation_message = addMessage("MultiLayer should contain at least one layer.");
"Can't setup DWBA simulation for given MultiLayer.\n" + m_validation_message; if (layers.size() == 1)
} if (layers.front()->layouts().isEmpty())
return m_valid_sample; addMessage("The single layer in your MultiLayer should contain a particle layout.");
for (auto* layer : layers)
for (auto* layout : layer->layouts()) {
if (layout->particles().isEmpty())
addMessage("Particle layout doesn't contain any particles.");
for (auto* particle : layout->particles()) {
validateItem(particle);
iterateItems(particle);
}
}
if (!m_messages.isEmpty())
m_messages = "Can't setup DWBA simulation for given MultiLayer.\n" + m_messages;
return m_messages.isEmpty();
} }
...@@ -26,25 +26,18 @@ class ParticleLayoutItem; ...@@ -26,25 +26,18 @@ class ParticleLayoutItem;
//! Validates SampleModel for MultiLayerItem suitable for simulation //! Validates SampleModel for MultiLayerItem suitable for simulation
class SampleValidator { class SampleValidator {
public: public:
SampleValidator();
bool isValidMultiLayer(const MultiLayerItem* multilayer); bool isValidMultiLayer(const MultiLayerItem* multilayer);
QString getValidationMessage() const { return m_validation_message; } QString getValidationMessage() const { return m_messages; }
private: private:
void initValidator();
void iterateItems(const SessionItem* parentItem); void iterateItems(const SessionItem* parentItem);
void validateItem(const SessionItem* item); void validateItem(const SessionItem* item);
QString validateMultiLayerItem(const MultiLayerItem* item); //! Adds this message to the report.
QString validateParticleLayoutItem(const ParticleLayoutItem* item); void addMessage(const QString& m);
QString validateParticleCoreShellItem(const ParticleCoreShellItem* item);
QString validateParticleCompositionItem(const ParticleCompositionItem* item);
bool m_valid_sample; QString m_messages;
QString m_validation_message;
}; };
#endif // BORNAGAIN_GUI_MODEL_SAMPLE_SAMPLEVALIDATOR_H #endif // BORNAGAIN_GUI_MODEL_SAMPLE_SAMPLEVALIDATOR_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