Multiple selection items for ListView.

Started by Kvaz1r, 01 October 2019, 16:25:29

Kvaz1r

In some cases there is important to have opportunity select several items from the list (for example if its file picker dialog).

texus

#1
It would be a nice addition, but you will have to implement this yourself too if you want it, I'm too busy playing video games right now :)

After briefly thinking about it, I guess for the API it should be enough to add the following 4 functions (with the MultiSelect boolean propery being false by default).
Code (cpp) Select
void setSelectedItems(const std::vector<std::size_t>& indices);
std::vector<std::size_t> getSelectedItemIndices() const;
void setMultiSelect(bool multiSelect);
bool getMultiSelect() const;

Kvaz1r

Yes, it doesn't seems hard, I will try to do it on the week.
As internal collection should I use std::set or std::vector and sort it in updateSelectedItem? I am not sure which one would fits here better.

texus

std::set seems like a better option, for the setSelectedItems/getSelectedItemIndices functions as well. Then you don't have to worry about sorting or having the same index twice.

Kvaz1r

Ok, I've started working on it, there is several issue:
1. What to do if deselectItem/0 called with multiselect mode? Should I also to add deselectItem/1 or even deselectItems/1? 
2. What to do if user try to select several items when multiselect is true? Change mode or select only first?

texus

1. For now we can just keep the deselectItem function. I'd have to review that in the future where I would probably rename it to "deselectItems" or just "deselect", but for now we can just keep the existing "deselectItem" function which will deselect all items.

2. I'm not sure what you mean. If multiselect is true, the user should be able to select multiple items (by having ctrl and/or shift pressed when clicking). If multiselect is false then the user can only select one item at a time and the ctrl and shift keys are ignored. The mode should never be changed automatically.

Kvaz1r

1. +
2. It was a typo, of course I mean multiselect is false. But I was thought about explicit incorrect calls(wrong indexes, wrong mode,...). Is there need special strategy for handling such situation? 

texus

So basically the question is what to do in setSelectedItems when setMultiSelect wasn't called yet (or when it was set to false again).

I wanted to look up how other libraries did it and I noticed that in .NET the selected items is a read-only property. Maybe we don't need to have a setSelectedItems at all?
If you do add the function then you can choose what it does, as long as the behavior is documented. When I said that the mode should not be changed automatically I wasn't thinking about a scenario like this, enabling multiselect when calling setSelectedItems with more than one item would be a valid solution. But unless someone has a need for the function maybe it would be better to just not have a setSelectedItems function.