ViewWidget.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2018-2021, 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. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ViewWidget.h"
  10. #include <AK/LexicalPath.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibCore/DirIterator.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/MappedFile.h>
  15. #include <LibCore/Timer.h>
  16. #include <LibGUI/MessageBox.h>
  17. #include <LibGfx/Bitmap.h>
  18. #include <LibGfx/Orientation.h>
  19. #include <LibGfx/Palette.h>
  20. #include <LibImageDecoderClient/Client.h>
  21. namespace ImageViewer {
  22. ViewWidget::ViewWidget()
  23. : m_timer(Core::Timer::construct())
  24. {
  25. set_fill_with_background_color(false);
  26. }
  27. ViewWidget::~ViewWidget()
  28. {
  29. }
  30. void ViewWidget::clear()
  31. {
  32. m_timer->stop();
  33. m_decoded_image.clear();
  34. m_bitmap = nullptr;
  35. if (on_image_change)
  36. on_image_change(m_bitmap);
  37. set_original_rect({});
  38. m_path = {};
  39. reset_view();
  40. update();
  41. }
  42. void ViewWidget::flip(Gfx::Orientation orientation)
  43. {
  44. m_bitmap = m_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
  45. set_original_rect(m_bitmap->rect());
  46. set_scale(scale());
  47. resize_window();
  48. }
  49. void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
  50. {
  51. m_bitmap = m_bitmap->rotated(rotation_direction).release_value_but_fixme_should_propagate_errors();
  52. set_original_rect(m_bitmap->rect());
  53. set_scale(scale());
  54. resize_window();
  55. }
  56. bool ViewWidget::is_next_available() const
  57. {
  58. if (m_current_index.has_value())
  59. return m_current_index.value() + 1 < m_files_in_same_dir.size();
  60. return false;
  61. }
  62. bool ViewWidget::is_previous_available() const
  63. {
  64. if (m_current_index.has_value())
  65. return m_current_index.value() > 0;
  66. return false;
  67. }
  68. Vector<String> ViewWidget::load_files_from_directory(const String& path) const
  69. {
  70. Vector<String> files_in_directory;
  71. auto current_dir = LexicalPath(path).parent().string();
  72. Core::DirIterator iterator(current_dir, Core::DirIterator::Flags::SkipDots);
  73. while (iterator.has_next()) {
  74. String file = iterator.next_full_path();
  75. if (!Gfx::Bitmap::is_path_a_supported_image_format(file))
  76. continue;
  77. files_in_directory.append(file);
  78. }
  79. return files_in_directory;
  80. }
  81. void ViewWidget::set_path(const String& path)
  82. {
  83. m_path = path;
  84. m_files_in_same_dir = load_files_from_directory(path);
  85. m_current_index = m_files_in_same_dir.find_first_index(path);
  86. }
  87. void ViewWidget::navigate(Directions direction)
  88. {
  89. if (!m_current_index.has_value()) {
  90. return;
  91. }
  92. auto index = m_current_index.value();
  93. if (direction == Directions::Back) {
  94. index--;
  95. } else if (direction == Directions::Forward) {
  96. index++;
  97. } else if (direction == Directions::First) {
  98. index = 0;
  99. } else if (direction == Directions::Last) {
  100. index = m_files_in_same_dir.size() - 1;
  101. }
  102. m_current_index = index;
  103. this->load_from_file(m_files_in_same_dir.at(index));
  104. }
  105. void ViewWidget::doubleclick_event(GUI::MouseEvent&)
  106. {
  107. on_doubleclick();
  108. }
  109. void ViewWidget::paint_event(GUI::PaintEvent& event)
  110. {
  111. Frame::paint_event(event);
  112. GUI::Painter painter(*this);
  113. painter.add_clip_rect(event.rect());
  114. painter.add_clip_rect(frame_inner_rect());
  115. Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
  116. if (!m_bitmap.is_null())
  117. painter.draw_scaled_bitmap(content_rect(), *m_bitmap, m_bitmap->rect(), 1.0f, m_scaling_mode);
  118. }
  119. void ViewWidget::mousedown_event(GUI::MouseEvent& event)
  120. {
  121. if (event.button() == GUI::MouseButton::Primary)
  122. start_panning(event.position());
  123. GUI::AbstractZoomPanWidget::mousedown_event(event);
  124. }
  125. void ViewWidget::mouseup_event(GUI::MouseEvent& event)
  126. {
  127. if (event.button() == GUI::MouseButton::Primary)
  128. stop_panning();
  129. GUI::AbstractZoomPanWidget::mouseup_event(event);
  130. }
  131. void ViewWidget::load_from_file(const String& path)
  132. {
  133. auto show_error = [&] {
  134. GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error);
  135. };
  136. auto file_or_error = Core::MappedFile::map(path);
  137. if (file_or_error.is_error()) {
  138. show_error();
  139. return;
  140. }
  141. auto& mapped_file = *file_or_error.value();
  142. // Spawn a new ImageDecoder service process and connect to it.
  143. auto client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
  144. auto decoded_image_or_error = client->decode_image(mapped_file.bytes());
  145. if (!decoded_image_or_error.has_value()) {
  146. show_error();
  147. return;
  148. }
  149. m_decoded_image = decoded_image_or_error.release_value();
  150. m_bitmap = m_decoded_image->frames[0].bitmap;
  151. set_original_rect(m_bitmap->rect());
  152. if (on_image_change)
  153. on_image_change(m_bitmap);
  154. if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
  155. const auto& first_frame = m_decoded_image->frames[0];
  156. m_timer->set_interval(first_frame.duration);
  157. m_timer->on_timeout = [this] { animate(); };
  158. m_timer->start();
  159. } else {
  160. m_timer->stop();
  161. }
  162. m_path = Core::File::real_path_for(path);
  163. reset_view();
  164. }
  165. void ViewWidget::drop_event(GUI::DropEvent& event)
  166. {
  167. event.accept();
  168. if (on_drop)
  169. on_drop(event);
  170. }
  171. void ViewWidget::resize_window()
  172. {
  173. if (window()->is_fullscreen() || window()->is_maximized())
  174. return;
  175. auto absolute_bitmap_rect = content_rect();
  176. absolute_bitmap_rect.translate_by(window()->rect().top_left());
  177. if (window()->rect().contains(absolute_bitmap_rect))
  178. return;
  179. if (!m_bitmap)
  180. return;
  181. auto new_size = content_rect().size();
  182. if (new_size.width() < 300)
  183. new_size.set_width(300);
  184. if (new_size.height() < 200)
  185. new_size.set_height(200);
  186. new_size.set_height(new_size.height() + m_toolbar_height);
  187. window()->resize(new_size);
  188. }
  189. void ViewWidget::set_bitmap(const Gfx::Bitmap* bitmap)
  190. {
  191. if (m_bitmap == bitmap)
  192. return;
  193. m_bitmap = bitmap;
  194. set_original_rect(m_bitmap->rect());
  195. update();
  196. }
  197. // Same as ImageWidget::animate(), you probably want to keep any changes in sync
  198. void ViewWidget::animate()
  199. {
  200. if (!m_decoded_image.has_value())
  201. return;
  202. m_current_frame_index = (m_current_frame_index + 1) % m_decoded_image->frames.size();
  203. const auto& current_frame = m_decoded_image->frames[m_current_frame_index];
  204. set_bitmap(current_frame.bitmap);
  205. if ((int)current_frame.duration != m_timer->interval()) {
  206. m_timer->restart(current_frame.duration);
  207. }
  208. if (m_current_frame_index == m_decoded_image->frames.size() - 1) {
  209. ++m_loops_completed;
  210. if (m_loops_completed > 0 && m_loops_completed == m_decoded_image->loop_count) {
  211. m_timer->stop();
  212. }
  213. }
  214. }
  215. void ViewWidget::set_scaling_mode(Gfx::Painter::ScalingMode scaling_mode)
  216. {
  217. m_scaling_mode = scaling_mode;
  218. update();
  219. }
  220. }