TGUI  1.6.1
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
230 TGUI_NODISCARD std::vector<String> getHoveredItem() const;
231
246 bool setItemIndexInParent(const std::vector<String>& hierarchy, std::size_t index);
247
267 int getItemIndexInParent(const std::vector<String>& hierarchy) const;
268
300 bool changeItemHierarchy(const std::vector<String>& oldHierarchy, const std::vector<String>& newHierarchy);
301
314 TGUI_NODISCARD ConstNode getNode(const std::vector<String>& hierarchy) const;
315
320 TGUI_NODISCARD std::vector<ConstNode> getNodes() const;
321
326 void setItemHeight(unsigned int itemHeight);
327
332 TGUI_NODISCARD unsigned int getItemHeight() const;
333
339 TGUI_DEPRECATED("Use getVerticalScrollbar()->setValue(value) instead") void setVerticalScrollbarValue(unsigned int value);
340
346 TGUI_DEPRECATED("Use getVerticalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
347
355 TGUI_DEPRECATED("Use getVerticalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const;
356
362 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setValue(value) instead") void setHorizontalScrollbarValue(unsigned int value);
363
369 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
370
378 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const;
379
385 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
386
390 bool leftMousePressed(Vector2f pos) override;
391
395 void leftMouseReleased(Vector2f pos) override;
396
400 void rightMousePressed(Vector2f pos) override;
401
405 void mouseMoved(Vector2f pos) override;
406
410 bool scrolled(float delta, Vector2f pos, bool touch) override;
411
415 void mouseNoLongerOnWidget() override;
416
420 void leftMouseButtonNoLongerDown() override;
421
425 void keyPressed(const Event::KeyEvent& event) override;
426
436 bool canHandleKeyPress(const Event::KeyEvent& event) override;
437
441 void markNodesDirty();
442
449 void draw(BackendRenderTarget& target, RenderStates states) const override;
450
452 protected:
453
463 TGUI_NODISCARD Signal& getSignal(String signalName) override;
464
470 void rendererChanged(const String& property) override;
471
475 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
476
480 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
481
485 void updateTextSize() override;
486
493 void scrollbarPolicyChanged(Orientation orientation) override;
494
496 // Returns the size without the borders
498 TGUI_NODISCARD Vector2f getInnerSize() const;
499
501 // Updates the bounds of the icons
503 void updateIconBounds();
504
506 // This function is called every frame with the time passed since the last frame.
508 bool updateTime(Duration elapsedTime) override;
509
511 // Makes a copy of the widget
513 TGUI_NODISCARD Widget::Ptr clone() const override;
514
518 void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
519
523 void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const String& text);
524
528 TGUI_NODISCARD Node* findParentNode(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
529
531 // Expands or collapses one of the visible items
533 void toggleNodeInternal(std::size_t index);
534
536 // Expands or collapses a node
538 bool expandOrCollapse(const std::vector<String>& hierarchy, bool expand);
539
541 // Helper function to load the items from a text file
543 void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
544
546 // Rebuilds the list of visible items and positions the texts
548 unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
549
551 // Updates the text colors of the selected and hovered items
553 void updateSelectedAndHoveringItemColors();
554
556 // Updates the text color of the hovered item
558 void updateHoveredItem(int item);
559
561 // Updates the text color of the selected item
563 void updateSelectedItem(int item);
564
566 public:
567
568 SignalItemHierarchy onItemSelect = {"ItemSelected"};
569 SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
570 SignalItemHierarchy onExpand = {"Expanded"};
571 SignalItemHierarchy onCollapse = {"Collapsed"};
572 SignalItemHierarchy onRightClick = {"RightClicked"};
573
575 protected:
576
577 // This contains the nodes of the tree
578 std::vector<std::shared_ptr<Node>> m_nodes;
579 std::vector<std::shared_ptr<Node>> m_visibleNodes;
580
581 int m_selectedItem = -1;
582 int m_hoveredItem = -1;
583
584 unsigned int m_itemHeight = 0;
585 float m_maxRight = 0;
586
587 Vector2f m_iconBounds;
588
589 bool m_possibleDoubleClick = false;
590 int m_doubleClickNodeIndex = -1;
591
592 Sprite m_spriteBackground;
593 Sprite m_spriteBranchExpanded;
594 Sprite m_spriteBranchCollapsed;
595 Sprite m_spriteLeaf;
596
597 // Cached renderer properties
598 Borders m_bordersCached;
599 Borders m_paddingCached;
600 Color m_borderColorCached;
601 Color m_backgroundColorCached;
602 Color m_textColorCached;
603 Color m_textColorHoverCached;
604 Color m_selectedTextColorCached;
605 Color m_selectedTextColorHoverCached;
606 Color m_selectedBackgroundColorCached;
607 Color m_selectedBackgroundColorHoverCached;
608 Color m_backgroundColorHoverCached;
609 TextStyles m_textStyleCached;
610
612 };
613
615}
616
618
619#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:673
Wrapper for durations.
Definition Duration.hpp:55
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1060
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
Definition TreeViewRenderer.hpp:35
Tree view widget.
Definition TreeView.hpp:40
void deselectItem()
Deselect the item if one was selected.
bool addItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new item to the tree view.
unsigned int getItemHeight() const
Returns the height of the items in the tree view.
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.
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.
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.
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.
std::vector< String > getSelectedItem() const
Returns the selected item.
static TreeView::Ptr copy(const TreeView::ConstPtr &treeView)
Makes a copy of another tree view.
static TreeView::Ptr create()
Creates a new tree view widget.
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.
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:52
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