@@ -1,79 +1,81 | |||
|
1 | 1 | #ifndef SCIQLOP_DATASOURCECONTROLLER_H |
|
2 | 2 | #define SCIQLOP_DATASOURCECONTROLLER_H |
|
3 | 3 | |
|
4 | 4 | #include <QLoggingCategory> |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include <QUuid> |
|
7 | 7 | |
|
8 | 8 | #include <Common/spimpl.h> |
|
9 | 9 | |
|
10 | 10 | Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceController) |
|
11 | 11 | |
|
12 | 12 | class DataSourceItem; |
|
13 | 13 | class IDataProvider; |
|
14 | 14 | |
|
15 | 15 | /** |
|
16 | 16 | * @brief The DataSourceController class aims to make the link between SciQlop and its plugins. This |
|
17 | 17 | * is the intermediate class that SciQlop has to use in the way to connect a data source. Please |
|
18 | 18 | * first use register method to initialize a plugin specified by its metadata name (JSON plugin |
|
19 | 19 | * source) then others specifics method will be able to access it. You can load a data source driver |
|
20 | 20 | * plugin then create a data source. |
|
21 | 21 | */ |
|
22 | 22 | class DataSourceController : public QObject { |
|
23 | 23 | Q_OBJECT |
|
24 | 24 | public: |
|
25 | 25 | explicit DataSourceController(QObject *parent = 0); |
|
26 | 26 | virtual ~DataSourceController(); |
|
27 | 27 | |
|
28 | 28 | /** |
|
29 | 29 | * Registers a data source. The method delivers a unique id that can be used afterwards to |
|
30 | 30 | * access to the data source properties (structure, connection parameters, data provider, etc.) |
|
31 | 31 | * @param dataSourceName the name of the data source |
|
32 | 32 | * @return the unique id with which the data source has been registered |
|
33 | 33 | */ |
|
34 | 34 | QUuid registerDataSource(const QString &dataSourceName) noexcept; |
|
35 | 35 | |
|
36 | 36 | /** |
|
37 | 37 | * Sets the structure of a data source. The controller takes ownership of the structure. |
|
38 | 38 | * @param dataSourceUid the unique id with which the data source has been registered into the |
|
39 | 39 | * controller. If it is invalid, the method has no effect. |
|
40 | 40 | * @param dataSourceItem the structure of the data source |
|
41 | 41 | * @sa registerDataSource() |
|
42 | 42 | */ |
|
43 | 43 | void setDataSourceItem(const QUuid &dataSourceUid, |
|
44 | 44 | std::unique_ptr<DataSourceItem> dataSourceItem) noexcept; |
|
45 | 45 | |
|
46 | 46 | /** |
|
47 | 47 | * Sets the data provider used to retrieve data from of a data source. The controller takes |
|
48 | 48 | * ownership of the provider. |
|
49 | 49 | * @param dataSourceUid the unique id with which the data source has been registered into the |
|
50 | 50 | * controller. If it is invalid, the method has no effect. |
|
51 | 51 | * @param dataProvider the provider of the data source |
|
52 | 52 | * @sa registerDataSource() |
|
53 | 53 | */ |
|
54 | 54 | void setDataProvider(const QUuid &dataSourceUid, |
|
55 | 55 | std::unique_ptr<IDataProvider> dataProvider) noexcept; |
|
56 | 56 | |
|
57 | 57 | /** |
|
58 | 58 | * Loads an item (product) as a variable in SciQlop |
|
59 | * @param dataSourceUid the unique id of the data source containing the item. It is used to get | |
|
60 | * the data provider associated to the data source, and pass it to for the variable creation | |
|
59 | 61 | * @param productItem the item to load |
|
60 | 62 | */ |
|
61 | void loadProductItem(const DataSourceItem &productItem) noexcept; | |
|
63 | void loadProductItem(const QUuid &dataSourceUid, const DataSourceItem &productItem) noexcept; | |
|
62 | 64 | |
|
63 | 65 | public slots: |
|
64 | 66 | /// Manage init/end of the controller |
|
65 | 67 | void initialize(); |
|
66 | 68 | void finalize(); |
|
67 | 69 | |
|
68 | 70 | signals: |
|
69 | 71 | /// Signal emitted when a structure has been set for a data source |
|
70 | 72 | void dataSourceItemSet(DataSourceItem *dataSourceItem); |
|
71 | 73 | |
|
72 | 74 | private: |
|
73 | 75 | void waitForFinish(); |
|
74 | 76 | |
|
75 | 77 | class DataSourceControllerPrivate; |
|
76 | 78 | spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl; |
|
77 | 79 | }; |
|
78 | 80 | |
|
79 | 81 | #endif // SCIQLOP_DATASOURCECONTROLLER_H |
@@ -1,100 +1,104 | |||
|
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 <QDir> |
|
10 | 10 | #include <QStandardPaths> |
|
11 | 11 | |
|
12 | 12 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") |
|
13 | 13 | |
|
14 | 14 | class DataSourceController::DataSourceControllerPrivate { |
|
15 | 15 | public: |
|
16 | 16 | QMutex m_WorkingMutex; |
|
17 | 17 | /// Data sources registered |
|
18 | 18 | QHash<QUuid, QString> m_DataSources; |
|
19 | 19 | /// Data sources structures |
|
20 | 20 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; |
|
21 | 21 | /// Data providers registered |
|
22 | std::map<QUuid, std::unique_ptr<IDataProvider> > m_DataProviders; | |
|
22 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and | |
|
23 | /// continue to live without necessarily the data source controller | |
|
24 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; | |
|
23 | 25 | }; |
|
24 | 26 | |
|
25 | 27 | DataSourceController::DataSourceController(QObject *parent) |
|
26 | 28 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} |
|
27 | 29 | { |
|
28 | 30 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") |
|
29 | 31 | << QThread::currentThread(); |
|
30 | 32 | } |
|
31 | 33 | |
|
32 | 34 | DataSourceController::~DataSourceController() |
|
33 | 35 | { |
|
34 | 36 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") |
|
35 | 37 | << QThread::currentThread(); |
|
36 | 38 | this->waitForFinish(); |
|
37 | 39 | } |
|
38 | 40 | |
|
39 | 41 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept |
|
40 | 42 | { |
|
41 | 43 | auto dataSourceUid = QUuid::createUuid(); |
|
42 | 44 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); |
|
43 | 45 | |
|
44 | 46 | return dataSourceUid; |
|
45 | 47 | } |
|
46 | 48 | |
|
47 | 49 | void DataSourceController::setDataSourceItem( |
|
48 | 50 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept |
|
49 | 51 | { |
|
50 | 52 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
53 | // The data provider is implicitly converted to a shared_ptr | |
|
51 | 54 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); |
|
52 | 55 | |
|
53 | 56 | // Retrieves the data source item to emit the signal with it |
|
54 | 57 | auto it = impl->m_DataSourceItems.find(dataSourceUid); |
|
55 | 58 | if (it != impl->m_DataSourceItems.end()) { |
|
56 | 59 | emit dataSourceItemSet(it->second.get()); |
|
57 | 60 | } |
|
58 | 61 | } |
|
59 | 62 | else { |
|
60 | 63 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " |
|
61 | 64 | "data source has been registered with the uid") |
|
62 | 65 | .arg(dataSourceUid.toString()); |
|
63 | 66 | } |
|
64 | 67 | } |
|
65 | 68 | |
|
66 | 69 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, |
|
67 | 70 | std::unique_ptr<IDataProvider> dataProvider) noexcept |
|
68 | 71 | { |
|
69 | 72 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
70 | 73 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); |
|
71 | 74 | } |
|
72 | 75 | else { |
|
73 | 76 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " |
|
74 | 77 | "source has been registered with the uid") |
|
75 | 78 | .arg(dataSourceUid.toString()); |
|
76 | 79 | } |
|
77 | 80 | } |
|
78 | 81 | |
|
79 |
void DataSourceController::loadProductItem(const |
|
|
82 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, | |
|
83 | const DataSourceItem &productItem) noexcept | |
|
80 | 84 | { |
|
81 | 85 | /// @todo ALX |
|
82 | 86 | } |
|
83 | 87 | |
|
84 | 88 | void DataSourceController::initialize() |
|
85 | 89 | { |
|
86 | 90 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") |
|
87 | 91 | << QThread::currentThread(); |
|
88 | 92 | impl->m_WorkingMutex.lock(); |
|
89 | 93 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); |
|
90 | 94 | } |
|
91 | 95 | |
|
92 | 96 | void DataSourceController::finalize() |
|
93 | 97 | { |
|
94 | 98 | impl->m_WorkingMutex.unlock(); |
|
95 | 99 | } |
|
96 | 100 | |
|
97 | 101 | void DataSourceController::waitForFinish() |
|
98 | 102 | { |
|
99 | 103 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
100 | 104 | } |
@@ -1,79 +1,81 | |||
|
1 | 1 | #include "MockPlugin.h" |
|
2 | 2 | #include "CosinusProvider.h" |
|
3 | 3 | |
|
4 | 4 | #include <DataSource/DataSourceController.h> |
|
5 | 5 | #include <DataSource/DataSourceItem.h> |
|
6 | 6 | #include <DataSource/DataSourceItemAction.h> |
|
7 | 7 | |
|
8 | 8 | #include <SqpApplication.h> |
|
9 | 9 | |
|
10 | 10 | Q_LOGGING_CATEGORY(LOG_MockPlugin, "MockPlugin") |
|
11 | 11 | |
|
12 | 12 | namespace { |
|
13 | 13 | |
|
14 | 14 | /// Name of the data source |
|
15 | 15 | const auto DATA_SOURCE_NAME = QStringLiteral("MMS"); |
|
16 | 16 | |
|
17 | 17 | /// Creates the data provider relative to the plugin |
|
18 | 18 | std::unique_ptr<IDataProvider> createDataProvider() noexcept |
|
19 | 19 | { |
|
20 | 20 | return std::make_unique<CosinusProvider>(); |
|
21 | 21 | } |
|
22 | 22 | |
|
23 |
std::unique_ptr<DataSourceItem> createProductItem(const QString &productName |
|
|
23 | std::unique_ptr<DataSourceItem> createProductItem(const QString &productName, | |
|
24 | const QUuid &dataSourceUid) | |
|
24 | 25 | { |
|
25 | 26 | auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, |
|
26 | 27 | QVector<QVariant>{productName}); |
|
27 | 28 | |
|
28 | 29 | // Add action to load product from DataSourceController |
|
29 | 30 | result->addAction(std::make_unique<DataSourceItemAction>( |
|
30 |
QObject::tr("Load %1 product").arg(productName), |
|
|
31 | QObject::tr("Load %1 product").arg(productName), | |
|
32 | [productName, dataSourceUid](DataSourceItem &item) { | |
|
31 | 33 | if (auto app = sqpApp) { |
|
32 | app->dataSourceController().loadProductItem(item); | |
|
34 | app->dataSourceController().loadProductItem(dataSourceUid, item); | |
|
33 | 35 | } |
|
34 | 36 | })); |
|
35 | 37 | |
|
36 | 38 | return result; |
|
37 | 39 | } |
|
38 | 40 | |
|
39 | 41 | /// Creates the data source item relative to the plugin |
|
40 | std::unique_ptr<DataSourceItem> createDataSourceItem() noexcept | |
|
42 | std::unique_ptr<DataSourceItem> createDataSourceItem(const QUuid &dataSourceUid) noexcept | |
|
41 | 43 | { |
|
42 | 44 | // Magnetic field products |
|
43 | 45 | auto magneticFieldFolder = std::make_unique<DataSourceItem>( |
|
44 | 46 | DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Magnetic field")}); |
|
45 | magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM"))); | |
|
46 | magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC"))); | |
|
47 | magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM"), dataSourceUid)); | |
|
48 | magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC"), dataSourceUid)); | |
|
47 | 49 | |
|
48 | 50 | // Electric field products |
|
49 | 51 | auto electricFieldFolder = std::make_unique<DataSourceItem>( |
|
50 | 52 | DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Electric field")}); |
|
51 | 53 | |
|
52 | 54 | // Root |
|
53 | 55 | auto root = std::make_unique<DataSourceItem>(DataSourceItemType::NODE, |
|
54 | 56 | QVector<QVariant>{DATA_SOURCE_NAME}); |
|
55 | 57 | root->appendChild(std::move(magneticFieldFolder)); |
|
56 | 58 | root->appendChild(std::move(electricFieldFolder)); |
|
57 | 59 | |
|
58 | 60 | return root; |
|
59 | 61 | } |
|
60 | 62 | |
|
61 | 63 | } // namespace |
|
62 | 64 | |
|
63 | 65 | void MockPlugin::initialize() |
|
64 | 66 | { |
|
65 | 67 | if (auto app = sqpApp) { |
|
66 | 68 | // Registers to the data source controller |
|
67 | 69 | auto &dataSourceController = app->dataSourceController(); |
|
68 | 70 | auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME); |
|
69 | 71 | |
|
70 | 72 | // Sets data source tree |
|
71 | dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem()); | |
|
73 | dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem(dataSourceUid)); | |
|
72 | 74 | |
|
73 | 75 | // Sets data provider |
|
74 | 76 | dataSourceController.setDataProvider(dataSourceUid, createDataProvider()); |
|
75 | 77 | } |
|
76 | 78 | else { |
|
77 | 79 | qCWarning(LOG_MockPlugin()) << tr("Can't access to SciQlop application"); |
|
78 | 80 | } |
|
79 | 81 | } |
General Comments 0
You need to be logged in to leave comments.
Login now