Spreadsheet: Enable Drag&Drop and Copy/Paste

These currently only work on the current sheet, in the current
Spreadsheet instance, but they're still freakin' cool!
This commit is contained in:
AnotherTest 2020-11-02 20:21:07 +03:30 committed by Andreas Kling
parent 821e875bc0
commit 0e544b8afa
Notes: sideshowbarker 2024-07-19 01:34:21 +09:00
5 changed files with 123 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include "SpreadsheetModel.h"
#include "ConditionalFormatting.h"
#include <AK/URL.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Object.h>
@ -68,6 +69,12 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
return cell->typed_display();
}
if (role == GUI::ModelRole::DragData) {
// FIXME: It would be really nice if we could send out a URL *and* some extra data,
// The Event already has support for this, but the user-facing API does not.
return Position { m_sheet->column(index.column()), (size_t)index.row() }.to_url().to_string();
}
if (role == GUI::ModelRole::TextAlignment) {
const auto* cell = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
if (!cell)

View file

@ -44,6 +44,7 @@ public:
virtual void set_data(const GUI::ModelIndex&, const GUI::Variant&) override;
virtual void update() override;
virtual bool is_column_sortable(int) const override { return false; }
virtual StringView drag_data_type() const override { return "text/x-spreadsheet-data"; }
private:
explicit SheetModel(Sheet& sheet)

View file

@ -27,6 +27,9 @@
#include "SpreadsheetView.h"
#include "CellTypeDialog.h"
#include "SpreadsheetModel.h"
#include <AK/ScopeGuard.h>
#include <AK/URL.h>
#include <LibCore/MimeData.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/HeaderView.h>
#include <LibGUI/Menu.h>
@ -144,6 +147,49 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
m_table_view->update();
}
}));
m_table_view->on_drop = [&](const GUI::ModelIndex& index, const GUI::DropEvent& event) {
if (!index.is_valid())
return;
ScopeGuard update_after_drop { [this] { update(); } };
if (event.mime_data().has_format("text/x-spreadsheet-data")) {
auto data = event.mime_data().data("text/x-spreadsheet-data");
StringView urls { data.data(), data.size() };
bool first = true;
for (auto url : urls.lines(false)) {
if (!first) { // FIXME: Allow d&d from many cells to many cells, somehow.
dbg() << "Ignored '" << url << "'";
continue;
}
first = false;
auto& target_cell = m_sheet->ensure({ m_sheet->column(index.column()), (size_t)index.row() });
auto* source_cell = m_sheet->from_url(url);
if (!source_cell) {
target_cell.set_data("");
return;
}
auto ref_cells = target_cell.referencing_cells;
target_cell = *source_cell;
target_cell.dirty = true;
target_cell.referencing_cells = move(ref_cells);
}
return;
}
if (event.mime_data().has_text()) {
auto* target_cell = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
ASSERT(target_cell);
target_cell->set_data(event.text());
return;
}
};
}
void SpreadsheetView::hide_event(GUI::HideEvent&)

View file

@ -46,6 +46,7 @@ public:
const String& current_filename() const { return m_workbook->current_filename(); }
const Sheet& current_worksheet() const { return m_selected_view->sheet(); }
Sheet& current_worksheet() { return m_selected_view->sheet(); }
void set_filename(const String& filename);
Workbook& workbook() { return *m_workbook; }

View file

@ -31,6 +31,7 @@
#include <LibCore/File.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Forward.h>
#include <LibGUI/Icon.h>
@ -150,6 +151,73 @@ int main(int argc, char* argv[])
spreadsheet_widget.set_filename(current_filename);
}));
auto& edit_menu = menubar->add_menu("Edit");
edit_menu.add_action(GUI::CommonActions::make_copy_action([&](auto&) {
auto& cells = spreadsheet_widget.current_worksheet().selected_cells();
ASSERT(!cells.is_empty());
StringBuilder text_builder, url_builder;
bool first = true;
for (auto& cell : cells) {
url_builder.append(cell.to_url().to_string());
url_builder.append('\n');
auto cell_data = spreadsheet_widget.current_worksheet().at(cell);
if (!first)
text_builder.append('\t');
if (cell_data)
text_builder.append(cell_data->data);
first = false;
}
HashMap<String, String> metadata;
metadata.set("text/x-spreadsheet-data", 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_paste_action([&](auto&) {
auto& cells = spreadsheet_widget.current_worksheet().selected_cells();
ASSERT(!cells.is_empty());
const auto& data = GUI::Clipboard::the().data_and_type();
if (auto spreadsheet_data = data.metadata.get("text/x-spreadsheet-data"); spreadsheet_data.has_value()) {
Vector<URL> urls;
for (auto line : spreadsheet_data.value().split_view('\n')) {
if (line.is_empty())
continue;
URL url { line };
if (!url.is_valid())
continue;
urls.append(move(url));
}
if (urls.size() == 1 && cells.size() == 1) {
auto& cell = *cells.begin();
auto& url = urls.first();
auto* source_cell = spreadsheet_widget.current_worksheet().from_url(url);
if (source_cell) {
auto& target_cell = spreadsheet_widget.current_worksheet().ensure(cell);
auto references = target_cell.referencing_cells;
target_cell = *source_cell;
target_cell.referencing_cells = move(references);
target_cell.dirty = true;
spreadsheet_widget.update();
}
return;
}
if (urls.size() != cells.size()) {
// FIXME: Somehow copy a bunch of cells into another bunch of cells.
TODO();
}
} 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() });
spreadsheet_widget.update();
}
},
window));
auto& help_menu = menubar->add_menu("Help");
help_menu.add_action(GUI::Action::create(