summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorMustafa Quraish <mustafaq9@gmail.com>2021-09-02 21:09:04 -0400
committerAndreas Kling <kling@serenityos.org>2021-09-06 23:43:49 +0200
commit905bc7938742350c4d8091268515912bcd6dfaa5 (patch)
tree41d28f6526f48b007544105a908446f358ac1639 /Userland/Applications
parent456938add04d200a1e025d2f4542f2dc5244641e (diff)
downloadserenity-905bc7938742350c4d8091268515912bcd6dfaa5.zip
PixelPaint: Add `Crop to Selection` Action
In addition to adding the action, this commit also makes the `did_change_rect()` method take in an optional rect, which represents the new rect position. By default it is the same as `rect()`. When we are cropping an image, we don't want to move the whole cropped section to the top-left of the original image in the ImageEditor widget, so this allows us to keep the cropped image's position fixed.
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/PixelPaint/Image.cpp18
-rw-r--r--Userland/Applications/PixelPaint/Image.h3
-rw-r--r--Userland/Applications/PixelPaint/MainWidget.cpp11
3 files changed, 29 insertions, 3 deletions
diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp
index f5d9c18bfe..191dd512af 100644
--- a/Userland/Applications/PixelPaint/Image.cpp
+++ b/Userland/Applications/PixelPaint/Image.cpp
@@ -458,10 +458,11 @@ void Image::did_change(Gfx::IntRect const& a_modified_rect)
client->image_did_change(modified_rect);
}
-void Image::did_change_rect()
+void Image::did_change_rect(Gfx::IntRect const& a_modified_rect)
{
+ auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
for (auto* client : m_clients)
- client->image_did_change_rect(rect());
+ client->image_did_change_rect(modified_rect);
}
ImageUndoCommand::ImageUndoCommand(Image& image)
@@ -518,4 +519,17 @@ void Image::rotate(Gfx::RotationDirection direction)
did_change_rect();
}
+void Image::crop(Gfx::IntRect const& cropped_rect)
+{
+ for (auto& layer : m_layers) {
+ auto cropped = layer.bitmap().cropped(cropped_rect);
+ VERIFY(cropped);
+ layer.set_bitmap(*cropped);
+ layer.did_modify_bitmap(rect());
+ }
+
+ m_size = { cropped_rect.width(), cropped_rect.height() };
+ did_change_rect(cropped_rect);
+}
+
}
diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h
index 7f8422b806..daa508075b 100644
--- a/Userland/Applications/PixelPaint/Image.h
+++ b/Userland/Applications/PixelPaint/Image.h
@@ -101,12 +101,13 @@ public:
void flip(Gfx::Orientation orientation);
void rotate(Gfx::RotationDirection direction);
+ void crop(Gfx::IntRect const& rect);
private:
explicit Image(Gfx::IntSize const&);
void did_change(Gfx::IntRect const& modified_rect = {});
- void did_change_rect();
+ void did_change_rect(Gfx::IntRect const& modified_rect = {});
void did_modify_layer_stack();
String m_path;
diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp
index 51e5806c75..0434cf3fe8 100644
--- a/Userland/Applications/PixelPaint/MainWidget.cpp
+++ b/Userland/Applications/PixelPaint/MainWidget.cpp
@@ -375,6 +375,17 @@ void MainWidget::initialize_menubar(GUI::Window& window)
return;
editor->image().rotate(Gfx::RotationDirection::Clockwise);
}));
+ image_menu.add_separator();
+ image_menu.add_action(GUI::Action::create(
+ "&Crop To Selection", [&](auto&) {
+ auto* editor = current_image_editor();
+ // FIXME: disable this action if there is no selection
+ if (!editor || editor->selection().is_empty())
+ return;
+ auto crop_rect = editor->selection().bounding_rect();
+ editor->image().crop(crop_rect);
+ editor->selection().clear();
+ }));
auto& layer_menu = window.add_menu("&Layer");
layer_menu.add_action(GUI::Action::create(