1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

ImageViewer: Inherit from AbstractZoomPanWidget

This commit is contained in:
Mustafa Quraish 2022-01-08 04:28:06 -05:00 committed by Andreas Kling
parent 5d7f2086b0
commit b21d128075
2 changed files with 21 additions and 117 deletions

View file

@ -2,6 +2,7 @@
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org> * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com> * Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com>
* Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -38,6 +39,7 @@ void ViewWidget::clear()
m_bitmap = nullptr; m_bitmap = nullptr;
if (on_image_change) if (on_image_change)
on_image_change(m_bitmap); on_image_change(m_bitmap);
set_original_rect({});
m_path = {}; m_path = {};
reset_view(); reset_view();
@ -47,7 +49,8 @@ void ViewWidget::clear()
void ViewWidget::flip(Gfx::Orientation orientation) void ViewWidget::flip(Gfx::Orientation orientation)
{ {
m_bitmap = m_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors(); 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(); resize_window();
} }
@ -55,7 +58,8 @@ void ViewWidget::flip(Gfx::Orientation orientation)
void ViewWidget::rotate(Gfx::RotationDirection rotation_direction) void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
{ {
m_bitmap = m_bitmap->rotated(rotation_direction).release_value_but_fixme_should_propagate_errors(); 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(); resize_window();
} }
@ -117,50 +121,6 @@ void ViewWidget::navigate(Directions direction)
this->load_from_file(m_files_in_same_dir.at(index)); 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&) void ViewWidget::doubleclick_event(GUI::MouseEvent&)
{ {
on_doubleclick(); on_doubleclick();
@ -177,59 +137,21 @@ void ViewWidget::paint_event(GUI::PaintEvent& event)
Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette()); Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
if (!m_bitmap.is_null()) 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) void ViewWidget::mousedown_event(GUI::MouseEvent& event)
{ {
if (event.button() != GUI::MouseButton::Primary) if (event.button() == GUI::MouseButton::Primary)
return; start_panning(event.position());
m_click_position = event.position(); GUI::AbstractZoomPanWidget::mousedown_event(event);
m_saved_pan_origin = m_pan_origin;
} }
void ViewWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { } void ViewWidget::mouseup_event(GUI::MouseEvent& event)
void ViewWidget::mousemove_event(GUI::MouseEvent& event)
{ {
if (!(event.buttons() & GUI::MouseButton::Primary)) if (event.button() == GUI::MouseButton::Primary)
return; stop_panning();
GUI::AbstractZoomPanWidget::mouseup_event(event);
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);
} }
void ViewWidget::load_from_file(const String& path) 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_decoded_image = decoded_image_or_error.release_value();
m_bitmap = m_decoded_image->frames[0].bitmap; m_bitmap = m_decoded_image->frames[0].bitmap;
set_original_rect(m_bitmap->rect());
if (on_image_change) if (on_image_change)
on_image_change(m_bitmap); 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_path = Core::File::real_path_for(path);
m_scale = -1;
reset_view(); reset_view();
} }
@ -286,7 +208,7 @@ void ViewWidget::resize_window()
if (window()->is_fullscreen() || window()->is_maximized()) if (window()->is_fullscreen() || window()->is_maximized())
return; return;
auto absolute_bitmap_rect = m_bitmap_rect; auto absolute_bitmap_rect = content_rect();
absolute_bitmap_rect.translate_by(window()->rect().top_left()); absolute_bitmap_rect.translate_by(window()->rect().top_left());
if (window()->rect().contains(absolute_bitmap_rect)) if (window()->rect().contains(absolute_bitmap_rect))
return; return;
@ -294,7 +216,7 @@ void ViewWidget::resize_window()
if (!m_bitmap) if (!m_bitmap)
return; return;
auto new_size = m_bitmap_rect.size(); auto new_size = content_rect().size();
if (new_size.width() < 300) if (new_size.width() < 300)
new_size.set_width(300); new_size.set_width(300);
@ -305,17 +227,12 @@ void ViewWidget::resize_window()
window()->resize(new_size); 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) void ViewWidget::set_bitmap(const Gfx::Bitmap* bitmap)
{ {
if (m_bitmap == bitmap) if (m_bitmap == bitmap)
return; return;
m_bitmap = bitmap; m_bitmap = bitmap;
set_original_rect(m_bitmap->rect());
update(); update();
} }

View file

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