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