From 1421a553dda1c886d404af1d577309e4c90cdd05 Mon Sep 17 00:00:00 2001 From: Tobias Knopff <t.knopff@fz-juelich.de> Date: Mon, 31 May 2021 13:02:58 +0200 Subject: [PATCH] Make Instrument2DItem::setDetectorGroup a template --- GUI/Models/InstrumentItems.cpp | 5 ----- GUI/Models/InstrumentItems.h | 11 ++++++++++- GUI/Models/TransformFromDomain.cpp | 6 ++---- Tests/UnitTests/GUI/TestLinkInstrument.cpp | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/GUI/Models/InstrumentItems.cpp b/GUI/Models/InstrumentItems.cpp index 1e0d318db7f..77d137ab503 100644 --- a/GUI/Models/InstrumentItems.cpp +++ b/GUI/Models/InstrumentItems.cpp @@ -251,11 +251,6 @@ GroupItem* Instrument2DItem::detectorGroup() return item<GroupItem>(P_DETECTOR); } -void Instrument2DItem::setDetectorGroup(const QString& modelType) -{ - setGroupProperty(P_DETECTOR, modelType); -} - void Instrument2DItem::clearMasks() { detectorItem()->clearMasks(); diff --git a/GUI/Models/InstrumentItems.h b/GUI/Models/InstrumentItems.h index 5df38ee20d0..58393bb537c 100644 --- a/GUI/Models/InstrumentItems.h +++ b/GUI/Models/InstrumentItems.h @@ -101,7 +101,7 @@ public: DetectorItem* detectorItem() const; GroupItem* detectorGroup(); - void setDetectorGroup(const QString& modelType); + template <typename T> T* setDetectorGroup(); void clearMasks() override; void importMasks(const MaskContainerItem* maskContainer) override; @@ -177,4 +177,13 @@ T* InstrumentItem::setBackgroundType() return setGroupPropertyType<T>(P_BACKGROUND); } +template <typename T> +T* Instrument2DItem::setDetectorGroup() +{ + static_assert(std::is_base_of<DetectorItem,T>::value, + "Class must be derived from DetectorItem"); + + return static_cast<T*>(setGroupProperty(P_DETECTOR, T::M_TYPE)); +} + #endif // BORNAGAIN_GUI_MODELS_INSTRUMENTITEMS_H diff --git a/GUI/Models/TransformFromDomain.cpp b/GUI/Models/TransformFromDomain.cpp index bc37e33b5e8..e8373ed47ae 100644 --- a/GUI/Models/TransformFromDomain.cpp +++ b/GUI/Models/TransformFromDomain.cpp @@ -298,12 +298,10 @@ void TransformFromDomain::setDetectorGeometry(Instrument2DItem* instrument_item, const IDetector& detector) { if (auto det = dynamic_cast<const SphericalDetector*>(&detector)) { - instrument_item->setDetectorGroup(SphericalDetectorItem::M_TYPE); - auto item = dynamic_cast<SphericalDetectorItem*>(instrument_item->detectorItem()); + auto item = instrument_item->setDetectorGroup<SphericalDetectorItem>(); setSphericalDetector(item, *det); } else if (auto det = dynamic_cast<const RectangularDetector*>(&detector)) { - instrument_item->setDetectorGroup(RectangularDetectorItem::M_TYPE); - auto item = dynamic_cast<RectangularDetectorItem*>(instrument_item->detectorItem()); + auto item = instrument_item->setDetectorGroup<RectangularDetectorItem>(); setRectangularDetector(item, *det); } else { throw Error( diff --git a/Tests/UnitTests/GUI/TestLinkInstrument.cpp b/Tests/UnitTests/GUI/TestLinkInstrument.cpp index 067a8666bb6..a2bbe7b3d4e 100644 --- a/Tests/UnitTests/GUI/TestLinkInstrument.cpp +++ b/Tests/UnitTests/GUI/TestLinkInstrument.cpp @@ -37,7 +37,7 @@ TEST_F(TestLinkInstrument, test_canLinkToInstrument) EXPECT_EQ(manager.linkedRealDataItems(instrument), QList<RealDataItem*>() << realData); // changing detector type and checking that link remain - instrument->setDetectorGroup(RectangularDetectorItem::M_TYPE); + instrument->setDetectorGroup<RectangularDetectorItem>(); EXPECT_EQ(manager.linkedRealDataItems(instrument), QList<RealDataItem*>() << realData); // changing detector binning and checking that link is destroyed -- GitLab