From bd65ecf05c5f10bc8547969f8b49f975d6c62b08 Mon Sep 17 00:00:00 2001 From: Baitinq Date: Thu, 22 Dec 2022 02:07:58 +0100 Subject: [PATCH] PixelPaint: Cleanup the Image class This patch just introduces some general cleanup regarding unused imports and adding the const qualifier to eligible functions and variables. --- Userland/Applications/PixelPaint/Image.cpp | 45 ++++++++++------------ Userland/Applications/PixelPaint/Image.h | 6 +-- Userland/Applications/PixelPaint/Layer.cpp | 2 +- Userland/Applications/PixelPaint/Layer.h | 20 +++++----- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 6831979c12..15d572d71c 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -11,9 +11,6 @@ #include "Selection.h" #include #include -#include -#include -#include #include #include #include @@ -45,7 +42,7 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) con float scale = (float)dest_rect.width() / (float)rect().width(); Gfx::PainterStateSaver saver(painter); painter.add_clip_rect(dest_rect); - for (auto& layer : m_layers) { + for (auto const& layer : m_layers) { if (!layer.is_visible()) continue; auto target = dest_rect.translated(layer.location().x() * scale, layer.location().y() * scale); @@ -75,7 +72,7 @@ ErrorOr> Image::try_decode_bitmap(ReadonlyBytes bitma return decoded_bitmap.release_nonnull(); } -ErrorOr> Image::try_create_from_bitmap(NonnullRefPtr bitmap) +ErrorOr> Image::try_create_from_bitmap(NonnullRefPtr const& bitmap) { auto image = TRY(try_create_with_size({ bitmap->width(), bitmap->height() })); auto layer = TRY(Layer::try_create_with_bitmap(*image, *bitmap, "Background")); @@ -88,8 +85,8 @@ ErrorOr> Image::try_create_from_pixel_paint_json(JsonObject auto image = TRY(try_create_with_size({ json.get("width"sv).to_i32(), json.get("height"sv).to_i32() })); auto layers_value = json.get("layers"sv); - for (auto& layer_value : layers_value.as_array().values()) { - auto& layer_object = layer_value.as_object(); + for (auto const& layer_value : layers_value.as_array().values()) { + auto const& layer_object = layer_value.as_object(); auto name = layer_object.get("name"sv).as_string(); auto bitmap_base64_encoded = layer_object.get("bitmap"sv).as_string(); @@ -97,7 +94,7 @@ ErrorOr> Image::try_create_from_pixel_paint_json(JsonObject auto bitmap = TRY(try_decode_bitmap(bitmap_data)); auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name)); - if (auto mask_object = layer_object.get("mask"sv); !mask_object.is_null()) { + if (auto const& mask_object = layer_object.get("mask"sv); !mask_object.is_null()) { auto mask_base64_encoded = mask_object.as_string(); auto mask_data = TRY(decode_base64(mask_base64_encoded)); auto mask = TRY(try_decode_bitmap(mask_data)); @@ -174,7 +171,7 @@ RefPtr Image::try_copy_bitmap(Selection const& selection) const return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors(); } -ErrorOr Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel) +ErrorOr Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel) const { auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888; auto bitmap = TRY(try_compose_bitmap(bitmap_format)); @@ -188,7 +185,7 @@ ErrorOr Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_ch return {}; } -ErrorOr Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel) +ErrorOr Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel) const { auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888; auto bitmap = TRY(try_compose_bitmap(bitmap_format)); @@ -507,14 +504,14 @@ ErrorOr Image::flip(Gfx::Orientation orientation) if (layer.is_selected()) selected_layer_index = i; - TRY(new_layer->flip(orientation, Layer::NotifyClients::NO)); + TRY(new_layer->flip(orientation, Layer::NotifyClients::No)); - flipped_layers.append(new_layer); + flipped_layers.unchecked_append(new_layer); } m_layers = move(flipped_layers); for (auto& layer : m_layers) - layer.did_modify_bitmap({}, Layer::NotifyClients::YES); + layer.did_modify_bitmap({}, Layer::NotifyClients::No); select_layer(&m_layers[selected_layer_index]); @@ -538,14 +535,14 @@ ErrorOr Image::rotate(Gfx::RotationDirection direction) if (layer.is_selected()) selected_layer_index = i; - TRY(new_layer->rotate(direction, Layer::NotifyClients::NO)); + TRY(new_layer->rotate(direction, Layer::NotifyClients::No)); - rotated_layers.append(new_layer); + rotated_layers.unchecked_append(new_layer); } m_layers = move(rotated_layers); for (auto& layer : m_layers) - layer.did_modify_bitmap({}, Layer::NotifyClients::YES); + layer.did_modify_bitmap({}, Layer::NotifyClients::Yes); select_layer(&m_layers[selected_layer_index]); @@ -572,19 +569,19 @@ ErrorOr Image::crop(Gfx::IntRect const& cropped_rect) auto layer_location = new_layer->location(); auto layer_local_crop_rect = new_layer->relative_rect().intersected(cropped_rect).translated(-layer_location.x(), -layer_location.y()); - TRY(new_layer->crop(layer_local_crop_rect, Layer::NotifyClients::NO)); + TRY(new_layer->crop(layer_local_crop_rect, Layer::NotifyClients::No)); auto new_layer_x = max(0, layer_location.x() - cropped_rect.x()); auto new_layer_y = max(0, layer_location.y() - cropped_rect.y()); new_layer->set_location({ new_layer_x, new_layer_y }); - cropped_layers.append(new_layer); + cropped_layers.unchecked_append(new_layer); } m_layers = move(cropped_layers); for (auto& layer : m_layers) - layer.did_modify_bitmap({}, Layer::NotifyClients::YES); + layer.did_modify_bitmap({}, Layer::NotifyClients::Yes); select_layer(&m_layers[selected_layer_index]); @@ -600,7 +597,7 @@ Optional Image::nonempty_content_bounding_rect() const return {}; Optional bounding_rect; - for (auto& layer : m_layers) { + for (auto const& layer : m_layers) { auto layer_content_rect_in_layer_coordinates = layer.nonempty_content_bounding_rect(); if (!layer_content_rect_in_layer_coordinates.has_value()) continue; @@ -641,14 +638,14 @@ ErrorOr Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode sca selected_layer_index = i; Gfx::IntPoint new_location(scale_x * new_layer->location().x(), scale_y * new_layer->location().y()); - TRY(new_layer->resize(new_size, new_location, scaling_mode, Layer::NotifyClients::NO)); + TRY(new_layer->resize(new_size, new_location, scaling_mode, Layer::NotifyClients::No)); - resized_layers.append(new_layer); + resized_layers.unchecked_append(new_layer); } m_layers = move(resized_layers); for (auto& layer : m_layers) - layer.did_modify_bitmap({}, Layer::NotifyClients::YES); + layer.did_modify_bitmap({}, Layer::NotifyClients::Yes); select_layer(&m_layers[selected_layer_index]); @@ -661,7 +658,7 @@ ErrorOr Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode sca Color Image::color_at(Gfx::IntPoint point) const { Color color; - for (auto& layer : m_layers) { + for (auto const& layer : m_layers) { if (!layer.is_visible() || !layer.rect().contains(point)) continue; diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index e6b3ccab9a..7ae7b0c7dd 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -48,7 +48,7 @@ class Image : public RefCounted { public: static ErrorOr> try_create_with_size(Gfx::IntSize); static ErrorOr> try_create_from_pixel_paint_json(JsonObject const&); - static ErrorOr> try_create_from_bitmap(NonnullRefPtr); + static ErrorOr> try_create_from_bitmap(NonnullRefPtr const&); static ErrorOr> try_decode_bitmap(ReadonlyBytes); @@ -73,8 +73,8 @@ public: void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const; ErrorOr serialize_as_json(JsonObjectSerializer& json) const; - ErrorOr export_bmp_to_file(Core::File&, bool preserve_alpha_channel); - ErrorOr export_png_to_file(Core::File&, bool preserve_alpha_channel); + ErrorOr export_bmp_to_file(Core::File&, bool preserve_alpha_channel) const; + ErrorOr export_png_to_file(Core::File&, bool preserve_alpha_channel) const; ErrorOr export_qoi_to_file(Core::File&) const; void move_layer_to_front(Layer&); diff --git a/Userland/Applications/PixelPaint/Layer.cpp b/Userland/Applications/PixelPaint/Layer.cpp index 9e41e7f104..cb318f8f98 100644 --- a/Userland/Applications/PixelPaint/Layer.cpp +++ b/Userland/Applications/PixelPaint/Layer.cpp @@ -83,7 +83,7 @@ void Layer::did_modify_bitmap(Gfx::IntRect const& rect, NotifyClients notify_cli // NOTE: If NotifyClients::NO is passed to this function the caller should handle notifying // the clients of any bitmap changes. - if (notify_clients == NotifyClients::YES) + if (notify_clients == NotifyClients::Yes) m_image.layer_did_modify_bitmap({}, *this, rect); update_cached_bitmap(); } diff --git a/Userland/Applications/PixelPaint/Layer.h b/Userland/Applications/PixelPaint/Layer.h index f9cae667a2..fe8275ed1c 100644 --- a/Userland/Applications/PixelPaint/Layer.h +++ b/Userland/Applications/PixelPaint/Layer.h @@ -56,23 +56,23 @@ public: DeprecatedString const& name() const { return m_name; } void set_name(DeprecatedString); - enum NotifyClients { - YES, - NO + enum class NotifyClients { + Yes, + No }; - ErrorOr flip(Gfx::Orientation orientation, NotifyClients notify_clients = NotifyClients::YES); - ErrorOr rotate(Gfx::RotationDirection direction, NotifyClients notify_clients = NotifyClients::YES); - ErrorOr crop(Gfx::IntRect const& rect, NotifyClients notify_clients = NotifyClients::YES); - ErrorOr resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::YES); - ErrorOr resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::YES); - ErrorOr resize(Gfx::IntSize new_size, Gfx::IntPoint new_location, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::YES); + ErrorOr flip(Gfx::Orientation orientation, NotifyClients notify_clients = NotifyClients::Yes); + ErrorOr rotate(Gfx::RotationDirection direction, NotifyClients notify_clients = NotifyClients::Yes); + ErrorOr crop(Gfx::IntRect const& rect, NotifyClients notify_clients = NotifyClients::Yes); + ErrorOr resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::Yes); + ErrorOr resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::Yes); + ErrorOr resize(Gfx::IntSize new_size, Gfx::IntPoint new_location, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::Yes); Optional nonempty_content_bounding_rect() const; ErrorOr try_set_bitmaps(NonnullRefPtr content, RefPtr mask); - void did_modify_bitmap(Gfx::IntRect const& = {}, NotifyClients notify_clients = NotifyClients::YES); + void did_modify_bitmap(Gfx::IntRect const& = {}, NotifyClients notify_clients = NotifyClients::Yes); void set_selected(bool selected) { m_selected = selected; } bool is_selected() const { return m_selected; }