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