ImageWidget.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/MappedFile.h>
  8. #include <LibGUI/ImageWidget.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/ImageDecoder.h>
  12. REGISTER_WIDGET(GUI, ImageWidget)
  13. namespace GUI {
  14. ImageWidget::ImageWidget(StringView)
  15. : m_timer(Core::Timer::construct())
  16. {
  17. set_frame_thickness(0);
  18. set_frame_shadow(Gfx::FrameShadow::Plain);
  19. set_frame_shape(Gfx::FrameShape::NoFrame);
  20. set_auto_resize(true);
  21. REGISTER_BOOL_PROPERTY("auto_resize", auto_resize, set_auto_resize);
  22. REGISTER_BOOL_PROPERTY("should_stretch", should_stretch, set_should_stretch);
  23. }
  24. void ImageWidget::set_bitmap(Gfx::Bitmap const* bitmap)
  25. {
  26. if (m_bitmap == bitmap)
  27. return;
  28. m_bitmap = bitmap;
  29. if (m_bitmap && m_auto_resize)
  30. set_fixed_size(m_bitmap->size());
  31. update();
  32. }
  33. void ImageWidget::set_auto_resize(bool value)
  34. {
  35. m_auto_resize = value;
  36. if (m_bitmap)
  37. set_fixed_size(m_bitmap->size());
  38. }
  39. // Same as ImageViewer::ViewWidget::animate(), you probably want to keep any changes in sync
  40. void ImageWidget::animate()
  41. {
  42. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  43. auto current_frame = m_image_decoder->frame(m_current_frame_index).release_value_but_fixme_should_propagate_errors();
  44. set_bitmap(current_frame.image);
  45. if (current_frame.duration != m_timer->interval()) {
  46. m_timer->restart(current_frame.duration);
  47. }
  48. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  49. ++m_loops_completed;
  50. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  51. m_timer->stop();
  52. }
  53. }
  54. }
  55. void ImageWidget::load_from_file(StringView path)
  56. {
  57. auto file_or_error = Core::MappedFile::map(path);
  58. if (file_or_error.is_error())
  59. return;
  60. auto& mapped_file = *file_or_error.value();
  61. m_image_decoder = Gfx::ImageDecoder::try_create(mapped_file.bytes());
  62. VERIFY(m_image_decoder);
  63. auto frame = m_image_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  64. auto bitmap = frame.image;
  65. VERIFY(bitmap);
  66. set_bitmap(bitmap);
  67. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  68. auto first_frame = m_image_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  69. m_timer->set_interval(first_frame.duration);
  70. m_timer->on_timeout = [this] { animate(); };
  71. m_timer->start();
  72. }
  73. }
  74. void ImageWidget::mousedown_event(GUI::MouseEvent&)
  75. {
  76. if (on_click)
  77. on_click();
  78. }
  79. void ImageWidget::paint_event(PaintEvent& event)
  80. {
  81. Frame::paint_event(event);
  82. if (!m_bitmap) {
  83. return;
  84. }
  85. Painter painter(*this);
  86. painter.add_clip_rect(event.rect());
  87. if (m_should_stretch) {
  88. painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
  89. } else {
  90. auto location = frame_inner_rect().center().translated(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
  91. painter.blit(location, *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
  92. }
  93. }
  94. void ImageWidget::set_opacity_percent(int percent)
  95. {
  96. if (m_opacity_percent == percent)
  97. return;
  98. m_opacity_percent = percent;
  99. update();
  100. }
  101. }