LibGUI: Add granular ColumnView helpers for column and index at events

This patch extracts logic for getting colum at an event position and for
getting an index in a column at an event position from
index_at_event_position into separate functions.
This commit is contained in:
networkException 2022-09-04 16:48:35 +02:00 committed by Linus Groh
parent 707b4f83eb
commit f0a20fc902
Notes: sideshowbarker 2024-07-17 07:31:24 +09:00
2 changed files with 30 additions and 9 deletions

View file

@ -206,7 +206,7 @@ void ColumnsView::update_column_sizes()
set_content_size({ total_width, total_height }); set_content_size({ total_width, total_height });
} }
ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position) const Optional<ColumnsView::Column> ColumnsView::column_at_event_position(Gfx::IntPoint const& a_position) const
{ {
if (!model()) if (!model())
return {}; return {};
@ -215,7 +215,7 @@ ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position)
int column_x = 0; int column_x = 0;
for (auto& column : m_columns) { for (auto const& column : m_columns) {
if (position.x() < column_x) if (position.x() < column_x)
break; break;
if (position.x() > column_x + column.width) { if (position.x() > column_x + column.width) {
@ -223,12 +223,7 @@ ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position)
continue; continue;
} }
int row = position.y() / item_height(); return column;
int row_count = model()->row_count(column.parent_index);
if (row >= row_count)
return {};
return model()->index(row, m_model_column, column.parent_index);
} }
return {}; return {};
@ -248,6 +243,25 @@ void ColumnsView::select_range(ModelIndex const& index)
} }
} }
ModelIndex ColumnsView::index_at_event_position_in_column(Gfx::IntPoint const& position, Column const& column) const
{
int row = position.y() / item_height();
int row_count = model()->row_count(column.parent_index);
if (row >= row_count)
return {};
return model()->index(row, m_model_column, column.parent_index);
}
ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& position) const
{
auto const& column = column_at_event_position(position);
if (!column.has_value())
return {};
return index_at_event_position_in_column(position, *column);
}
void ColumnsView::mousedown_event(MouseEvent& event) void ColumnsView::mousedown_event(MouseEvent& event)
{ {
AbstractView::mousedown_event(event); AbstractView::mousedown_event(event);
@ -258,7 +272,11 @@ void ColumnsView::mousedown_event(MouseEvent& event)
if (event.button() != MouseButton::Primary) if (event.button() != MouseButton::Primary)
return; return;
auto index = index_at_event_position(event.position()); auto column = column_at_event_position(event.position());
if (!column.has_value())
return;
auto index = index_at_event_position_in_column(event.position(), *column);
if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) { if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
if (model()->row_count(index)) if (model()->row_count(index))
push_column(index); push_column(index);

View file

@ -48,6 +48,9 @@ private:
// TODO: per-column vertical scroll? // TODO: per-column vertical scroll?
}; };
Optional<Column> column_at_event_position(Gfx::IntPoint const&) const;
ModelIndex index_at_event_position_in_column(Gfx::IntPoint const&, Column const&) const;
Vector<Column> m_columns; Vector<Column> m_columns;
int m_model_column { 0 }; int m_model_column { 0 };
}; };