mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
LibGUI: Rename SortingProxyModel "target" to "source" instead
This commit is contained in:
parent
db6a4f2954
commit
82e949aa7c
4 changed files with 36 additions and 35 deletions
|
@ -306,7 +306,7 @@ void DirectoryView::open_next_directory()
|
||||||
GUI::ModelIndex DirectoryView::map_table_view_index(const GUI::ModelIndex& index) const
|
GUI::ModelIndex DirectoryView::map_table_view_index(const GUI::ModelIndex& index) const
|
||||||
{
|
{
|
||||||
auto& filter_model = (const GUI::SortingProxyModel&)*m_table_view->model();
|
auto& filter_model = (const GUI::SortingProxyModel&)*m_table_view->model();
|
||||||
return filter_model.map_to_target(index);
|
return filter_model.map_to_source(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DirectoryView::update_statusbar()
|
void DirectoryView::update_statusbar()
|
||||||
|
|
|
@ -208,7 +208,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const
|
||||||
m_view->on_selection_change = [this] {
|
m_view->on_selection_change = [this] {
|
||||||
auto index = m_view->selection().first();
|
auto index = m_view->selection().first();
|
||||||
auto& filter_model = (SortingProxyModel&)*m_view->model();
|
auto& filter_model = (SortingProxyModel&)*m_view->model();
|
||||||
auto local_index = filter_model.map_to_target(index);
|
auto local_index = filter_model.map_to_source(index);
|
||||||
const FileSystemModel::Node& node = m_model->node(local_index);
|
const FileSystemModel::Node& node = m_model->node(local_index);
|
||||||
LexicalPath path { node.full_path(m_model) };
|
LexicalPath path { node.full_path(m_model) };
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const
|
||||||
|
|
||||||
m_view->on_activation = [this](auto& index) {
|
m_view->on_activation = [this](auto& index) {
|
||||||
auto& filter_model = (SortingProxyModel&)*m_view->model();
|
auto& filter_model = (SortingProxyModel&)*m_view->model();
|
||||||
auto local_index = filter_model.map_to_target(index);
|
auto local_index = filter_model.map_to_source(index);
|
||||||
const FileSystemModel::Node& node = m_model->node(local_index);
|
const FileSystemModel::Node& node = m_model->node(local_index);
|
||||||
auto path = node.full_path(m_model);
|
auto path = node.full_path(m_model);
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,8 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/TemporaryChange.h>
|
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
|
#include <AK/TemporaryChange.h>
|
||||||
#include <LibGUI/AbstractView.h>
|
#include <LibGUI/AbstractView.h>
|
||||||
#include <LibGUI/SortingProxyModel.h>
|
#include <LibGUI/SortingProxyModel.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -33,21 +33,21 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model>&& target)
|
SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model> source)
|
||||||
: m_target(move(target))
|
: m_source(move(source))
|
||||||
, m_key_column(-1)
|
, m_key_column(-1)
|
||||||
{
|
{
|
||||||
// Since the target model already called Model::did_update we can't
|
// Since the source model already called Model::did_update we can't
|
||||||
// assume we will get another call. So, we need to register for further
|
// assume we will get another call. So, we need to register for further
|
||||||
// updates and just call resort() right away, otherwise requests
|
// updates and just call resort() right away, otherwise requests
|
||||||
// to this model won't work because there are no indices to map
|
// to this model won't work because there are no indices to map
|
||||||
m_target->register_client(*this);
|
m_source->register_client(*this);
|
||||||
resort();
|
resort();
|
||||||
}
|
}
|
||||||
|
|
||||||
SortingProxyModel::~SortingProxyModel()
|
SortingProxyModel::~SortingProxyModel()
|
||||||
{
|
{
|
||||||
m_target->unregister_client(*this);
|
m_source->unregister_client(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SortingProxyModel::on_model_update(unsigned flags)
|
void SortingProxyModel::on_model_update(unsigned flags)
|
||||||
|
@ -57,45 +57,45 @@ void SortingProxyModel::on_model_update(unsigned flags)
|
||||||
|
|
||||||
int SortingProxyModel::row_count(const ModelIndex& index) const
|
int SortingProxyModel::row_count(const ModelIndex& index) const
|
||||||
{
|
{
|
||||||
auto target_index = map_to_target(index);
|
auto source_index = map_to_source(index);
|
||||||
return target().row_count(target_index);
|
return source().row_count(source_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SortingProxyModel::column_count(const ModelIndex& index) const
|
int SortingProxyModel::column_count(const ModelIndex& index) const
|
||||||
{
|
{
|
||||||
auto target_index = map_to_target(index);
|
auto source_index = map_to_source(index);
|
||||||
return target().column_count(target_index);
|
return source().column_count(source_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const
|
ModelIndex SortingProxyModel::map_to_source(const ModelIndex& index) const
|
||||||
{
|
{
|
||||||
if (!index.is_valid())
|
if (!index.is_valid())
|
||||||
return {};
|
return {};
|
||||||
if (static_cast<size_t>(index.row()) >= m_row_mappings.size() || index.column() >= column_count())
|
if (static_cast<size_t>(index.row()) >= m_row_mappings.size() || index.column() >= column_count())
|
||||||
return {};
|
return {};
|
||||||
return target().index(m_row_mappings[index.row()], index.column());
|
return source().index(m_row_mappings[index.row()], index.column());
|
||||||
}
|
}
|
||||||
|
|
||||||
String SortingProxyModel::column_name(int column) const
|
String SortingProxyModel::column_name(int column) const
|
||||||
{
|
{
|
||||||
return target().column_name(column);
|
return source().column_name(column);
|
||||||
}
|
}
|
||||||
|
|
||||||
Variant SortingProxyModel::data(const ModelIndex& index, Role role) const
|
Variant SortingProxyModel::data(const ModelIndex& index, Role role) const
|
||||||
{
|
{
|
||||||
auto target_index = map_to_target(index);
|
auto source_index = map_to_source(index);
|
||||||
ASSERT(target_index.is_valid());
|
ASSERT(source_index.is_valid());
|
||||||
return target().data(target_index, role);
|
return source().data(source_index, role);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SortingProxyModel::update()
|
void SortingProxyModel::update()
|
||||||
{
|
{
|
||||||
target().update();
|
source().update();
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView SortingProxyModel::drag_data_type() const
|
StringView SortingProxyModel::drag_data_type() const
|
||||||
{
|
{
|
||||||
return target().drag_data_type();
|
return source().drag_data_type();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SortingProxyModel::set_key_column_and_sort_order(int column, SortOrder sort_order)
|
void SortingProxyModel::set_key_column_and_sort_order(int column, SortOrder sort_order)
|
||||||
|
@ -113,7 +113,7 @@ void SortingProxyModel::resort(unsigned flags)
|
||||||
{
|
{
|
||||||
TemporaryChange change(m_sorting, true);
|
TemporaryChange change(m_sorting, true);
|
||||||
auto old_row_mappings = m_row_mappings;
|
auto old_row_mappings = m_row_mappings;
|
||||||
int row_count = target().row_count();
|
int row_count = source().row_count();
|
||||||
m_row_mappings.resize(row_count);
|
m_row_mappings.resize(row_count);
|
||||||
for (int i = 0; i < row_count; ++i)
|
for (int i = 0; i < row_count; ++i)
|
||||||
m_row_mappings[i] = i;
|
m_row_mappings[i] = i;
|
||||||
|
@ -122,8 +122,8 @@ void SortingProxyModel::resort(unsigned flags)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
|
quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
|
||||||
auto data1 = target().data(target().index(row1, m_key_column), m_sort_role);
|
auto data1 = source().data(source().index(row1, m_key_column), m_sort_role);
|
||||||
auto data2 = target().data(target().index(row2, m_key_column), m_sort_role);
|
auto data2 = source().data(source().index(row2, m_key_column), m_sort_role);
|
||||||
if (data1 == data2)
|
if (data1 == data2)
|
||||||
return 0;
|
return 0;
|
||||||
bool is_less_than;
|
bool is_less_than;
|
||||||
|
@ -135,13 +135,13 @@ void SortingProxyModel::resort(unsigned flags)
|
||||||
});
|
});
|
||||||
for_each_view([&](AbstractView& view) {
|
for_each_view([&](AbstractView& view) {
|
||||||
view.selection().change_from_model({}, [&](ModelSelection& selection) {
|
view.selection().change_from_model({}, [&](ModelSelection& selection) {
|
||||||
Vector<ModelIndex> selected_indexes_in_target;
|
Vector<ModelIndex> selected_indexes_in_source;
|
||||||
selection.for_each_index([&](const ModelIndex& index) {
|
selection.for_each_index([&](const ModelIndex& index) {
|
||||||
selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
|
selected_indexes_in_source.append(source().index(old_row_mappings[index.row()], index.column()));
|
||||||
});
|
});
|
||||||
|
|
||||||
selection.clear();
|
selection.clear();
|
||||||
for (auto& index : selected_indexes_in_target) {
|
for (auto& index : selected_indexes_in_source) {
|
||||||
for (size_t i = 0; i < m_row_mappings.size(); ++i) {
|
for (size_t i = 0; i < m_row_mappings.size(); ++i) {
|
||||||
if (m_row_mappings[i] == index.row()) {
|
if (m_row_mappings[i] == index.row()) {
|
||||||
selection.add(this->index(i, index.column()));
|
selection.add(this->index(i, index.column()));
|
||||||
|
@ -156,7 +156,7 @@ void SortingProxyModel::resort(unsigned flags)
|
||||||
|
|
||||||
bool SortingProxyModel::is_column_sortable(int column_index) const
|
bool SortingProxyModel::is_column_sortable(int column_index) const
|
||||||
{
|
{
|
||||||
return target().is_column_sortable(column_index);
|
return source().is_column_sortable(column_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,10 +30,11 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
class SortingProxyModel final : public Model
|
class SortingProxyModel final
|
||||||
|
: public Model
|
||||||
, private ModelClient {
|
, private ModelClient {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<SortingProxyModel> create(NonnullRefPtr<Model>&& model) { return adopt(*new SortingProxyModel(move(model))); }
|
static NonnullRefPtr<SortingProxyModel> create(NonnullRefPtr<Model> source) { return adopt(*new SortingProxyModel(move(source))); }
|
||||||
virtual ~SortingProxyModel() override;
|
virtual ~SortingProxyModel() override;
|
||||||
|
|
||||||
virtual int row_count(const ModelIndex& = ModelIndex()) const override;
|
virtual int row_count(const ModelIndex& = ModelIndex()) const override;
|
||||||
|
@ -48,25 +49,25 @@ public:
|
||||||
virtual void set_key_column_and_sort_order(int, SortOrder) override;
|
virtual void set_key_column_and_sort_order(int, SortOrder) override;
|
||||||
virtual bool is_column_sortable(int column_index) const override;
|
virtual bool is_column_sortable(int column_index) const override;
|
||||||
|
|
||||||
ModelIndex map_to_target(const ModelIndex&) const;
|
ModelIndex map_to_source(const ModelIndex&) const;
|
||||||
|
|
||||||
Role sort_role() const { return m_sort_role; }
|
Role sort_role() const { return m_sort_role; }
|
||||||
void set_sort_role(Role role) { m_sort_role = role; }
|
void set_sort_role(Role role) { m_sort_role = role; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit SortingProxyModel(NonnullRefPtr<Model>&&);
|
explicit SortingProxyModel(NonnullRefPtr<Model> source);
|
||||||
|
|
||||||
virtual void on_model_update(unsigned) override;
|
virtual void on_model_update(unsigned) override;
|
||||||
|
|
||||||
Model& target() { return *m_target; }
|
Model& source() { return *m_source; }
|
||||||
const Model& target() const { return *m_target; }
|
const Model& source() const { return *m_source; }
|
||||||
|
|
||||||
void resort(unsigned flags = Model::UpdateFlag::DontInvalidateIndexes);
|
void resort(unsigned flags = Model::UpdateFlag::DontInvalidateIndexes);
|
||||||
|
|
||||||
void set_sorting_case_sensitive(bool b) { m_sorting_case_sensitive = b; }
|
void set_sorting_case_sensitive(bool b) { m_sorting_case_sensitive = b; }
|
||||||
bool is_sorting_case_sensitive() { return m_sorting_case_sensitive; }
|
bool is_sorting_case_sensitive() { return m_sorting_case_sensitive; }
|
||||||
|
|
||||||
NonnullRefPtr<Model> m_target;
|
NonnullRefPtr<Model> m_source;
|
||||||
Vector<int> m_row_mappings;
|
Vector<int> m_row_mappings;
|
||||||
int m_key_column { -1 };
|
int m_key_column { -1 };
|
||||||
SortOrder m_sort_order { SortOrder::Ascending };
|
SortOrder m_sort_order { SortOrder::Ascending };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue