##// END OF EJS Templates
Adds method for registering a data provider in the data source controller
Alexandre Leroux -
r127:95d739a33d44
parent child
Show More
@@ -1,61 +1,73
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 class IDataProvider;
13 14
14 15 /**
15 16 * @brief The DataSourceController class aims to make the link between SciQlop and its plugins. This
16 17 * is the intermediate class that SciQlop has to use in the way to connect a data source. Please
17 18 * first use register method to initialize a plugin specified by its metadata name (JSON plugin
18 19 * source) then others specifics method will be able to access it. You can load a data source driver
19 20 * plugin then create a data source.
20 21 */
21 22 class DataSourceController : public QObject {
22 23 Q_OBJECT
23 24 public:
24 25 explicit DataSourceController(QObject *parent = 0);
25 26 virtual ~DataSourceController();
26 27
27 28 /**
28 29 * Registers a data source. The method delivers a unique id that can be used afterwards to
29 30 * access to the data source properties (structure, connection parameters, data provider, etc.)
30 31 * @param dataSourceName the name of the data source
31 32 * @return the unique id with which the data source has been registered
32 33 */
33 34 QUuid registerDataSource(const QString &dataSourceName) noexcept;
34 35
35 36 /**
36 37 * Sets the structure of a data source. The controller takes ownership of the structure.
37 38 * @param dataSourceUid the unique id with which the data source has been registered into the
38 39 * controller. If it is invalid, the method has no effect.
39 40 * @param dataSourceItem the structure of the data source
40 41 * @sa registerDataSource()
41 42 */
42 43 void setDataSourceItem(const QUuid &dataSourceUid,
43 44 std::unique_ptr<DataSourceItem> dataSourceItem) noexcept;
44 45
46 /**
47 * Sets the data provider used to retrieve data from of a data source. The controller takes
48 * ownership of the provider.
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.
51 * @param dataProvider the provider of the data source
52 * @sa registerDataSource()
53 */
54 void setDataProvider(const QUuid &dataSourceUid,
55 std::unique_ptr<IDataProvider> dataProvider) noexcept;
56
45 57 public slots:
46 58 /// Manage init/end of the controller
47 59 void initialize();
48 60 void finalize();
49 61
50 62 signals:
51 63 /// Signal emitted when a structure has been set for a data source
52 64 void dataSourceItemSet(DataSourceItem *dataSourceItem);
53 65
54 66 private:
55 67 void waitForFinish();
56 68
57 69 class DataSourceControllerPrivate;
58 70 spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl;
59 71 };
60 72
61 73 #endif // SCIQLOP_DATASOURCECONTROLLER_H
@@ -1,78 +1,95
1 1 #include <DataSource/DataSourceController.h>
2 2 #include <DataSource/DataSourceItem.h>
3 3
4 #include <Data/IDataProvider.h>
5
4 6 #include <QMutex>
5 7 #include <QThread>
6 8
7 9 #include <QDir>
8 10 #include <QStandardPaths>
9 11
10 12 Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController")
11 13
12 14 class DataSourceController::DataSourceControllerPrivate {
13 15 public:
14 16 QMutex m_WorkingMutex;
15 17 /// Data sources registered
16 18 QHash<QUuid, QString> m_DataSources;
17 19 /// Data sources structures
18 20 std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems;
21 /// Data providers registered
22 std::map<QUuid, std::unique_ptr<IDataProvider> > m_DataProviders;
19 23 };
20 24
21 25 DataSourceController::DataSourceController(QObject *parent)
22 26 : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()}
23 27 {
24 28 qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction")
25 29 << QThread::currentThread();
26 30 }
27 31
28 32 DataSourceController::~DataSourceController()
29 33 {
30 34 qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction")
31 35 << QThread::currentThread();
32 36 this->waitForFinish();
33 37 }
34 38
35 39 QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept
36 40 {
37 41 auto dataSourceUid = QUuid::createUuid();
38 42 impl->m_DataSources.insert(dataSourceUid, dataSourceName);
39 43
40 44 return dataSourceUid;
41 45 }
42 46
43 47 void DataSourceController::setDataSourceItem(
44 48 const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept
45 49 {
46 50 if (impl->m_DataSources.contains(dataSourceUid)) {
47 51 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
48 52
49 53 // Retrieves the data source item to emit the signal with it
50 54 auto it = impl->m_DataSourceItems.find(dataSourceUid);
51 55 if (it != impl->m_DataSourceItems.end()) {
52 56 emit dataSourceItemSet(it->second.get());
53 57 }
54 58 }
55 59 else {
56 60 qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no "
57 61 "data source has been registered with the uid")
58 62 .arg(dataSourceUid.toString());
59 63 }
60 64 }
61 65
66 void DataSourceController::setDataProvider(const QUuid &dataSourceUid,
67 std::unique_ptr<IDataProvider> dataProvider) noexcept
68 {
69 if (impl->m_DataSources.contains(dataSourceUid)) {
70 impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider)));
71 }
72 else {
73 qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data "
74 "source has been registered with the uid")
75 .arg(dataSourceUid.toString());
76 }
77 }
78
62 79 void DataSourceController::initialize()
63 80 {
64 81 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
65 82 << QThread::currentThread();
66 83 impl->m_WorkingMutex.lock();
67 84 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
68 85 }
69 86
70 87 void DataSourceController::finalize()
71 88 {
72 89 impl->m_WorkingMutex.unlock();
73 90 }
74 91
75 92 void DataSourceController::waitForFinish()
76 93 {
77 94 QMutexLocker locker{&impl->m_WorkingMutex};
78 95 }
General Comments 0
You need to be logged in to leave comments. Login now