--- --- 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/Widgets/Scrollbar.hpp>
29#include <TGUI/Renderers/TreeViewRenderer.hpp>
30#include <TGUI/Text.hpp>
31
33
34TGUI_MODULE_EXPORT namespace tgui
35{
39 class TGUI_API TreeView : public Widget, public DualScrollbarChildInterface
40 {
41 public:
42
43 using Ptr = std::shared_ptr<TreeView>;
44 using ConstPtr = std::shared_ptr<const TreeView>;
45
46 static constexpr const char StaticWidgetType[] = "TreeView";
47
49 struct ConstNode
50 {
51 bool expanded = true;
52 String text;
53 std::vector<ConstNode> nodes;
54 };
55
57 struct Node
58 {
59 Text text;
60 unsigned int depth = 0;
61 bool expanded = true;
62 Node* parent = nullptr;
63 std::vector<std::shared_ptr<Node>> nodes;
64 };
65
73 TreeView(const char* typeName = StaticWidgetType, bool initRenderer = true);
74
76 // Copy constructor
78 TreeView(const TreeView&);
79
81 // Move constructor
83 TreeView(TreeView&&) = default;
84
86 // Overload of copy assignment operator
88 TreeView& operator=(const TreeView&);
89
91 // Overload of move assignment operator
93 TreeView& operator=(TreeView&&) = default;
94
99 TGUI_NODISCARD static TreeView::Ptr create();
100
106 TGUI_NODISCARD static TreeView::Ptr copy(const TreeView::ConstPtr& treeView);
107
112 TGUI_NODISCARD TreeViewRenderer* getSharedRenderer() override;
113 TGUI_NODISCARD const TreeViewRenderer* getSharedRenderer() const override;
114
120 TGUI_NODISCARD TreeViewRenderer* getRenderer() override;
121
126 void setSize(const Layout2d& size) override;
127 using Widget::setSize;
128
147 bool addItem(const std::vector<String>& hierarchy, bool createParents = true);
148
164 bool changeItem(const std::vector<String>& hierarchy, const String& leafText);
165
171 void expand(const std::vector<String>& hierarchy);
172
176 void expandAll();
177
183 void collapse(const std::vector<String>& hierarchy);
184
189
197 bool selectItem(const std::vector<String>& hierarchy);
198
203
212 bool removeItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
213
218
223 TGUI_NODISCARD std::vector<String> getSelectedItem() const;
224
229 TGUI_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);
267
299 bool changeItemHierarchy(const std::vector<String>& oldHierarchy, const std::vector<String>& newHierarchy);
300
313 TGUI_NODISCARD ConstNode getNode(const std::vector<String>& hierarchy) const;
314
319 TGUI_NODISCARD std::vector<ConstNode> getNodes() const;
320
325 void setItemHeight(unsigned int itemHeight);
326
331 TGUI_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") TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
346
354 TGUI_DEPRECATED("Use getVerticalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const;
355
361 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setValue(value) instead") void setHorizontalScrollbarValue(unsigned int value);
362
368 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
369
377 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const;
378
384 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
385
389 bool leftMousePressed(Vector2f pos) override;
390
394 void leftMouseReleased(Vector2f pos) override;
395
399 void rightMousePressed(Vector2f pos) override;
400
404 void mouseMoved(Vector2f pos) override;
405
409 bool scrolled(float delta, Vector2f pos, bool touch) override;
410
414 void mouseNoLongerOnWidget() override;
415
419 void leftMouseButtonNoLongerDown() override;
420
424 void keyPressed(const Event::KeyEvent& event) override;
425
435 bool canHandleKeyPress(const Event::KeyEvent& event) override;
436
440 void markNodesDirty();
441
448 void draw(BackendRenderTarget& target, RenderStates states) const override;
449
451 protected:
452
462 TGUI_NODISCARD Signal& getSignal(String signalName) override;
463
469 void rendererChanged(const String& property) override;
470
474 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
475
479 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
480
484 void updateTextSize() override;
485
492 void scrollbarPolicyChanged(Orientation orientation) override;
493
495 // Returns the size without the borders
497 TGUI_NODISCARD Vector2f getInnerSize() const;
498
500 // Updates the bounds of the icons
502 void updateIconBounds();
503
505 // This function is called every frame with the time passed since the last frame.
507 bool updateTime(Duration elapsedTime) override;
508
510 // Makes a copy of the widget
512 TGUI_NODISCARD Widget::Ptr clone() const override;
513
517 void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
518
522 void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const String& text);
523
527 TGUI_NODISCARD Node* findParentNode(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
528
530 // Expands or collapses one of the visible items
532 void toggleNodeInternal(std::size_t index);
533
535 // Expands or collapses a node
537 bool expandOrCollapse(const std::vector<String>& hierarchy, bool expand);
538
540 // Helper function to load the items from a text file
542 void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
543
545 // Rebuilds the list of visible items and positions the texts
547 unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
548
550 // Updates the text colors of the selected and hovered items
552 void updateSelectedAndHoveringItemColors();
553
555 // Updates the text color of the hovered item
557 void updateHoveredItem(int item);
558
560 // Updates the text color of the selected item
562 void updateSelectedItem(int item);
563
565 public:
566
567 SignalItemHierarchy onItemSelect = {"ItemSelected"};
568 SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
569 SignalItemHierarchy onExpand = {"Expanded"};
570 SignalItemHierarchy onCollapse = {"Collapsed"};
571 SignalItemHierarchy onRightClick = {"RightClicked"};
572
574 protected:
575
576 // This contains the nodes of the tree
577 std::vector<std::shared_ptr<Node>> m_nodes;
578 std::vector<std::shared_ptr<Node>> m_visibleNodes;
579
580 int m_selectedItem = -1;
581 int m_hoveredItem = -1;
582
583 unsigned int m_itemHeight = 0;
584 float m_maxRight = 0;
585
586 Vector2f m_iconBounds;
587
588 bool m_possibleDoubleClick = false;
589 int m_doubleClickNodeIndex = -1;
590
591 Sprite m_spriteBackground;
592 Sprite m_spriteBranchExpanded;
593 Sprite m_spriteBranchCollapsed;
594 Sprite m_spriteLeaf;
595
596 // Cached renderer properties
597 Borders m_bordersCached;
598 Borders m_paddingCached;
599 Color m_borderColorCached;
600 Color m_backgroundColorCached;
601 Color m_textColorCached;
602 Color m_textColorHoverCached;
603 Color m_selectedTextColorCached;
604 Color m_selectedTextColorHoverCached;
605 Color m_selectedBackgroundColorCached;
606 Color m_selectedBackgroundColorHoverCached;
607 Color m_backgroundColorHoverCached;
608 TextStyles m_textStyleCached;
609
611 };
612
614}
615
617
618#endif // TGUI_TREE_VIEW_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Base class for widgets with both a vertical and horizontal scrollbar.
Definition Scrollbar.hpp:672
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:40
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.
int getItemIndexInParent(const std::vector< String > &hierarchy)
Returns the index of an item, based on the items that share the same parent.
bool addItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new item to the tree view.
static TGUI_NODISCARD TreeView::Ptr create()
Creates a new tree view widget.
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 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.
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.
TGUI_NODISCARD std::vector< String > getHoveredItem() const
Returns the item currently below the mouse cursor.
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.
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...
TGUI_NODISCARD std::vector< ConstNode > getNodes() const
Returns the nodes in the tree view.
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
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:51
Definition Event.hpp:38
States used for drawing.
Definition RenderStates.hpp:38
Read-only node representation used by getNode and getNodes.
Definition TreeView.hpp:50
Internal representation of a node.
Definition TreeView.hpp:58