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