diff --git a/GUI/Model/Sample/FTDecayFunctionItemCatalogs.cpp b/GUI/Model/Sample/FTDecayFunctionItemCatalogs.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0648f2d12c8ab9299bff713d1ced97c833fe49a1 --- /dev/null +++ b/GUI/Model/Sample/FTDecayFunctionItemCatalogs.cpp @@ -0,0 +1,136 @@ +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Model/Sample/FTDecayFunctionItemCatalogs.cpp +//! @brief Implements FTDecayFunctionItemCatalog classes +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2021 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************************************ + +#include "GUI/Model/Sample/FTDecayFunctionItemCatalogs.h" +#include "Base/Util/Assert.h" +#include "GUI/Model/Sample/FTDecayFunctionItems.h" + +FTDecayFunction1DItemCatalog::CatalogedType* FTDecayFunction1DItemCatalog::create(Type type) +{ + switch (type) { + case Type::Cauchy: + return new FTDecayFunction1DCauchyItem(); + case Type::Gauss: + return new FTDecayFunction1DGaussItem(); + case Type::Triangle: + return new FTDecayFunction1DTriangleItem(); + case Type::Voigt: + return new FTDecayFunction1DVoigtItem(); + default: + ASSERT(false); + } +} + +QVector<FTDecayFunction1DItemCatalog::Type> FTDecayFunction1DItemCatalog::types() +{ + return {Type::Cauchy, Type::Gauss, Type::Triangle, Type::Voigt}; +} + +FTDecayFunction1DItemCatalog::UiInfo FTDecayFunction1DItemCatalog::uiInfo(Type type) +{ + auto createUiInfo = [](const QString& menuEntry, const QString& description) { + UiInfo info; + info.menuEntry = menuEntry; + info.description = description; + return info; + }; + + switch (type) { + case Type::Cauchy: + return createUiInfo("Cauchy 1D", "One-dimensional Cauchy decay function"); + case Type::Gauss: + return createUiInfo("Gauss 1D", "One-dimensional Gauss decay function"); + case Type::Triangle: + return createUiInfo("Triangle 1D", "One-dimensional triangle decay function"); + case Type::Voigt: + return createUiInfo("Voigt 1D", "One-dimensional pseudo-Voigt decay function"); + default: + ASSERT(false); + } +} + +FTDecayFunction1DItemCatalog::Type FTDecayFunction1DItemCatalog::type(CatalogedType* item) +{ + ASSERT(item); + +#define CHECK(type) \ + if (dynamic_cast<FTDecayFunction1D##type##Item*>(item)) \ + return Type::type + + CHECK(Cauchy); + CHECK(Gauss); + CHECK(Triangle); + CHECK(Voigt); +#undef CHECK + + ASSERT(false); +} + +/* --------------------------------------------------------------------------------------------- */ + +FTDecayFunction2DItemCatalog::CatalogedType* FTDecayFunction2DItemCatalog::create(Type type) +{ + switch (type) { + case Type::Cauchy: + return new FTDecayFunction2DCauchyItem(); + case Type::Gauss: + return new FTDecayFunction2DGaussItem(); + case Type::Voigt: + return new FTDecayFunction2DVoigtItem(); + default: + ASSERT(false); + } +} + +QVector<FTDecayFunction2DItemCatalog::Type> FTDecayFunction2DItemCatalog::types() +{ + return {Type::Cauchy, Type::Gauss, Type::Voigt}; +} + +FTDecayFunction2DItemCatalog::UiInfo FTDecayFunction2DItemCatalog::uiInfo(Type type) +{ + auto createUiInfo = [](const QString& menuEntry, const QString& description) { + UiInfo info; + info.menuEntry = menuEntry; + info.description = description; + return info; + }; + + switch (type) { + case Type::Cauchy: + return createUiInfo("Cauchy 2D", "Two-dimensional Cauchy decay function"); + case Type::Gauss: + return createUiInfo("Gauss 2D", "Two-dimensional Gauss decay function"); + case Type::Voigt: + return createUiInfo("Voigt 2D", "Two-dimensional pseudo-Voigt decay function"); + default: + ASSERT(false); + } +} + +FTDecayFunction2DItemCatalog::Type FTDecayFunction2DItemCatalog::type(CatalogedType* item) +{ + ASSERT(item); + +#define CHECK(type) \ + if (dynamic_cast<FTDecayFunction2D##type##Item*>(item)) \ + return Type::type + + CHECK(Cauchy); + CHECK(Gauss); + CHECK(Voigt); +#undef CHECK + + ASSERT(false); +} diff --git a/GUI/Model/Sample/FTDecayFunctionItemCatalogs.h b/GUI/Model/Sample/FTDecayFunctionItemCatalogs.h new file mode 100644 index 0000000000000000000000000000000000000000..9e98ee82dee4dece623510e98b364a46bfc23eb1 --- /dev/null +++ b/GUI/Model/Sample/FTDecayFunctionItemCatalogs.h @@ -0,0 +1,77 @@ +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Model/Sample/FTDecayFunctionItemCatalogs.h +//! @brief Defines FTDecayFunctionItemCatalog classes +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2021 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************************************ + +#ifndef BORNAGAIN_GUI_MODEL_SAMPLE_FTDECAYFUNCTIONITEMCATALOGS_H +#define BORNAGAIN_GUI_MODEL_SAMPLE_FTDECAYFUNCTIONITEMCATALOGS_H + +#include <QString> +#include <QVector> + +class FTDecayFunction1DItem; +class FTDecayFunction2DItem; + +class FTDecayFunction1DItemCatalog { +public: + using CatalogedType = FTDecayFunction1DItem; + + // Do not change the numbering! It is serialized! + enum class Type : uint8_t { Cauchy = 1, Gauss = 2, Triangle = 3, Voigt = 4 }; + + struct UiInfo { + QString menuEntry; + QString description; + QString iconPath; + }; + + //! Creates the item of the given type. + static CatalogedType* create(Type type); + + //! List of available types, sorted as expected in the UI. + static QVector<Type> types(); + + //! UiInfo on the given type. + static UiInfo uiInfo(Type t); + + //! Returns the enum type of the given item. + static Type type(CatalogedType* item); +}; + +class FTDecayFunction2DItemCatalog { +public: + using CatalogedType = FTDecayFunction2DItem; + + // Do not change the numbering! It is serialized! + enum class Type : uint8_t { Cauchy = 1, Gauss = 2, Voigt = 3 }; + + struct UiInfo { + QString menuEntry; + QString description; + QString iconPath; + }; + + //! Creates the item of the given type. + static CatalogedType* create(Type type); + + //! List of available types, sorted as expected in the UI. + static QVector<Type> types(); + + //! UiInfo on the given type. + static UiInfo uiInfo(Type t); + + //! Returns the enum type of the given item. + static Type type(CatalogedType* item); +}; + + +#endif // BORNAGAIN_GUI_MODEL_SAMPLE_FTDECAYFUNCTIONITEMCATALOGS_H diff --git a/GUI/Model/Sample/FTDecayFunctionItems.cpp b/GUI/Model/Sample/FTDecayFunctionItems.cpp index df49062ddad9304f22cf6376cb05e2dec0d29051..fcf4001067f498ad7dfb2b4a8bc5ddea408b36cb 100644 --- a/GUI/Model/Sample/FTDecayFunctionItems.cpp +++ b/GUI/Model/Sample/FTDecayFunctionItems.cpp @@ -14,16 +14,19 @@ #include "GUI/Model/Sample/FTDecayFunctionItems.h" #include "Base/Const/Units.h" +#include <QList> // --------------------------------------------------------------------------------------------- // -FTDecayFunction1DItem::FTDecayFunction1DItem(const QString& name) : SessionItem(name) {} +FTDecayFunction1DItem::FTDecayFunction1DItem() +{ + m_decayLength.init("Decay length", "Decay length (half-width of the distribution)", 1000.0, + Unit::nanometer, "decay"); +} DoubleDescriptor FTDecayFunction1DItem::decayLength() const { - DoubleDescriptor d(getItem(P_DECAY_LENGTH), Unit::nanometer); - d.tooltip = "Decay length (half-width of the distribution)"; - return d; + return m_decayLength; } DoubleDescriptors FTDecayFunction1DItem::valueDescriptors() const @@ -31,20 +34,8 @@ DoubleDescriptors FTDecayFunction1DItem::valueDescriptors() const return {decayLength()}; } -void FTDecayFunction1DItem::add_decay_property() -{ - addProperty(P_DECAY_LENGTH, 1000.0) - ->setToolTip("Decay length (half-width of the distribution in nanometers)"); -} - // --------------------------------------------------------------------------------------------- // -FTDecayFunction1DCauchyItem::FTDecayFunction1DCauchyItem() : FTDecayFunction1DItem(M_TYPE) -{ - setToolTip("One-dimensional Cauchy decay function"); - add_decay_property(); -} - std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DCauchyItem::createFTDecayFunction() const { return std::make_unique<FTDecayFunction1DCauchy>(decayLength()); @@ -52,12 +43,6 @@ std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DCauchyItem::createFTDecayFu // --------------------------------------------------------------------------------------------- // -FTDecayFunction1DGaussItem::FTDecayFunction1DGaussItem() : FTDecayFunction1DItem(M_TYPE) -{ - setToolTip("One-dimensional Gauss decay function"); - add_decay_property(); -} - std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DGaussItem::createFTDecayFunction() const { return std::make_unique<FTDecayFunction1DGauss>(decayLength()); @@ -65,12 +50,6 @@ std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DGaussItem::createFTDecayFun // --------------------------------------------------------------------------------------------- // -FTDecayFunction1DTriangleItem::FTDecayFunction1DTriangleItem() : FTDecayFunction1DItem(M_TYPE) -{ - setToolTip("One-dimensional triangle decay function"); - add_decay_property(); -} - std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DTriangleItem::createFTDecayFunction() const { return std::make_unique<FTDecayFunction1DTriangle>(decayLength()); @@ -78,23 +57,20 @@ std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DTriangleItem::createFTDecay // --------------------------------------------------------------------------------------------- // -FTDecayFunction1DVoigtItem::FTDecayFunction1DVoigtItem() : FTDecayFunction1DItem(M_TYPE) +FTDecayFunction1DVoigtItem::FTDecayFunction1DVoigtItem() { - setToolTip("One-dimensional pseudo-Voigt decay function"); - add_decay_property(); - addProperty(P_ETA, 0.5) - ->setLimits(RealLimits::limited(0.0, 1.0)) - .setToolTip("Parameter [0,1] to balance between Cauchy (eta=0.0) and Gauss (eta=1.0)"); + m_eta.init("Eta", "Parameter [0,1] to balance between Cauchy (eta=0.0) and Gauss (eta=1.0)", + 0.5, Unit::unitless, 3, RealLimits::limited(0.0, 1.0), "eta"); } std::unique_ptr<IFTDecayFunction1D> FTDecayFunction1DVoigtItem::createFTDecayFunction() const { - return std::make_unique<FTDecayFunction1DVoigt>(decayLength(), getItemValue(P_ETA).toDouble()); + return std::make_unique<FTDecayFunction1DVoigt>(decayLength(), m_eta); } DoubleDescriptor FTDecayFunction1DVoigtItem::eta() const { - return DoubleDescriptor(getItem(P_ETA), Unit::unitless); + return m_eta; } DoubleDescriptors FTDecayFunction1DVoigtItem::valueDescriptors() const @@ -104,40 +80,48 @@ DoubleDescriptors FTDecayFunction1DVoigtItem::valueDescriptors() const // --------------------------------------------------------------------------------------------- // +FTDecayFunction2DItem::FTDecayFunction2DItem() +{ + m_decayLengthX.init( + "Decay length X", + "Decay length (half-width of the distribution) along x-axis of the distribution", 1000.0, + Unit::nanometer, "decayX"); + m_decayLengthY.init( + "Decay length Y", + "Decay length (half-width of the distribution) along y-axis of the distribution", 1000.0, + Unit::nanometer, "decayY"); + m_gamma.init("Gamma", "Distribution orientation with respect to the first lattice vector", 0.0, + Unit::degree, "gamma"); +} + DoubleDescriptor FTDecayFunction2DItem::decayLengthX() const { - DoubleDescriptor d(getItem(P_DECAY_LENGTH_X), Unit::nanometer); - d.tooltip = "Decay length (half-width of the distribution) along x-axis of the distribution"; - return d; + return m_decayLengthX; } -void FTDecayFunction2DItem::setDecayLengthX(const double decay_length_x) +void FTDecayFunction2DItem::setDecayLengthX(const double len) { - setItemValue(P_DECAY_LENGTH_X, decay_length_x); + m_decayLengthX.set(len); } DoubleDescriptor FTDecayFunction2DItem::decayLengthY() const { - DoubleDescriptor d(getItem(P_DECAY_LENGTH_Y), Unit::nanometer); - d.tooltip = "Decay length (half-width of the distribution) along y-axis of the distribution"; - return d; + return m_decayLengthY; } -void FTDecayFunction2DItem::setDecayLengthY(const double decay_length_y) +void FTDecayFunction2DItem::setDecayLengthY(const double len) { - setItemValue(P_DECAY_LENGTH_Y, decay_length_y); + m_decayLengthY.set(len); } DoubleDescriptor FTDecayFunction2DItem::gamma() const { - DoubleDescriptor d(getItem(P_GAMMA), Unit::degree); - d.tooltip = "Distribution orientation with respect to the first lattice vector"; - return d; + return m_gamma; } void FTDecayFunction2DItem::setGamma(const double gamma) { - setItemValue(P_GAMMA, gamma); + m_gamma.set(gamma); } DoubleDescriptors FTDecayFunction2DItem::valueDescriptors() const @@ -145,35 +129,8 @@ DoubleDescriptors FTDecayFunction2DItem::valueDescriptors() const return {decayLengthX(), decayLengthY(), gamma()}; } -FTDecayFunction2DItem::FTDecayFunction2DItem(const QString& name) : SessionItem(name) {} - -void FTDecayFunction2DItem::add_decay_property() -{ - addProperty(P_DECAY_LENGTH_X, 1000.0) - ->setToolTip("Decay length (half-width of the distribution in nanometers) " - "\nalong x-axis of the distribution"); - addProperty(P_DECAY_LENGTH_Y, 1000.0) - ->setToolTip("Decay length (half-width of the distribution in nanometers) " - "\nalong y-axis of the distribution"); -} - -void FTDecayFunction2DItem::add_gammadelta_property() -{ - addProperty(P_GAMMA, 0.0) - ->setToolTip( - "Distribution orientation with respect to the first lattice vector in degrees"); - addProperty(P_DELTA, 90.0)->setVisible(false); -} - // --------------------------------------------------------------------------------------------- // -FTDecayFunction2DCauchyItem::FTDecayFunction2DCauchyItem() : FTDecayFunction2DItem(M_TYPE) -{ - setToolTip("Two-dimensional Cauchy decay function"); - add_decay_property(); - add_gammadelta_property(); -} - std::unique_ptr<IFTDecayFunction2D> FTDecayFunction2DCauchyItem::createFTDecayFunction() const { return std::make_unique<FTDecayFunction2DCauchy>(decayLengthX(), decayLengthY(), @@ -182,13 +139,6 @@ std::unique_ptr<IFTDecayFunction2D> FTDecayFunction2DCauchyItem::createFTDecayFu // --------------------------------------------------------------------------------------------- // -FTDecayFunction2DGaussItem::FTDecayFunction2DGaussItem() : FTDecayFunction2DItem(M_TYPE) -{ - setToolTip("Two-dimensional Gauss decay function"); - add_decay_property(); - add_gammadelta_property(); -} - std::unique_ptr<IFTDecayFunction2D> FTDecayFunction2DGaussItem::createFTDecayFunction() const { return std::make_unique<FTDecayFunction2DGauss>(decayLengthX(), decayLengthY(), @@ -197,30 +147,26 @@ std::unique_ptr<IFTDecayFunction2D> FTDecayFunction2DGaussItem::createFTDecayFun // --------------------------------------------------------------------------------------------- // -FTDecayFunction2DVoigtItem::FTDecayFunction2DVoigtItem() : FTDecayFunction2DItem(M_TYPE) +FTDecayFunction2DVoigtItem::FTDecayFunction2DVoigtItem() { - setToolTip("Two-dimensional pseudo-Voigt decay function"); - add_decay_property(); - addProperty(P_ETA, 0.5) - ->setLimits(RealLimits::limited(0.0, 1.0)) - .setToolTip("Parameter [0,1] to balance between Cauchy (eta=0.0) and Gauss (eta=1.0)"); - add_gammadelta_property(); + m_eta.init("Eta", "Parameter [0,1] to balance between Cauchy (eta=0.0) and Gauss (eta=1.0)", + 0.5, Unit::unitless, 3, RealLimits::limited(0.0, 1.0), "eta"); } std::unique_ptr<IFTDecayFunction2D> FTDecayFunction2DVoigtItem::createFTDecayFunction() const { - return std::make_unique<FTDecayFunction2DVoigt>( - decayLengthX(), decayLengthY(), getItemValue(P_ETA).toDouble(), Units::deg2rad(gamma())); + return std::make_unique<FTDecayFunction2DVoigt>(decayLengthX(), decayLengthY(), m_eta, + Units::deg2rad(gamma())); } void FTDecayFunction2DVoigtItem::setEta(const double eta) { - setItemValue(P_ETA, eta); + m_eta.set(eta); } DoubleDescriptor FTDecayFunction2DVoigtItem::eta() const { - return DoubleDescriptor(getItem(P_ETA), Unit::unitless); + return m_eta; } DoubleDescriptors FTDecayFunction2DVoigtItem::valueDescriptors() const diff --git a/GUI/Model/Sample/FTDecayFunctionItems.h b/GUI/Model/Sample/FTDecayFunctionItems.h index 1bf8930957500b411f5e3a2e7ff781d2d5017aaa..926ee91f980c615a1ee81d07f2b7bd572e695c3b 100644 --- a/GUI/Model/Sample/FTDecayFunctionItems.h +++ b/GUI/Model/Sample/FTDecayFunctionItems.h @@ -15,78 +15,57 @@ #ifndef BORNAGAIN_GUI_MODEL_SAMPLE_FTDECAYFUNCTIONITEMS_H #define BORNAGAIN_GUI_MODEL_SAMPLE_FTDECAYFUNCTIONITEMS_H -#include "GUI/Model/Session/SessionItem.h" #include "GUI/Model/Types/DoubleDescriptor.h" +#include "GUI/Model/Types/DoubleProperty.h" #include "Sample/Correlations/FTDecay1D.h" #include "Sample/Correlations/FTDecay2D.h" -class FTDecayFunction1DItem : public SessionItem { -private: - static constexpr auto P_DECAY_LENGTH{"DecayLength"}; - +class FTDecayFunction1DItem { public: + ~FTDecayFunction1DItem() = default; virtual std::unique_ptr<IFTDecayFunction1D> createFTDecayFunction() const = 0; - ~FTDecayFunction1DItem() override = default; DoubleDescriptor decayLength() const; - virtual DoubleDescriptors valueDescriptors() const; protected: - explicit FTDecayFunction1DItem(const QString& name); - void add_decay_property(); + FTDecayFunction1DItem(); + DoubleProperty m_decayLength; }; class FTDecayFunction1DCauchyItem : public FTDecayFunction1DItem { public: - static constexpr auto M_TYPE{"FTDecayFunction1DCauchy"}; - - FTDecayFunction1DCauchyItem(); std::unique_ptr<IFTDecayFunction1D> createFTDecayFunction() const override; }; class FTDecayFunction1DGaussItem : public FTDecayFunction1DItem { public: - static constexpr auto M_TYPE{"FTDecayFunction1DGauss"}; - - FTDecayFunction1DGaussItem(); std::unique_ptr<IFTDecayFunction1D> createFTDecayFunction() const override; }; class FTDecayFunction1DTriangleItem : public FTDecayFunction1DItem { public: - static constexpr auto M_TYPE{"FTDecayFunction1DTriangle"}; - - FTDecayFunction1DTriangleItem(); std::unique_ptr<IFTDecayFunction1D> createFTDecayFunction() const override; }; class FTDecayFunction1DVoigtItem : public FTDecayFunction1DItem { -private: - static constexpr auto P_ETA{"Eta"}; - public: - static constexpr auto M_TYPE{"FTDecayFunction1DVoigt"}; - FTDecayFunction1DVoigtItem(); std::unique_ptr<IFTDecayFunction1D> createFTDecayFunction() const override; DoubleDescriptor eta() const; DoubleDescriptors valueDescriptors() const override; + +private: + DoubleProperty m_eta; }; // --------------------------------------------------------------------------------------------- // -class FTDecayFunction2DItem : public SessionItem { -private: - static constexpr auto P_DECAY_LENGTH_X{"DecayLengthX"}; - static constexpr auto P_DECAY_LENGTH_Y{"DecayLengthY"}; - static constexpr auto P_GAMMA{"Gamma"}; - static constexpr auto P_DELTA{"Delta"}; - +class FTDecayFunction2DItem { public: + ~FTDecayFunction2DItem() = default; virtual std::unique_ptr<IFTDecayFunction2D> createFTDecayFunction() const = 0; - ~FTDecayFunction2DItem() override = default; DoubleDescriptor decayLengthX() const; void setDecayLengthX(double decay_length_x); @@ -100,34 +79,25 @@ public: virtual DoubleDescriptors valueDescriptors() const; protected: - explicit FTDecayFunction2DItem(const QString& name); - void add_decay_property(); - void add_gammadelta_property(); + FTDecayFunction2DItem(); + + DoubleProperty m_decayLengthX; + DoubleProperty m_decayLengthY; + DoubleProperty m_gamma; }; class FTDecayFunction2DCauchyItem : public FTDecayFunction2DItem { public: - static constexpr auto M_TYPE{"FTDecayFunction2DCauchy"}; - - FTDecayFunction2DCauchyItem(); std::unique_ptr<IFTDecayFunction2D> createFTDecayFunction() const override; }; class FTDecayFunction2DGaussItem : public FTDecayFunction2DItem { public: - static constexpr auto M_TYPE{"FTDecayFunction2DGauss"}; - - FTDecayFunction2DGaussItem(); std::unique_ptr<IFTDecayFunction2D> createFTDecayFunction() const override; }; class FTDecayFunction2DVoigtItem : public FTDecayFunction2DItem { -private: - static constexpr auto P_ETA{"Eta"}; - public: - static constexpr auto M_TYPE{"FTDecayFunction2DVoigt"}; - FTDecayFunction2DVoigtItem(); std::unique_ptr<IFTDecayFunction2D> createFTDecayFunction() const override; @@ -135,6 +105,9 @@ public: DoubleDescriptor eta() const; virtual DoubleDescriptors valueDescriptors() const override; + +private: + DoubleProperty m_eta; }; #endif // BORNAGAIN_GUI_MODEL_SAMPLE_FTDECAYFUNCTIONITEMS_H