Spreadsheet+LibGUI: Calculate cell position given scroll position

Take into account the current scroll position when calculating the
position of cells. This way when the user scrolls either horizontally
or vertically, the calculations done to find the cell position
will be correct.
This commit is contained in:
martinfalisse 2022-03-07 16:26:21 +01:00 committed by Ali Mohammad Pur
parent 5759b25ca8
commit 11dffbd96f
Notes: sideshowbarker 2024-07-17 17:08:08 +09:00
3 changed files with 11 additions and 4 deletions

View file

@ -94,8 +94,8 @@ void InfinitelyScrollableTableView::mousemove_event(GUI::MouseEvent& event)
top_left_most_index = index;
});
auto top_left_rect = content_rect(top_left_most_index);
auto bottom_right_rect = content_rect(bottom_right_most_index);
auto top_left_rect = content_rect_minus_scrollbars(top_left_most_index);
auto bottom_right_rect = content_rect_minus_scrollbars(bottom_right_most_index);
auto distance_tl = top_left_rect.center() - event.position();
auto distance_br = bottom_right_rect.center() - event.position();
auto is_over_top_line = false;
@ -201,7 +201,7 @@ void InfinitelyScrollableTableView::mousedown_event(GUI::MouseEvent& event)
m_is_dragging_for_cut = true;
else if (m_is_hovering_extend_zone)
m_is_dragging_for_extend = true;
auto rect = content_rect(m_target_cell);
auto rect = content_rect_minus_scrollbars(m_target_cell);
GUI::MouseEvent adjusted_event = { (GUI::Event::Type)event.type(), rect.center(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y() };
AbstractTableView::mousedown_event(adjusted_event);
} else {
@ -236,7 +236,7 @@ void InfinitelyScrollableTableView::mouseup_event(GUI::MouseEvent& event)
m_has_committed_to_cutting = false;
m_has_committed_to_extending = false;
if (m_is_hovering_cut_zone || m_is_hovering_extend_zone) {
auto rect = content_rect(m_target_cell);
auto rect = content_rect_minus_scrollbars(m_target_cell);
GUI::MouseEvent adjusted_event = { (GUI::Event::Type)event.type(), rect.center(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y() };
TableView::mouseup_event(adjusted_event);
} else {

View file

@ -326,6 +326,12 @@ Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const
return content_rect(index.row(), index.column());
}
Gfx::IntRect AbstractTableView::content_rect_minus_scrollbars(const ModelIndex& index) const
{
auto naive_content_rect = content_rect(index.row(), index.column());
return { naive_content_rect.x() - horizontal_scrollbar().value(), naive_content_rect.y() - vertical_scrollbar().value(), naive_content_rect.width(), naive_content_rect.height() };
}
Gfx::IntRect AbstractTableView::row_rect(int item_index) const
{
return { row_header().is_visible() ? row_header().width() : 0,

View file

@ -55,6 +55,7 @@ public:
Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
Gfx::IntRect content_rect_minus_scrollbars(const ModelIndex&) const;
Gfx::IntRect content_rect(int row, int column) const;
Gfx::IntRect row_rect(int item_index) const;