25#ifndef TGUI_TREE_VIEW_HPP
26#define TGUI_TREE_VIEW_HPP
28#include <TGUI/Renderers/TreeViewRenderer.hpp>
29#include <TGUI/Text.hpp>
30#include <TGUI/Widgets/Scrollbar.hpp>
42 using Ptr = std::shared_ptr<TreeView>;
43 using ConstPtr = std::shared_ptr<const TreeView>;
52 std::vector<ConstNode> nodes;
59 unsigned int depth = 0;
61 Node* parent =
nullptr;
62 std::vector<std::shared_ptr<Node>> nodes;
72 explicit TreeView(
const char* typeName =
StaticWidgetType,
bool initRenderer =
true);
77 TreeView(
const TreeView& other);
82 TreeView(TreeView&&) =
default;
87 TreeView& operator=(
const TreeView& other);
92 TreeView& operator=(TreeView&&) =
default;
98 [[nodiscard]]
static TreeView::Ptr
create();
105 [[nodiscard]]
static TreeView::Ptr
copy(
const TreeView::ConstPtr& treeView);
146 bool addItem(
const std::vector<String>& hierarchy,
bool createParents =
true);
170 void expand(
const std::vector<String>& hierarchy);
182 void collapse(
const std::vector<String>& hierarchy);
211 bool removeItem(
const std::vector<String>& hierarchy,
bool removeParentsWhenEmpty =
true);
299 bool changeItemHierarchy(
const std::vector<String>& oldHierarchy,
const std::vector<String>& newHierarchy);
319 [[nodiscard]] std::vector<ConstNode>
getNodes()
const;
345 TGUI_DEPRECATED(
"Use getVerticalScrollbar()->getValue() instead")
355 TGUI_DEPRECATED(
"Use getVerticalScrollbar()->getMaxValue() instead")
363 TGUI_DEPRECATED(
"Use getHorizontalScrollbar()->setValue(value) instead")
371 TGUI_DEPRECATED(
"Use getHorizontalScrollbar()->getValue() instead")
381 TGUI_DEPRECATED(
"Use getHorizontalScrollbar()->getMaxValue() instead")
399 void leftMouseReleased(Vector2f pos) override;
404 void rightMousePressed(Vector2f pos) override;
409 void mouseMoved(Vector2f pos) override;
414 bool scrolled(
float delta, Vector2f pos,
bool touch) override;
419 void mouseNoLongerOnWidget() override;
424 void leftMouseButtonNoLongerDown() override;
429 void keyPressed(const
Event::KeyEvent& event) override;
445 void markNodesDirty();
479 [[nodiscard]] std::unique_ptr<DataIO::
Node>
save(SavingRenderersMap& renderers) const override;
484 void load(const std::unique_ptr<DataIO::
Node>& node, const LoadingRenderersMap& renderers) override;
502 [[nodiscard]] Vector2f getInnerSize() const;
507 void updateIconBounds();
512 bool updateTime(
Duration elapsedTime) override;
517 [[nodiscard]] Widget::Ptr
clone() const override;
522 void updateTextColors(std::vector<std::shared_ptr<
Node>>& nodes);
527 void createNode(std::vector<std::shared_ptr<
Node>>& nodes,
Node* parent, const
String& text);
532 [[nodiscard]]
Node* findParentNode(const std::vector<
String>& hierarchy,
533 unsigned int parentIndex,
534 std::vector<std::shared_ptr<
Node>>& nodes,
541 void toggleNodeInternal(std::
size_t index);
546 bool expandOrCollapse(const std::vector<
String>& hierarchy,
bool expand);
551 void loadItems(const std::unique_ptr<DataIO::
Node>& node, std::vector<std::shared_ptr<
Node>>& items,
Node* parent);
556 unsigned int updateVisibleNodes(std::vector<std::shared_ptr<
Node>>& nodes,
Node* selectedNode,
float textPadding,
unsigned int pos);
561 void updateSelectedAndHoveringItemColors();
566 void updateHoveredItem(
int item);
571 void updateSelectedItem(
int item);
587 std::vector<std::shared_ptr<Node>> m_nodes;
588 std::vector<std::shared_ptr<Node>> m_visibleNodes;
590 int m_selectedItem = -1;
591 int m_hoveredItem = -1;
593 unsigned int m_itemHeight = 0;
594 float m_maxRight = 0;
596 Vector2f m_iconBounds;
598 bool m_possibleDoubleClick =
false;
599 int m_doubleClickNodeIndex = -1;
601 Sprite m_spriteBackground;
602 Sprite m_spriteBranchExpanded;
603 Sprite m_spriteBranchCollapsed;
607 Borders m_bordersCached;
608 Borders m_paddingCached;
609 Color m_borderColorCached;
610 Color m_backgroundColorCached;
611 Color m_textColorCached;
612 Color m_textColorHoverCached;
613 Color m_selectedTextColorCached;
614 Color m_selectedTextColorHoverCached;
615 Color m_selectedBackgroundColorCached;
616 Color m_selectedBackgroundColorHoverCached;
617 Color m_backgroundColorHoverCached;
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
Wrapper for durations.
Definition Duration.hpp:52
Class to store the position or size of a widget.
Definition Layout.hpp:320
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1094
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
Definition TreeViewRenderer.hpp:35
SignalItemHierarchy onCollapse
A branch node was collapsed in the tree view. Optional parameter: collapsed node.
Definition TreeView.hpp:579
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
SignalItemHierarchy onExpand
A branch node was expanded in the tree view. Optional parameter: expanded node.
Definition TreeView.hpp:578
void deselectItem()
Deselect the item if one was selected.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
unsigned int getVerticalScrollbarMaxValue() const
Returns the maximum thumb position of the vertical scrollbar.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
unsigned int getHorizontalScrollbarMaxValue() const
Returns the maximum thumb position of the horizontal scrollbar.
SignalItemHierarchy onDoubleClick
A leaf node was double clicked. Optional parameter: selected node.
Definition TreeView.hpp:577
bool addItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new item to the tree view.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
unsigned int getItemHeight() const
Returns the height of the items in the tree view.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void scrollbarPolicyChanged(Orientation orientation) override
Called when the policy of one of the scrollbars has been changed calling either getVerticalScrollbar(...
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition TreeView.hpp:45
std::vector< ConstNode > getNodes() const
Returns the nodes in the tree view.
void collapseAll()
Collapses all items.
void collapse(const std::vector< String > &hierarchy)
Collapses the given item.
bool changeItem(const std::vector< String > &hierarchy, const String &leafText)
Changes the text of a leaf item.
bool selectItem(const std::vector< String > &hierarchy)
Selects an item in the tree view.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
TreeViewRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
ConstNode getNode(const std::vector< String > &hierarchy) const
Returns the node in the tree view at a given hierarchy.
SignalItemHierarchy onRightClick
Right mouse button was pressed on top of a node. Optional parameter: node below mouse.
Definition TreeView.hpp:580
bool removeItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes an item.
TreeViewRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setItemHeight(unsigned int itemHeight)
Changes the height of the items in the tree view.
void expandAll()
Expands all items.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
void expand(const std::vector< String > &hierarchy)
Expands the given item.
void removeAllItems()
Removes all items.
bool setItemIndexInParent(const std::vector< String > &hierarchy, std::size_t index)
Sets the index of an item, based on the items that share the same parent.
std::vector< String > getHoveredItem() const
Returns the item currently below the mouse cursor.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
std::vector< String > getSelectedItem() const
Returns the selected item.
static TreeView::Ptr copy(const TreeView::ConstPtr &treeView)
Makes a copy of another tree view.
SignalItemHierarchy onItemSelect
An node was selected in the tree view. Optional parameter: selected node.
Definition TreeView.hpp:576
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
static TreeView::Ptr create()
Creates a new tree view widget.
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
int getItemIndexInParent(const std::vector< String > &hierarchy) const
Returns the index of an item, based on the items that share the same parent.
bool changeItemHierarchy(const std::vector< String > &oldHierarchy, const std::vector< String > &newHierarchy)
Moves one item and its children from one location in the hierarchy to a completely different location...
void setSize(const Layout2d &size) override
Changes the size of the tree view.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Orientation
Orientation of the object.
Definition Layout.hpp:50
States used for drawing.
Definition RenderStates.hpp:38
Read-only node representation used by getNode and getNodes.
Definition TreeView.hpp:49
Internal representation of a node.
Definition TreeView.hpp:57