QSWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2018-2020, 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 "QSWidget.h"
  8. #include <AK/MappedFile.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/DirIterator.h>
  11. #include <LibCore/Timer.h>
  12. #include <LibGUI/MessageBox.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/Orientation.h>
  16. #include <LibGfx/Palette.h>
  17. QSWidget::QSWidget()
  18. : m_timer(Core::Timer::construct())
  19. {
  20. set_fill_with_background_color(false);
  21. }
  22. QSWidget::~QSWidget()
  23. {
  24. }
  25. void QSWidget::clear()
  26. {
  27. m_bitmap = nullptr;
  28. m_path = {};
  29. reset_view();
  30. update();
  31. }
  32. void QSWidget::flip(Gfx::Orientation orientation)
  33. {
  34. m_bitmap = m_bitmap->flipped(orientation);
  35. set_scale(m_scale);
  36. resize_window();
  37. }
  38. void QSWidget::rotate(Gfx::RotationDirection rotation_direction)
  39. {
  40. m_bitmap = m_bitmap->rotated(rotation_direction);
  41. set_scale(m_scale);
  42. resize_window();
  43. }
  44. void QSWidget::navigate(Directions direction)
  45. {
  46. if (m_path == nullptr)
  47. return;
  48. auto parts = m_path.split('/');
  49. parts.remove(parts.size() - 1);
  50. StringBuilder sb;
  51. sb.append("/");
  52. sb.join("/", parts);
  53. auto current_dir = sb.to_string();
  54. if (m_files_in_same_dir.is_empty()) {
  55. Core::DirIterator iterator(current_dir, Core::DirIterator::Flags::SkipDots);
  56. while (iterator.has_next()) {
  57. String file = iterator.next_full_path();
  58. if (!Gfx::Bitmap::is_path_a_supported_image_format(file))
  59. continue;
  60. m_files_in_same_dir.append(file);
  61. }
  62. }
  63. auto current_index = m_files_in_same_dir.find_first_index(m_path);
  64. if (!current_index.has_value()) {
  65. return;
  66. }
  67. size_t index = current_index.value();
  68. if (direction == Directions::Back) {
  69. if (index == 0) {
  70. GUI::MessageBox::show(window(), "This is the first file.", "Cannot open image", GUI::MessageBox::Type::Error);
  71. return;
  72. }
  73. index--;
  74. } else if (direction == Directions::Forward) {
  75. if (index == m_files_in_same_dir.size() - 1) {
  76. GUI::MessageBox::show(window(), "This is the last file.", "Cannot open image", GUI::MessageBox::Type::Error);
  77. return;
  78. }
  79. index++;
  80. } else if (direction == Directions::First) {
  81. index = 0;
  82. } else if (direction == Directions::Last) {
  83. index = m_files_in_same_dir.size() - 1;
  84. }
  85. this->load_from_file(m_files_in_same_dir.at(index));
  86. }
  87. void QSWidget::set_scale(int scale)
  88. {
  89. if (m_bitmap.is_null())
  90. return;
  91. if (m_scale == scale) {
  92. update();
  93. return;
  94. }
  95. if (scale < 10)
  96. scale = 10;
  97. if (scale > 1000)
  98. scale = 1000;
  99. m_scale = scale;
  100. float scale_factor = (float)m_scale / 100.0f;
  101. Gfx::IntSize new_size;
  102. new_size.set_width(m_bitmap->width() * scale_factor);
  103. new_size.set_height(m_bitmap->height() * scale_factor);
  104. m_bitmap_rect.set_size(new_size);
  105. if (on_scale_change)
  106. on_scale_change(m_scale, m_bitmap_rect);
  107. relayout();
  108. }
  109. void QSWidget::relayout()
  110. {
  111. if (m_bitmap.is_null())
  112. return;
  113. Gfx::IntSize new_size = m_bitmap_rect.size();
  114. Gfx::IntPoint new_location;
  115. new_location.set_x((width() / 2) - (new_size.width() / 2) - m_pan_origin.x());
  116. new_location.set_y((height() / 2) - (new_size.height() / 2) - m_pan_origin.y());
  117. m_bitmap_rect.set_location(new_location);
  118. update();
  119. }
  120. void QSWidget::resize_event(GUI::ResizeEvent& event)
  121. {
  122. relayout();
  123. GUI::Widget::resize_event(event);
  124. }
  125. void QSWidget::doubleclick_event(GUI::MouseEvent&)
  126. {
  127. on_doubleclick();
  128. }
  129. void QSWidget::paint_event(GUI::PaintEvent& event)
  130. {
  131. Frame::paint_event(event);
  132. GUI::Painter painter(*this);
  133. painter.add_clip_rect(event.rect());
  134. painter.add_clip_rect(frame_inner_rect());
  135. Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
  136. if (!m_bitmap.is_null())
  137. painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
  138. }
  139. void QSWidget::mousedown_event(GUI::MouseEvent& event)
  140. {
  141. if (event.button() != GUI::MouseButton::Left)
  142. return;
  143. m_click_position = event.position();
  144. m_saved_pan_origin = m_pan_origin;
  145. }
  146. void QSWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { }
  147. void QSWidget::mousemove_event(GUI::MouseEvent& event)
  148. {
  149. if (!(event.buttons() & GUI::MouseButton::Left))
  150. return;
  151. auto delta = event.position() - m_click_position;
  152. m_pan_origin = m_saved_pan_origin.translated(
  153. -delta.x(),
  154. -delta.y());
  155. relayout();
  156. }
  157. void QSWidget::mousewheel_event(GUI::MouseEvent& event)
  158. {
  159. int new_scale = m_scale - event.wheel_delta() * 10;
  160. if (new_scale < 10)
  161. new_scale = 10;
  162. if (new_scale > 1000)
  163. new_scale = 1000;
  164. if (new_scale == m_scale) {
  165. return;
  166. }
  167. auto old_scale_factor = (float)m_scale / 100.0f;
  168. auto new_scale_factor = (float)new_scale / 100.0f;
  169. // focus_point is the window position the cursor is pointing to.
  170. // The pixel (in image space) the cursor points to is located at
  171. // (m_pan_origin + focus_point) / scale_factor.
  172. // We want the image after scaling to be panned in such a way that the cursor
  173. // will still point to the same image pixel. Basically, we need to solve
  174. // (m_pan_origin + focus_point) / old_scale_factor = (new_m_pan_origin + focus_point) / new_scale_factor.
  175. Gfx::FloatPoint focus_point {
  176. event.x() - width() / 2.0f,
  177. event.y() - height() / 2.0f
  178. };
  179. // A little algebra shows that new m_pan_origin equals to:
  180. m_pan_origin = (m_pan_origin + focus_point) * (new_scale_factor / old_scale_factor) - focus_point;
  181. set_scale(new_scale);
  182. }
  183. void QSWidget::load_from_file(const String& path)
  184. {
  185. auto show_error = [&] {
  186. GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error);
  187. };
  188. auto file_or_error = MappedFile::map(path);
  189. if (file_or_error.is_error()) {
  190. show_error();
  191. return;
  192. }
  193. auto& mapped_file = *file_or_error.value();
  194. m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size());
  195. auto bitmap = m_image_decoder->bitmap();
  196. if (!bitmap) {
  197. show_error();
  198. return;
  199. }
  200. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  201. const auto& first_frame = m_image_decoder->frame(0);
  202. m_timer->set_interval(first_frame.duration);
  203. m_timer->on_timeout = [this] { animate(); };
  204. m_timer->start();
  205. }
  206. m_path = path;
  207. m_bitmap = bitmap;
  208. m_scale = -1;
  209. reset_view();
  210. }
  211. void QSWidget::drop_event(GUI::DropEvent& event)
  212. {
  213. event.accept();
  214. if (on_drop)
  215. on_drop(event);
  216. }
  217. void QSWidget::resize_window()
  218. {
  219. if (window()->is_fullscreen())
  220. return;
  221. if (!m_bitmap)
  222. return;
  223. auto new_size = m_bitmap->size();
  224. if (new_size.width() < 300)
  225. new_size.set_width(300);
  226. if (new_size.height() < 200)
  227. new_size.set_height(200);
  228. new_size.set_height(new_size.height() + m_toolbar_height);
  229. window()->resize(new_size);
  230. }
  231. void QSWidget::reset_view()
  232. {
  233. m_pan_origin = { 0, 0 };
  234. set_scale(100);
  235. }
  236. void QSWidget::set_bitmap(const Gfx::Bitmap* bitmap)
  237. {
  238. if (m_bitmap == bitmap)
  239. return;
  240. m_bitmap = bitmap;
  241. update();
  242. }
  243. // Same as ImageWidget::animate(), you probably want to keep any changes in sync
  244. void QSWidget::animate()
  245. {
  246. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  247. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  248. set_bitmap(current_frame.image);
  249. if (current_frame.duration != m_timer->interval()) {
  250. m_timer->restart(current_frame.duration);
  251. }
  252. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  253. ++m_loops_completed;
  254. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  255. m_timer->stop();
  256. }
  257. }
  258. }