##// END OF EJS Templates
Amda provider update (3)...
Alexandre Leroux -
r381:4f3a3edcd1d8
parent child
Show More
@@ -1,120 +1,121
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) {
52 if (!dataSourceItem) {
53 qCWarning(LOG_DataSourceController())
53 qCWarning(LOG_DataSourceController())
54 << tr("Data source item can't be registered (null item)");
54 << tr("Data source item can't be registered (null item)");
55 return;
55 return;
56 }
56 }
57
57
58 if (impl->m_DataSources.contains(dataSourceUid)) {
58 if (impl->m_DataSources.contains(dataSourceUid)) {
59 // The data provider is implicitly converted to a shared_ptr
59 // The data provider is implicitly converted to a shared_ptr
60 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
60 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
61
61
62 // Retrieves the data source item to emit the signal with it
62 // Retrieves the data source item to emit the signal with it
63 auto it = impl->m_DataSourceItems.find(dataSourceUid);
63 auto it = impl->m_DataSourceItems.find(dataSourceUid);
64 if (it != impl->m_DataSourceItems.end()) {
64 if (it != impl->m_DataSourceItems.end()) {
65 emit dataSourceItemSet(it->second.get());
65 emit dataSourceItemSet(it->second.get());
66 }
66 }
67 }
67 }
68 else {
68 else {
69 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 "
70 "data source has been registered with the uid")
70 "data source has been registered with the uid")
71 .arg(dataSourceUid.toString());
71 .arg(dataSourceUid.toString());
72 }
72 }
73 }
73 }
74
74
75 void DataSourceController::setDataProvider(const QUuid &dataSourceUid,
75 void DataSourceController::setDataProvider(const QUuid &dataSourceUid,
76 std::unique_ptr<IDataProvider> dataProvider) noexcept
76 std::unique_ptr<IDataProvider> dataProvider) noexcept
77 {
77 {
78 if (impl->m_DataSources.contains(dataSourceUid)) {
78 if (impl->m_DataSources.contains(dataSourceUid)) {
79 impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider)));
79 impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider)));
80 }
80 }
81 else {
81 else {
82 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 "
83 "source has been registered with the uid")
83 "source has been registered with the uid")
84 .arg(dataSourceUid.toString());
84 .arg(dataSourceUid.toString());
85 }
85 }
86 }
86 }
87
87
88 void DataSourceController::loadProductItem(const QUuid &dataSourceUid,
88 void DataSourceController::loadProductItem(const QUuid &dataSourceUid,
89 const DataSourceItem &productItem) noexcept
89 const DataSourceItem &productItem) noexcept
90 {
90 {
91 if (productItem.type() == DataSourceItemType::PRODUCT) {
91 if (productItem.type() == DataSourceItemType::PRODUCT
92 || productItem.type() == DataSourceItemType::COMPONENT) {
92 /// Retrieves the data provider of the data source (if any)
93 /// Retrieves the data provider of the data source (if any)
93 auto it = impl->m_DataProviders.find(dataSourceUid);
94 auto it = impl->m_DataProviders.find(dataSourceUid);
94 auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr;
95 auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr;
95
96
96 /// @todo retrieve timerange, and pass it to the signal
97 /// @todo retrieve timerange, and pass it to the signal
97 emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider);
98 emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider);
98 }
99 }
99 else {
100 else {
100 qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product");
101 qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product");
101 }
102 }
102 }
103 }
103
104
104 void DataSourceController::initialize()
105 void DataSourceController::initialize()
105 {
106 {
106 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
107 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
107 << QThread::currentThread();
108 << QThread::currentThread();
108 impl->m_WorkingMutex.lock();
109 impl->m_WorkingMutex.lock();
109 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
110 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
110 }
111 }
111
112
112 void DataSourceController::finalize()
113 void DataSourceController::finalize()
113 {
114 {
114 impl->m_WorkingMutex.unlock();
115 impl->m_WorkingMutex.unlock();
115 }
116 }
116
117
117 void DataSourceController::waitForFinish()
118 void DataSourceController::waitForFinish()
118 {
119 {
119 QMutexLocker locker{&impl->m_WorkingMutex};
120 QMutexLocker locker{&impl->m_WorkingMutex};
120 }
121 }
@@ -1,68 +1,73
1 #include "AmdaPlugin.h"
1 #include "AmdaPlugin.h"
2 #include "AmdaParser.h"
2 #include "AmdaParser.h"
3 #include "AmdaProvider.h"
3 #include "AmdaProvider.h"
4
4
5 #include <DataSource/DataSourceController.h>
5 #include <DataSource/DataSourceController.h>
6 #include <DataSource/DataSourceItem.h>
6 #include <DataSource/DataSourceItem.h>
7 #include <DataSource/DataSourceItemAction.h>
7 #include <DataSource/DataSourceItemAction.h>
8
8
9 #include <SqpApplication.h>
9 #include <SqpApplication.h>
10
10
11 Q_LOGGING_CATEGORY(LOG_AmdaPlugin, "AmdaPlugin")
11 Q_LOGGING_CATEGORY(LOG_AmdaPlugin, "AmdaPlugin")
12
12
13 namespace {
13 namespace {
14
14
15 /// Name of the data source
15 /// Name of the data source
16 const auto DATA_SOURCE_NAME = QStringLiteral("AMDA");
16 const auto DATA_SOURCE_NAME = QStringLiteral("AMDA");
17
17
18 /// Path of the file used to generate the data source item for AMDA
18 /// Path of the file used to generate the data source item for AMDA
19 const auto JSON_FILE_PATH = QStringLiteral(":/samples/AmdaSample.json");
19 const auto JSON_FILE_PATH = QStringLiteral(":/samples/AmdaSample.json");
20
20
21 void associateActions(DataSourceItem &item, const QUuid &dataSourceUid)
21 void associateActions(DataSourceItem &item, const QUuid &dataSourceUid)
22 {
22 {
23 if (item.type() == DataSourceItemType::PRODUCT) {
23 auto addLoadAction = [&item, dataSourceUid](const QString &label) {
24 auto itemName = item.name();
24 item.addAction(
25
25 std::make_unique<DataSourceItemAction>(label, [dataSourceUid](DataSourceItem &item) {
26 item.addAction(std::make_unique<DataSourceItemAction>(
27 QObject::tr("Load %1 product").arg(itemName),
28 [itemName, dataSourceUid](DataSourceItem &item) {
29 if (auto app = sqpApp) {
26 if (auto app = sqpApp) {
30 app->dataSourceController().loadProductItem(dataSourceUid, item);
27 app->dataSourceController().loadProductItem(dataSourceUid, item);
31 }
28 }
32 }));
29 }));
30 };
31
32 const auto itemType = item.type();
33 if (itemType == DataSourceItemType::PRODUCT) {
34 addLoadAction(QObject::tr("Load %1 product").arg(item.name()));
35 }
36 else if (itemType == DataSourceItemType::COMPONENT) {
37 addLoadAction(QObject::tr("Load %1 component").arg(item.name()));
33 }
38 }
34
39
35 auto count = item.childCount();
40 auto count = item.childCount();
36 for (auto i = 0; i < count; ++i) {
41 for (auto i = 0; i < count; ++i) {
37 if (auto child = item.child(i)) {
42 if (auto child = item.child(i)) {
38 associateActions(*child, dataSourceUid);
43 associateActions(*child, dataSourceUid);
39 }
44 }
40 }
45 }
41 }
46 }
42
47
43 } // namespace
48 } // namespace
44
49
45 void AmdaPlugin::initialize()
50 void AmdaPlugin::initialize()
46 {
51 {
47 if (auto app = sqpApp) {
52 if (auto app = sqpApp) {
48 // Registers to the data source controller
53 // Registers to the data source controller
49 auto &dataSourceController = app->dataSourceController();
54 auto &dataSourceController = app->dataSourceController();
50 auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
55 auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
51
56
52 // Sets data source tree
57 // Sets data source tree
53 if (auto dataSourceItem = AmdaParser::readJson(JSON_FILE_PATH)) {
58 if (auto dataSourceItem = AmdaParser::readJson(JSON_FILE_PATH)) {
54 associateActions(*dataSourceItem, dataSourceUid);
59 associateActions(*dataSourceItem, dataSourceUid);
55
60
56 dataSourceController.setDataSourceItem(dataSourceUid, std::move(dataSourceItem));
61 dataSourceController.setDataSourceItem(dataSourceUid, std::move(dataSourceItem));
57 }
62 }
58 else {
63 else {
59 qCCritical(LOG_AmdaPlugin()) << tr("No data source item could be generated for AMDA");
64 qCCritical(LOG_AmdaPlugin()) << tr("No data source item could be generated for AMDA");
60 }
65 }
61
66
62 // Sets data provider
67 // Sets data provider
63 dataSourceController.setDataProvider(dataSourceUid, std::make_unique<AmdaProvider>());
68 dataSourceController.setDataProvider(dataSourceUid, std::make_unique<AmdaProvider>());
64 }
69 }
65 else {
70 else {
66 qCWarning(LOG_AmdaPlugin()) << tr("Can't access to SciQlop application");
71 qCWarning(LOG_AmdaPlugin()) << tr("Can't access to SciQlop application");
67 }
72 }
68 }
73 }
General Comments 0
You need to be logged in to leave comments. Login now