@@ -1,52 +1,59 | |||
|
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 | /** |
|
10 | * Possible types of an item | |
|
11 | */ | |
|
12 | enum class DataSourceItemType { NODE, PRODUCT }; | |
|
13 | ||
|
14 | /** | |
|
10 | 15 | * @brief The DataSourceItem class aims to represent a structure element of a data source. |
|
11 | 16 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) |
|
12 | 17 | * containing other DataSourceItem objects (children). |
|
13 | 18 | * For each DataSourceItem can be associated a set of data representing it. |
|
14 | 19 | */ |
|
15 | 20 | class DataSourceItem { |
|
16 | 21 | public: |
|
17 | explicit DataSourceItem(QVector<QVariant> data = {}); | |
|
22 | explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {}); | |
|
18 | 23 | |
|
19 | 24 | /** |
|
20 | 25 | * Adds a child to the item. The item takes ownership of the child. |
|
21 | 26 | * @param child the child to add |
|
22 | 27 | */ |
|
23 | 28 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
24 | 29 | |
|
25 | 30 | /** |
|
26 | 31 | * Returns the item's child associated to an index |
|
27 | 32 | * @param childIndex the index to search |
|
28 | 33 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
29 | 34 | */ |
|
30 | 35 | DataSourceItem *child(int childIndex) const noexcept; |
|
31 | 36 | |
|
32 | 37 | int childCount() const noexcept; |
|
33 | 38 | |
|
34 | 39 | /** |
|
35 | 40 | * Get the data associated to an index |
|
36 | 41 | * @param dataIndex the index to search |
|
37 | 42 | * @return the data found if index is valid, default QVariant otherwise |
|
38 | 43 | */ |
|
39 | 44 | QVariant data(int dataIndex) const noexcept; |
|
40 | 45 | |
|
41 | 46 | /** |
|
42 | 47 | * Get the item's parent |
|
43 | 48 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
44 | 49 | */ |
|
45 | 50 | DataSourceItem *parentItem() const noexcept; |
|
46 | 51 | |
|
52 | DataSourceItemType type() const noexcept; | |
|
53 | ||
|
47 | 54 | private: |
|
48 | 55 | class DataSourceItemPrivate; |
|
49 | 56 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
50 | 57 | }; |
|
51 | 58 | |
|
52 | 59 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,50 +1,56 | |||
|
1 | 1 | #include <DataSource/DataSourceItem.h> |
|
2 | 2 | |
|
3 | 3 | #include <QVector> |
|
4 | 4 | |
|
5 | 5 | struct DataSourceItem::DataSourceItemPrivate { |
|
6 | explicit DataSourceItemPrivate(QVector<QVariant> data) | |
|
7 | : m_Parent{nullptr}, m_Children{}, m_Data{std::move(data)} | |
|
6 | explicit DataSourceItemPrivate(DataSourceItemType type, QVector<QVariant> data) | |
|
7 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)} | |
|
8 | 8 | { |
|
9 | 9 | } |
|
10 | 10 | |
|
11 | 11 | DataSourceItem *m_Parent; |
|
12 | 12 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
13 | DataSourceItemType m_Type; | |
|
13 | 14 | QVector<QVariant> m_Data; |
|
14 | 15 | }; |
|
15 | 16 | |
|
16 | DataSourceItem::DataSourceItem(QVector<QVariant> data) | |
|
17 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(data)} | |
|
17 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVector<QVariant> data) | |
|
18 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} | |
|
18 | 19 | { |
|
19 | 20 | } |
|
20 | 21 | |
|
21 | 22 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
22 | 23 | { |
|
23 | 24 | child->impl->m_Parent = this; |
|
24 | 25 | impl->m_Children.push_back(std::move(child)); |
|
25 | 26 | } |
|
26 | 27 | |
|
27 | 28 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
28 | 29 | { |
|
29 | 30 | if (childIndex < 0 || childIndex >= childCount()) { |
|
30 | 31 | return nullptr; |
|
31 | 32 | } |
|
32 | 33 | else { |
|
33 | 34 | return impl->m_Children.at(childIndex).get(); |
|
34 | 35 | } |
|
35 | 36 | } |
|
36 | 37 | |
|
37 | 38 | int DataSourceItem::childCount() const noexcept |
|
38 | 39 | { |
|
39 | 40 | return impl->m_Children.size(); |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | QVariant DataSourceItem::data(int dataIndex) const noexcept |
|
43 | 44 | { |
|
44 | 45 | return impl->m_Data.value(dataIndex); |
|
45 | 46 | } |
|
46 | 47 | |
|
47 | 48 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
48 | 49 | { |
|
49 | 50 | return impl->m_Parent; |
|
50 | 51 | } |
|
52 | ||
|
53 | DataSourceItemType DataSourceItem::type() const noexcept | |
|
54 | { | |
|
55 | return impl->m_Type; | |
|
56 | } |
@@ -1,49 +1,51 | |||
|
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(const DataSourceItem &))}; |
|
30 | 30 | |
|
31 | 31 | // Create a data source item |
|
32 | 32 | auto source1Name = QStringLiteral("Source1"); |
|
33 | 33 | auto source1Values = QVector<QVariant>{source1Name}; |
|
34 | auto source1Item = std::make_unique<DataSourceItem>(std::move(source1Values)); | |
|
34 | auto source1Item | |
|
35 | = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, std::move(source1Values)); | |
|
35 | 36 | |
|
36 | 37 | // Add data source item to the controller and check that a signal has been emitted after setting |
|
37 | 38 | // data source item in the controller |
|
38 | 39 | auto source1Uid = dataSourceController.registerDataSource(source1Name); |
|
39 | 40 | dataSourceController.setDataSourceItem(source1Uid, std::move(source1Item)); |
|
40 | 41 | QCOMPARE(signalSpy.count(), 1); |
|
41 | 42 | |
|
42 | 43 | // Try to a data source item with an unregistered uid and check that no signal has been emitted |
|
43 | 44 | auto unregisteredUid = QUuid::createUuid(); |
|
44 |
dataSourceController.setDataSourceItem( |
|
|
45 | dataSourceController.setDataSourceItem( | |
|
46 | unregisteredUid, std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT)); | |
|
45 | 47 | QCOMPARE(signalSpy.count(), 1); |
|
46 | 48 | } |
|
47 | 49 | |
|
48 | 50 | QTEST_MAIN(TestDataSourceController) |
|
49 | 51 | #include "TestDataSourceController.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now