--- --- TGUI: include/TGUI/Widgets/TreeView.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
TreeView.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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/CopiedSharedPtr.hpp>
29#include <TGUI/Widgets/Scrollbar.hpp>
30#include <TGUI/Renderers/TreeViewRenderer.hpp>
31#include <TGUI/Text.hpp>
32
34
35TGUI_MODULE_EXPORT namespace tgui
36{
40 class TGUI_API TreeView : public Widget
41 {
42 public:
43
44 using Ptr = std::shared_ptr<TreeView>;
45 using ConstPtr = std::shared_ptr<const TreeView>;
46
47 static constexpr const char StaticWidgetType[] = "TreeView";
48
50 struct ConstNode
51 {
52 bool expanded = true;
53 String text;
54 std::vector<ConstNode> nodes;
55 };
56
58 struct Node
59 {
60 Text text;
61 unsigned int depth = 0;
62 bool expanded = true;
63 Node* parent = nullptr;
64 std::vector<std::shared_ptr<Node>> nodes;
65 };
66
74 TreeView(const char* typeName = StaticWidgetType, bool initRenderer = true);
75
77 // Copy constructor
79 TreeView(const TreeView&);
80
82 // Move constructor
84 TreeView(TreeView&&) = default;
85
87 // Overload of copy assignment operator
89 TreeView& operator=(const TreeView&);
90
92 // Overload of move assignment operator
94 TreeView& operator=(TreeView&&) = default;
95
100 TGUI_NODISCARD static TreeView::Ptr create();
101
107 TGUI_NODISCARD static TreeView::Ptr copy(const TreeView::ConstPtr& treeView);
108
113 TGUI_NODISCARD TreeViewRenderer* getSharedRenderer() override;
114 TGUI_NODISCARD const TreeViewRenderer* getSharedRenderer() const override;
115
121 TGUI_NODISCARD TreeViewRenderer* getRenderer() override;
122
127 void setSize(const Layout2d& size) override;
128 using Widget::setSize;
129
148 bool addItem(const std::vector<String>& hierarchy, bool createParents = true);
149
165 bool changeItem(const std::vector<String>& hierarchy, const String& leafText);
166
172 void expand(const std::vector<String>& hierarchy);
173
177 void expandAll();
178
184 void collapse(const std::vector<String>& hierarchy);
185
190
198 bool selectItem(const std::vector<String>& hierarchy);
199
204
213 bool removeItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
214
219
224 TGUI_NODISCARD std::vector<String> getSelectedItem() const;
225
238 TGUI_NODISCARD ConstNode getNode(const std::vector<String>& hierarchy) const;
239
244 TGUI_NODISCARD std::vector<ConstNode> getNodes() const;
245
250 void setItemHeight(unsigned int itemHeight);
251
256 TGUI_NODISCARD unsigned int getItemHeight() const;
257
263 void setVerticalScrollbarValue(unsigned int value);
264
270 TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
271
279 TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const;
280
286 void setHorizontalScrollbarValue(unsigned int value);
287
293 TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
294
302 TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const;
303
309 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
310
314 bool leftMousePressed(Vector2f pos) override;
315
319 void leftMouseReleased(Vector2f pos) override;
320
324 void rightMousePressed(Vector2f pos) override;
325
329 void mouseMoved(Vector2f pos) override;
330
334 bool scrolled(float delta, Vector2f pos, bool touch) override;
335
339 void mouseNoLongerOnWidget() override;
340
344 void leftMouseButtonNoLongerDown() override;
345
349 void keyPressed(const Event::KeyEvent& event) override;
350
360 bool canHandleKeyPress(const Event::KeyEvent& event) override;
361
365 void markNodesDirty();
366
373 void draw(BackendRenderTarget& target, RenderStates states) const override;
374
376 protected:
377
387 TGUI_NODISCARD Signal& getSignal(String signalName) override;
388
394 void rendererChanged(const String& property) override;
395
399 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
400
404 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
405
409 void updateTextSize() override;
410
412 // Returns the size without the borders
414 TGUI_NODISCARD Vector2f getInnerSize() const;
415
417 // Updates the bounds of the icons
419 void updateIconBounds();
420
422 // This function is called every frame with the time passed since the last frame.
424 bool updateTime(Duration elapsedTime) override;
425
427 // Makes a copy of the widget
429 TGUI_NODISCARD Widget::Ptr clone() const override;
430
434 void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
435
439 void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const String& text);
440
444 TGUI_NODISCARD Node* findParentNode(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
445
447 // Expands or collapses one of the visible items
449 void toggleNodeInternal(std::size_t index);
450
452 // Expands or collapses a node
454 bool expandOrCollapse(const std::vector<String>& hierarchy, bool expand);
455
457 // Helper function to load the items from a text file
459 void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
460
462 // Rebuilds the list of visible items and positions the texts
464 unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
465
467 // Updates the text colors of the selected and hovered items
469 void updateSelectedAndHoveringItemColors();
470
472 // Updates the text color of the hovered item
474 void updateHoveredItem(int item);
475
477 // Updates the text color of the selected item
479 void updateSelectedItem(int item);
480
482 public:
483
484 SignalItemHierarchy onItemSelect = {"ItemSelected"};
485 SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
486 SignalItemHierarchy onExpand = {"Expanded"};
487 SignalItemHierarchy onCollapse = {"Collapsed"};
488 SignalItemHierarchy onRightClick = {"RightClicked"};
489
491 protected:
492
493 // This contains the nodes of the tree
494 std::vector<std::shared_ptr<Node>> m_nodes;
495 std::vector<std::shared_ptr<Node>> m_visibleNodes;
496
497 int m_selectedItem = -1;
498 int m_hoveredItem = -1;
499
500 unsigned int m_itemHeight = 0;
501 float m_maxRight = 0;
502
503 Vector2f m_iconBounds;
504
505 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
506 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
507
508 bool m_possibleDoubleClick = false;
509 int m_doubleClickNodeIndex = -1;
510
511 Sprite m_spriteBackground;
512 Sprite m_spriteBranchExpanded;
513 Sprite m_spriteBranchCollapsed;
514 Sprite m_spriteLeaf;
515
516 // Cached renderer properties
517 Borders m_bordersCached;
518 Borders m_paddingCached;
519 Color m_borderColorCached;
520 Color m_backgroundColorCached;
521 Color m_textColorCached;
522 Color m_textColorHoverCached;
523 Color m_selectedTextColorCached;
524 Color m_selectedTextColorHoverCached;
525 Color m_selectedBackgroundColorCached;
526 Color m_selectedBackgroundColorHoverCached;
527 Color m_backgroundColorHoverCached;
528 TextStyles m_textStyleCached;
529
531 };
532
534}
535
537
538#endif // TGUI_TREE_VIEW_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Definition CopiedSharedPtr.hpp:42
Wrapper for durations.
Definition Duration.hpp:55
Class to store the position or size of a widget.
Definition Layout.hpp:313
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1060
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
Wrapper for text styles.
Definition TextStyle.hpp:55
Tree view widget.
Definition TreeView.hpp:41
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
TGUI_NODISCARD TreeViewRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD ConstNode getNode(const std::vector< String > &hierarchy) const
Returns the node in the tree view at a given hierarchy.
void deselectItem()
Deselect the item if one was selected.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
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)
static TGUI_NODISCARD TreeView::Ptr create()
Creates a new tree view widget.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
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.
TGUI_NODISCARD std::vector< String > getSelectedItem() const
Returns the selected item.
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...
TGUI_NODISCARD 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.
TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const
Returns the maximum thumb position of the horizontal scrollbar.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
bool removeItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes an item.
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.
TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const
Returns the maximum thumb position of the vertical scrollbar.
static TGUI_NODISCARD TreeView::Ptr copy(const TreeView::ConstPtr &treeView)
Makes a copy of another tree view.
TGUI_NODISCARD TreeViewRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD unsigned int getItemHeight() const
Returns the height of the items in the tree view.
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.
TGUI_NODISCARD std::vector< ConstNode > getNodes() const
Returns the nodes in the tree view.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setSize(const Layout2d &size) override
Changes the size of the tree view.
Definition TreeViewRenderer.hpp:35
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
KeyPressed event parameters.
Definition Event.hpp:168
States used for drawing.
Definition RenderStates.hpp:38
Read-only node representation used by getNode and getNodes.
Definition TreeView.hpp:51
Internal representation of a node.
Definition TreeView.hpp:59