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