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.
This commit is contained in:
Andreas Kling 2019-12-19 20:39:11 +01:00
parent f276030969
commit ea91f24a49
Notes: sideshowbarker 2024-07-19 10:48:10 +09:00
3 changed files with 51 additions and 5 deletions

View file

@ -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());
}
}

View file

@ -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;
};

View file

@ -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();
}