diff options
author | Marcus Nilsson <brainbomb@gmail.com> | 2021-07-05 22:42:43 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-08 20:16:26 +0200 |
commit | 9be08e33eae4d582695c605d2a076e96e3a783a9 (patch) | |
tree | 9f609791e51017ed0f6f9a611b5af5a844e79959 | |
parent | 76a18e1172a9389fad540fcfdf81252e237da2de (diff) | |
download | serenity-9be08e33eae4d582695c605d2a076e96e3a783a9.zip |
PixelPaint: Add method to flatten image layers
This adds Image::flatten_all_layers() that allows the user to flatten
all the visible layers into one.
-rw-r--r-- | Userland/Applications/PixelPaint/Image.cpp | 18 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Image.h | 1 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/LayerListWidget.cpp | 1 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/main.cpp | 10 |
4 files changed, 30 insertions, 0 deletions
diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index a5435a29e2..ba2131baf6 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -366,6 +366,24 @@ void Image::remove_layer(Layer& layer) did_modify_layer_stack(); } +void Image::flatten_all_layers() +{ + if (m_layers.size() < 2) + return; + + auto& bottom_layer = m_layers.at(0); + + GUI::Painter painter(bottom_layer.bitmap()); + paint_into(painter, { 0, 0, m_size.width(), m_size.height() }); + + for (size_t index = m_layers.size() - 1; index > 0; index--) { + auto& layer = m_layers.at(index); + remove_layer(layer); + } + bottom_layer.set_name("Background"); + select_layer(&bottom_layer); +} + void Image::select_layer(Layer* layer) { for (auto* client : m_clients) diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index e42f9d8259..1268b8d9d0 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -68,6 +68,7 @@ public: void change_layer_index(size_t old_index, size_t new_index); void remove_layer(Layer&); void select_layer(Layer*); + void flatten_all_layers(); void add_client(ImageClient&); void remove_client(ImageClient&); diff --git a/Userland/Applications/PixelPaint/LayerListWidget.cpp b/Userland/Applications/PixelPaint/LayerListWidget.cpp index 7f231ea124..32a93c590c 100644 --- a/Userland/Applications/PixelPaint/LayerListWidget.cpp +++ b/Userland/Applications/PixelPaint/LayerListWidget.cpp @@ -217,6 +217,7 @@ void LayerListWidget::image_did_remove_layer(size_t layer_index) m_moving_gadget_index = {}; } m_gadgets.remove(layer_index); + m_selected_layer_index = 0; relayout_gadgets(); } diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 057d43062b..5c6bb7abbc 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -408,6 +408,16 @@ int main(int argc, char** argv) layer_list_widget.on_context_menu_request = [&](auto& event) { layer_menu.popup(event.screen_position()); }; + layer_menu.add_separator(); + layer_menu.add_action(GUI::Action::create( + "&Flatten Image", { Mod_Ctrl, Key_F }, [&](auto&) { + auto* editor = current_image_editor(); + if (!editor) + return; + editor->image().flatten_all_layers(); + editor->did_complete_action(); + }, + window)); auto& filter_menu = menubar->add_menu("&Filter"); auto& spatial_filters_menu = filter_menu.add_submenu("&Spatial"); |