TGUI  0.8.5
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) const;
152 
153 
160  void setColumnWidth(std::size_t index, float width);
161 
162 
170  float getColumnWidth(std::size_t index) const;
171 
172 
179  void setColumnAlignment(std::size_t columnIndex, ColumnAlignment alignment);
180 
181 
189  ColumnAlignment getColumnAlignment(std::size_t 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  float getCurrentHeaderHeight() const;
228 
229 
235  void setHeaderVisible(bool showHeader);
236 
237 
243  bool getHeaderVisible() const;
244 
245 
253  std::size_t addItem(const sf::String& text);
254 
255 
263  std::size_t addItem(const std::vector<sf::String>& item);
264 
265 
271  void addMultipleItems(const std::vector<std::vector<sf::String>>& items);
272 
273 
282  bool changeItem(std::size_t index, const std::vector<sf::String>& item);
283 
284 
294  bool changeSubItem(std::size_t index, std::size_t column, const sf::String& item);
295 
296 
304  bool removeItem(std::size_t index);
305 
306 
310  void removeAllItems();
311 
312 
318  void setSelectedItem(std::size_t index);
319 
320 
324  void deselectItem();
325 
326 
332  int getSelectedItemIndex() const;
333 
334 
341  void setItemIcon(std::size_t index, const Texture& texture);
342 
343 
351  Texture getItemIcon(std::size_t index) const;
352 
353 
359  std::size_t getItemCount() const;
360 
361 
369  sf::String getItem(std::size_t index) const;
370 
371 
381  std::vector<sf::String> getItemRow(std::size_t index) const;
382 
383 
389  std::vector<sf::String> getItems() const;
390 
391 
397  std::vector<std::vector<sf::String>> getItemRows() const;
398 
399 
405  void setItemHeight(unsigned int itemHeight);
406 
407 
413  unsigned int getItemHeight() const;
414 
415 
426  void setTextSize(unsigned int textSize);
427 
428 
434  unsigned int getTextSize() const;
435 
436 
444  void setHeaderTextSize(unsigned int textSize);
445 
446 
452  unsigned int getHeaderTextSize() const;
453 
454 
460  void setSeparatorWidth(unsigned int width);
461 
462 
468  unsigned int getSeparatorWidth() const;
469 
470 
476  void setHeaderSeparatorHeight(unsigned int height);
477 
478 
484  unsigned int getHeaderSeparatorHeight() const;
485 
486 
492  void setGridLinesWidth(unsigned int width);
493 
494 
500  unsigned int getGridLinesWidth() const;
501 
502 
510  void setAutoScroll(bool autoScroll);
511 
512 
518  bool getAutoScroll() const;
519 
520 
528  void setShowVerticalGridLines(bool showGridLines);
529 
530 
536  bool getShowVerticalGridLines() const;
537 
538 
546  void setShowHorizontalGridLines(bool showGridLines);
547 
548 
554  bool getShowHorizontalGridLines() const;
555 
556 
564  void setExpandLastColumn(bool expand);
565 
566 
572  bool getExpandLastColumn() const;
573 
574 
579  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
580 
581 
586  Scrollbar::Policy getVerticalScrollbarPolicy() const;
587 
588 
593  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
594 
595 
600  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
601 
602 
607  bool mouseOnWidget(Vector2f pos) const override;
608 
612  void leftMousePressed(Vector2f pos) override;
613 
617  void leftMouseReleased(Vector2f pos) override;
618 
622  void rightMousePressed(Vector2f pos) override;
623 
627  void mouseMoved(Vector2f pos) override;
628 
632  bool mouseWheelScrolled(float delta, Vector2f pos) override;
633 
637  void mouseNoLongerOnWidget() override;
638 
642  void mouseNoLongerDown() override;
643 
644 
651  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
652 
653 
655  protected:
656 
666  Signal& getSignal(std::string signalName) override;
667 
668 
674  void rendererChanged(const std::string& property) override;
675 
676 
680  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
681 
682 
686  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
687 
688 
690  // Returns the size without the borders
692  Vector2f getInnerSize() const;
693 
694 
696  // Create a Text object for an item from the given caption, using the preset color, font, text size and opacity
698  Text createText(const sf::String& caption);
699 
700 
702  // Create a Text object for a header text from the given caption, using the preset color, font, text size and opacity
704  Text createHeaderText(const sf::String& caption);
705 
706 
708  // Changes the color of all Text objects in an item
710  void setItemColor(std::size_t index, const Color& color);
711 
712 
714  // Calculate the width of the column based on its caption when no column width was provided
716  float calculateAutoColumnWidth(const Text& text);
717 
718 
720  // Update the colors of the selected and hovered items
722  void updateSelectedAndhoveredItemColors();
723 
724 
726  // Update the color of all the items
728  void updateItemColors();
729 
730 
732  // Changes the color of all header texts
734  void updateHeaderTextsColor();
735 
736 
738  // Update on which item the mouse is standing
740  void updateHoveredItem(int item);
741 
742 
744  // Update which item is selected
746  void updateSelectedItem(int item);
747 
748 
750  // Update on which item the mouse is standing, given the current mouse position
752  void updateHoveredItemByMousePos(Vector2f mousePos);
753 
754 
756  // Recalculate the size and viewport size of the scrollbars
758  void updateScrollbars();
759 
761  // Recalculate the maximum value for the vertical scrollbar
763  void updateVerticalScrollbarMaximum();
764 
766  // Recalculate the maximum value for the horizontal scrollbar
768  void updateHorizontalScrollbarMaximum();
769 
770 
772  // Draw the header text for a single column
774  void drawHeaderText(sf::RenderTarget& target, sf::RenderStates states, float columnWidth, float headerHeight, std::size_t column) const;
775 
776 
778  // Draw the texts in a single column
780  void drawColumn(sf::RenderTarget& target, sf::RenderStates states, std::size_t firstItem, std::size_t lastItem, std::size_t column, float columnWidth) const;
781 
782 
784  // This function is called every frame with the time passed since the last frame.
786  void update(sf::Time elapsedTime) override;
787 
788 
790  // Makes a copy of the widget
792  Widget::Ptr clone() const override
793  {
794  return std::make_shared<ListView>(*this);
795  }
796 
797 
799  public:
800 
801  SignalInt onItemSelect = {"ItemSelected"};
802  SignalInt onDoubleClick = {"DoubleClicked"};
803  SignalInt onRightClick = {"RightClicked"};
804 
805 
807  protected:
808 
809  std::vector<Column> m_columns;
810  std::vector<Item> m_items;
811 
812  int m_selectedItem = -1;
813  int m_hoveredItem = -1;
814 
815  float m_requestedHeaderHeight = 0;
816  unsigned int m_itemHeight = 0;
817  unsigned int m_requestedTextSize = 0;
818  unsigned int m_textSize = 0;
819  unsigned int m_headerTextSize = 0;
820  unsigned int m_headerSeparatorHeight = 0;
821  unsigned int m_separatorWidth = 1;
822  unsigned int m_gridLinesWidth = 1;
823  unsigned int m_iconCount = 0;
824  float m_maxIconWidth = 0;
825  bool m_headerVisible = true;
826  bool m_showHorizontalGridLines = false;
827  bool m_showVerticalGridLines = true;
828  bool m_expandLastColumn = false;
829 
830  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
831  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
832  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
833  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
834 
835  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
836  bool m_autoScroll = true; // Should the list view scroll to the bottom when a new item is added?
837 
838  // Cached renderer properties
839  Borders m_bordersCached;
840  Borders m_paddingCached;
841  Color m_borderColorCached;
842  Color m_separatorColorCached;
843  Color m_gridLinesColorCached;
844  Color m_headerTextColorCached;
845  Color m_headerBackgroundColorCached;
846  Color m_backgroundColorCached;
847  Color m_backgroundColorHoverCached;
848  Color m_selectedBackgroundColorCached;
849  Color m_selectedBackgroundColorHoverCached;
850  Color m_textColorCached;
851  Color m_textColorHoverCached;
852  Color m_selectedTextColorCached;
853  Color m_selectedTextColorHoverCached;
854 
856  };
857 
859 }
860 
862 
863 #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:45
Wrapper for colors.
Definition: Color.hpp:48
Class to store the position or size of a widget.
Definition: Layout.hpp:258
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't know its exact type.
Definition: ListView.hpp:792
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