Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibGUI/AbstractView.h>
8#include <LibGUI/Model.h>
9#include <LibGUI/ModelSelection.h>
10
11namespace GUI {
12
13void ModelSelection::remove_all_matching(Function<bool(ModelIndex const&)> const& filter)
14{
15 if (m_indices.remove_all_matching(filter))
16 notify_selection_changed();
17}
18
19void ModelSelection::set(ModelIndex const& index)
20{
21 VERIFY(index.is_valid());
22 if (m_indices.size() == 1 && m_indices.contains(index))
23 return;
24 m_indices.clear();
25 m_indices.set(index);
26 notify_selection_changed();
27}
28
29void ModelSelection::add(ModelIndex const& index)
30{
31 VERIFY(index.is_valid());
32 if (m_indices.set(index) == AK::HashSetResult::InsertedNewEntry)
33 notify_selection_changed();
34}
35
36void ModelSelection::add_all(Vector<ModelIndex> const& indices)
37{
38 {
39 TemporaryChange notify_change { m_disable_notify, true };
40 for (auto& index : indices)
41 add(index);
42 }
43
44 if (m_notify_pending)
45 notify_selection_changed();
46}
47
48void ModelSelection::toggle(ModelIndex const& index)
49{
50 VERIFY(index.is_valid());
51 if (m_indices.contains(index))
52 m_indices.remove(index);
53 else
54 m_indices.set(index);
55 notify_selection_changed();
56}
57
58bool ModelSelection::remove(ModelIndex const& index)
59{
60 VERIFY(index.is_valid());
61 if (!m_indices.contains(index))
62 return false;
63 m_indices.remove(index);
64 notify_selection_changed();
65 return true;
66}
67
68void ModelSelection::clear()
69{
70 if (m_indices.is_empty())
71 return;
72 m_indices.clear();
73 notify_selection_changed();
74}
75
76void ModelSelection::notify_selection_changed()
77{
78 if (!m_disable_notify) {
79 m_view.notify_selection_changed({});
80 m_notify_pending = false;
81 } else {
82 m_notify_pending = true;
83 }
84}
85
86}