ViewWidget.cpp 8.7 KB

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