summaryrefslogtreecommitdiff
path: root/Applications
diff options
context:
space:
mode:
authorTibor Nagy <xnagytibor@gmail.com>2020-04-05 20:25:14 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-06 09:04:15 +0200
commit63b11e094db38fc433e6a105c3f559b0c69bef7d (patch)
tree5b043a11b375e920129447efe44456e647fd361f /Applications
parent795067e08c92e4be2565bc22f2980e25fb412d12 (diff)
downloadserenity-63b11e094db38fc433e6a105c3f559b0c69bef7d.zip
QuickShow: Miscellaneous improvements
Major changes are: The layout and mouse handling has been rewritten to always center images in the window. QuickShow now accepts multiple images on drag and drop. The first image gets opened in the current window, further images are opened in new processes. QSWidget now loads images on its own with QSWidget::load_from_file(). An on_drop callback has been introduced for QSWidget. Added an open menu. Added an about box and placeholder icons to the application. QuickShow now starts without loading the sunset-retro wallpaper.
Diffstat (limited to 'Applications')
-rw-r--r--Applications/QuickShow/QSWidget.cpp99
-rw-r--r--Applications/QuickShow/QSWidget.h16
-rw-r--r--Applications/QuickShow/main.cpp92
3 files changed, 117 insertions, 90 deletions
diff --git a/Applications/QuickShow/QSWidget.cpp b/Applications/QuickShow/QSWidget.cpp
index 4c4b994ae8..520d016d6e 100644
--- a/Applications/QuickShow/QSWidget.cpp
+++ b/Applications/QuickShow/QSWidget.cpp
@@ -25,8 +25,6 @@
*/
#include "QSWidget.h"
-#include <AK/URL.h>
-#include <LibCore/MimeData.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
@@ -42,21 +40,23 @@ QSWidget::~QSWidget()
{
}
-void QSWidget::set_bitmap(NonnullRefPtr<Gfx::Bitmap> bitmap)
-{
- m_bitmap = move(bitmap);
-}
-
void QSWidget::relayout()
{
if (m_bitmap.is_null())
return;
- Gfx::Size new_size;
float scale_factor = (float)m_scale / 100.0f;
+
+ Gfx::Size new_size;
new_size.set_width(m_bitmap->width() * scale_factor);
new_size.set_height(m_bitmap->height() * scale_factor);
m_bitmap_rect.set_size(new_size);
+
+ Gfx::Point new_location;
+ new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x() * scale_factor));
+ new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y() * scale_factor));
+ m_bitmap_rect.set_location(new_location);
+
update();
}
@@ -68,22 +68,21 @@ void QSWidget::resize_event(GUI::ResizeEvent& event)
void QSWidget::paint_event(GUI::PaintEvent& event)
{
- if (m_bitmap.is_null())
- return;
-
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
painter.fill_rect_with_checkerboard(rect(), { 8, 8 }, palette().base().darkened(0.9), palette().base());
- painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
+
+ if (!m_bitmap.is_null())
+ painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
}
void QSWidget::mousedown_event(GUI::MouseEvent& event)
{
if (event.button() != GUI::MouseButton::Left)
return;
- m_pan_origin = event.position();
- m_pan_bitmap_origin = m_bitmap_rect.location();
+ m_click_position = event.position();
+ m_saved_pan_origin = m_pan_origin;
}
void QSWidget::mouseup_event(GUI::MouseEvent& event)
@@ -96,64 +95,66 @@ void QSWidget::mousemove_event(GUI::MouseEvent& event)
if (!(event.buttons() & GUI::MouseButton::Left))
return;
- auto delta = event.position() - m_pan_origin;
- m_bitmap_rect.set_location(m_pan_bitmap_origin.translated(delta));
- update();
+ auto delta = event.position() - m_click_position;
+ float scale_factor = (float)m_scale / 100.0f;
+ m_pan_origin = m_saved_pan_origin.translated(
+ -delta.x() / scale_factor,
+ -delta.y() / scale_factor);
+
+ relayout();
}
void QSWidget::mousewheel_event(GUI::MouseEvent& event)
{
auto old_scale = m_scale;
auto old_scale_factor = (float)m_scale / 100.0f;
- auto zoom_point = event.position().translated(-m_bitmap_rect.location());
- zoom_point.set_x((float)zoom_point.x() / old_scale_factor);
- zoom_point.set_y((float)zoom_point.y() / old_scale_factor);
+
m_scale += -event.wheel_delta() * 10;
if (m_scale < 10)
m_scale = 10;
if (m_scale > 1000)
m_scale = 1000;
- relayout();
+
auto new_scale_factor = (float)m_scale / 100.0f;
- auto scale_factor_change = new_scale_factor - old_scale_factor;
- m_bitmap_rect.move_by(-Gfx::Point((float)zoom_point.x() * scale_factor_change, (float)zoom_point.y() * scale_factor_change));
+
+ auto focus_point = Gfx::FloatPoint(
+ m_pan_origin.x() - ((float)event.x() - (float)width() / 2.0) / old_scale_factor,
+ m_pan_origin.y() - ((float)event.y() - (float)height() / 2.0) / old_scale_factor);
+
+ m_pan_origin = Gfx::FloatPoint(
+ focus_point.x() - new_scale_factor / old_scale_factor * (focus_point.x() - m_pan_origin.x()),
+ focus_point.y() - new_scale_factor / old_scale_factor * (focus_point.y() - m_pan_origin.y()));
+
+ relayout();
+
if (old_scale != m_scale) {
if (on_scale_change)
on_scale_change(m_scale);
}
}
-void QSWidget::set_path(const String& path)
+void QSWidget::load_from_file(const String& path)
{
+ auto bitmap = Gfx::Bitmap::load_from_file(path);
+ if (!bitmap) {
+ GUI::MessageBox::show(String::format("Failed to open %s", path.characters()), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
+ return;
+ }
+
+ window()->resize(bitmap->size());
+
m_path = path;
+ m_bitmap = bitmap;
+ m_scale = 100;
+ m_pan_origin = { 0, 0 };
+ if (on_scale_change)
+ on_scale_change(m_scale);
+ relayout();
}
void QSWidget::drop_event(GUI::DropEvent& event)
{
event.accept();
- window()->move_to_front();
-
- if (event.mime_data().has_urls()) {
- auto urls = event.mime_data().urls();
- if (urls.is_empty())
- return;
- if (urls.size() > 1) {
- GUI::MessageBox::show("QuickShow can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
- return;
- }
- auto url = urls.first();
- auto bitmap = Gfx::Bitmap::load_from_file(url.path());
- if (!bitmap) {
- GUI::MessageBox::show(String::format("Failed to open %s", url.to_string().characters()), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::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());
- }
+ if (on_drop)
+ on_drop(event);
}
diff --git a/Applications/QuickShow/QSWidget.h b/Applications/QuickShow/QSWidget.h
index 46bf310fcc..79b1940720 100644
--- a/Applications/QuickShow/QSWidget.h
+++ b/Applications/QuickShow/QSWidget.h
@@ -27,6 +27,7 @@
#pragma once
#include <LibGUI/Frame.h>
+#include <LibGfx/FloatPoint.h>
class QSLabel;
@@ -35,13 +36,13 @@ class QSWidget final : public GUI::Frame {
public:
virtual ~QSWidget() override;
- void set_bitmap(NonnullRefPtr<Gfx::Bitmap>);
const Gfx::Bitmap* bitmap() const { return m_bitmap.ptr(); }
-
- void set_path(const String&);
const String& path() const { return m_path; }
+ void load_from_file(const String&);
+
Function<void(int)> on_scale_change;
+ Function<void(const GUI::DropEvent&)> on_drop;
private:
QSWidget();
@@ -55,10 +56,13 @@ private:
void relayout();
+ String m_path;
RefPtr<Gfx::Bitmap> m_bitmap;
+
Gfx::Rect m_bitmap_rect;
int m_scale { 100 };
- Gfx::Point m_pan_origin;
- Gfx::Point m_pan_bitmap_origin;
- String m_path;
+ Gfx::FloatPoint m_pan_origin;
+
+ Gfx::Point m_click_position;
+ Gfx::FloatPoint m_saved_pan_origin;
};
diff --git a/Applications/QuickShow/main.cpp b/Applications/QuickShow/main.cpp
index 6b36549494..53ff0dfd68 100644
--- a/Applications/QuickShow/main.cpp
+++ b/Applications/QuickShow/main.cpp
@@ -25,9 +25,13 @@
*/
#include "QSWidget.h"
+#include <AK/URL.h>
+#include <LibCore/MimeData.h>
+#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
+#include <LibGUI/FilePicker.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h>
@@ -37,35 +41,18 @@
int main(int argc, char** argv)
{
- if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
+ if (pledge("stdio shared_buffer accept 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", nullptr) < 0) {
+ if (pledge("stdio shared_buffer accept rpath proc exec thread", nullptr) < 0) {
perror("pledge");
return 1;
}
- auto menubar = make<GUI::MenuBar>();
-
- auto& app_menu = menubar->add_menu("QuickShow");
- app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
- GUI::Application::the().quit(0);
- return;
- }));
-
- menubar->add_menu("File");
-
- auto& help_menu = menubar->add_menu("Help");
- help_menu.add_action(GUI::Action::create("About", [](const GUI::Action&) {
- dbgprintf("FIXME: Implement Help/About\n");
- }));
-
- app.set_menubar(move(menubar));
-
#if 0
if (argc != 2) {
printf("usage: qs <image-file>\n");
@@ -73,35 +60,70 @@ int main(int argc, char** argv)
}
#endif
- const char* path = "/res/wallpapers/sunset-retro.png";
- if (argc > 1)
- path = argv[1];
-
- auto bitmap = Gfx::Bitmap::load_from_file(path);
- if (!bitmap) {
- fprintf(stderr, "Failed to load %s\n", path);
- return 1;
- }
-
auto window = GUI::Window::construct();
+ window->set_double_buffering_enabled(true);
+ window->set_rect(200, 200, 300, 200);
+ window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-image.png"));
+
auto& widget = window->set_main_widget<QSWidget>();
- widget.set_path(path);
- widget.set_bitmap(*bitmap);
+ if (argc > 1)
+ widget.load_from_file(argv[1]);
auto update_window_title = [&](int scale) {
- window->set_title(String::format("%s %s %d%% - QuickShow", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
+ if (widget.bitmap())
+ window->set_title(String::format("%s %s %d%% - QuickShow", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
+ else
+ window->set_title("QuickShow");
};
- window->set_double_buffering_enabled(true);
update_window_title(100);
- window->set_rect(200, 200, bitmap->width(), bitmap->height());
widget.on_scale_change = [&](int scale) {
update_window_title(scale);
};
+ widget.on_drop = [&](auto& event) {
+ window->move_to_front();
+
+ if (event.mime_data().has_urls()) {
+ auto urls = event.mime_data().urls();
+
+ if (!urls.is_empty()) {
+ auto url = urls.first();
+ widget.load_from_file(url.path());
+ }
+
+ for (size_t i = 1; i < urls.size(); ++i) {
+ if (fork() == 0) {
+ execl("/bin/QuickShow", "/bin/QuickShow", urls[i].path().characters(), nullptr);
+ ASSERT_NOT_REACHED();
+ }
+ }
+ }
+ };
+
+ 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_separator();
+ app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
+ app.quit();
+ }));
+
+ auto& help_menu = menubar->add_menu("Help");
+ help_menu.add_action(GUI::Action::create("About", [&](auto&) {
+ GUI::AboutDialog::show("QuickShow", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-image.png"), window);
+ }));
+
+ app.set_menubar(move(menubar));
+
window->show();
- bitmap = nullptr;
return app.exec();
}