From 60c23f038c2ef3142cbfe22b31e2177d9749b441 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de> Date: Wed, 24 Jan 2024 09:48:42 +0100 Subject: [PATCH] Cloned GUI/Model/Descriptor/SelectionProperty -> GUI/Model/Descriptor/SelectionVector --- GUI/Model/Descriptor/SelectionProperty.h | 80 ------------------ GUI/Model/Descriptor/SelectionVector.h | 100 +++++++++++++++++++++++ GUI/Model/Device/InstrumentsSet.h | 2 +- GUI/Model/Mask/MasksSet.h | 2 +- GUI/Model/Sample/CompoundItem.h | 1 + GUI/Model/Sample/ParticleLayoutItem.h | 2 +- 6 files changed, 104 insertions(+), 83 deletions(-) create mode 100644 GUI/Model/Descriptor/SelectionVector.h diff --git a/GUI/Model/Descriptor/SelectionProperty.h b/GUI/Model/Descriptor/SelectionProperty.h index 05ebd9e8b54..cdf769a73b2 100644 --- a/GUI/Model/Descriptor/SelectionProperty.h +++ b/GUI/Model/Descriptor/SelectionProperty.h @@ -215,84 +215,4 @@ private: std::function<void(CatalogedType* newItem, const CatalogedType* oldItem)> m_initializer; }; -//! A special kind of owning vector, providing "standard" interfaces for -//! 'std::vector<SelectionProperty<Catalog>>', used in classes that work not just with single -//! SelectionProperty but with the set of them, for example 'InstrumentsSet', 'CompoundItem', -//! 'ParticleLayoutItems', 'MaskItems' - -template <typename Catalog> class SelectionVector { -public: - using CatalogedType = typename Catalog::CatalogedType; - - void delete_element(const CatalogedType* element) - { - for (size_t i = 0; i < m_selections.size(); i++) - if (m_selections[i].currentItem() == element) - m_selections.erase(m_selections.begin() + i); - } - - void delete_at(size_t i) { m_selections.erase(m_selections.begin() + i); } - - void insert_at(size_t i, CatalogedType* element) - { - SelectionProperty<Catalog> newSelection; - newSelection.setCurrentItem(element); - m_selections.insert(m_selections.begin() + i, std::move(newSelection)); - } - - int index_of(const CatalogedType* element) const - { - for (size_t i = 0; i < m_selections.size(); i++) - if (m_selections[i].currentItem() == element) - return int(i); - return -1; - } - - void move(size_t fromIndex, size_t toIndex) - { - if (fromIndex > toIndex) - std::rotate(m_selections.rend() - fromIndex - 1, m_selections.rend() - fromIndex, - m_selections.rend() - toIndex); - else - std::rotate(m_selections.begin() + fromIndex, m_selections.begin() + fromIndex + 1, - m_selections.begin() + toIndex + 1); - } - - QVector<CatalogedType*> toModifiableQVector() const - { - QVector<CatalogedType*> result; - for (const auto& sel : m_selections) - result.append(sel.currentItem()); - return result; - } - - QVector<const CatalogedType*> toQVector() const - { - QVector<const CatalogedType*> result; - for (const auto& sel : m_selections) - result.append(sel.currentItem()); - return result; - } - - void push_back(CatalogedType* item) { insert_at(m_selections.size(), item); } - void clear() { m_selections.clear(); } - size_t size() const { return m_selections.size(); } - bool empty() const { return m_selections.empty(); } - SelectionProperty<Catalog>& operator[](int i) { return m_selections[i]; } - SelectionProperty<Catalog>& at(int i) { return m_selections.at(i); } - SelectionProperty<Catalog>& front() { return m_selections.front(); } - SelectionProperty<Catalog>& back() { return m_selections.back(); } - - using Iterator = typename std::vector<SelectionProperty<Catalog>>::iterator; - using ConstIterator = typename std::vector<SelectionProperty<Catalog>>::const_iterator; - - ConstIterator begin() const { return m_selections.cbegin(); } - ConstIterator end() const { return m_selections.cend(); } - Iterator begin() { return m_selections.begin(); } - Iterator end() { return m_selections.end(); } - -private: - std::vector<SelectionProperty<Catalog>> m_selections; -}; - #endif // BORNAGAIN_GUI_MODEL_DESCRIPTOR_SELECTIONPROPERTY_H diff --git a/GUI/Model/Descriptor/SelectionVector.h b/GUI/Model/Descriptor/SelectionVector.h new file mode 100644 index 00000000000..88ddf673a95 --- /dev/null +++ b/GUI/Model/Descriptor/SelectionVector.h @@ -0,0 +1,100 @@ +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Model/Descriptor/SelectionVector.h +//! @brief Defines class SelectionVector. +//! +//! @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_DESCRIPTOR_SELECTIONVECTOR_H +#define BORNAGAIN_GUI_MODEL_DESCRIPTOR_SELECTIONVECTOR_H + +#include "GUI/Model/Descriptor/SelectionProperty.h" + +//! A special kind of owning vector, providing "standard" interfaces for +//! 'std::vector<SelectionProperty<Catalog>>', used in classes that work not just with single +//! SelectionProperty but with the set of them, for example 'InstrumentsSet', 'CompoundItem', +//! 'ParticleLayoutItems', 'MaskItems' + +template <typename Catalog> class SelectionVector { +public: + using CatalogedType = typename Catalog::CatalogedType; + + void delete_element(const CatalogedType* element) + { + for (size_t i = 0; i < m_selections.size(); i++) + if (m_selections[i].currentItem() == element) + m_selections.erase(m_selections.begin() + i); + } + + void delete_at(size_t i) { m_selections.erase(m_selections.begin() + i); } + + void insert_at(size_t i, CatalogedType* element) + { + SelectionProperty<Catalog> newSelection; + newSelection.setCurrentItem(element); + m_selections.insert(m_selections.begin() + i, std::move(newSelection)); + } + + int index_of(const CatalogedType* element) const + { + for (size_t i = 0; i < m_selections.size(); i++) + if (m_selections[i].currentItem() == element) + return int(i); + return -1; + } + + void move(size_t fromIndex, size_t toIndex) + { + if (fromIndex > toIndex) + std::rotate(m_selections.rend() - fromIndex - 1, m_selections.rend() - fromIndex, + m_selections.rend() - toIndex); + else + std::rotate(m_selections.begin() + fromIndex, m_selections.begin() + fromIndex + 1, + m_selections.begin() + toIndex + 1); + } + + QVector<CatalogedType*> toModifiableQVector() const + { + QVector<CatalogedType*> result; + for (const auto& sel : m_selections) + result.append(sel.currentItem()); + return result; + } + + QVector<const CatalogedType*> toQVector() const + { + QVector<const CatalogedType*> result; + for (const auto& sel : m_selections) + result.append(sel.currentItem()); + return result; + } + + void push_back(CatalogedType* item) { insert_at(m_selections.size(), item); } + void clear() { m_selections.clear(); } + size_t size() const { return m_selections.size(); } + bool empty() const { return m_selections.empty(); } + SelectionProperty<Catalog>& operator[](int i) { return m_selections[i]; } + SelectionProperty<Catalog>& at(int i) { return m_selections.at(i); } + SelectionProperty<Catalog>& front() { return m_selections.front(); } + SelectionProperty<Catalog>& back() { return m_selections.back(); } + + using Iterator = typename std::vector<SelectionProperty<Catalog>>::iterator; + using ConstIterator = typename std::vector<SelectionProperty<Catalog>>::const_iterator; + + ConstIterator begin() const { return m_selections.cbegin(); } + ConstIterator end() const { return m_selections.cend(); } + Iterator begin() { return m_selections.begin(); } + Iterator end() { return m_selections.end(); } + +private: + std::vector<SelectionProperty<Catalog>> m_selections; +}; + +#endif // BORNAGAIN_GUI_MODEL_DESCRIPTOR_SELECTIONVECTOR_H diff --git a/GUI/Model/Device/InstrumentsSet.h b/GUI/Model/Device/InstrumentsSet.h index 40f67425a2c..e3af0b3f940 100644 --- a/GUI/Model/Device/InstrumentsSet.h +++ b/GUI/Model/Device/InstrumentsSet.h @@ -15,7 +15,7 @@ #ifndef BORNAGAIN_GUI_MODEL_DEVICE_INSTRUMENTSSET_H #define BORNAGAIN_GUI_MODEL_DEVICE_INSTRUMENTSSET_H -#include "GUI/Model/Descriptor/SelectionProperty.h" +#include "GUI/Model/Descriptor/SelectionVector.h" #include "GUI/Model/Device/InstrumentItemCatalog.h" #include <QVector> #include <QXmlStreamWriter> diff --git a/GUI/Model/Mask/MasksSet.h b/GUI/Model/Mask/MasksSet.h index 0b2709194e1..6dbf0fc2c58 100644 --- a/GUI/Model/Mask/MasksSet.h +++ b/GUI/Model/Mask/MasksSet.h @@ -15,7 +15,7 @@ #ifndef BORNAGAIN_GUI_MODEL_MASK_MASKSSET_H #define BORNAGAIN_GUI_MODEL_MASK_MASKSSET_H -#include "GUI/Model/Descriptor/SelectionProperty.h" +#include "GUI/Model/Descriptor/SelectionVector.h" #include "GUI/Model/Mask/MaskItemCatalog.h" #include "GUI/Model/Mask/OverlayItem.h" #include <QModelIndex> diff --git a/GUI/Model/Sample/CompoundItem.h b/GUI/Model/Sample/CompoundItem.h index c82c6476029..87277d3bc61 100644 --- a/GUI/Model/Sample/CompoundItem.h +++ b/GUI/Model/Sample/CompoundItem.h @@ -15,6 +15,7 @@ #ifndef BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H #define BORNAGAIN_GUI_MODEL_SAMPLE_COMPOUNDITEM_H +#include "GUI/Model/Descriptor/SelectionVector.h" #include "GUI/Model/Sample/ItemWithParticles.h" #include "GUI/Model/Sample/ItemWithParticlesCatalog.h" #include "Sample/Particle/Compound.h" diff --git a/GUI/Model/Sample/ParticleLayoutItem.h b/GUI/Model/Sample/ParticleLayoutItem.h index 2c6aa7a23a4..517bccfecb9 100644 --- a/GUI/Model/Sample/ParticleLayoutItem.h +++ b/GUI/Model/Sample/ParticleLayoutItem.h @@ -16,7 +16,7 @@ #define BORNAGAIN_GUI_MODEL_SAMPLE_PARTICLELAYOUTITEM_H #include "GUI/Model/Descriptor/DoubleProperty.h" -#include "GUI/Model/Descriptor/SelectionProperty.h" +#include "GUI/Model/Descriptor/SelectionVector.h" #include "GUI/Model/Sample/InterferenceItemCatalog.h" #include "GUI/Model/Sample/InterferenceItems.h" #include "GUI/Model/Sample/Item3D.h" -- GitLab