@@ -0,0 +1,48 | |||
|
1 | #ifndef SCIQLOP_DATASOURCEITEMACTION_H | |
|
2 | #define SCIQLOP_DATASOURCEITEMACTION_H | |
|
3 | ||
|
4 | #include <Common/spimpl.h> | |
|
5 | ||
|
6 | #include <QLoggingCategory> | |
|
7 | #include <QObject> | |
|
8 | ||
|
9 | Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceItemAction) | |
|
10 | ||
|
11 | class DataSourceItem; | |
|
12 | ||
|
13 | /** | |
|
14 | * @brief The DataSourceItemAction class represents an action on a data source item. | |
|
15 | * | |
|
16 | * An action is a function that will be executed when the slot execute() is called. | |
|
17 | */ | |
|
18 | class DataSourceItemAction : public QObject { | |
|
19 | ||
|
20 | Q_OBJECT | |
|
21 | ||
|
22 | public: | |
|
23 | /// Signature of the function associated to the action | |
|
24 | using ExecuteFunction = std::function<void(DataSourceItem &dataSourceItem)>; | |
|
25 | ||
|
26 | /** | |
|
27 | * Ctor | |
|
28 | * @param name the name of the action | |
|
29 | * @param fun the function that will be called when the action is executed | |
|
30 | * @sa execute() | |
|
31 | */ | |
|
32 | explicit DataSourceItemAction(const QString &name, ExecuteFunction fun); | |
|
33 | ||
|
34 | QString name() const noexcept; | |
|
35 | ||
|
36 | /// Sets the data source item concerned by the action | |
|
37 | void setDataSourceItem(DataSourceItem *dataSourceItem) noexcept; | |
|
38 | ||
|
39 | public slots: | |
|
40 | /// Executes the action | |
|
41 | void execute(); | |
|
42 | ||
|
43 | private: | |
|
44 | class DataSourceItemActionPrivate; | |
|
45 | spimpl::unique_impl_ptr<DataSourceItemActionPrivate> impl; | |
|
46 | }; | |
|
47 | ||
|
48 | #endif // SCIQLOP_DATASOURCEITEMACTION_H |
@@ -0,0 +1,42 | |||
|
1 | #include <DataSource/DataSourceItemAction.h> | |
|
2 | ||
|
3 | Q_LOGGING_CATEGORY(LOG_DataSourceItemAction, "DataSourceItemAction") | |
|
4 | ||
|
5 | struct DataSourceItemAction::DataSourceItemActionPrivate { | |
|
6 | explicit DataSourceItemActionPrivate(const QString &name, | |
|
7 | DataSourceItemAction::ExecuteFunction fun) | |
|
8 | : m_Name{name}, m_Fun{std::move(fun)}, m_DataSourceItem{nullptr} | |
|
9 | { | |
|
10 | } | |
|
11 | ||
|
12 | QString m_Name; | |
|
13 | DataSourceItemAction::ExecuteFunction m_Fun; | |
|
14 | /// Item associated to the action (can be null, in which case the action will not be executed) | |
|
15 | DataSourceItem *m_DataSourceItem; | |
|
16 | }; | |
|
17 | ||
|
18 | DataSourceItemAction::DataSourceItemAction(const QString &name, ExecuteFunction fun) | |
|
19 | : impl{spimpl::make_unique_impl<DataSourceItemActionPrivate>(name, std::move(fun))} | |
|
20 | { | |
|
21 | } | |
|
22 | ||
|
23 | QString DataSourceItemAction::name() const noexcept | |
|
24 | { | |
|
25 | return impl->m_Name; | |
|
26 | } | |
|
27 | ||
|
28 | void DataSourceItemAction::setDataSourceItem(DataSourceItem *dataSourceItem) noexcept | |
|
29 | { | |
|
30 | impl->m_DataSourceItem = dataSourceItem; | |
|
31 | } | |
|
32 | ||
|
33 | void DataSourceItemAction::execute() | |
|
34 | { | |
|
35 | if (impl->m_DataSourceItem) { | |
|
36 | impl->m_Fun(*impl->m_DataSourceItem); | |
|
37 | } | |
|
38 | else { | |
|
39 | qCDebug(LOG_DataSourceItemAction()) | |
|
40 | << tr("Can't execute action : no item has been associated"); | |
|
41 | } | |
|
42 | } |
@@ -6,6 +6,8 | |||
|
6 | 6 | #include <QVariant> |
|
7 | 7 | #include <QVector> |
|
8 | 8 | |
|
9 | class DataSourceItemAction; | |
|
10 | ||
|
9 | 11 | /** |
|
10 | 12 | * Possible types of an item |
|
11 | 13 | */ |
@@ -21,6 +23,16 class DataSourceItem { | |||
|
21 | 23 | public: |
|
22 | 24 | explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {}); |
|
23 | 25 | |
|
26 | /// @return the actions of the item as a vector | |
|
27 | QVector<DataSourceItemAction *> actions() const noexcept; | |
|
28 | ||
|
29 | /** | |
|
30 | * Adds an action to the item. The item takes ownership of the action, and the action is | |
|
31 | * automatically associated to the item | |
|
32 | * @param action the action to add | |
|
33 | */ | |
|
34 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; | |
|
35 | ||
|
24 | 36 | /** |
|
25 | 37 | * Adds a child to the item. The item takes ownership of the child. |
|
26 | 38 | * @param child the child to add |
@@ -1,10 +1,11 | |||
|
1 | 1 | #include <DataSource/DataSourceItem.h> |
|
2 | #include <DataSource/DataSourceItemAction.h> | |
|
2 | 3 | |
|
3 | 4 | #include <QVector> |
|
4 | 5 | |
|
5 | 6 | struct DataSourceItem::DataSourceItemPrivate { |
|
6 | 7 | explicit DataSourceItemPrivate(DataSourceItemType type, QVector<QVariant> data) |
|
7 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)} | |
|
8 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} | |
|
8 | 9 | { |
|
9 | 10 | } |
|
10 | 11 | |
@@ -12,6 +13,7 struct DataSourceItem::DataSourceItemPrivate { | |||
|
12 | 13 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
13 | 14 | DataSourceItemType m_Type; |
|
14 | 15 | QVector<QVariant> m_Data; |
|
16 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; | |
|
15 | 17 | }; |
|
16 | 18 | |
|
17 | 19 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data) |
@@ -19,6 +21,22 DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data) | |||
|
19 | 21 | { |
|
20 | 22 | } |
|
21 | 23 | |
|
24 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept | |
|
25 | { | |
|
26 | QVector<DataSourceItemAction *> result{}; | |
|
27 | ||
|
28 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), | |
|
29 | std::back_inserter(result), [](const auto &action) { return action.get(); }); | |
|
30 | ||
|
31 | return result; | |
|
32 | } | |
|
33 | ||
|
34 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept | |
|
35 | { | |
|
36 | action->setDataSourceItem(this); | |
|
37 | impl->m_Actions.push_back(std::move(action)); | |
|
38 | } | |
|
39 | ||
|
22 | 40 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
23 | 41 | { |
|
24 | 42 | child->impl->m_Parent = this; |
General Comments 0
You need to be logged in to leave comments.
Login now