@@ -1,87 +1,101 | |||||
1 | #include <DataSource/DataSourceItem.h> |
|
1 | #include <DataSource/DataSourceItem.h> | |
|
2 | #include <DataSource/DataSourceItemAction.h> | |||
2 | #include <DataSource/DataSourceTreeWidgetItem.h> |
|
3 | #include <DataSource/DataSourceTreeWidgetItem.h> | |
3 |
|
4 | |||
4 | #include <SqpApplication.h> |
|
5 | #include <SqpApplication.h> | |
5 |
|
6 | |||
6 | #include <QAction> |
|
7 | #include <QAction> | |
7 |
|
8 | |||
8 | Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem") |
|
9 | Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem") | |
9 |
|
10 | |||
10 | namespace { |
|
11 | namespace { | |
11 |
|
12 | |||
12 | QIcon itemIcon(const DataSourceItem *dataSource) |
|
13 | QIcon itemIcon(const DataSourceItem *dataSource) | |
13 | { |
|
14 | { | |
14 | if (dataSource) { |
|
15 | if (dataSource) { | |
15 | auto dataSourceType = dataSource->type(); |
|
16 | auto dataSourceType = dataSource->type(); | |
16 | switch (dataSourceType) { |
|
17 | switch (dataSourceType) { | |
17 | case DataSourceItemType::NODE: |
|
18 | case DataSourceItemType::NODE: | |
18 | return sqpApp->style()->standardIcon(QStyle::SP_DirIcon); |
|
19 | return sqpApp->style()->standardIcon(QStyle::SP_DirIcon); | |
19 | case DataSourceItemType::PRODUCT: |
|
20 | case DataSourceItemType::PRODUCT: | |
20 | return sqpApp->style()->standardIcon(QStyle::SP_FileIcon); |
|
21 | return sqpApp->style()->standardIcon(QStyle::SP_FileIcon); | |
21 | default: |
|
22 | default: | |
22 | // No action |
|
23 | // No action | |
23 | break; |
|
24 | break; | |
24 | } |
|
25 | } | |
25 |
|
26 | |||
26 | qCWarning(LOG_DataSourceTreeWidgetItem()) |
|
27 | qCWarning(LOG_DataSourceTreeWidgetItem()) | |
27 | << QObject::tr("Can't set data source icon : unknown data source type"); |
|
28 | << QObject::tr("Can't set data source icon : unknown data source type"); | |
28 | } |
|
29 | } | |
29 | else { |
|
30 | else { | |
30 | qCWarning(LOG_DataSourceTreeWidgetItem()) |
|
31 | qCWarning(LOG_DataSourceTreeWidgetItem()) | |
31 | << QObject::tr("Can't set data source icon : the data source is null"); |
|
32 | << QObject::tr("Can't set data source icon : the data source is null"); | |
32 | } |
|
33 | } | |
33 |
|
34 | |||
34 | // Default cases |
|
35 | // Default cases | |
35 | return QIcon{}; |
|
36 | return QIcon{}; | |
36 | } |
|
37 | } | |
37 |
|
38 | |||
38 | } // namespace |
|
39 | } // namespace | |
39 |
|
40 | |||
40 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { |
|
41 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { | |
41 | explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {} |
|
42 | explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {} | |
42 |
|
43 | |||
43 | /// Model used to retrieve data source information |
|
44 | /// Model used to retrieve data source information | |
44 | const DataSourceItem *m_Data; |
|
45 | const DataSourceItem *m_Data; | |
45 | /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of |
|
46 | /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of | |
46 | /// the actions |
|
47 | /// the actions | |
47 | QList<QAction *> m_Actions; |
|
48 | QList<QAction *> m_Actions; | |
48 | }; |
|
49 | }; | |
49 |
|
50 | |||
50 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type) |
|
51 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type) | |
51 | : DataSourceTreeWidgetItem{nullptr, data, type} |
|
52 | : DataSourceTreeWidgetItem{nullptr, data, type} | |
52 | { |
|
53 | { | |
53 | } |
|
54 | } | |
54 |
|
55 | |||
55 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data, |
|
56 | DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data, | |
56 | int type) |
|
57 | int type) | |
57 | : QTreeWidgetItem{parent, type}, |
|
58 | : QTreeWidgetItem{parent, type}, | |
58 | impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)} |
|
59 | impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)} | |
59 | { |
|
60 | { | |
60 | // Sets the icon depending on the data source |
|
61 | // Sets the icon depending on the data source | |
61 | setIcon(0, itemIcon(impl->m_Data)); |
|
62 | setIcon(0, itemIcon(impl->m_Data)); | |
62 |
|
63 | |||
63 |
// |
|
64 | // Generates tree actions based on the item actions | |
|
65 | auto createTreeAction = [this, &parent](const auto &itemAction) { | |||
|
66 | auto treeAction = new QAction{itemAction->name(), parent}; | |||
|
67 | ||||
|
68 | // Executes item action when tree action is triggered | |||
|
69 | QObject::connect(treeAction, &QAction::triggered, itemAction, | |||
|
70 | &DataSourceItemAction::execute); | |||
|
71 | ||||
|
72 | return treeAction; | |||
|
73 | }; | |||
|
74 | ||||
|
75 | auto itemActions = impl->m_Data->actions(); | |||
|
76 | std::transform(std::cbegin(itemActions), std::cend(itemActions), | |||
|
77 | std::back_inserter(impl->m_Actions), createTreeAction); | |||
64 | } |
|
78 | } | |
65 |
|
79 | |||
66 | QVariant DataSourceTreeWidgetItem::data(int column, int role) const |
|
80 | QVariant DataSourceTreeWidgetItem::data(int column, int role) const | |
67 | { |
|
81 | { | |
68 | if (role == Qt::DisplayRole) { |
|
82 | if (role == Qt::DisplayRole) { | |
69 | return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{}; |
|
83 | return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{}; | |
70 | } |
|
84 | } | |
71 | else { |
|
85 | else { | |
72 | return QTreeWidgetItem::data(column, role); |
|
86 | return QTreeWidgetItem::data(column, role); | |
73 | } |
|
87 | } | |
74 | } |
|
88 | } | |
75 |
|
89 | |||
76 | void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value) |
|
90 | void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value) | |
77 | { |
|
91 | { | |
78 | // Data can't be changed by edition |
|
92 | // Data can't be changed by edition | |
79 | if (role != Qt::EditRole) { |
|
93 | if (role != Qt::EditRole) { | |
80 | QTreeWidgetItem::setData(column, role, value); |
|
94 | QTreeWidgetItem::setData(column, role, value); | |
81 | } |
|
95 | } | |
82 | } |
|
96 | } | |
83 |
|
97 | |||
84 | QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept |
|
98 | QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept | |
85 | { |
|
99 | { | |
86 | return impl->m_Actions; |
|
100 | return impl->m_Actions; | |
87 | } |
|
101 | } |
General Comments 0
You need to be logged in to leave comments.
Login now