AbstractZoomPanWidget.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/Frame.h>
  8. #include <LibGfx/Point.h>
  9. #include <LibGfx/Rect.h>
  10. namespace GUI {
  11. class AbstractZoomPanWidget : public GUI::Frame {
  12. C_OBJECT(AbstractZoomPanWidget);
  13. public:
  14. void set_scale(float scale);
  15. float scale() const { return m_scale; }
  16. void set_scale_bounds(float min_scale, float max_scale);
  17. void scale_by(float amount);
  18. void scale_centered(float new_scale, Gfx::IntPoint const& center);
  19. bool is_panning() const { return m_is_panning; }
  20. void start_panning(Gfx::IntPoint const& position);
  21. void stop_panning();
  22. void pan_to(Gfx::IntPoint const& position);
  23. // Should be overridden by derived classes if they want updates.
  24. virtual void handle_relayout(Gfx::IntRect const&) { update(); }
  25. void relayout();
  26. Gfx::FloatPoint frame_to_content_position(Gfx::IntPoint const& frame_position) const;
  27. Gfx::FloatRect frame_to_content_rect(Gfx::IntRect const& frame_rect) const;
  28. Gfx::FloatPoint content_to_frame_position(Gfx::IntPoint const& content_position) const;
  29. Gfx::FloatRect content_to_frame_rect(Gfx::IntRect const& content_rect) const;
  30. virtual void mousewheel_event(GUI::MouseEvent& event) override;
  31. virtual void mousedown_event(GUI::MouseEvent& event) override;
  32. virtual void resize_event(GUI::ResizeEvent& event) override;
  33. virtual void mousemove_event(GUI::MouseEvent& event) override;
  34. virtual void mouseup_event(GUI::MouseEvent& event) override;
  35. void set_original_rect(Gfx::IntRect const& rect) { m_original_rect = rect; }
  36. void set_content_rect(Gfx::IntRect const& content_rect);
  37. void set_origin(Gfx::FloatPoint const& origin) { m_origin = origin; }
  38. void reset_view();
  39. Gfx::IntRect content_rect() const { return m_content_rect; }
  40. Function<void(float)> on_scale_change;
  41. enum class FitType {
  42. Width,
  43. Height,
  44. Both
  45. };
  46. void fit_content_to_rect(Gfx::IntRect const& rect, FitType = FitType::Both);
  47. void fit_content_to_view(FitType fit_type = FitType::Both)
  48. {
  49. fit_content_to_rect(rect(), fit_type);
  50. }
  51. private:
  52. Gfx::IntRect m_original_rect;
  53. Gfx::IntRect m_content_rect;
  54. Gfx::IntPoint m_pan_mouse_pos;
  55. Gfx::FloatPoint m_origin;
  56. Gfx::FloatPoint m_pan_start;
  57. bool m_is_panning { false };
  58. float m_min_scale { 0.1f };
  59. float m_max_scale { 10.0f };
  60. float m_scale { 1.0f };
  61. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> m_saved_cursor { Gfx::StandardCursor::None };
  62. };
  63. }