PixelPaint: Add copy action (copies the selection from active layer)

You can now select a part of a layer, copy it, and then paste it as
a new layer. Very cool :^)
This commit is contained in:
Andreas Kling 2021-06-14 17:59:15 +02:00
parent 4cecd79000
commit 765286f691
Notes: sideshowbarker 2024-07-18 12:14:37 +09:00
4 changed files with 29 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#include "Layer.h"
#include "Image.h"
#include "Selection.h"
#include <LibGfx/Bitmap.h>
namespace PixelPaint {
@ -89,4 +90,11 @@ void Layer::set_name(String name)
m_image.layer_did_modify_properties({}, *this);
}
RefPtr<Gfx::Bitmap> Layer::try_copy_bitmap(Selection const& selection) const
{
auto bounding_rect = selection.bounding_rect().translated(-m_location);
// FIXME: This needs to be smarter once we add more complex selections.
return m_bitmap->cropped(bounding_rect);
}
}

View file

@ -15,6 +15,7 @@
namespace PixelPaint {
class Image;
class Selection;
class Layer
: public RefCounted<Layer>
@ -56,6 +57,8 @@ public:
int opacity_percent() const { return m_opacity_percent; }
void set_opacity_percent(int);
RefPtr<Gfx::Bitmap> try_copy_bitmap(Selection const&) const;
private:
Layer(Image&, NonnullRefPtr<Gfx::Bitmap>, String name);

View file

@ -21,6 +21,7 @@ public:
bool is_empty() const { return m_rect.is_empty(); }
void clear() { m_rect = {}; }
void set(Gfx::IntRect const& rect) { m_rect = rect; }
Gfx::IntRect bounding_rect() const { return m_rect; }
void paint(Gfx::Painter&, ImageEditor const&);

View file

@ -160,6 +160,21 @@ int main(int argc, char** argv)
}));
auto& edit_menu = menubar->add_menu("&Edit");
auto copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
VERIFY(image_editor.image());
if (!image_editor.active_layer()) {
dbgln("Cannot copy with no active layer selected");
return;
}
auto bitmap = image_editor.active_layer()->try_copy_bitmap(image_editor.selection());
if (!bitmap) {
dbgln("try_copy() from Layer failed");
return;
}
GUI::Clipboard::the().set_bitmap(*bitmap);
});
auto paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
VERIFY(image_editor.image());
auto bitmap = GUI::Clipboard::the().bitmap();
@ -175,6 +190,7 @@ int main(int argc, char** argv)
};
paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "image/x-serenityos");
edit_menu.add_action(copy_action);
edit_menu.add_action(paste_action);
auto undo_action = GUI::CommonActions::make_undo_action([&](auto&) {
@ -375,6 +391,7 @@ int main(int argc, char** argv)
toolbar.add_action(open_image_action);
toolbar.add_action(save_image_as_action);
toolbar.add_separator();
toolbar.add_action(copy_action);
toolbar.add_action(paste_action);
toolbar.add_action(undo_action);
toolbar.add_action(redo_action);