TGUI 1.13
Loading...
Searching...
No Matches
TreeView.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_TREE_VIEW_HPP
26#define TGUI_TREE_VIEW_HPP
27
28#include <TGUI/Renderers/TreeViewRenderer.hpp>
29#include <TGUI/Text.hpp>
30#include <TGUI/Widgets/Scrollbar.hpp>
31
33
34namespace tgui
35{
39 class TGUI_API TreeView : public Widget, public DualScrollbarChildInterface
40 {
41 public:
42 using Ptr = std::shared_ptr<TreeView>;
43 using ConstPtr = std::shared_ptr<const TreeView>;
44
45 static constexpr char StaticWidgetType[] = "TreeView";
46
48 struct ConstNode
49 {
50 bool expanded = true;
51 String text;
52 std::vector<ConstNode> nodes;
53 };
54
56 struct Node
57 {
58 Text text;
59 unsigned int depth = 0;
60 bool expanded = true;
61 Node* parent = nullptr;
62 std::vector<std::shared_ptr<Node>> nodes;
63 };
64
72 explicit TreeView(const char* typeName = StaticWidgetType, bool initRenderer = true);
73
75 // Copy constructor
77 TreeView(const TreeView& other);
78
80 // Move constructor
82 TreeView(TreeView&&) = default;
83
85 // Overload of copy assignment operator
87 TreeView& operator=(const TreeView& other);
88
90 // Overload of move assignment operator
92 TreeView& operator=(TreeView&&) = default;
93
98 [[nodiscard]] static TreeView::Ptr create();
99
105 [[nodiscard]] static TreeView::Ptr copy(const TreeView::ConstPtr& treeView);
106
111 [[nodiscard]] TreeViewRenderer* getSharedRenderer() override;
112 [[nodiscard]] const TreeViewRenderer* getSharedRenderer() const override;
113
119 [[nodiscard]] TreeViewRenderer* getRenderer() override;
120
125 void setSize(const Layout2d& size) override;
126 using Widget::setSize;
127
146 bool addItem(const std::vector<String>& hierarchy, bool createParents = true);
147
163 bool changeItem(const std::vector<String>& hierarchy, const String& leafText);
164
170 void expand(const std::vector<String>& hierarchy);
171
175 void expandAll();
176
182 void collapse(const std::vector<String>& hierarchy);
183
188
196 bool selectItem(const std::vector<String>& hierarchy);
197
202
211 bool removeItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
212
217
222 [[nodiscard]] std::vector<String> getSelectedItem() const;
223
229 [[nodiscard]] std::vector<String> getHoveredItem() const;
230
245 bool setItemIndexInParent(const std::vector<String>& hierarchy, std::size_t index);
246
266 int getItemIndexInParent(const std::vector<String>& hierarchy) const;
267
299 bool changeItemHierarchy(const std::vector<String>& oldHierarchy, const std::vector<String>& newHierarchy);
300
313 [[nodiscard]] ConstNode getNode(const std::vector<String>& hierarchy) const;
314
319 [[nodiscard]] std::vector<ConstNode> getNodes() const;
320
325 void setItemHeight(unsigned int itemHeight);
326
331 [[nodiscard]] unsigned int getItemHeight() const;
332
338 TGUI_DEPRECATED("Use getVerticalScrollbar()->setValue(value) instead") void setVerticalScrollbarValue(unsigned int value);
339
345 TGUI_DEPRECATED("Use getVerticalScrollbar()->getValue() instead")
346 [[nodiscard]] unsigned int getVerticalScrollbarValue() const;
347
355 TGUI_DEPRECATED("Use getVerticalScrollbar()->getMaxValue() instead")
356 [[nodiscard]] unsigned int getVerticalScrollbarMaxValue() const;
357
363 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setValue(value) instead")
364 void setHorizontalScrollbarValue(unsigned int value);
365
371 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getValue() instead")
372 [[nodiscard]] unsigned int getHorizontalScrollbarValue() const;
373
381 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getMaxValue() instead")
382 [[nodiscard]] unsigned int getHorizontalScrollbarMaxValue() const;
383
389 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
390
394 bool leftMousePressed(Vector2f pos) override;
395
399 void leftMouseReleased(Vector2f pos) override;
400
404 void rightMousePressed(Vector2f pos) override;
405
409 void mouseMoved(Vector2f pos) override;
410
414 bool scrolled(float delta, Vector2f pos, bool touch) override;
415
419 void mouseNoLongerOnWidget() override;
420
424 void leftMouseButtonNoLongerDown() override;
425
429 void keyPressed(const Event::KeyEvent& event) override;
430
440 bool canHandleKeyPress(const Event::KeyEvent& event) override;
441
445 void markNodesDirty();
446
453 void draw(BackendRenderTarget& target, RenderStates states) const override;
454
456
457 protected:
467 [[nodiscard]] Signal& getSignal(String signalName) override;
468
474 void rendererChanged(const String& property) override;
475
479 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
480
484 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
485
489 void updateTextSize() override;
490
497 void scrollbarPolicyChanged(Orientation orientation) override;
498
500 // Returns the size without the borders
502 [[nodiscard]] Vector2f getInnerSize() const;
503
505 // Updates the bounds of the icons
507 void updateIconBounds();
508
510 // This function is called every frame with the time passed since the last frame.
512 bool updateTime(Duration elapsedTime) override;
513
515 // Makes a copy of the widget
517 [[nodiscard]] Widget::Ptr clone() const override;
518
522 void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
523
527 void createNode(std::vector<std::shared_ptr<Node>>& nodes, Node* parent, const String& text);
528
532 [[nodiscard]] Node* findParentNode(const std::vector<String>& hierarchy,
533 unsigned int parentIndex,
534 std::vector<std::shared_ptr<Node>>& nodes,
535 Node* parent,
536 bool createParents);
537
539 // Expands or collapses one of the visible items
541 void toggleNodeInternal(std::size_t index);
542
544 // Expands or collapses a node
546 bool expandOrCollapse(const std::vector<String>& hierarchy, bool expand);
547
549 // Helper function to load the items from a text file
551 void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
552
554 // Rebuilds the list of visible items and positions the texts
556 unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
557
559 // Updates the text colors of the selected and hovered items
561 void updateSelectedAndHoveringItemColors();
562
564 // Updates the text color of the hovered item
566 void updateHoveredItem(int item);
567
569 // Updates the text color of the selected item
571 void updateSelectedItem(int item);
572
574
575 public:
577 SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
581 "RightClicked"};
582
584
585 protected:
586 // This contains the nodes of the tree
587 std::vector<std::shared_ptr<Node>> m_nodes;
588 std::vector<std::shared_ptr<Node>> m_visibleNodes;
589
590 int m_selectedItem = -1;
591 int m_hoveredItem = -1;
592
593 unsigned int m_itemHeight = 0;
594 float m_maxRight = 0;
595
596 Vector2f m_iconBounds;
597
598 bool m_possibleDoubleClick = false;
599 int m_doubleClickNodeIndex = -1;
600
601 Sprite m_spriteBackground;
602 Sprite m_spriteBranchExpanded;
603 Sprite m_spriteBranchCollapsed;
604 Sprite m_spriteLeaf;
605
606 // Cached renderer properties
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;
618 TextStyles m_textStyleCached;
619 };
620} // namespace tgui
621
622#endif // TGUI_TREE_VIEW_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
DualScrollbarChildInterface()
Default constructor.
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
Definition Sprite.hpp:45
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.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Orientation
Orientation of the object.
Definition Layout.hpp:50
Definition Event.hpp:36
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