|
1 | NO CONTENT: new file 100644, binary diff hidden |
|
1 | NO CONTENT: new file 100644, binary diff hidden |
|
1 | NO CONTENT: new file 100644, binary diff hidden |
|
1 | NO CONTENT: new file 100644, binary diff hidden |
@@ -1,73 +1,94 | |||
|
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 | enum class DataSourceItemType { NODE, PRODUCT }; | |
|
14 | enum class DataSourceItemType { NODE, PRODUCT, COMPONENT }; | |
|
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 | explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {}); | |
|
24 | /// Key associated with the name of the item | |
|
25 | static const QString NAME_DATA_KEY; | |
|
26 | ||
|
27 | explicit DataSourceItem(DataSourceItemType type, const QString &name); | |
|
28 | explicit DataSourceItem(DataSourceItemType type, QHash<QString, QVariant> data = {}); | |
|
25 | 29 | |
|
26 | 30 | /// @return the actions of the item as a vector |
|
27 | 31 | QVector<DataSourceItemAction *> actions() const noexcept; |
|
28 | 32 | |
|
29 | 33 | /** |
|
30 | 34 | * Adds an action to the item. The item takes ownership of the action, and the action is |
|
31 | 35 | * automatically associated to the item |
|
32 | 36 | * @param action the action to add |
|
33 | 37 | */ |
|
34 | 38 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; |
|
35 | 39 | |
|
36 | 40 | /** |
|
37 | 41 | * Adds a child to the item. The item takes ownership of the child. |
|
38 | 42 | * @param child the child to add |
|
39 | 43 | */ |
|
40 | 44 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
41 | 45 | |
|
42 | 46 | /** |
|
43 | 47 | * Returns the item's child associated to an index |
|
44 | 48 | * @param childIndex the index to search |
|
45 | 49 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
46 | 50 | */ |
|
47 | 51 | DataSourceItem *child(int childIndex) const noexcept; |
|
48 | 52 | |
|
49 | 53 | int childCount() const noexcept; |
|
50 | 54 | |
|
51 | 55 | /** |
|
52 |
* Get the data associated to a |
|
|
53 |
* @param |
|
|
54 |
* @return the data found if |
|
|
56 | * Get the data associated to a key | |
|
57 | * @param key the key to search | |
|
58 | * @return the data found if key is valid, default QVariant otherwise | |
|
55 | 59 | */ |
|
56 |
QVariant data( |
|
|
60 | QVariant data(const QString &key) const noexcept; | |
|
61 | ||
|
62 | /// Gets all data | |
|
63 | const QHash<QString, QVariant> &data() const noexcept; | |
|
64 | ||
|
65 | bool isRoot() const noexcept; | |
|
57 | 66 | |
|
58 | 67 | QString name() const noexcept; |
|
59 | 68 | |
|
60 | 69 | /** |
|
61 | 70 | * Get the item's parent |
|
62 | 71 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
63 | 72 | */ |
|
64 | 73 | DataSourceItem *parentItem() const noexcept; |
|
65 | 74 | |
|
75 | /** | |
|
76 | * Sets or appends a value to a key | |
|
77 | * @param key the key | |
|
78 | * @param value the value | |
|
79 | * @param append if true, the value is added to the values already existing for the key, | |
|
80 | * otherwise it replaces the existing values | |
|
81 | */ | |
|
82 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; | |
|
83 | ||
|
66 | 84 | DataSourceItemType type() const noexcept; |
|
67 | 85 | |
|
86 | bool operator==(const DataSourceItem &other); | |
|
87 | bool operator!=(const DataSourceItem &other); | |
|
88 | ||
|
68 | 89 | private: |
|
69 | 90 | class DataSourceItemPrivate; |
|
70 | 91 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
71 | 92 | }; |
|
72 | 93 | |
|
73 | 94 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,86 +1,140 | |||
|
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 | |
|
6 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); | |
|
12 | 7 | |
|
13 | 8 | struct DataSourceItem::DataSourceItemPrivate { |
|
14 |
explicit DataSourceItemPrivate(DataSourceItemType type, Q |
|
|
9 | explicit DataSourceItemPrivate(DataSourceItemType type, QHash<QString, QVariant> data) | |
|
15 | 10 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} |
|
16 | 11 | { |
|
17 | 12 | } |
|
18 | 13 | |
|
19 | 14 | DataSourceItem *m_Parent; |
|
20 | 15 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
21 | 16 | DataSourceItemType m_Type; |
|
22 |
Q |
|
|
17 | QHash<QString, QVariant> m_Data; | |
|
23 | 18 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; |
|
24 | 19 | }; |
|
25 | 20 | |
|
26 |
DataSourceItem::DataSourceItem(DataSourceItemType type, |
|
|
21 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) | |
|
22 | : DataSourceItem{type, QHash<QString, QVariant>{{NAME_DATA_KEY, name}}} | |
|
23 | { | |
|
24 | } | |
|
25 | ||
|
26 | DataSourceItem::DataSourceItem(DataSourceItemType type, QHash<QString, QVariant> data) | |
|
27 | 27 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} |
|
28 | 28 | { |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept |
|
32 | 32 | { |
|
33 | 33 | auto result = QVector<DataSourceItemAction *>{}; |
|
34 | 34 | |
|
35 | 35 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), |
|
36 | 36 | std::back_inserter(result), [](const auto &action) { return action.get(); }); |
|
37 | 37 | |
|
38 | 38 | return result; |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept |
|
42 | 42 | { |
|
43 | 43 | action->setDataSourceItem(this); |
|
44 | 44 | impl->m_Actions.push_back(std::move(action)); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
48 | 48 | { |
|
49 | 49 | child->impl->m_Parent = this; |
|
50 | 50 | impl->m_Children.push_back(std::move(child)); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
54 | 54 | { |
|
55 | 55 | if (childIndex < 0 || childIndex >= childCount()) { |
|
56 | 56 | return nullptr; |
|
57 | 57 | } |
|
58 | 58 | else { |
|
59 | 59 | return impl->m_Children.at(childIndex).get(); |
|
60 | 60 | } |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | int DataSourceItem::childCount() const noexcept |
|
64 | 64 | { |
|
65 | 65 | return impl->m_Children.size(); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 |
QVariant DataSourceItem::data( |
|
|
68 | QVariant DataSourceItem::data(const QString &key) const noexcept | |
|
69 | 69 | { |
|
70 |
return impl->m_Data.value( |
|
|
70 | return impl->m_Data.value(key); | |
|
71 | } | |
|
72 | ||
|
73 | const QHash<QString, QVariant> &DataSourceItem::data() const noexcept | |
|
74 | { | |
|
75 | return impl->m_Data; | |
|
76 | } | |
|
77 | ||
|
78 | bool DataSourceItem::isRoot() const noexcept | |
|
79 | { | |
|
80 | return impl->m_Parent == nullptr; | |
|
71 | 81 | } |
|
72 | 82 | |
|
73 | 83 | QString DataSourceItem::name() const noexcept |
|
74 | 84 | { |
|
75 |
return data(NAME_ |
|
|
85 | return data(NAME_DATA_KEY).toString(); | |
|
76 | 86 | } |
|
77 | 87 | |
|
78 | 88 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
79 | 89 | { |
|
80 | 90 | return impl->m_Parent; |
|
81 | 91 | } |
|
82 | 92 | |
|
93 | void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept | |
|
94 | { | |
|
95 | auto it = impl->m_Data.constFind(key); | |
|
96 | if (append && it != impl->m_Data.constEnd()) { | |
|
97 | // Case of an existing value to which we want to add to the new value | |
|
98 | if (it->canConvert<QVariantList>()) { | |
|
99 | auto variantList = it->value<QVariantList>(); | |
|
100 | variantList.append(value); | |
|
101 | ||
|
102 | impl->m_Data.insert(key, variantList); | |
|
103 | } | |
|
104 | else { | |
|
105 | impl->m_Data.insert(key, QVariantList{*it, value}); | |
|
106 | } | |
|
107 | } | |
|
108 | else { | |
|
109 | // Other cases : | |
|
110 | // - new value in map OR | |
|
111 | // - replacement of an existing value (not appending) | |
|
112 | impl->m_Data.insert(key, value); | |
|
113 | } | |
|
114 | } | |
|
115 | ||
|
83 | 116 | DataSourceItemType DataSourceItem::type() const noexcept |
|
84 | 117 | { |
|
85 | 118 | return impl->m_Type; |
|
86 | 119 | } |
|
120 | ||
|
121 | bool DataSourceItem::operator==(const DataSourceItem &other) | |
|
122 | { | |
|
123 | // Compares items' attributes | |
|
124 | if (std::tie(impl->m_Type, impl->m_Data) == std::tie(other.impl->m_Type, other.impl->m_Data)) { | |
|
125 | // Compares contents of items' children | |
|
126 | return std::equal(std::cbegin(impl->m_Children), std::cend(impl->m_Children), | |
|
127 | std::cbegin(other.impl->m_Children), | |
|
128 | [](const auto &itemChild, const auto &otherChild) { | |
|
129 | return *itemChild == *otherChild; | |
|
130 | }); | |
|
131 | } | |
|
132 | else { | |
|
133 | return false; | |
|
134 | } | |
|
135 | } | |
|
136 | ||
|
137 | bool DataSourceItem::operator!=(const DataSourceItem &other) | |
|
138 | { | |
|
139 | return !(*this == other); | |
|
140 | } |
@@ -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,10 +1,14 | |||
|
1 | 1 | <RCC> |
|
2 | 2 | <qresource prefix="/"> |
|
3 | <file>icones/dataSourceComponent.png</file> | |
|
4 | <file>icones/dataSourceNode.png</file> | |
|
5 | <file>icones/dataSourceProduct.png</file> | |
|
6 | <file>icones/dataSourceRoot.png</file> | |
|
3 | 7 | <file>icones/delete.png</file> |
|
4 | 8 | <file>icones/openInspector.png</file> |
|
5 | 9 | <file>icones/next.png</file> |
|
6 | 10 | <file>icones/plot.png</file> |
|
7 | 11 | <file>icones/previous.png</file> |
|
8 | 12 | <file>icones/unplot.png</file> |
|
9 | 13 | </qresource> |
|
10 | 14 | </RCC> |
@@ -1,101 +1,171 | |||
|
1 | 1 | #include <DataSource/DataSourceItem.h> |
|
2 | 2 | #include <DataSource/DataSourceItemAction.h> |
|
3 | 3 | #include <DataSource/DataSourceTreeWidgetItem.h> |
|
4 | 4 | |
|
5 | #include <SqpApplication.h> | |
|
6 | ||
|
7 | 5 | #include <QAction> |
|
8 | 6 | |
|
9 | 7 | Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem") |
|
10 | 8 | |
|
11 | 9 | namespace { |
|
12 | 10 | |
|
11 | // Column indexes | |
|
12 | const auto NAME_COLUMN = 0; | |
|
13 | ||
|
13 | 14 | QIcon itemIcon(const DataSourceItem *dataSource) |
|
14 | 15 | { |
|
15 | 16 | if (dataSource) { |
|
16 | 17 | auto dataSourceType = dataSource->type(); |
|
17 | 18 | switch (dataSourceType) { |
|
18 | case DataSourceItemType::NODE: | |
|
19 | return sqpApp->style()->standardIcon(QStyle::SP_DirIcon); | |
|
19 | case DataSourceItemType::NODE: { | |
|
20 | return dataSource->isRoot() ? QIcon{":/icones/dataSourceRoot.png"} | |
|
21 | : QIcon{":/icones/dataSourceNode.png"}; | |
|
22 | } | |
|
20 | 23 | case DataSourceItemType::PRODUCT: |
|
21 | return sqpApp->style()->standardIcon(QStyle::SP_FileIcon); | |
|
24 | return QIcon{":/icones/dataSourceProduct.png"}; | |
|
25 | case DataSourceItemType::COMPONENT: | |
|
26 | return QIcon{":/icones/dataSourceComponent.png"}; | |
|
22 | 27 | default: |
|
23 | 28 | // No action |
|
24 | 29 | break; |
|
25 | 30 | } |
|
26 | 31 | |
|
27 | 32 | qCWarning(LOG_DataSourceTreeWidgetItem()) |
|
28 | 33 | << QObject::tr("Can't set data source icon : unknown data source type"); |
|
29 | 34 | } |
|
30 | 35 | else { |
|
31 |
qC |
|
|
36 | qCCritical(LOG_DataSourceTreeWidgetItem()) | |
|
32 | 37 | << QObject::tr("Can't set data source icon : the data source is null"); |
|
33 | 38 | } |
|
34 | 39 | |
|
35 | 40 | // Default cases |
|
36 | 41 | return QIcon{}; |
|
37 | 42 | } |
|
38 | 43 | |
|
44 | /// @return the tooltip text for a variant. The text depends on whether the data is a simple variant | |
|
45 | /// or a list of variants | |
|
46 | QString tooltipValue(const QVariant &variant) noexcept | |
|
47 | { | |
|
48 | // If the variant is a list of variants, the text of the tooltip is of the form: {val1, val2, | |
|
49 | // ...} | |
|
50 | if (variant.canConvert<QVariantList>()) { | |
|
51 | auto valueString = QStringLiteral("{"); | |
|
52 | ||
|
53 | auto variantList = variant.value<QVariantList>(); | |
|
54 | for (auto it = variantList.cbegin(), end = variantList.cend(); it != end; ++it) { | |
|
55 | valueString.append(it->toString()); | |
|
56 | ||
|
57 | if (std::distance(it, end) != 1) { | |
|
58 | valueString.append(", "); | |
|
59 | } | |
|
60 | } | |
|
61 | ||
|
62 | valueString.append(QStringLiteral("}")); | |
|
63 | ||
|
64 | return valueString; | |
|
65 | } | |
|
66 | else { | |
|
67 | return variant.toString(); | |
|
68 | } | |
|
69 | } | |
|
70 | ||
|
71 | QString itemTooltip(const DataSourceItem *dataSource) noexcept | |
|
72 | { | |
|
73 | // The tooltip displays all item's data | |
|
74 | if (dataSource) { | |
|
75 | auto result = QString{}; | |
|
76 | ||
|
77 | const auto &data = dataSource->data(); | |
|
78 | for (auto it = data.cbegin(), end = data.cend(); it != end; ++it) { | |
|
79 | result.append(QString{"<b>%1:</b> %2<br/>"}.arg(it.key(), tooltipValue(it.value()))); | |
|
80 | } | |
|
81 | ||
|
82 | return result; | |
|
83 | } | |
|
84 | else { | |
|
85 | qCCritical(LOG_DataSourceTreeWidgetItem()) | |
|
86 | << QObject::tr("Can't set data source tooltip : the data source is null"); | |
|
87 | ||
|
88 | return QString{}; | |
|
89 | } | |
|
90 | } | |
|
91 | ||
|
39 | 92 | } // namespace |
|
40 | 93 | |
|
41 | 94 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { |
|
42 | 95 | explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {} |
|
43 | 96 | |
|
44 | 97 | /// Model used to retrieve data source information |
|
45 | 98 | const DataSourceItem *m_Data; |
|
46 | 99 | /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of |
|
47 | 100 | /// the actions |
|
48 | 101 | QList<QAction *> m_Actions; |
|
49 | 102 | }; |
|
50 | 103 | |
|
51 | 104 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type) |
|
52 | 105 | : DataSourceTreeWidgetItem{nullptr, data, type} |
|
53 | 106 | { |
|
54 | 107 | } |
|
55 | 108 | |
|
56 | 109 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data, |
|
57 | 110 | int type) |
|
58 | 111 | : QTreeWidgetItem{parent, type}, |
|
59 | 112 | impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)} |
|
60 | 113 | { |
|
61 | // Sets the icon depending on the data source | |
|
114 | // Sets the icon and the tooltip depending on the data source | |
|
62 | 115 | setIcon(0, itemIcon(impl->m_Data)); |
|
116 | setToolTip(0, itemTooltip(impl->m_Data)); | |
|
63 | 117 | |
|
64 | 118 | // Generates tree actions based on the item actions |
|
65 | 119 | auto createTreeAction = [this, &parent](const auto &itemAction) { |
|
66 | 120 | auto treeAction = new QAction{itemAction->name(), parent}; |
|
67 | 121 | |
|
68 | 122 | // Executes item action when tree action is triggered |
|
69 | 123 | QObject::connect(treeAction, &QAction::triggered, itemAction, |
|
70 | 124 | &DataSourceItemAction::execute); |
|
71 | 125 | |
|
72 | 126 | return treeAction; |
|
73 | 127 | }; |
|
74 | 128 | |
|
75 | 129 | auto itemActions = impl->m_Data->actions(); |
|
76 | 130 | std::transform(std::cbegin(itemActions), std::cend(itemActions), |
|
77 | 131 | std::back_inserter(impl->m_Actions), createTreeAction); |
|
78 | 132 | } |
|
79 | 133 | |
|
80 | 134 | QVariant DataSourceTreeWidgetItem::data(int column, int role) const |
|
81 | 135 | { |
|
82 | 136 | if (role == Qt::DisplayRole) { |
|
83 | return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{}; | |
|
137 | if (impl->m_Data) { | |
|
138 | switch (column) { | |
|
139 | case NAME_COLUMN: | |
|
140 | return impl->m_Data->name(); | |
|
141 | default: | |
|
142 | // No action | |
|
143 | break; | |
|
144 | } | |
|
145 | ||
|
146 | qCWarning(LOG_DataSourceTreeWidgetItem()) | |
|
147 | << QObject::tr("Can't get data (unknown column %1)").arg(column); | |
|
148 | } | |
|
149 | else { | |
|
150 | qCCritical(LOG_DataSourceTreeWidgetItem()) << QObject::tr("Can't get data (null item)"); | |
|
151 | } | |
|
152 | ||
|
153 | return QVariant{}; | |
|
84 | 154 | } |
|
85 | 155 | else { |
|
86 | 156 | return QTreeWidgetItem::data(column, role); |
|
87 | 157 | } |
|
88 | 158 | } |
|
89 | 159 | |
|
90 | 160 | void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value) |
|
91 | 161 | { |
|
92 | 162 | // Data can't be changed by edition |
|
93 | 163 | if (role != Qt::EditRole) { |
|
94 | 164 | QTreeWidgetItem::setData(column, role, value); |
|
95 | 165 | } |
|
96 | 166 | } |
|
97 | 167 | |
|
98 | 168 | QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept |
|
99 | 169 | { |
|
100 | 170 | return impl->m_Actions; |
|
101 | 171 | } |
@@ -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