diff options
Diffstat (limited to 'Applications/QuickShow/QSWidget.cpp')
-rw-r--r-- | Applications/QuickShow/QSWidget.cpp | 99 |
1 files changed, 50 insertions, 49 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); } |