Skip to content

Commit bfa38e2

Browse files
Add tree filtering to Advanced Import Settings
* Implements node and resource filtering in the scene import settings dialog. * Moved TreeSortAndFilterBar class from ObjectDB Profiler module to the `editor/` folder so its available for every other Tree in the editor that has filtering. Added editor\gui\tree_sort_and_filter_line_edit.cpp editor\gui\tree_sort_and_filter_line_edit.h Updated editor\import\3d\scene_import_settings.cpp editor\import\3d\scene_import_settings.h modules\objectdb_profiler\editor\data_viewers\class_view.cpp modules\objectdb_profiler\editor\data_viewers\node_view.h modules\objectdb_profiler\editor\data_viewers\object_view.h modules\objectdb_profiler\editor\data_viewers\refcounted_view.h modules\objectdb_profiler\editor\data_viewers\shared_controls.cpp modules\objectdb_profiler\editor\data_viewers\shared_controls.h
1 parent 6459609 commit bfa38e2

13 files changed

Lines changed: 373 additions & 276 deletions
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/**************************************************************************/
2+
/* tree_sort_and_filter_line_edit.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "tree_sort_and_filter_line_edit.h"
32+
33+
#include "core/object/callable_mp.h"
34+
#include "editor/editor_node.h"
35+
#include "editor/editor_string_names.h"
36+
#include "editor/themes/editor_scale.h"
37+
#include "scene/gui/line_edit.h"
38+
#include "scene/gui/menu_button.h"
39+
40+
void TreeSortAndFilterBar::_apply_filter(TreeItem *p_current_node) {
41+
if (!p_current_node) {
42+
p_current_node = managed_tree->get_root();
43+
}
44+
45+
if (!p_current_node) {
46+
return;
47+
}
48+
49+
// Reset ourselves to default state.
50+
p_current_node->set_visible(true);
51+
p_current_node->clear_custom_color(0);
52+
53+
// Go through each child and filter them.
54+
bool any_child_visible = false;
55+
for (TreeItem *child = p_current_node->get_first_child(); child; child = child->get_next()) {
56+
_apply_filter(child);
57+
if (child->is_visible()) {
58+
any_child_visible = true;
59+
}
60+
}
61+
62+
// Check if we match the filter.
63+
String filter_str = filter_edit->get_text().strip_edges(true, true).to_lower();
64+
65+
// We are visible.
66+
bool matches_filter = false;
67+
for (int i = 0; i < managed_tree->get_columns(); i++) {
68+
if (p_current_node->get_text(i).to_lower().contains(filter_str)) {
69+
matches_filter = true;
70+
break;
71+
}
72+
}
73+
if (matches_filter || filter_str.is_empty()) {
74+
p_current_node->set_visible(true);
75+
} else if (any_child_visible) {
76+
// We have a visible child.
77+
p_current_node->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
78+
} else {
79+
// We and our children are not visible.
80+
p_current_node->set_visible(false);
81+
}
82+
}
83+
84+
void TreeSortAndFilterBar::_apply_sort() {
85+
if (!sort_button->is_visible()) {
86+
return;
87+
}
88+
for (int i = 0; i != sort_button->get_popup()->get_item_count(); i++) {
89+
// Update the popup buttons to be checked/unchecked.
90+
sort_button->get_popup()->set_item_checked(i, (i == (int)current_sort));
91+
}
92+
93+
SortItem sort = sort_items[current_sort];
94+
95+
List<TreeItem *> items_to_sort;
96+
items_to_sort.push_back(managed_tree->get_root());
97+
98+
while (items_to_sort.size() > 0) {
99+
TreeItem *to_sort = items_to_sort.front()->get();
100+
items_to_sort.pop_front();
101+
102+
LocalVector<TreeItemColumn> items;
103+
items.reserve(to_sort->get_child_count());
104+
for (int i = 0; i < to_sort->get_child_count(); i++) {
105+
items.push_back(TreeItemColumn(to_sort->get_child(i), sort.column));
106+
}
107+
108+
if (sort.type == ALPHA_SORT && sort.ascending == true) {
109+
items.sort_custom<TreeItemAlphaComparator>();
110+
}
111+
if (sort.type == ALPHA_SORT && sort.ascending == false) {
112+
items.sort_custom<TreeItemAlphaComparator>();
113+
items.reverse();
114+
}
115+
if (sort.type == NUMERIC_SORT && sort.ascending == true) {
116+
items.sort_custom<TreeItemNumericComparator>();
117+
}
118+
if (sort.type == NUMERIC_SORT && sort.ascending == false) {
119+
items.sort_custom<TreeItemNumericComparator>();
120+
items.reverse();
121+
}
122+
123+
TreeItem *previous = nullptr;
124+
for (const TreeItemColumn &item : items) {
125+
if (previous != nullptr) {
126+
item.item->move_after(previous);
127+
} else {
128+
item.item->move_before(to_sort->get_first_child());
129+
}
130+
previous = item.item;
131+
items_to_sort.push_back(item.item);
132+
}
133+
}
134+
}
135+
136+
void TreeSortAndFilterBar::_sort_changed(int p_id) {
137+
current_sort = p_id;
138+
_apply_sort();
139+
}
140+
141+
void TreeSortAndFilterBar::_filter_changed(const String &p_filter) {
142+
_apply_filter();
143+
}
144+
145+
TreeSortAndFilterBar::TreeSortAndFilterBar(Tree *p_managed_tree, const String &p_filter_placeholder_text) :
146+
managed_tree(p_managed_tree) {
147+
set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
148+
add_theme_constant_override("h_separation", 10 * EDSCALE);
149+
filter_edit = memnew(LineEdit);
150+
filter_edit->set_clear_button_enabled(true);
151+
filter_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
152+
filter_edit->set_placeholder(p_filter_placeholder_text);
153+
add_child(filter_edit);
154+
filter_edit->connect(SceneStringName(text_changed), callable_mp(this, &TreeSortAndFilterBar::_filter_changed));
155+
156+
sort_button = memnew(MenuButton);
157+
sort_button->set_visible(false);
158+
sort_button->set_flat(false);
159+
sort_button->set_theme_type_variation("FlatMenuButton");
160+
PopupMenu *p = sort_button->get_popup();
161+
p->connect(SceneStringName(id_pressed), callable_mp(this, &TreeSortAndFilterBar::_sort_changed));
162+
163+
add_child(sort_button);
164+
}
165+
166+
void TreeSortAndFilterBar::_notification(int p_what) {
167+
switch (p_what) {
168+
case NOTIFICATION_THEME_CHANGED: {
169+
filter_edit->set_right_icon(get_editor_theme_icon(SNAME("Search")));
170+
sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
171+
apply();
172+
} break;
173+
}
174+
}
175+
176+
TreeSortAndFilterBar::SortOptionIndexes TreeSortAndFilterBar::add_sort_option(const String &p_new_option, SortType p_sort_type, int p_sort_column, bool p_is_default) {
177+
sort_button->set_visible(true);
178+
bool is_first_item = sort_items.is_empty();
179+
SortItem item_ascending(sort_items.size(), vformat(TTR("Sort By %s (Ascending)"), p_new_option), p_sort_type, true, p_sort_column);
180+
sort_items[item_ascending.id] = item_ascending;
181+
sort_button->get_popup()->add_radio_check_item(item_ascending.label, item_ascending.id);
182+
183+
SortItem item_descending(sort_items.size(), vformat(TTR("Sort By %s (Descending)"), p_new_option), p_sort_type, false, p_sort_column);
184+
sort_items[item_descending.id] = item_descending;
185+
sort_button->get_popup()->add_radio_check_item(item_descending.label, item_descending.id);
186+
187+
if (is_first_item) {
188+
sort_button->get_popup()->set_item_checked(0, true);
189+
}
190+
191+
SortOptionIndexes indexes;
192+
indexes.ascending = item_ascending.id;
193+
indexes.descending = item_descending.id;
194+
return indexes;
195+
}
196+
197+
void TreeSortAndFilterBar::clear_filter() {
198+
filter_edit->clear();
199+
}
200+
201+
void TreeSortAndFilterBar::clear() {
202+
sort_button->set_visible(false);
203+
sort_button->get_popup()->clear();
204+
filter_edit->clear();
205+
}
206+
207+
void TreeSortAndFilterBar::select_sort(int p_item_id) {
208+
_sort_changed(p_item_id);
209+
}
210+
211+
void TreeSortAndFilterBar::apply() {
212+
if (!managed_tree || !managed_tree->get_root()) {
213+
return;
214+
}
215+
216+
_apply_sort();
217+
_apply_filter();
218+
}
219+
220+
void TreeSortAndFilterBar::set_tree(Tree *p_managed_tree) {
221+
if (managed_tree == p_managed_tree) {
222+
return;
223+
}
224+
managed_tree = p_managed_tree;
225+
apply();
226+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**************************************************************************/
2+
/* tree_sort_and_filter_line_edit.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "scene/gui/box_container.h"
34+
#include "scene/gui/tree.h"
35+
36+
class LineEdit;
37+
class MenuButton;
38+
39+
class TreeSortAndFilterBar : public HBoxContainer {
40+
GDCLASS(TreeSortAndFilterBar, HBoxContainer);
41+
42+
public:
43+
// The ways a column can be sorted, either alphabetically or numerically.
44+
enum SortType {
45+
NUMERIC_SORT = 0,
46+
ALPHA_SORT,
47+
SORT_TYPE_MAX
48+
};
49+
50+
// Returned when a new sort is added. Each new sort can be either ascending or descending,
51+
// so we return the index of each sort option.
52+
struct SortOptionIndexes {
53+
int ascending;
54+
int descending;
55+
};
56+
57+
protected:
58+
// Context needed to sort the tree in a certain way.
59+
// Combines a sort type, the column to apply it, and if it's ascending or descending.
60+
struct SortItem {
61+
SortItem() {}
62+
SortItem(int p_id, const String &p_label, SortType p_type, bool p_ascending, int p_column) :
63+
id(p_id), label(p_label), type(p_type), ascending(p_ascending), column(p_column) {}
64+
int id = 0;
65+
String label;
66+
SortType type = SortType::NUMERIC_SORT;
67+
bool ascending = false;
68+
int column = 0;
69+
};
70+
71+
struct TreeItemColumn {
72+
TreeItemColumn() {}
73+
TreeItemColumn(TreeItem *p_item, int p_column) :
74+
item(p_item), column(p_column) {}
75+
TreeItem *item = nullptr;
76+
int column;
77+
};
78+
79+
struct TreeItemAlphaComparator {
80+
bool operator()(const TreeItemColumn &p_a, const TreeItemColumn &p_b) const {
81+
return NoCaseComparator()(p_a.item->get_text(p_a.column), p_b.item->get_text(p_b.column));
82+
}
83+
};
84+
85+
struct TreeItemNumericComparator {
86+
bool operator()(const TreeItemColumn &p_a, const TreeItemColumn &p_b) const {
87+
return p_a.item->get_text(p_a.column).to_int() < p_b.item->get_text(p_b.column).to_int();
88+
}
89+
};
90+
91+
Tree *managed_tree = nullptr;
92+
LineEdit *filter_edit = nullptr;
93+
MenuButton *sort_button = nullptr;
94+
HashMap<int, SortItem> sort_items;
95+
int current_sort = 0;
96+
97+
void _apply_filter(TreeItem *p_current_node = nullptr);
98+
void _apply_sort();
99+
void _sort_changed(int p_id);
100+
void _filter_changed(const String &p_filter);
101+
102+
public:
103+
TreeSortAndFilterBar(Tree *p_managed_tree, const String &p_filter_placeholder_text);
104+
void _notification(int p_what);
105+
void set_tree(Tree *p_managed_tree);
106+
SortOptionIndexes add_sort_option(const String &p_new_option, SortType p_sort_type, int p_sort_column, bool p_is_default = false);
107+
void clear_filter();
108+
void clear();
109+
void select_sort(int p_item_id);
110+
void apply();
111+
};

0 commit comments

Comments
 (0)