TGUI  0.8.8
TreeView.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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  sf::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 
68  // Default constructor
70  TreeView();
71 
73  // Copy constructor
75  TreeView(const TreeView&);
76 
78  // Move constructor
80  TreeView(TreeView&&) = default;
81 
83  // Overload of copy assignment operator
85  TreeView& operator=(const TreeView&);
86 
88  // Overload of move assignment operator
90  TreeView& operator=(TreeView&&) = default;
91 
92 
97  static TreeView::Ptr create();
98 
99 
105  static TreeView::Ptr copy(TreeView::ConstPtr treeView);
106 
107 
113  const TreeViewRenderer* getSharedRenderer() const;
114 
115 
122  const TreeViewRenderer* getRenderer() const;
123 
124 
129  void setSize(const Layout2d& size) override;
130  using Widget::setSize;
131 
132 
141  bool addItem(const std::vector<sf::String>& hierarchy, bool createParents = true);
142 
143 
149  void expand(const std::vector<sf::String>& hierarchy);
150 
151 
155  void expandAll();
156 
157 
163  void collapse(const std::vector<sf::String>& hierarchy);
164 
165 
169  void collapseAll();
170 
171 
179  bool selectItem(const std::vector<sf::String>& hierarchy);
180 
181 
185  void deselectItem();
186 
187 
196  bool removeItem(const std::vector<sf::String>& hierarchy, bool removeParentsWhenEmpty = true);
197 
198 
203 
204 
209  std::vector<sf::String> getSelectedItem() const;
210 
211 
216  std::vector<ConstNode> getNodes() const;
217 
218 
223  void setItemHeight(unsigned int itemHeight);
224 
225 
230  unsigned int getItemHeight() const;
231 
232 
243  void setTextSize(unsigned int textSize) override;
244 
245 
251  void setVerticalScrollbarValue(unsigned int value);
252 
253 
259  unsigned int getVerticalScrollbarValue() const;
260 
261 
267  void setHorizontalScrollbarValue(unsigned int value);
268 
269 
275  unsigned int getHorizontalScrollbarValue() const;
276 
277 
283  bool mouseOnWidget(Vector2f pos) const override;
284 
288  void leftMousePressed(Vector2f pos) override;
289 
293  void leftMouseReleased(Vector2f pos) override;
294 
298  void rightMousePressed(Vector2f pos) override;
299 
303  void mouseMoved(Vector2f pos) override;
304 
308  bool mouseWheelScrolled(float delta, Vector2f pos) override;
309 
313  void mouseNoLongerOnWidget() override;
314 
318  void leftMouseButtonNoLongerDown() override;
319 
323  void markNodesDirty();
324 
325 
333  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
334 
335 
336 
338  protected:
339 
349  Signal& getSignal(std::string signalName) override;
350 
351 
358  void rendererChanged(const std::string& property) override;
359 
360 
364  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
365 
366 
370  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
371 
372 
374  // Returns the size without the borders
376  Vector2f getInnerSize() const;
377 
378 
380  // Updates the bounds of the icons
382  void updateIconBounds();
383 
384 
386  // This function is called every frame with the time passed since the last frame.
388  bool update(sf::Time elapsedTime) override;
389 
390 
392  // Makes a copy of the widget
394  Widget::Ptr clone() const override
395  {
396  return std::make_shared<TreeView>(*this);
397  }
398 
399 
403  void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
404 
405 
409  void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const sf::String& text);
410 
411 
415  Node* findParentNode(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
416 
417 
419  // Expands or collapses one of the visible items
421  void toggleNodeInternal(std::size_t index);
422 
423 
425  // Expands or collapses a node
427  bool expandOrCollapse(const std::vector<sf::String>& hierarchy, bool expand);
428 
429 
431  // Helper function to load the items from a text file
433  void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
434 
435 
437  // Rebuilds the list of visible items and positions the texts
439  unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
440 
441 
443  // Updates the text colors of the selected and hovered items
445  void updateSelectedAndHoveringItemColors();
446 
447 
449  // Updates the text color of the hovered item
451  void updateHoveredItem(int item);
452 
453 
455  // Updates the text color of the selected item
457  void updateSelectedItem(int item);
458 
459 
461  public:
462 
463  SignalItemHierarchy onItemSelect = {"ItemSelected"};
464  SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
465  SignalItemHierarchy onExpand = {"Expanded"};
466  SignalItemHierarchy onCollapse = {"Collapsed"};
467  SignalItemHierarchy onRightClick = {"RightClicked"};
468 
470  protected:
471 
472  // This contains the nodes of the tree
473  std::vector<std::shared_ptr<Node>> m_nodes;
474  std::vector<std::shared_ptr<Node>> m_visibleNodes;
475 
476  int m_selectedItem = -1;
477  int m_hoveredItem = -1;
478 
479  // The size must be stored
480  unsigned int m_itemHeight = 0;
481  unsigned int m_requestedTextSize = 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_spriteBranchExpanded;
494  Sprite m_spriteBranchCollapsed;
495  Sprite m_spriteLeaf;
496 
497  // Cached renderer properties
498  Borders m_bordersCached;
499  Borders m_paddingCached;
500  Color m_borderColorCached;
501  Color m_backgroundColorCached;
502  Color m_textColorCached;
503  Color m_textColorHoverCached;
504  Color m_selectedTextColorCached;
505  Color m_selectedTextColorHoverCached;
506  Color m_selectedBackgroundColorCached;
507  Color m_selectedBackgroundColorHoverCached;
508  Color m_backgroundColorHoverCached;
509  TextStyle m_textStyleCached;
510 
512  };
513 
515 }
516 
518 
519 #endif // TGUI_TREE_VIEW_HPP
unsigned int getItemHeight() const
Returns the height of the items in the tree view.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
Definition: CopiedSharedPtr.hpp:40
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
void expand(const std::vector< sf::String > &hierarchy)
Expands the given item.
void removeAllItems()
Removes all items.
Tree view widget.
Definition: TreeView.hpp:42
bool removeItem(const std::vector< sf::String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes an item.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void deselectItem()
Deselect the item if one was selected.
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.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Definition: Text.hpp:43
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:731
void expandAll()
Expands all items.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Wrapper for colors.
Definition: Color.hpp:49
void collapseAll()
Collapses all items.
TreeViewRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:78
void setItemHeight(unsigned int itemHeight)
Changes 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::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
Definition: Sprite.hpp:46
Read-only node representation used by getNodes.
Definition: TreeView.hpp:50
bool selectItem(const std::vector< sf::String > &hierarchy)
Selects an item in the tree view.
Definition: TreeViewRenderer.hpp:37
Definition: Vector2f.hpp:39
static TreeView::Ptr copy(TreeView::ConstPtr treeView)
Makes a copy of another tree view.
Definition: Outline.hpp:39
void collapse(const std::vector< sf::String > &hierarchy)
Collapses the given item.
The parent class for every widget.
Definition: Widget.hpp:74
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
std::vector< sf::String > getSelectedItem() const
Returns the selected item.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
bool addItem(const std::vector< sf::String > &hierarchy, bool createParents=true)
Adds a new item to the tree view.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TreeView.hpp:394
static TreeView::Ptr create()
Creates a new tree view widget.
TreeViewRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Internal representation of a node.
Definition: TreeView.hpp:58
Wrapper for text styles.
Definition: TextStyle.hpp:47
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
void setTextSize(unsigned int textSize) override
Changes the text size of the items.