Skip to content

Commit

Permalink
Spreadsheet: Implement the cut operation for cells
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
IdanHo authored and awesomekling committed Mar 1, 2021
1 parent b474f49 commit 147d30a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Userland/Applications/Spreadsheet/Spreadsheet.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down
9 changes: 7 additions & 2 deletions Userland/Applications/Spreadsheet/Spreadsheet.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -140,7 +140,12 @@ class Sheet : public Core::Object {

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;
Expand Down
20 changes: 14 additions & 6 deletions Userland/Applications/Spreadsheet/main.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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(); } };

Expand All @@ -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())
Expand All @@ -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() });
Expand Down

0 comments on commit 147d30a

Please sign in to comment.