PixelPaint: Move saving a project from Image into ImageEditor

The ImageEditor knows more about the image than Image itself. So to save
a project with all the information known to the program about an image
it's logical that ImageEditor performs that task rather than the Image.

There isn't any additional data added yet, but now there's the
possibility to do so.
This commit is contained in:
Tobias Christiansen 2021-08-31 20:30:51 +02:00 committed by Andreas Kling
parent 60e526fdd2
commit e867e4b84b
5 changed files with 21 additions and 19 deletions

View file

@ -231,23 +231,6 @@ void Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const
}
}
Result<void, String> Image::write_to_fd_and_close(int fd) const
{
StringBuilder builder;
JsonObjectSerializer json(builder);
serialize_as_json(json);
json.finish();
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return String { file->error_string() };
if (!file->write(builder.string_view()))
return String { file->error_string() };
return {};
}
Result<void, String> Image::write_to_file(const String& file_path) const
{
StringBuilder builder;

View file

@ -68,7 +68,6 @@ public:
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
void serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
Result<void, String> write_to_fd_and_close(int fd) const;
Result<void, String> write_to_file(String const& file_path) const;
Result<void, String> export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel);
Result<void, String> export_png_to_fd_and_close(int fd, bool preserve_alpha_channel);

View file

@ -469,4 +469,22 @@ void ImageEditor::image_select_layer(Layer* layer)
{
set_active_layer(layer);
}
Result<void, String> ImageEditor::save_project_to_fd_and_close(int fd) const
{
StringBuilder builder;
JsonObjectSerializer json(builder);
m_image->serialize_as_json(json);
json.finish();
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return String { file->error_string() };
if (!file->write(builder.string_view()))
return String { file->error_string() };
return {};
}
}

View file

@ -84,6 +84,8 @@ public:
Gfx::FloatPoint image_position_to_editor_position(Gfx::IntPoint const&) const;
Gfx::FloatPoint editor_position_to_image_position(Gfx::IntPoint const&) const;
Result<void, String> save_project_to_fd_and_close(int fd) const;
NonnullRefPtrVector<Guide> const& guides() const { return m_guides; }
bool guide_visibility() { return m_show_guides; }
void set_guide_visibility(bool show_guides);

View file

@ -187,7 +187,7 @@ int main(int argc, char** argv)
auto save_result = FileSystemAccessClient::Client::the().save_file(window->window_id(), "untitled", "pp");
if (save_result.error != 0)
return;
auto result = editor->image().write_to_fd_and_close(*save_result.fd);
auto result = editor->save_project_to_fd_and_close(*save_result.fd);
if (result.is_error()) {
GUI::MessageBox::show_error(window, String::formatted("Could not save {}: {}", *save_result.chosen_file, result.error()));
return;