Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions editor/gui/tree_sort_and_filter_line_edit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/**************************************************************************/
/* tree_sort_and_filter_line_edit.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "tree_sort_and_filter_line_edit.h"

#include "core/object/callable_mp.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/menu_button.h"

void TreeSortAndFilterBar::_apply_filter(TreeItem *p_current_node) {
if (!p_current_node) {
p_current_node = managed_tree->get_root();
}

if (!p_current_node) {
return;
}

// Reset ourselves to default state.
p_current_node->set_visible(true);
p_current_node->clear_custom_color(0);

// Go through each child and filter them.
bool any_child_visible = false;
for (TreeItem *child = p_current_node->get_first_child(); child; child = child->get_next()) {
_apply_filter(child);
if (child->is_visible()) {
any_child_visible = true;
}
}

// Check if we match the filter.
String filter_str = filter_edit->get_text().strip_edges(true, true).to_lower();

// We are visible.
bool matches_filter = false;
for (int i = 0; i < managed_tree->get_columns(); i++) {
if (p_current_node->get_text(i).to_lower().contains(filter_str)) {
matches_filter = true;
break;
}
}
if (matches_filter || filter_str.is_empty()) {
p_current_node->set_visible(true);
} else if (any_child_visible) {
// We have a visible child.
p_current_node->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
} else {
// We and our children are not visible.
p_current_node->set_visible(false);
}
}

void TreeSortAndFilterBar::_apply_sort() {
if (!sort_button->is_visible()) {
return;
}
for (int i = 0; i != sort_button->get_popup()->get_item_count(); i++) {
// Update the popup buttons to be checked/unchecked.
sort_button->get_popup()->set_item_checked(i, (i == (int)current_sort));
}

SortItem sort = sort_items[current_sort];

List<TreeItem *> items_to_sort;
items_to_sort.push_back(managed_tree->get_root());

while (items_to_sort.size() > 0) {
TreeItem *to_sort = items_to_sort.front()->get();
items_to_sort.pop_front();

LocalVector<TreeItemColumn> items;
items.reserve(to_sort->get_child_count());
for (int i = 0; i < to_sort->get_child_count(); i++) {
items.push_back(TreeItemColumn(to_sort->get_child(i), sort.column));
}

if (sort.type == ALPHA_SORT && sort.ascending == true) {
items.sort_custom<TreeItemAlphaComparator>();
}
if (sort.type == ALPHA_SORT && sort.ascending == false) {
items.sort_custom<TreeItemAlphaComparator>();
items.reverse();
}
if (sort.type == NUMERIC_SORT && sort.ascending == true) {
items.sort_custom<TreeItemNumericComparator>();
}
if (sort.type == NUMERIC_SORT && sort.ascending == false) {
items.sort_custom<TreeItemNumericComparator>();
items.reverse();
}

TreeItem *previous = nullptr;
for (const TreeItemColumn &item : items) {
if (previous != nullptr) {
item.item->move_after(previous);
} else {
item.item->move_before(to_sort->get_first_child());
}
previous = item.item;
items_to_sort.push_back(item.item);
}
}
}

void TreeSortAndFilterBar::_sort_changed(int p_id) {
current_sort = p_id;
_apply_sort();
}

void TreeSortAndFilterBar::_filter_changed(const String &p_filter) {
_apply_filter();
}

TreeSortAndFilterBar::TreeSortAndFilterBar(Tree *p_managed_tree, const String &p_filter_placeholder_text) :
managed_tree(p_managed_tree) {
set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
add_theme_constant_override("h_separation", 10 * EDSCALE);
filter_edit = memnew(LineEdit);
filter_edit->set_clear_button_enabled(true);
filter_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
filter_edit->set_placeholder(p_filter_placeholder_text);
add_child(filter_edit);
filter_edit->connect(SceneStringName(text_changed), callable_mp(this, &TreeSortAndFilterBar::_filter_changed));

sort_button = memnew(MenuButton);
sort_button->set_visible(false);
sort_button->set_flat(false);
sort_button->set_theme_type_variation("FlatMenuButton");
PopupMenu *p = sort_button->get_popup();
p->connect(SceneStringName(id_pressed), callable_mp(this, &TreeSortAndFilterBar::_sort_changed));

add_child(sort_button);
}

void TreeSortAndFilterBar::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
filter_edit->set_right_icon(get_editor_theme_icon(SNAME("Search")));
sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
apply();
} break;
}
}

TreeSortAndFilterBar::SortOptionIndexes TreeSortAndFilterBar::add_sort_option(const String &p_new_option, SortType p_sort_type, int p_sort_column, bool p_is_default) {
sort_button->set_visible(true);
bool is_first_item = sort_items.is_empty();
SortItem item_ascending(sort_items.size(), vformat(TTR("Sort By %s (Ascending)"), p_new_option), p_sort_type, true, p_sort_column);
sort_items[item_ascending.id] = item_ascending;
sort_button->get_popup()->add_radio_check_item(item_ascending.label, item_ascending.id);

SortItem item_descending(sort_items.size(), vformat(TTR("Sort By %s (Descending)"), p_new_option), p_sort_type, false, p_sort_column);
sort_items[item_descending.id] = item_descending;
sort_button->get_popup()->add_radio_check_item(item_descending.label, item_descending.id);

if (is_first_item) {
sort_button->get_popup()->set_item_checked(0, true);
}

SortOptionIndexes indexes;
indexes.ascending = item_ascending.id;
indexes.descending = item_descending.id;
return indexes;
}

void TreeSortAndFilterBar::clear_filter() {
filter_edit->clear();
}

void TreeSortAndFilterBar::clear() {
sort_button->set_visible(false);
sort_button->get_popup()->clear();
filter_edit->clear();
}

void TreeSortAndFilterBar::select_sort(int p_item_id) {
_sort_changed(p_item_id);
}

void TreeSortAndFilterBar::apply() {
if (!managed_tree || !managed_tree->get_root()) {
return;
}

_apply_sort();
_apply_filter();
}

void TreeSortAndFilterBar::set_tree(Tree *p_managed_tree) {
if (managed_tree == p_managed_tree) {
return;
}
managed_tree = p_managed_tree;
apply();
}
111 changes: 111 additions & 0 deletions editor/gui/tree_sort_and_filter_line_edit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**************************************************************************/
/* tree_sort_and_filter_line_edit.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "scene/gui/box_container.h"
#include "scene/gui/tree.h"

class LineEdit;
class MenuButton;

class TreeSortAndFilterBar : public HBoxContainer {
GDCLASS(TreeSortAndFilterBar, HBoxContainer);

public:
// The ways a column can be sorted, either alphabetically or numerically.
enum SortType {
NUMERIC_SORT = 0,
ALPHA_SORT,
SORT_TYPE_MAX
};

// Returned when a new sort is added. Each new sort can be either ascending or descending,
// so we return the index of each sort option.
struct SortOptionIndexes {
int ascending;
int descending;
};

protected:
// Context needed to sort the tree in a certain way.
// Combines a sort type, the column to apply it, and if it's ascending or descending.
struct SortItem {
SortItem() {}
SortItem(int p_id, const String &p_label, SortType p_type, bool p_ascending, int p_column) :
id(p_id), label(p_label), type(p_type), ascending(p_ascending), column(p_column) {}
int id = 0;
String label;
SortType type = SortType::NUMERIC_SORT;
bool ascending = false;
int column = 0;
};

struct TreeItemColumn {
TreeItemColumn() {}
TreeItemColumn(TreeItem *p_item, int p_column) :
item(p_item), column(p_column) {}
TreeItem *item = nullptr;
int column;
};

struct TreeItemAlphaComparator {
bool operator()(const TreeItemColumn &p_a, const TreeItemColumn &p_b) const {
return NoCaseComparator()(p_a.item->get_text(p_a.column), p_b.item->get_text(p_b.column));
}
};

struct TreeItemNumericComparator {
bool operator()(const TreeItemColumn &p_a, const TreeItemColumn &p_b) const {
return p_a.item->get_text(p_a.column).to_int() < p_b.item->get_text(p_b.column).to_int();
}
};

Tree *managed_tree = nullptr;
LineEdit *filter_edit = nullptr;
MenuButton *sort_button = nullptr;
HashMap<int, SortItem> sort_items;
int current_sort = 0;

void _apply_filter(TreeItem *p_current_node = nullptr);
void _apply_sort();
void _sort_changed(int p_id);
void _filter_changed(const String &p_filter);

public:
TreeSortAndFilterBar(Tree *p_managed_tree, const String &p_filter_placeholder_text);
void _notification(int p_what);
void set_tree(Tree *p_managed_tree);
SortOptionIndexes add_sort_option(const String &p_new_option, SortType p_sort_type, int p_sort_column, bool p_is_default = false);
void clear_filter();
void clear();
void select_sort(int p_item_id);
void apply();
};
Loading