Path: blob/master/modules/objectdb_profiler/editor/data_viewers/shared_controls.cpp
11325 views
/**************************************************************************/1/* shared_controls.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "shared_controls.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/label.h"36#include "scene/gui/line_edit.h"37#include "scene/gui/menu_button.h"38#include "scene/resources/style_box_flat.h"3940SpanningHeader::SpanningHeader(const String &p_text) {41Ref<StyleBoxFlat> title_sbf;42title_sbf.instantiate();43title_sbf->set_bg_color(EditorNode::get_singleton()->get_editor_theme()->get_color("dark_color_3", "Editor"));44add_theme_style_override(SceneStringName(panel), title_sbf);45set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);46Label *title = memnew(Label(p_text));47add_child(title);48title->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);49title->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);50}5152DarkPanelContainer::DarkPanelContainer() {53set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);54set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);55Ref<StyleBoxFlat> content_wrapper_sbf;56content_wrapper_sbf.instantiate();57content_wrapper_sbf->set_bg_color(EditorNode::get_singleton()->get_editor_theme()->get_color("dark_color_2", "Editor"));58add_theme_style_override(SceneStringName(panel), content_wrapper_sbf);59}6061void TreeSortAndFilterBar::_apply_filter(TreeItem *p_current_node) {62if (!p_current_node) {63p_current_node = managed_tree->get_root();64}6566if (!p_current_node) {67return;68}6970// Reset ourselves to default state.71p_current_node->set_visible(true);72p_current_node->clear_custom_color(0);7374// Go through each child and filter them.75bool any_child_visible = false;76for (TreeItem *child = p_current_node->get_first_child(); child; child = child->get_next()) {77_apply_filter(child);78if (child->is_visible()) {79any_child_visible = true;80}81}8283// Check if we match the filter.84String filter_str = filter_edit->get_text().strip_edges(true, true).to_lower();8586// We are visible.87bool matches_filter = false;88for (int i = 0; i < managed_tree->get_columns(); i++) {89if (p_current_node->get_text(i).to_lower().contains(filter_str)) {90matches_filter = true;91break;92}93}94if (matches_filter || filter_str.is_empty()) {95p_current_node->set_visible(true);96} else if (any_child_visible) {97// We have a visible child.98p_current_node->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));99} else {100// We and our children are not visible.101p_current_node->set_visible(false);102}103}104105void TreeSortAndFilterBar::_apply_sort() {106if (!sort_button->is_visible()) {107return;108}109for (int i = 0; i != sort_button->get_popup()->get_item_count(); i++) {110// Update the popup buttons to be checked/unchecked.111sort_button->get_popup()->set_item_checked(i, (i == (int)current_sort));112}113114SortItem sort = sort_items[current_sort];115116List<TreeItem *> items_to_sort;117items_to_sort.push_back(managed_tree->get_root());118119while (items_to_sort.size() > 0) {120TreeItem *to_sort = items_to_sort.front()->get();121items_to_sort.pop_front();122123LocalVector<TreeItemColumn> items;124items.reserve(to_sort->get_child_count());125for (int i = 0; i < to_sort->get_child_count(); i++) {126items.push_back(TreeItemColumn(to_sort->get_child(i), sort.column));127}128129if (sort.type == ALPHA_SORT && sort.ascending == true) {130items.sort_custom<TreeItemAlphaComparator>();131}132if (sort.type == ALPHA_SORT && sort.ascending == false) {133items.sort_custom<TreeItemAlphaComparator>();134items.reverse();135}136if (sort.type == NUMERIC_SORT && sort.ascending == true) {137items.sort_custom<TreeItemNumericComparator>();138}139if (sort.type == NUMERIC_SORT && sort.ascending == false) {140items.sort_custom<TreeItemNumericComparator>();141items.reverse();142}143144TreeItem *previous = nullptr;145for (const TreeItemColumn &item : items) {146if (previous != nullptr) {147item.item->move_after(previous);148} else {149item.item->move_before(to_sort->get_first_child());150}151previous = item.item;152items_to_sort.push_back(item.item);153}154}155}156157void TreeSortAndFilterBar::_sort_changed(int p_id) {158current_sort = p_id;159_apply_sort();160}161162void TreeSortAndFilterBar::_filter_changed(const String &p_filter) {163_apply_filter();164}165166TreeSortAndFilterBar::TreeSortAndFilterBar(Tree *p_managed_tree, const String &p_filter_placeholder_text) :167managed_tree(p_managed_tree) {168set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);169add_theme_constant_override("h_separation", 10 * EDSCALE);170filter_edit = memnew(LineEdit);171filter_edit->set_clear_button_enabled(true);172filter_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);173filter_edit->set_placeholder(p_filter_placeholder_text);174add_child(filter_edit);175filter_edit->connect(SceneStringName(text_changed), callable_mp(this, &TreeSortAndFilterBar::_filter_changed));176177sort_button = memnew(MenuButton);178sort_button->set_visible(false);179sort_button->set_flat(false);180sort_button->set_theme_type_variation("FlatMenuButton");181PopupMenu *p = sort_button->get_popup();182p->connect(SceneStringName(id_pressed), callable_mp(this, &TreeSortAndFilterBar::_sort_changed));183184add_child(sort_button);185}186187void TreeSortAndFilterBar::_notification(int p_what) {188switch (p_what) {189case NOTIFICATION_POSTINITIALIZE:190case NOTIFICATION_THEME_CHANGED: {191filter_edit->set_right_icon(get_editor_theme_icon(SNAME("Search")));192sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));193apply();194} break;195}196}197198TreeSortAndFilterBar::SortOptionIndexes TreeSortAndFilterBar::add_sort_option(const String &p_new_option, SortType p_sort_type, int p_sort_column, bool p_is_default) {199sort_button->set_visible(true);200bool is_first_item = sort_items.is_empty();201SortItem item_ascending(sort_items.size(), vformat(TTRC("Sort By %s (Ascending)"), p_new_option), p_sort_type, true, p_sort_column);202sort_items[item_ascending.id] = item_ascending;203sort_button->get_popup()->add_radio_check_item(item_ascending.label, item_ascending.id);204205SortItem item_descending(sort_items.size(), vformat(TTRC("Sort By %s (Descending)"), p_new_option), p_sort_type, false, p_sort_column);206sort_items[item_descending.id] = item_descending;207sort_button->get_popup()->add_radio_check_item(item_descending.label, item_descending.id);208209if (is_first_item) {210sort_button->get_popup()->set_item_checked(0, true);211}212213SortOptionIndexes indexes;214indexes.ascending = item_ascending.id;215indexes.descending = item_descending.id;216return indexes;217}218219void TreeSortAndFilterBar::clear_filter() {220filter_edit->clear();221}222223void TreeSortAndFilterBar::clear() {224sort_button->set_visible(false);225sort_button->get_popup()->clear();226filter_edit->clear();227}228229void TreeSortAndFilterBar::select_sort(int p_item_id) {230_sort_changed(p_item_id);231}232233void TreeSortAndFilterBar::apply() {234if (!managed_tree || !managed_tree->get_root()) {235return;236}237238_apply_sort();239_apply_filter();240}241242243