diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index 193a495..72c4d1b 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -6,6 +6,8 @@ #include #include +class DataSourceItemAction; + /** * Possible types of an item */ @@ -21,6 +23,16 @@ class DataSourceItem { public: explicit DataSourceItem(DataSourceItemType type, QVector data = {}); + /// @return the actions of the item as a vector + QVector actions() const noexcept; + + /** + * Adds an action to the item. The item takes ownership of the action, and the action is + * automatically associated to the item + * @param action the action to add + */ + void addAction(std::unique_ptr action) noexcept; + /** * Adds a child to the item. The item takes ownership of the child. * @param child the child to add diff --git a/core/include/DataSource/DataSourceItemAction.h b/core/include/DataSource/DataSourceItemAction.h new file mode 100644 index 0000000..1e64959 --- /dev/null +++ b/core/include/DataSource/DataSourceItemAction.h @@ -0,0 +1,48 @@ +#ifndef SCIQLOP_DATASOURCEITEMACTION_H +#define SCIQLOP_DATASOURCEITEMACTION_H + +#include + +#include +#include + +Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceItemAction) + +class DataSourceItem; + +/** + * @brief The DataSourceItemAction class represents an action on a data source item. + * + * An action is a function that will be executed when the slot execute() is called. + */ +class DataSourceItemAction : public QObject { + + Q_OBJECT + +public: + /// Signature of the function associated to the action + using ExecuteFunction = std::function; + + /** + * Ctor + * @param name the name of the action + * @param fun the function that will be called when the action is executed + * @sa execute() + */ + explicit DataSourceItemAction(const QString &name, ExecuteFunction fun); + + QString name() const noexcept; + + /// Sets the data source item concerned by the action + void setDataSourceItem(DataSourceItem *dataSourceItem) noexcept; + +public slots: + /// Executes the action + void execute(); + +private: + class DataSourceItemActionPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_DATASOURCEITEMACTION_H diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 43c884b..b01421a 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -1,10 +1,11 @@ #include +#include #include struct DataSourceItem::DataSourceItemPrivate { explicit DataSourceItemPrivate(DataSourceItemType type, QVector data) - : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)} + : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} { } @@ -12,6 +13,7 @@ struct DataSourceItem::DataSourceItemPrivate { std::vector > m_Children; DataSourceItemType m_Type; QVector m_Data; + std::vector > m_Actions; }; DataSourceItem::DataSourceItem(DataSourceItemType type, QVector data) @@ -19,6 +21,22 @@ DataSourceItem::DataSourceItem(DataSourceItemType type, QVector data) { } +QVector DataSourceItem::actions() const noexcept +{ + QVector result{}; + + std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), + std::back_inserter(result), [](const auto &action) { return action.get(); }); + + return result; +} + +void DataSourceItem::addAction(std::unique_ptr action) noexcept +{ + action->setDataSourceItem(this); + impl->m_Actions.push_back(std::move(action)); +} + void DataSourceItem::appendChild(std::unique_ptr child) noexcept { child->impl->m_Parent = this; diff --git a/core/src/DataSource/DataSourceItemAction.cpp b/core/src/DataSource/DataSourceItemAction.cpp new file mode 100644 index 0000000..e791e8f --- /dev/null +++ b/core/src/DataSource/DataSourceItemAction.cpp @@ -0,0 +1,42 @@ +#include + +Q_LOGGING_CATEGORY(LOG_DataSourceItemAction, "DataSourceItemAction") + +struct DataSourceItemAction::DataSourceItemActionPrivate { + explicit DataSourceItemActionPrivate(const QString &name, + DataSourceItemAction::ExecuteFunction fun) + : m_Name{name}, m_Fun{std::move(fun)}, m_DataSourceItem{nullptr} + { + } + + QString m_Name; + DataSourceItemAction::ExecuteFunction m_Fun; + /// Item associated to the action (can be null, in which case the action will not be executed) + DataSourceItem *m_DataSourceItem; +}; + +DataSourceItemAction::DataSourceItemAction(const QString &name, ExecuteFunction fun) + : impl{spimpl::make_unique_impl(name, std::move(fun))} +{ +} + +QString DataSourceItemAction::name() const noexcept +{ + return impl->m_Name; +} + +void DataSourceItemAction::setDataSourceItem(DataSourceItem *dataSourceItem) noexcept +{ + impl->m_DataSourceItem = dataSourceItem; +} + +void DataSourceItemAction::execute() +{ + if (impl->m_DataSourceItem) { + impl->m_Fun(*impl->m_DataSourceItem); + } + else { + qCDebug(LOG_DataSourceItemAction()) + << tr("Can't execute action : no item has been associated"); + } +}