2020-01-18 11:38:21 +03: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-03-09 18:13:08 +03:00
|
|
|
#include <AK/QuickSort.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/AbstractView.h>
|
|
|
|
#include <LibGUI/SortingProxyModel.h>
|
2019-03-09 15:33:52 +03:00
|
|
|
#include <stdio.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <stdlib.h>
|
2019-03-09 15:33:52 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model>&& target)
|
2019-03-09 15:33:52 +03:00
|
|
|
: m_target(move(target))
|
|
|
|
, m_key_column(-1)
|
|
|
|
{
|
2019-08-20 20:45:08 +03:00
|
|
|
m_target->on_update = [this] {
|
2019-03-09 15:33:52 +03:00
|
|
|
resort();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
SortingProxyModel::~SortingProxyModel()
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
int SortingProxyModel::row_count(const ModelIndex& index) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
2019-03-29 05:27:03 +03:00
|
|
|
return target().row_count(index);
|
2019-03-09 15:33:52 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
int SortingProxyModel::column_count(const ModelIndex& index) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
2019-03-29 05:27:03 +03:00
|
|
|
return target().column_count(index);
|
2019-03-09 15:33:52 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
2019-03-09 16:52:25 +03:00
|
|
|
if (!index.is_valid())
|
2019-06-07 12:46:02 +03:00
|
|
|
return {};
|
2020-02-25 16:49:47 +03:00
|
|
|
if (static_cast<size_t>(index.row()) >= m_row_mappings.size() || index.column() >= column_count())
|
2019-06-07 12:46:02 +03:00
|
|
|
return {};
|
2019-03-29 06:58:15 +03:00
|
|
|
return target().index(m_row_mappings[index.row()], index.column());
|
2019-03-09 15:33:52 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
String SortingProxyModel::row_name(int index) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
|
|
|
return target().row_name(index);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
String SortingProxyModel::column_name(int index) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
|
|
|
return target().column_name(index);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Model::ColumnMetadata SortingProxyModel::column_metadata(int index) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
|
|
|
return target().column_metadata(index);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Variant SortingProxyModel::data(const ModelIndex& index, Role role) const
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
2020-02-24 22:54:27 +03:00
|
|
|
auto target_index = map_to_target(index);
|
|
|
|
if (!target_index.is_valid()) {
|
|
|
|
dbg() << "BUG! SortingProxyModel: Unable to convert " << index << " to target";
|
|
|
|
return {};
|
|
|
|
}
|
2019-03-09 16:24:34 +03:00
|
|
|
return target().data(map_to_target(index), role);
|
2019-03-09 15:33:52 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void SortingProxyModel::update()
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
|
|
|
target().update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
StringView SortingProxyModel::drag_data_type() const
|
2020-01-22 21:15:46 +03:00
|
|
|
{
|
|
|
|
return target().drag_data_type();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void SortingProxyModel::set_key_column_and_sort_order(int column, SortOrder sort_order)
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
|
|
|
if (column == m_key_column && sort_order == m_sort_order)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASSERT(column >= 0 && column < column_count());
|
|
|
|
m_key_column = column;
|
|
|
|
m_sort_order = sort_order;
|
|
|
|
resort();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void SortingProxyModel::resort()
|
2019-03-09 15:33:52 +03:00
|
|
|
{
|
2019-11-27 21:04:35 +03:00
|
|
|
auto old_row_mappings = m_row_mappings;
|
2019-03-09 15:33:52 +03:00
|
|
|
int row_count = target().row_count();
|
|
|
|
m_row_mappings.resize(row_count);
|
|
|
|
for (int i = 0; i < row_count; ++i)
|
|
|
|
m_row_mappings[i] = i;
|
2019-05-09 16:51:57 +03:00
|
|
|
if (m_key_column == -1) {
|
2020-04-12 13:03:33 +03:00
|
|
|
did_update(Model::UpdateFlag::DontInvalidateIndexes);
|
2019-03-09 15:33:52 +03:00
|
|
|
return;
|
2019-05-09 16:51:57 +03:00
|
|
|
}
|
2020-03-03 18:01:37 +03:00
|
|
|
quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
|
2020-02-02 17:07:41 +03:00
|
|
|
auto data1 = target().data(target().index(row1, m_key_column), Model::Role::Sort);
|
|
|
|
auto data2 = target().data(target().index(row2, m_key_column), Model::Role::Sort);
|
2019-03-09 15:33:52 +03:00
|
|
|
if (data1 == data2)
|
|
|
|
return 0;
|
2019-08-11 19:11:25 +03:00
|
|
|
bool is_less_than;
|
2019-11-27 21:04:35 +03:00
|
|
|
if (data1.is_string() && data2.is_string() && !m_sorting_case_sensitive)
|
|
|
|
is_less_than = data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
|
|
|
|
else
|
|
|
|
is_less_than = data1 < data2;
|
2020-02-02 17:07:41 +03:00
|
|
|
return m_sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
|
2019-03-09 18:13:08 +03:00
|
|
|
});
|
2020-04-12 13:03:33 +03:00
|
|
|
did_update(Model::UpdateFlag::DontInvalidateIndexes);
|
2020-02-02 17:07:41 +03:00
|
|
|
for_each_view([&](AbstractView& view) {
|
2019-11-27 21:04:35 +03:00
|
|
|
auto& selection = view.selection();
|
2020-02-02 17:07:41 +03:00
|
|
|
Vector<ModelIndex> selected_indexes_in_target;
|
|
|
|
selection.for_each_index([&](const ModelIndex& index) {
|
2019-11-27 21:04:35 +03:00
|
|
|
selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
|
|
|
|
});
|
|
|
|
|
|
|
|
selection.clear();
|
|
|
|
for (auto& index : selected_indexes_in_target) {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_row_mappings.size(); ++i) {
|
2019-11-27 21:04:35 +03:00
|
|
|
if (m_row_mappings[i] == index.row()) {
|
|
|
|
selection.add(this->index(i, index.column()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-03-09 15:33:52 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|