TGUI  0.8.6
TreeView.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
112  TreeViewRenderer* getSharedRenderer();
113  const TreeViewRenderer* getSharedRenderer() const;
114 
115 
121  TreeViewRenderer* getRenderer();
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 
202  void removeAllItems();
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);
244 
245 
250  unsigned int getTextSize() const;
251 
252 
258  void setVerticalScrollbarValue(unsigned int value);
259 
260 
266  unsigned int getVerticalScrollbarValue() const;
267 
268 
274  void setHorizontalScrollbarValue(unsigned int value);
275 
276 
282  unsigned int getHorizontalScrollbarValue() const;
283 
284 
290  bool mouseOnWidget(Vector2f pos) const override;
291 
295  void leftMousePressed(Vector2f pos) override;
296 
300  void leftMouseReleased(Vector2f pos) override;
301 
305  void mouseMoved(Vector2f pos) override;
306 
310  bool mouseWheelScrolled(float delta, Vector2f pos) override;
311 
315  void mouseNoLongerOnWidget() override;
316 
320  void leftMouseButtonNoLongerDown() override;
321 
325  void markNodesDirty();
326 
327 
335  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
336 
337 
338 
340  protected:
341 
351  Signal& getSignal(std::string signalName) override;
352 
353 
360  void rendererChanged(const std::string& property) override;
361 
362 
366  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
367 
368 
372  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
373 
374 
376  // Returns the size without the borders
378  Vector2f getInnerSize() const;
379 
380 
382  // Updates the bounds of the icons
384  void updateIconBounds();
385 
386 
388  // This function is called every frame with the time passed since the last frame.
390  void update(sf::Time elapsedTime) override;
391 
392 
394  // Makes a copy of the widget
396  Widget::Ptr clone() const override
397  {
398  return std::make_shared<TreeView>(*this);
399  }
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 sf::String& text);
412 
413 
417  Node* findParentNode(const std::vector<sf::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<sf::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 
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  // The size must be stored
482  unsigned int m_itemHeight = 0;
483  unsigned int m_requestedTextSize = 0;
484  unsigned int m_textSize = 0;
485  float m_maxRight = 0;
486 
487  Vector2f m_iconBounds;
488 
489  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
490  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
491 
492 
493  bool m_possibleDoubleClick = false;
494  int m_doubleClickNodeIndex = -1;
495 
496  Sprite m_spriteBranchExpanded;
497  Sprite m_spriteBranchCollapsed;
498  Sprite m_spriteLeaf;
499 
500  // Cached renderer properties
501  Borders m_bordersCached;
502  Borders m_paddingCached;
503  Color m_borderColorCached;
504  Color m_backgroundColorCached;
505  Color m_textColorCached;
506  Color m_textColorHoverCached;
507  Color m_selectedTextColorCached;
508  Color m_selectedTextColorHoverCached;
509  Color m_selectedBackgroundColorCached;
510  Color m_selectedBackgroundColorHoverCached;
511  Color m_backgroundColorHoverCached;
512  TextStyle m_textStyleCached;
513 
515  };
516 
518 }
519 
521 
522 #endif // TGUI_TREE_VIEW_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
Tree view widget.
Definition: TreeView.hpp:41
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Text.hpp:42
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:750
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:48
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:78
Definition: Sprite.hpp:45
Read-only node representation used by getNodes.
Definition: TreeView.hpp:49
Definition: TreeViewRenderer.hpp:36
Definition: Vector2f.hpp:38
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TreeView.hpp:396
Internal representation of a node.
Definition: TreeView.hpp:57
Wrapper for text styles.
Definition: TextStyle.hpp:46