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