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
Notes: sideshowbarker 2024-07-19 04:48:44 +09:00
4 changed files with 21 additions and 17 deletions

View File

@ -166,8 +166,7 @@ DirectoryView::DirectoryView()
handle_activation(index);
};
m_table_view->on_activation = [&](auto& index) {
auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model();
handle_activation(filter_model.map_to_target(index));
handle_activation(map_table_view_index(index));
};
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) {
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) {
if (on_context_menu_request)
@ -201,7 +200,7 @@ DirectoryView::DirectoryView()
m_table_view->on_drop = [this](auto& index, auto& event) {
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) {
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()
{
size_t total_size = model().node({}).total_size;
@ -338,10 +343,8 @@ void DirectoryView::update_statusbar()
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.
if (m_view_mode == ViewMode::Table) {
auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model();
index = filter_model.map_to_target(index);
}
if (m_view_mode == ViewMode::Table)
index = map_table_view_index(index);
auto& node = model().node(index);
if (!node.symlink_target.is_empty()) {

View File

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

View File

@ -332,6 +332,7 @@ const FileSystemModel::Node& FileSystemModel::node(const ModelIndex& index) cons
{
if (!index.is_valid())
return *m_root;
ASSERT(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
{
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
{
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
@ -74,19 +76,16 @@ ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const
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
{
auto target_index = map_to_target(index);
if (!target_index.is_valid()) {
dbg() << "BUG! SortingProxyModel: Unable to convert " << index << " to target";
return {};
}
return target().data(map_to_target(index), role);
ASSERT(target_index.is_valid());
return target().data(target_index, role);
}
void SortingProxyModel::update()