##// END OF EJS Templates
Adds action in the mock plugin to load products
Alexandre Leroux -
r146:f61d116e8daa
parent child
Show More
@@ -1,73 +1,79
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 40 * @param dataSourceItem the structure of the data source
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 /**
58 * Loads an item (product) as a variable in SciQlop
59 * @param productItem the item to load
60 */
61 void loadProductItem(const DataSourceItem &productItem) noexcept;
62
57 63 public slots:
58 64 /// Manage init/end of the controller
59 65 void initialize();
60 66 void finalize();
61 67
62 68 signals:
63 69 /// Signal emitted when a structure has been set for a data source
64 70 void dataSourceItemSet(DataSourceItem *dataSourceItem);
65 71
66 72 private:
67 73 void waitForFinish();
68 74
69 75 class DataSourceControllerPrivate;
70 76 spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl;
71 77 };
72 78
73 79 #endif // SCIQLOP_DATASOURCECONTROLLER_H
@@ -1,71 +1,73
1 1 #ifndef SCIQLOP_DATASOURCEITEM_H
2 2 #define SCIQLOP_DATASOURCEITEM_H
3 3
4 4 #include <Common/spimpl.h>
5 5
6 6 #include <QVariant>
7 7 #include <QVector>
8 8
9 9 class DataSourceItemAction;
10 10
11 11 /**
12 12 * Possible types of an item
13 13 */
14 14 enum class DataSourceItemType { NODE, PRODUCT };
15 15
16 16 /**
17 17 * @brief The DataSourceItem class aims to represent a structure element of a data source.
18 18 * A data source has a tree structure that is made up of a main DataSourceItem object (root)
19 19 * containing other DataSourceItem objects (children).
20 20 * For each DataSourceItem can be associated a set of data representing it.
21 21 */
22 22 class DataSourceItem {
23 23 public:
24 24 explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {});
25 25
26 26 /// @return the actions of the item as a vector
27 27 QVector<DataSourceItemAction *> actions() const noexcept;
28 28
29 29 /**
30 30 * Adds an action to the item. The item takes ownership of the action, and the action is
31 31 * automatically associated to the item
32 32 * @param action the action to add
33 33 */
34 34 void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept;
35 35
36 36 /**
37 37 * Adds a child to the item. The item takes ownership of the child.
38 38 * @param child the child to add
39 39 */
40 40 void appendChild(std::unique_ptr<DataSourceItem> child) noexcept;
41 41
42 42 /**
43 43 * Returns the item's child associated to an index
44 44 * @param childIndex the index to search
45 45 * @return a pointer to the child if index is valid, nullptr otherwise
46 46 */
47 47 DataSourceItem *child(int childIndex) const noexcept;
48 48
49 49 int childCount() const noexcept;
50 50
51 51 /**
52 52 * Get the data associated to an index
53 53 * @param dataIndex the index to search
54 54 * @return the data found if index is valid, default QVariant otherwise
55 55 */
56 56 QVariant data(int dataIndex) const noexcept;
57 57
58 QString name() const noexcept;
59
58 60 /**
59 61 * Get the item's parent
60 62 * @return a pointer to the parent if it exists, nullptr if the item is a root
61 63 */
62 64 DataSourceItem *parentItem() const noexcept;
63 65
64 66 DataSourceItemType type() const noexcept;
65 67
66 68 private:
67 69 class DataSourceItemPrivate;
68 70 spimpl::unique_impl_ptr<DataSourceItemPrivate> impl;
69 71 };
70 72
71 73 #endif // SCIQLOP_DATASOURCEITEMMODEL_H
@@ -1,95 +1,100
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 std::map<QUuid, std::unique_ptr<IDataProvider> > m_DataProviders;
23 23 };
24 24
25 25 DataSourceController::DataSourceController(QObject *parent)
26 26 : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()}
27 27 {
28 28 qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction")
29 29 << QThread::currentThread();
30 30 }
31 31
32 32 DataSourceController::~DataSourceController()
33 33 {
34 34 qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction")
35 35 << QThread::currentThread();
36 36 this->waitForFinish();
37 37 }
38 38
39 39 QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept
40 40 {
41 41 auto dataSourceUid = QUuid::createUuid();
42 42 impl->m_DataSources.insert(dataSourceUid, dataSourceName);
43 43
44 44 return dataSourceUid;
45 45 }
46 46
47 47 void DataSourceController::setDataSourceItem(
48 48 const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept
49 49 {
50 50 if (impl->m_DataSources.contains(dataSourceUid)) {
51 51 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
52 52
53 53 // Retrieves the data source item to emit the signal with it
54 54 auto it = impl->m_DataSourceItems.find(dataSourceUid);
55 55 if (it != impl->m_DataSourceItems.end()) {
56 56 emit dataSourceItemSet(it->second.get());
57 57 }
58 58 }
59 59 else {
60 60 qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no "
61 61 "data source has been registered with the uid")
62 62 .arg(dataSourceUid.toString());
63 63 }
64 64 }
65 65
66 66 void DataSourceController::setDataProvider(const QUuid &dataSourceUid,
67 67 std::unique_ptr<IDataProvider> dataProvider) noexcept
68 68 {
69 69 if (impl->m_DataSources.contains(dataSourceUid)) {
70 70 impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider)));
71 71 }
72 72 else {
73 73 qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data "
74 74 "source has been registered with the uid")
75 75 .arg(dataSourceUid.toString());
76 76 }
77 77 }
78 78
79 void DataSourceController::loadProductItem(const DataSourceItem &productItem) noexcept
80 {
81 /// @todo ALX
82 }
83
79 84 void DataSourceController::initialize()
80 85 {
81 86 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
82 87 << QThread::currentThread();
83 88 impl->m_WorkingMutex.lock();
84 89 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
85 90 }
86 91
87 92 void DataSourceController::finalize()
88 93 {
89 94 impl->m_WorkingMutex.unlock();
90 95 }
91 96
92 97 void DataSourceController::waitForFinish()
93 98 {
94 99 QMutexLocker locker{&impl->m_WorkingMutex};
95 100 }
@@ -1,74 +1,86
1 1 #include <DataSource/DataSourceItem.h>
2 2 #include <DataSource/DataSourceItemAction.h>
3 3
4 4 #include <QVector>
5 5
6 namespace {
7
8 /// Index of the 'name' value in the item
9 const auto NAME_INDEX = 0;
10
11 } // namespace
12
6 13 struct DataSourceItem::DataSourceItemPrivate {
7 14 explicit DataSourceItemPrivate(DataSourceItemType type, QVector<QVariant> data)
8 15 : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{}
9 16 {
10 17 }
11 18
12 19 DataSourceItem *m_Parent;
13 20 std::vector<std::unique_ptr<DataSourceItem> > m_Children;
14 21 DataSourceItemType m_Type;
15 22 QVector<QVariant> m_Data;
16 23 std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions;
17 24 };
18 25
19 26 DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data)
20 27 : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))}
21 28 {
22 29 }
23 30
24 31 QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept
25 32 {
26 33 QVector<DataSourceItemAction *> result{};
27 34
28 35 std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions),
29 36 std::back_inserter(result), [](const auto &action) { return action.get(); });
30 37
31 38 return result;
32 39 }
33 40
34 41 void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept
35 42 {
36 43 action->setDataSourceItem(this);
37 44 impl->m_Actions.push_back(std::move(action));
38 45 }
39 46
40 47 void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept
41 48 {
42 49 child->impl->m_Parent = this;
43 50 impl->m_Children.push_back(std::move(child));
44 51 }
45 52
46 53 DataSourceItem *DataSourceItem::child(int childIndex) const noexcept
47 54 {
48 55 if (childIndex < 0 || childIndex >= childCount()) {
49 56 return nullptr;
50 57 }
51 58 else {
52 59 return impl->m_Children.at(childIndex).get();
53 60 }
54 61 }
55 62
56 63 int DataSourceItem::childCount() const noexcept
57 64 {
58 65 return impl->m_Children.size();
59 66 }
60 67
61 68 QVariant DataSourceItem::data(int dataIndex) const noexcept
62 69 {
63 70 return impl->m_Data.value(dataIndex);
64 71 }
65 72
73 QString DataSourceItem::name() const noexcept
74 {
75 return data(NAME_INDEX).toString();
76 }
77
66 78 DataSourceItem *DataSourceItem::parentItem() const noexcept
67 79 {
68 80 return impl->m_Parent;
69 81 }
70 82
71 83 DataSourceItemType DataSourceItem::type() const noexcept
72 84 {
73 85 return impl->m_Type;
74 86 }
@@ -1,67 +1,79
1 1 #include "MockPlugin.h"
2 2 #include "CosinusProvider.h"
3 3
4 4 #include <DataSource/DataSourceController.h>
5 5 #include <DataSource/DataSourceItem.h>
6 #include <DataSource/DataSourceItemAction.h>
6 7
7 8 #include <SqpApplication.h>
8 9
9 10 Q_LOGGING_CATEGORY(LOG_MockPlugin, "MockPlugin")
10 11
11 12 namespace {
12 13
13 14 /// Name of the data source
14 15 const auto DATA_SOURCE_NAME = QStringLiteral("MMS");
15 16
16 17 /// Creates the data provider relative to the plugin
17 18 std::unique_ptr<IDataProvider> createDataProvider() noexcept
18 19 {
19 20 return std::make_unique<CosinusProvider>();
20 21 }
21 22
23 std::unique_ptr<DataSourceItem> createProductItem(const QString &productName)
24 {
25 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
26 QVector<QVariant>{productName});
27
28 // Add action to load product from DataSourceController
29 result->addAction(std::make_unique<DataSourceItemAction>(
30 QObject::tr("Load %1 product").arg(productName), [productName](DataSourceItem &item) {
31 if (auto app = sqpApp) {
32 app->dataSourceController().loadProductItem(item);
33 }
34 }));
35
36 return result;
37 }
38
22 39 /// Creates the data source item relative to the plugin
23 40 std::unique_ptr<DataSourceItem> createDataSourceItem() noexcept
24 41 {
25 42 // Magnetic field products
26 auto fgmProduct = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
27 QVector<QVariant>{QStringLiteral("FGM")});
28 auto scProduct = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
29 QVector<QVariant>{QStringLiteral("SC")});
30
31 43 auto magneticFieldFolder = std::make_unique<DataSourceItem>(
32 44 DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Magnetic field")});
33 magneticFieldFolder->appendChild(std::move(fgmProduct));
34 magneticFieldFolder->appendChild(std::move(scProduct));
45 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM")));
46 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC")));
35 47
36 48 // Electric field products
37 49 auto electricFieldFolder = std::make_unique<DataSourceItem>(
38 50 DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Electric field")});
39 51
40 52 // Root
41 53 auto root = std::make_unique<DataSourceItem>(DataSourceItemType::NODE,
42 54 QVector<QVariant>{DATA_SOURCE_NAME});
43 55 root->appendChild(std::move(magneticFieldFolder));
44 56 root->appendChild(std::move(electricFieldFolder));
45 57
46 58 return root;
47 59 }
48 60
49 61 } // namespace
50 62
51 63 void MockPlugin::initialize()
52 64 {
53 65 if (auto app = sqpApp) {
54 66 // Registers to the data source controller
55 67 auto &dataSourceController = app->dataSourceController();
56 68 auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
57 69
58 70 // Sets data source tree
59 71 dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem());
60 72
61 73 // Sets data provider
62 74 dataSourceController.setDataProvider(dataSourceUid, createDataProvider());
63 75 }
64 76 else {
65 77 qCWarning(LOG_MockPlugin()) << tr("Can't access to SciQlop application");
66 78 }
67 79 }
General Comments 0
You need to be logged in to leave comments. Login now