mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibGUI: Add GListView::index_at_event_position()
And port GListView::mousedown_event() to it.
This commit is contained in:
parent
b4923938e1
commit
2ccad40a16
2 changed files with 20 additions and 9 deletions
|
@ -84,7 +84,20 @@ Rect GListView::content_rect(const GModelIndex& index) const
|
||||||
return content_rect(index.row());
|
return content_rect(index.row());
|
||||||
}
|
}
|
||||||
|
|
||||||
Point GListView::adjusted_position(const Point& position)
|
GModelIndex GListView::index_at_event_position(const Point& point) const
|
||||||
|
{
|
||||||
|
ASSERT(model());
|
||||||
|
|
||||||
|
auto adjusted_position = this->adjusted_position(point);
|
||||||
|
for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
|
||||||
|
if (!content_rect(row).contains(adjusted_position))
|
||||||
|
continue;
|
||||||
|
return model()->index(row, m_model_column);
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Point GListView::adjusted_position(const Point& position) const
|
||||||
{
|
{
|
||||||
return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
|
return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
|
||||||
}
|
}
|
||||||
|
@ -97,18 +110,15 @@ void GListView::mousedown_event(GMouseEvent& event)
|
||||||
if (event.button() != GMouseButton::Left)
|
if (event.button() != GMouseButton::Left)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto adjusted_position = this->adjusted_position(event.position());
|
auto index = index_at_event_position(event.position());
|
||||||
for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
|
if (index.is_valid()) {
|
||||||
if (!content_rect(row).contains(adjusted_position))
|
|
||||||
continue;
|
|
||||||
auto index = model()->index(row, m_model_column);
|
|
||||||
if (event.modifiers() & Mod_Ctrl)
|
if (event.modifiers() & Mod_Ctrl)
|
||||||
selection().toggle(index);
|
selection().toggle(index);
|
||||||
else
|
else
|
||||||
selection().set(index);
|
selection().set(index);
|
||||||
return;
|
} else {
|
||||||
|
selection().clear();
|
||||||
}
|
}
|
||||||
selection().clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GListView::paint_event(GPaintEvent& event)
|
void GListView::paint_event(GPaintEvent& event)
|
||||||
|
|
|
@ -49,8 +49,9 @@ public:
|
||||||
|
|
||||||
void scroll_into_view(const GModelIndex&, Orientation);
|
void scroll_into_view(const GModelIndex&, Orientation);
|
||||||
|
|
||||||
Point adjusted_position(const Point&);
|
Point adjusted_position(const Point&) const;
|
||||||
|
|
||||||
|
GModelIndex index_at_event_position(const Point&) const;
|
||||||
virtual Rect content_rect(const GModelIndex&) const override;
|
virtual Rect content_rect(const GModelIndex&) const override;
|
||||||
|
|
||||||
int model_column() const { return m_model_column; }
|
int model_column() const { return m_model_column; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue