QSWidget.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "QSWidget.h"
  28. #include <AK/MappedFile.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/Timer.h>
  32. #include <LibGUI/MessageBox.h>
  33. #include <LibGUI/Painter.h>
  34. #include <LibGfx/Bitmap.h>
  35. #include <LibGfx/Orientation.h>
  36. #include <LibGfx/Palette.h>
  37. QSWidget::QSWidget()
  38. : m_timer(Core::Timer::construct())
  39. {
  40. set_fill_with_background_color(false);
  41. }
  42. QSWidget::~QSWidget()
  43. {
  44. }
  45. void QSWidget::clear()
  46. {
  47. m_bitmap = nullptr;
  48. m_path = {};
  49. reset_view();
  50. update();
  51. }
  52. void QSWidget::flip(Gfx::Orientation orientation)
  53. {
  54. m_bitmap = m_bitmap->flipped(orientation);
  55. set_scale(m_scale);
  56. resize_window();
  57. }
  58. void QSWidget::rotate(Gfx::RotationDirection rotation_direction)
  59. {
  60. m_bitmap = m_bitmap->rotated(rotation_direction);
  61. set_scale(m_scale);
  62. resize_window();
  63. }
  64. void QSWidget::navigate(Directions direction)
  65. {
  66. if (m_path == nullptr)
  67. return;
  68. auto parts = m_path.split('/');
  69. parts.remove(parts.size() - 1);
  70. StringBuilder sb;
  71. sb.append("/");
  72. sb.join("/", parts);
  73. auto current_dir = sb.to_string();
  74. if (m_files_in_same_dir.is_empty()) {
  75. Core::DirIterator iterator(current_dir, Core::DirIterator::Flags::SkipDots);
  76. while (iterator.has_next()) {
  77. String file = iterator.next_full_path();
  78. if (!Gfx::Bitmap::is_path_a_supported_image_format(file))
  79. continue;
  80. m_files_in_same_dir.append(file);
  81. }
  82. }
  83. auto current_index = m_files_in_same_dir.find_first_index(m_path);
  84. if (!current_index.has_value()) {
  85. return;
  86. }
  87. size_t index = current_index.value();
  88. if (direction == Directions::Back) {
  89. if (index == 0) {
  90. GUI::MessageBox::show(window(), "This is the first file.", "Cannot open image", GUI::MessageBox::Type::Error);
  91. return;
  92. }
  93. index--;
  94. } else if (direction == Directions::Forward) {
  95. if (index == m_files_in_same_dir.size() - 1) {
  96. GUI::MessageBox::show(window(), "This is the last file.", "Cannot open image", GUI::MessageBox::Type::Error);
  97. return;
  98. }
  99. index++;
  100. } else if (direction == Directions::First) {
  101. index = 0;
  102. } else if (direction == Directions::Last) {
  103. index = m_files_in_same_dir.size() - 1;
  104. }
  105. this->load_from_file(m_files_in_same_dir.at(index));
  106. }
  107. void QSWidget::set_scale(int scale)
  108. {
  109. if (m_bitmap.is_null())
  110. return;
  111. if (m_scale == scale) {
  112. update();
  113. return;
  114. }
  115. if (scale < 10)
  116. scale = 10;
  117. if (scale > 1000)
  118. scale = 1000;
  119. m_scale = scale;
  120. float scale_factor = (float)m_scale / 100.0f;
  121. Gfx::IntSize new_size;
  122. new_size.set_width(m_bitmap->width() * scale_factor);
  123. new_size.set_height(m_bitmap->height() * scale_factor);
  124. m_bitmap_rect.set_size(new_size);
  125. if (on_scale_change)
  126. on_scale_change(m_scale, m_bitmap_rect);
  127. relayout();
  128. }
  129. void QSWidget::relayout()
  130. {
  131. if (m_bitmap.is_null())
  132. return;
  133. Gfx::IntSize new_size = m_bitmap_rect.size();
  134. Gfx::IntPoint new_location;
  135. new_location.set_x((width() / 2) - (new_size.width() / 2) - m_pan_origin.x());
  136. new_location.set_y((height() / 2) - (new_size.height() / 2) - m_pan_origin.y());
  137. m_bitmap_rect.set_location(new_location);
  138. update();
  139. }
  140. void QSWidget::resize_event(GUI::ResizeEvent& event)
  141. {
  142. relayout();
  143. GUI::Widget::resize_event(event);
  144. }
  145. void QSWidget::doubleclick_event(GUI::MouseEvent&)
  146. {
  147. on_doubleclick();
  148. }
  149. void QSWidget::paint_event(GUI::PaintEvent& event)
  150. {
  151. Frame::paint_event(event);
  152. GUI::Painter painter(*this);
  153. painter.add_clip_rect(event.rect());
  154. painter.add_clip_rect(frame_inner_rect());
  155. Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
  156. if (!m_bitmap.is_null())
  157. painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
  158. }
  159. void QSWidget::mousedown_event(GUI::MouseEvent& event)
  160. {
  161. if (event.button() != GUI::MouseButton::Left)
  162. return;
  163. m_click_position = event.position();
  164. m_saved_pan_origin = m_pan_origin;
  165. }
  166. void QSWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { }
  167. void QSWidget::mousemove_event(GUI::MouseEvent& event)
  168. {
  169. if (!(event.buttons() & GUI::MouseButton::Left))
  170. return;
  171. auto delta = event.position() - m_click_position;
  172. m_pan_origin = m_saved_pan_origin.translated(
  173. -delta.x(),
  174. -delta.y());
  175. relayout();
  176. }
  177. void QSWidget::mousewheel_event(GUI::MouseEvent& event)
  178. {
  179. int new_scale = m_scale - event.wheel_delta() * 10;
  180. if (new_scale < 10)
  181. new_scale = 10;
  182. if (new_scale > 1000)
  183. new_scale = 1000;
  184. if (new_scale == m_scale) {
  185. return;
  186. }
  187. auto old_scale_factor = (float)m_scale / 100.0f;
  188. auto new_scale_factor = (float)new_scale / 100.0f;
  189. // focus_point is the window position the cursor is pointing to.
  190. // The pixel (in image space) the cursor points to is located at
  191. // (m_pan_origin + focus_point) / scale_factor.
  192. // We want the image after scaling to be panned in such a way that the cursor
  193. // will still point to the same image pixel. Basically, we need to solve
  194. // (m_pan_origin + focus_point) / old_scale_factor = (new_m_pan_origin + focus_point) / new_scale_factor.
  195. Gfx::FloatPoint focus_point {
  196. event.x() - width() / 2.0f,
  197. event.y() - height() / 2.0f
  198. };
  199. // A little algebra shows that new m_pan_origin equals to:
  200. m_pan_origin = (m_pan_origin + focus_point) * (new_scale_factor / old_scale_factor) - focus_point;
  201. set_scale(new_scale);
  202. }
  203. void QSWidget::load_from_file(const String& path)
  204. {
  205. auto show_error = [&] {
  206. GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error);
  207. };
  208. auto file_or_error = MappedFile::map(path);
  209. if (file_or_error.is_error()) {
  210. show_error();
  211. return;
  212. }
  213. auto& mapped_file = *file_or_error.value();
  214. m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size());
  215. auto bitmap = m_image_decoder->bitmap();
  216. if (!bitmap) {
  217. show_error();
  218. return;
  219. }
  220. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  221. const auto& first_frame = m_image_decoder->frame(0);
  222. m_timer->set_interval(first_frame.duration);
  223. m_timer->on_timeout = [this] { animate(); };
  224. m_timer->start();
  225. }
  226. m_path = path;
  227. m_bitmap = bitmap;
  228. m_scale = -1;
  229. reset_view();
  230. }
  231. void QSWidget::drop_event(GUI::DropEvent& event)
  232. {
  233. event.accept();
  234. if (on_drop)
  235. on_drop(event);
  236. }
  237. void QSWidget::resize_window()
  238. {
  239. if (window()->is_fullscreen())
  240. return;
  241. if (!m_bitmap)
  242. return;
  243. auto new_size = m_bitmap->size();
  244. if (new_size.width() < 300)
  245. new_size.set_width(300);
  246. if (new_size.height() < 200)
  247. new_size.set_height(200);
  248. new_size.set_height(new_size.height() + m_toolbar_height);
  249. window()->resize(new_size);
  250. }
  251. void QSWidget::reset_view()
  252. {
  253. m_pan_origin = { 0, 0 };
  254. set_scale(100);
  255. }
  256. void QSWidget::set_bitmap(const Gfx::Bitmap* bitmap)
  257. {
  258. if (m_bitmap == bitmap)
  259. return;
  260. m_bitmap = bitmap;
  261. update();
  262. }
  263. // Same as ImageWidget::animate(), you probably want to keep any changes in sync
  264. void QSWidget::animate()
  265. {
  266. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  267. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  268. set_bitmap(current_frame.image);
  269. if (current_frame.duration != m_timer->interval()) {
  270. m_timer->restart(current_frame.duration);
  271. }
  272. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  273. ++m_loops_completed;
  274. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  275. m_timer->stop();
  276. }
  277. }
  278. }