|
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,77 +1,77 | |||
|
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 | 24 | /// Key associated with the name of the item |
|
25 | 25 | static const QString NAME_DATA_KEY; |
|
26 | 26 | |
|
27 | 27 | explicit DataSourceItem(DataSourceItemType type, const QString &name); |
|
28 | 28 | explicit DataSourceItem(DataSourceItemType type, QHash<QString, QVariant> data = {}); |
|
29 | 29 | |
|
30 | 30 | /// @return the actions of the item as a vector |
|
31 | 31 | QVector<DataSourceItemAction *> actions() const noexcept; |
|
32 | 32 | |
|
33 | 33 | /** |
|
34 | 34 | * Adds an action to the item. The item takes ownership of the action, and the action is |
|
35 | 35 | * automatically associated to the item |
|
36 | 36 | * @param action the action to add |
|
37 | 37 | */ |
|
38 | 38 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; |
|
39 | 39 | |
|
40 | 40 | /** |
|
41 | 41 | * Adds a child to the item. The item takes ownership of the child. |
|
42 | 42 | * @param child the child to add |
|
43 | 43 | */ |
|
44 | 44 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
45 | 45 | |
|
46 | 46 | /** |
|
47 | 47 | * Returns the item's child associated to an index |
|
48 | 48 | * @param childIndex the index to search |
|
49 | 49 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
50 | 50 | */ |
|
51 | 51 | DataSourceItem *child(int childIndex) const noexcept; |
|
52 | 52 | |
|
53 | 53 | int childCount() const noexcept; |
|
54 | 54 | |
|
55 | 55 | /** |
|
56 | 56 | * Get the data associated to a key |
|
57 | 57 | * @param key the key to search |
|
58 | 58 | * @return the data found if key is valid, default QVariant otherwise |
|
59 | 59 | */ |
|
60 | 60 | QVariant data(const QString &key) const noexcept; |
|
61 | 61 | |
|
62 | 62 | QString name() const noexcept; |
|
63 | 63 | |
|
64 | 64 | /** |
|
65 | 65 | * Get the item's parent |
|
66 | 66 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
67 | 67 | */ |
|
68 | 68 | DataSourceItem *parentItem() const noexcept; |
|
69 | 69 | |
|
70 | 70 | DataSourceItemType type() const noexcept; |
|
71 | 71 | |
|
72 | 72 | private: |
|
73 | 73 | class DataSourceItemPrivate; |
|
74 | 74 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
75 | 75 | }; |
|
76 | 76 | |
|
77 | 77 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,10 +1,13 | |||
|
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> | |
|
3 | 6 | <file>icones/delete.png</file> |
|
4 | 7 | <file>icones/openInspector.png</file> |
|
5 | 8 | <file>icones/next.png</file> |
|
6 | 9 | <file>icones/plot.png</file> |
|
7 | 10 | <file>icones/previous.png</file> |
|
8 | 11 | <file>icones/unplot.png</file> |
|
9 | 12 | </qresource> |
|
10 | 13 | </RCC> |
@@ -1,120 +1,120 | |||
|
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 | |
|
13 | 11 | // Column indexes |
|
14 | 12 | const auto NAME_COLUMN = 0; |
|
15 | 13 | |
|
16 | 14 | QIcon itemIcon(const DataSourceItem *dataSource) |
|
17 | 15 | { |
|
18 | 16 | if (dataSource) { |
|
19 | 17 | auto dataSourceType = dataSource->type(); |
|
20 | 18 | switch (dataSourceType) { |
|
21 | 19 | case DataSourceItemType::NODE: |
|
22 | return sqpApp->style()->standardIcon(QStyle::SP_DirIcon); | |
|
20 | return QIcon{":/icones/dataSourceNode.png"}; | |
|
23 | 21 | case DataSourceItemType::PRODUCT: |
|
24 | return sqpApp->style()->standardIcon(QStyle::SP_FileIcon); | |
|
22 | return QIcon{":/icones/dataSourceProduct.png"}; | |
|
23 | case DataSourceItemType::COMPONENT: | |
|
24 | return QIcon{":/icones/dataSourceComponent.png"}; | |
|
25 | 25 | default: |
|
26 | 26 | // No action |
|
27 | 27 | break; |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | qCWarning(LOG_DataSourceTreeWidgetItem()) |
|
31 | 31 | << QObject::tr("Can't set data source icon : unknown data source type"); |
|
32 | 32 | } |
|
33 | 33 | else { |
|
34 | 34 | qCWarning(LOG_DataSourceTreeWidgetItem()) |
|
35 | 35 | << QObject::tr("Can't set data source icon : the data source is null"); |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | // Default cases |
|
39 | 39 | return QIcon{}; |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | } // namespace |
|
43 | 43 | |
|
44 | 44 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { |
|
45 | 45 | explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {} |
|
46 | 46 | |
|
47 | 47 | /// Model used to retrieve data source information |
|
48 | 48 | const DataSourceItem *m_Data; |
|
49 | 49 | /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of |
|
50 | 50 | /// the actions |
|
51 | 51 | QList<QAction *> m_Actions; |
|
52 | 52 | }; |
|
53 | 53 | |
|
54 | 54 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type) |
|
55 | 55 | : DataSourceTreeWidgetItem{nullptr, data, type} |
|
56 | 56 | { |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data, |
|
60 | 60 | int type) |
|
61 | 61 | : QTreeWidgetItem{parent, type}, |
|
62 | 62 | impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)} |
|
63 | 63 | { |
|
64 | 64 | // Sets the icon depending on the data source |
|
65 | 65 | setIcon(0, itemIcon(impl->m_Data)); |
|
66 | 66 | |
|
67 | 67 | // Generates tree actions based on the item actions |
|
68 | 68 | auto createTreeAction = [this, &parent](const auto &itemAction) { |
|
69 | 69 | auto treeAction = new QAction{itemAction->name(), parent}; |
|
70 | 70 | |
|
71 | 71 | // Executes item action when tree action is triggered |
|
72 | 72 | QObject::connect(treeAction, &QAction::triggered, itemAction, |
|
73 | 73 | &DataSourceItemAction::execute); |
|
74 | 74 | |
|
75 | 75 | return treeAction; |
|
76 | 76 | }; |
|
77 | 77 | |
|
78 | 78 | auto itemActions = impl->m_Data->actions(); |
|
79 | 79 | std::transform(std::cbegin(itemActions), std::cend(itemActions), |
|
80 | 80 | std::back_inserter(impl->m_Actions), createTreeAction); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | QVariant DataSourceTreeWidgetItem::data(int column, int role) const |
|
84 | 84 | { |
|
85 | 85 | if (role == Qt::DisplayRole) { |
|
86 | 86 | if (impl->m_Data) { |
|
87 | 87 | switch (column) { |
|
88 | 88 | case NAME_COLUMN: |
|
89 | 89 | return impl->m_Data->name(); |
|
90 | 90 | default: |
|
91 | 91 | // No action |
|
92 | 92 | break; |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | qCWarning(LOG_DataSourceTreeWidgetItem()) |
|
96 | 96 | << QObject::tr("Can't get data (unknown column %1)").arg(column); |
|
97 | 97 | } |
|
98 | 98 | else { |
|
99 | 99 | qCCritical(LOG_DataSourceTreeWidgetItem()) << QObject::tr("Can't get data (null item)"); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | return QVariant{}; |
|
103 | 103 | } |
|
104 | 104 | else { |
|
105 | 105 | return QTreeWidgetItem::data(column, role); |
|
106 | 106 | } |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value) |
|
110 | 110 | { |
|
111 | 111 | // Data can't be changed by edition |
|
112 | 112 | if (role != Qt::EditRole) { |
|
113 | 113 | QTreeWidgetItem::setData(column, role, value); |
|
114 | 114 | } |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept |
|
118 | 118 | { |
|
119 | 119 | return impl->m_Actions; |
|
120 | 120 | } |
General Comments 0
You need to be logged in to leave comments.
Login now