1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

LibGUI+FileManager: Fix forgetting to map sorting proxy model indexes

Also assert indexes are valid in a few more places.

Finally fixes https://github.com/SerenityOS/serenity/issues/1440 and
https://github.com/SerenityOS/serenity/issues/2787 :^)
This commit is contained in:
Sergey Bugaev 2020-07-15 14:33:12 +03:00 committed by Andreas Kling
parent e12b591509
commit 5fd8dbacb1
4 changed files with 21 additions and 17 deletions

View file

@ -166,8 +166,7 @@ DirectoryView::DirectoryView()
handle_activation(index); handle_activation(index);
}; };
m_table_view->on_activation = [&](auto& index) { m_table_view->on_activation = [&](auto& index) {
auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model(); handle_activation(map_table_view_index(index));
handle_activation(filter_model.map_to_target(index));
}; };
m_table_view->on_selection_change = [this] { m_table_view->on_selection_change = [this] {
@ -188,7 +187,7 @@ DirectoryView::DirectoryView()
m_table_view->on_context_menu_request = [this](auto& index, auto& event) { m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
if (on_context_menu_request) if (on_context_menu_request)
on_context_menu_request(*m_table_view, index, event); on_context_menu_request(*m_table_view, map_table_view_index(index), event);
}; };
m_icon_view->on_context_menu_request = [this](auto& index, auto& event) { m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
if (on_context_menu_request) if (on_context_menu_request)
@ -201,7 +200,7 @@ DirectoryView::DirectoryView()
m_table_view->on_drop = [this](auto& index, auto& event) { m_table_view->on_drop = [this](auto& index, auto& event) {
if (on_drop) if (on_drop)
on_drop(*m_table_view, index, event); on_drop(*m_table_view, map_table_view_index(index), event);
}; };
m_icon_view->on_drop = [this](auto& index, auto& event) { m_icon_view->on_drop = [this](auto& index, auto& event) {
if (on_drop) if (on_drop)
@ -304,6 +303,12 @@ void DirectoryView::open_next_directory()
} }
} }
GUI::ModelIndex DirectoryView::map_table_view_index(const GUI::ModelIndex& index) const
{
auto& filter_model = (const GUI::SortingProxyModel&)*m_table_view->model();
return filter_model.map_to_target(index);
}
void DirectoryView::update_statusbar() void DirectoryView::update_statusbar()
{ {
size_t total_size = model().node({}).total_size; size_t total_size = model().node({}).total_size;
@ -338,10 +343,8 @@ void DirectoryView::update_statusbar()
auto index = current_view().selection().first(); auto index = current_view().selection().first();
// FIXME: This is disgusting. This code should not even be aware that there is a GUI::SortingProxyModel in the table view. // FIXME: This is disgusting. This code should not even be aware that there is a GUI::SortingProxyModel in the table view.
if (m_view_mode == ViewMode::Table) { if (m_view_mode == ViewMode::Table)
auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model(); index = map_table_view_index(index);
index = filter_model.map_to_target(index);
}
auto& node = model().node(index); auto& node = model().node(index);
if (!node.symlink_target.is_empty()) { if (!node.symlink_target.is_empty()) {

View file

@ -119,6 +119,7 @@ private:
virtual void on_model_update(unsigned) override; virtual void on_model_update(unsigned) override;
void handle_activation(const GUI::ModelIndex&); void handle_activation(const GUI::ModelIndex&);
GUI::ModelIndex map_table_view_index(const GUI::ModelIndex&) const;
void set_status_message(const StringView&); void set_status_message(const StringView&);
void update_statusbar(); void update_statusbar();

View file

@ -332,6 +332,7 @@ const FileSystemModel::Node& FileSystemModel::node(const ModelIndex& index) cons
{ {
if (!index.is_valid()) if (!index.is_valid())
return *m_root; return *m_root;
ASSERT(index.internal_data());
return *(Node*)index.internal_data(); return *(Node*)index.internal_data();
} }

View file

@ -57,12 +57,14 @@ void SortingProxyModel::on_model_update(unsigned flags)
int SortingProxyModel::row_count(const ModelIndex& index) const int SortingProxyModel::row_count(const ModelIndex& index) const
{ {
return target().row_count(index); auto target_index = map_to_target(index);
return target().row_count(target_index);
} }
int SortingProxyModel::column_count(const ModelIndex& index) const int SortingProxyModel::column_count(const ModelIndex& index) const
{ {
return target().column_count(index); auto target_index = map_to_target(index);
return target().column_count(target_index);
} }
ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const
@ -74,19 +76,16 @@ ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const
return target().index(m_row_mappings[index.row()], index.column()); return target().index(m_row_mappings[index.row()], index.column());
} }
String SortingProxyModel::column_name(int index) const String SortingProxyModel::column_name(int column) const
{ {
return target().column_name(index); return target().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 target_index = map_to_target(index);
if (!target_index.is_valid()) { ASSERT(target_index.is_valid());
dbg() << "BUG! SortingProxyModel: Unable to convert " << index << " to target"; return target().data(target_index, role);
return {};
}
return target().data(map_to_target(index), role);
} }
void SortingProxyModel::update() void SortingProxyModel::update()