diff options
author | Hüseyin ASLITÜRK <asliturk@hotmail.com> | 2020-04-12 16:40:05 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-12 18:08:11 +0200 |
commit | 6c1af174a1cb2befb919b95ce73a399b012cef4e (patch) | |
tree | b92c5bc95b98826633884f8e2dff252ff35a6507 | |
parent | f88ceb872a53632c5411e95b8ede35a63dbc98a4 (diff) | |
download | serenity-6c1af174a1cb2befb919b95ce73a399b012cef4e.zip |
QuickShow: Add Delete action
Delete current file from file system.
-rw-r--r-- | Applications/QuickShow/QSWidget.cpp | 9 | ||||
-rw-r--r-- | Applications/QuickShow/QSWidget.h | 1 | ||||
-rw-r--r-- | Applications/QuickShow/main.cpp | 63 |
3 files changed, 62 insertions, 11 deletions
diff --git a/Applications/QuickShow/QSWidget.cpp b/Applications/QuickShow/QSWidget.cpp index abf6c52b9b..a58c2f4568 100644 --- a/Applications/QuickShow/QSWidget.cpp +++ b/Applications/QuickShow/QSWidget.cpp @@ -42,6 +42,15 @@ QSWidget::~QSWidget() { } +void QSWidget::clear() +{ + m_bitmap = nullptr; + m_path = {}; + + on_scale_change(100); + update(); +} + void QSWidget::flip(Gfx::Orientation orientation) { m_bitmap = m_bitmap->flipped(orientation); diff --git a/Applications/QuickShow/QSWidget.h b/Applications/QuickShow/QSWidget.h index 65d6917b7d..0253d9a8e6 100644 --- a/Applications/QuickShow/QSWidget.h +++ b/Applications/QuickShow/QSWidget.h @@ -49,6 +49,7 @@ public: void set_scale(int); int scale() { return m_scale; } + void clear(); void flip(Gfx::Orientation); void rotate(Gfx::RotationDirection); void navigate(Directions); diff --git a/Applications/QuickShow/main.cpp b/Applications/QuickShow/main.cpp index 000636cd5a..5bb4fe0c59 100644 --- a/Applications/QuickShow/main.cpp +++ b/Applications/QuickShow/main.cpp @@ -36,20 +36,22 @@ #include <LibGUI/Label.h> #include <LibGUI/Menu.h> #include <LibGUI/MenuBar.h> +#include <LibGUI/MessageBox.h> #include <LibGUI/Window.h> #include <LibGfx/Bitmap.h> #include <stdio.h> +#include <string.h> int main(int argc, char** argv) { - if (pledge("stdio shared_buffer accept rpath unix cpath fattr proc exec thread", nullptr) < 0) { + if (pledge("stdio shared_buffer accept cpath rpath unix cpath fattr proc exec thread", nullptr) < 0) { perror("pledge"); return 1; } GUI::Application app(argc, argv); - if (pledge("stdio shared_buffer accept rpath proc exec thread", nullptr) < 0) { + if (pledge("stdio shared_buffer accept cpath rpath proc exec thread", nullptr) < 0) { perror("pledge"); return 1; } @@ -100,6 +102,51 @@ int main(int argc, char** argv) }; // Actions + auto open_action = GUI::CommonActions::make_open_action( + [&](auto&) { + Optional<String> path = GUI::FilePicker::get_open_filepath("Open image..."); + if (path.has_value()) { + widget.load_from_file(path.value()); + } + }); + + auto delete_action = GUI::CommonActions::make_delete_action( + [&](auto&) { + auto path = widget.path(); + if(path.is_empty()) + return; + + auto msgbox_result = GUI::MessageBox::show(String::format("Really delete %s?", path.characters()), + "Confirm deletion", + GUI::MessageBox::Type::Warning, + GUI::MessageBox::InputType::OKCancel, + window); + + if (msgbox_result == GUI::MessageBox::ExecCancel) + return; + + auto unlink_result = unlink(widget.path().characters()); + dbg() << "unlink_result::" << unlink_result; + + if (unlink_result < 0) { + int saved_errno = errno; + GUI::MessageBox::show(String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)), + "Delete failed", + GUI::MessageBox::Type::Error, + GUI::MessageBox::InputType::OK, + window); + + return; + } + + widget.clear(); + }); + + auto quit_action = GUI::CommonActions::make_quit_action( + [&](auto&) { + app.quit(); + }); + auto rotate_left_action = GUI::Action::create("Rotate Left", { Mod_None, Key_L }, [&](auto&) { widget.rotate(Gfx::RotationDirection::Left); @@ -163,16 +210,10 @@ int main(int argc, char** argv) auto menubar = make<GUI::MenuBar>(); auto& app_menu = menubar->add_menu("QuickShow"); - app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { - Optional<String> path = GUI::FilePicker::get_open_filepath("Open image..."); - if (path.has_value()) { - widget.load_from_file(path.value()); - } - })); + app_menu.add_action(open_action); + app_menu.add_action(delete_action); app_menu.add_separator(); - app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { - app.quit(); - })); + app_menu.add_action(quit_action); auto& image_menu = menubar->add_menu("Image"); image_menu.add_action(rotate_left_action); |