diff --git a/changelog.md b/changelog.md index c417cdc9f..17825bb8a 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ TGUI 1.2 (TBD) - Added Theme::replace function - Added TreeView::changeItem function +- Added TreeView::getNode function - Added ignoreMouseEvents function to canvas widgets - Replaced getWidgetAtPosition with getWidgetAtPos - getWidgetBelowMouseCursor was given a parameter for recursive search diff --git a/include/TGUI/Widgets/TreeView.hpp b/include/TGUI/Widgets/TreeView.hpp index 6b7adb7e9..19d203f3d 100644 --- a/include/TGUI/Widgets/TreeView.hpp +++ b/include/TGUI/Widgets/TreeView.hpp @@ -48,10 +48,10 @@ TGUI_MODULE_EXPORT namespace tgui static constexpr const char StaticWidgetType[] = "TreeView"; //!< Type name of the widget - /// @brief Read-only node representation used by getNodes + /// @brief Read-only node representation used by getNode and getNodes struct ConstNode { - bool expanded; + bool expanded = true; String text; std::vector nodes; }; @@ -244,6 +244,21 @@ TGUI_MODULE_EXPORT namespace tgui TGUI_NODISCARD std::vector getSelectedItem() const; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Returns the node in the tree view at a given hierarchy + /// + /// @param hierarchy Hierarchy of items, identifying the node to retrieve + /// + /// @return Node that was identified by the hierarchy + /// + /// If no node exists at the given hierarchy, the returned node will have an empty text and no children. + /// If the node was found, its text property will always match with the last element of the hierarchy. + /// + /// @since TGUI 1.2 + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TGUI_NODISCARD ConstNode getNode(const std::vector& hierarchy) const; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the nodes in the tree view /// @return List of nodes diff --git a/src/Widgets/TreeView.cpp b/src/Widgets/TreeView.cpp index 45edb7ce9..9be4de1c5 100644 --- a/src/Widgets/TreeView.cpp +++ b/src/Widgets/TreeView.cpp @@ -598,6 +598,25 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TreeView::ConstNode TreeView::getNode(const std::vector& hierarchy) const + { + TreeView::ConstNode constNode; + + if (hierarchy.empty()) + return constNode; + + auto* node = findNode(m_nodes, hierarchy, 0); + if (!node) + return constNode; + + constNode.expanded = node->expanded; + constNode.text = node->text.getString(); + constNode.nodes = convertNodesToConstNodes(node->nodes); + return constNode; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + std::vector TreeView::getNodes() const { return convertNodesToConstNodes(m_nodes); diff --git a/tests/Widgets/TreeView.cpp b/tests/Widgets/TreeView.cpp index 85e64056f..c9b823afb 100644 --- a/tests/Widgets/TreeView.cpp +++ b/tests/Widgets/TreeView.cpp @@ -122,6 +122,21 @@ TEST_CASE("[TreeView]") REQUIRE(treeView->getNodes()[1].nodes[1].nodes.size() == 2); REQUIRE(treeView->getNodes()[1].nodes[1].nodes[0].text == "Truck"); REQUIRE(treeView->getNodes()[1].nodes[1].nodes[1].text == "Car"); + + REQUIRE(treeView->getNode({"Smilies"}).text == "Smilies"); + REQUIRE(treeView->getNode({"Smilies"}).nodes.size() == 3); + REQUIRE(treeView->getNode({"Smilies", "Neither"}).text == "Neither"); + REQUIRE(treeView->getNode({"Smilies", "Neither"}).nodes.empty()); + REQUIRE(treeView->getNode({"Vehicles", "Whole"}).text == "Whole"); + REQUIRE(treeView->getNode({"Vehicles", "Whole"}).nodes.size() == 2); + REQUIRE(treeView->getNode({"Vehicles", "Whole"}).nodes[0].text == "Truck"); + REQUIRE(treeView->getNode({"Vehicles", "Whole"}).nodes[1].text == "Car"); + + // Testing invalid getNode calls + REQUIRE(treeView->getNode({}).text == ""); + REQUIRE(treeView->getNode({}).nodes.empty()); + REQUIRE(treeView->getNode({"Vehicles", "Train"}).text == ""); + REQUIRE(treeView->getNode({"Vehicles", "Train"}).nodes.empty()); } SECTION("Changing items")