1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

Spreadsheet: Add "Save As"

This commit is contained in:
AnotherTest 2020-08-25 01:06:32 +04:30 committed by Andreas Kling
parent 5568da9a59
commit 3320bb45d1
Notes: sideshowbarker 2024-07-19 03:11:31 +09:00
3 changed files with 41 additions and 0 deletions

View file

@ -137,6 +137,20 @@ SpreadsheetWidget::~SpreadsheetWidget()
{
}
void SpreadsheetWidget::set_filename(const String& filename)
{
if (m_current_filename == filename)
return;
m_current_filename = filename;
StringBuilder builder;
builder.append("Spreadsheet - ");
builder.append(m_current_filename);
window()->set_title(builder.string_view());
window()->update();
}
void SpreadsheetWidget::load(const StringView& filename)
{
auto file_or_error = Core::File::open(filename, Core::IODevice::OpenMode::ReadOnly);
@ -195,6 +209,8 @@ void SpreadsheetWidget::load(const StringView& filename)
}
setup_tabs();
set_filename(filename);
}
void SpreadsheetWidget::save(const StringView& filename)
@ -230,6 +246,8 @@ void SpreadsheetWidget::save(const StringView& filename)
GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
return;
}
set_filename(filename);
}
}

View file

@ -41,6 +41,9 @@ public:
void save(const StringView& filename);
void load(const StringView& filename);
const String& current_filename() const { return m_current_filename; }
void set_filename(const String& filename);
private:
explicit SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets = {}, bool should_add_sheet_if_empty = true);
@ -51,6 +54,8 @@ private:
RefPtr<GUI::TabWidget> m_tab_widget;
RefPtr<GUI::Label> m_current_cell_label;
RefPtr<GUI::TextEditor> m_cell_value_editor;
String m_current_filename;
};
}

View file

@ -107,13 +107,31 @@ int main(int argc, char* argv[])
spreadsheet_widget.load(load_path.value());
}));
file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
if (spreadsheet_widget.current_filename().is_empty()) {
String name = "sheet";
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
if (!save_path.has_value())
return;
spreadsheet_widget.save(save_path.value());
} else {
spreadsheet_widget.save(spreadsheet_widget.current_filename());
}
}));
file_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
auto current_filename = spreadsheet_widget.current_filename();
String name = "sheet";
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
if (!save_path.has_value())
return;
spreadsheet_widget.save(save_path.value());
if (!current_filename.is_empty())
spreadsheet_widget.set_filename(current_filename);
}));
app->set_menubar(move(menubar));