diff --git a/GUI/coregui/Models/AngleProperty.cpp b/GUI/coregui/Models/AngleProperty.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82b5cd0d2929ab84467d0ee6cd7bb72d85fffb00 --- /dev/null +++ b/GUI/coregui/Models/AngleProperty.cpp @@ -0,0 +1,146 @@ +#include "AngleProperty.h" +#include "Units.h" +#include "GUIHelpers.h" +#include "item_constants.h" + +QStringList AngleProperty::m_labels = QStringList() + << Constants::UnitsDegrees << Constants::UnitsRadians; + +//AngleProperty::AngleProperty(double angle_value, AngleUnits angle_units) +// : m_angle_in_radians(0) +// , m_angle_units(angle_units) +//{ +// if(m_angle_units == AngleProperty::Radians) { +// m_angle_in_radians = angle_value; +// } +// else { +// m_angle_in_radians = Units::deg2rad(angle_value); +// } +//} + + +AngleProperty::AngleProperty(double angle_value, const QString &angle_units) + : m_angle_in_radians(0) + , m_angle_units(angle_units) +{ + if(m_angle_units == Constants::UnitsRadians) { + m_angle_in_radians = angle_value; + } + else if(m_angle_units == Constants::UnitsDegrees){ + m_angle_in_radians = Units::deg2rad(angle_value); + } + else { + throw GUIHelpers::Error("AngleProperty::AngleProperty() -> Error. Unknown units"); + } +} + + +//AngleProperty::AngleUnits AngleProperty::getUnits() const +//{ +// return m_angle_units; +//} + + +//void AngleProperty::setUnits(AngleUnits units) +//{ +// m_angle_units = units; +//} + + +QString AngleProperty::getUnits() const +{ +// return m_labels[m_angle_units]; + return m_angle_units; +} + +void AngleProperty::setUnits(const QString &units) +{ + if(!m_labels.contains(units)) + throw GUIHelpers::Error("AngleProperty::setUnitsString() -> Error. Unknown units"); + + m_angle_units = units; +// if(units == Constants::UnitsDegrees) { +// m_angle_units = Degrees; +// } +// else { +// m_angle_units = Radians; +// } +} + + +bool AngleProperty::inRadians() const +{ + return m_angle_units == Constants::UnitsRadians; +} + + +void AngleProperty::setInRadians() +{ + m_angle_units = Constants::UnitsRadians; +} + + +bool AngleProperty::inDegrees() const +{ + return m_angle_units == Constants::UnitsDegrees; +} + + +void AngleProperty::setInDegrees() +{ + m_angle_units = Constants::UnitsDegrees; +} + + +double AngleProperty::getValue() const +{ +// return (m_angle_units == Radians ? m_angle_in_radians : Units::rad2deg(m_angle_in_radians)); + return (m_angle_units == Constants::UnitsRadians ? m_angle_in_radians : Units::rad2deg(m_angle_in_radians)); +} + + +void AngleProperty::setValue(double value) +{ + if(m_angle_units == Constants::UnitsRadians) { + m_angle_in_radians = value; + } + else { + m_angle_in_radians = Units::deg2rad(value); + } +} + + +double AngleProperty::getValueInRadians() const +{ + return m_angle_in_radians; +} + + +QStringList AngleProperty::getLabels() const +{ + return m_labels; +} + +QVariant AngleProperty::Degrees(double value) +{ + AngleProperty angle(value, Constants::UnitsDegrees); + QVariant result; + result.setValue(angle); + return result; +} + +QVariant AngleProperty::Radians(double value) +{ + AngleProperty angle(value, Constants::UnitsRadians); + QVariant result; + result.setValue(angle); + return result; +} + + +QVariant AngleProperty::getVariant() const +{ + QVariant result; + result.setValue(*this); + return result; +} diff --git a/GUI/coregui/Models/AngleProperty.h b/GUI/coregui/Models/AngleProperty.h index 587afeaa157a590ea16f3cf465646570671d661a..67d0e701b4b65587784a819d6c80fc2d5c5f96c7 100644 --- a/GUI/coregui/Models/AngleProperty.h +++ b/GUI/coregui/Models/AngleProperty.h @@ -5,18 +5,18 @@ #include <QStringList> #include <QMetaType> #include <QVariant> -#include "Units.h" -//! The AngleProperty keeps angle value together with units (radias, degrees) +//! The AngleProperty keeps angle value together with units (radians, degrees) //! in which the angle should be presented to the user. class AngleProperty { public: - enum AngleUnits { Radians, Degrees}; - - AngleProperty(double angle_value = 0.0, AngleUnits angle_units = Radians); + AngleProperty(double angle_value = 0.0, const QString &angle_units = QString()); ~AngleProperty(){} + QString getUnits() const; + void setUnits(const QString &units); + bool inRadians() const; void setInRadians(); @@ -31,75 +31,19 @@ public: double getValueInRadians() const; + QStringList getLabels() const; + + static QVariant Degrees(double value = 0.0); + static QVariant Radians(double value = 0.0); + QVariant getVariant() const; private: double m_angle_in_radians; - AngleUnits m_angle_units; + QString m_angle_units; + static QStringList m_labels; }; - -inline AngleProperty::AngleProperty(double angle_value, AngleUnits angle_units) - : m_angle_in_radians(0) - , m_angle_units(angle_units) -{ - if(m_angle_units == AngleProperty::Radians) { - m_angle_in_radians = angle_value; - } - else { - m_angle_in_radians = Units::deg2rad(angle_value); - } -} - -inline bool AngleProperty::inRadians() const -{ - return m_angle_units == Radians; -} - -inline void AngleProperty::setInRadians() -{ - m_angle_units = Radians; -} - -inline bool AngleProperty::inDegrees() const -{ - return m_angle_units == Degrees; -} - -inline void AngleProperty::setInDegrees() -{ - m_angle_units = Degrees; -} - -inline double AngleProperty::getValue() const -{ - return (m_angle_units == Radians ? m_angle_in_radians : Units::rad2deg(m_angle_in_radians)); -} - -inline void AngleProperty::setValue(double value) -{ - if(m_angle_units == Radians) { - m_angle_in_radians = value; - } - else { - m_angle_in_radians = Units::deg2rad(value); - } -} - -inline double AngleProperty::getValueInRadians() const -{ - return m_angle_in_radians; -} - -inline QVariant AngleProperty::getVariant() const -{ - QVariant result; - result.setValue(*this); - return result; -} - Q_DECLARE_METATYPE(AngleProperty) - - #endif diff --git a/GUI/coregui/Models/BeamItem.cpp b/GUI/coregui/Models/BeamItem.cpp index 34f1ffbd84f94a9aaca459980295d4d8992ea8a8..515103a007e8eeb636283ee5a0b9f2b6d3975ede 100644 --- a/GUI/coregui/Models/BeamItem.cpp +++ b/GUI/coregui/Models/BeamItem.cpp @@ -10,8 +10,6 @@ const QString BeamItem::P_WAVELENGTH = "Wavelength [nm]"; const QString BeamItem::P_INCLINATION_ANGLE = "Inclination Angle"; const QString BeamItem::P_AZIMUTHAL_ANGLE = "Azimuthal Angle"; const QString BeamItem::P_ANGLE_UNITS = "Angle units"; -const QString BeamItem::P_AZIMUTHAL_ANGLE2 = "Azimuthal Angle 2"; -const QString BeamItem::P_INCLINATION_ANGLE2 = "Inclination Angle 2"; BeamItem::BeamItem(ParameterizedItem *parent) : ParameterizedItem(Constants::BeamType, parent) @@ -20,16 +18,9 @@ BeamItem::BeamItem(ParameterizedItem *parent) registerProperty(P_INTENSITY, 1e+08); registerProperty(P_WAVELENGTH, 0.1); - registerProperty(P_INCLINATION_ANGLE, 0.2); - registerProperty(P_AZIMUTHAL_ANGLE, 0.0); - - AngleProperty inclination_angle(0.2, AngleProperty::Degrees); - registerProperty(P_INCLINATION_ANGLE2, inclination_angle.getVariant()); - - ComboProperty units; - units << "Degrees" << "Radians"; - registerProperty(P_ANGLE_UNITS, units.getVariant()); - + registerProperty(P_INCLINATION_ANGLE, AngleProperty::Degrees(0.2)); + registerProperty(P_AZIMUTHAL_ANGLE, AngleProperty::Degrees(0.1)); + registerProperty(P_ANGLE_UNITS, AngleProperty::Degrees()); } @@ -37,22 +28,17 @@ void BeamItem::onPropertyChange(const QString &name) { if(name == P_ANGLE_UNITS) { qDebug() << "BeamItem::onPropertyChange()" << name; - ComboProperty angle_units_property = getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<ComboProperty>(); + AngleProperty angle_units_property = getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<AngleProperty>(); qDebug() << " angle_units_property" << angle_units_property.getValue(); - AngleProperty inclination_angle = getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE2).value<AngleProperty>(); - if(angle_units_property.getValue() == "Degrees") { - inclination_angle.setInDegrees(); - } else { - inclination_angle.setInRadians(); - } - setRegisteredProperty(P_INCLINATION_ANGLE2, inclination_angle.getVariant()); - - + AngleProperty inclination_angle = getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE).value<AngleProperty>(); + inclination_angle.setUnits(angle_units_property.getUnits()); + setRegisteredProperty(P_INCLINATION_ANGLE, inclination_angle.getVariant()); -// if(angle_units_property.getValue() == "Degrees") { + AngleProperty azimuthal_angle = getRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE).value<AngleProperty>(); + azimuthal_angle.setUnits(angle_units_property.getUnits()); + setRegisteredProperty(P_AZIMUTHAL_ANGLE, azimuthal_angle.getVariant()); -// } } ParameterizedItem::onPropertyChange(name); diff --git a/GUI/coregui/Models/BeamItem.h b/GUI/coregui/Models/BeamItem.h index 577c579027b59731e5f2126b6e2a8d329b912219..c9657fdb77e1338bf598a3d3239f3d34abe641c0 100644 --- a/GUI/coregui/Models/BeamItem.h +++ b/GUI/coregui/Models/BeamItem.h @@ -8,7 +8,7 @@ class BeamItem : public ParameterizedItem { Q_OBJECT public: - static const QString P_INTENSITY, P_WAVELENGTH, P_ANGLE_UNITS, P_INCLINATION_ANGLE, P_AZIMUTHAL_ANGLE, P_INCLINATION_ANGLE2, P_AZIMUTHAL_ANGLE2; + static const QString P_INTENSITY, P_WAVELENGTH, P_ANGLE_UNITS, P_INCLINATION_ANGLE, P_AZIMUTHAL_ANGLE; explicit BeamItem(ParameterizedItem *parent=0); ~BeamItem(){} diff --git a/GUI/coregui/Models/item_constants.h b/GUI/coregui/Models/item_constants.h index 979fca30239857f0150f05acd1c49bf057a62c70..085f872187a3a43de33f44c9a0e83b0d96410145 100644 --- a/GUI/coregui/Models/item_constants.h +++ b/GUI/coregui/Models/item_constants.h @@ -84,6 +84,9 @@ const ModelType FTDistribution2DGroup = "PDF 2D"; const ModelType LatticeGroup = "Lattice group"; const ModelType MaterialGroup = "Material group"; +// --- Units ------------------------------------------------------------------- +const ModelType UnitsDegrees = "Degrees"; +const ModelType UnitsRadians = "Radians"; } diff --git a/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.cpp b/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.cpp index e58e14b61d80e0f7329201e5696138ef26d6007d..5b255c1a0b71689561a7b7efec3626d0cd5deff7 100644 --- a/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.cpp +++ b/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.cpp @@ -112,11 +112,13 @@ void BeamEditorWidget::onChangedAngle(const QString &) if(m_block_signals) return; // m_currentItem->setRegisteredProperty(BeamItem::P_INCLINATION_ANGLE, m_inclinationAngleSpinBox->value()); - AngleProperty inclination_angle = m_currentItem->getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE2).value<AngleProperty>(); + AngleProperty inclination_angle = m_currentItem->getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE).value<AngleProperty>(); inclination_angle.setValue(m_inclinationAngleSpinBox->value()); - m_currentItem->setRegisteredProperty(BeamItem::P_INCLINATION_ANGLE2, inclination_angle.getVariant()); + m_currentItem->setRegisteredProperty(BeamItem::P_INCLINATION_ANGLE, inclination_angle.getVariant()); - m_currentItem->setRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE, m_azimuthalAngleSpinBox->value()); + AngleProperty azimuthal_angle = m_currentItem->getRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE).value<AngleProperty>(); + azimuthal_angle.setValue(m_azimuthalAngleSpinBox->value()); + m_currentItem->setRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE, azimuthal_angle.getVariant()); } @@ -132,24 +134,9 @@ void BeamEditorWidget::onAngleUnitsChanged(int) { if(m_block_signals) return; qDebug() << "BeamEditorWidget::onAngleUnitsChanged(int) " << m_angleUnits->currentText(); - ComboProperty units_property = m_currentItem->getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<ComboProperty>(); - units_property.setValue(m_angleUnits->currentText()); + AngleProperty units_property = m_currentItem->getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<AngleProperty>(); + units_property.setUnits(m_angleUnits->currentText()); m_currentItem->setRegisteredProperty(BeamItem::P_ANGLE_UNITS, units_property.getVariant()); - -// double alpha = m_currentItem->getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE).toDouble(); -// double phi = m_currentItem->getRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE).toDouble(); - -// ComboProperty angle_units_property = m_currentItem->getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<ComboProperty>(); -// updateAngleUnits(angle_units_property.getValue()); - -// if(m_angleUnits->currentText() == "Degrees") { -// m_inclinationAngleSpinBox->setValue(Units::rad2deg(alpha)); -// m_azimuthalAngleSpinBox->setValue(Units::rad2deg(phi)); -// } else { -// m_inclinationAngleSpinBox->setValue(Units::deg2rad(alpha)); -// m_azimuthalAngleSpinBox->setValue(Units::deg2rad(phi)); -// } - } @@ -163,23 +150,15 @@ void BeamEditorWidget::updateWidgets() m_wavelengthSpinBox->setValue(m_currentItem->getRegisteredProperty(BeamItem::P_WAVELENGTH).toDouble()); m_angleUnits->clear(); - ComboProperty angle_units_property = m_currentItem->getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<ComboProperty>(); - m_angleUnits->addItems(angle_units_property.getValues()); - m_angleUnits->setCurrentText(angle_units_property.getValue()); - - - AngleProperty inclination_angle = m_currentItem->getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE2).value<AngleProperty>(); - if(inclination_angle.inDegrees()) { - setAngleUnits(m_inclinationAngleSpinBox, "Degrees"); - } else { - setAngleUnits(m_inclinationAngleSpinBox, "Radians"); - } -// m_inclinationAngleSpinBox->setValue(m_currentItem->getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE).toDouble()); - m_inclinationAngleSpinBox->setValue(inclination_angle.getValue()); - + AngleProperty angle_units_property = m_currentItem->getRegisteredProperty(BeamItem::P_ANGLE_UNITS).value<AngleProperty>(); + m_angleUnits->addItems(angle_units_property.getLabels()); + m_angleUnits->setCurrentText(angle_units_property.getUnits()); + AngleProperty inclination_angle = m_currentItem->getRegisteredProperty(BeamItem::P_INCLINATION_ANGLE).value<AngleProperty>(); + setAngleUnits(m_inclinationAngleSpinBox, inclination_angle); - m_azimuthalAngleSpinBox->setValue(m_currentItem->getRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE).toDouble()); + AngleProperty azimuthal_angle = m_currentItem->getRegisteredProperty(BeamItem::P_AZIMUTHAL_ANGLE).value<AngleProperty>(); + setAngleUnits(m_azimuthalAngleSpinBox, azimuthal_angle); setBlockSignals(false); } @@ -191,31 +170,21 @@ void BeamEditorWidget::setBlockSignals(bool flag) } -//! Update angle limits for all angle editors depending on units: radians or degrees -void BeamEditorWidget::updateAngleUnits(const QString &units) +void BeamEditorWidget::setAngleUnits(QDoubleSpinBox *editor, const AngleProperty &units, double min_deg, double max_deg) { - setAngleUnits(m_inclinationAngleSpinBox, units); - setAngleUnits(m_azimuthalAngleSpinBox, units); -} - - -//! Sets angle units and corresponding limits for QDoubleSpinBox -void BeamEditorWidget::setAngleUnits(QDoubleSpinBox *editor, const QString &units) -{ - if(units == QStringLiteral("Degrees")) { + if(units.inDegrees()) { editor->setSingleStep(0.01); - editor->setMinimum(-90.0); - editor->setMaximum(90.0); + editor->setMinimum(min_deg); + editor->setMaximum(max_deg); editor->setDecimals(3); + editor->setValue(units.getValue()); } - else if(units == QStringLiteral("Radians")) { + else { editor->setSingleStep(0.0001); - editor->setMinimum(Units::deg2rad(-90.0)); - editor->setMaximum(Units::deg2rad(90.0)); + editor->setMinimum(Units::deg2rad(min_deg)); + editor->setMaximum(Units::deg2rad(max_deg)); editor->setDecimals(6); - } - else { - throw GUIHelpers::Error("BeamEditorWidget::setAngleUnits() ->"); + editor->setValue(units.getValue()); } } diff --git a/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.h b/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.h index f62fce7a4e24b35994315de5467890dc1732ecd9..5a00a48ba857f3d2c622ec407dae50c990095e40 100644 --- a/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.h +++ b/GUI/coregui/Views/Components/InstrumentWidgets/BeamEditorWidget.h @@ -9,6 +9,7 @@ class QLineEdit; class QLabel; class BeamItem; class QDoubleValidator; +class AngleProperty; class BeamEditorWidget : public QWidget { @@ -31,8 +32,7 @@ public slots: private: void updateWidgets(); void setBlockSignals(bool flag); - void updateAngleUnits(const QString &units); - void setAngleUnits(QDoubleSpinBox *, const QString &units); + void setAngleUnits(QDoubleSpinBox *, const AngleProperty &units, double min_deg = -90.0, double max_deg = 90.0); QLineEdit *m_intensityText; QDoubleValidator *m_intensityValidator; diff --git a/GUI/coregui/utils/GUIHelpers.cpp b/GUI/coregui/utils/GUIHelpers.cpp index cbfede46ee3ec1709d372598c740eea7601b3056..052f439ed4c9ec16c6a6a7b1a43e45af75dbdda9 100644 --- a/GUI/coregui/utils/GUIHelpers.cpp +++ b/GUI/coregui/utils/GUIHelpers.cpp @@ -106,4 +106,6 @@ int getVariantType(const QVariant &variant) } + + } // namespace GUIHelpers diff --git a/GUI/coregui/utils/GUIHelpers.h b/GUI/coregui/utils/GUIHelpers.h index bcc5d3b8e60537656ae20ccb14e4d7fa695b2bad..b35fbf0e89d3178f25dcee3ccb698b3fc7168e3d 100644 --- a/GUI/coregui/utils/GUIHelpers.h +++ b/GUI/coregui/utils/GUIHelpers.h @@ -18,7 +18,6 @@ #include <QWidget> #include <QString> - #include <exception> class QVariant;