1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

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.
This commit is contained in:
Tibor Nagy 2020-04-05 20:25:14 +02:00 committed by Andreas Kling
parent 795067e08c
commit 63b11e094d
3 changed files with 117 additions and 90 deletions

View file

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