ViewWidget.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/DirIterator.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. 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<DeprecatedString> ViewWidget::load_files_from_directory(DeprecatedString const& path) const
  69. {
  70. Vector<DeprecatedString> 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. DeprecatedString 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(DeprecatedString const& 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(DeprecatedString const& path)
  132. {
  133. auto show_error = [&] {
  134. GUI::MessageBox::show(window(), DeprecatedString::formatted("Failed to open {}", path), "Cannot open image"sv, 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 mime_type = Core::guess_mime_type_based_on_filename(path);
  145. auto decoded_image_or_error = client->decode_image(mapped_file.bytes(), mime_type);
  146. if (!decoded_image_or_error.has_value()) {
  147. show_error();
  148. return;
  149. }
  150. m_decoded_image = decoded_image_or_error.release_value();
  151. m_bitmap = m_decoded_image->frames[0].bitmap;
  152. if (m_bitmap.is_null()) {
  153. show_error();
  154. return;
  155. }
  156. set_original_rect(m_bitmap->rect());
  157. if (on_image_change)
  158. on_image_change(m_bitmap);
  159. if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
  160. auto const& first_frame = m_decoded_image->frames[0];
  161. m_timer->set_interval(first_frame.duration);
  162. m_timer->on_timeout = [this] { animate(); };
  163. m_timer->start();
  164. } else {
  165. m_timer->stop();
  166. }
  167. m_path = Core::DeprecatedFile::real_path_for(path);
  168. GUI::Application::the()->set_most_recently_open_file(String::from_utf8(path).release_value_but_fixme_should_propagate_errors());
  169. reset_view();
  170. }
  171. void ViewWidget::drag_enter_event(GUI::DragEvent& event)
  172. {
  173. auto const& mime_types = event.mime_types();
  174. if (mime_types.contains_slow("text/uri-list"))
  175. event.accept();
  176. }
  177. void ViewWidget::drop_event(GUI::DropEvent& event)
  178. {
  179. event.accept();
  180. if (on_drop)
  181. on_drop(event);
  182. }
  183. void ViewWidget::resize_window()
  184. {
  185. if (window()->is_fullscreen() || window()->is_maximized())
  186. return;
  187. auto absolute_bitmap_rect = content_rect();
  188. absolute_bitmap_rect.translate_by(window()->rect().top_left());
  189. if (window()->rect().contains(absolute_bitmap_rect))
  190. return;
  191. if (!m_bitmap)
  192. return;
  193. auto new_size = content_rect().size();
  194. if (new_size.width() < 300)
  195. new_size.set_width(300);
  196. if (new_size.height() < 200)
  197. new_size.set_height(200);
  198. new_size.set_height(new_size.height() + m_toolbar_height);
  199. window()->resize(new_size);
  200. }
  201. void ViewWidget::set_bitmap(Gfx::Bitmap const* bitmap)
  202. {
  203. if (m_bitmap == bitmap)
  204. return;
  205. m_bitmap = bitmap;
  206. set_original_rect(m_bitmap->rect());
  207. update();
  208. }
  209. // Same as ImageWidget::animate(), you probably want to keep any changes in sync
  210. void ViewWidget::animate()
  211. {
  212. if (!m_decoded_image.has_value())
  213. return;
  214. m_current_frame_index = (m_current_frame_index + 1) % m_decoded_image->frames.size();
  215. auto const& current_frame = m_decoded_image->frames[m_current_frame_index];
  216. set_bitmap(current_frame.bitmap);
  217. if ((int)current_frame.duration != m_timer->interval()) {
  218. m_timer->restart(current_frame.duration);
  219. }
  220. if (m_current_frame_index == m_decoded_image->frames.size() - 1) {
  221. ++m_loops_completed;
  222. if (m_loops_completed > 0 && m_loops_completed == m_decoded_image->loop_count) {
  223. m_timer->stop();
  224. }
  225. }
  226. }
  227. void ViewWidget::set_scaling_mode(Gfx::Painter::ScalingMode scaling_mode)
  228. {
  229. m_scaling_mode = scaling_mode;
  230. update();
  231. }
  232. }