1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:17:45 +00:00

PixelPaint: Rename Image::create_foo() => Image::try_create_foo()

Factory functions that may fail should be called try_create().
This commit is contained in:
Andreas Kling 2021-06-11 17:49:04 +02:00
parent 29e80178a8
commit a9e98bad8a
3 changed files with 22 additions and 23 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -22,7 +22,7 @@
namespace PixelPaint {
RefPtr<Image> Image::create_with_size(Gfx::IntSize const& size)
RefPtr<Image> Image::try_create_with_size(Gfx::IntSize const& size)
{
if (size.is_empty())
return nullptr;
@ -52,9 +52,9 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect)
}
}
RefPtr<Image> Image::create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
RefPtr<Image> Image::try_create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
{
auto image = create_with_size({ bitmap->width(), bitmap->height() });
auto image = try_create_with_size({ bitmap->width(), bitmap->height() });
if (image.is_null())
return nullptr;
@ -67,7 +67,7 @@ RefPtr<Image> Image::create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
return image;
}
RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
RefPtr<Image> Image::try_create_from_pixel_paint_file(String const& file_path)
{
auto file = fopen(file_path.characters(), "r");
fseek(file, 0L, SEEK_END);
@ -83,7 +83,7 @@ RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
return nullptr;
auto json = json_or_error.value().as_object();
auto image = create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() });
auto image = try_create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() });
json.get("layers").as_array().for_each([&](JsonValue json_layer) {
auto json_layer_object = json_layer.as_object();
auto width = json_layer_object.get("width").to_i32();
@ -105,14 +105,11 @@ RefPtr<Image> Image::create_from_pixel_paint_file(String const& file_path)
return image;
}
RefPtr<Image> Image::create_from_file(String const& file_path)
RefPtr<Image> Image::try_create_from_file(String const& file_path)
{
auto bitmap = Gfx::Bitmap::load_from_file(file_path);
if (bitmap) {
return create_from_bitmap(bitmap);
}
return create_from_pixel_paint_file(file_path);
if (auto bitmap = Gfx::Bitmap::load_from_file(file_path))
return try_create_from_bitmap(bitmap);
return try_create_from_pixel_paint_file(file_path);
}
void Image::save(String const& file_path) const
@ -188,7 +185,9 @@ void Image::add_layer(NonnullRefPtr<Layer> layer)
RefPtr<Image> Image::take_snapshot() const
{
auto snapshot = create_with_size(m_size);
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));
return snapshot;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -36,9 +36,9 @@ protected:
class Image : public RefCounted<Image> {
public:
static RefPtr<Image> create_with_size(Gfx::IntSize const&);
static RefPtr<Image> create_from_file(String const& file_path);
static RefPtr<Image> create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap);
static RefPtr<Image> try_create_with_size(Gfx::IntSize const&);
static RefPtr<Image> try_create_from_file(String const& file_path);
static RefPtr<Image> try_create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap);
size_t layer_count() const { return m_layers.size(); }
Layer const& layer(size_t index) const { return m_layers.at(index); }
@ -75,7 +75,7 @@ public:
private:
explicit Image(Gfx::IntSize const&);
static RefPtr<Image> create_from_pixel_paint_file(String const& file_path);
static RefPtr<Image> try_create_from_pixel_paint_file(String const& file_path);
void did_change();
void did_modify_layer_stack();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -87,7 +87,7 @@ int main(int argc, char** argv)
"&New Image...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](auto&) {
auto dialog = PixelPaint::CreateNewImageDialog::construct(window);
if (dialog->exec() == GUI::Dialog::ExecOK) {
auto image = PixelPaint::Image::create_with_size(dialog->image_size());
auto image = PixelPaint::Image::try_create_with_size(dialog->image_size());
auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background");
image->add_layer(*bg_layer);
bg_layer->bitmap().fill(Color::White);
@ -100,7 +100,7 @@ int main(int argc, char** argv)
window);
auto open_image_file = [&](auto& path) {
auto image = PixelPaint::Image::create_from_file(path);
auto image = PixelPaint::Image::try_create_from_file(path);
if (!image) {
GUI::MessageBox::show_error(window, String::formatted("Invalid image file: {}", path));
return;
@ -395,7 +395,7 @@ int main(int argc, char** argv)
if (Core::File::exists(image_file_real_path)) {
open_image_file(image_file_real_path);
} else {
auto image = PixelPaint::Image::create_with_size({ 480, 360 });
auto image = PixelPaint::Image::try_create_with_size({ 480, 360 });
auto bg_layer = PixelPaint::Layer::create_with_size(*image, image->size(), "Background");
image->add_layer(*bg_layer);