2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-09-07 17:14:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-07-11 12:47:26 +00:00
|
|
|
#include <AK/Badge.h>
|
2019-09-07 17:14:59 +00:00
|
|
|
#include <AK/HashTable.h>
|
2020-09-18 07:49:51 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2020-07-11 12:47:26 +00:00
|
|
|
#include <AK/TemporaryChange.h>
|
2020-02-16 08:17:49 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/ModelIndex.h>
|
2019-09-07 17:14:59 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
namespace GUI {
|
2019-09-07 17:14:59 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
class ModelSelection {
|
2020-08-14 10:04:03 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(ModelSelection);
|
|
|
|
AK_MAKE_NONMOVABLE(ModelSelection);
|
|
|
|
|
2019-09-07 17:14:59 +00:00
|
|
|
public:
|
2020-02-02 14:07:41 +00:00
|
|
|
ModelSelection(AbstractView& view)
|
2019-09-07 17:14:59 +00:00
|
|
|
: m_view(view)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-12 16:42:15 +00:00
|
|
|
int size() const { return m_indexes.size(); }
|
2019-09-07 17:14:59 +00:00
|
|
|
bool is_empty() const { return m_indexes.is_empty(); }
|
2020-02-02 14:07:41 +00:00
|
|
|
bool contains(const ModelIndex& index) const { return m_indexes.contains(index); }
|
2019-09-07 17:33:58 +00:00
|
|
|
bool contains_row(int row) const
|
|
|
|
{
|
|
|
|
for (auto& index : m_indexes) {
|
|
|
|
if (index.row() == row)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-07 17:14:59 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void set(const ModelIndex&);
|
|
|
|
void add(const ModelIndex&);
|
2020-11-30 06:29:14 +00:00
|
|
|
void add_all(const Vector<ModelIndex>&);
|
2020-02-02 14:07:41 +00:00
|
|
|
void toggle(const ModelIndex&);
|
|
|
|
bool remove(const ModelIndex&);
|
2019-09-07 17:14:59 +00:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_index(Callback callback)
|
|
|
|
{
|
2019-09-17 07:26:10 +00:00
|
|
|
for (auto& index : indexes())
|
2019-09-07 17:14:59 +00:00
|
|
|
callback(index);
|
|
|
|
}
|
|
|
|
|
2019-09-12 16:42:15 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_index(Callback callback) const
|
|
|
|
{
|
2019-09-17 07:26:10 +00:00
|
|
|
for (auto& index : indexes())
|
2019-09-12 16:42:15 +00:00
|
|
|
callback(index);
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
Vector<ModelIndex> indexes() const
|
2019-09-17 07:26:10 +00:00
|
|
|
{
|
2020-02-02 14:07:41 +00:00
|
|
|
Vector<ModelIndex> selected_indexes;
|
2019-09-17 07:26:10 +00:00
|
|
|
|
|
|
|
for (auto& index : m_indexes)
|
|
|
|
selected_indexes.append(index);
|
2020-01-10 16:06:00 +00:00
|
|
|
|
2019-09-17 07:26:10 +00:00
|
|
|
return selected_indexes;
|
|
|
|
}
|
|
|
|
|
2019-09-07 17:14:59 +00:00
|
|
|
// FIXME: This doesn't guarantee that what you get is the lowest or "first" index selected..
|
2020-02-02 14:07:41 +00:00
|
|
|
ModelIndex first() const
|
2019-09-07 17:14:59 +00:00
|
|
|
{
|
|
|
|
if (m_indexes.is_empty())
|
|
|
|
return {};
|
|
|
|
return *m_indexes.begin();
|
|
|
|
}
|
|
|
|
|
2020-04-09 07:51:44 +00:00
|
|
|
void remove_matching(Function<bool(const ModelIndex&)>);
|
|
|
|
|
2020-07-11 12:47:26 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-09-07 17:14:59 +00:00
|
|
|
private:
|
2020-07-11 12:47:26 +00:00
|
|
|
void notify_selection_changed();
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
AbstractView& m_view;
|
|
|
|
HashTable<ModelIndex> m_indexes;
|
2020-07-11 12:47:26 +00:00
|
|
|
bool m_disable_notify { false };
|
|
|
|
bool m_notify_pending { false };
|
2019-09-07 17:14:59 +00:00
|
|
|
};
|
2020-02-02 14:07:41 +00:00
|
|
|
|
|
|
|
}
|