diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-12-19 20:39:11 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-12-20 20:07:10 +0100 |
commit | ea91f24a498f9cd8fc5d56b08bedd3512ab5fce8 (patch) | |
tree | 065f24604ea6116271ddd1ade659c51689a533e1 /Applications | |
parent | f276030969b3ef3fb2685c095f31f1530f8945a6 (diff) | |
download | serenity-ea91f24a498f9cd8fc5d56b08bedd3512ab5fce8.zip |
QuickShow: Allow dropping an image file on the QuickShow window
We now try to open image files dropped on us from FileManager. :^)
The QuickShow code is not exactly well-factored, and should be fixes up
to not do the same thing twice, etc.
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/QuickShow/QSWidget.cpp | 40 | ||||
-rw-r--r-- | Applications/QuickShow/QSWidget.h | 6 | ||||
-rw-r--r-- | Applications/QuickShow/main.cpp | 10 |
3 files changed, 51 insertions, 5 deletions
diff --git a/Applications/QuickShow/QSWidget.cpp b/Applications/QuickShow/QSWidget.cpp index c3d32da202..6e51750c1e 100644 --- a/Applications/QuickShow/QSWidget.cpp +++ b/Applications/QuickShow/QSWidget.cpp @@ -1,6 +1,9 @@ #include "QSWidget.h" -#include <LibGUI/GPainter.h> +#include <AK/URL.h> #include <LibDraw/GraphicsBitmap.h> +#include <LibGUI/GMessageBox.h> +#include <LibGUI/GPainter.h> +#include <LibGUI/GWindow.h> QSWidget::QSWidget(GWidget* parent) : GFrame(parent) @@ -90,3 +93,38 @@ void QSWidget::mousewheel_event(GMouseEvent& event) on_scale_change(m_scale); } } + +void QSWidget::set_path(const String& path) +{ + m_path = path; +} + +void QSWidget::drop_event(GDropEvent& event) +{ + event.accept(); + window()->move_to_front(); + + if (event.data_type() == "url-list") { + auto lines = event.data().split_view('\n'); + if (lines.is_empty()) + return; + if (lines.size() > 1) { + GMessageBox::show("QuickShow can only open one file at a time!", "One at a time please!", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); + return; + } + URL url(lines[0]); + auto bitmap = GraphicsBitmap::load_from_file(url.path()); + if (!bitmap) { + GMessageBox::show(String::format("Failed to open %s", url.to_string().characters()), "Cannot open image", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); + return; + } + + m_path = url.path(); + m_bitmap = bitmap; + m_scale = 100; + if (on_scale_change) + on_scale_change(m_scale); + relayout(); + m_bitmap_rect.center_within(rect()); + } +} diff --git a/Applications/QuickShow/QSWidget.h b/Applications/QuickShow/QSWidget.h index aca9151552..195ff60434 100644 --- a/Applications/QuickShow/QSWidget.h +++ b/Applications/QuickShow/QSWidget.h @@ -11,6 +11,10 @@ public: virtual ~QSWidget() override; void set_bitmap(NonnullRefPtr<GraphicsBitmap>); + const GraphicsBitmap* bitmap() const { return m_bitmap.ptr(); } + + void set_path(const String&); + const String& path() const { return m_path; } Function<void(int)> on_scale_change; @@ -22,6 +26,7 @@ private: virtual void mouseup_event(GMouseEvent&) override; virtual void mousemove_event(GMouseEvent&) override; virtual void mousewheel_event(GMouseEvent&) override; + virtual void drop_event(GDropEvent&) override; void relayout(); @@ -30,4 +35,5 @@ private: int m_scale { 100 }; Point m_pan_origin; Point m_pan_bitmap_origin; + String m_path; }; diff --git a/Applications/QuickShow/main.cpp b/Applications/QuickShow/main.cpp index 73bec79aac..e43c8ea5e0 100644 --- a/Applications/QuickShow/main.cpp +++ b/Applications/QuickShow/main.cpp @@ -44,30 +44,32 @@ int main(int argc, char** argv) if (argc > 1) path = argv[1]; - auto bitmap = load_png(path); + auto bitmap = GraphicsBitmap::load_from_file(path); if (!bitmap) { fprintf(stderr, "Failed to load %s\n", path); return 1; } auto window = GWindow::construct(); + auto widget = QSWidget::construct(); + widget->set_path(path); + widget->set_bitmap(*bitmap); auto update_window_title = [&](int scale) { - window->set_title(String::format("QuickShow: %s %s %d%%", path, bitmap->size().to_string().characters(), scale)); + window->set_title(String::format("QuickShow: %s %s %d%%", widget->path().characters(), widget->bitmap()->size().to_string().characters(), scale)); }; window->set_double_buffering_enabled(true); update_window_title(100); window->set_rect(200, 200, bitmap->width(), bitmap->height()); - auto widget = QSWidget::construct(); widget->on_scale_change = [&](int scale) { update_window_title(scale); }; - widget->set_bitmap(*bitmap); window->set_main_widget(widget); window->show(); + bitmap = nullptr; return app.exec(); } |