TGUI  0.8.6
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 #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  Sprite icon;
66  };
67 
68  struct Column
69  {
70  float width = 0;
71  float designWidth = 0;
72  Text text;
73  ColumnAlignment alignment = ColumnAlignment::Left;
74  };
75 
76 
78  // Default constructor
80  ListView();
81 
82 
87  static ListView::Ptr create();
88 
89 
97  static ListView::Ptr copy(ListView::ConstPtr listView);
98 
99 
104  ListViewRenderer* getSharedRenderer();
105  const ListViewRenderer* getSharedRenderer() const;
106 
112  ListViewRenderer* getRenderer();
113  const ListViewRenderer* getRenderer() const;
114 
115 
121  void setSize(const Layout2d& size) override;
122  using Widget::setSize;
123 
124 
134  std::size_t addColumn(const sf::String& text, float width = 0, ColumnAlignment alignment = ColumnAlignment::Left);
135 
136 
143  void setColumnText(std::size_t index, const sf::String& text);
144 
145 
153  sf::String getColumnText(std::size_t index) const;
154 
155 
162  void setColumnWidth(std::size_t index, float width);
163 
164 
172  float getColumnWidth(std::size_t index) const;
173 
174 
181  void setColumnAlignment(std::size_t columnIndex, ColumnAlignment alignment);
182 
183 
191  ColumnAlignment getColumnAlignment(std::size_t columnIndex) const;
192 
193 
197  void removeAllColumns();
198 
199 
205  std::size_t getColumnCount() const;
206 
207 
213  void setHeaderHeight(float height);
214 
215 
221  float getHeaderHeight() const;
222 
223 
229  float getCurrentHeaderHeight() const;
230 
231 
237  void setHeaderVisible(bool showHeader);
238 
239 
245  bool getHeaderVisible() const;
246 
247 
255  std::size_t addItem(const sf::String& text);
256 
257 
265  std::size_t addItem(const std::vector<sf::String>& item);
266 
267 
273  void addMultipleItems(const std::vector<std::vector<sf::String>>& items);
274 
275 
284  bool changeItem(std::size_t index, const std::vector<sf::String>& item);
285 
286 
296  bool changeSubItem(std::size_t index, std::size_t column, const sf::String& item);
297 
298 
306  bool removeItem(std::size_t index);
307 
308 
312  void removeAllItems();
313 
314 
320  void setSelectedItem(std::size_t index);
321 
322 
328  void setSelectedItems(const std::set<std::size_t>& indices);
329 
330 #ifndef TGUI_REMOVE_DEPRECATED_CODE
331  TGUI_DEPRECATED("Use deselectItems instead") void deselectItem();
335 #endif
336 
340  void deselectItems();
341 
342 
348  int getSelectedItemIndex() const;
349 
350 
356  std::set<std::size_t> getSelectedItemIndices() const;
357 
358 
364  void setMultiSelect(bool multiSelect);
365 
366 
372  bool getMultiSelect() const;
373 
374 
381  void setItemIcon(std::size_t index, const Texture& texture);
382 
383 
391  Texture getItemIcon(std::size_t index) const;
392 
393 
399  std::size_t getItemCount() const;
400 
401 
409  sf::String getItem(std::size_t index) const;
410 
411 
421  std::vector<sf::String> getItemRow(std::size_t index) const;
422 
423 
432  sf::String getItemCell(std::size_t rowIndex, std::size_t columnIndex) const;
433 
434 
440  std::vector<sf::String> getItems() const;
441 
442 
448  std::vector<std::vector<sf::String>> getItemRows() const;
449 
450 
457  void sort(std::size_t index, const std::function<bool(const sf::String&, const sf::String&)>& cmp);
458 
459 
465  void setItemHeight(unsigned int itemHeight);
466 
467 
473  unsigned int getItemHeight() const;
474 
475 
486  void setTextSize(unsigned int textSize);
487 
488 
494  unsigned int getTextSize() const;
495 
496 
504  void setHeaderTextSize(unsigned int textSize);
505 
506 
512  unsigned int getHeaderTextSize() const;
513 
514 
520  void setSeparatorWidth(unsigned int width);
521 
522 
528  unsigned int getSeparatorWidth() const;
529 
530 
536  void setHeaderSeparatorHeight(unsigned int height);
537 
538 
544  unsigned int getHeaderSeparatorHeight() const;
545 
546 
552  void setGridLinesWidth(unsigned int width);
553 
554 
560  unsigned int getGridLinesWidth() const;
561 
562 
570  void setAutoScroll(bool autoScroll);
571 
572 
578  bool getAutoScroll() const;
579 
580 
588  void setShowVerticalGridLines(bool showGridLines);
589 
590 
596  bool getShowVerticalGridLines() const;
597 
598 
606  void setShowHorizontalGridLines(bool showGridLines);
607 
608 
614  bool getShowHorizontalGridLines() const;
615 
616 
624  void setExpandLastColumn(bool expand);
625 
626 
632  bool getExpandLastColumn() const;
633 
634 
639  void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
640 
641 
646  Scrollbar::Policy getVerticalScrollbarPolicy() const;
647 
648 
653  void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
654 
655 
660  Scrollbar::Policy getHorizontalScrollbarPolicy() const;
661 
662 
668  void setVerticalScrollbarValue(unsigned int value);
669 
670 
676  unsigned int getVerticalScrollbarValue() const;
677 
678 
684  void setHorizontalScrollbarValue(unsigned int value);
685 
686 
692  unsigned int getHorizontalScrollbarValue() const;
693 
694 
699  bool mouseOnWidget(Vector2f pos) const override;
700 
704  void leftMousePressed(Vector2f pos) override;
705 
709  void leftMouseReleased(Vector2f pos) override;
710 
714  void rightMousePressed(Vector2f pos) override;
715 
719  void mouseMoved(Vector2f pos) override;
720 
724  bool mouseWheelScrolled(float delta, Vector2f pos) override;
725 
729  void mouseNoLongerOnWidget() override;
730 
734  void leftMouseButtonNoLongerDown() override;
735 
736 
743  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
744 
745 
747  protected:
748 
758  Signal& getSignal(std::string signalName) override;
759 
760 
766  void rendererChanged(const std::string& property) override;
767 
768 
772  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
773 
774 
778  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
779 
780 
782  // Returns the size without the borders
784  Vector2f getInnerSize() const;
785 
786 
788  // Create a Text object for an item from the given caption, using the preset color, font, text size and opacity
790  Text createText(const sf::String& caption);
791 
792 
794  // Create a Text object for a header text from the given caption, using the preset color, font, text size and opacity
796  Text createHeaderText(const sf::String& caption);
797 
798 
800  // Changes the color of all Text objects in an item
802  virtual void setItemColor(std::size_t index, const Color& color);
803 
804 
806  // Calculate the width of the column based on its caption when no column width was provided
808  float calculateAutoColumnWidth(const Text& text);
809 
810 
812  // Update the colors of the selected and hovered items
814  void updateSelectedAndhoveredItemColors();
815 
816 
818  // Update the color of all the items
820  void updateItemColors();
821 
822 
824  // Changes the color of all header texts
826  void updateHeaderTextsColor();
827 
828 
830  // Update on which item the mouse is standing
832  void updateHoveredItem(int item);
833 
834 
836  // Update which item is selected
838  void updateSelectedItem(int item);
839 
840 
842  // Add item to selected set
844  void addSelectedItem(int item);
845 
846 
848  // Remove item from selected set
850  void removeSelectedItem(std::size_t item);
851 
852 
854  // Update on which item the mouse is standing, given the current mouse position
856  void updateHoveredItemByMousePos(Vector2f mousePos);
857 
858 
860  // Returns either the configured separator width or the width of vertical grid lines, whichever is larger.
862  unsigned int getTotalSeparatorWidth() const;
863 
864 
866  // Found out which column is located below the mouse. The mouseLeft is relative to the widget position.
867  // This function should only be called after checking that the mouse is positioned on top of the header.
869  int getColumnIndexBelowMouse(float mouseLeft);
870 
871 
873  // Recalculate the size and viewport size of the scrollbars
875  void updateScrollbars();
876 
878  // Recalculate the maximum value for the vertical scrollbar
880  void updateVerticalScrollbarMaximum();
881 
883  // Recalculate the maximum value for the horizontal scrollbar
885  void updateHorizontalScrollbarMaximum();
886 
887 
889  // Draw the header text for a single column
891  void drawHeaderText(sf::RenderTarget& target, sf::RenderStates states, float columnWidth, float headerHeight, std::size_t column) const;
892 
893 
895  // Draw the texts in a single column
897  void drawColumn(sf::RenderTarget& target, sf::RenderStates states, std::size_t firstItem, std::size_t lastItem, std::size_t column, float columnWidth) const;
898 
899 
901  // This function is called every frame with the time passed since the last frame.
903  void update(sf::Time elapsedTime) override;
904 
905 
907  // Makes a copy of the widget
909  Widget::Ptr clone() const override
910  {
911  return std::make_shared<ListView>(*this);
912  }
913 
914 
916  public:
917 
918  SignalInt onItemSelect = {"ItemSelected"};
919  SignalInt onDoubleClick = {"DoubleClicked"};
920  SignalInt onRightClick = {"RightClicked"};
921  SignalInt onHeaderClick = {"HeaderClicked"};
922 
923 
925  protected:
926 
927  std::vector<Column> m_columns;
928  std::vector<Item> m_items;
929  std::set<std::size_t> m_selectedItems;
930 
931  int m_hoveredItem = -1;
932  int m_lastMouseDownItem = -1;
933 
934  float m_requestedHeaderHeight = 0;
935  unsigned int m_itemHeight = 0;
936  unsigned int m_requestedTextSize = 0;
937  unsigned int m_textSize = 0;
938  unsigned int m_headerTextSize = 0;
939  unsigned int m_headerSeparatorHeight = 0;
940  unsigned int m_separatorWidth = 1;
941  unsigned int m_gridLinesWidth = 1;
942  unsigned int m_iconCount = 0;
943  float m_maxIconWidth = 0;
944  bool m_headerVisible = true;
945  bool m_showHorizontalGridLines = false;
946  bool m_showVerticalGridLines = true;
947  bool m_expandLastColumn = false;
948  bool m_multiSelect = false;
949 
950  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
951  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
952  Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
953  Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
954 
955  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
956  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
957  bool m_autoScroll = true; // Should the list view scroll to the bottom when a new item is added?
958 
959  // Cached renderer properties
960  Borders m_bordersCached;
961  Borders m_paddingCached;
962  Color m_borderColorCached;
963  Color m_separatorColorCached;
964  Color m_gridLinesColorCached;
965  Color m_headerTextColorCached;
966  Color m_headerBackgroundColorCached;
967  Color m_backgroundColorCached;
968  Color m_backgroundColorHoverCached;
969  Color m_selectedBackgroundColorCached;
970  Color m_selectedBackgroundColorHoverCached;
971  Color m_textColorCached;
972  Color m_textColorHoverCached;
973  Color m_selectedTextColorCached;
974  Color m_selectedTextColorHoverCached;
975 
977  };
978 
980 }
981 
983 
984 #endif // TGUI_LIST_VIEW_HPP
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
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:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
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
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:48
Definition: ListView.hpp:68
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListView.hpp:909
Definition: Sprite.hpp:45
Definition: Texture.hpp:41
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
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.
Definition: ListViewRenderer.hpp:36
List view widget.
Definition: ListView.hpp:45