##// END OF EJS Templates
Creates helper that filters a tree widget according to a filter function
Alexandre Leroux -
r476:becb718e8802
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 }
General Comments 0
You need to be logged in to leave comments. Login now