Skip to content
Snippets Groups Projects
Commit dee3e2dd authored by Mikhail Svechnikov's avatar Mikhail Svechnikov
Browse files

SelectionProperty: inherits from AbstractSelectionProperty

parent c1312808
No related branches found
No related tags found
1 merge request!1176GUI: delete SelectionDescriptors class
...@@ -18,6 +18,18 @@ ...@@ -18,6 +18,18 @@
#include "GUI/Model/Descriptor/SelectionDescriptor.h" #include "GUI/Model/Descriptor/SelectionDescriptor.h"
#include "GUI/Support/XML/Streamer.h" #include "GUI/Support/XML/Streamer.h"
//! Abstract base class for SelectionProperty to ease referencing.
class AbstractSelectionProperty {
public:
virtual ~AbstractSelectionProperty() = default;
//! Set currently selected option
virtual void setCurrentIndex(int newIndex) = 0;
//! Get currently selected option
virtual int currentIndex() const = 0;
};
//! Class for representing a selection, its attributes and its accessors. //! Class for representing a selection, its attributes and its accessors.
//! //!
//! A "selection" in this context is a class instance out of a range of possible class instances. //! A "selection" in this context is a class instance out of a range of possible class instances.
...@@ -49,7 +61,7 @@ ...@@ -49,7 +61,7 @@
//! \sa SelectionDescriptor //! \sa SelectionDescriptor
//! //!
template <typename Catalog> template <typename Catalog>
class SelectionProperty { class SelectionProperty : public AbstractSelectionProperty{
public: public:
using CatalogedType = typename Catalog::CatalogedType; using CatalogedType = typename Catalog::CatalogedType;
...@@ -61,7 +73,9 @@ public: ...@@ -61,7 +73,9 @@ public:
template <typename... ArgsForCreation> template <typename... ArgsForCreation>
void init(const QString& label, const QString& tooltip, ArgsForCreation... argsForCreation) void init(const QString& label, const QString& tooltip, ArgsForCreation... argsForCreation)
{ {
initFields(label, tooltip); initFieldsAndSetter(label, tooltip, argsForCreation...);
setCurrentIndex(0);
initDescriptor(label, tooltip, argsForCreation...); initDescriptor(label, tooltip, argsForCreation...);
m_descriptor.setCurrentIndex(0); m_descriptor.setCurrentIndex(0);
} }
...@@ -83,7 +97,9 @@ public: ...@@ -83,7 +97,9 @@ public:
{ {
m_initializer = initializer; m_initializer = initializer;
initFields(label, tooltip); initFieldsAndSetter(label, tooltip);
setCurrentIndex(0);
initDescriptor(label, tooltip); initDescriptor(label, tooltip);
m_descriptor.setCurrentIndex(0); m_descriptor.setCurrentIndex(0);
} }
...@@ -108,7 +124,9 @@ public: ...@@ -108,7 +124,9 @@ public:
m_initializer = initializer; m_initializer = initializer;
m_types = types; m_types = types;
initFields(label, tooltip); initFieldsAndSetter(label, tooltip);
setCurrentIndex(0);
initDescriptor(label, tooltip); initDescriptor(label, tooltip);
m_descriptor.setCurrentIndex(0); m_descriptor.setCurrentIndex(0);
} }
...@@ -154,14 +172,22 @@ public: ...@@ -154,14 +172,22 @@ public:
} }
private: private:
void initFields(const QString& label, const QString& tooltip) { template <typename... ArgsForCreation>
void initFieldsAndSetter(const QString& label, const QString& tooltip, ArgsForCreation... argsForCreation) {
m_label = label; m_label = label;
m_tooltip = tooltip; m_tooltip = tooltip;
m_options.clear(); m_options.clear();
for (const auto type : m_types) for (const auto type : m_types)
m_options << Catalog::uiInfo(type).menuEntry; m_options << Catalog::uiInfo(type).menuEntry;
currentIndexSetter = [=](int current) {
auto* p = Catalog::create(m_types[current], argsForCreation...);
if (m_initializer)
m_initializer(p, m_p.get());
m_p.reset(p);
};
} }
template <typename... ArgsForCreation> template <typename... ArgsForCreation>
void initDescriptor(const QString& label, const QString& tooltip, void initDescriptor(const QString& label, const QString& tooltip,
ArgsForCreation... argsForCreation) ArgsForCreation... argsForCreation)
...@@ -190,6 +216,9 @@ private: ...@@ -190,6 +216,9 @@ private:
QStringList options() {return m_options;} QStringList options() {return m_options;}
CatalogedType* currentItem() {return m_p.get();} CatalogedType* currentItem() {return m_p.get();}
void setCurrentIndex(int newIndex) override { currentIndexSetter(newIndex); }
int currentIndex() const override { return m_types.indexOf(Catalog::type(m_p.get())); }
private: private:
SelectionDescriptor<CatalogedType*> SelectionDescriptor<CatalogedType*>
m_descriptor; //!< descriptor, holding attributes like label, tooltip m_descriptor; //!< descriptor, holding attributes like label, tooltip
...@@ -201,6 +230,9 @@ private: ...@@ -201,6 +230,9 @@ private:
QStringList m_options; //!< List of options, usually presented as combo entries QStringList m_options; //!< List of options, usually presented as combo entries
QVector<typename Catalog::Type> m_types = Catalog::types(); QVector<typename Catalog::Type> m_types = Catalog::types();
//! Reimplementable function to set currently selected option.
std::function<void(int)> currentIndexSetter = nullptr;
//! initializer function. Can be empty. //! initializer function. Can be empty.
//! The first argument is the new item, the second is the old one if present; can be null. //! The first argument is the new item, the second is the old one if present; can be null.
//! This is intended to maybe copy values from the old to the new selection. oldItem also can be //! This is intended to maybe copy values from the old to the new selection. oldItem also can be
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment