@@ -1,152 +1,154 | |||
|
1 | 1 | #ifndef SCIQLOP_DATASOURCEITEM_H |
|
2 | 2 | #define SCIQLOP_DATASOURCEITEM_H |
|
3 | 3 | |
|
4 | 4 | #include "CoreGlobal.h" |
|
5 | 5 | |
|
6 | 6 | #include <Common/spimpl.h> |
|
7 | 7 | |
|
8 | 8 | #include <QVariant> |
|
9 | 9 | #include <QVector> |
|
10 | 10 | |
|
11 | 11 | class DataSourceItemAction; |
|
12 | 12 | |
|
13 | 13 | /** |
|
14 | 14 | * Possible types of an item |
|
15 | 15 | */ |
|
16 | 16 | enum class DataSourceItemType { NODE, PRODUCT, COMPONENT }; |
|
17 | 17 | |
|
18 | 18 | /** |
|
19 | 19 | * @brief The DataSourceItem class aims to represent a structure element of a data source. |
|
20 | 20 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) |
|
21 | 21 | * containing other DataSourceItem objects (children). |
|
22 | 22 | * For each DataSourceItem can be associated a set of data representing it. |
|
23 | 23 | */ |
|
24 | 24 | class SCIQLOP_CORE_EXPORT DataSourceItem { |
|
25 | 25 | public: |
|
26 | 26 | /// Key associated with the name of the item |
|
27 | 27 | static const QString NAME_DATA_KEY; |
|
28 | /// Key associated with the plugin of the item | |
|
29 | static const QString PLUGIN_DATA_KEY; | |
|
28 | 30 | |
|
29 | 31 | explicit DataSourceItem(DataSourceItemType type, const QString &name); |
|
30 | 32 | explicit DataSourceItem(DataSourceItemType type, QVariantHash data = {}); |
|
31 | 33 | |
|
32 | 34 | std::unique_ptr<DataSourceItem> clone() const; |
|
33 | 35 | |
|
34 | 36 | /// @return the actions of the item as a vector |
|
35 | 37 | QVector<DataSourceItemAction *> actions() const noexcept; |
|
36 | 38 | |
|
37 | 39 | /** |
|
38 | 40 | * Adds an action to the item. The item takes ownership of the action, and the action is |
|
39 | 41 | * automatically associated to the item |
|
40 | 42 | * @param action the action to add |
|
41 | 43 | */ |
|
42 | 44 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; |
|
43 | 45 | |
|
44 | 46 | /** |
|
45 | 47 | * Adds a child to the item. The item takes ownership of the child. |
|
46 | 48 | * @param child the child to add |
|
47 | 49 | */ |
|
48 | 50 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
49 | 51 | |
|
50 | 52 | /** |
|
51 | 53 | * Returns the item's child associated to an index |
|
52 | 54 | * @param childIndex the index to search |
|
53 | 55 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
54 | 56 | */ |
|
55 | 57 | DataSourceItem *child(int childIndex) const noexcept; |
|
56 | 58 | |
|
57 | 59 | int childCount() const noexcept; |
|
58 | 60 | |
|
59 | 61 | /** |
|
60 | 62 | * Get the data associated to a key |
|
61 | 63 | * @param key the key to search |
|
62 | 64 | * @return the data found if key is valid, default QVariant otherwise |
|
63 | 65 | */ |
|
64 | 66 | QVariant data(const QString &key) const noexcept; |
|
65 | 67 | |
|
66 | 68 | /// Gets all data |
|
67 | 69 | QVariantHash data() const noexcept; |
|
68 | 70 | |
|
69 | 71 | /** |
|
70 | 72 | * Merge in the item the source item passed as parameter. |
|
71 | 73 | * |
|
72 | 74 | * The merge is done by adding as child of the item the complete tree represented by the source |
|
73 | 75 | * item. If a part of the tree already exists in the item (based on the name of the nodes), it |
|
74 | 76 | * is merged by completing the existing tree by items "leaves" (products, components or nodes |
|
75 | 77 | * with no child). |
|
76 | 78 | * |
|
77 | 79 | * For example, with item representing the tree: |
|
78 | 80 | * R (root node) |
|
79 | 81 | * - N1 (node) |
|
80 | 82 | * -- N11 (node) |
|
81 | 83 | * --- P1 (product) |
|
82 | 84 | * --- P2 (product) |
|
83 | 85 | * - N2 (node) |
|
84 | 86 | * |
|
85 | 87 | * and the source item representing the tree: |
|
86 | 88 | * N1 (root node) |
|
87 | 89 | * - N11 (node) |
|
88 | 90 | * -- P3 (product) |
|
89 | 91 | * - N12 (node) |
|
90 | 92 | * |
|
91 | 93 | * The leaves of the source item to merge into the item are N1/N11/P3 and N1/N12 => we therefore |
|
92 | 94 | * have the following merge result: |
|
93 | 95 | * R |
|
94 | 96 | * - N1 |
|
95 | 97 | * -- N11 |
|
96 | 98 | * --- P1 |
|
97 | 99 | * --- P2 |
|
98 | 100 | * --- P3 (added leaf) |
|
99 | 101 | * -- N12 (added leaf) |
|
100 | 102 | * |
|
101 | 103 | * @param item the source item |
|
102 | 104 | * @remarks No control is performed on products or components that are merged into the same tree |
|
103 | 105 | * part (two products or components may have the same name) |
|
104 | 106 | * @remarks the merge is made by copy (source item is not changed and still exists after the |
|
105 | 107 | * operation) |
|
106 | 108 | */ |
|
107 | 109 | void merge(const DataSourceItem &item); |
|
108 | 110 | |
|
109 | 111 | bool isRoot() const noexcept; |
|
110 | 112 | |
|
111 | 113 | QString name() const noexcept; |
|
112 | 114 | |
|
113 | 115 | /** |
|
114 | 116 | * Get the item's parent |
|
115 | 117 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
116 | 118 | */ |
|
117 | 119 | DataSourceItem *parentItem() const noexcept; |
|
118 | 120 | |
|
119 | 121 | /** |
|
120 | 122 | * Gets the item's root |
|
121 | 123 | * @return the top parent, the item itself if it's the root item |
|
122 | 124 | */ |
|
123 | 125 | const DataSourceItem &rootItem() const noexcept; |
|
124 | 126 | |
|
125 | 127 | /** |
|
126 | 128 | * Sets or appends a value to a key |
|
127 | 129 | * @param key the key |
|
128 | 130 | * @param value the value |
|
129 | 131 | * @param append if true, the value is added to the values already existing for the key, |
|
130 | 132 | * otherwise it replaces the existing values |
|
131 | 133 | */ |
|
132 | 134 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; |
|
133 | 135 | |
|
134 | 136 | DataSourceItemType type() const noexcept; |
|
135 | 137 | |
|
136 | 138 | /** |
|
137 | 139 | * @brief Searches the first child matching the specified data. |
|
138 | 140 | * @param data The data to search. |
|
139 | 141 | * @param recursive So the search recursively. |
|
140 | 142 | * @return the item matching the data or nullptr if it was not found. |
|
141 | 143 | */ |
|
142 | 144 | DataSourceItem *findItem(const QVariantHash &data, bool recursive); |
|
143 | 145 | |
|
144 | 146 | bool operator==(const DataSourceItem &other); |
|
145 | 147 | bool operator!=(const DataSourceItem &other); |
|
146 | 148 | |
|
147 | 149 | private: |
|
148 | 150 | class DataSourceItemPrivate; |
|
149 | 151 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
150 | 152 | }; |
|
151 | 153 | |
|
152 | 154 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,192 +1,169 | |||
|
1 | 1 | #include "DataSource/DataSourceController.h" |
|
2 | 2 | #include "DataSource/DataSourceItem.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataProvider.h> |
|
5 | 5 | |
|
6 | 6 | #include <QMutex> |
|
7 | 7 | #include <QThread> |
|
8 | 8 | |
|
9 | 9 | #include <QDataStream> |
|
10 | 10 | #include <QDir> |
|
11 | 11 | #include <QStandardPaths> |
|
12 | 12 | |
|
13 | 13 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") |
|
14 | 14 | |
|
15 | namespace { | |
|
16 | ||
|
17 | /** | |
|
18 | * Builds the metadata of the variable that will be generated from the loading of an item | |
|
19 | * @param dataSourceItem the data source item from which to generate the metadata | |
|
20 | * @return the metadata of the variable | |
|
21 | */ | |
|
22 | QVariantHash variableMetadata(const DataSourceItem &dataSourceItem) | |
|
23 | { | |
|
24 | // Variable metadata contains... | |
|
25 | ||
|
26 | // ... all metadata of the item | |
|
27 | auto result = dataSourceItem.data(); | |
|
28 | ||
|
29 | // ... and the name of the plugin, recovered from root item | |
|
30 | result.insert(QStringLiteral("plugin"), dataSourceItem.rootItem().name()); | |
|
31 | ||
|
32 | return result; | |
|
33 | } | |
|
34 | ||
|
35 | } // namespace | |
|
36 | ||
|
37 | 15 | class DataSourceController::DataSourceControllerPrivate { |
|
38 | 16 | public: |
|
39 | 17 | QMutex m_WorkingMutex; |
|
40 | 18 | /// Data sources registered |
|
41 | 19 | QHash<QUuid, QString> m_DataSources; |
|
42 | 20 | /// Data sources structures |
|
43 | 21 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; |
|
44 | 22 | /// Data providers registered |
|
45 | 23 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and |
|
46 | 24 | /// continue to live without necessarily the data source controller |
|
47 | 25 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; |
|
48 | 26 | |
|
49 | 27 | // Search for the first datasource item matching the specified data |
|
50 | 28 | DataSourceItem *findDataSourceItem(const QVariantHash &data) |
|
51 | 29 | { |
|
52 | 30 | DataSourceItem *sourceItem = nullptr; |
|
53 | 31 | for (const auto &item : m_DataSourceItems) { |
|
54 | 32 | sourceItem = item.second->findItem(data, true); |
|
55 | 33 | if (sourceItem) { |
|
56 | 34 | break; |
|
57 | 35 | } |
|
58 | 36 | } |
|
59 | 37 | |
|
60 | 38 | return sourceItem; |
|
61 | 39 | } |
|
62 | 40 | }; |
|
63 | 41 | |
|
64 | 42 | DataSourceController::DataSourceController(QObject *parent) |
|
65 | 43 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} |
|
66 | 44 | { |
|
67 | 45 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") |
|
68 | 46 | << QThread::currentThread(); |
|
69 | 47 | } |
|
70 | 48 | |
|
71 | 49 | DataSourceController::~DataSourceController() |
|
72 | 50 | { |
|
73 | 51 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") |
|
74 | 52 | << QThread::currentThread(); |
|
75 | 53 | this->waitForFinish(); |
|
76 | 54 | } |
|
77 | 55 | |
|
78 | 56 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept |
|
79 | 57 | { |
|
80 | 58 | auto dataSourceUid = QUuid::createUuid(); |
|
81 | 59 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); |
|
82 | 60 | |
|
83 | 61 | return dataSourceUid; |
|
84 | 62 | } |
|
85 | 63 | |
|
86 | 64 | void DataSourceController::setDataSourceItem( |
|
87 | 65 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept |
|
88 | 66 | { |
|
89 | 67 | if (!dataSourceItem) { |
|
90 | 68 | qCWarning(LOG_DataSourceController()) |
|
91 | 69 | << tr("Data source item can't be registered (null item)"); |
|
92 | 70 | return; |
|
93 | 71 | } |
|
94 | 72 | |
|
95 | 73 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
96 | 74 | // The data provider is implicitly converted to a shared_ptr |
|
97 | 75 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); |
|
98 | 76 | |
|
99 | 77 | // Retrieves the data source item to emit the signal with it |
|
100 | 78 | auto it = impl->m_DataSourceItems.find(dataSourceUid); |
|
101 | 79 | if (it != impl->m_DataSourceItems.end()) { |
|
102 | 80 | emit dataSourceItemSet(it->second.get()); |
|
103 | 81 | } |
|
104 | 82 | } |
|
105 | 83 | else { |
|
106 | 84 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " |
|
107 | 85 | "data source has been registered with the uid") |
|
108 | 86 | .arg(dataSourceUid.toString()); |
|
109 | 87 | } |
|
110 | 88 | } |
|
111 | 89 | |
|
112 | 90 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, |
|
113 | 91 | std::unique_ptr<IDataProvider> dataProvider) noexcept |
|
114 | 92 | { |
|
115 | 93 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
116 | 94 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); |
|
117 | 95 | } |
|
118 | 96 | else { |
|
119 | 97 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " |
|
120 | 98 | "source has been registered with the uid") |
|
121 | 99 | .arg(dataSourceUid.toString()); |
|
122 | 100 | } |
|
123 | 101 | } |
|
124 | 102 | |
|
125 | 103 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, |
|
126 | 104 | const DataSourceItem &productItem) noexcept |
|
127 | 105 | { |
|
128 | 106 | if (productItem.type() == DataSourceItemType::PRODUCT |
|
129 | 107 | || productItem.type() == DataSourceItemType::COMPONENT) { |
|
130 | 108 | /// Retrieves the data provider of the data source (if any) |
|
131 | 109 | auto it = impl->m_DataProviders.find(dataSourceUid); |
|
132 | 110 | auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; |
|
133 | 111 | |
|
134 |
emit variableCreationRequested(productItem.name(), |
|
|
135 | dataProvider); | |
|
112 | emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider); | |
|
136 | 113 | } |
|
137 | 114 | else { |
|
138 | 115 | qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); |
|
139 | 116 | } |
|
140 | 117 | } |
|
141 | 118 | |
|
142 | 119 | QByteArray DataSourceController::mimeDataForProductsData(const QVariantList &productsData) |
|
143 | 120 | { |
|
144 | 121 | QByteArray encodedData; |
|
145 | 122 | QDataStream stream{&encodedData, QIODevice::WriteOnly}; |
|
146 | 123 | |
|
147 | 124 | stream << productsData; |
|
148 | 125 | |
|
149 | 126 | return encodedData; |
|
150 | 127 | } |
|
151 | 128 | |
|
152 | 129 | QVariantList DataSourceController::productsDataForMimeData(const QByteArray &mimeData) |
|
153 | 130 | { |
|
154 | 131 | QDataStream stream{mimeData}; |
|
155 | 132 | |
|
156 | 133 | QVariantList productList; |
|
157 | 134 | stream >> productList; |
|
158 | 135 | |
|
159 | 136 | return productList; |
|
160 | 137 | } |
|
161 | 138 | |
|
162 | 139 | void DataSourceController::initialize() |
|
163 | 140 | { |
|
164 | 141 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") |
|
165 | 142 | << QThread::currentThread(); |
|
166 | 143 | impl->m_WorkingMutex.lock(); |
|
167 | 144 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); |
|
168 | 145 | } |
|
169 | 146 | |
|
170 | 147 | void DataSourceController::finalize() |
|
171 | 148 | { |
|
172 | 149 | impl->m_WorkingMutex.unlock(); |
|
173 | 150 | } |
|
174 | 151 | |
|
175 | 152 | void DataSourceController::requestVariable(const QVariantHash &productData) |
|
176 | 153 | { |
|
177 | 154 | auto sourceItem = impl->findDataSourceItem(productData); |
|
178 | 155 | |
|
179 | 156 | if (sourceItem) { |
|
180 | 157 | auto sourceName = sourceItem->rootItem().name(); |
|
181 | 158 | auto sourceId = impl->m_DataSources.key(sourceName); |
|
182 | 159 | loadProductItem(sourceId, *sourceItem); |
|
183 | 160 | } |
|
184 | 161 | else { |
|
185 | 162 | qCWarning(LOG_DataSourceController()) << tr("requestVariable, product data not found"); |
|
186 | 163 | } |
|
187 | 164 | } |
|
188 | 165 | |
|
189 | 166 | void DataSourceController::waitForFinish() |
|
190 | 167 | { |
|
191 | 168 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
192 | 169 | } |
@@ -1,185 +1,186 | |||
|
1 | 1 | #include <DataSource/DataSourceItem.h> |
|
2 | 2 | #include <DataSource/DataSourceItemAction.h> |
|
3 | 3 | #include <DataSource/DataSourceItemMergeHelper.h> |
|
4 | 4 | |
|
5 | 5 | #include <QVector> |
|
6 | 6 | |
|
7 | 7 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); |
|
8 | const QString DataSourceItem::PLUGIN_DATA_KEY = QStringLiteral("plugin"); | |
|
8 | 9 | |
|
9 | 10 | struct DataSourceItem::DataSourceItemPrivate { |
|
10 | 11 | explicit DataSourceItemPrivate(DataSourceItemType type, QVariantHash data) |
|
11 | 12 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} |
|
12 | 13 | { |
|
13 | 14 | } |
|
14 | 15 | |
|
15 | 16 | DataSourceItem *m_Parent; |
|
16 | 17 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
17 | 18 | DataSourceItemType m_Type; |
|
18 | 19 | QVariantHash m_Data; |
|
19 | 20 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; |
|
20 | 21 | }; |
|
21 | 22 | |
|
22 | 23 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) |
|
23 | 24 | : DataSourceItem{type, QVariantHash{{NAME_DATA_KEY, name}}} |
|
24 | 25 | { |
|
25 | 26 | } |
|
26 | 27 | |
|
27 | 28 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVariantHash data) |
|
28 | 29 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} |
|
29 | 30 | { |
|
30 | 31 | } |
|
31 | 32 | |
|
32 | 33 | std::unique_ptr<DataSourceItem> DataSourceItem::clone() const |
|
33 | 34 | { |
|
34 | 35 | auto result = std::make_unique<DataSourceItem>(impl->m_Type, impl->m_Data); |
|
35 | 36 | |
|
36 | 37 | // Clones children |
|
37 | 38 | for (const auto &child : impl->m_Children) { |
|
38 | 39 | result->appendChild(std::move(child->clone())); |
|
39 | 40 | } |
|
40 | 41 | |
|
41 | 42 | // Clones actions |
|
42 | 43 | for (const auto &action : impl->m_Actions) { |
|
43 | 44 | result->addAction(std::move(action->clone())); |
|
44 | 45 | } |
|
45 | 46 | |
|
46 | 47 | return result; |
|
47 | 48 | } |
|
48 | 49 | |
|
49 | 50 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept |
|
50 | 51 | { |
|
51 | 52 | auto result = QVector<DataSourceItemAction *>{}; |
|
52 | 53 | |
|
53 | 54 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), |
|
54 | 55 | std::back_inserter(result), [](const auto &action) { return action.get(); }); |
|
55 | 56 | |
|
56 | 57 | return result; |
|
57 | 58 | } |
|
58 | 59 | |
|
59 | 60 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept |
|
60 | 61 | { |
|
61 | 62 | action->setDataSourceItem(this); |
|
62 | 63 | impl->m_Actions.push_back(std::move(action)); |
|
63 | 64 | } |
|
64 | 65 | |
|
65 | 66 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
66 | 67 | { |
|
67 | 68 | child->impl->m_Parent = this; |
|
68 | 69 | impl->m_Children.push_back(std::move(child)); |
|
69 | 70 | } |
|
70 | 71 | |
|
71 | 72 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
72 | 73 | { |
|
73 | 74 | if (childIndex < 0 || childIndex >= childCount()) { |
|
74 | 75 | return nullptr; |
|
75 | 76 | } |
|
76 | 77 | else { |
|
77 | 78 | return impl->m_Children.at(childIndex).get(); |
|
78 | 79 | } |
|
79 | 80 | } |
|
80 | 81 | |
|
81 | 82 | int DataSourceItem::childCount() const noexcept |
|
82 | 83 | { |
|
83 | 84 | return impl->m_Children.size(); |
|
84 | 85 | } |
|
85 | 86 | |
|
86 | 87 | QVariant DataSourceItem::data(const QString &key) const noexcept |
|
87 | 88 | { |
|
88 | 89 | return impl->m_Data.value(key); |
|
89 | 90 | } |
|
90 | 91 | |
|
91 | 92 | QVariantHash DataSourceItem::data() const noexcept |
|
92 | 93 | { |
|
93 | 94 | return impl->m_Data; |
|
94 | 95 | } |
|
95 | 96 | |
|
96 | 97 | void DataSourceItem::merge(const DataSourceItem &item) |
|
97 | 98 | { |
|
98 | 99 | DataSourceItemMergeHelper::merge(item, *this); |
|
99 | 100 | } |
|
100 | 101 | |
|
101 | 102 | bool DataSourceItem::isRoot() const noexcept |
|
102 | 103 | { |
|
103 | 104 | return impl->m_Parent == nullptr; |
|
104 | 105 | } |
|
105 | 106 | |
|
106 | 107 | QString DataSourceItem::name() const noexcept |
|
107 | 108 | { |
|
108 | 109 | return data(NAME_DATA_KEY).toString(); |
|
109 | 110 | } |
|
110 | 111 | |
|
111 | 112 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
112 | 113 | { |
|
113 | 114 | return impl->m_Parent; |
|
114 | 115 | } |
|
115 | 116 | |
|
116 | 117 | const DataSourceItem &DataSourceItem::rootItem() const noexcept |
|
117 | 118 | { |
|
118 | 119 | return isRoot() ? *this : parentItem()->rootItem(); |
|
119 | 120 | } |
|
120 | 121 | |
|
121 | 122 | void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept |
|
122 | 123 | { |
|
123 | 124 | auto it = impl->m_Data.constFind(key); |
|
124 | 125 | if (append && it != impl->m_Data.constEnd()) { |
|
125 | 126 | // Case of an existing value to which we want to add to the new value |
|
126 | 127 | if (it->canConvert<QVariantList>()) { |
|
127 | 128 | auto variantList = it->value<QVariantList>(); |
|
128 | 129 | variantList.append(value); |
|
129 | 130 | |
|
130 | 131 | impl->m_Data.insert(key, variantList); |
|
131 | 132 | } |
|
132 | 133 | else { |
|
133 | 134 | impl->m_Data.insert(key, QVariantList{*it, value}); |
|
134 | 135 | } |
|
135 | 136 | } |
|
136 | 137 | else { |
|
137 | 138 | // Other cases : |
|
138 | 139 | // - new value in map OR |
|
139 | 140 | // - replacement of an existing value (not appending) |
|
140 | 141 | impl->m_Data.insert(key, value); |
|
141 | 142 | } |
|
142 | 143 | } |
|
143 | 144 | |
|
144 | 145 | DataSourceItemType DataSourceItem::type() const noexcept |
|
145 | 146 | { |
|
146 | 147 | return impl->m_Type; |
|
147 | 148 | } |
|
148 | 149 | |
|
149 | 150 | DataSourceItem *DataSourceItem::findItem(const QVariantHash &data, bool recursive) |
|
150 | 151 | { |
|
151 | 152 | for (const auto &child : impl->m_Children) { |
|
152 | 153 | if (child->impl->m_Data == data) { |
|
153 | 154 | return child.get(); |
|
154 | 155 | } |
|
155 | 156 | |
|
156 | 157 | if (recursive) { |
|
157 | 158 | if (auto foundItem = child->findItem(data, true)) { |
|
158 | 159 | return foundItem; |
|
159 | 160 | } |
|
160 | 161 | } |
|
161 | 162 | } |
|
162 | 163 | |
|
163 | 164 | return nullptr; |
|
164 | 165 | } |
|
165 | 166 | |
|
166 | 167 | bool DataSourceItem::operator==(const DataSourceItem &other) |
|
167 | 168 | { |
|
168 | 169 | // Compares items' attributes |
|
169 | 170 | if (std::tie(impl->m_Type, impl->m_Data) == std::tie(other.impl->m_Type, other.impl->m_Data)) { |
|
170 | 171 | // Compares contents of items' children |
|
171 | 172 | return std::equal(std::cbegin(impl->m_Children), std::cend(impl->m_Children), |
|
172 | 173 | std::cbegin(other.impl->m_Children), |
|
173 | 174 | [](const auto &itemChild, const auto &otherChild) { |
|
174 | 175 | return *itemChild == *otherChild; |
|
175 | 176 | }); |
|
176 | 177 | } |
|
177 | 178 | else { |
|
178 | 179 | return false; |
|
179 | 180 | } |
|
180 | 181 | } |
|
181 | 182 | |
|
182 | 183 | bool DataSourceItem::operator!=(const DataSourceItem &other) |
|
183 | 184 | { |
|
184 | 185 | return !(*this == other); |
|
185 | 186 | } |
@@ -1,74 +1,77 | |||
|
1 | 1 | #include "AmdaPlugin.h" |
|
2 | 2 | #include "AmdaDefs.h" |
|
3 | 3 | #include "AmdaParser.h" |
|
4 | 4 | #include "AmdaProvider.h" |
|
5 | 5 | |
|
6 | 6 | #include <DataSource/DataSourceController.h> |
|
7 | 7 | #include <DataSource/DataSourceItem.h> |
|
8 | 8 | #include <DataSource/DataSourceItemAction.h> |
|
9 | 9 | |
|
10 | 10 | #include <SqpApplication.h> |
|
11 | 11 | |
|
12 | 12 | Q_LOGGING_CATEGORY(LOG_AmdaPlugin, "AmdaPlugin") |
|
13 | 13 | |
|
14 | 14 | namespace { |
|
15 | 15 | |
|
16 | 16 | /// Name of the data source |
|
17 | 17 | const auto DATA_SOURCE_NAME = QStringLiteral("AMDA"); |
|
18 | 18 | |
|
19 | 19 | /// Path of the file used to generate the data source item for AMDA |
|
20 | 20 | const auto JSON_FILE_PATH = QStringLiteral(":/samples/AmdaSampleV3.json"); |
|
21 | 21 | |
|
22 | 22 | void associateActions(DataSourceItem &item, const QUuid &dataSourceUid) |
|
23 | 23 | { |
|
24 | 24 | auto addLoadAction = [&item, dataSourceUid](const QString &label) { |
|
25 | 25 | item.addAction( |
|
26 | 26 | std::make_unique<DataSourceItemAction>(label, [dataSourceUid](DataSourceItem &item) { |
|
27 | 27 | if (auto app = sqpApp) { |
|
28 | 28 | app->dataSourceController().loadProductItem(dataSourceUid, item); |
|
29 | 29 | } |
|
30 | 30 | })); |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | 33 | const auto itemType = item.type(); |
|
34 | if (itemType == DataSourceItemType::PRODUCT) { | |
|
35 | addLoadAction(QObject::tr("Load %1 product").arg(item.name())); | |
|
36 | } | |
|
37 | else if (itemType == DataSourceItemType::COMPONENT) { | |
|
38 | addLoadAction(QObject::tr("Load %1 component").arg(item.name())); | |
|
34 | if (itemType == DataSourceItemType::PRODUCT || itemType == DataSourceItemType::COMPONENT) { | |
|
35 | // Adds plugin name to item metadata | |
|
36 | item.setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME); | |
|
37 | ||
|
38 | // Adds load action | |
|
39 | auto actionLabel = QObject::tr( | |
|
40 | itemType == DataSourceItemType::PRODUCT ? "Load %1 product" : "Load %1 component"); | |
|
41 | addLoadAction(actionLabel.arg(item.name())); | |
|
39 | 42 | } |
|
40 | 43 | |
|
41 | 44 | auto count = item.childCount(); |
|
42 | 45 | for (auto i = 0; i < count; ++i) { |
|
43 | 46 | if (auto child = item.child(i)) { |
|
44 | 47 | associateActions(*child, dataSourceUid); |
|
45 | 48 | } |
|
46 | 49 | } |
|
47 | 50 | } |
|
48 | 51 | |
|
49 | 52 | } // namespace |
|
50 | 53 | |
|
51 | 54 | void AmdaPlugin::initialize() |
|
52 | 55 | { |
|
53 | 56 | if (auto app = sqpApp) { |
|
54 | 57 | // Registers to the data source controller |
|
55 | 58 | auto &dataSourceController = app->dataSourceController(); |
|
56 | 59 | auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME); |
|
57 | 60 | |
|
58 | 61 | // Sets data source tree |
|
59 | 62 | if (auto dataSourceItem = AmdaParser::readJson(JSON_FILE_PATH)) { |
|
60 | 63 | associateActions(*dataSourceItem, dataSourceUid); |
|
61 | 64 | |
|
62 | 65 | dataSourceController.setDataSourceItem(dataSourceUid, std::move(dataSourceItem)); |
|
63 | 66 | } |
|
64 | 67 | else { |
|
65 | 68 | qCCritical(LOG_AmdaPlugin()) << tr("No data source item could be generated for AMDA"); |
|
66 | 69 | } |
|
67 | 70 | |
|
68 | 71 | // Sets data provider |
|
69 | 72 | dataSourceController.setDataProvider(dataSourceUid, std::make_unique<AmdaProvider>()); |
|
70 | 73 | } |
|
71 | 74 | else { |
|
72 | 75 | qCWarning(LOG_AmdaPlugin()) << tr("Can't access to SciQlop application"); |
|
73 | 76 | } |
|
74 | 77 | } |
@@ -1,114 +1,118 | |||
|
1 | 1 | #include "MockPlugin.h" |
|
2 | 2 | #include "CosinusProvider.h" |
|
3 | 3 | #include "MockDefs.h" |
|
4 | 4 | |
|
5 | 5 | #include <DataSource/DataSourceController.h> |
|
6 | 6 | #include <DataSource/DataSourceItem.h> |
|
7 | 7 | #include <DataSource/DataSourceItemAction.h> |
|
8 | 8 | |
|
9 | 9 | #include <SqpApplication.h> |
|
10 | 10 | |
|
11 | 11 | Q_LOGGING_CATEGORY(LOG_MockPlugin, "MockPlugin") |
|
12 | 12 | |
|
13 | 13 | namespace { |
|
14 | 14 | |
|
15 | 15 | /// Name of the data source |
|
16 | 16 | const auto DATA_SOURCE_NAME = QStringLiteral("MMS"); |
|
17 | 17 | |
|
18 | 18 | /// Creates the data provider relative to the plugin |
|
19 | 19 | std::unique_ptr<IDataProvider> createDataProvider() noexcept |
|
20 | 20 | { |
|
21 | 21 | return std::make_unique<CosinusProvider>(); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | std::unique_ptr<DataSourceItem> createProductItem(const QVariantHash &data, |
|
25 | 25 | const QUuid &dataSourceUid) |
|
26 | 26 | { |
|
27 | 27 | auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, data); |
|
28 | ||
|
29 | // Adds plugin name to product metadata | |
|
30 | result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME); | |
|
31 | ||
|
28 | 32 | auto productName = data.value(DataSourceItem::NAME_DATA_KEY).toString(); |
|
29 | 33 | |
|
30 | 34 | // Add action to load product from DataSourceController |
|
31 | 35 | result->addAction(std::make_unique<DataSourceItemAction>( |
|
32 | 36 | QObject::tr("Load %1 product").arg(productName), |
|
33 | 37 | [productName, dataSourceUid](DataSourceItem &item) { |
|
34 | 38 | if (auto app = sqpApp) { |
|
35 | 39 | app->dataSourceController().loadProductItem(dataSourceUid, item); |
|
36 | 40 | } |
|
37 | 41 | })); |
|
38 | 42 | |
|
39 | 43 | return result; |
|
40 | 44 | } |
|
41 | 45 | |
|
42 | 46 | /// Creates the data source item relative to the plugin |
|
43 | 47 | std::unique_ptr<DataSourceItem> createDataSourceItem(const QUuid &dataSourceUid) noexcept |
|
44 | 48 | { |
|
45 | 49 | // Magnetic field products |
|
46 | 50 | auto magneticFieldFolder = std::make_unique<DataSourceItem>(DataSourceItemType::NODE, |
|
47 | 51 | QStringLiteral("Magnetic field")); |
|
48 | 52 | magneticFieldFolder->appendChild( |
|
49 | 53 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Scalar 10 Hz")}, |
|
50 | 54 | {COSINUS_TYPE_KEY, "scalar"}, |
|
51 | 55 | {COSINUS_FREQUENCY_KEY, 10.}}, |
|
52 | 56 | dataSourceUid)); |
|
53 | 57 | magneticFieldFolder->appendChild( |
|
54 | 58 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Scalar 60 Hz")}, |
|
55 | 59 | {COSINUS_TYPE_KEY, "scalar"}, |
|
56 | 60 | {COSINUS_FREQUENCY_KEY, 60.}}, |
|
57 | 61 | dataSourceUid)); |
|
58 | 62 | magneticFieldFolder->appendChild( |
|
59 | 63 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Scalar 100 Hz")}, |
|
60 | 64 | {COSINUS_TYPE_KEY, "scalar"}, |
|
61 | 65 | {COSINUS_FREQUENCY_KEY, 100.}}, |
|
62 | 66 | dataSourceUid)); |
|
63 | 67 | magneticFieldFolder->appendChild( |
|
64 | 68 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Vector 10 Hz")}, |
|
65 | 69 | {COSINUS_TYPE_KEY, "vector"}, |
|
66 | 70 | {COSINUS_FREQUENCY_KEY, 10.}}, |
|
67 | 71 | dataSourceUid)); |
|
68 | 72 | magneticFieldFolder->appendChild( |
|
69 | 73 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Vector 60 Hz")}, |
|
70 | 74 | {COSINUS_TYPE_KEY, "vector"}, |
|
71 | 75 | {COSINUS_FREQUENCY_KEY, 60.}}, |
|
72 | 76 | dataSourceUid)); |
|
73 | 77 | magneticFieldFolder->appendChild( |
|
74 | 78 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Vector 100 Hz")}, |
|
75 | 79 | {COSINUS_TYPE_KEY, "vector"}, |
|
76 | 80 | {COSINUS_FREQUENCY_KEY, 100.}}, |
|
77 | 81 | dataSourceUid)); |
|
78 | 82 | magneticFieldFolder->appendChild( |
|
79 | 83 | createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Spectrogram 1 Hz")}, |
|
80 | 84 | {COSINUS_TYPE_KEY, "spectrogram"}, |
|
81 | 85 | {COSINUS_FREQUENCY_KEY, 1.}}, |
|
82 | 86 | dataSourceUid)); |
|
83 | 87 | |
|
84 | 88 | // Electric field products |
|
85 | 89 | auto electricFieldFolder = std::make_unique<DataSourceItem>(DataSourceItemType::NODE, |
|
86 | 90 | QStringLiteral("Electric field")); |
|
87 | 91 | |
|
88 | 92 | // Root |
|
89 | 93 | auto root = std::make_unique<DataSourceItem>(DataSourceItemType::NODE, DATA_SOURCE_NAME); |
|
90 | 94 | root->appendChild(std::move(magneticFieldFolder)); |
|
91 | 95 | root->appendChild(std::move(electricFieldFolder)); |
|
92 | 96 | |
|
93 | 97 | return root; |
|
94 | 98 | } |
|
95 | 99 | |
|
96 | 100 | } // namespace |
|
97 | 101 | |
|
98 | 102 | void MockPlugin::initialize() |
|
99 | 103 | { |
|
100 | 104 | if (auto app = sqpApp) { |
|
101 | 105 | // Registers to the data source controller |
|
102 | 106 | auto &dataSourceController = app->dataSourceController(); |
|
103 | 107 | auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME); |
|
104 | 108 | |
|
105 | 109 | // Sets data source tree |
|
106 | 110 | dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem(dataSourceUid)); |
|
107 | 111 | |
|
108 | 112 | // Sets data provider |
|
109 | 113 | dataSourceController.setDataProvider(dataSourceUid, createDataProvider()); |
|
110 | 114 | } |
|
111 | 115 | else { |
|
112 | 116 | qCWarning(LOG_MockPlugin()) << tr("Can't access to SciQlop application"); |
|
113 | 117 | } |
|
114 | 118 | } |
General Comments 0
You need to be logged in to leave comments.
Login now