ViewWidget.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  6. * Copyright (c) 2022, the SerenityOS developers.
  7. * Copyright (c) 2023, Caoimhe Byrne <caoimhebyrne06@gmail.com>
  8. *
  9. * SPDX-License-Identifier: BSD-2-Clause
  10. */
  11. #pragma once
  12. #include <LibCore/Timer.h>
  13. #include <LibGUI/AbstractZoomPanWidget.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibImageDecoderClient/Client.h>
  16. namespace ImageViewer {
  17. class ViewWidget final : public GUI::AbstractZoomPanWidget {
  18. C_OBJECT(ViewWidget)
  19. public:
  20. enum Directions {
  21. First,
  22. Back,
  23. Forward,
  24. Last
  25. };
  26. virtual ~ViewWidget() override = default;
  27. Gfx::Bitmap const* bitmap() const { return m_bitmap.ptr(); }
  28. String const& path() const { return m_path; }
  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(String const& path);
  34. void resize_window();
  35. void scale_image_for_window();
  36. void set_scaling_mode(Gfx::Painter::ScalingMode);
  37. bool is_next_available() const;
  38. bool is_previous_available() const;
  39. void clear();
  40. void flip(Gfx::Orientation);
  41. void rotate(Gfx::RotationDirection);
  42. void navigate(Directions);
  43. void open_file(String const&, Core::File&);
  44. Function<void()> on_doubleclick;
  45. Function<void(const GUI::DropEvent&)> on_drop;
  46. Function<void(Gfx::Bitmap const*)> 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 mousedown_event(GUI::MouseEvent&) override;
  52. virtual void mouseup_event(GUI::MouseEvent&) override;
  53. virtual void drag_enter_event(GUI::DragEvent&) override;
  54. virtual void drop_event(GUI::DropEvent&) override;
  55. virtual void resize_event(GUI::ResizeEvent&) override;
  56. void set_bitmap(Gfx::Bitmap const* bitmap);
  57. void animate();
  58. Vector<DeprecatedString> load_files_from_directory(DeprecatedString const& path) const;
  59. ErrorOr<void> try_open_file(String const&, Core::File&);
  60. String m_path;
  61. RefPtr<Gfx::Bitmap const> m_bitmap;
  62. Optional<ImageDecoderClient::DecodedImage> m_decoded_image;
  63. size_t m_current_frame_index { 0 };
  64. size_t m_loops_completed { 0 };
  65. NonnullRefPtr<Core::Timer> m_timer;
  66. int m_toolbar_height { 28 };
  67. bool m_scaled_for_first_image { false };
  68. Vector<DeprecatedString> m_files_in_same_dir;
  69. Optional<size_t> m_current_index;
  70. Gfx::Painter::ScalingMode m_scaling_mode { Gfx::Painter::ScalingMode::NearestNeighbor };
  71. };
  72. }