TGUI  0.9-dev
ListView.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_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 #include <set>
35 
37 
38 namespace tgui
39 {
45  class TGUI_API ListView : public Widget
46  {
47  public:
48 
49  typedef std::shared_ptr<ListView> Ptr;
50  typedef std::shared_ptr<const ListView> ConstPtr;
51 
55  enum class ColumnAlignment
56  {
57  Left,
58  Center,
59  Right
60  };
61 
62  struct Item
63  {
64  std::vector<Text> texts;
65  Any data;
66  Sprite icon;
67  };
68 
69  struct Column
70  {
71  float width = 0;
72  float designWidth = 0;
73  Text text;
74  ColumnAlignment alignment = ColumnAlignment::Left;
75  };
76 
77 
79  // Default constructor
81  ListView();
82 
83 
88  static ListView::Ptr create();
89 
90 
98  static ListView::Ptr copy(ListView::ConstPtr listView);
99 
100 
105  ListViewRenderer* getSharedRenderer();
106  const ListViewRenderer* getSharedRenderer() const;
107 
113  ListViewRenderer* getRenderer();
114  const ListViewRenderer* getRenderer() const;
115 
116 
122  void setSize(const Layout2d& size) override;
123  using Widget::setSize;
124 
125 
135  std::size_t addColumn(const String& text, float width = 0, ColumnAlignment alignment = ColumnAlignment::Left);
136 
137 
144  void setColumnText(std::size_t index, const String& text);
145 
146 
154  String getColumnText(std::size_t index) const;
155 
156 
163  void setColumnWidth(std::size_t index, float width);
164 
165 
173  float getColumnWidth(std::size_t index) const;
174 
175 
182  void setColumnAlignment(std::size_t columnIndex, ColumnAlignment alignment);
183 
184 
192  ColumnAlignment getColumnAlignment(std::size_t columnIndex) const;
193 
194 
198  void removeAllColumns();
199 
200 
206  std::size_t getColumnCount() const;
207 
208 
214  void setHeaderHeight(float height);
215 
216 
222  float getHeaderHeight() const;
223 
224 
230  float getCurrentHeaderHeight() const;
231 
232 
238  void setHeaderVisible(bool showHeader);
239 
240 
246  bool getHeaderVisible() const;
247 
248 
256  std::size_t addItem(const String& text);
257 
258 
266  std::size_t addItem(const std::vector<String>& item);
267 
268 
274  void addMultipleItems(const std::vector<std::vector<String>>& items);
275 
276 
285  bool changeItem(std::size_t index, const std::vector<String>& item);
286 
287 
297  bool changeSubItem(std::size_t index, std::size_t column, const String& item);
298 
299 
307  bool removeItem(std::size_t index);
308 
309 
313  void removeAllItems();
314 
315 
321  void setSelectedItem(std::size_t index);
322 
323 
329  void setSelectedItems(const std::set<std::size_t>& indices);
330 
331 
335  void deselectItems();
336 
337 
343  int getSelectedItemIndex() const;
344 
345 
351  std::set<std::size_t> getSelectedItemIndices() const;
352 
353 
359  void setMultiSelect(bool multiSelect);
360 
361 
367  bool getMultiSelect() const;
368 
369 
381  void setItemData(std::size_t index, Any data);
382 
383 
390  template <typename T>
391  T getItemData(std::size_t index) const
392  {
393  if (index < m_items.size())
394  return AnyCast<T>(m_items[index].data);
395  else
396  throw std::bad_cast();
397  }
398 
399 
406  void setItemIcon(std::size_t index, const Texture& texture);
407 
408 
416  Texture getItemIcon(std::size_t index) const;
417 
418 
424  std::size_t getItemCount() const;
425 
426 
434  String getItem(std::size_t index) const;
435 
436 
446  std::vector<String> getItemRow(std::size_t index) const;
447 
448 
457  String getItemCell(std::size_t rowIndex, std::size_t columnIndex) const;
458 
459 
465  std::vector<String> getItems() const;
466 
467 
473  std::vector<std::vector<String>> getItemRows() const;
474 
475 
482  void sort(std::size_t index, const std::function<bool(const String&, const String&)>& cmp);
483 
484 
490  void setItemHeight(unsigned int itemHeight);
491 
492 
498  unsigned int getItemHeight() const;
499 
500 
511  void setTextSize(unsigned int textSize) override;
512 
513 
521  void setHeaderTextSize(unsigned int textSize);
522 
523 
529  unsigned int getHeaderTextSize() const;
530 
531 
537  void setSeparatorWidth(unsigned int width);
538 
539 
545  unsigned int getSeparatorWidth() const;
546 
547 
553  void setHeaderSeparatorHeight(unsigned int height);
554 
555 
561  unsigned int getHeaderSeparatorHeight() const;
562 
563 
569  void setGridLinesWidth(unsigned int width);
570 
571 
577  unsigned int getGridLinesWidth() const;
578 
579 
587  void setAutoScroll(bool autoScroll);
588 
589 
595  bool getAutoScroll() const;
596 
597 
605  void setShowVerticalGridLines(bool showGridLines);
606 
607 
613  bool getShowVerticalGridLines() const;
614 
615 
623  void setShowHorizontalGridLines(bool showGridLines);
624 
625 
631  bool getShowHorizontalGridLines() const;
632 
633 
641  void setExpandLastColumn(bool expand);
642 
643 
649  bool getExpandLastColumn() const;
650 
651 
656  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
657 
658 
663  Scrollbar::Policy getVerticalScrollbarPolicy() const;
664 
665 
670  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
671 
672 
677  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
678 
679 
685  void setVerticalScrollbarValue(unsigned int value);
686 
687 
693  unsigned int getVerticalScrollbarValue() const;
694 
695 
701  void setHorizontalScrollbarValue(unsigned int value);
702 
703 
709  unsigned int getHorizontalScrollbarValue() const;
710 
711 
716  bool mouseOnWidget(Vector2f pos) const override;
717 
721  void leftMousePressed(Vector2f pos) override;
722 
726  void leftMouseReleased(Vector2f pos) override;
727 
731  void rightMousePressed(Vector2f pos) override;
732 
736  void mouseMoved(Vector2f pos) override;
737 
741  bool mouseWheelScrolled(float delta, Vector2f pos) override;
742 
746  void mouseNoLongerOnWidget() override;
747 
751  void leftMouseButtonNoLongerDown() override;
752 
753 
760  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
761 
762 
764  protected:
765 
775  Signal& getSignal(String signalName) override;
776 
777 
783  void rendererChanged(const String& property) override;
784 
785 
789  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
790 
791 
795  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
796 
797 
799  // Returns the size without the borders
801  Vector2f getInnerSize() const;
802 
803 
805  // Create a Text object for an item from the given caption, using the preset color, font, text size and opacity
807  Text createText(const String& caption);
808 
809 
811  // Create a Text object for a header text from the given caption, using the preset color, font, text size and opacity
813  Text createHeaderText(const String& caption);
814 
815 
817  // Changes the color of all Text objects in an item
819  virtual void setItemColor(std::size_t index, const Color& color);
820 
821 
823  // Calculate the width of the column based on its caption when no column width was provided
825  float calculateAutoColumnWidth(const Text& text);
826 
827 
829  // Update the colors of the selected and hovered items
831  void updateSelectedAndhoveredItemColors();
832 
833 
835  // Update the color of all the items
837  void updateItemColors();
838 
839 
841  // Changes the color of all header texts
843  void updateHeaderTextsColor();
844 
845 
847  // Update on which item the mouse is standing
849  void updateHoveredItem(int item);
850 
851 
853  // Update which item is selected
855  void updateSelectedItem(int item);
856 
857 
859  // Add item to selected set
861  void addSelectedItem(int item);
862 
863 
865  // Remove item from selected set
867  void removeSelectedItem(std::size_t item);
868 
869 
871  // Update on which item the mouse is standing, given the current mouse position
873  void updateHoveredItemByMousePos(Vector2f mousePos);
874 
875 
877  // Returns either the configured separator width or the width of vertical grid lines, whichever is larger.
879  unsigned int getTotalSeparatorWidth() const;
880 
881 
883  // Found out which column is located below the mouse. The mouseLeft is relative to the widget position.
884  // This function should only be called after checking that the mouse is positioned on top of the header.
886  int getColumnIndexBelowMouse(float mouseLeft);
887 
888 
890  // Recalculate the size and viewport size of the scrollbars
892  void updateScrollbars();
893 
895  // Recalculate the maximum value for the vertical scrollbar
897  void updateVerticalScrollbarMaximum();
898 
900  // Recalculate the maximum value for the horizontal scrollbar
902  void updateHorizontalScrollbarMaximum();
903 
904 
906  // Draw the header text for a single column
908  void drawHeaderText(sf::RenderTarget& target, sf::RenderStates states, float columnWidth, float headerHeight, std::size_t column) const;
909 
910 
912  // Draw the texts in a single column
914  void drawColumn(sf::RenderTarget& target, sf::RenderStates states, std::size_t firstItem, std::size_t lastItem, std::size_t column, float columnWidth) const;
915 
916 
918  // This function is called every frame with the time passed since the last frame.
920  void update(Duration elapsedTime) override;
921 
922 
924  // Makes a copy of the widget
926  Widget::Ptr clone() const override
927  {
928  return std::make_shared<ListView>(*this);
929  }
930 
931 
933  public:
934 
935  SignalInt onItemSelect = {"ItemSelected"};
936  SignalInt onDoubleClick = {"DoubleClicked"};
937  SignalInt onRightClick = {"RightClicked"};
938  SignalInt onHeaderClick = {"HeaderClicked"};
939 
940 
942  protected:
943 
944  std::vector<Column> m_columns;
945  std::vector<Item> m_items;
946  std::set<std::size_t> m_selectedItems;
947 
948  int m_hoveredItem = -1;
949  int m_lastMouseDownItem = -1;
950 
951  float m_requestedHeaderHeight = 0;
952  unsigned int m_itemHeight = 0;
953  unsigned int m_requestedTextSize = 0;
954  unsigned int m_headerTextSize = 0;
955  unsigned int m_headerSeparatorHeight = 0;
956  unsigned int m_separatorWidth = 1;
957  unsigned int m_gridLinesWidth = 1;
958  unsigned int m_iconCount = 0;
959  float m_maxIconWidth = 0;
960  bool m_headerVisible = true;
961  bool m_showHorizontalGridLines = false;
962  bool m_showVerticalGridLines = true;
963  bool m_expandLastColumn = false;
964  bool m_multiSelect = false;
965 
966  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
967  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
968  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
969  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
970 
971  int m_mouseOnHeaderIndex = -1; // If the left mouse is down, this contains the index of the column if the mouse went down on the header
972  int m_possibleDoubleClick = -1; // Will be set to index of item after the first click, but gets reset to -1 when the second click does not occur soon after
973  bool m_autoScroll = true; // Should the list view scroll to the bottom when a new item is added?
974 
975  // Cached renderer properties
976  Borders m_bordersCached;
977  Borders m_paddingCached;
978  Color m_borderColorCached;
979  Color m_separatorColorCached;
980  Color m_gridLinesColorCached;
981  Color m_headerTextColorCached;
982  Color m_headerBackgroundColorCached;
983  Color m_backgroundColorCached;
984  Color m_backgroundColorHoverCached;
985  Color m_selectedBackgroundColorCached;
986  Color m_selectedBackgroundColorHoverCached;
987  Color m_textColorCached;
988  Color m_textColorHoverCached;
989  Color m_selectedTextColorCached;
990  Color m_selectedTextColorHoverCached;
991 
993  };
994 
996 }
997 
999 
1000 #endif // TGUI_LIST_VIEW_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
Definition: ListView.hpp:62
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition: ListView.hpp:49
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
ColumnAlignment
The text alignment for all texts within a column.
Definition: ListView.hpp:55
Definition: Text.hpp:42
std::shared_ptr< const ListView > ConstPtr
Shared constant widget pointer.
Definition: ListView.hpp:50
@ Automatic
Show the scrollbar only when needed (default)
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:47
T getItemData(std::size_t index) const
Returns user data stored in the item.
Definition: ListView.hpp:391
Definition: ListView.hpp:69
Wrapper for durations.
Definition: Duration.hpp:48
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListView.hpp:926
Definition: Sprite.hpp:45
Definition: Texture.hpp:42
Wrapper class to store strings.
Definition: String.hpp:70
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:68
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: ListViewRenderer.hpp:36
List view widget.
Definition: ListView.hpp:45