@@ -1,90 +1,90 | |||
|
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 | * @param dataSourceItem the structure of the data source | |
|
40 | * @param dataSourceItem the structure of the data source. It must be not null to be registered | |
|
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 | 59 | * @param dataSourceUid the unique id of the data source containing the item. It is used to get |
|
60 | 60 | * the data provider associated to the data source, and pass it to for the variable creation |
|
61 | 61 | * @param productItem the item to load |
|
62 | 62 | */ |
|
63 | 63 | void loadProductItem(const QUuid &dataSourceUid, const DataSourceItem &productItem) noexcept; |
|
64 | 64 | |
|
65 | 65 | public slots: |
|
66 | 66 | /// Manage init/end of the controller |
|
67 | 67 | void initialize(); |
|
68 | 68 | void finalize(); |
|
69 | 69 | |
|
70 | 70 | signals: |
|
71 | 71 | /// Signal emitted when a structure has been set for a data source |
|
72 | 72 | void dataSourceItemSet(DataSourceItem *dataSourceItem); |
|
73 | 73 | |
|
74 | 74 | /** |
|
75 | 75 | * Signal emitted when a variable creation is asked for a product |
|
76 | 76 | * @param variableName the name of the variable |
|
77 | 77 | * @param variableProvider the provider that will be used to retrieve the data of the variable |
|
78 | 78 | * (can be null) |
|
79 | 79 | */ |
|
80 | 80 | void variableCreationRequested(const QString &variableName, |
|
81 | 81 | std::shared_ptr<IDataProvider> variableProvider); |
|
82 | 82 | |
|
83 | 83 | private: |
|
84 | 84 | void waitForFinish(); |
|
85 | 85 | |
|
86 | 86 | class DataSourceControllerPrivate; |
|
87 | 87 | spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl; |
|
88 | 88 | }; |
|
89 | 89 | |
|
90 | 90 | #endif // SCIQLOP_DATASOURCECONTROLLER_H |
@@ -1,114 +1,120 | |||
|
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 | 22 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and |
|
23 | 23 | /// continue to live without necessarily the data source controller |
|
24 | 24 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; |
|
25 | 25 | }; |
|
26 | 26 | |
|
27 | 27 | DataSourceController::DataSourceController(QObject *parent) |
|
28 | 28 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} |
|
29 | 29 | { |
|
30 | 30 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") |
|
31 | 31 | << QThread::currentThread(); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | DataSourceController::~DataSourceController() |
|
35 | 35 | { |
|
36 | 36 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") |
|
37 | 37 | << QThread::currentThread(); |
|
38 | 38 | this->waitForFinish(); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept |
|
42 | 42 | { |
|
43 | 43 | auto dataSourceUid = QUuid::createUuid(); |
|
44 | 44 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); |
|
45 | 45 | |
|
46 | 46 | return dataSourceUid; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | void DataSourceController::setDataSourceItem( |
|
50 | 50 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept |
|
51 | 51 | { |
|
52 | if (!dataSourceItem) { | |
|
53 | qCWarning(LOG_DataSourceController()) | |
|
54 | << tr("Data source item can't be registered (null item)"); | |
|
55 | return; | |
|
56 | } | |
|
57 | ||
|
52 | 58 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
53 | 59 | // The data provider is implicitly converted to a shared_ptr |
|
54 | 60 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); |
|
55 | 61 | |
|
56 | 62 | // Retrieves the data source item to emit the signal with it |
|
57 | 63 | auto it = impl->m_DataSourceItems.find(dataSourceUid); |
|
58 | 64 | if (it != impl->m_DataSourceItems.end()) { |
|
59 | 65 | emit dataSourceItemSet(it->second.get()); |
|
60 | 66 | } |
|
61 | 67 | } |
|
62 | 68 | else { |
|
63 | 69 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " |
|
64 | 70 | "data source has been registered with the uid") |
|
65 | 71 | .arg(dataSourceUid.toString()); |
|
66 | 72 | } |
|
67 | 73 | } |
|
68 | 74 | |
|
69 | 75 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, |
|
70 | 76 | std::unique_ptr<IDataProvider> dataProvider) noexcept |
|
71 | 77 | { |
|
72 | 78 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
73 | 79 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); |
|
74 | 80 | } |
|
75 | 81 | else { |
|
76 | 82 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " |
|
77 | 83 | "source has been registered with the uid") |
|
78 | 84 | .arg(dataSourceUid.toString()); |
|
79 | 85 | } |
|
80 | 86 | } |
|
81 | 87 | |
|
82 | 88 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, |
|
83 | 89 | const DataSourceItem &productItem) noexcept |
|
84 | 90 | { |
|
85 | 91 | if (productItem.type() == DataSourceItemType::PRODUCT) { |
|
86 | 92 | /// Retrieves the data provider of the data source (if any) |
|
87 | 93 | auto it = impl->m_DataProviders.find(dataSourceUid); |
|
88 | 94 | auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; |
|
89 | 95 | |
|
90 | 96 | /// @todo retrieve timerange, and pass it to the signal |
|
91 | 97 | emit variableCreationRequested(productItem.name(), dataProvider); |
|
92 | 98 | } |
|
93 | 99 | else { |
|
94 | 100 | qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); |
|
95 | 101 | } |
|
96 | 102 | } |
|
97 | 103 | |
|
98 | 104 | void DataSourceController::initialize() |
|
99 | 105 | { |
|
100 | 106 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") |
|
101 | 107 | << QThread::currentThread(); |
|
102 | 108 | impl->m_WorkingMutex.lock(); |
|
103 | 109 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); |
|
104 | 110 | } |
|
105 | 111 | |
|
106 | 112 | void DataSourceController::finalize() |
|
107 | 113 | { |
|
108 | 114 | impl->m_WorkingMutex.unlock(); |
|
109 | 115 | } |
|
110 | 116 | |
|
111 | 117 | void DataSourceController::waitForFinish() |
|
112 | 118 | { |
|
113 | 119 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
114 | 120 | } |
General Comments 0
You need to be logged in to leave comments.
Login now