summaryrefslogtreecommitdiff
path: root/Userland/Demos/Mandelbrot
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-18 03:16:56 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-18 16:01:32 +0200
commit77bd3f75ce0088ee134be9dc29e5dcc970d9214c (patch)
treea8868952ea51b9a854c88ca76aa5c6c9946da1a4 /Userland/Demos/Mandelbrot
parentb429ee88f8af5f3434fc1603b88c1bfd97cec6fd (diff)
downloadserenity-77bd3f75ce0088ee134be9dc29e5dcc970d9214c.zip
Mandelbrot: Add support for exporting the current image
Unfortunately this means unveil() won't work - at least until we get something like FilePickerServer.
Diffstat (limited to 'Userland/Demos/Mandelbrot')
-rw-r--r--Userland/Demos/Mandelbrot/Mandelbrot.cpp33
1 files changed, 31 insertions, 2 deletions
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 <LibGUI/Action.h>
#include <LibGUI/Application.h>
+#include <LibGUI/FilePicker.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
+#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
+#include <LibGfx/PNGWriter.h>
#include <unistd.h>
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<Mandelbrot>();
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<String> 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<Mandelbrot>();
window->show();
auto app_icon = GUI::Icon::default_icon("app-mandelbrot");