TGUI  0.9.1
TreeView.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
36 namespace tgui
37 {
41  class TGUI_API TreeView : public Widget
42  {
43  public:
44 
45  typedef std::shared_ptr<TreeView> Ptr;
46  typedef std::shared_ptr<const TreeView> ConstPtr;
47 
49  struct ConstNode
50  {
51  bool expanded;
52  String text;
53  std::vector<ConstNode> nodes;
54  };
55 
57  struct Node
58  {
59  Text text;
60  unsigned depth = 0;
61  bool expanded = true;
62  Node* parent;
63  std::vector<std::shared_ptr<Node>> nodes;
64  };
65 
66 
74  TreeView(const char* typeName = "TreeView", bool initRenderer = true);
75 
76 
78  // Copy constructor
80  TreeView(const TreeView&);
81 
83  // Move constructor
85  TreeView(TreeView&&) = default;
86 
88  // Overload of copy assignment operator
90  TreeView& operator=(const TreeView&);
91 
93  // Overload of move assignment operator
95  TreeView& operator=(TreeView&&) = default;
96 
97 
102  static TreeView::Ptr create();
103 
104 
110  static TreeView::Ptr copy(TreeView::ConstPtr treeView);
111 
112 
118  const TreeViewRenderer* getSharedRenderer() const;
119 
120 
127  const TreeViewRenderer* getRenderer() const;
128 
129 
134  void setSize(const Layout2d& size) override;
135  using Widget::setSize;
136 
137 
146  bool addItem(const std::vector<String>& hierarchy, bool createParents = true);
147 
148 
154  void expand(const std::vector<String>& hierarchy);
155 
156 
160  void expandAll();
161 
162 
168  void collapse(const std::vector<String>& hierarchy);
169 
170 
174  void collapseAll();
175 
176 
184  bool selectItem(const std::vector<String>& hierarchy);
185 
186 
190  void deselectItem();
191 
192 
201  bool removeItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
202 
203 
208 
209 
214  std::vector<String> getSelectedItem() const;
215 
216 
221  std::vector<ConstNode> getNodes() const;
222 
223 
228  void setItemHeight(unsigned int itemHeight);
229 
230 
235  unsigned int getItemHeight() const;
236 
237 
248  void setTextSize(unsigned int textSize) override;
249 
250 
256  void setVerticalScrollbarValue(unsigned int value);
257 
258 
264  unsigned int getVerticalScrollbarValue() const;
265 
266 
272  void setHorizontalScrollbarValue(unsigned int value);
273 
274 
280  unsigned int getHorizontalScrollbarValue() const;
281 
282 
288  bool isMouseOnWidget(Vector2f pos) const override;
289 
293  void leftMousePressed(Vector2f pos) override;
294 
298  void leftMouseReleased(Vector2f pos) override;
299 
303  void rightMousePressed(Vector2f pos) override;
304 
308  void mouseMoved(Vector2f pos) override;
309 
313  bool mouseWheelScrolled(float delta, Vector2f pos) override;
314 
318  void mouseNoLongerOnWidget() override;
319 
323  void leftMouseButtonNoLongerDown() override;
324 
328  void markNodesDirty();
329 
330 
338  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
339 
340 
341 
343  protected:
344 
354  Signal& getSignal(String signalName) override;
355 
356 
363  void rendererChanged(const String& property) override;
364 
365 
369  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
370 
371 
375  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) 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  return std::make_shared<TreeView>(*this);
402  }
403 
404 
408  void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
409 
410 
414  void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const String& text);
415 
416 
420  Node* findParentNode(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
421 
422 
424  // Expands or collapses one of the visible items
426  void toggleNodeInternal(std::size_t index);
427 
428 
430  // Expands or collapses a node
432  bool expandOrCollapse(const std::vector<String>& hierarchy, bool expand);
433 
434 
436  // Helper function to load the items from a text file
438  void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
439 
440 
442  // Rebuilds the list of visible items and positions the texts
444  unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
445 
446 
448  // Updates the text colors of the selected and hovered items
450  void updateSelectedAndHoveringItemColors();
451 
452 
454  // Updates the text color of the hovered item
456  void updateHoveredItem(int item);
457 
458 
460  // Updates the text color of the selected item
462  void updateSelectedItem(int item);
463 
464 
466  public:
467 
468  SignalItemHierarchy onItemSelect = {"ItemSelected"};
469  SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
470  SignalItemHierarchy onExpand = {"Expanded"};
471  SignalItemHierarchy onCollapse = {"Collapsed"};
472  SignalItemHierarchy onRightClick = {"RightClicked"};
473 
475  protected:
476 
477  // This contains the nodes of the tree
478  std::vector<std::shared_ptr<Node>> m_nodes;
479  std::vector<std::shared_ptr<Node>> m_visibleNodes;
480 
481  int m_selectedItem = -1;
482  int m_hoveredItem = -1;
483 
484  // The size must be stored
485  unsigned int m_itemHeight = 0;
486  unsigned int m_requestedTextSize = 0;
487  float m_maxRight = 0;
488 
489  Vector2f m_iconBounds;
490 
491  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
492  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
493 
494 
495  bool m_possibleDoubleClick = false;
496  int m_doubleClickNodeIndex = -1;
497 
498  Sprite m_spriteBranchExpanded;
499  Sprite m_spriteBranchCollapsed;
500  Sprite m_spriteLeaf;
501 
502  // Cached renderer properties
503  Borders m_bordersCached;
504  Borders m_paddingCached;
505  Color m_borderColorCached;
506  Color m_backgroundColorCached;
507  Color m_textColorCached;
508  Color m_textColorHoverCached;
509  Color m_selectedTextColorCached;
510  Color m_selectedTextColorHoverCached;
511  Color m_selectedBackgroundColorCached;
512  Color m_selectedBackgroundColorHoverCached;
513  Color m_backgroundColorHoverCached;
514  TextStyles m_textStyleCached;
515 
517  };
518 
520 }
521 
523 
524 #endif // TGUI_TREE_VIEW_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:52
Definition: CopiedSharedPtr.hpp:40
Wrapper for durations.
Definition: Duration.hpp:52
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:780
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:49
Wrapper class to store strings.
Definition: String.hpp:74
Wrapper for text styles.
Definition: TextStyle.hpp:58
Definition: Text.hpp:44
Definition: TreeViewRenderer.hpp:37
Tree view widget.
Definition: TreeView.hpp:42
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.
Definition: TreeView.hpp:399
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
TreeViewRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::vector< String > getSelectedItem() const
Returns the selected item.
bool addItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new item to the tree view.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
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 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.
std::vector< ConstNode > getNodes() const
Returns the nodes in the tree view.
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.
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 isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
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.
static TreeView::Ptr copy(TreeView::ConstPtr treeView)
Makes a copy of another tree view.
TreeViewRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setTextSize(unsigned int textSize) override
Changes the text size of the items.
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
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:74
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39
Read-only node representation used by getNodes.
Definition: TreeView.hpp:50
Internal representation of a node.
Definition: TreeView.hpp:58