##// END OF EJS Templates
Adds actions for items in the DataSourceWidget...
Alexandre Leroux -
r142:11579fae1cc2
parent child
Show More
@@ -1,32 +1,35
1 1 #ifndef SCIQLOP_DATASOURCETREEWIDGETITEM_H
2 2 #define SCIQLOP_DATASOURCETREEWIDGETITEM_H
3 3
4 4 #include <Common/spimpl.h>
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QTreeWidgetItem>
8 8
9 9 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem)
10 10
11 11 class DataSourceItem;
12 12
13 13 /**
14 14 * @brief The DataSourceTreeWidgetItem is the graphical representation of a data source item. It is
15 15 * intended to be displayed in a QTreeWidget.
16 16 * @sa DataSourceItem
17 17 */
18 18 class DataSourceTreeWidgetItem : public QTreeWidgetItem {
19 19 public:
20 20 explicit DataSourceTreeWidgetItem(const DataSourceItem *data, int type = Type);
21 21 explicit DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
22 22 int type = Type);
23 23
24 24 virtual QVariant data(int column, int role) const override;
25 25 virtual void setData(int column, int role, const QVariant &value) override;
26 26
27 /// @return the actions associated to the item
28 QList<QAction *> actions() const noexcept;
29
27 30 private:
28 31 class DataSourceTreeWidgetItemPrivate;
29 32 spimpl::unique_impl_ptr<DataSourceTreeWidgetItemPrivate> impl;
30 33 };
31 34
32 35 #endif // SCIQLOP_DATASOURCETREEWIDGETITEM_H
@@ -1,75 +1,87
1 1 #include <DataSource/DataSourceItem.h>
2 2 #include <DataSource/DataSourceTreeWidgetItem.h>
3 3
4 4 #include <SqpApplication.h>
5 5
6 #include <QAction>
7
6 8 Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem")
7 9
8 10 namespace {
9 11
10 12 QIcon itemIcon(const DataSourceItem *dataSource)
11 13 {
12 14 if (dataSource) {
13 15 auto dataSourceType = dataSource->type();
14 16 switch (dataSourceType) {
15 17 case DataSourceItemType::NODE:
16 18 return sqpApp->style()->standardIcon(QStyle::SP_DirIcon);
17 19 case DataSourceItemType::PRODUCT:
18 20 return sqpApp->style()->standardIcon(QStyle::SP_FileIcon);
19 21 default:
20 22 // No action
21 23 break;
22 24 }
23 25
24 26 qCWarning(LOG_DataSourceTreeWidgetItem())
25 27 << QObject::tr("Can't set data source icon : unknown data source type");
26 28 }
27 29 else {
28 30 qCWarning(LOG_DataSourceTreeWidgetItem())
29 31 << QObject::tr("Can't set data source icon : the data source is null");
30 32 }
31 33
32 34 // Default cases
33 35 return QIcon{};
34 36 }
35 37
36 38 } // namespace
37 39
38 40 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
39 41 explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
40 42
41 43 /// Model used to retrieve data source information
42 44 const DataSourceItem *m_Data;
45 /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
46 /// the actions
47 QList<QAction *> m_Actions;
43 48 };
44 49
45 50 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type)
46 51 : DataSourceTreeWidgetItem{nullptr, data, type}
47 52 {
48 53 }
49 54
50 55 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
51 56 int type)
52 57 : QTreeWidgetItem{parent, type},
53 58 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
54 59 {
55 60 // Sets the icon depending on the data source
56 61 setIcon(0, itemIcon(impl->m_Data));
62
63 /// @todo ALX : generate actions based on the DataSourceItem (model)
57 64 }
58 65
59 66 QVariant DataSourceTreeWidgetItem::data(int column, int role) const
60 67 {
61 68 if (role == Qt::DisplayRole) {
62 69 return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{};
63 70 }
64 71 else {
65 72 return QTreeWidgetItem::data(column, role);
66 73 }
67 74 }
68 75
69 76 void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value)
70 77 {
71 78 // Data can't be changed by edition
72 79 if (role != Qt::EditRole) {
73 80 QTreeWidgetItem::setData(column, role, value);
74 81 }
75 82 }
83
84 QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
85 {
86 return impl->m_Actions;
87 }
General Comments 0
You need to be logged in to leave comments. Login now