mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
Mandelbrot: Make it possible for export_image to return an error
...and if we do get an error, present it to the user.
This commit is contained in:
parent
2fc2025f49
commit
95b4ffad39
1 changed files with 10 additions and 6 deletions
|
@ -217,7 +217,7 @@ enum class ImageType {
|
||||||
class Mandelbrot : public GUI::Frame {
|
class Mandelbrot : public GUI::Frame {
|
||||||
C_OBJECT(Mandelbrot)
|
C_OBJECT(Mandelbrot)
|
||||||
|
|
||||||
void export_image(DeprecatedString const& export_path, ImageType image_type);
|
ErrorOr<void> export_image(DeprecatedString const& export_path, ImageType image_type);
|
||||||
|
|
||||||
enum class Zoom {
|
enum class Zoom {
|
||||||
In,
|
In,
|
||||||
|
@ -366,7 +366,7 @@ void Mandelbrot::resize_event(GUI::ResizeEvent& event)
|
||||||
m_set.resize(event.size());
|
m_set.resize(event.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mandelbrot::export_image(DeprecatedString const& export_path, ImageType image_type)
|
ErrorOr<void> Mandelbrot::export_image(DeprecatedString const& export_path, ImageType image_type)
|
||||||
{
|
{
|
||||||
m_set.resize(Gfx::IntSize { 1920, 1080 });
|
m_set.resize(Gfx::IntSize { 1920, 1080 });
|
||||||
ByteBuffer encoded_data;
|
ByteBuffer encoded_data;
|
||||||
|
@ -389,10 +389,11 @@ void Mandelbrot::export_image(DeprecatedString const& export_path, ImageType ima
|
||||||
auto file = fopen(export_path.characters(), "wb");
|
auto file = fopen(export_path.characters(), "wb");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
GUI::MessageBox::show(window(), DeprecatedString::formatted("Could not open '{}' for writing.", export_path), "Mandelbrot"sv, GUI::MessageBox::Type::Error);
|
GUI::MessageBox::show(window(), DeprecatedString::formatted("Could not open '{}' for writing.", export_path), "Mandelbrot"sv, GUI::MessageBox::Type::Error);
|
||||||
return;
|
return {};
|
||||||
}
|
}
|
||||||
fwrite(encoded_data.data(), 1, encoded_data.size(), file);
|
fwrite(encoded_data.data(), 1, encoded_data.size(), file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
@ -423,21 +424,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
Optional<DeprecatedString> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "bmp");
|
Optional<DeprecatedString> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "bmp");
|
||||||
if (!export_path.has_value())
|
if (!export_path.has_value())
|
||||||
return;
|
return;
|
||||||
mandelbrot->export_image(export_path.value(), ImageType::BMP);
|
if (auto result = mandelbrot->export_image(export_path.value(), ImageType::BMP); result.is_error())
|
||||||
|
GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error()));
|
||||||
})));
|
})));
|
||||||
TRY(export_submenu.try_add_action(GUI::Action::create("As &PNG", { Mod_Ctrl | Mod_Shift, Key_S },
|
TRY(export_submenu.try_add_action(GUI::Action::create("As &PNG", { Mod_Ctrl | Mod_Shift, Key_S },
|
||||||
[&](GUI::Action&) {
|
[&](GUI::Action&) {
|
||||||
Optional<DeprecatedString> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "png");
|
Optional<DeprecatedString> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "png");
|
||||||
if (!export_path.has_value())
|
if (!export_path.has_value())
|
||||||
return;
|
return;
|
||||||
mandelbrot->export_image(export_path.value(), ImageType::PNG);
|
if (auto result = mandelbrot->export_image(export_path.value(), ImageType::PNG); result.is_error())
|
||||||
|
GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error()));
|
||||||
})));
|
})));
|
||||||
TRY(export_submenu.try_add_action(GUI::Action::create("As &QOI",
|
TRY(export_submenu.try_add_action(GUI::Action::create("As &QOI",
|
||||||
[&](GUI::Action&) {
|
[&](GUI::Action&) {
|
||||||
Optional<DeprecatedString> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "qoi");
|
Optional<DeprecatedString> export_path = GUI::FilePicker::get_save_filepath(window, "untitled", "qoi");
|
||||||
if (!export_path.has_value())
|
if (!export_path.has_value())
|
||||||
return;
|
return;
|
||||||
mandelbrot->export_image(export_path.value(), ImageType::QOI);
|
if (auto result = mandelbrot->export_image(export_path.value(), ImageType::QOI); result.is_error())
|
||||||
|
GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error()));
|
||||||
})));
|
})));
|
||||||
|
|
||||||
export_submenu.set_icon(TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/save.png"sv)));
|
export_submenu.set_icon(TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/save.png"sv)));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue