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