TGUI  1.0-beta
Loading...
Searching...
No Matches
TreeView.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
26#ifndef TGUI_TREE_VIEW_HPP
27#define TGUI_TREE_VIEW_HPP
28
29#include <TGUI/CopiedSharedPtr.hpp>
30#include <TGUI/Widgets/Scrollbar.hpp>
31#include <TGUI/Renderers/TreeViewRenderer.hpp>
32#include <TGUI/Text.hpp>
33
35
36namespace tgui
37{
41 class TGUI_API TreeView : public Widget
42 {
43 public:
44
45 using Ptr = std::shared_ptr<TreeView>;
46 using ConstPtr = std::shared_ptr<const TreeView>;
47
48 static constexpr const char StaticWidgetType[] = "TreeView";
49
50
52 struct ConstNode
53 {
54 bool expanded;
55 String text;
56 std::vector<ConstNode> nodes;
57 };
58
60 struct Node
61 {
62 Text text;
63 unsigned int depth = 0;
64 bool expanded = true;
65 Node* parent = nullptr;
66 std::vector<std::shared_ptr<Node>> nodes;
67 };
68
69
77 TreeView(const char* typeName = StaticWidgetType, bool initRenderer = true);
78
79
81 // Copy constructor
83 TreeView(const TreeView&);
84
86 // Move constructor
88 TreeView(TreeView&&) = default;
89
91 // Overload of copy assignment operator
93 TreeView& operator=(const TreeView&);
94
96 // Overload of move assignment operator
98 TreeView& operator=(TreeView&&) = default;
99
100
105 static TreeView::Ptr create();
106
107
113 static TreeView::Ptr copy(const TreeView::ConstPtr& treeView);
114
115
121 const TreeViewRenderer* getSharedRenderer() const;
122
123
130
131
136 void setSize(const Layout2d& size) override;
137 using Widget::setSize;
138
139
148 bool addItem(const std::vector<String>& hierarchy, bool createParents = true);
149
150
156 void expand(const std::vector<String>& hierarchy);
157
158
162 void expandAll();
163
164
170 void collapse(const std::vector<String>& hierarchy);
171
172
177
178
186 bool selectItem(const std::vector<String>& hierarchy);
187
188
193
194
203 bool removeItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
204
205
210
211
216 std::vector<String> getSelectedItem() const;
217
218
223 std::vector<ConstNode> getNodes() const;
224
225
230 void setItemHeight(unsigned int itemHeight);
231
232
237 unsigned int getItemHeight() const;
238
239
245 void setVerticalScrollbarValue(unsigned int value);
246
247
253 unsigned int getVerticalScrollbarValue() const;
254
255
261 void setHorizontalScrollbarValue(unsigned int value);
262
263
269 unsigned int getHorizontalScrollbarValue() const;
270
271
277 bool isMouseOnWidget(Vector2f pos) const override;
278
282 void leftMousePressed(Vector2f pos) override;
283
287 void leftMouseReleased(Vector2f pos) override;
288
292 void rightMousePressed(Vector2f pos) override;
293
297 void mouseMoved(Vector2f pos) override;
298
302 bool mouseWheelScrolled(float delta, Vector2f pos) override;
303
307 void mouseNoLongerOnWidget() override;
308
312 void leftMouseButtonNoLongerDown() override;
313
317 void keyPressed(const Event::KeyEvent& event) override;
318
322 void markNodesDirty();
323
324
332 void draw(BackendRenderTarget& target, RenderStates states) const override;
333
334
335
337 protected:
338
348 Signal& getSignal(String signalName) override;
349
350
357 void rendererChanged(const String& property) override;
358
359
363 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
364
365
369 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
370
371
375 void updateTextSize() override;
376
377
379 // Returns the size without the borders
381 Vector2f getInnerSize() const;
382
383
385 // Updates the bounds of the icons
387 void updateIconBounds();
388
389
391 // This function is called every frame with the time passed since the last frame.
393 bool updateTime(Duration elapsedTime) override;
394
395
397 // Makes a copy of the widget
399 Widget::Ptr clone() const override;
400
401
405 void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
406
407
411 void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const String& text);
412
413
417 Node* findParentNode(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
418
419
421 // Expands or collapses one of the visible items
423 void toggleNodeInternal(std::size_t index);
424
425
427 // Expands or collapses a node
429 bool expandOrCollapse(const std::vector<String>& hierarchy, bool expand);
430
431
433 // Helper function to load the items from a text file
435 void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
436
437
439 // Rebuilds the list of visible items and positions the texts
441 unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
442
443
445 // Updates the text colors of the selected and hovered items
447 void updateSelectedAndHoveringItemColors();
448
449
451 // Updates the text color of the hovered item
453 void updateHoveredItem(int item);
454
455
457 // Updates the text color of the selected item
459 void updateSelectedItem(int item);
460
461
463 public:
464
465 SignalItemHierarchy onItemSelect = {"ItemSelected"};
466 SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
467 SignalItemHierarchy onExpand = {"Expanded"};
468 SignalItemHierarchy onCollapse = {"Collapsed"};
469 SignalItemHierarchy onRightClick = {"RightClicked"};
470
472 protected:
473
474 // This contains the nodes of the tree
475 std::vector<std::shared_ptr<Node>> m_nodes;
476 std::vector<std::shared_ptr<Node>> m_visibleNodes;
477
478 int m_selectedItem = -1;
479 int m_hoveredItem = -1;
480
481 unsigned int m_itemHeight = 0;
482 float m_maxRight = 0;
483
484 Vector2f m_iconBounds;
485
486 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
487 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
488
489
490 bool m_possibleDoubleClick = false;
491 int m_doubleClickNodeIndex = -1;
492
493 Sprite m_spriteBackground;
494 Sprite m_spriteBranchExpanded;
495 Sprite m_spriteBranchCollapsed;
496 Sprite m_spriteLeaf;
497
498 // Cached renderer properties
499 Borders m_bordersCached;
500 Borders m_paddingCached;
501 Color m_borderColorCached;
502 Color m_backgroundColorCached;
503 Color m_textColorCached;
504 Color m_textColorHoverCached;
505 Color m_selectedTextColorCached;
506 Color m_selectedTextColorHoverCached;
507 Color m_selectedBackgroundColorCached;
508 Color m_selectedBackgroundColorHoverCached;
509 Color m_backgroundColorHoverCached;
510 TextStyles m_textStyleCached;
511
513 };
514
516}
517
519
520#endif // TGUI_TREE_VIEW_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:63
Definition: CopiedSharedPtr.hpp:40
Wrapper for durations.
Definition: Duration.hpp:52
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:960
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
Wrapper for text styles.
Definition: TextStyle.hpp:58
Definition: Text.hpp:48
Definition: TreeViewRenderer.hpp:37
Tree view widget.
Definition: TreeView.hpp:42
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.
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.
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 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.
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 selectItem(const std::vector< String > &hierarchy)
Selects an item in the tree view.
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 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.
TreeViewRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
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.
TreeViewRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setSize(const Layout2d &size) override
Changes the size of the tree view.
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
KeyPressed event parameters.
Definition: Event.hpp:167
States used for drawing.
Definition: RenderStates.hpp:39
Read-only node representation used by getNodes.
Definition: TreeView.hpp:53
Internal representation of a node.
Definition: TreeView.hpp:61