From 77bd3f75ce0088ee134be9dc29e5dcc970d9214c Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 18 May 2021 03:16:56 +0200 Subject: Mandelbrot: Add support for exporting the current image Unfortunately this means unveil() won't work - at least until we get something like FilePickerServer. --- Userland/Demos/Mandelbrot/Mandelbrot.cpp | 33 ++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'Userland/Demos/Mandelbrot') diff --git a/Userland/Demos/Mandelbrot/Mandelbrot.cpp b/Userland/Demos/Mandelbrot/Mandelbrot.cpp index fb9b068bbb..5d8e8eb189 100644 --- a/Userland/Demos/Mandelbrot/Mandelbrot.cpp +++ b/Userland/Demos/Mandelbrot/Mandelbrot.cpp @@ -7,13 +7,16 @@ #include #include +#include #include #include #include +#include #include #include #include #include +#include #include class MandelbrotSet { @@ -126,6 +129,9 @@ private: class Mandelbrot : public GUI::Widget { C_OBJECT(Mandelbrot) + + void export_image(String const& export_path); + private: virtual void paint_event(GUI::PaintEvent&) override; virtual void mousedown_event(GUI::MouseEvent& event) override; @@ -203,15 +209,29 @@ void Mandelbrot::resize_event(GUI::ResizeEvent& event) m_set.resize(event.size()); } +void Mandelbrot::export_image(String const& export_path) +{ + auto png = Gfx::PNGWriter::encode(m_set.bitmap()); + auto file = fopen(export_path.characters(), "wb"); + if (!file) { + GUI::MessageBox::show(window(), String::formatted("Could not open '{}' for writing.", export_path), "Mandelbrot", GUI::MessageBox::Type::Error); + return; + } + fwrite(png.data(), 1, png.size(), file); + fclose(file); + GUI::MessageBox::show(window(), "Image was successfully exported.", "Mandelbrot", GUI::MessageBox::Type::Information); +} + int main(int argc, char** argv) { auto app = GUI::Application::construct(argc, argv); - if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) { + if (pledge("stdio thread recvfd sendfd rpath wpath cpath", nullptr) < 0) { perror("pledge"); return 1; } +#if 0 if (unveil("/res", "r") < 0) { perror("unveil"); return 1; @@ -221,18 +241,27 @@ int main(int argc, char** argv) perror("unveil"); return 1; } +#endif auto window = GUI::Window::construct(); window->set_double_buffering_enabled(false); window->set_title("Mandelbrot"); window->set_minimum_size(320, 240); window->resize(window->minimum_size() * 2); + auto& mandelbrot = window->set_main_widget(); auto menubar = GUI::Menubar::construct(); auto& file_menu = menubar->add_menu("&File"); + file_menu.add_action(GUI::Action::create("&Export...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), + [&](GUI::Action&) { + Optional export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "png"); + if (!export_path.has_value()) + return; + mandelbrot.export_image(export_path.value()); + })); + file_menu.add_separator(); file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })); window->set_menubar(move(menubar)); - window->set_main_widget(); window->show(); auto app_icon = GUI::Icon::default_icon("app-mandelbrot"); -- cgit v1.2.3