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

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.
This commit is contained in:
Mustafa Quraish 2021-09-02 21:09:04 -04:00 committed by Andreas Kling
parent 456938add0
commit 905bc79387
3 changed files with 29 additions and 3 deletions

View file

@ -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);
}
}