|
|
|
@ -1,51 +1,54 @@
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
class SelectionManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
SelectionManager() :
|
|
|
|
|
explicit SelectionManager() :
|
|
|
|
|
_multiselection_enabled(false)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void discard()
|
|
|
|
|
{
|
|
|
|
|
_selected_things.clear();
|
|
|
|
|
_selected_ids.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fetch(T * const thing)
|
|
|
|
|
void fetch(int note_id, int element_id)
|
|
|
|
|
{
|
|
|
|
|
bool already_there = std::any_of(_selected_things.begin(), _selected_things.end(),
|
|
|
|
|
[&thing](const auto& selected_thing)
|
|
|
|
|
{
|
|
|
|
|
return thing == selected_thing;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!already_there)
|
|
|
|
|
_selected_things.emplace_back(thing);
|
|
|
|
|
if (!isSelected(note_id, element_id))
|
|
|
|
|
_selected_ids[note_id].emplace_back(element_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void remove(T * const thing)
|
|
|
|
|
void remove(int note_id, int element_id)
|
|
|
|
|
{
|
|
|
|
|
for (std::size_t i = 0; i < _selected_things.size(); ++i)
|
|
|
|
|
if (_selected_ids.count(note_id) == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto& elements_ids = _selected_ids[note_id];
|
|
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < elements_ids.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if (thing == _selected_things.at(i))
|
|
|
|
|
if (element_id == elements_ids.at(i))
|
|
|
|
|
{
|
|
|
|
|
_selected_things[i] = std::move(_selected_things.back());
|
|
|
|
|
_selected_things.pop_back();
|
|
|
|
|
elements_ids[i] = std::move(elements_ids.back());
|
|
|
|
|
elements_ids.pop_back();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isSelected(const T * const thing) const
|
|
|
|
|
bool isSelected(int note_id, int element_id) const
|
|
|
|
|
{
|
|
|
|
|
for (const auto& selected_thing : thing)
|
|
|
|
|
if (thing == selected_thing)
|
|
|
|
|
if (_selected_ids.count(note_id) == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const std::vector<int>& selected_elements = _selected_ids.at(note_id);
|
|
|
|
|
for (const auto& selected_element : selected_elements)
|
|
|
|
|
if (selected_element == element_id)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
@ -61,13 +64,7 @@ public:
|
|
|
|
|
return _multiselection_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void apply(const std::function<void(T*)>& function)
|
|
|
|
|
{
|
|
|
|
|
for (auto& thing : _selected_things)
|
|
|
|
|
function(thing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<T*> _selected_things;
|
|
|
|
|
std::map<int, std::vector<int>> _selected_ids;
|
|
|
|
|
bool _multiselection_enabled;
|
|
|
|
|
};
|
|
|
|
|