ImageWidget.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. REGISTER_STRING_PROPERTY("bitmap", bitmap, load_from_file);
  24. }
  25. void ImageWidget::set_bitmap(Gfx::Bitmap const* bitmap)
  26. {
  27. if (m_bitmap == bitmap)
  28. return;
  29. m_bitmap = bitmap;
  30. if (m_bitmap && m_auto_resize)
  31. set_fixed_size(m_bitmap->size());
  32. update();
  33. }
  34. void ImageWidget::set_auto_resize(bool value)
  35. {
  36. m_auto_resize = value;
  37. if (m_bitmap)
  38. set_fixed_size(m_bitmap->size());
  39. }
  40. // Same as ImageViewer::ViewWidget::animate(), you probably want to keep any changes in sync
  41. void ImageWidget::animate()
  42. {
  43. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  44. auto current_frame = m_image_decoder->frame(m_current_frame_index).release_value_but_fixme_should_propagate_errors();
  45. set_bitmap(current_frame.image);
  46. if (current_frame.duration != m_timer->interval()) {
  47. m_timer->restart(current_frame.duration);
  48. }
  49. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  50. ++m_loops_completed;
  51. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  52. m_timer->stop();
  53. }
  54. }
  55. }
  56. void ImageWidget::load_from_file(StringView path)
  57. {
  58. auto file_or_error = Core::MappedFile::map(path);
  59. if (file_or_error.is_error())
  60. return;
  61. auto& mapped_file = *file_or_error.value();
  62. m_image_decoder = Gfx::ImageDecoder::try_create(mapped_file.bytes());
  63. VERIFY(m_image_decoder);
  64. auto frame = m_image_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  65. auto bitmap = frame.image;
  66. VERIFY(bitmap);
  67. set_bitmap(bitmap);
  68. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  69. auto first_frame = m_image_decoder->frame(0).release_value_but_fixme_should_propagate_errors();
  70. m_timer->set_interval(first_frame.duration);
  71. m_timer->on_timeout = [this] { animate(); };
  72. m_timer->start();
  73. }
  74. }
  75. void ImageWidget::mousedown_event(GUI::MouseEvent&)
  76. {
  77. if (on_click)
  78. on_click();
  79. }
  80. void ImageWidget::paint_event(PaintEvent& event)
  81. {
  82. Frame::paint_event(event);
  83. if (!m_bitmap) {
  84. return;
  85. }
  86. Painter painter(*this);
  87. painter.add_clip_rect(event.rect());
  88. if (m_should_stretch) {
  89. painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
  90. } else {
  91. auto location = frame_inner_rect().center().translated(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
  92. painter.blit(location, *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
  93. }
  94. }
  95. void ImageWidget::set_opacity_percent(int percent)
  96. {
  97. if (m_opacity_percent == percent)
  98. return;
  99. m_opacity_percent = percent;
  100. update();
  101. }
  102. }