Skip to content
Snippets Groups Projects
Commit 93a70817 authored by David Li's avatar David Li Committed by Pospelov, Gennady
Browse files

more unit test for model mapper in items

parent bbc6d69e
No related branches found
No related tags found
No related merge requests found
...@@ -33,24 +33,24 @@ BeamDistributionItem::BeamDistributionItem(const QString name) ...@@ -33,24 +33,24 @@ BeamDistributionItem::BeamDistributionItem(const QString name)
setGroupProperty(P_DISTRIBUTION, Constants::DistributionNoneType); setGroupProperty(P_DISTRIBUTION, Constants::DistributionNoneType);
} }
void BeamDistributionItem::onPropertyChange(const QString &name) //void BeamDistributionItem::onPropertyChange(const QString &name)
{ //{
if(name == P_CACHED_VALUE) { // if(name == P_CACHED_VALUE) {
DistributionItem *distribution = dynamic_cast<DistributionItem *>(getGroupItem(P_DISTRIBUTION)); // DistributionItem *distribution = dynamic_cast<DistributionItem *>(getGroupItem(P_DISTRIBUTION));
if(distribution) { // if(distribution) {
double cached_value = getRegisteredProperty(P_CACHED_VALUE).toDouble(); // double cached_value = getRegisteredProperty(P_CACHED_VALUE).toDouble();
PropertyAttribute cached_attribute = getPropertyAttribute(P_CACHED_VALUE); // PropertyAttribute cached_attribute = getPropertyAttribute(P_CACHED_VALUE);
cached_attribute.setVisible(); // cached_attribute.setVisible();
// do not propagate this change back to me, or I will enter an infinite // // do not propagate this change back to me, or I will enter an infinite
// signal-slot loop // // signal-slot loop
disconnect(getGroupItem(P_DISTRIBUTION), SIGNAL(propertyChanged(QString)), // disconnect(getGroupItem(P_DISTRIBUTION), SIGNAL(propertyChanged(QString)),
this, SLOT(processSubItemPropertyChanged(QString)) ); // this, SLOT(processSubItemPropertyChanged(QString)) );
distribution->init_parameters(cached_value, cached_attribute); // distribution->init_parameters(cached_value, cached_attribute);
connect(getGroupItem(P_DISTRIBUTION), SIGNAL(propertyChanged(QString)), // connect(getGroupItem(P_DISTRIBUTION), SIGNAL(propertyChanged(QString)),
this, SLOT(processSubItemPropertyChanged(QString)), Qt::UniqueConnection); // this, SLOT(processSubItemPropertyChanged(QString)), Qt::UniqueConnection);
} // }
} // }
} //}
//! returns parameter distribution to add into the Simulation //! returns parameter distribution to add into the Simulation
std::unique_ptr<ParameterDistribution> std::unique_ptr<ParameterDistribution>
...@@ -88,31 +88,31 @@ BeamDistributionItem::getParameterDistributionForName(const std::string &paramet ...@@ -88,31 +88,31 @@ BeamDistributionItem::getParameterDistributionForName(const std::string &paramet
return P_par_distr; return P_par_distr;
} }
//! updates new DistributionItem with cached_value ////! updates new DistributionItem with cached_value
void BeamDistributionItem::onSubItemChanged(const QString &propertyName) //void BeamDistributionItem::onSubItemChanged(const QString &propertyName)
{ //{
qDebug() << "BeamWavelengthItem::onSubItemChanged(const QString &propertyName)" << propertyName; // qDebug() << "BeamWavelengthItem::onSubItemChanged(const QString &propertyName)" << propertyName;
if(propertyName == P_DISTRIBUTION) { // if(propertyName == P_DISTRIBUTION) {
DistributionItem *distribution = dynamic_cast<DistributionItem *>(getGroupItem(P_DISTRIBUTION)); // DistributionItem *distribution = dynamic_cast<DistributionItem *>(getGroupItem(P_DISTRIBUTION));
Q_ASSERT(distribution); // Q_ASSERT(distribution);
double cached_value = getRegisteredProperty(P_CACHED_VALUE).toDouble(); // double cached_value = getRegisteredProperty(P_CACHED_VALUE).toDouble();
PropertyAttribute cached_attribute = getPropertyAttribute(P_CACHED_VALUE); // PropertyAttribute cached_attribute = getPropertyAttribute(P_CACHED_VALUE);
cached_attribute.setVisible(); // cached_attribute.setVisible();
distribution->init_parameters(cached_value, cached_attribute); // distribution->init_parameters(cached_value, cached_attribute);
} // }
ParameterizedItem::onSubItemChanged(propertyName); // ParameterizedItem::onSubItemChanged(propertyName);
} //}
void BeamDistributionItem::onSubItemPropertyChanged(const QString &property_group, const QString &property_name) //void BeamDistributionItem::onSubItemPropertyChanged(const QString &property_group, const QString &property_name)
{ //{
qDebug() << "BeamWavelengthItem::onSubItemPropertyChanged(const QString &property_group, const QString &property_name)" << property_group << property_name; // qDebug() << "BeamWavelengthItem::onSubItemPropertyChanged(const QString &property_group, const QString &property_name)" << property_group << property_name;
if(property_group == P_DISTRIBUTION && property_name == DistributionNoneItem::P_VALUE) { // if(property_group == P_DISTRIBUTION && property_name == DistributionNoneItem::P_VALUE) {
double value_to_cache = getGroupItem(P_DISTRIBUTION)-> // double value_to_cache = getGroupItem(P_DISTRIBUTION)->
getRegisteredProperty(DistributionNoneItem::P_VALUE).toDouble(); // getRegisteredProperty(DistributionNoneItem::P_VALUE).toDouble();
setRegisteredProperty(P_CACHED_VALUE, value_to_cache); // setRegisteredProperty(P_CACHED_VALUE, value_to_cache);
} // }
ParameterizedItem::onSubItemPropertyChanged(property_group, property_name); // ParameterizedItem::onSubItemPropertyChanged(property_group, property_name);
} //}
std::unique_ptr<IDistribution1D> BeamDistributionItem::createDistribution1D() std::unique_ptr<IDistribution1D> BeamDistributionItem::createDistribution1D()
{ {
......
...@@ -34,14 +34,14 @@ public: ...@@ -34,14 +34,14 @@ public:
static const QString P_CACHED_VALUE; static const QString P_CACHED_VALUE;
explicit BeamDistributionItem(const QString name = QString()); explicit BeamDistributionItem(const QString name = QString());
virtual ~BeamDistributionItem(){} virtual ~BeamDistributionItem(){}
virtual void onPropertyChange(const QString &name); // virtual void onPropertyChange(const QString &name);
std::unique_ptr<ParameterDistribution> getParameterDistributionForName( std::unique_ptr<ParameterDistribution> getParameterDistributionForName(
const std::string &parameter_name); const std::string &parameter_name);
protected slots: //protected slots:
virtual void onSubItemChanged(const QString &propertyName); // virtual void onSubItemChanged(const QString &propertyName);
virtual void onSubItemPropertyChanged(const QString &property_group, const QString &property_name); // virtual void onSubItemPropertyChanged(const QString &property_group, const QString &property_name);
protected: protected:
virtual std::unique_ptr<IDistribution1D> createDistribution1D(); virtual std::unique_ptr<IDistribution1D> createDistribution1D();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "DetectorItems.h" #include "DetectorItems.h"
#include "AngleProperty.h" #include "AngleProperty.h"
#include "MaskItems.h" #include "MaskItems.h"
#include "ModelMapper.h"
#include <QDebug> #include <QDebug>
const QString DetectorItem::P_DETECTOR = "DetectorType"; const QString DetectorItem::P_DETECTOR = "DetectorType";
...@@ -26,6 +27,18 @@ DetectorItem::DetectorItem() ...@@ -26,6 +27,18 @@ DetectorItem::DetectorItem()
registerGroupProperty(P_DETECTOR, Constants::DetectorGroup); registerGroupProperty(P_DETECTOR, Constants::DetectorGroup);
addToValidChildren(Constants::MaskContainerType); addToValidChildren(Constants::MaskContainerType);
setGroupProperty(P_DETECTOR, Constants::SphericalDetectorType); setGroupProperty(P_DETECTOR, Constants::SphericalDetectorType);
mapper()->setOnPropertyChange(
[this] (const QString &name)
{
if(name == P_DETECTOR) {
if(ParameterizedItem *maskContainer = getMaskContainerItem()) {
ParameterizedItem *item = takeChildItem(rowOfChild(maskContainer));
Q_ASSERT(item == maskContainer);
delete item;
}
}
});
} }
MaskContainerItem *DetectorItem::getMaskContainerItem() const MaskContainerItem *DetectorItem::getMaskContainerItem() const
...@@ -38,17 +51,3 @@ MaskContainerItem *DetectorItem::getMaskContainerItem() const ...@@ -38,17 +51,3 @@ MaskContainerItem *DetectorItem::getMaskContainerItem() const
return 0; return 0;
} }
void DetectorItem::onSubItemChanged(const QString &propertyName)
{
if(propertyName == P_DETECTOR) {
if(ParameterizedItem *maskContainer = getMaskContainerItem()) {
ParameterizedItem *item = takeChildItem(rowOfChild(maskContainer));
Q_ASSERT(item == maskContainer);
delete item;
}
}
ParameterizedItem::onSubItemChanged(propertyName);
}
...@@ -33,8 +33,6 @@ public: ...@@ -33,8 +33,6 @@ public:
MaskContainerItem *getMaskContainerItem() const; MaskContainerItem *getMaskContainerItem() const;
protected slots:
virtual void onSubItemChanged(const QString &propertyName);
}; };
......
...@@ -104,7 +104,7 @@ void GroupProperty::setCurrentLabel(const QString &label) ...@@ -104,7 +104,7 @@ void GroupProperty::setCurrentLabel(const QString &label)
{ {
if(type() == FIXED) { if(type() == FIXED) {
m_type_label_map[m_current_type] = label; m_type_label_map[m_current_type] = label;
if(m_parent) emit m_parent->propertyChanged(getGroupName()); if(m_parent) m_parent->emitPropertyChanged(getGroupName());
} }
} }
......
...@@ -84,7 +84,7 @@ InterferenceFunction2DParaCrystalItem::InterferenceFunction2DParaCrystalItem( ...@@ -84,7 +84,7 @@ InterferenceFunction2DParaCrystalItem::InterferenceFunction2DParaCrystalItem(
if(getRegisteredProperty(P_XI_INTEGRATION).toBool()) { if(getRegisteredProperty(P_XI_INTEGRATION).toBool()) {
getPropertyAttribute(P_ROTATION_ANGLE).setDisabled(); getPropertyAttribute(P_ROTATION_ANGLE).setDisabled();
} else { } else {
getPropertyAttribute(P_ROTATION_ANGLE).setVisible(); getPropertyAttribute(P_ROTATION_ANGLE).setEnabled();
} }
getPropertyItem(P_ROTATION_ANGLE)->emitValueChanged(); getPropertyItem(P_ROTATION_ANGLE)->emitValueChanged();
} }
......
...@@ -659,47 +659,3 @@ void ParameterizedItem::print() const ...@@ -659,47 +659,3 @@ void ParameterizedItem::print() const
// } // }
// qDebug() << " "; // qDebug() << " ";
} }
////! called when new SubItem appeared
void ParameterizedItem::onSubItemChanged(const QString &propertyName)
{
Q_UNUSED(propertyName);
// connect(m_sub_items[propertyName], SIGNAL(propertyChanged(QString)), this,
// SLOT(processSubItemPropertyChanged(QString)), Qt::UniqueConnection);
// emit subItemChanged(propertyName);
}
void ParameterizedItem::onSubItemPropertyChanged(const QString &property_group,
const QString &property_name)
{
emit subItemPropertyChanged(property_group, property_name);
if (mp_parent)
mp_parent->onChildPropertyChange(this, property_group);
}
void ParameterizedItem::onSiblingsChanged()
{
emit siblingsChanged();
}
////! called when SubItem change one of its properties
void ParameterizedItem::processSubItemPropertyChanged(const QString &propertyName)
{
Q_UNUSED(propertyName);
return;
// ParameterizedItem *propertyItem = qobject_cast<ParameterizedItem *>(sender());
/*for (QMap<QString, ParameterizedItem *>::iterator it = m_sub_items.begin();
it != m_sub_items.end(); ++it) {
if (it.value() == propertyItem) {
GroupProperty_t group_property
= getRegisteredProperty(it.key()).value<GroupProperty_t>();
group_property->setCurrentLabel(propertyItem->itemLabel());
onSubItemPropertyChanged(it.key(), propertyName);
return;
}
}*/
// throw -1; // NEW
// throw GUIHelpers::Error("ParameterizedItem::onSubItemPropertyChanged() ->"
// " Error. No such propertyItem found");
}
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <QObject> #include <QObject>
#include <QVector> #include <QVector>
#include <QVariant> #include <QVariant>
#include <QMetaType>
class SessionModel; class SessionModel;
...@@ -108,7 +109,7 @@ public: ...@@ -108,7 +109,7 @@ public:
PortInfo::EPorts port() const; PortInfo::EPorts port() const;
//! not quite sure how this works, leave it as it is //! not quite sure how this works, leave it as it is
void setPort(PortInfo::EPorts nport); virtual void setPort(PortInfo::EPorts nport);
// navigation functions // navigation functions
...@@ -304,24 +305,6 @@ signals: ...@@ -304,24 +305,6 @@ signals:
void subItemPropertyChanged(const QString &property_group, const QString &property_name); void subItemPropertyChanged(const QString &property_group, const QString &property_name);
void siblingsChanged(); void siblingsChanged();
protected slots:
virtual void onSubItemChanged(const QString &propertyName);
virtual void onSubItemPropertyChanged(const QString &property_group,
const QString &property_name);
virtual void onSiblingsChanged();
private slots:
virtual void processSubItemPropertyChanged(const QString &propertyName);
protected:
void notifySiblings();
}; };
#endif /* PARAMETERIZEDITEM_H_ */ #endif /* PARAMETERIZEDITEM_H_ */
...@@ -49,7 +49,6 @@ ParticleCompositionItem::ParticleCompositionItem() ...@@ -49,7 +49,6 @@ ParticleCompositionItem::ParticleCompositionItem()
void ParticleCompositionItem::insertChildItem(int row, ParameterizedItem *item) void ParticleCompositionItem::insertChildItem(int row, ParameterizedItem *item)
{ {
// int port = item->getRegisteredProperty(ParameterizedItem::OBSOLETE_P_PORT).toInt();
int port = int(item->port()); int port = int(item->port());
ParameterizedItem::insertChildItem(row, item); ParameterizedItem::insertChildItem(row, item);
if (item->modelType() == Constants::ParticleType if (item->modelType() == Constants::ParticleType
......
...@@ -50,23 +50,38 @@ void ParticleItem::insertChildItem(int row, ParameterizedItem *item) ...@@ -50,23 +50,38 @@ void ParticleItem::insertChildItem(int row, ParameterizedItem *item)
ParameterizedItem::insertChildItem(row, item); ParameterizedItem::insertChildItem(row, item);
if (item->modelType() == Constants::TransformationType) { if (item->modelType() == Constants::TransformationType) {
int port = int(item->port()); int port = int(item->port());
// int port = item->getRegisteredProperty(ParameterizedItem::OBSOLETE_P_PORT).toInt();
if (port == PortInfo::DEFAULT) { if (port == PortInfo::DEFAULT) {
item->setPort(PortInfo::PORT_0); item->setPort(PortInfo::PORT_0);
} }
} }
} }
void ParticleItem::onPropertyChange(const QString &name) std::unique_ptr<Particle> ParticleItem::createParticle() const
{
auto P_material = TransformToDomain::createDomainMaterial(*this);
auto P_particle = GUIHelpers::make_unique<Particle>(*P_material);
double abundance = getRegisteredProperty(ParticleItem::P_ABUNDANCE).toDouble();
P_particle->setAbundance(abundance);
auto ffItem = static_cast<FormFactorItem*>(getGroupItem(ParticleItem::P_FORM_FACTOR));
Q_ASSERT(ffItem);
auto P_ff = ffItem->createFormFactor();
P_particle->setFormFactor(*P_ff);
TransformToDomain::setTransformationInfo(P_particle.get(), *this);
return P_particle;
}
void ParticleItem::setPort(ParameterizedItem::PortInfo::EPorts nport)
{ {
ParameterizedItem::onPropertyChange(name); if (parent()) {
if (name == "OBSOLETE_P_PORT" && parent()) {
if (parent()->modelType() == Constants::ParticleCoreShellType if (parent()->modelType() == Constants::ParticleCoreShellType
|| parent()->modelType() == Constants::ParticleCompositionType || parent()->modelType() == Constants::ParticleCompositionType
|| parent()->modelType() == Constants::ParticleDistributionType) { || parent()->modelType() == Constants::ParticleDistributionType) {
setRegisteredProperty(ParticleItem::P_ABUNDANCE, 1.0); setRegisteredProperty(ParticleItem::P_ABUNDANCE, 1.0);
getPropertyAttribute(ParticleItem::P_ABUNDANCE).setDisabled(); getPropertyAttribute(ParticleItem::P_ABUNDANCE).setDisabled();
// int port = getRegisteredProperty(ParameterizedItem::OBSOLETE_P_PORT).toInt();
int port = int(this->port()); int port = int(this->port());
if (parent()->modelType() == Constants::ParticleCoreShellType) { if (parent()->modelType() == Constants::ParticleCoreShellType) {
auto p_coreshell = static_cast<ParticleCoreShellItem*>(parent()); auto p_coreshell = static_cast<ParticleCoreShellItem*>(parent());
...@@ -82,21 +97,3 @@ void ParticleItem::onPropertyChange(const QString &name) ...@@ -82,21 +97,3 @@ void ParticleItem::onPropertyChange(const QString &name)
} }
} }
} }
std::unique_ptr<Particle> ParticleItem::createParticle() const
{
auto P_material = TransformToDomain::createDomainMaterial(*this);
auto P_particle = GUIHelpers::make_unique<Particle>(*P_material);
double abundance = getRegisteredProperty(ParticleItem::P_ABUNDANCE).toDouble();
P_particle->setAbundance(abundance);
auto ffItem = static_cast<FormFactorItem*>(getGroupItem(ParticleItem::P_FORM_FACTOR));
Q_ASSERT(ffItem);
auto P_ff = ffItem->createFormFactor();
P_particle->setFormFactor(*P_ff);
TransformToDomain::setTransformationInfo(P_particle.get(), *this);
return P_particle;
}
...@@ -31,9 +31,9 @@ public: ...@@ -31,9 +31,9 @@ public:
static const QString P_POSITION; static const QString P_POSITION;
explicit ParticleItem(); explicit ParticleItem();
virtual ~ParticleItem() {} virtual ~ParticleItem() {}
virtual void insertChildItem(int row, ParameterizedItem *item); virtual void insertChildItem(int row, ParameterizedItem *item);
virtual void onPropertyChange(const QString &name);
std::unique_ptr<Particle> createParticle() const; std::unique_ptr<Particle> createParticle() const;
virtual void setPort(PortInfo::EPorts nport);
}; };
#endif // PARTICLEITEM_H #endif // PARTICLEITEM_H
...@@ -3,10 +3,14 @@ ...@@ -3,10 +3,14 @@
#include "ParameterizedItem.h" #include "ParameterizedItem.h"
#include "SampleModel.h" #include "SampleModel.h"
#include "InstrumentModel.h"
#include "item_constants.h" #include "item_constants.h"
#include "ParticleItem.h" #include "ParticleItem.h"
#include "ParticleCompositionItem.h" #include "ParticleCompositionItem.h"
#include "ParticleDistributionItem.h" #include "ParticleDistributionItem.h"
#include "InterferenceFunctionItems.h"
#include "DetectorItems.h"
#include "ComboProperty.h"
#include <memory> #include <memory>
#include <QtTest> #include <QtTest>
...@@ -15,6 +19,9 @@ class TestMapper : public QObject { ...@@ -15,6 +19,9 @@ class TestMapper : public QObject {
private slots: private slots:
void test_ParticeleCompositionUpdate(); void test_ParticeleCompositionUpdate();
void test_Inference2DRotationAngleToggle();
void test_instrumentAlignmentPropertyVisibility();
void test_removeMaskOnDetectorChange();
}; };
inline void TestMapper::test_ParticeleCompositionUpdate() inline void TestMapper::test_ParticeleCompositionUpdate()
...@@ -38,5 +45,60 @@ inline void TestMapper::test_ParticeleCompositionUpdate() ...@@ -38,5 +45,60 @@ inline void TestMapper::test_ParticeleCompositionUpdate()
} }
inline void TestMapper::test_Inference2DRotationAngleToggle()
{
SampleModel model;
ParameterizedItem *multilayer = model.insertNewItem(Constants::MultiLayerType);
ParameterizedItem *layer = model.insertNewItem(Constants::LayerType, multilayer->index());
ParameterizedItem *layout = model.insertNewItem(Constants::ParticleLayoutType, layer->index());
ParameterizedItem *inference = model.insertNewItem(Constants::InterferenceFunction2DParaCrystalType, layout->index());
// rotation (xi) should be disabled if integration is on
inference->setRegisteredProperty(InterferenceFunction2DParaCrystalItem::P_XI_INTEGRATION, true);
QVERIFY(inference->getPropertyAttribute(InterferenceFunction2DParaCrystalItem::P_ROTATION_ANGLE).isDisabled());
// rotation (xi) should be enabled if integration is off
inference->setRegisteredProperty(InterferenceFunction2DParaCrystalItem::P_XI_INTEGRATION, false);
QVERIFY(!inference->getPropertyAttribute(InterferenceFunction2DParaCrystalItem::P_ROTATION_ANGLE).isDisabled());
}
inline void TestMapper::test_instrumentAlignmentPropertyVisibility()
{
InstrumentModel model;
ParameterizedItem *instrument = model.insertNewItem(Constants::InstrumentType);
ParameterizedItem *detector = model.insertNewItem(Constants::DetectorType, instrument->index());
detector->setGroupProperty(DetectorItem::P_DETECTOR, Constants::RectangularDetectorType);
ParameterizedItem *rectangular = detector->getGroupItem(DetectorItem::P_DETECTOR);
ComboProperty alignment = rectangular->getRegisteredProperty(RectangularDetectorItem::P_ALIGNMENT)
.value<ComboProperty>();
// generic has some more items visible
alignment.setValue(Constants::ALIGNMENT_GENERIC);
rectangular->setRegisteredProperty(RectangularDetectorItem::P_ALIGNMENT, QVariant::fromValue<ComboProperty>(alignment));
QVERIFY(rectangular->getPropertyAttribute(RectangularDetectorItem::P_NORMAL).isHidden() == false);
// should be disabled if we switch
alignment.setValue(Constants::ALIGNMENT_TO_REFLECTED_BEAM);
rectangular->setRegisteredProperty(RectangularDetectorItem::P_ALIGNMENT, QVariant::fromValue<ComboProperty>(alignment));
QVERIFY(rectangular->getPropertyAttribute(RectangularDetectorItem::P_NORMAL).isHidden() == true);
}
inline void TestMapper::test_removeMaskOnDetectorChange()
{
InstrumentModel model;
ParameterizedItem *instrument = model.insertNewItem(Constants::InstrumentType);
ParameterizedItem *detector = model.insertNewItem(Constants::DetectorType, instrument->index());
detector->setGroupProperty(DetectorItem::P_DETECTOR, Constants::RectangularDetectorType);
model.insertNewItem(Constants::MaskContainerType, detector->index());
QVERIFY(detector->getUnregisteredChildren().size() == 1);
// after change the mask container should be removed
detector->setGroupProperty(DetectorItem::P_DETECTOR, Constants::SphericalDetectorType);
QVERIFY(detector->getUnregisteredChildren().size() == 0);
}
#endif #endif
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