TGUI  0.8.9
ListView.hpp
1
2//
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
38namespace 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
88
89
98
99
105 const ListViewRenderer* getSharedRenderer() const;
106
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
198
199
205 std::size_t getColumnCount() const;
206
207
213 void setHeaderHeight(float height);
214
215
221 float getHeaderHeight() const;
222
223
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
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
334 TGUI_DEPRECATED("Use deselectItems instead") void deselectItem();
335#endif
336
341
342
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) override;
487
488
496 void setHeaderTextSize(unsigned int textSize);
497
498
504 unsigned int getHeaderTextSize() const;
505
506
512 void setSeparatorWidth(unsigned int width);
513
514
520 unsigned int getSeparatorWidth() const;
521
522
528 void setHeaderSeparatorHeight(unsigned int height);
529
530
536 unsigned int getHeaderSeparatorHeight() const;
537
538
544 void setGridLinesWidth(unsigned int width);
545
546
552 unsigned int getGridLinesWidth() const;
553
554
562 void setAutoScroll(bool autoScroll);
563
564
570 bool getAutoScroll() const;
571
572
580 void setShowVerticalGridLines(bool showGridLines);
581
582
589
590
598 void setShowHorizontalGridLines(bool showGridLines);
599
600
607
608
616 void setExpandLastColumn(bool expand);
617
618
625
626
632
633
639
640
646
647
653
654
660 void setVerticalScrollbarValue(unsigned int value);
661
662
668 unsigned int getVerticalScrollbarValue() const;
669
670
676 void setHorizontalScrollbarValue(unsigned int value);
677
678
684 unsigned int getHorizontalScrollbarValue() const;
685
686
691 bool mouseOnWidget(Vector2f pos) const override;
692
696 void leftMousePressed(Vector2f pos) override;
697
701 void leftMouseReleased(Vector2f pos) override;
702
706 void rightMousePressed(Vector2f pos) override;
707
711 void mouseMoved(Vector2f pos) override;
712
716 bool mouseWheelScrolled(float delta, Vector2f pos) override;
717
721 void mouseNoLongerOnWidget() override;
722
726 void leftMouseButtonNoLongerDown() override;
727
728
735 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
736
737
739 protected:
740
750 Signal& getSignal(std::string signalName) override;
751
752
758 void rendererChanged(const std::string& property) override;
759
760
764 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
765
766
770 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
771
772
774 // Returns the size without the borders
776 Vector2f getInnerSize() const;
777
778
780 // Create a Text object for an item from the given caption, using the preset color, font, text size and opacity
782 Text createText(const sf::String& caption);
783
784
786 // Create a Text object for a header text from the given caption, using the preset color, font, text size and opacity
788 Text createHeaderText(const sf::String& caption);
789
790
792 // Changes the color of all Text objects in an item
794 virtual void setItemColor(std::size_t index, const Color& color);
795
796
798 // Calculate the width of the column based on its caption when no column width was provided
800 float calculateAutoColumnWidth(const Text& text);
801
802
804 // Update the colors of the selected and hovered items
806 void updateSelectedAndhoveredItemColors();
807
808
810 // Update the color of all the items
812 void updateItemColors();
813
814
816 // Changes the color of all header texts
818 void updateHeaderTextsColor();
819
820
822 // Update on which item the mouse is standing
824 void updateHoveredItem(int item);
825
826
828 // Update which item is selected
830 void updateSelectedItem(int item);
831
832
834 // Add item to selected set
836 void addSelectedItem(int item);
837
838
840 // Remove item from selected set
842 void removeSelectedItem(std::size_t item);
843
844
846 // Update on which item the mouse is standing, given the current mouse position
848 void updateHoveredItemByMousePos(Vector2f mousePos);
849
850
852 // Returns either the configured separator width or the width of vertical grid lines, whichever is larger.
854 unsigned int getTotalSeparatorWidth() const;
855
856
858 // Found out which column is located below the mouse. The mouseLeft is relative to the widget position.
859 // This function should only be called after checking that the mouse is positioned on top of the header.
861 int getColumnIndexBelowMouse(float mouseLeft);
862
863
865 // Recalculate the size and viewport size of the scrollbars
867 void updateScrollbars();
868
870 // Recalculate the maximum value for the vertical scrollbar
872 void updateVerticalScrollbarMaximum();
873
875 // Recalculate the maximum value for the horizontal scrollbar
877 void updateHorizontalScrollbarMaximum();
878
879
881 // Draw the header text for a single column
883 void drawHeaderText(sf::RenderTarget& target, sf::RenderStates states, float columnWidth, float headerHeight, std::size_t column) const;
884
885
887 // Draw the texts in a single column
889 void drawColumn(sf::RenderTarget& target, sf::RenderStates states, std::size_t firstItem, std::size_t lastItem, std::size_t column, float columnWidth) const;
890
891
893 // This function is called every frame with the time passed since the last frame.
895 bool update(sf::Time elapsedTime) override;
896
897
899 // Makes a copy of the widget
901 Widget::Ptr clone() const override
902 {
903 return std::make_shared<ListView>(*this);
904 }
905
906
908 public:
909
910 SignalInt onItemSelect = {"ItemSelected"};
911 SignalInt onDoubleClick = {"DoubleClicked"};
912 SignalInt onRightClick = {"RightClicked"};
913 SignalInt onHeaderClick = {"HeaderClicked"};
914
915
917 protected:
918
919 std::vector<Column> m_columns;
920 std::vector<Item> m_items;
921 std::set<std::size_t> m_selectedItems;
922
923 int m_hoveredItem = -1;
924 int m_lastMouseDownItem = -1;
925
926 float m_requestedHeaderHeight = 0;
927 unsigned int m_itemHeight = 0;
928 unsigned int m_requestedTextSize = 0;
929 unsigned int m_headerTextSize = 0;
930 unsigned int m_headerSeparatorHeight = 0;
931 unsigned int m_separatorWidth = 1;
932 unsigned int m_gridLinesWidth = 1;
933 unsigned int m_iconCount = 0;
934 float m_maxIconWidth = 0;
935 bool m_headerVisible = true;
936 bool m_showHorizontalGridLines = false;
937 bool m_showVerticalGridLines = true;
938 bool m_expandLastColumn = false;
939 bool m_multiSelect = false;
940
941 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
942 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
943 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
944 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
945
946 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
947 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
948 bool m_autoScroll = true; // Should the list view scroll to the bottom when a new item is added?
949
950 // Cached renderer properties
951 Borders m_bordersCached;
952 Borders m_paddingCached;
953 Color m_borderColorCached;
954 Color m_separatorColorCached;
955 Color m_gridLinesColorCached;
956 Color m_headerTextColorCached;
957 Color m_headerBackgroundColorCached;
958 Color m_backgroundColorCached;
959 Color m_backgroundColorHoverCached;
960 Color m_selectedBackgroundColorCached;
961 Color m_selectedBackgroundColorHoverCached;
962 Color m_textColorCached;
963 Color m_textColorHoverCached;
964 Color m_selectedTextColorCached;
965 Color m_selectedTextColorHoverCached;
966
968 };
969
971}
972
974
975#endif // TGUI_LIST_VIEW_HPP
Wrapper for colors.
Definition: Color.hpp:49
Definition: CopiedSharedPtr.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Definition: ListViewRenderer.hpp:37
List view widget.
Definition: ListView.hpp:46
void setShowVerticalGridLines(bool showGridLines)
Changes whether lines are drawn between columns.
Scrollbar::Policy getVerticalScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition: ListView.hpp:49
void setSelectedItems(const std::set< std::size_t > &indices)
Selects items in the list view.
void setTextSize(unsigned int textSize) override
Changes the text size of the items.
sf::String getItem(std::size_t index) const
Retrieves an item in the list.
bool getShowHorizontalGridLines() const
Returns whether lines are drawn between items.
void setHeaderTextSize(unsigned int textSize)
Changes the text size of the header caption.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
void setHorizontalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the horizontal scrollbar should be displayed.
void removeAllItems()
Removes all items from the list.
void setColumnAlignment(std::size_t columnIndex, ColumnAlignment alignment)
Changes the text alignment within a column.
std::vector< sf::String > getItemRow(std::size_t index) const
Retrieves the values of all columns for an item in the list.
ColumnAlignment getColumnAlignment(std::size_t columnIndex) const
Returns the current text alignment within a column.
std::size_t addItem(const sf::String &text)
Adds an item to the list.
void setColumnWidth(std::size_t index, float width)
Changes the width of a column.
sf::String getColumnText(std::size_t index) const
Returns the text of a column.
static ListView::Ptr copy(ListView::ConstPtr listView)
Makes a copy of another list view.
std::shared_ptr< const ListView > ConstPtr
Shared constant widget pointer.
Definition: ListView.hpp:50
unsigned int getSeparatorWidth() const
Returns the width of the column separator.
void setSize(const Layout2d &size) override
Changes the size of the list view.
void setHeaderVisible(bool showHeader)
Changes whether the header is shown.
std::vector< sf::String > getItems() const
Returns a list of the texts in the first column for all items in the list view.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
bool getExpandLastColumn() const
Returns whether the last column is expanded to fill the list view (if all columns fit inside the list...
float getHeaderHeight() const
Returns the height of the header row.
std::size_t getColumnCount() const
Returns the amount of columns in the list view.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
bool changeItem(std::size_t index, const std::vector< sf::String > &item)
Changes an item with values for multiple columns to the list.
unsigned int getGridLinesWidth() const
Returns the width of the grid lines.
void sort(std::size_t index, const std::function< bool(const sf::String &, const sf::String &)> &cmp)
Sort items.
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
bool getHeaderVisible() const
Returns whether the header is shown.
int getSelectedItemIndex() const
Gets the index of the selected item.
unsigned int getItemHeight() const
Returns the height of the items in the list view.
void setItemIcon(std::size_t index, const Texture &texture)
Sets a small icon in front of the item.
float getColumnWidth(std::size_t index) const
Returns the width of a column.
void setVerticalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
ColumnAlignment
The text alignment for all texts within a column.
Definition: ListView.hpp:56
static ListView::Ptr create()
Creates a new list view widget.
std::size_t getItemCount() const
Returns the amount of items in the list view.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
ListViewRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getAutoScroll() const
Returns whether the list view scrolls to the bottom when a new item is added.
void setColumnText(std::size_t index, const sf::String &text)
Changes the text of a column.
void setHeaderHeight(float height)
Changes the height of the header row.
void setShowHorizontalGridLines(bool showGridLines)
Changes whether lines are drawn between items.
void deselectItems()
Deselects the selected items.
void addMultipleItems(const std::vector< std::vector< sf::String > > &items)
Adds multiple items to the list.
void setExpandLastColumn(bool expand)
Changes whether the last column is expanded to fill the list view (if all columns fit inside the list...
void setAutoScroll(bool autoScroll)
Changes whether the list view scrolls to the bottom when a new item is added.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ListView.hpp:901
std::set< std::size_t > getSelectedItemIndices() const
Gets the indices of the selected items.
void setItemHeight(unsigned int itemHeight)
Changes the height of the items in the list view.
void setSelectedItem(std::size_t index)
Selects an item in the list view.
void setSeparatorWidth(unsigned int width)
Changes the width of the column separator.
bool getMultiSelect() const
Returns multi selection of the items is allowed.
void setHeaderSeparatorHeight(unsigned int height)
Changes the height of the separator between the header and the items.
unsigned int getHeaderTextSize() const
Returns the text size of the header caption.
Texture getItemIcon(std::size_t index) const
Gets the icon displayed in front of the item.
Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
void setMultiSelect(bool multiSelect)
Allow multi selection of the items.
void removeAllColumns()
Removes all columns.
sf::String getItemCell(std::size_t rowIndex, std::size_t columnIndex) const
Retrieves the value for a cell in the list.
bool removeItem(std::size_t index)
Removes the item from the list view.
float getCurrentHeaderHeight() const
Returns the height of the header or 0 if no header row is shown.
bool getShowVerticalGridLines() const
Returns whether lines are drawn between items.
std::size_t addColumn(const sf::String &text, float width=0, ColumnAlignment alignment=ColumnAlignment::Left)
Adds a column.
unsigned int getHeaderSeparatorHeight() const
Returns the height of the separator between the header and the items.
void setGridLinesWidth(unsigned int width)
Changes the width of the grid lines.
ListViewRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::vector< std::vector< sf::String > > getItemRows() const
Returns a list of all column values for all items in the list view.
std::size_t addItem(const std::vector< sf::String > &item)
Adds an item with values for multiple columns to the list.
bool changeSubItem(std::size_t index, std::size_t column, const sf::String &item)
Changes the caption of a single value in the item.
Definition: Outline.hpp:39
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
@ Automatic
Show the scrollbar only when needed (default)
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:231
Definition: Sprite.hpp:46
Definition: Text.hpp:43
Definition: Texture.hpp:42
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Definition: ListView.hpp:69
Definition: ListView.hpp:63