forked from NaiJi/project-kyoku
Turn SelectionManager into template
parent
b92bffb43e
commit
70d3284eeb
@ -1,63 +0,0 @@
|
||||
#include "selectionmanager.h"
|
||||
#include "classicmocknote.h"
|
||||
#include "mockelement.h"
|
||||
#include "graphics/classicselectiongraphics.h"
|
||||
#include <algorithm>
|
||||
|
||||
SelectionManager::SelectionManager() :
|
||||
_multiselection_enabled(false)
|
||||
{}
|
||||
|
||||
void SelectionManager::discard()
|
||||
{
|
||||
for (const auto& note : _selected_notes)
|
||||
for (auto& element : note->getElements())
|
||||
element.selected = false;
|
||||
|
||||
_selected_notes.clear();
|
||||
}
|
||||
|
||||
void SelectionManager::fetch(ClassicMockNote * const note)
|
||||
{
|
||||
bool already_there = std::any_of(_selected_notes.begin(), _selected_notes.end(),
|
||||
[¬e](const auto& selected_note)
|
||||
{
|
||||
return note == selected_note;
|
||||
});
|
||||
|
||||
if (!already_there)
|
||||
_selected_notes.emplace_back(note);
|
||||
}
|
||||
|
||||
void SelectionManager::remove(ClassicMockNote * const note)
|
||||
{
|
||||
for (std::size_t i = 0; i < _selected_notes.size(); ++i)
|
||||
{
|
||||
if (note == _selected_notes.at(i))
|
||||
{
|
||||
_selected_notes[i] = std::move(_selected_notes.back());
|
||||
_selected_notes.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SelectionManager::enableMultiselection(bool enable)
|
||||
{
|
||||
_multiselection_enabled = enable;
|
||||
}
|
||||
|
||||
bool SelectionManager::isMultiselectionEnabled() const
|
||||
{
|
||||
return _multiselection_enabled;
|
||||
}
|
||||
|
||||
void SelectionManager::display() const
|
||||
{
|
||||
for (const auto& note : _selected_notes)
|
||||
for (const auto& element : note->getElements())
|
||||
if (element.selected)
|
||||
{
|
||||
element.selection->display();
|
||||
}
|
||||
}
|
@ -1,27 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include "classicmocknote.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
template <class T>
|
||||
class SelectionManager
|
||||
{
|
||||
public:
|
||||
explicit SelectionManager();
|
||||
SelectionManager() :
|
||||
_multiselection_enabled(false),
|
||||
deselection([](T* thing) { thing->deselect(); })
|
||||
{}
|
||||
|
||||
// Remove whole selection completely
|
||||
void discard();
|
||||
void discard()
|
||||
{
|
||||
apply(deselection);
|
||||
_selected_things.clear();
|
||||
}
|
||||
|
||||
void fetch(T * const thing)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
void fetch(ClassicMockNote * const note);
|
||||
void remove(ClassicMockNote * const note);
|
||||
void remove(T * const thing)
|
||||
{
|
||||
for (std::size_t i = 0; i < _selected_things.size(); ++i)
|
||||
{
|
||||
if (thing == _selected_things.at(i))
|
||||
{
|
||||
_selected_things[i] = std::move(_selected_things.back());
|
||||
_selected_things.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void enableMultiselection(bool enable = true);
|
||||
bool isMultiselectionEnabled() const;
|
||||
void enableMultiselection(bool enable = true)
|
||||
{
|
||||
_multiselection_enabled = enable;
|
||||
}
|
||||
|
||||
void display() const;
|
||||
bool isMultiselectionEnabled() const
|
||||
{
|
||||
return _multiselection_enabled;
|
||||
}
|
||||
|
||||
void apply(const std::function<void(T*)>& function)
|
||||
{
|
||||
for (auto& thing : _selected_things)
|
||||
function(thing);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<ClassicMockNote*> _selected_notes;
|
||||
std::vector<T*> _selected_things;
|
||||
bool _multiselection_enabled;
|
||||
|
||||
const std::function<void(T*)> deselection;
|
||||
};
|
||||
|
Loading…
Reference in New Issue