Explorar o código

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.
martinfalisse %!s(int64=3) %!d(string=hai) anos
pai
achega
11dffbd96f

+ 4 - 4
Userland/Applications/Spreadsheet/SpreadsheetView.cpp

@@ -94,8 +94,8 @@ void InfinitelyScrollableTableView::mousemove_event(GUI::MouseEvent& event)
                     top_left_most_index = index;
                     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_tl = top_left_rect.center() - event.position();
             auto distance_br = bottom_right_rect.center() - event.position();
             auto distance_br = bottom_right_rect.center() - event.position();
             auto is_over_top_line = false;
             auto is_over_top_line = false;
@@ -201,7 +201,7 @@ void InfinitelyScrollableTableView::mousedown_event(GUI::MouseEvent& event)
             m_is_dragging_for_cut = true;
             m_is_dragging_for_cut = true;
         else if (m_is_hovering_extend_zone)
         else if (m_is_hovering_extend_zone)
             m_is_dragging_for_extend = true;
             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() };
         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);
         AbstractTableView::mousedown_event(adjusted_event);
     } else {
     } else {
@@ -236,7 +236,7 @@ void InfinitelyScrollableTableView::mouseup_event(GUI::MouseEvent& event)
     m_has_committed_to_cutting = false;
     m_has_committed_to_cutting = false;
     m_has_committed_to_extending = false;
     m_has_committed_to_extending = false;
     if (m_is_hovering_cut_zone || m_is_hovering_extend_zone) {
     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() };
         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);
         TableView::mouseup_event(adjusted_event);
     } else {
     } else {

+ 6 - 0
Userland/Libraries/LibGUI/AbstractTableView.cpp

@@ -326,6 +326,12 @@ Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const
     return content_rect(index.row(), index.column());
     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
 Gfx::IntRect AbstractTableView::row_rect(int item_index) const
 {
 {
     return { row_header().is_visible() ? row_header().width() : 0,
     return { row_header().is_visible() ? row_header().width() : 0,

+ 1 - 0
Userland/Libraries/LibGUI/AbstractTableView.h

@@ -55,6 +55,7 @@ public:
     Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
     Gfx::IntPoint adjusted_position(const Gfx::IntPoint&) const;
 
 
     virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
     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 content_rect(int row, int column) const;
     Gfx::IntRect row_rect(int item_index) const;
     Gfx::IntRect row_rect(int item_index) const;