##// END OF EJS Templates
Updates references to DataSourceItem after changes
Alexandre Leroux -
r343:21cfeb46fc36
parent child
Show More
@@ -1,51 +1,49
1 #include <DataSource/DataSourceController.h>
1 #include <DataSource/DataSourceController.h>
2 #include <DataSource/DataSourceItem.h>
2 #include <DataSource/DataSourceItem.h>
3
3
4 #include <QObject>
4 #include <QObject>
5 #include <QtTest>
5 #include <QtTest>
6
6
7 #include <memory>
7 #include <memory>
8
8
9 class TestDataSourceController : public QObject {
9 class TestDataSourceController : public QObject {
10 Q_OBJECT
10 Q_OBJECT
11 private slots:
11 private slots:
12 void testRegisterDataSource();
12 void testRegisterDataSource();
13 void testSetDataSourceItem();
13 void testSetDataSourceItem();
14 };
14 };
15
15
16 void TestDataSourceController::testRegisterDataSource()
16 void TestDataSourceController::testRegisterDataSource()
17 {
17 {
18 DataSourceController dataSourceController{};
18 DataSourceController dataSourceController{};
19
19
20 auto uid = dataSourceController.registerDataSource(QStringLiteral("Source1"));
20 auto uid = dataSourceController.registerDataSource(QStringLiteral("Source1"));
21 QVERIFY(!uid.isNull());
21 QVERIFY(!uid.isNull());
22 }
22 }
23
23
24 void TestDataSourceController::testSetDataSourceItem()
24 void TestDataSourceController::testSetDataSourceItem()
25 {
25 {
26 DataSourceController dataSourceController{};
26 DataSourceController dataSourceController{};
27
27
28 // Spy to test controllers' signals
28 // Spy to test controllers' signals
29 QSignalSpy signalSpy{&dataSourceController, SIGNAL(dataSourceItemSet(DataSourceItem *))};
29 QSignalSpy signalSpy{&dataSourceController, SIGNAL(dataSourceItemSet(DataSourceItem *))};
30
30
31 // Create a data source item
31 // Create a data source item
32 auto source1Name = QStringLiteral("Source1");
32 auto source1Name = QStringLiteral("Source1");
33 auto source1Values = QVector<QVariant>{source1Name};
33 auto source1Item = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, source1Name);
34 auto source1Item
35 = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, std::move(source1Values));
36
34
37 // Add data source item to the controller and check that a signal has been emitted after setting
35 // Add data source item to the controller and check that a signal has been emitted after setting
38 // data source item in the controller
36 // data source item in the controller
39 auto source1Uid = dataSourceController.registerDataSource(source1Name);
37 auto source1Uid = dataSourceController.registerDataSource(source1Name);
40 dataSourceController.setDataSourceItem(source1Uid, std::move(source1Item));
38 dataSourceController.setDataSourceItem(source1Uid, std::move(source1Item));
41 QCOMPARE(signalSpy.count(), 1);
39 QCOMPARE(signalSpy.count(), 1);
42
40
43 // Try to a data source item with an unregistered uid and check that no signal has been emitted
41 // Try to a data source item with an unregistered uid and check that no signal has been emitted
44 auto unregisteredUid = QUuid::createUuid();
42 auto unregisteredUid = QUuid::createUuid();
45 dataSourceController.setDataSourceItem(
43 dataSourceController.setDataSourceItem(
46 unregisteredUid, std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT));
44 unregisteredUid, std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT));
47 QCOMPARE(signalSpy.count(), 1);
45 QCOMPARE(signalSpy.count(), 1);
48 }
46 }
49
47
50 QTEST_MAIN(TestDataSourceController)
48 QTEST_MAIN(TestDataSourceController)
51 #include "TestDataSourceController.moc"
49 #include "TestDataSourceController.moc"
@@ -1,101 +1,120
1 #include <DataSource/DataSourceItem.h>
1 #include <DataSource/DataSourceItem.h>
2 #include <DataSource/DataSourceItemAction.h>
2 #include <DataSource/DataSourceItemAction.h>
3 #include <DataSource/DataSourceTreeWidgetItem.h>
3 #include <DataSource/DataSourceTreeWidgetItem.h>
4
4
5 #include <SqpApplication.h>
5 #include <SqpApplication.h>
6
6
7 #include <QAction>
7 #include <QAction>
8
8
9 Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem")
9 Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem")
10
10
11 namespace {
11 namespace {
12
12
13 // Column indexes
14 const auto NAME_COLUMN = 0;
15
13 QIcon itemIcon(const DataSourceItem *dataSource)
16 QIcon itemIcon(const DataSourceItem *dataSource)
14 {
17 {
15 if (dataSource) {
18 if (dataSource) {
16 auto dataSourceType = dataSource->type();
19 auto dataSourceType = dataSource->type();
17 switch (dataSourceType) {
20 switch (dataSourceType) {
18 case DataSourceItemType::NODE:
21 case DataSourceItemType::NODE:
19 return sqpApp->style()->standardIcon(QStyle::SP_DirIcon);
22 return sqpApp->style()->standardIcon(QStyle::SP_DirIcon);
20 case DataSourceItemType::PRODUCT:
23 case DataSourceItemType::PRODUCT:
21 return sqpApp->style()->standardIcon(QStyle::SP_FileIcon);
24 return sqpApp->style()->standardIcon(QStyle::SP_FileIcon);
22 default:
25 default:
23 // No action
26 // No action
24 break;
27 break;
25 }
28 }
26
29
27 qCWarning(LOG_DataSourceTreeWidgetItem())
30 qCWarning(LOG_DataSourceTreeWidgetItem())
28 << QObject::tr("Can't set data source icon : unknown data source type");
31 << QObject::tr("Can't set data source icon : unknown data source type");
29 }
32 }
30 else {
33 else {
31 qCWarning(LOG_DataSourceTreeWidgetItem())
34 qCWarning(LOG_DataSourceTreeWidgetItem())
32 << QObject::tr("Can't set data source icon : the data source is null");
35 << QObject::tr("Can't set data source icon : the data source is null");
33 }
36 }
34
37
35 // Default cases
38 // Default cases
36 return QIcon{};
39 return QIcon{};
37 }
40 }
38
41
39 } // namespace
42 } // namespace
40
43
41 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
44 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
42 explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
45 explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
43
46
44 /// Model used to retrieve data source information
47 /// Model used to retrieve data source information
45 const DataSourceItem *m_Data;
48 const DataSourceItem *m_Data;
46 /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
49 /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
47 /// the actions
50 /// the actions
48 QList<QAction *> m_Actions;
51 QList<QAction *> m_Actions;
49 };
52 };
50
53
51 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type)
54 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type)
52 : DataSourceTreeWidgetItem{nullptr, data, type}
55 : DataSourceTreeWidgetItem{nullptr, data, type}
53 {
56 {
54 }
57 }
55
58
56 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
59 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
57 int type)
60 int type)
58 : QTreeWidgetItem{parent, type},
61 : QTreeWidgetItem{parent, type},
59 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
62 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
60 {
63 {
61 // Sets the icon depending on the data source
64 // Sets the icon depending on the data source
62 setIcon(0, itemIcon(impl->m_Data));
65 setIcon(0, itemIcon(impl->m_Data));
63
66
64 // Generates tree actions based on the item actions
67 // Generates tree actions based on the item actions
65 auto createTreeAction = [this, &parent](const auto &itemAction) {
68 auto createTreeAction = [this, &parent](const auto &itemAction) {
66 auto treeAction = new QAction{itemAction->name(), parent};
69 auto treeAction = new QAction{itemAction->name(), parent};
67
70
68 // Executes item action when tree action is triggered
71 // Executes item action when tree action is triggered
69 QObject::connect(treeAction, &QAction::triggered, itemAction,
72 QObject::connect(treeAction, &QAction::triggered, itemAction,
70 &DataSourceItemAction::execute);
73 &DataSourceItemAction::execute);
71
74
72 return treeAction;
75 return treeAction;
73 };
76 };
74
77
75 auto itemActions = impl->m_Data->actions();
78 auto itemActions = impl->m_Data->actions();
76 std::transform(std::cbegin(itemActions), std::cend(itemActions),
79 std::transform(std::cbegin(itemActions), std::cend(itemActions),
77 std::back_inserter(impl->m_Actions), createTreeAction);
80 std::back_inserter(impl->m_Actions), createTreeAction);
78 }
81 }
79
82
80 QVariant DataSourceTreeWidgetItem::data(int column, int role) const
83 QVariant DataSourceTreeWidgetItem::data(int column, int role) const
81 {
84 {
82 if (role == Qt::DisplayRole) {
85 if (role == Qt::DisplayRole) {
83 return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{};
86 if (impl->m_Data) {
87 switch (column) {
88 case NAME_COLUMN:
89 return impl->m_Data->name();
90 default:
91 // No action
92 break;
93 }
94
95 qCWarning(LOG_DataSourceTreeWidgetItem())
96 << QObject::tr("Can't get data (unknown column %1)").arg(column);
97 }
98 else {
99 qCCritical(LOG_DataSourceTreeWidgetItem()) << QObject::tr("Can't get data (null item)");
100 }
101
102 return QVariant{};
84 }
103 }
85 else {
104 else {
86 return QTreeWidgetItem::data(column, role);
105 return QTreeWidgetItem::data(column, role);
87 }
106 }
88 }
107 }
89
108
90 void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value)
109 void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value)
91 {
110 {
92 // Data can't be changed by edition
111 // Data can't be changed by edition
93 if (role != Qt::EditRole) {
112 if (role != Qt::EditRole) {
94 QTreeWidgetItem::setData(column, role, value);
113 QTreeWidgetItem::setData(column, role, value);
95 }
114 }
96 }
115 }
97
116
98 QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
117 QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
99 {
118 {
100 return impl->m_Actions;
119 return impl->m_Actions;
101 }
120 }
@@ -1,81 +1,79
1 #include "MockPlugin.h"
1 #include "MockPlugin.h"
2 #include "CosinusProvider.h"
2 #include "CosinusProvider.h"
3
3
4 #include <DataSource/DataSourceController.h>
4 #include <DataSource/DataSourceController.h>
5 #include <DataSource/DataSourceItem.h>
5 #include <DataSource/DataSourceItem.h>
6 #include <DataSource/DataSourceItemAction.h>
6 #include <DataSource/DataSourceItemAction.h>
7
7
8 #include <SqpApplication.h>
8 #include <SqpApplication.h>
9
9
10 Q_LOGGING_CATEGORY(LOG_MockPlugin, "MockPlugin")
10 Q_LOGGING_CATEGORY(LOG_MockPlugin, "MockPlugin")
11
11
12 namespace {
12 namespace {
13
13
14 /// Name of the data source
14 /// Name of the data source
15 const auto DATA_SOURCE_NAME = QStringLiteral("MMS");
15 const auto DATA_SOURCE_NAME = QStringLiteral("MMS");
16
16
17 /// Creates the data provider relative to the plugin
17 /// Creates the data provider relative to the plugin
18 std::unique_ptr<IDataProvider> createDataProvider() noexcept
18 std::unique_ptr<IDataProvider> createDataProvider() noexcept
19 {
19 {
20 return std::make_unique<CosinusProvider>();
20 return std::make_unique<CosinusProvider>();
21 }
21 }
22
22
23 std::unique_ptr<DataSourceItem> createProductItem(const QString &productName,
23 std::unique_ptr<DataSourceItem> createProductItem(const QString &productName,
24 const QUuid &dataSourceUid)
24 const QUuid &dataSourceUid)
25 {
25 {
26 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT,
26 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, productName);
27 QVector<QVariant>{productName});
28
27
29 // Add action to load product from DataSourceController
28 // Add action to load product from DataSourceController
30 result->addAction(std::make_unique<DataSourceItemAction>(
29 result->addAction(std::make_unique<DataSourceItemAction>(
31 QObject::tr("Load %1 product").arg(productName),
30 QObject::tr("Load %1 product").arg(productName),
32 [productName, dataSourceUid](DataSourceItem &item) {
31 [productName, dataSourceUid](DataSourceItem &item) {
33 if (auto app = sqpApp) {
32 if (auto app = sqpApp) {
34 app->dataSourceController().loadProductItem(dataSourceUid, item);
33 app->dataSourceController().loadProductItem(dataSourceUid, item);
35 }
34 }
36 }));
35 }));
37
36
38 return result;
37 return result;
39 }
38 }
40
39
41 /// Creates the data source item relative to the plugin
40 /// Creates the data source item relative to the plugin
42 std::unique_ptr<DataSourceItem> createDataSourceItem(const QUuid &dataSourceUid) noexcept
41 std::unique_ptr<DataSourceItem> createDataSourceItem(const QUuid &dataSourceUid) noexcept
43 {
42 {
44 // Magnetic field products
43 // Magnetic field products
45 auto magneticFieldFolder = std::make_unique<DataSourceItem>(
44 auto magneticFieldFolder = std::make_unique<DataSourceItem>(DataSourceItemType::NODE,
46 DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Magnetic field")});
45 QStringLiteral("Magnetic field"));
47 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM"), dataSourceUid));
46 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM"), dataSourceUid));
48 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC"), dataSourceUid));
47 magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC"), dataSourceUid));
49
48
50 // Electric field products
49 // Electric field products
51 auto electricFieldFolder = std::make_unique<DataSourceItem>(
50 auto electricFieldFolder = std::make_unique<DataSourceItem>(DataSourceItemType::NODE,
52 DataSourceItemType::NODE, QVector<QVariant>{QStringLiteral("Electric field")});
51 QStringLiteral("Electric field"));
53
52
54 // Root
53 // Root
55 auto root = std::make_unique<DataSourceItem>(DataSourceItemType::NODE,
54 auto root = std::make_unique<DataSourceItem>(DataSourceItemType::NODE, DATA_SOURCE_NAME);
56 QVector<QVariant>{DATA_SOURCE_NAME});
57 root->appendChild(std::move(magneticFieldFolder));
55 root->appendChild(std::move(magneticFieldFolder));
58 root->appendChild(std::move(electricFieldFolder));
56 root->appendChild(std::move(electricFieldFolder));
59
57
60 return root;
58 return root;
61 }
59 }
62
60
63 } // namespace
61 } // namespace
64
62
65 void MockPlugin::initialize()
63 void MockPlugin::initialize()
66 {
64 {
67 if (auto app = sqpApp) {
65 if (auto app = sqpApp) {
68 // Registers to the data source controller
66 // Registers to the data source controller
69 auto &dataSourceController = app->dataSourceController();
67 auto &dataSourceController = app->dataSourceController();
70 auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
68 auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
71
69
72 // Sets data source tree
70 // Sets data source tree
73 dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem(dataSourceUid));
71 dataSourceController.setDataSourceItem(dataSourceUid, createDataSourceItem(dataSourceUid));
74
72
75 // Sets data provider
73 // Sets data provider
76 dataSourceController.setDataProvider(dataSourceUid, createDataProvider());
74 dataSourceController.setDataProvider(dataSourceUid, createDataProvider());
77 }
75 }
78 else {
76 else {
79 qCWarning(LOG_MockPlugin()) << tr("Can't access to SciQlop application");
77 qCWarning(LOG_MockPlugin()) << tr("Can't access to SciQlop application");
80 }
78 }
81 }
79 }
General Comments 0
You need to be logged in to leave comments. Login now