1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 21:25:07 +00:00
serenity/Libraries/LibGUI/GModelSelection.h
Andreas Kling 82559e211d LibGUI: Add GModelSelection to help implementing multiple-select views
Each GAbstractView now has a GModelSelection backed by a simple
HashTable<GModelIndex>. When the selection changes somehow, the view
gets notified via the notify_selection_changed() callback.

In the future it will probably make sense to move to using some kind of
ranges as the internal representation instead.
2019-09-07 19:21:07 +02:00

41 lines
940 B
C++

#pragma once
#include <AK/HashTable.h>
#include <LibGUI/GModelIndex.h>
class GAbstractView;
class GModelSelection {
public:
GModelSelection(GAbstractView& view)
: m_view(view)
{
}
bool is_empty() const { return m_indexes.is_empty(); }
bool contains(const GModelIndex& index) const { return m_indexes.contains(index); }
void set(const GModelIndex&);
void add(const GModelIndex&);
bool remove(const GModelIndex&);
void clear();
template<typename Callback>
void for_each_index(Callback callback)
{
for (auto& index : m_indexes)
callback(index);
}
// FIXME: This doesn't guarantee that what you get is the lowest or "first" index selected..
GModelIndex first() const
{
if (m_indexes.is_empty())
return {};
return *m_indexes.begin();
}
private:
GAbstractView& m_view;
HashTable<GModelIndex> m_indexes;
};