From e98d0dafa0a43ce3201b9bd69ce05efe3a26bc2d Mon Sep 17 00:00:00 2001 From: martinfalisse Date: Tue, 8 Feb 2022 11:54:04 +0100 Subject: [PATCH] Spreadsheet: On cut end select same cells in target location When finished dragging and cutting, select the cells in the destination. E.g. if you select 5 cells and drag and paste them in a new location, select the 5 pasted cells in the destination. --- .../Spreadsheet/SpreadsheetView.cpp | 32 +++++++++++++++++++ .../Spreadsheet/SpreadsheetView.h | 1 + 2 files changed, 33 insertions(+) diff --git a/Userland/Applications/Spreadsheet/SpreadsheetView.cpp b/Userland/Applications/Spreadsheet/SpreadsheetView.cpp index b8b1536243..b82c74487d 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetView.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetView.cpp @@ -201,6 +201,38 @@ void InfinitelyScrollableTableView::mouseup_event(GUI::MouseEvent& event) } } +void InfinitelyScrollableTableView::drop_event(GUI::DropEvent& event) +{ + TableView::drop_event(event); + m_is_dragging_for_copy = false; + set_override_cursor(Gfx::StandardCursor::Arrow); + auto drop_index = index_at_event_position(event.position()); + if (selection().size() > 0) { + // Get top left index position of previous selection + auto top_left_most_index = selection().first(); + selection().for_each_index([&](auto& index) { + if (index.row() < top_left_most_index.row()) + top_left_most_index = index; + else if (index.column() < top_left_most_index.column()) + top_left_most_index = index; + }); + + // Compare with drag location + auto x_diff = drop_index.column() - top_left_most_index.column(); + auto y_diff = drop_index.row() - top_left_most_index.row(); + + // Set new selection + Vector new_selection; + for (auto& index : selection().indices()) { + auto new_index = model()->index(index.row() + y_diff, index.column() + x_diff); + new_selection.append(move(new_index)); + } + selection().clear(); + AbstractTableView::set_cursor(drop_index, SelectionUpdate::Set); + selection().add_all(new_selection); + } +} + void SpreadsheetView::update_with_model() { m_sheet_model->update(); diff --git a/Userland/Applications/Spreadsheet/SpreadsheetView.h b/Userland/Applications/Spreadsheet/SpreadsheetView.h index 6d4111e1f8..5ae78353d6 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetView.h +++ b/Userland/Applications/Spreadsheet/SpreadsheetView.h @@ -76,6 +76,7 @@ private: virtual void mousemove_event(GUI::MouseEvent&) override; virtual void mousedown_event(GUI::MouseEvent&) override; virtual void mouseup_event(GUI::MouseEvent&) override; + virtual void drop_event(GUI::DropEvent&) override; bool m_should_intercept_drag { false }; bool m_has_committed_to_dragging { false };