diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-06-25 22:06:18 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-26 22:21:17 +0100 |
commit | 64ef808aeb7605d29d79d2b8e856f340c32604e4 (patch) | |
tree | 88e45b16861581e9fba42ab7731976a7f7587a99 /Userland/Applications/Spreadsheet | |
parent | 135683795bcf10b5f88af4335e48b66551d7c8a7 (diff) | |
download | serenity-64ef808aeb7605d29d79d2b8e856f340c32604e4.zip |
Spreadsheet: Allow importing sheets into an existing workbook
Diffstat (limited to 'Userland/Applications/Spreadsheet')
4 files changed, 50 insertions, 0 deletions
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index dbc58e0519..a7fa639f3f 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -130,6 +130,14 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe load_file(*response.value()); }); + m_import_action = GUI::Action::create("Import sheets...", [&](auto&) { + auto response = FileSystemAccessClient::Client::the().try_open_file(window()); + if (response.is_error()) + return; + + import_sheets(*response.value()); + }); + m_save_action = GUI::CommonActions::make_save_action([&](auto&) { if (current_filename().is_empty()) { m_save_as_action->activate(); @@ -448,6 +456,30 @@ void SpreadsheetWidget::load_file(Core::File& file) update_window_title(); } +void SpreadsheetWidget::import_sheets(Core::File& file) +{ + auto result = m_workbook->import_file(file); + if (result.is_error()) { + GUI::MessageBox::show_error(window(), result.error()); + return; + } + + if (!result.value()) + return; + + window()->set_modified(true); + + m_cell_value_editor->on_change = nullptr; + m_current_cell_label->set_text(""); + m_should_change_selected_cells = false; + while (auto* widget = m_tab_widget->active_widget()) { + m_tab_widget->remove_tab(*widget); + } + + setup_tabs(m_workbook->sheets()); + update_window_title(); +} + bool SpreadsheetWidget::request_close() { if (!undo_stack().is_current_modified()) @@ -553,6 +585,8 @@ void SpreadsheetWidget::initialize_menubar(GUI::Window& window) file_menu.add_action(*m_save_action); file_menu.add_action(*m_save_as_action); file_menu.add_separator(); + file_menu.add_action(*m_import_action); + file_menu.add_separator(); file_menu.add_action(*m_quit_action); auto& edit_menu = window.add_menu("&Edit"); diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h index abc5864201..734223b04e 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h @@ -25,6 +25,7 @@ public: void save(Core::File&); void load_file(Core::File&); + void import_sheets(Core::File&); bool request_close(); void add_sheet(); void add_sheet(NonnullRefPtr<Sheet>&&); @@ -83,6 +84,8 @@ private: RefPtr<GUI::Action> m_save_as_action; RefPtr<GUI::Action> m_quit_action; + RefPtr<GUI::Action> m_import_action; + RefPtr<GUI::Action> m_cut_action; RefPtr<GUI::Action> m_copy_action; RefPtr<GUI::Action> m_paste_action; diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp index 32887e6af3..b418c49cbe 100644 --- a/Userland/Applications/Spreadsheet/Workbook.cpp +++ b/Userland/Applications/Spreadsheet/Workbook.cpp @@ -76,4 +76,15 @@ Result<bool, String> Workbook::write_to_file(Core::File& file) return true; } +Result<bool, String> Workbook::import_file(Core::File& file) +{ + auto mime = Core::guess_mime_type_based_on_filename(file.filename()); + + auto sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, file, *this)); + auto has_any_changes = !sheets.is_empty(); + m_sheets.extend(move(sheets)); + + return has_any_changes; +} + } diff --git a/Userland/Applications/Spreadsheet/Workbook.h b/Userland/Applications/Spreadsheet/Workbook.h index 656d00fc96..8ee7e7b895 100644 --- a/Userland/Applications/Spreadsheet/Workbook.h +++ b/Userland/Applications/Spreadsheet/Workbook.h @@ -20,6 +20,8 @@ public: Result<bool, String> open_file(Core::File&); Result<bool, String> write_to_file(Core::File&); + Result<bool, String> import_file(Core::File&); + String const& current_filename() const { return m_current_filename; } bool set_filename(String const& filename); bool dirty() { return m_dirty; } |