Spreadsheet: Allow importing sheets into an existing workbook

This commit is contained in:
Ali Mohammad Pur 2022-06-25 22:06:18 +04:30 committed by Linus Groh
parent 135683795b
commit 64ef808aeb
Notes: sideshowbarker 2024-07-17 09:57:04 +09:00
4 changed files with 50 additions and 0 deletions

View File

@ -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");

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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; }