ViewWidget.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibCore/Timer.h>
  10. #include <LibGUI/Frame.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGfx/Point.h>
  13. #include <LibImageDecoderClient/Client.h>
  14. namespace ImageViewer {
  15. class ViewWidget final : public GUI::Frame {
  16. C_OBJECT(ViewWidget)
  17. public:
  18. enum Directions {
  19. First,
  20. Back,
  21. Forward,
  22. Last
  23. };
  24. virtual ~ViewWidget() override;
  25. const Gfx::Bitmap* bitmap() const { return m_bitmap.ptr(); }
  26. const String& path() const { return m_path; }
  27. void set_scale(float);
  28. float scale() { return m_scale; }
  29. void set_toolbar_height(int height) { m_toolbar_height = height; }
  30. int toolbar_height() { return m_toolbar_height; }
  31. bool scaled_for_first_image() { return m_scaled_for_first_image; }
  32. void set_scaled_for_first_image(bool val) { m_scaled_for_first_image = val; }
  33. void set_path(const String& path);
  34. void resize_window();
  35. void set_scaling_mode(Gfx::Painter::ScalingMode);
  36. bool is_next_available() const;
  37. bool is_previous_available() const;
  38. void clear();
  39. void flip(Gfx::Orientation);
  40. void rotate(Gfx::RotationDirection);
  41. void navigate(Directions);
  42. void load_from_file(const String&);
  43. Function<void(float)> on_scale_change;
  44. Function<void()> on_doubleclick;
  45. Function<void(const GUI::DropEvent&)> on_drop;
  46. Function<void(const Gfx::Bitmap*)> on_image_change;
  47. private:
  48. ViewWidget();
  49. virtual void doubleclick_event(GUI::MouseEvent&) override;
  50. virtual void paint_event(GUI::PaintEvent&) override;
  51. virtual void resize_event(GUI::ResizeEvent&) override;
  52. virtual void mousedown_event(GUI::MouseEvent&) override;
  53. virtual void mouseup_event(GUI::MouseEvent&) override;
  54. virtual void mousemove_event(GUI::MouseEvent&) override;
  55. virtual void mousewheel_event(GUI::MouseEvent&) override;
  56. virtual void drop_event(GUI::DropEvent&) override;
  57. void set_bitmap(const Gfx::Bitmap* bitmap);
  58. void relayout();
  59. void reset_view();
  60. void animate();
  61. Vector<String> load_files_from_directory(const String& path) const;
  62. String m_path;
  63. RefPtr<Gfx::Bitmap> m_bitmap;
  64. Gfx::IntRect m_bitmap_rect;
  65. Optional<ImageDecoderClient::DecodedImage> m_decoded_image;
  66. size_t m_current_frame_index { 0 };
  67. size_t m_loops_completed { 0 };
  68. NonnullRefPtr<Core::Timer> m_timer;
  69. float m_scale { -1 };
  70. int m_toolbar_height { 28 };
  71. bool m_scaled_for_first_image { false };
  72. Gfx::FloatPoint m_pan_origin;
  73. Gfx::IntPoint m_click_position;
  74. Gfx::FloatPoint m_saved_pan_origin;
  75. Vector<String> m_files_in_same_dir;
  76. Optional<size_t> m_current_index;
  77. Gfx::Painter::ScalingMode m_scaling_mode { Gfx::Painter::ScalingMode::NearestNeighbor };
  78. };
  79. }