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