From 9c5de113b1bda12ef41c3b7ab6398f7f0e1931f9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 11 Jun 2021 23:06:46 +0200 Subject: [PATCH] PixelPaint: Rename Layer::create_foo() => Layer::try_create_foo() --- Userland/Applications/PixelPaint/Image.cpp | 21 +++++++++++++-------- Userland/Applications/PixelPaint/Layer.cpp | 10 +++++----- Userland/Applications/PixelPaint/Layer.h | 8 ++++---- Userland/Applications/PixelPaint/main.cpp | 11 +++++++---- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 88020e81c3..a756470cc3 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -55,15 +55,14 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) RefPtr Image::try_create_from_bitmap(RefPtr bitmap) { auto image = try_create_with_size({ bitmap->width(), bitmap->height() }); - if (image.is_null()) + if (!image) return nullptr; - auto layer = Layer::create_with_bitmap(*image, *bitmap, "Background"); - if (layer.is_null()) + auto layer = Layer::try_create_with_bitmap(*image, *bitmap, "Background"); + if (!layer) return nullptr; image->add_layer(layer.release_nonnull()); - return image; } @@ -89,7 +88,8 @@ RefPtr Image::try_create_from_pixel_paint_file(String const& file_path) auto width = json_layer_object.get("width").to_i32(); auto height = json_layer_object.get("height").to_i32(); auto name = json_layer_object.get("name").as_string(); - auto layer = Layer::create_with_size(*image, { width, height }, name); + auto layer = Layer::try_create_with_size(*image, { width, height }, name); + VERIFY(layer); layer->set_location({ json_layer_object.get("locationx").to_i32(), json_layer_object.get("locationy").to_i32() }); layer->set_opacity_percent(json_layer_object.get("opacity_percent").to_i32()); layer->set_visible(json_layer_object.get("visible").as_bool()); @@ -188,8 +188,12 @@ RefPtr Image::take_snapshot() const auto snapshot = try_create_with_size(m_size); if (!snapshot) return nullptr; - for (const auto& layer : m_layers) - snapshot->add_layer(*Layer::create_snapshot(*snapshot, layer)); + for (const auto& layer : m_layers) { + auto layer_snapshot = Layer::try_create_snapshot(*snapshot, layer); + if (!layer_snapshot) + return nullptr; + snapshot->add_layer(layer_snapshot.release_nonnull()); + } return snapshot; } @@ -198,7 +202,8 @@ void Image::restore_snapshot(Image const& snapshot) m_layers.clear(); select_layer(nullptr); for (const auto& snapshot_layer : snapshot.m_layers) { - auto layer = Layer::create_snapshot(*this, snapshot_layer); + auto layer = Layer::try_create_snapshot(*this, snapshot_layer); + VERIFY(layer); if (layer->is_selected()) select_layer(layer.ptr()); add_layer(*layer); diff --git a/Userland/Applications/PixelPaint/Layer.cpp b/Userland/Applications/PixelPaint/Layer.cpp index 280a39fe8c..2b483f7885 100644 --- a/Userland/Applications/PixelPaint/Layer.cpp +++ b/Userland/Applications/PixelPaint/Layer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,7 +10,7 @@ namespace PixelPaint { -RefPtr Layer::create_with_size(Image& image, Gfx::IntSize const& size, String const& name) +RefPtr Layer::try_create_with_size(Image& image, Gfx::IntSize const& size, String const& name) { if (size.is_empty()) return nullptr; @@ -21,7 +21,7 @@ RefPtr Layer::create_with_size(Image& image, Gfx::IntSize const& size, St return adopt_ref(*new Layer(image, size, name)); } -RefPtr Layer::create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap, String const& name) +RefPtr Layer::try_create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap, String const& name) { if (bitmap.size().is_empty()) return nullptr; @@ -32,9 +32,9 @@ RefPtr Layer::create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap, return adopt_ref(*new Layer(image, bitmap, name)); } -RefPtr Layer::create_snapshot(Image& image, Layer const& layer) +RefPtr Layer::try_create_snapshot(Image& image, Layer const& layer) { - auto snapshot = create_with_bitmap(image, *layer.bitmap().clone(), layer.name()); + auto snapshot = try_create_with_bitmap(image, *layer.bitmap().clone(), layer.name()); /* We set these properties directly because calling the setters might notify the image of an update on the newly created layer, but this diff --git a/Userland/Applications/PixelPaint/Layer.h b/Userland/Applications/PixelPaint/Layer.h index c0f58e4402..8286ed84a0 100644 --- a/Userland/Applications/PixelPaint/Layer.h +++ b/Userland/Applications/PixelPaint/Layer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -24,9 +24,9 @@ class Layer AK_MAKE_NONMOVABLE(Layer); public: - static RefPtr create_with_size(Image&, Gfx::IntSize const&, String const& name); - static RefPtr create_with_bitmap(Image&, Gfx::Bitmap const&, String const& name); - static RefPtr create_snapshot(Image&, Layer const&); + static RefPtr try_create_with_size(Image&, Gfx::IntSize const&, String const& name); + static RefPtr try_create_with_bitmap(Image&, Gfx::Bitmap const&, String const& name); + static RefPtr try_create_snapshot(Image&, Layer const&); ~Layer() { } diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index c6958b713f..6617a2011d 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -88,7 +88,8 @@ int main(int argc, char** argv) auto dialog = PixelPaint::CreateNewImageDialog::construct(window); if (dialog->exec() == GUI::Dialog::ExecOK) { auto image = PixelPaint::Image::try_create_with_size(dialog->image_size()); - auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background"); + auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background"); + VERIFY(bg_layer); image->add_layer(*bg_layer); bg_layer->bitmap().fill(Color::White); @@ -170,7 +171,8 @@ int main(int argc, char** argv) if (!bitmap) return; - auto layer = PixelPaint::Layer::create_with_bitmap(*image_editor.image(), *bitmap, "Pasted layer"); + auto layer = PixelPaint::Layer::try_create_with_bitmap(*image_editor.image(), *bitmap, "Pasted layer"); + VERIFY(layer); image_editor.image()->add_layer(layer.release_nonnull()); }); GUI::Clipboard::the().on_change = [&](auto& mime_type) { @@ -228,7 +230,7 @@ int main(int argc, char** argv) "New &Layer...", { Mod_Ctrl | Mod_Shift, Key_N }, [&](auto&) { auto dialog = PixelPaint::CreateNewLayerDialog::construct(image_editor.image()->size(), window); if (dialog->exec() == GUI::Dialog::ExecOK) { - auto layer = PixelPaint::Layer::create_with_size(*image_editor.image(), dialog->layer_size(), dialog->layer_name()); + auto layer = PixelPaint::Layer::try_create_with_size(*image_editor.image(), dialog->layer_size(), dialog->layer_name()); if (!layer) { GUI::MessageBox::show_error(window, String::formatted("Unable to create layer with size {}", dialog->size().to_string())); return; @@ -397,7 +399,8 @@ int main(int argc, char** argv) } else { auto image = PixelPaint::Image::try_create_with_size({ 480, 360 }); - auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background"); + auto bg_layer = PixelPaint::Layer::try_create_with_size(*image, image->size(), "Background"); + VERIFY(bg_layer); image->add_layer(*bg_layer); bg_layer->bitmap().fill(Color::White);