diff --git a/cpp/open3d/visualization/gui/TreeView.cpp b/cpp/open3d/visualization/gui/TreeView.cpp index 72fca10bd0e..c6980230c46 100644 --- a/cpp/open3d/visualization/gui/TreeView.cpp +++ b/cpp/open3d/visualization/gui/TreeView.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -212,6 +213,7 @@ struct TreeView::Impl { std::shared_ptr cell; Item *parent = nullptr; std::list children; + std::optional expanded; }; int id_; Item root_; @@ -361,6 +363,21 @@ void TreeView::Layout(const LayoutContext &context) { // to defer layout to Draw(). } +void TreeView::Expand(ItemId id) { + auto it = impl_->id2item_.find(id); + if (it != impl_->id2item_.end()) { + it->second->expanded = true; + } + // Invalidate(); +} + +void TreeView::Collapse(ItemId id) { + auto it = impl_->id2item_.find(id); + if (it != impl_->id2item_.end()) { + it->second->expanded = false; + } +} + Widget::DrawResult TreeView::Draw(const DrawContext &context) { auto result = Widget::DrawResult::NONE; auto &frame = GetFrame(); @@ -417,8 +434,12 @@ Widget::DrawResult TreeView::Draw(const DrawContext &context) { colorToImguiRGBA(context.theme.tree_selected_color)); } - int flags = ImGuiTreeNodeFlags_DefaultOpen | - ImGuiTreeNodeFlags_AllowItemOverlap; + int flags = ImGuiTreeNodeFlags_AllowItemOverlap; + bool expanded = item.expanded.value_or(true); + + if (expanded) { + flags |= ImGuiTreeNodeFlags_DefaultOpen; + } if (impl_->can_select_parents_) { flags |= ImGuiTreeNodeFlags_OpenOnDoubleClick; flags |= ImGuiTreeNodeFlags_OpenOnArrow; @@ -465,7 +486,10 @@ Widget::DrawResult TreeView::Draw(const DrawContext &context) { } }; - if (ImGui::TreeNodeEx(item.id_string.c_str(), flags, "%s", "")) { + bool open = ImGui::TreeNodeEx(item.id_string.c_str(), flags, "%s", ""); + item.expanded = open; + + if (open) { DrawThis(item, height, is_selectable); for (auto &child : item.children) { diff --git a/cpp/open3d/visualization/gui/TreeView.h b/cpp/open3d/visualization/gui/TreeView.h index 956f896149e..64d0e88fcce 100644 --- a/cpp/open3d/visualization/gui/TreeView.h +++ b/cpp/open3d/visualization/gui/TreeView.h @@ -134,6 +134,9 @@ class TreeView : public Widget { void SetOnSelectionChanged( std::function on_selection_changed); + void Expand(ItemId); + void Collapse(ItemId); + private: struct Impl; std::unique_ptr impl_; diff --git a/cpp/pybind/t/geometry/pointcloud.cpp b/cpp/pybind/t/geometry/pointcloud.cpp index f872b9fcd68..b343debe08c 100644 --- a/cpp/pybind/t/geometry/pointcloud.cpp +++ b/cpp/pybind/t/geometry/pointcloud.cpp @@ -261,9 +261,10 @@ void pybind_pointcloud_definitions(py::module& m) { "non-negative number less than number of points in the " "input pointcloud.", "start_index"_a = 0); - pointcloud.def("remove_radius_outliers", &PointCloud::RemoveRadiusOutliers, - "nb_points"_a, "search_radius"_a, - R"(Remove points that have less than nb_points neighbors in a + pointcloud.def( + "remove_radius_outliers", &PointCloud::RemoveRadiusOutliers, + "nb_points"_a, "search_radius"_a, + R"(Remove points that have less than nb_points neighbors in a sphere of a given search radius. Args: diff --git a/cpp/pybind/visualization/gui/gui.cpp b/cpp/pybind/visualization/gui/gui.cpp index 2d92297062f..e39b7c3d2db 100644 --- a/cpp/pybind/visualization/gui/gui.cpp +++ b/cpp/pybind/visualization/gui/gui.cpp @@ -1665,9 +1665,13 @@ void pybind_gui_definitions(py::module &m) { .def("set_on_selection_changed", &TreeView::SetOnSelectionChanged, "Sets f(new_item_id) which is called when the user " "changes the selection."); + .def("expand", &TreeView::Expand, "Expand (open) the given TreeView item.") - // ---- TreeView cells ---- - auto checkable_cell = static_cast< + .def("collapse", &TreeView::Collapse, + "Collapse (close) the given TreeView item.") + + // ---- TreeView cells ---- + auto checkable_cell = static_cast< py::class_, Widget>>( m_gui.attr("CheckableTextTreeCell")); diff --git a/cpp/tests/visualization/CMakeLists.txt b/cpp/tests/visualization/CMakeLists.txt index 1d216c0591f..dbee3c13870 100644 --- a/cpp/tests/visualization/CMakeLists.txt +++ b/cpp/tests/visualization/CMakeLists.txt @@ -3,3 +3,17 @@ if (BUILD_GUI) rendering/MaterialModifier.cpp ) endif() + + +add_executable(TreeViewTest + TreeViewTest.cpp +) + +target_link_libraries(TreeViewTest + PRIVATE + Open3D::Open3D +) + +set_target_properties(TreeViewTest PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +)