listview and add item

Started by billarhos, 24 February 2019, 16:03:52


Hi Texus

You have:

1 : std::size_t addItem(const sf::String& text);
2: std::size_t addItem(const std::vector<sf::String>& item);
3 : bool removeItem(std::size_t index);

4 : void setSelectedItem(std::size_t index);
5: int getSelectedItemIndex() const;
i am sure that i forget some other functions

1. Add item as a row or in a specific cell in a specific column

std::size_t addItem(std::size_t columnIndex, const sf::String& text);

2: should be

std::size_t addRow(const std::vector<sf::String>& item);

3. is remove item - or remove row
4. setSelectedItem on which column
5. is selected row or selected a specific cell in a specific column

there is a need to be distiguish the cell index and the column index.
Also there is need for a "setCell(column, cell, text)" function like :

void setColumnText(std::size_t index, const sf::String& text);

and may be a

std::size_t addMultiRows(const std::vector<std::vector<sf::String>> & item);

thank you


Although I don't want the API to be the same as the corresponding Windows component, I was trying to keep it somewhat similar. So I'm a bit hesitant to make some of these changes.

1. I see an item as an entire row, so an addItem function that takes a column argument doesn't fit with my vision of the ListView. I consider the "addItem(const sf::String& text)" to only exist for convenience in case someone just uses the widget for a single column or in case I add an addSubItem function in the future. The "addItem(const std::vector<sf::String>& item)" is what I consider the "real" addItem function.

2. I could rename it to addRow or addItemRow though, but the reason would be to have the naming more consistent with getItemRow (which only uses the term "row" because I couldn't find a better word and the "getItem" function already existed).

3. Similarly, removeItem removes an entire row because the row is the item.

4-5. I'm not sure how to best deal with selecting a single cell, the ListView doesn't need to support something like that imho. If you have some use case for it then maybe I can still add an option to select a cell instead of a row though, but it wouldn't be the default behavior.

A way to update an item is still lacking, so I can definately add something like "bool updateItem(std::size_t index, std::vector<sf::String>)". Maybe I'll add something like "updateSubItem(std::size_t rowIndex, std::size_t columnIndex, sf::String)" too, but the updateItem is the most important one as there is currently no way at all to change an item.

A "void addMultipleItems(const std::vector<std::vector<sf::String>> & item)" could be added, but only for performance reasons. In terms of usability, you can just call addItem in a loop, so there isn't much need for such a function. But if you are working with over 100 items then it could still be useful because it would insert the items slightly faster. Although I wonder if the difference would be big enough to notice, that is something that I should test first.


Ok Texus. I respect that you want to keep the different namethology than windows.

So, "updateItem" and "updateSubItem" are good with me. And there are plenty of use cases that need both of these functions.

Also i know many cases that you could add more than 100 items. Sometimes, there is a need to load more than 1000 items. Look at "Mp3tag", Virtual Dj" or similar programs. Recently i used "Mp3tag" where i had to load some music folders with more than 10000 mp3 files altogether. And that's the reason for a "addMultipleItems" function.

I didn't check if you have a right click on a item-row! Have you?


There isn't a right click event yet, but I could add a "ItemRightClicked" signal.

I did some performance tests and for 5000 items the addMultipleItems can reduce the time from 0.168 to 0.045 seconds.
I'll add it, but most people won't need it. Inserting 1000 items with addItem takes only 0.04 seconds, which will hardly be noticeable when doing it only once during the loading phase.
Edit: That was in debug mode, inserting 5000 items with addItem only takes 0.06 seconds in release mode.


That is quite fast. How many columns your test have? Have you made any test with 5 or more columns?


The test I did was just to get a quick idea of the performance, it wasn't very realistic: all items were identical and there was only one column.
With 10000 items and 10 columns (still identical items of only 6 characters) it takes about 0.32 seconds in release mode. Using addMultipleItems only decreases it to 0.2 - 0.26 seconds (depending on whether you include constructing the vector in the measurements). So the difference itsn't that spectacular (difference is smaller in release than it was in debug) as the biggest performance cost is actually creating the items. The performance scales pretty linearly, 10000 items is about 10 times slower than 1000 items. For columns the same goes: the performance of 10000 items in a single column should be comparable to 1000 items with 10 columns.

Edit: I might even be able to reduce the time by another 10-15% if I don't unnecessary create a sprite for the icon, but I think the current performance is already great.


I know that listview is a demanding widget. A lot can be done in future to improve speed and functionality. For example, it would be great to add a "search-text-box" in new widgets in trello. Even windows does not have this widget. Searching a "listview" for a specific text can be done by binding a "listview" widget with a "searchbar" widget and easily exclude rows to show results. Also this functionality can be added to current "textbox" and also expanded for labels or text boxes where people need to look for a specific string. (aka ctrl+f)


Could you give an example of how such a search box would look visually? Because I'm not sure what you mean and how you expect it to be used. And how do you expect searched text to show up in Label or TextBox?


QuoteAlso this functionality can be added to current "textbox"

So a search box is actually a text box. Just see on top right on visual studio or press "ctrl+F" in notepad. The text found (if found) is highlighted! Easy!

However in a listview the lines-rowes that does not contain the request searched string must be excluded.


Highlighting is something that won't happen soon, a RichTextBox class would first have to exist. The TextBox text is currently split in up to 5 parts to draw it, highlighting would require it to be split in an unlimited amount of parts (as it depends on how many matches there were).
An editor with highlighting and searching though it is not something that is likely to ever exist in TGUI. You would have to write it yourself with a couple of buttons (e.g. goto previous and next match), an edit box (to type the searched word) and a RichTextLabel or RichTextBox that allows setting colors of any part of the text. The RichText widget is still missing in TGUI and may be included in the future, although there are currently no plans for it as it would be a lot of work.

The ListView case should probably just be implemented in your own code, that gives a lot more flexibility (e.g. to search only at the beginning of the word or only search in first column, etc).


Maybe a highlighted text is identical with selected text. And there is selected text in text box.

Normally the search in a listview apply to all columns but also this is not a huge handicap .

And yes everything that i thought about can be implemented in our code. ;D


A "setSelectedText(size_t indexBegin, size_t sizeLetters)" function for textbox widget would be useful in many cases.

Also can not found setHorizontalAlignment and setVerticalAlignment for textbox. Have you miss them for any particular reason?


QuoteMaybe a highlighted text is identical with selected text. And there is selected text in text box.
Still easier said than done, the TextBox hardcodes that there is only one part selected. Implementing something like highlighting in TextBox might not be that hard but currently has very low priority.

QuoteA "setSelectedText(size_t indexBegin, size_t sizeLetters)" function for textbox widget would be useful in many cases.
I can't immediately think of a use case for it, the TextBox is meant to write text in, not for visualizing data.

QuoteAlso can not found setHorizontalAlignment and setVerticalAlignment for textbox. Have you miss them for any particular reason?
Alignment in a TextBox is an extremely niche use case imho, I don't think I've ever encountered it in any application. I can understand that it could be useful so I'll add it to the todo list, but it will be harder to add than in Label where there is no text interaction.


QuoteMaybe a highlighted text is identical with selected text. And there is selected text in text box.

My mistake. In this phrase i should added one more sentence. This phrase:

QuoteSo you can use selected text instead of highlight text.

So, forget everything about "hightlighted" text.

A function like setSelectedText(size_t indexBegin, size_t sizeLetters) can be used in any case of making (in our code) a search functionality. After all in textbox there is a "getSelectedText" function.

I realize that "setHorizontalAlignment and setVerticalAlignment for textbox" were missing when yesterday i tried to make an example while we were talking about searching functionality in textboxes. I overcome this lack by declearing bigger font size. But at the end i never finished this example due to lack of "setSelectedText" function.


The following things have been added:
- RightClicked signal in ListView
- addMultipleItems, changeItem and changeSubItem functions in ListView
- setSelectedText function in TextBox

Edit: The unit tests are hanging somewhere on the right click event on windows (while the tests don't hang there on linux), so right click might not work yet. I'll try to figure that out tomorrow.