Skip to content

Commit

Permalink
Added getNode function to TreeView to only retrieve part of the hiera…
Browse files Browse the repository at this point in the history
…rchy that would be returned by getNodes
  • Loading branch information
texus committed Jan 13, 2024
1 parent eb2d154 commit 50080c0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions include/TGUI/Widgets/TreeView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConstNode> nodes;
};
Expand Down Expand Up @@ -244,6 +244,21 @@ TGUI_MODULE_EXPORT namespace tgui
TGUI_NODISCARD std::vector<String> 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<String>& hierarchy) const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the nodes in the tree view
/// @return List of nodes
Expand Down
19 changes: 19 additions & 0 deletions src/Widgets/TreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,25 @@ namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TreeView::ConstNode TreeView::getNode(const std::vector<String>& 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::ConstNode> TreeView::getNodes() const
{
return convertNodesToConstNodes(m_nodes);
Expand Down
15 changes: 15 additions & 0 deletions tests/Widgets/TreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 50080c0

Please sign in to comment.