Browse Source

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.
Tobias Christiansen 3 years ago
parent
commit
e867e4b84b

+ 0 - 17
Userland/Applications/PixelPaint/Image.cpp

@@ -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
 Result<void, String> Image::write_to_file(const String& file_path) const
 {
 {
     StringBuilder builder;
     StringBuilder builder;

+ 0 - 1
Userland/Applications/PixelPaint/Image.h

@@ -68,7 +68,6 @@ public:
     void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
     void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
 
 
     void serialize_as_json(JsonObjectSerializer<StringBuilder>& json) 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> 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_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);
     Result<void, String> export_png_to_fd_and_close(int fd, bool preserve_alpha_channel);

+ 18 - 0
Userland/Applications/PixelPaint/ImageEditor.cpp

@@ -469,4 +469,22 @@ void ImageEditor::image_select_layer(Layer* layer)
 {
 {
     set_active_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 {};
+}
+
 }
 }

+ 2 - 0
Userland/Applications/PixelPaint/ImageEditor.h

@@ -84,6 +84,8 @@ public:
     Gfx::FloatPoint image_position_to_editor_position(Gfx::IntPoint const&) const;
     Gfx::FloatPoint image_position_to_editor_position(Gfx::IntPoint const&) const;
     Gfx::FloatPoint editor_position_to_image_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; }
     NonnullRefPtrVector<Guide> const& guides() const { return m_guides; }
     bool guide_visibility() { return m_show_guides; }
     bool guide_visibility() { return m_show_guides; }
     void set_guide_visibility(bool show_guides);
     void set_guide_visibility(bool show_guides);

+ 1 - 1
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");
         auto save_result = FileSystemAccessClient::Client::the().save_file(window->window_id(), "untitled", "pp");
         if (save_result.error != 0)
         if (save_result.error != 0)
             return;
             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()) {
         if (result.is_error()) {
             GUI::MessageBox::show_error(window, String::formatted("Could not save {}: {}", *save_result.chosen_file, result.error()));
             GUI::MessageBox::show_error(window, String::formatted("Could not save {}: {}", *save_result.chosen_file, result.error()));
             return;
             return;