Multiple selection items for ListView.

  • 7 Replies
  • 59 Views
Multiple selection items for ListView.
« on: 01 October 2019, 16:25:29 »
In some cases there is important to have opportunity select several items from the list (for example if its file picker dialog).

*

texus

  • *****
  • 1475
    • View Profile
    • Texus's Blog
Re: Multiple selection items for ListView.
« Reply #1 on: 01 October 2019, 22:29:33 »
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).
void setSelectedItems(const std::vector<std::size_t>& indices);
std::vector<std::size_t> getSelectedItemIndices() const;
void setMultiSelect(bool multiSelect);
bool getMultiSelect() const;
« Last Edit: 01 October 2019, 22:31:16 by texus »

Re: Multiple selection items for ListView.
« Reply #2 on: 01 October 2019, 22:57:10 »
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

  • *****
  • 1475
    • View Profile
    • Texus's Blog
Re: Multiple selection items for ListView.
« Reply #3 on: 02 October 2019, 07:55:22 »
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.

Re: Multiple selection items for ListView.
« Reply #4 on: 05 October 2019, 19:49:21 »
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

  • *****
  • 1475
    • View Profile
    • Texus's Blog
Re: Multiple selection items for ListView.
« Reply #5 on: 05 October 2019, 19:55:55 »
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.

Re: Multiple selection items for ListView.
« Reply #6 on: 05 October 2019, 20:10:16 »
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

  • *****
  • 1475
    • View Profile
    • Texus's Blog
Re: Multiple selection items for ListView.
« Reply #7 on: 05 October 2019, 22:17:33 »
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.