ViewWidget.cpp 8.0 KB

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