diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-02-28 16:54:53 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-01 11:37:02 +0100 |
commit | 147d30ae4f8b97543ea412885f308efc8fc5c3f3 (patch) | |
tree | ec31cb05f92eea44ad4938cc912a18f1b882232a | |
parent | b474f4916479fbf64e6fb5a66cb25b8496e153b3 (diff) | |
download | serenity-147d30ae4f8b97543ea412885f308efc8fc5c3f3.zip |
Spreadsheet: Implement the cut operation for cells
This is done by adding another field to our custom
text/x-spreadsheet-data mime-type that specifies the
action (just copy/cut for now)
-rw-r--r-- | Userland/Applications/Spreadsheet/Spreadsheet.cpp | 6 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Spreadsheet.h | 9 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/main.cpp | 20 |
3 files changed, 25 insertions, 10 deletions
diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.cpp b/Userland/Applications/Spreadsheet/Spreadsheet.cpp index 9fe6240462..48bdbc5e97 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Userland/Applications/Spreadsheet/Spreadsheet.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -325,7 +325,7 @@ Position Sheet::offset_relative_to(const Position& base, const Position& offset, return { new_column, new_row }; } -void Sheet::copy_cells(Vector<Position> from, Vector<Position> to, Optional<Position> resolve_relative_to) +void Sheet::copy_cells(Vector<Position> from, Vector<Position> to, Optional<Position> resolve_relative_to, CopyOperation copy_operation) { auto copy_to = [&](auto& source_position, Position target_position) { auto& target_cell = ensure(target_position); @@ -337,6 +337,8 @@ void Sheet::copy_cells(Vector<Position> from, Vector<Position> to, Optional<Posi } target_cell.copy_from(*source_cell); + if (copy_operation == CopyOperation::Cut) + source_cell->set_data(""); }; if (from.size() == to.size()) { diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.h b/Userland/Applications/Spreadsheet/Spreadsheet.h index 2b903dea46..ba0377dba1 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.h +++ b/Userland/Applications/Spreadsheet/Spreadsheet.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -140,7 +140,12 @@ public: const Workbook& workbook() const { return m_workbook; } - void copy_cells(Vector<Position> from, Vector<Position> to, Optional<Position> resolve_relative_to = {}); + enum class CopyOperation { + Copy, + Cut + }; + + void copy_cells(Vector<Position> from, Vector<Position> to, Optional<Position> resolve_relative_to = {}, CopyOperation copy_operation = CopyOperation::Copy); /// Gives the bottom-right corner of the smallest bounding box containing all the written data. Position written_data_bounds() const; diff --git a/Userland/Applications/Spreadsheet/main.cpp b/Userland/Applications/Spreadsheet/main.cpp index 606aca0309..f306531645 100644 --- a/Userland/Applications/Spreadsheet/main.cpp +++ b/Userland/Applications/Spreadsheet/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -157,13 +157,16 @@ int main(int argc, char* argv[]) }; auto& edit_menu = menubar->add_menu("Edit"); - edit_menu.add_action(GUI::CommonActions::make_copy_action([&](auto&) { + + auto clipboard_action = [&](bool is_cut) { /// text/x-spreadsheet-data: + /// - action: copy/cut /// - currently selected cell /// - selected cell+ auto& cells = spreadsheet_widget.current_worksheet().selected_cells(); VERIFY(!cells.is_empty()); StringBuilder text_builder, url_builder; + url_builder.append(is_cut ? "cut\n" : "copy\n"); bool first = true; auto cursor = spreadsheet_widget.current_selection_cursor(); if (cursor) { @@ -190,10 +193,13 @@ int main(int argc, char* argv[]) } HashMap<String, String> metadata; metadata.set("text/x-spreadsheet-data", url_builder.to_string()); + dbgln(url_builder.to_string()); GUI::Clipboard::the().set_data(text_builder.string_view().bytes(), "text/plain", move(metadata)); - }, - window)); + }; + + edit_menu.add_action(GUI::CommonActions::make_copy_action([&] { clipboard_action(false); }, window)); + edit_menu.add_action(GUI::CommonActions::make_cut_action([&] { clipboard_action(true); }, window)); edit_menu.add_action(GUI::CommonActions::make_paste_action([&](auto&) { ScopeGuard update_after_paste { [&] { spreadsheet_widget.update(); } }; @@ -203,8 +209,10 @@ int main(int argc, char* argv[]) if (auto spreadsheet_data = data.metadata.get("text/x-spreadsheet-data"); spreadsheet_data.has_value()) { Vector<Spreadsheet::Position> source_positions, target_positions; auto& sheet = spreadsheet_widget.current_worksheet(); + auto lines = spreadsheet_data.value().split_view('\n'); + auto action = lines.take_first(); - for (auto& line : spreadsheet_data.value().split_view('\n')) { + for (auto& line : lines) { dbgln("Paste line '{}'", line); auto position = sheet.position_from_url(line); if (position.has_value()) @@ -218,7 +226,7 @@ int main(int argc, char* argv[]) return; auto first_position = source_positions.take_first(); - sheet.copy_cells(move(source_positions), move(target_positions), first_position); + sheet.copy_cells(move(source_positions), move(target_positions), first_position, action == "cut" ? Spreadsheet::Sheet::CopyOperation::Cut : Spreadsheet::Sheet::CopyOperation::Copy); } else { for (auto& cell : spreadsheet_widget.current_worksheet().selected_cells()) spreadsheet_widget.current_worksheet().ensure(cell).set_data(StringView { data.data.data(), data.data.size() }); |