ViewWidget.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "ViewWidget.h"
  9. #include <AK/LexicalPath.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/MappedFile.h>
  14. #include <LibCore/Timer.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGfx/Bitmap.h>
  17. #include <LibGfx/Orientation.h>
  18. #include <LibGfx/Palette.h>
  19. #include <LibImageDecoderClient/Client.h>
  20. namespace ImageViewer {
  21. ViewWidget::ViewWidget()
  22. : m_timer(Core::Timer::construct())
  23. {
  24. set_fill_with_background_color(false);
  25. }
  26. ViewWidget::~ViewWidget()
  27. {
  28. }
  29. void ViewWidget::clear()
  30. {
  31. m_timer->stop();
  32. m_decoded_image.clear();
  33. m_bitmap = nullptr;
  34. if (on_image_change)
  35. on_image_change(m_bitmap);
  36. m_path = {};
  37. reset_view();
  38. update();
  39. }
  40. void ViewWidget::flip(Gfx::Orientation orientation)
  41. {
  42. m_bitmap = m_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
  43. set_scale(m_scale);
  44. resize_window();
  45. }
  46. void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
  47. {
  48. m_bitmap = m_bitmap->rotated(rotation_direction).release_value_but_fixme_should_propagate_errors();
  49. set_scale(m_scale);
  50. resize_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<String> ViewWidget::load_files_from_directory(const String& path) const
  65. {
  66. Vector<String> files_in_directory;
  67. auto current_dir = LexicalPath(path).parent().string();
  68. Core::DirIterator iterator(current_dir, Core::DirIterator::Flags::SkipDots);
  69. while (iterator.has_next()) {
  70. String file = iterator.next_full_path();
  71. if (!Gfx::Bitmap::is_path_a_supported_image_format(file))
  72. continue;
  73. files_in_directory.append(file);
  74. }
  75. return files_in_directory;
  76. }
  77. void ViewWidget::set_path(const String& 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::set_scale(float scale)
  102. {
  103. if (m_bitmap.is_null())
  104. return;
  105. if (scale < 0.1f)
  106. scale = 0.1f;
  107. if (scale > 10.f)
  108. scale = 10.f;
  109. m_scale = scale;
  110. Gfx::IntSize new_size;
  111. new_size.set_width(m_bitmap->width() * m_scale);
  112. new_size.set_height(m_bitmap->height() * m_scale);
  113. m_bitmap_rect.set_size(new_size);
  114. if (on_scale_change)
  115. on_scale_change(m_scale);
  116. relayout();
  117. }
  118. void ViewWidget::relayout()
  119. {
  120. if (m_bitmap.is_null())
  121. return;
  122. Gfx::IntSize new_size = m_bitmap_rect.size();
  123. Gfx::IntPoint new_location;
  124. new_location.set_x((width() / 2) - (new_size.width() / 2) - m_pan_origin.x());
  125. new_location.set_y((height() / 2) - (new_size.height() / 2) - m_pan_origin.y());
  126. m_bitmap_rect.set_location(new_location);
  127. update();
  128. }
  129. void ViewWidget::resize_event(GUI::ResizeEvent& event)
  130. {
  131. relayout();
  132. GUI::Widget::resize_event(event);
  133. }
  134. void ViewWidget::doubleclick_event(GUI::MouseEvent&)
  135. {
  136. on_doubleclick();
  137. }
  138. void ViewWidget::paint_event(GUI::PaintEvent& event)
  139. {
  140. Frame::paint_event(event);
  141. GUI::Painter painter(*this);
  142. painter.add_clip_rect(event.rect());
  143. painter.add_clip_rect(frame_inner_rect());
  144. Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
  145. if (!m_bitmap.is_null())
  146. painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect(), 1.0f, m_scaling_mode);
  147. }
  148. void ViewWidget::mousedown_event(GUI::MouseEvent& event)
  149. {
  150. if (event.button() != GUI::MouseButton::Primary)
  151. return;
  152. m_click_position = event.position();
  153. m_saved_pan_origin = m_pan_origin;
  154. }
  155. void ViewWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { }
  156. void ViewWidget::mousemove_event(GUI::MouseEvent& event)
  157. {
  158. if (!(event.buttons() & GUI::MouseButton::Primary))
  159. return;
  160. auto delta = event.position() - m_click_position;
  161. m_pan_origin = m_saved_pan_origin.translated(
  162. -delta.x(),
  163. -delta.y());
  164. relayout();
  165. }
  166. void ViewWidget::mousewheel_event(GUI::MouseEvent& event)
  167. {
  168. float new_scale = m_scale / AK::exp2(event.wheel_delta() / 8.f);
  169. if (new_scale < 0.1f)
  170. new_scale = 0.1f;
  171. if (new_scale > 10.f)
  172. new_scale = 10.f;
  173. if (new_scale == m_scale) {
  174. return;
  175. }
  176. // focus_point is the window position the cursor is pointing to.
  177. // The pixel (in image space) the cursor points to is located at
  178. // (m_pan_origin + focus_point) / scale_factor.
  179. // We want the image after scaling to be panned in such a way that the cursor
  180. // will still point to the same image pixel. Basically, we need to solve
  181. // (m_pan_origin + focus_point) / old_scale_factor = (new_m_pan_origin + focus_point) / new_scale_factor.
  182. Gfx::FloatPoint focus_point {
  183. event.x() - width() / 2.0f,
  184. event.y() - height() / 2.0f
  185. };
  186. // A little algebra shows that new m_pan_origin equals to:
  187. m_pan_origin = (m_pan_origin + focus_point) * (new_scale / m_scale) - focus_point;
  188. set_scale(new_scale);
  189. }
  190. void ViewWidget::load_from_file(const String& path)
  191. {
  192. auto show_error = [&] {
  193. GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error);
  194. };
  195. auto file_or_error = Core::MappedFile::map(path);
  196. if (file_or_error.is_error()) {
  197. show_error();
  198. return;
  199. }
  200. auto& mapped_file = *file_or_error.value();
  201. // Spawn a new ImageDecoder service process and connect to it.
  202. auto client = ImageDecoderClient::Client::construct();
  203. auto decoded_image_or_error = client->decode_image(mapped_file.bytes());
  204. if (!decoded_image_or_error.has_value()) {
  205. show_error();
  206. return;
  207. }
  208. m_decoded_image = decoded_image_or_error.release_value();
  209. m_bitmap = m_decoded_image->frames[0].bitmap;
  210. if (on_image_change)
  211. on_image_change(m_bitmap);
  212. if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
  213. const auto& first_frame = m_decoded_image->frames[0];
  214. m_timer->set_interval(first_frame.duration);
  215. m_timer->on_timeout = [this] { animate(); };
  216. m_timer->start();
  217. } else {
  218. m_timer->stop();
  219. }
  220. m_path = Core::File::real_path_for(path);
  221. m_scale = -1;
  222. reset_view();
  223. }
  224. void ViewWidget::drop_event(GUI::DropEvent& event)
  225. {
  226. event.accept();
  227. if (on_drop)
  228. on_drop(event);
  229. }
  230. void ViewWidget::resize_window()
  231. {
  232. if (window()->is_fullscreen() || window()->is_maximized())
  233. return;
  234. auto absolute_bitmap_rect = m_bitmap_rect;
  235. absolute_bitmap_rect.translate_by(window()->rect().top_left());
  236. if (window()->rect().contains(absolute_bitmap_rect))
  237. return;
  238. if (!m_bitmap)
  239. return;
  240. auto new_size = m_bitmap_rect.size();
  241. if (new_size.width() < 300)
  242. new_size.set_width(300);
  243. if (new_size.height() < 200)
  244. new_size.set_height(200);
  245. new_size.set_height(new_size.height() + m_toolbar_height);
  246. window()->resize(new_size);
  247. }
  248. void ViewWidget::reset_view()
  249. {
  250. m_pan_origin = { 0, 0 };
  251. set_scale(1.f);
  252. }
  253. void ViewWidget::set_bitmap(const Gfx::Bitmap* bitmap)
  254. {
  255. if (m_bitmap == bitmap)
  256. return;
  257. m_bitmap = bitmap;
  258. update();
  259. }
  260. // Same as ImageWidget::animate(), you probably want to keep any changes in sync
  261. void ViewWidget::animate()
  262. {
  263. if (!m_decoded_image.has_value())
  264. return;
  265. m_current_frame_index = (m_current_frame_index + 1) % m_decoded_image->frames.size();
  266. const auto& current_frame = m_decoded_image->frames[m_current_frame_index];
  267. set_bitmap(current_frame.bitmap);
  268. if ((int)current_frame.duration != m_timer->interval()) {
  269. m_timer->restart(current_frame.duration);
  270. }
  271. if (m_current_frame_index == m_decoded_image->frames.size() - 1) {
  272. ++m_loops_completed;
  273. if (m_loops_completed > 0 && m_loops_completed == m_decoded_image->loop_count) {
  274. m_timer->stop();
  275. }
  276. }
  277. }
  278. void ViewWidget::set_scaling_mode(Gfx::Painter::ScalingMode scaling_mode)
  279. {
  280. m_scaling_mode = scaling_mode;
  281. update();
  282. }
  283. }