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