2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/AbstractView.h>
|
2020-02-16 05:17:49 -03:00
|
|
|
#include <LibGUI/Model.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/ModelSelection.h>
|
2019-09-07 14:14:59 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2022-01-05 13:03:49 -03:00
|
|
|
void ModelSelection::remove_all_matching(Function<bool(ModelIndex const&)> filter)
|
2020-04-09 04:51:44 -03:00
|
|
|
{
|
2022-01-05 13:02:40 -03:00
|
|
|
if (m_indices.remove_all_matching([&](ModelIndex const& index) { return filter(index); }))
|
2020-07-11 09:47:26 -03:00
|
|
|
notify_selection_changed();
|
2020-04-09 04:51:44 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void ModelSelection::set(const ModelIndex& index)
|
2019-09-07 14:14:59 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(index.is_valid());
|
2021-04-29 17:23:52 -03:00
|
|
|
if (m_indices.size() == 1 && m_indices.contains(index))
|
2019-09-07 14:14:59 -03:00
|
|
|
return;
|
2021-04-29 17:23:52 -03:00
|
|
|
m_indices.clear();
|
|
|
|
|
m_indices.set(index);
|
2020-07-11 09:47:26 -03:00
|
|
|
notify_selection_changed();
|
2019-09-07 14:14:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void ModelSelection::add(const ModelIndex& index)
|
2019-09-07 14:14:59 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(index.is_valid());
|
2022-01-06 14:01:12 -03:00
|
|
|
if (m_indices.set(index) == AK::HashSetResult::InsertedNewEntry)
|
|
|
|
|
notify_selection_changed();
|
2019-09-07 14:14:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-11-30 03:29:14 -03:00
|
|
|
void ModelSelection::add_all(const Vector<ModelIndex>& indices)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
TemporaryChange notify_change { m_disable_notify, true };
|
|
|
|
|
for (auto& index : indices)
|
|
|
|
|
add(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_notify_pending)
|
|
|
|
|
notify_selection_changed();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void ModelSelection::toggle(const ModelIndex& index)
|
2019-09-07 14:33:58 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(index.is_valid());
|
2021-04-29 17:23:52 -03:00
|
|
|
if (m_indices.contains(index))
|
|
|
|
|
m_indices.remove(index);
|
2019-09-07 14:33:58 -03:00
|
|
|
else
|
2021-04-29 17:23:52 -03:00
|
|
|
m_indices.set(index);
|
2020-07-11 09:47:26 -03:00
|
|
|
notify_selection_changed();
|
2019-09-07 14:33:58 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
bool ModelSelection::remove(const ModelIndex& index)
|
2019-09-07 14:14:59 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(index.is_valid());
|
2021-04-29 17:23:52 -03:00
|
|
|
if (!m_indices.contains(index))
|
2019-09-07 14:14:59 -03:00
|
|
|
return false;
|
2021-04-29 17:23:52 -03:00
|
|
|
m_indices.remove(index);
|
2020-07-11 09:47:26 -03:00
|
|
|
notify_selection_changed();
|
2019-09-07 14:14:59 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void ModelSelection::clear()
|
2019-09-07 14:14:59 -03:00
|
|
|
{
|
2021-04-29 17:23:52 -03:00
|
|
|
if (m_indices.is_empty())
|
2019-09-07 14:14:59 -03:00
|
|
|
return;
|
2021-04-29 17:23:52 -03:00
|
|
|
m_indices.clear();
|
2020-07-11 09:47:26 -03:00
|
|
|
notify_selection_changed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelSelection::notify_selection_changed()
|
|
|
|
|
{
|
|
|
|
|
if (!m_disable_notify) {
|
|
|
|
|
m_view.notify_selection_changed({});
|
|
|
|
|
m_notify_pending = false;
|
|
|
|
|
} else {
|
|
|
|
|
m_notify_pending = true;
|
|
|
|
|
}
|
2019-09-07 14:14:59 -03:00
|
|
|
}
|
2020-02-02 11:07:41 -03:00
|
|
|
|
|
|
|
|
}
|