From 79f102ba12be4bee0042c93de1ff2cd6c2a55cc7 Mon Sep 17 00:00:00 2001 From: Matthias Puchner <github@mpuchner.de> Date: Fri, 22 Oct 2021 12:04:42 +0200 Subject: [PATCH] add abstract base class for SelectionDescriptors to ease referencing --- GUI/Models/SelectionDescriptor.h | 34 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/GUI/Models/SelectionDescriptor.h b/GUI/Models/SelectionDescriptor.h index abd9a9b76a0..43f7a283ce9 100644 --- a/GUI/Models/SelectionDescriptor.h +++ b/GUI/Models/SelectionDescriptor.h @@ -23,6 +23,18 @@ using std::function; +//! Abstract base class for SelectionDescriptor to ease referencing. +class AbstractSelectionDescriptor { +public: + virtual ~AbstractSelectionDescriptor() = default; + + //! Set currently selected option + virtual void setCurrentIndex(int newIndex) const = 0; + + //! Get currently selected option + virtual int currentIndex() const = 0; +}; + //! Describes a selection (various possibilities and the current one). //! //! Usually a selection is presented as a combo box. @@ -38,7 +50,7 @@ using std::function; //! By using this class, the underlying data scheme is hidden from the user of the data. This e.g. //! eases SessionItem migration. The underlying implementation can be a GroupItem, a simple pointer //! member, a std::variant or any other construction to define a selection. -template <typename T> class SelectionDescriptor { +template <typename T> class SelectionDescriptor : public AbstractSelectionDescriptor { public: SelectionDescriptor() = default; @@ -51,7 +63,7 @@ public: { label = item->displayName(); options = item->value().value<ComboProperty>().getValues(); - setCurrentIndex = [=](int index) { + currentIndexSetter = [=](int index) { ComboProperty comboProperty = item->value().value<ComboProperty>(); if (comboProperty.currentIndex() != index) { @@ -59,18 +71,22 @@ public: item->setValue(QVariant::fromValue<ComboProperty>(comboProperty)); } }; - currentIndex = [=] { return item->value().value<ComboProperty>().currentIndex(); }; + currentIndexGetter = [=] { return item->value().value<ComboProperty>().currentIndex(); }; if constexpr (std::is_pointer<T>::value) currentItem = [=] { return dynamic_cast<T>(item->currentItem()); }; } - QString label; //!< A label text (short, no trailing colon) - QString tooltip; //!< Tooltip text - QStringList options; //!< List of options, usually presented as combo entries - function<void(int)> setCurrentIndex; //!< Function to set currently selected option - function<int()> currentIndex; //!< Function to get currently selected option - function<T()> currentItem; //!< Function to get currently selected item + virtual void setCurrentIndex(int newIndex) const override { currentIndexSetter(newIndex); } + + virtual int currentIndex() const override { return currentIndexGetter(); } + + QString label; //!< A label text (short, no trailing colon) + QString tooltip; //!< Tooltip text + QStringList options; //!< List of options, usually presented as combo entries + function<void(int)> currentIndexSetter; //!< Function to set currently selected option + function<int()> currentIndexGetter; //!< Function to get currently selected option + function<T()> currentItem; //!< Function to get currently selected item }; #endif // BORNAGAIN_GUI_MODELS_SELECTIONDESCRIPTOR_H -- GitLab