##// END OF EJS Templates
Merge branch 'feature/DataSourceFilter' into develop
Alexandre Leroux -
r441:5cdac9f101d7 merge
parent child
Show More
@@ -0,0 +1,23
1 #ifndef SCIQLOP_DATASOURCETREEWIDGETHELPER_H
2 #define SCIQLOP_DATASOURCETREEWIDGETHELPER_H
3
4 #include <functional>
5
6 class DataSourceTreeWidgetItem;
7 class QTreeWidget;
8
9 class DataSourceTreeWidgetHelper {
10 public:
11 /// Signature of the function associated to the filtering action
12 using FilterFunction = std::function<bool(const DataSourceTreeWidgetItem &dataSourceItem)>;
13
14 /**
15 * Filters a tree widget according to a function. If an item is valid according to this
16 * function, all of its ancestors and children are shown
17 * @param treeWidget the widget to filter
18 * @param fun the filter function
19 */
20 static void filter(QTreeWidget &treeWidget, FilterFunction fun) noexcept;
21 };
22
23 #endif // SCIQLOP_DATASOURCETREEWIDGETHELPER_H
@@ -0,0 +1,36
1 #include "DataSource/DataSourceTreeWidgetHelper.h"
2 #include "DataSource/DataSourceTreeWidgetItem.h"
3
4 namespace {
5
6 bool filterTreeItem(DataSourceTreeWidgetItem &treeItem,
7 DataSourceTreeWidgetHelper::FilterFunction fun, bool parentValid = false)
8 {
9 auto selfValid = parentValid || fun(treeItem);
10
11 auto childValid = false;
12 auto childCount = treeItem.childCount();
13 for (auto i = 0; i < childCount; ++i) {
14 if (auto childItem = dynamic_cast<DataSourceTreeWidgetItem *>(treeItem.child(i))) {
15 childValid |= filterTreeItem(*childItem, fun, selfValid);
16 }
17 }
18
19 auto valid = selfValid || childValid;
20
21 treeItem.setHidden(!valid);
22
23 return valid;
24 }
25
26 } // namespace
27
28 void DataSourceTreeWidgetHelper::filter(QTreeWidget &treeWidget, FilterFunction fun) noexcept
29 {
30 auto itemCount = treeWidget.topLevelItemCount();
31 for (auto i = 0; i < itemCount; ++i) {
32 if (auto item = dynamic_cast<DataSourceTreeWidgetItem *>(treeWidget.topLevelItem(i))) {
33 filterTreeItem(*item, fun);
34 }
35 }
36 }
@@ -1,35 +1,37
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 const DataSourceItem *data() const;
25
24 26 virtual QVariant data(int column, int role) const override;
25 27 virtual void setData(int column, int role, const QVariant &value) override;
26 28
27 29 /// @return the actions associated to the item
28 30 QList<QAction *> actions() const noexcept;
29 31
30 32 private:
31 33 class DataSourceTreeWidgetItemPrivate;
32 34 spimpl::unique_impl_ptr<DataSourceTreeWidgetItemPrivate> impl;
33 35 };
34 36
35 37 #endif // SCIQLOP_DATASOURCETREEWIDGETITEM_H
@@ -1,39 +1,42
1 1 #ifndef SCIQLOP_DATASOURCEWIDGET_H
2 2 #define SCIQLOP_DATASOURCEWIDGET_H
3 3
4 4 #include <QWidget>
5 5
6 6 namespace Ui {
7 7 class DataSourceWidget;
8 8 } // Ui
9 9
10 10 class DataSourceItem;
11 11
12 12 /**
13 13 * @brief The DataSourceWidget handles the graphical representation (as a tree) of the data sources
14 14 * attached to SciQlop.
15 15 */
16 16 class DataSourceWidget : public QWidget {
17 17 Q_OBJECT
18 18
19 19 public:
20 20 explicit DataSourceWidget(QWidget *parent = 0);
21 21 virtual ~DataSourceWidget() noexcept;
22 22
23 23 public slots:
24 24 /**
25 25 * Adds a data source. An item associated to the data source is created and then added to the
26 26 * representation tree
27 27 * @param dataSource the data source to add. The pointer has to be not null
28 28 */
29 29 void addDataSource(DataSourceItem *dataSource) noexcept;
30 30
31 31 private:
32 32 Ui::DataSourceWidget *ui;
33 33
34 34 private slots:
35 /// Slot called when the filtering text has changed
36 void filterChanged(const QString &text) noexcept;
37
35 38 /// Slot called when right clicking on an item in the tree (displays a menu)
36 39 void onTreeMenuRequested(const QPoint &pos) noexcept;
37 40 };
38 41
39 42 #endif // SCIQLOP_DATASOURCEWIDGET_H
@@ -1,171 +1,176
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 19 case DataSourceItemType::NODE: {
20 20 return dataSource->isRoot() ? QIcon{":/icones/dataSourceRoot.png"}
21 21 : QIcon{":/icones/dataSourceNode.png"};
22 22 }
23 23 case DataSourceItemType::PRODUCT:
24 24 return QIcon{":/icones/dataSourceProduct.png"};
25 25 case DataSourceItemType::COMPONENT:
26 26 return QIcon{":/icones/dataSourceComponent.png"};
27 27 default:
28 28 // No action
29 29 break;
30 30 }
31 31
32 32 qCWarning(LOG_DataSourceTreeWidgetItem())
33 33 << QObject::tr("Can't set data source icon : unknown data source type");
34 34 }
35 35 else {
36 36 qCCritical(LOG_DataSourceTreeWidgetItem())
37 37 << QObject::tr("Can't set data source icon : the data source is null");
38 38 }
39 39
40 40 // Default cases
41 41 return QIcon{};
42 42 }
43 43
44 44 /// @return the tooltip text for a variant. The text depends on whether the data is a simple variant
45 45 /// or a list of variants
46 46 QString tooltipValue(const QVariant &variant) noexcept
47 47 {
48 48 // If the variant is a list of variants, the text of the tooltip is of the form: {val1, val2,
49 49 // ...}
50 50 if (variant.canConvert<QVariantList>()) {
51 51 auto valueString = QStringLiteral("{");
52 52
53 53 auto variantList = variant.value<QVariantList>();
54 54 for (auto it = variantList.cbegin(), end = variantList.cend(); it != end; ++it) {
55 55 valueString.append(it->toString());
56 56
57 57 if (std::distance(it, end) != 1) {
58 58 valueString.append(", ");
59 59 }
60 60 }
61 61
62 62 valueString.append(QStringLiteral("}"));
63 63
64 64 return valueString;
65 65 }
66 66 else {
67 67 return variant.toString();
68 68 }
69 69 }
70 70
71 71 QString itemTooltip(const DataSourceItem *dataSource) noexcept
72 72 {
73 73 // The tooltip displays all item's data
74 74 if (dataSource) {
75 75 auto result = QString{};
76 76
77 77 const auto &data = dataSource->data();
78 78 for (auto it = data.cbegin(), end = data.cend(); it != end; ++it) {
79 79 result.append(QString{"<b>%1:</b> %2<br/>"}.arg(it.key(), tooltipValue(it.value())));
80 80 }
81 81
82 82 return result;
83 83 }
84 84 else {
85 85 qCCritical(LOG_DataSourceTreeWidgetItem())
86 86 << QObject::tr("Can't set data source tooltip : the data source is null");
87 87
88 88 return QString{};
89 89 }
90 90 }
91 91
92 92 } // namespace
93 93
94 94 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
95 95 explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
96 96
97 97 /// Model used to retrieve data source information
98 98 const DataSourceItem *m_Data;
99 99 /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
100 100 /// the actions
101 101 QList<QAction *> m_Actions;
102 102 };
103 103
104 104 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type)
105 105 : DataSourceTreeWidgetItem{nullptr, data, type}
106 106 {
107 107 }
108 108
109 109 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
110 110 int type)
111 111 : QTreeWidgetItem{parent, type},
112 112 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
113 113 {
114 114 // Sets the icon and the tooltip depending on the data source
115 115 setIcon(0, itemIcon(impl->m_Data));
116 116 setToolTip(0, itemTooltip(impl->m_Data));
117 117
118 118 // Generates tree actions based on the item actions
119 119 auto createTreeAction = [this, &parent](const auto &itemAction) {
120 120 auto treeAction = new QAction{itemAction->name(), parent};
121 121
122 122 // Executes item action when tree action is triggered
123 123 QObject::connect(treeAction, &QAction::triggered, itemAction,
124 124 &DataSourceItemAction::execute);
125 125
126 126 return treeAction;
127 127 };
128 128
129 129 auto itemActions = impl->m_Data->actions();
130 130 std::transform(std::cbegin(itemActions), std::cend(itemActions),
131 131 std::back_inserter(impl->m_Actions), createTreeAction);
132 132 }
133 133
134 const DataSourceItem *DataSourceTreeWidgetItem::data() const
135 {
136 return impl->m_Data;
137 }
138
134 139 QVariant DataSourceTreeWidgetItem::data(int column, int role) const
135 140 {
136 141 if (role == Qt::DisplayRole) {
137 142 if (impl->m_Data) {
138 143 switch (column) {
139 144 case NAME_COLUMN:
140 145 return impl->m_Data->name();
141 146 default:
142 147 // No action
143 148 break;
144 149 }
145 150
146 151 qCWarning(LOG_DataSourceTreeWidgetItem())
147 152 << QObject::tr("Can't get data (unknown column %1)").arg(column);
148 153 }
149 154 else {
150 155 qCCritical(LOG_DataSourceTreeWidgetItem()) << QObject::tr("Can't get data (null item)");
151 156 }
152 157
153 158 return QVariant{};
154 159 }
155 160 else {
156 161 return QTreeWidgetItem::data(column, role);
157 162 }
158 163 }
159 164
160 165 void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value)
161 166 {
162 167 // Data can't be changed by edition
163 168 if (role != Qt::EditRole) {
164 169 QTreeWidgetItem::setData(column, role, value);
165 170 }
166 171 }
167 172
168 173 QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
169 174 {
170 175 return impl->m_Actions;
171 176 }
@@ -1,77 +1,100
1 1 #include <DataSource/DataSourceWidget.h>
2 2
3 3 #include <ui_DataSourceWidget.h>
4 4
5 5 #include <DataSource/DataSourceItem.h>
6 #include <DataSource/DataSourceTreeWidgetHelper.h>
6 7 #include <DataSource/DataSourceTreeWidgetItem.h>
7 8
8 9 #include <QMenu>
9 10
10 11 namespace {
11 12
12 13 /// Number of columns displayed in the tree
13 14 const auto TREE_NB_COLUMNS = 1;
14 15
15 16 /// Header labels for the tree
16 17 const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")};
17 18
18 19 /**
19 20 * Creates the item associated to a data source
20 21 * @param dataSource the data source for which to create the item
21 22 * @return the new item
22 23 */
23 24 DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource)
24 25 {
25 26 // Creates item for the data source
26 27 auto item = new DataSourceTreeWidgetItem{dataSource};
27 28
28 29 // Generates items for the children of the data source
29 30 for (auto i = 0; i < dataSource->childCount(); ++i) {
30 31 item->addChild(createTreeWidgetItem(dataSource->child(i)));
31 32 }
32 33
33 34 return item;
34 35 }
35 36
36 37 } // namespace
37 38
38 39 DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget}
39 40 {
40 41 ui->setupUi(this);
41 42
42 43 // Set tree properties
43 44 ui->treeWidget->setColumnCount(TREE_NB_COLUMNS);
44 45 ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS);
45 46 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
46 47
47 48 // Connection to show a menu when right clicking on the tree
48 49 connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
49 50 &DataSourceWidget::onTreeMenuRequested);
51
52 // Connection to filter tree
53 connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &DataSourceWidget::filterChanged);
50 54 }
51 55
52 56 DataSourceWidget::~DataSourceWidget() noexcept
53 57 {
54 58 delete ui;
55 59 }
56 60
57 61 void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept
58 62 {
59 63 // Creates the item associated to the source and adds it to the tree widget. The tree widget
60 64 // takes the ownership of the item
61 65 if (dataSource) {
62 66 ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource));
63 67 }
64 68 }
65 69
70 void DataSourceWidget::filterChanged(const QString &text) noexcept
71 {
72 auto validateItem = [&text](const DataSourceTreeWidgetItem &item) {
73 auto regExp = QRegExp{text, Qt::CaseInsensitive, QRegExp::Wildcard};
74
75 // An item is valid if any of its metadata validates the text filter
76 auto itemMetadata = item.data()->data();
77 auto itemMetadataEnd = itemMetadata.cend();
78 auto acceptFilter
79 = [&regExp](const auto &variant) { return variant.toString().contains(regExp); };
80
81 return std::find_if(itemMetadata.cbegin(), itemMetadataEnd, acceptFilter)
82 != itemMetadataEnd;
83 };
84
85 // Applies filter on tree widget
86 DataSourceTreeWidgetHelper::filter(*ui->treeWidget, validateItem);
87 }
88
66 89 void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept
67 90 {
68 91 // Retrieves the selected item in the tree, and build the menu from its actions
69 92 if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) {
70 93 QMenu treeMenu{};
71 94 treeMenu.addActions(selectedItem->actions());
72 95
73 96 if (!treeMenu.isEmpty()) {
74 97 treeMenu.exec(mapToGlobal(pos));
75 98 }
76 99 }
77 100 }
@@ -1,42 +1,33
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>DataSourceWidget</class>
4 4 <widget class="QWidget" name="DataSourceWidget">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>400</width>
10 10 <height>300</height>
11 11 </rect>
12 12 </property>
13 13 <property name="windowTitle">
14 14 <string>Data sources</string>
15 15 </property>
16 16 <layout class="QGridLayout" name="gridLayout">
17 <property name="topMargin">
18 <number>0</number>
19 </property>
20 <property name="rightMargin">
21 <number>0</number>
22 </property>
23 <property name="bottomMargin">
24 <number>0</number>
25 </property>
26 <property name="spacing">
27 <number>0</number>
28 </property>
29 17 <item row="0" column="0">
18 <widget class="QLineEdit" name="filterLineEdit"/>
19 </item>
20 <item row="1" column="0">
30 21 <widget class="QTreeWidget" name="treeWidget">
31 22 <column>
32 23 <property name="text">
33 24 <string notr="true">1</string>
34 25 </property>
35 26 </column>
36 27 </widget>
37 28 </item>
38 29 </layout>
39 30 </widget>
40 31 <resources/>
41 32 <connections/>
42 33 </ui>
General Comments 0
You need to be logged in to leave comments. Login now