ViewWidget.cpp 9.0 KB

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