TGUI  0.8.3
ListView.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_LIST_VIEW_HPP
27 #define TGUI_LIST_VIEW_HPP
28 
29 
30 #include <TGUI/CopiedSharedPtr.hpp>
31 #include <TGUI/Widgets/Scrollbar.hpp>
32 #include <TGUI/Renderers/ListViewRenderer.hpp>
33 #include <TGUI/Text.hpp>
34 
36 
37 namespace tgui
38 {
43  class TGUI_API ListView : public Widget
44  {
45  public:
46 
47  typedef std::shared_ptr<ListView> Ptr;
48  typedef std::shared_ptr<const ListView> ConstPtr;
49 
53  enum class ColumnAlignment
54  {
55  Left,
56  Center,
57  Right
58  };
59 
60  struct Item
61  {
62  std::vector<Text> texts;
63  Sprite icon;
64  };
65 
66  struct Column
67  {
68  float width;
69  float designWidth;
70  Text text;
71  ColumnAlignment alignment;
72  };
73 
74 
76  // Default constructor
78  ListView();
79 
80 
85  static ListView::Ptr create();
86 
87 
95  static ListView::Ptr copy(ListView::ConstPtr listView);
96 
97 
102  ListViewRenderer* getSharedRenderer();
103  const ListViewRenderer* getSharedRenderer() const;
104 
110  ListViewRenderer* getRenderer();
111  const ListViewRenderer* getRenderer() const;
112 
113 
119  void setSize(const Layout2d& size) override;
120  using Widget::setSize;
121 
122 
132  std::size_t addColumn(const sf::String& text, float width = 0, ColumnAlignment alignment = ColumnAlignment::Left);
133 
134 
141  void setColumnText(std::size_t index, const sf::String& text);
142 
143 
151  sf::String getColumnText(std::size_t index);
152 
153 
160  void setColumnWidth(std::size_t index, float width);
161 
162 
170  float getColumnWidth(std::size_t index);
171 
172 
179  void setColumnAlignment(unsigned int columnIndex, ColumnAlignment alignment);
180 
181 
189  ColumnAlignment getColumnAlignment(unsigned int columnIndex) const;
190 
191 
195  void removeAllColumns();
196 
197 
203  std::size_t getColumnCount() const;
204 
205 
211  void setHeaderHeight(float height);
212 
213 
219  float getHeaderHeight() const;
220 
221 
227  void setHeaderVisible(bool showHeader);
228 
229 
235  bool getHeaderVisible() const;
236 
237 
245  std::size_t addItem(const sf::String& text);
246 
247 
255  std::size_t addItem(const std::vector<sf::String>& item);
256 
257 
265  bool removeItem(std::size_t index);
266 
267 
271  void removeAllItems();
272 
273 
279  void setSelectedItem(std::size_t index);
280 
281 
285  void deselectItem();
286 
287 
293  int getSelectedItemIndex() const;
294 
295 
302  void setItemIcon(std::size_t index, const Texture& texture);
303 
304 
312  Texture getItemIcon(std::size_t index) const;
313 
314 
320  std::size_t getItemCount() const;
321 
322 
330  sf::String getItem(std::size_t index) const;
331 
332 
342  std::vector<sf::String> getItemRow(std::size_t index) const;
343 
344 
350  std::vector<sf::String> getItems() const;
351 
352 
358  std::vector<std::vector<sf::String>> getItemRows() const;
359 
360 
366  void setItemHeight(unsigned int itemHeight);
367 
368 
374  unsigned int getItemHeight() const;
375 
376 
387  void setTextSize(unsigned int textSize);
388 
389 
395  unsigned int getTextSize() const;
396 
397 
405  void setHeaderTextSize(unsigned int textSize);
406 
407 
413  unsigned int getHeaderTextSize() const;
414 
415 
421  void setSeparatorWidth(unsigned int width);
422 
423 
429  unsigned int getSeparatorWidth() const;
430 
431 
439  void setAutoScroll(bool autoScroll);
440 
441 
447  bool getAutoScroll() const;
448 
449 
454  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
455 
456 
461  Scrollbar::Policy getVerticalScrollbarPolicy() const;
462 
463 
468  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
469 
470 
475  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
476 
477 
482  bool mouseOnWidget(Vector2f pos) const override;
483 
487  void leftMousePressed(Vector2f pos) override;
488 
492  void leftMouseReleased(Vector2f pos) override;
493 
497  void mouseMoved(Vector2f pos) override;
498 
502  bool mouseWheelScrolled(float delta, Vector2f pos) override;
503 
507  void mouseNoLongerOnWidget() override;
508 
512  void mouseNoLongerDown() override;
513 
514 
521  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
522 
523 
525  protected:
526 
536  Signal& getSignal(std::string signalName) override;
537 
538 
544  void rendererChanged(const std::string& property) override;
545 
546 
550  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
551 
552 
556  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
557 
558 
560  // Returns the size without the borders
562  Vector2f getInnerSize() const;
563 
564 
566  // Create a Text object for an item from the given caption, using the preset color, font, text size and opacity
568  Text createText(const sf::String& caption);
569 
570 
572  // Create a Text object for a header text from the given caption, using the preset color, font, text size and opacity
574  Text createHeaderText(const sf::String& caption);
575 
576 
578  // Changes the color of all Text objects in an item
580  void setItemColor(std::size_t index, const Color& color);
581 
582 
584  // Calculate the width of the column based on its caption when no column width was provided
586  float calculateAutoColumnWidth(const Text& text);
587 
588 
590  // Update the colors of the selected and hovered items
592  void updateSelectedAndhoveredItemColors();
593 
594 
596  // Update the color of all the items
598  void updateItemColors();
599 
600 
602  // Changes the color of all header texts
604  void updateHeaderTextsColor();
605 
606 
608  // Update on which item the mouse is standing
610  void updateHoveredItem(int item);
611 
612 
614  // Update which item is selected
616  void updateSelectedItem(int item);
617 
618 
620  // Recalculate the size and viewport size of the scrollbars
622  void updateScrollbars();
623 
625  // Recalculate the maximum value for the vertical scrollbar
627  void updateVerticalScrollbarMaximum();
628 
630  // Recalculate the maximum value for the horizontal scrollbar
632  void updateHorizontalScrollbarMaximum();
633 
634 
636  // Draw the header text for a single column
638  void drawHeaderText(sf::RenderTarget& target, sf::RenderStates states, float columnWidth, float headerHeight, std::size_t column) const;
639 
640 
642  // Draw the texts in a single column
644  void drawColumn(sf::RenderTarget& target, sf::RenderStates states, std::size_t firstItem, std::size_t lastItem, std::size_t column, float columnWidth) const;
645 
646 
648  // This function is called every frame with the time passed since the last frame.
650  void update(sf::Time elapsedTime) override;
651 
652 
654  // Makes a copy of the widget
656  Widget::Ptr clone() const override
657  {
658  return std::make_shared<ListView>(*this);
659  }
660 
661 
663  public:
664 
665  SignalInt onItemSelect = {"ItemSelected"};
666  SignalInt onDoubleClick = {"DoubleClicked"};
667 
668 
670  protected:
671 
672  std::vector<Column> m_columns;
673  std::vector<Item> m_items;
674 
675  int m_selectedItem = -1;
676  int m_hoveredItem = -1;
677 
678  float m_requestedHeaderHeight = 0;
679  unsigned int m_itemHeight = 0;
680  unsigned int m_requestedTextSize = 0;
681  unsigned int m_textSize = 0;
682  unsigned int m_headerTextSize = 0;
683  unsigned int m_separatorWidth = 1;
684  unsigned int m_iconCount = 0;
685  float m_maxIconWidth = 0;
686  bool m_headerVisible = true;
687 
688  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
689  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
690  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
691  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
692 
693  bool m_possibleDoubleClick = false; // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
694  bool m_autoScroll = true; // Should the list view scroll to the bottom when a new item is added?
695 
696  // Cached renderer properties
697  Borders m_bordersCached;
698  Borders m_paddingCached;
699  Color m_borderColorCached;
700  Color m_separatorColorCached;
701  Color m_headerTextColorCached;
702  Color m_headerBackgroundColorCached;
703  Color m_backgroundColorCached;
704  Color m_backgroundColorHoverCached;
705  Color m_selectedBackgroundColorCached;
706  Color m_selectedBackgroundColorHoverCached;
707  Color m_textColorCached;
708  Color m_textColorHoverCached;
709  Color m_selectedTextColorCached;
710  Color m_selectedTextColorHoverCached;
711 
713  };
714 
716 }
717 
719 
720 #endif // TGUI_LIST_VIEW_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Show the scrollbar only when needed (default)
List view widget.
Definition: ListView.hpp:43
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:245
The parent class for every widget.
Definition: Widget.hpp:72
Definition: Vector2f.hpp:37
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ListView.hpp:656
Definition: ListViewRenderer.hpp:36
Definition: ListView.hpp:60
Definition: Text.hpp:42
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: ListView.hpp:66
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition: ListView.hpp:47
Definition: Texture.hpp:41
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38
ColumnAlignment
The text alignment for all texts within a column.
Definition: ListView.hpp:53
std::shared_ptr< const ListView > ConstPtr
Shared constant widget pointer.
Definition: ListView.hpp:48