diff --git a/GUI/coregui/Models/BeamItem.cpp b/GUI/coregui/Models/BeamItem.cpp index ab44487835321a58271c97d89e85c8686de748a7..360601a8aadacd4110f8060862b5ce2766a441ef 100644 --- a/GUI/coregui/Models/BeamItem.cpp +++ b/GUI/coregui/Models/BeamItem.cpp @@ -66,29 +66,29 @@ BeamWavelengthItem::BeamWavelengthItem(ParameterizedItem *parent) } -void BeamWavelengthItem::onPropertyChange(const QString &name) -{ - qDebug() << " "; - qDebug() << " "; - qDebug() << " "; - qDebug() << " "; - qDebug() << "BeamWavelengthItem::onPropertyChange(const QString &name)" << name; +//void BeamWavelengthItem::onPropertyChange(const QString &name) +//{ +// qDebug() << " "; +// qDebug() << " "; +// qDebug() << " "; +// qDebug() << " "; +// qDebug() << "BeamWavelengthItem::onPropertyChange(const QString &name)" << name; - if(name == P_DISTRIBUTION) { - ParameterizedItem *distribution = getSubItems()[P_DISTRIBUTION]; - if(distribution) { - if(distribution->modelType() == Constants::DistributionNoneType) { - setPropertyAppearance(P_CACHED_VALUE, PropertyAttribute::VISIBLE); - qDebug() << "XXX setting to visible"; - } else { - setPropertyAppearance(P_CACHED_VALUE, PropertyAttribute::HIDDEN); - qDebug() << "XXX setting to hidden"; - } - ParameterizedItem::onPropertyChange(P_CACHED_VALUE); - } - } - ParameterizedItem::onPropertyChange(name); -} +// if(name == P_DISTRIBUTION) { +// ParameterizedItem *distribution = getSubItems()[P_DISTRIBUTION]; +// if(distribution) { +// if(distribution->modelType() == Constants::DistributionNoneType) { +// setPropertyAppearance(P_CACHED_VALUE, PropertyAttribute::VISIBLE); +// qDebug() << "XXX setting to visible"; +// } else { +// setPropertyAppearance(P_CACHED_VALUE, PropertyAttribute::HIDDEN); +// qDebug() << "XXX setting to hidden"; +// } +// ParameterizedItem::onPropertyChange(P_CACHED_VALUE); +// } +// } +// ParameterizedItem::onPropertyChange(name); +//} void BeamWavelengthItem::onSubItemChanged(const QString &propertyName) { @@ -109,6 +109,21 @@ void BeamWavelengthItem::onSubItemChanged(const QString &propertyName) ParameterizedItem::onSubItemChanged(propertyName); } +void BeamWavelengthItem::onSubItemPropertyChanged(const QString &property_group, const QString &property_name) +{ + qDebug() << "OOO"; + qDebug() << "OOO"; + qDebug() << "OOO"; + qDebug() << "OOO"; + 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) { + double value_to_cache = getSubItems()[P_DISTRIBUTION]->getRegisteredProperty(DistributionNoneItem::P_VALUE).toDouble(); + setRegisteredProperty(P_CACHED_VALUE, value_to_cache); + } +} + + + const QString TestBeamItem::P_INTENSITY = "Intensity [1/s]"; const QString TestBeamItem::P_WAVELENGTH = "Wavelength"; @@ -119,47 +134,3 @@ TestBeamItem::TestBeamItem(ParameterizedItem *parent) registerProperty(P_INTENSITY, 1e+08); registerGroupProperty(P_WAVELENGTH, Constants::BeamWavelengthType); } - - -const QString BeamDistributionItem::P_DISTRIBUTION = "Distribution"; -const QString BeamDistributionItem::P_VALUE = "Value"; -const QString BeamDistributionItem::P_COMBO = "Combo"; -const QString BeamDistributionItem::P_VECTOR = "Vector"; -BeamDistributionItem::BeamDistributionItem(ParameterizedItem *parent) - : ParameterizedItem(Constants::BeamDistributionType, parent) -{ - qDebug() << "BeamDistributionItem::BeamDistributionItem() 1.1"; - registerGroupProperty(P_DISTRIBUTION, Constants::DistributionExtendedGroup); -// qDebug() << "BeamDistributionItem::BeamDistributionItem() 1.2"; -// setGroupProperty(P_DISTRIBUTION, Constants::DistributionNoneType); -// qDebug() << "BeamDistributionItem::BeamDistributionItem() 1.3"; - //setPropertyAppearance(P_DISTRIBUTION, PropertyAttribute::HIDDEN); - qDebug() << "BeamDistributionItem::BeamDistributionItem() 1.4"; - registerProperty(P_VALUE, 99.0, PropertyAttribute(PropertyAttribute::DISABLED)); - - ComboProperty types; - types << "property 1" << "property 2" << "property 3"; - registerProperty(P_COMBO, types.getVariant()); - registerGroupProperty(P_VECTOR, Constants::VectorType); - - initProperties(); -} - -//void BeamDistributionItem::onPropertyChange(const QString &name) -//{ -// qDebug() << "BeamDistributionItem::onPropertyChange(const QString &name)" << name; -// print(); -// if(name == P_DISTRIBUTION) { -// ParameterizedItem *distribution = getSubItems()[P_DISTRIBUTION]; -// if(distribution && distribution->modelType() != Constants::DistributionNoneType) { -// registerProperty(P_VALUE, 1.0); -// } -// } -//} - -void BeamDistributionItem::initProperties() -{ - -} - - diff --git a/GUI/coregui/Models/BeamItem.h b/GUI/coregui/Models/BeamItem.h index bcdedbe25cdb104686102d3d7195814301270d16..b0a2273c57ff28efbb1880284f3dba3b28a02ec7 100644 --- a/GUI/coregui/Models/BeamItem.h +++ b/GUI/coregui/Models/BeamItem.h @@ -43,8 +43,10 @@ public: static const QString P_CACHED_VALUE; explicit BeamWavelengthItem(ParameterizedItem *parent=0); ~BeamWavelengthItem(){} - void onPropertyChange(const QString &name); +protected slots: +// void onPropertyChange(const QString &name); void onSubItemChanged(const QString &propertyName); + void onSubItemPropertyChanged(const QString &property_group, const QString &property_name); }; @@ -58,24 +60,5 @@ public: ~TestBeamItem(){} }; -class BA_CORE_API_ BeamDistributionItem : public ParameterizedItem -{ - Q_OBJECT -public: - static const QString P_DISTRIBUTION; - static const QString P_VALUE; - static const QString P_COMBO; - static const QString P_VECTOR; - explicit BeamDistributionItem(ParameterizedItem *parent=0); - ~BeamDistributionItem(){} -// virtual void onPropertyChange(const QString &name); -private: - void initProperties(); -}; - - - - - #endif diff --git a/GUI/coregui/Models/TestItem.cpp b/GUI/coregui/Models/TestItem.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4011c975152fb85c22159b85e959b5863c8f85f --- /dev/null +++ b/GUI/coregui/Models/TestItem.cpp @@ -0,0 +1,36 @@ +// ************************************************************************** // +// +// BornAgain: simulate and fit scattering at grazing incidence +// +//! @file coregui/Models/TestItem.cpp +//! @brief Implements class TestItem +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2015 +//! @authors Scientific Computing Group at MLZ Garching +//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke +// +// ************************************************************************** // + +#include "TestItem.h" +#include "ComboProperty.h" +#include "AngleProperty.h" +#include "Units.h" +#include <QDebug> + +const QString TestItem::P_DISTRIBUTION = "Distribution"; +const QString TestItem::P_VALUE = "Value"; +const QString TestItem::P_COMBO = "Combo"; +const QString TestItem::P_VECTOR = "Vector"; +TestItem::TestItem(ParameterizedItem *parent) + : ParameterizedItem(QString("TestItem"), parent) +{ + setItemName(QString("TestItem")); + registerGroupProperty(P_DISTRIBUTION, Constants::DistributionExtendedGroup); + registerProperty(P_VALUE, 99.0, PropertyAttribute(PropertyAttribute::DISABLED)); + ComboProperty types; + types << "property 1" << "property 2" << "property 3"; + registerProperty(P_COMBO, types.getVariant()); + registerGroupProperty(P_VECTOR, Constants::VectorType); +} diff --git a/GUI/coregui/Models/TestItem.h b/GUI/coregui/Models/TestItem.h new file mode 100644 index 0000000000000000000000000000000000000000..1f3b906bb985386293e79ef6087aef761e169872 --- /dev/null +++ b/GUI/coregui/Models/TestItem.h @@ -0,0 +1,34 @@ +// ************************************************************************** // +// +// BornAgain: simulate and fit scattering at grazing incidence +// +//! @file coregui/Models/TestItem.h +//! @brief Defines class TestItem +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2015 +//! @authors Scientific Computing Group at MLZ Garching +//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke +// +// ************************************************************************** // + +#ifndef TESTITEM_H +#define TESTITEM_H + +#include "ParameterizedItem.h" + +//! The TestItem class for TestParameterizedItem unit tests and for checking AwesomePropertyEditor machinery +class BA_CORE_API_ TestItem : public ParameterizedItem +{ + Q_OBJECT +public: + static const QString P_DISTRIBUTION; + static const QString P_VALUE; + static const QString P_COMBO; + static const QString P_VECTOR; + explicit TestItem(ParameterizedItem *parent=0); + ~TestItem(){} +}; + +#endif diff --git a/GUI/coregui/Views/PropertyEditor/AwesomePropertyEditor.cpp b/GUI/coregui/Views/PropertyEditor/AwesomePropertyEditor.cpp index 24ff22c06eda0d185eb023b2f875d3d3ef409cb0..c2559b23fa3b00dece667c0c50d8515b63dbf494 100644 --- a/GUI/coregui/Views/PropertyEditor/AwesomePropertyEditor.cpp +++ b/GUI/coregui/Views/PropertyEditor/AwesomePropertyEditor.cpp @@ -220,10 +220,6 @@ void AwesomePropertyEditor::onPropertyChanged(const QString &property_name) ParameterizedItem *item = qobject_cast<ParameterizedItem *>(sender()); qDebug() << "AwesomePropertyEditor::onPropertyChanged(const QString &property_name) BROWSER_ID:" << m_d->m_browser_type << "item->modelType(), property_name" << item->modelType() << property_name; - if(m_d->m_current_item_property_pair.m_item == item && m_d->m_current_item_property_pair.m_name == property_name) { - return; - } - QtVariantProperty *variant_property = m_d->m_item_to_property_to_qtvariant[item][property_name]; if(variant_property) { QVariant property_value = item->property(property_name.toUtf8().data()); @@ -256,6 +252,10 @@ void AwesomePropertyEditor::onSubItemChanged(const QString &property_name) ParameterizedItem *item = qobject_cast<ParameterizedItem *>(sender()); qDebug() << "AwesomePropertyEditor::onSubItemChanged(const QString &property_name) BROWSER_ID:" << m_d->m_browser_type << "item->modelType(), property_name" << item->modelType() << property_name; + if(m_d->m_current_item_property_pair.m_item == item && m_d->m_current_item_property_pair.m_name == property_name) { + return; + } + QtVariantProperty *variant_property = m_d->m_item_to_property_to_qtvariant[item][property_name]; if(variant_property) { QVariant property_value = item->property(property_name.toUtf8().data()); diff --git a/GUI/coregui/Views/TestView.cpp b/GUI/coregui/Views/TestView.cpp index 27709405eb3ca5010fa46006ccd7f4c91697a978..14fa3f1a883d3b65b3889dddea6ec56241f35b42 100644 --- a/GUI/coregui/Views/TestView.cpp +++ b/GUI/coregui/Views/TestView.cpp @@ -14,7 +14,7 @@ // ************************************************************************** // #include "TestView.h" -#include "BeamItem.h" +#include "TestItem.h" #include "LayerItem.h" #include "AwesomePropertyEditor.h" #include <QGroupBox> @@ -33,12 +33,11 @@ TestView::TestView(QWidget *parent) // whole content is represented as grid layout QGridLayout *gridLayout = new QGridLayout; - AwesomePropertyEditor *editor1 = new AwesomePropertyEditor(this); editor1->setMinimumSize(256, 256); AwesomePropertyEditor *editor2 = new AwesomePropertyEditor(this, AwesomePropertyEditor::BROWSER_GROUPBOX_TYPE); - BeamDistributionItem *item1 = new BeamDistributionItem; + TestItem *item1 = new TestItem; LayerItem *layer = new LayerItem(); editor1->addItemProperty(layer, LayerItem::P_THICKNESS, "MyGroup"); editor1->addItemProperties(item1, "MyGroup"); diff --git a/GUI/coregui/Views/TestView.h b/GUI/coregui/Views/TestView.h index 04330d7b69634ff472fabbbf41608a19748f714d..209b14223256fe292e2835c39dd8e8c84d2f431f 100644 --- a/GUI/coregui/Views/TestView.h +++ b/GUI/coregui/Views/TestView.h @@ -18,7 +18,6 @@ #include <QWidget> - class TestView : public QWidget { Q_OBJECT @@ -27,10 +26,6 @@ public: TestView(QWidget *parent = 0); virtual ~TestView() {} - }; - - - #endif diff --git a/Tests/UnitTests/TestGUI/TestParameterizedItem.h b/Tests/UnitTests/TestGUI/TestParameterizedItem.h index 069dab6cc4869d4a5e303922382f7ea251466510..d48c36ae38ee0dcf84bf16b31350f401baa7b532 100644 --- a/Tests/UnitTests/TestGUI/TestParameterizedItem.h +++ b/Tests/UnitTests/TestGUI/TestParameterizedItem.h @@ -4,12 +4,17 @@ #include <QtTest> #include "ParameterizedItem.h" +#include "GUIHelpers.h" +#include "verify_throw_macro.h" + + class TestParameterizedItem : public QObject { Q_OBJECT private slots: void test_ItemName(); + void test_registerProperty(); void test_SelectableGroupProperty(); }; @@ -24,15 +29,49 @@ inline void TestParameterizedItem::test_ItemName() QCOMPARE(item.getRegisteredProperty(ParameterizedItem::P_NAME).toString(), QString("NewName")); } -//const QString FormFactorGroup = "Form Factor"; -//const QString LayerRoughnessGroup = "Roughness"; -//const QString DetectorGroup = "Detector group"; -//const QString FTDistribution1DGroup = "PDF 1D"; -//const QString FTDistribution2DGroupA = "PDF 2D #1"; -//const QString FTDistribution2DGroupB = "PDF 2D #2"; -//const QString LatticeGroup = "Lattice_type"; +inline void TestParameterizedItem::test_registerProperty() +{ + ParameterizedItem item; + QString property_name("MyProperty"); + double value(1.0); + QSignalSpy spy(&item, SIGNAL(propertyChanged(QString))); + + // access non-existing property + QCOMPARE(false, item.isRegisteredProperty(property_name)); + QVERIFY_THROW(item.getRegisteredProperty(property_name), GUIHelpers::Error); + QVERIFY_THROW(item.setRegisteredProperty(property_name, value), GUIHelpers::Error); + + // registering new property + item.registerProperty(property_name, value); + QCOMPARE(true, item.isRegisteredProperty(property_name)); + QCOMPARE(spy.count(), 1); + QList<QVariant> arguments = spy.takeFirst(); + QCOMPARE(arguments.size(), 1); + QCOMPARE(arguments.at(0).toString(), property_name); + QCOMPARE(item.getRegisteredProperty(property_name).toDouble(), value); + QCOMPARE(spy.count(), 0); + + // setting property value + double new_value(2.0); + item.setRegisteredProperty(property_name, new_value); + QCOMPARE(spy.count(), 1); + arguments = spy.takeFirst(); + QCOMPARE(arguments.size(), 1); + QCOMPARE(arguments.at(0).toString(), property_name); + QCOMPARE(item.getRegisteredProperty(property_name).toDouble(), new_value); + // setting property value to wrong QVariant + QVERIFY_THROW(item.setRegisteredProperty(property_name, QString("aaa")), GUIHelpers::Error); + // remove registered property + item.removeRegisteredProperty(property_name); + QCOMPARE(spy.count(), 1); + arguments = spy.takeFirst(); + QCOMPARE(arguments.size(), 1); + QCOMPARE(arguments.at(0).toString(), property_name); + QVERIFY_THROW(item.getRegisteredProperty(property_name), GUIHelpers::Error); + +} inline void TestParameterizedItem::test_SelectableGroupProperty() { diff --git a/Tests/UnitTests/TestGUI/verify_throw_macro.h b/Tests/UnitTests/TestGUI/verify_throw_macro.h new file mode 100644 index 0000000000000000000000000000000000000000..ceb3dbb9165a0d861cbe794211f435d82c90ffe6 --- /dev/null +++ b/Tests/UnitTests/TestGUI/verify_throw_macro.h @@ -0,0 +1,33 @@ +// ************************************************************************** // +// +// BornAgain: simulate and fit scattering at grazing incidence +// +//! @file coregui/Models/verify_throw_macro.h +//! @brief Defines macro to test exception throw +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2015 +//! @authors Scientific Computing Group at MLZ Garching +//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke +// +// ************************************************************************** // + +#ifndef VERIFY_THROW_MACRO_H +#define VERIFY_THROW_MACRO_H + +#include <QTest> + +#define QVERIFY_THROW(expression, ExpectedExceptionType) \ +do \ +{ \ +bool caught_ = false; \ +try { expression; } \ +catch (ExpectedExceptionType const&) { caught_ = true; } \ +catch (...) {} \ +if (!QTest::qVerify(caught_, #expression ", " #ExpectedExceptionType, "", __FILE__, __LINE__))\ +return; \ +} while(0) + + +#endif