1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:27:45 +00:00

PixelPaint: Open image file passed as argument

This is obviously a requirement for #5374, oops :^)

Also handle errors gracefully, opening a file that isn't PixelPaint JSON
would previously crash.

Closes #5388.
This commit is contained in:
Linus Groh 2021-02-17 19:15:28 +01:00 committed by Andreas Kling
parent c4438d6fd4
commit fe266e03b6

View file

@ -36,6 +36,8 @@
#include "Tool.h" #include "Tool.h"
#include "ToolPropertiesWidget.h" #include "ToolPropertiesWidget.h"
#include "ToolboxWidget.h" #include "ToolboxWidget.h"
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
@ -65,6 +67,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
const char* image_file = nullptr;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(image_file, "PixelPaint image file (*.pp) to open", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
auto app_icon = GUI::Icon::default_icon("app-pixel-paint"); auto app_icon = GUI::Icon::default_icon("app-pixel-paint");
auto window = GUI::Window::construct(); auto window = GUI::Window::construct();
@ -93,6 +100,9 @@ int main(int argc, char** argv)
right_panel.set_layout<GUI::VerticalBoxLayout>(); right_panel.set_layout<GUI::VerticalBoxLayout>();
auto& layer_list_widget = right_panel.add<PixelPaint::LayerListWidget>(); auto& layer_list_widget = right_panel.add<PixelPaint::LayerListWidget>();
layer_list_widget.on_layer_select = [&](auto* layer) {
image_editor.set_active_layer(layer);
};
auto& layer_properties_widget = right_panel.add<PixelPaint::LayerPropertiesWidget>(); auto& layer_properties_widget = right_panel.add<PixelPaint::LayerPropertiesWidget>();
@ -108,6 +118,16 @@ int main(int argc, char** argv)
auto menubar = GUI::MenuBar::construct(); auto menubar = GUI::MenuBar::construct();
auto& app_menu = menubar->add_menu("PixelPaint"); auto& app_menu = menubar->add_menu("PixelPaint");
auto open_image_file = [&](auto& path) {
auto image = PixelPaint::Image::create_from_file(path);
if (!image) {
GUI::MessageBox::show_error(window, String::formatted("Invalid image file: {}", path));
return;
}
image_editor.set_image(image);
layer_list_widget.set_image(image);
};
app_menu.add_action( app_menu.add_action(
GUI::Action::create( GUI::Action::create(
"New", [&](auto&) { "New", [&](auto&) {
@ -130,9 +150,7 @@ int main(int argc, char** argv)
if (!open_path.has_value()) if (!open_path.has_value())
return; return;
auto image = PixelPaint::Image::create_from_file(open_path.value()); open_image_file(open_path.value());
image_editor.set_image(image);
layer_list_widget.set_image(image);
})); }));
app_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) { app_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
if (!image_editor.image()) if (!image_editor.image())
@ -372,6 +390,10 @@ int main(int argc, char** argv)
layer_properties_widget.set_layer(layer); layer_properties_widget.set_layer(layer);
}; };
auto image_file_real_path = Core::File::real_path_for(image_file);
if (Core::File::exists(image_file_real_path)) {
open_image_file(image_file_real_path);
} else {
auto image = PixelPaint::Image::create_with_size({ 640, 480 }); auto image = PixelPaint::Image::create_with_size({ 640, 480 });
auto bg_layer = PixelPaint::Layer::create_with_size(*image, { 640, 480 }, "Background"); auto bg_layer = PixelPaint::Layer::create_with_size(*image, { 640, 480 }, "Background");
@ -388,14 +410,11 @@ int main(int argc, char** argv)
image->add_layer(*fg_layer2); image->add_layer(*fg_layer2);
fg_layer2->bitmap().fill(Color::Blue); fg_layer2->bitmap().fill(Color::Blue);
layer_list_widget.on_layer_select = [&](auto* layer) {
image_editor.set_active_layer(layer);
};
layer_list_widget.set_image(image); layer_list_widget.set_image(image);
image_editor.set_image(image); image_editor.set_image(image);
image_editor.set_active_layer(bg_layer); image_editor.set_active_layer(bg_layer);
}
return app->exec(); return app->exec();
} }