@@ -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 | } |
@@ -1,59 +1,71 | |||||
1 | #ifndef SCIQLOP_DATASOURCEITEM_H |
|
1 | #ifndef SCIQLOP_DATASOURCEITEM_H | |
2 | #define SCIQLOP_DATASOURCEITEM_H |
|
2 | #define SCIQLOP_DATASOURCEITEM_H | |
3 |
|
3 | |||
4 | #include <Common/spimpl.h> |
|
4 | #include <Common/spimpl.h> | |
5 |
|
5 | |||
6 | #include <QVariant> |
|
6 | #include <QVariant> | |
7 | #include <QVector> |
|
7 | #include <QVector> | |
8 |
|
8 | |||
|
9 | class DataSourceItemAction; | |||
|
10 | ||||
9 | /** |
|
11 | /** | |
10 | * Possible types of an item |
|
12 | * Possible types of an item | |
11 | */ |
|
13 | */ | |
12 | enum class DataSourceItemType { NODE, PRODUCT }; |
|
14 | enum class DataSourceItemType { NODE, PRODUCT }; | |
13 |
|
15 | |||
14 | /** |
|
16 | /** | |
15 | * @brief The DataSourceItem class aims to represent a structure element of a data source. |
|
17 | * @brief The DataSourceItem class aims to represent a structure element of a data source. | |
16 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) |
|
18 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) | |
17 | * containing other DataSourceItem objects (children). |
|
19 | * containing other DataSourceItem objects (children). | |
18 | * For each DataSourceItem can be associated a set of data representing it. |
|
20 | * For each DataSourceItem can be associated a set of data representing it. | |
19 | */ |
|
21 | */ | |
20 | class DataSourceItem { |
|
22 | class DataSourceItem { | |
21 | public: |
|
23 | public: | |
22 | explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {}); |
|
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 | * Adds a child to the item. The item takes ownership of the child. |
|
37 | * Adds a child to the item. The item takes ownership of the child. | |
26 | * @param child the child to add |
|
38 | * @param child the child to add | |
27 | */ |
|
39 | */ | |
28 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
40 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; | |
29 |
|
41 | |||
30 | /** |
|
42 | /** | |
31 | * Returns the item's child associated to an index |
|
43 | * Returns the item's child associated to an index | |
32 | * @param childIndex the index to search |
|
44 | * @param childIndex the index to search | |
33 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
45 | * @return a pointer to the child if index is valid, nullptr otherwise | |
34 | */ |
|
46 | */ | |
35 | DataSourceItem *child(int childIndex) const noexcept; |
|
47 | DataSourceItem *child(int childIndex) const noexcept; | |
36 |
|
48 | |||
37 | int childCount() const noexcept; |
|
49 | int childCount() const noexcept; | |
38 |
|
50 | |||
39 | /** |
|
51 | /** | |
40 | * Get the data associated to an index |
|
52 | * Get the data associated to an index | |
41 | * @param dataIndex the index to search |
|
53 | * @param dataIndex the index to search | |
42 | * @return the data found if index is valid, default QVariant otherwise |
|
54 | * @return the data found if index is valid, default QVariant otherwise | |
43 | */ |
|
55 | */ | |
44 | QVariant data(int dataIndex) const noexcept; |
|
56 | QVariant data(int dataIndex) const noexcept; | |
45 |
|
57 | |||
46 | /** |
|
58 | /** | |
47 | * Get the item's parent |
|
59 | * Get the item's parent | |
48 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
60 | * @return a pointer to the parent if it exists, nullptr if the item is a root | |
49 | */ |
|
61 | */ | |
50 | DataSourceItem *parentItem() const noexcept; |
|
62 | DataSourceItem *parentItem() const noexcept; | |
51 |
|
63 | |||
52 | DataSourceItemType type() const noexcept; |
|
64 | DataSourceItemType type() const noexcept; | |
53 |
|
65 | |||
54 | private: |
|
66 | private: | |
55 | class DataSourceItemPrivate; |
|
67 | class DataSourceItemPrivate; | |
56 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
68 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; | |
57 | }; |
|
69 | }; | |
58 |
|
70 | |||
59 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
|
71 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,56 +1,74 | |||||
1 | #include <DataSource/DataSourceItem.h> |
|
1 | #include <DataSource/DataSourceItem.h> | |
|
2 | #include <DataSource/DataSourceItemAction.h> | |||
2 |
|
3 | |||
3 | #include <QVector> |
|
4 | #include <QVector> | |
4 |
|
5 | |||
5 | struct DataSourceItem::DataSourceItemPrivate { |
|
6 | struct DataSourceItem::DataSourceItemPrivate { | |
6 | explicit DataSourceItemPrivate(DataSourceItemType type, QVector<QVariant> data) |
|
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 | |||
11 | DataSourceItem *m_Parent; |
|
12 | DataSourceItem *m_Parent; | |
12 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
13 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; | |
13 | DataSourceItemType m_Type; |
|
14 | DataSourceItemType m_Type; | |
14 | QVector<QVariant> m_Data; |
|
15 | QVector<QVariant> m_Data; | |
|
16 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; | |||
15 | }; |
|
17 | }; | |
16 |
|
18 | |||
17 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data) |
|
19 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data) | |
18 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} |
|
20 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(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 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
40 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept | |
23 | { |
|
41 | { | |
24 | child->impl->m_Parent = this; |
|
42 | child->impl->m_Parent = this; | |
25 | impl->m_Children.push_back(std::move(child)); |
|
43 | impl->m_Children.push_back(std::move(child)); | |
26 | } |
|
44 | } | |
27 |
|
45 | |||
28 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
46 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept | |
29 | { |
|
47 | { | |
30 | if (childIndex < 0 || childIndex >= childCount()) { |
|
48 | if (childIndex < 0 || childIndex >= childCount()) { | |
31 | return nullptr; |
|
49 | return nullptr; | |
32 | } |
|
50 | } | |
33 | else { |
|
51 | else { | |
34 | return impl->m_Children.at(childIndex).get(); |
|
52 | return impl->m_Children.at(childIndex).get(); | |
35 | } |
|
53 | } | |
36 | } |
|
54 | } | |
37 |
|
55 | |||
38 | int DataSourceItem::childCount() const noexcept |
|
56 | int DataSourceItem::childCount() const noexcept | |
39 | { |
|
57 | { | |
40 | return impl->m_Children.size(); |
|
58 | return impl->m_Children.size(); | |
41 | } |
|
59 | } | |
42 |
|
60 | |||
43 | QVariant DataSourceItem::data(int dataIndex) const noexcept |
|
61 | QVariant DataSourceItem::data(int dataIndex) const noexcept | |
44 | { |
|
62 | { | |
45 | return impl->m_Data.value(dataIndex); |
|
63 | return impl->m_Data.value(dataIndex); | |
46 | } |
|
64 | } | |
47 |
|
65 | |||
48 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
66 | DataSourceItem *DataSourceItem::parentItem() const noexcept | |
49 | { |
|
67 | { | |
50 | return impl->m_Parent; |
|
68 | return impl->m_Parent; | |
51 | } |
|
69 | } | |
52 |
|
70 | |||
53 | DataSourceItemType DataSourceItem::type() const noexcept |
|
71 | DataSourceItemType DataSourceItem::type() const noexcept | |
54 | { |
|
72 | { | |
55 | return impl->m_Type; |
|
73 | return impl->m_Type; | |
56 | } |
|
74 | } |
General Comments 0
You need to be logged in to leave comments.
Login now