ladybird/Userland/Libraries/LibGUI/ModelSelection.h
Andreas Kling 3d4afe7614 Everywhere: "indexes" => "indices"
I've wasted a silly amount of time in the past fretting over which
of these words to use. Let's just choose one and use it everywhere. :^)
2021-04-29 22:23:52 +02:00

103 lines
2.4 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/HashTable.h>
#include <AK/Noncopyable.h>
#include <AK/TemporaryChange.h>
#include <AK/Vector.h>
#include <LibGUI/ModelIndex.h>
namespace GUI {
class ModelSelection {
AK_MAKE_NONCOPYABLE(ModelSelection);
AK_MAKE_NONMOVABLE(ModelSelection);
public:
ModelSelection(AbstractView& view)
: m_view(view)
{
}
int size() const { return m_indices.size(); }
bool is_empty() const { return m_indices.is_empty(); }
bool contains(const ModelIndex& index) const { return m_indices.contains(index); }
bool contains_row(int row) const
{
for (auto& index : m_indices) {
if (index.row() == row)
return true;
}
return false;
}
void set(const ModelIndex&);
void add(const ModelIndex&);
void add_all(const Vector<ModelIndex>&);
void toggle(const ModelIndex&);
bool remove(const ModelIndex&);
void clear();
template<typename Callback>
void for_each_index(Callback callback)
{
for (auto& index : indices())
callback(index);
}
template<typename Callback>
void for_each_index(Callback callback) const
{
for (auto& index : indices())
callback(index);
}
Vector<ModelIndex> indices() const
{
Vector<ModelIndex> selected_indices;
for (auto& index : m_indices)
selected_indices.append(index);
return selected_indices;
}
// FIXME: This doesn't guarantee that what you get is the lowest or "first" index selected..
ModelIndex first() const
{
if (m_indices.is_empty())
return {};
return *m_indices.begin();
}
void remove_matching(Function<bool(const ModelIndex&)>);
template<typename Function>
void change_from_model(Badge<SortingProxyModel>, Function f)
{
{
TemporaryChange change(m_disable_notify, true);
m_notify_pending = false;
f(*this);
}
if (m_notify_pending)
notify_selection_changed();
}
private:
void notify_selection_changed();
AbstractView& m_view;
HashTable<ModelIndex> m_indices;
bool m_disable_notify { false };
bool m_notify_pending { false };
};
}