From e867e4b84bfc3c8074f848db85edb97806b9f6ed Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Tue, 31 Aug 2021 20:30:51 +0200 Subject: [PATCH] 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. --- Userland/Applications/PixelPaint/Image.cpp | 17 ----------------- Userland/Applications/PixelPaint/Image.h | 1 - .../Applications/PixelPaint/ImageEditor.cpp | 18 ++++++++++++++++++ Userland/Applications/PixelPaint/ImageEditor.h | 2 ++ Userland/Applications/PixelPaint/main.cpp | 2 +- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index c6e16d8b9c..524e03f3e9 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -231,23 +231,6 @@ void Image::serialize_as_json(JsonObjectSerializer& json) const } } -Result 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 Image::write_to_file(const String& file_path) const { StringBuilder builder; diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index f83ed2ce18..13430df761 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -68,7 +68,6 @@ public: void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const; void serialize_as_json(JsonObjectSerializer& json) const; - Result write_to_fd_and_close(int fd) const; Result write_to_file(String const& file_path) const; Result export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel); Result export_png_to_fd_and_close(int fd, bool preserve_alpha_channel); diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 7e9d011e70..8f0f7e2da7 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -469,4 +469,22 @@ void ImageEditor::image_select_layer(Layer* layer) { set_active_layer(layer); } + +Result 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 {}; +} + } diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index a86cd4f154..04bb7d4c62 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -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 save_project_to_fd_and_close(int fd) const; + NonnullRefPtrVector const& guides() const { return m_guides; } bool guide_visibility() { return m_show_guides; } void set_guide_visibility(bool show_guides); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index b495cbe35a..26dbc1e81e 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -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;