Procházet zdrojové kódy

ImageViewer: Inherit from `AbstractZoomPanWidget`

Mustafa Quraish před 3 roky
rodič
revize
b21d128075

+ 18 - 101
Userland/Applications/ImageViewer/ViewWidget.cpp

@@ -2,6 +2,7 @@
  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  * Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com>
+ * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -38,6 +39,7 @@ void ViewWidget::clear()
     m_bitmap = nullptr;
     if (on_image_change)
         on_image_change(m_bitmap);
+    set_original_rect({});
     m_path = {};
 
     reset_view();
@@ -47,7 +49,8 @@ void ViewWidget::clear()
 void ViewWidget::flip(Gfx::Orientation orientation)
 {
     m_bitmap = m_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
-    set_scale(m_scale);
+    set_original_rect(m_bitmap->rect());
+    set_scale(scale());
 
     resize_window();
 }
@@ -55,7 +58,8 @@ void ViewWidget::flip(Gfx::Orientation orientation)
 void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
 {
     m_bitmap = m_bitmap->rotated(rotation_direction).release_value_but_fixme_should_propagate_errors();
-    set_scale(m_scale);
+    set_original_rect(m_bitmap->rect());
+    set_scale(scale());
 
     resize_window();
 }
@@ -117,50 +121,6 @@ void ViewWidget::navigate(Directions direction)
     this->load_from_file(m_files_in_same_dir.at(index));
 }
 
-void ViewWidget::set_scale(float scale)
-{
-    if (m_bitmap.is_null())
-        return;
-
-    if (scale < 0.1f)
-        scale = 0.1f;
-    if (scale > 10.f)
-        scale = 10.f;
-
-    m_scale = scale;
-
-    Gfx::IntSize new_size;
-    new_size.set_width(m_bitmap->width() * m_scale);
-    new_size.set_height(m_bitmap->height() * m_scale);
-    m_bitmap_rect.set_size(new_size);
-
-    if (on_scale_change)
-        on_scale_change(m_scale);
-
-    relayout();
-}
-
-void ViewWidget::relayout()
-{
-    if (m_bitmap.is_null())
-        return;
-
-    Gfx::IntSize new_size = m_bitmap_rect.size();
-
-    Gfx::IntPoint new_location;
-    new_location.set_x((width() / 2) - (new_size.width() / 2) - m_pan_origin.x());
-    new_location.set_y((height() / 2) - (new_size.height() / 2) - m_pan_origin.y());
-    m_bitmap_rect.set_location(new_location);
-
-    update();
-}
-
-void ViewWidget::resize_event(GUI::ResizeEvent& event)
-{
-    relayout();
-    GUI::Widget::resize_event(event);
-}
-
 void ViewWidget::doubleclick_event(GUI::MouseEvent&)
 {
     on_doubleclick();
@@ -177,59 +137,21 @@ void ViewWidget::paint_event(GUI::PaintEvent& event)
     Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
 
     if (!m_bitmap.is_null())
-        painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect(), 1.0f, m_scaling_mode);
+        painter.draw_scaled_bitmap(content_rect(), *m_bitmap, m_bitmap->rect(), 1.0f, m_scaling_mode);
 }
 
 void ViewWidget::mousedown_event(GUI::MouseEvent& event)
 {
-    if (event.button() != GUI::MouseButton::Primary)
-        return;
-    m_click_position = event.position();
-    m_saved_pan_origin = m_pan_origin;
+    if (event.button() == GUI::MouseButton::Primary)
+        start_panning(event.position());
+    GUI::AbstractZoomPanWidget::mousedown_event(event);
 }
 
-void ViewWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { }
-
-void ViewWidget::mousemove_event(GUI::MouseEvent& event)
+void ViewWidget::mouseup_event(GUI::MouseEvent& event)
 {
-    if (!(event.buttons() & GUI::MouseButton::Primary))
-        return;
-
-    auto delta = event.position() - m_click_position;
-    m_pan_origin = m_saved_pan_origin.translated(
-        -delta.x(),
-        -delta.y());
-
-    relayout();
-}
-
-void ViewWidget::mousewheel_event(GUI::MouseEvent& event)
-{
-    float new_scale = m_scale / AK::exp2(event.wheel_delta() / 8.f);
-    if (new_scale < 0.1f)
-        new_scale = 0.1f;
-    if (new_scale > 10.f)
-        new_scale = 10.f;
-
-    if (new_scale == m_scale) {
-        return;
-    }
-
-    // focus_point is the window position the cursor is pointing to.
-    // The pixel (in image space) the cursor points to is located at
-    // (m_pan_origin + focus_point) / scale_factor.
-    // We want the image after scaling to be panned in such a way that the cursor
-    // will still point to the same image pixel. Basically, we need to solve
-    // (m_pan_origin + focus_point) / old_scale_factor = (new_m_pan_origin + focus_point) / new_scale_factor.
-    Gfx::FloatPoint focus_point {
-        event.x() - width() / 2.0f,
-        event.y() - height() / 2.0f
-    };
-
-    // A little algebra shows that new m_pan_origin equals to:
-    m_pan_origin = (m_pan_origin + focus_point) * (new_scale / m_scale) - focus_point;
-
-    set_scale(new_scale);
+    if (event.button() == GUI::MouseButton::Primary)
+        stop_panning();
+    GUI::AbstractZoomPanWidget::mouseup_event(event);
 }
 
 void ViewWidget::load_from_file(const String& path)
@@ -257,6 +179,7 @@ void ViewWidget::load_from_file(const String& path)
 
     m_decoded_image = decoded_image_or_error.release_value();
     m_bitmap = m_decoded_image->frames[0].bitmap;
+    set_original_rect(m_bitmap->rect());
     if (on_image_change)
         on_image_change(m_bitmap);
 
@@ -270,7 +193,6 @@ void ViewWidget::load_from_file(const String& path)
     }
 
     m_path = Core::File::real_path_for(path);
-    m_scale = -1;
     reset_view();
 }
 
@@ -286,7 +208,7 @@ void ViewWidget::resize_window()
     if (window()->is_fullscreen() || window()->is_maximized())
         return;
 
-    auto absolute_bitmap_rect = m_bitmap_rect;
+    auto absolute_bitmap_rect = content_rect();
     absolute_bitmap_rect.translate_by(window()->rect().top_left());
     if (window()->rect().contains(absolute_bitmap_rect))
         return;
@@ -294,7 +216,7 @@ void ViewWidget::resize_window()
     if (!m_bitmap)
         return;
 
-    auto new_size = m_bitmap_rect.size();
+    auto new_size = content_rect().size();
 
     if (new_size.width() < 300)
         new_size.set_width(300);
@@ -305,17 +227,12 @@ void ViewWidget::resize_window()
     window()->resize(new_size);
 }
 
-void ViewWidget::reset_view()
-{
-    m_pan_origin = { 0, 0 };
-    set_scale(1.f);
-}
-
 void ViewWidget::set_bitmap(const Gfx::Bitmap* bitmap)
 {
     if (m_bitmap == bitmap)
         return;
     m_bitmap = bitmap;
+    set_original_rect(m_bitmap->rect());
     update();
 }
 

+ 3 - 16
Userland/Applications/ImageViewer/ViewWidget.h

@@ -2,6 +2,7 @@
  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  * Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com>
+ * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -9,14 +10,13 @@
 #pragma once
 
 #include <LibCore/Timer.h>
-#include <LibGUI/Frame.h>
+#include <LibGUI/AbstractZoomPanWidget.h>
 #include <LibGUI/Painter.h>
-#include <LibGfx/Point.h>
 #include <LibImageDecoderClient/Client.h>
 
 namespace ImageViewer {
 
-class ViewWidget final : public GUI::Frame {
+class ViewWidget final : public GUI::AbstractZoomPanWidget {
     C_OBJECT(ViewWidget)
 public:
     enum Directions {
@@ -30,8 +30,6 @@ public:
 
     const Gfx::Bitmap* bitmap() const { return m_bitmap.ptr(); }
     const String& path() const { return m_path; }
-    void set_scale(float);
-    float scale() { return m_scale; }
     void set_toolbar_height(int height) { m_toolbar_height = height; }
     int toolbar_height() { return m_toolbar_height; }
     bool scaled_for_first_image() { return m_scaled_for_first_image; }
@@ -49,7 +47,6 @@ public:
     void navigate(Directions);
     void load_from_file(const String&);
 
-    Function<void(float)> on_scale_change;
     Function<void()> on_doubleclick;
     Function<void(const GUI::DropEvent&)> on_drop;
     Function<void(const Gfx::Bitmap*)> on_image_change;
@@ -58,34 +55,24 @@ private:
     ViewWidget();
     virtual void doubleclick_event(GUI::MouseEvent&) override;
     virtual void paint_event(GUI::PaintEvent&) override;
-    virtual void resize_event(GUI::ResizeEvent&) override;
     virtual void mousedown_event(GUI::MouseEvent&) override;
     virtual void mouseup_event(GUI::MouseEvent&) override;
-    virtual void mousemove_event(GUI::MouseEvent&) override;
-    virtual void mousewheel_event(GUI::MouseEvent&) override;
     virtual void drop_event(GUI::DropEvent&) override;
 
     void set_bitmap(const Gfx::Bitmap* bitmap);
-    void relayout();
-    void reset_view();
     void animate();
     Vector<String> load_files_from_directory(const String& path) const;
 
     String m_path;
     RefPtr<Gfx::Bitmap> m_bitmap;
-    Gfx::IntRect m_bitmap_rect;
     Optional<ImageDecoderClient::DecodedImage> m_decoded_image;
 
     size_t m_current_frame_index { 0 };
     size_t m_loops_completed { 0 };
     NonnullRefPtr<Core::Timer> m_timer;
 
-    float m_scale { -1 };
     int m_toolbar_height { 28 };
     bool m_scaled_for_first_image { false };
-    Gfx::FloatPoint m_pan_origin;
-    Gfx::IntPoint m_click_position;
-    Gfx::FloatPoint m_saved_pan_origin;
     Vector<String> m_files_in_same_dir;
     Optional<size_t> m_current_index;
     Gfx::Painter::ScalingMode m_scaling_mode { Gfx::Painter::ScalingMode::NearestNeighbor };