diff options
author | Andreas Kling <kling@serenityos.org> | 2021-06-14 17:59:15 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-14 18:25:17 +0200 |
commit | 765286f691154075989573356ef838e0d3c0c70c (patch) | |
tree | b68b082f30f37713a6f113022a44a72c596db7f8 /Userland/Applications | |
parent | 4cecd7900036554c7d153ca8b876bbc49b3bd31f (diff) | |
download | serenity-765286f691154075989573356ef838e0d3c0c70c.zip |
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 :^)
Diffstat (limited to 'Userland/Applications')
-rw-r--r-- | Userland/Applications/PixelPaint/Layer.cpp | 8 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Layer.h | 3 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Selection.h | 1 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/main.cpp | 17 |
4 files changed, 29 insertions, 0 deletions
diff --git a/Userland/Applications/PixelPaint/Layer.cpp b/Userland/Applications/PixelPaint/Layer.cpp index c2c47c226d..a64324bb45 100644 --- a/Userland/Applications/PixelPaint/Layer.cpp +++ b/Userland/Applications/PixelPaint/Layer.cpp @@ -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); +} + } diff --git a/Userland/Applications/PixelPaint/Layer.h b/Userland/Applications/PixelPaint/Layer.h index c6b52e252e..3aa1a0056f 100644 --- a/Userland/Applications/PixelPaint/Layer.h +++ b/Userland/Applications/PixelPaint/Layer.h @@ -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); diff --git a/Userland/Applications/PixelPaint/Selection.h b/Userland/Applications/PixelPaint/Selection.h index 1322a14563..9c73f4f29a 100644 --- a/Userland/Applications/PixelPaint/Selection.h +++ b/Userland/Applications/PixelPaint/Selection.h @@ -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&); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 1006697e8e..18a0d9aa6a 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -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); |