Serenity Operating System
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 * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
6 * Copyright (c) 2022, the SerenityOS developers.
7 *
8 * SPDX-License-Identifier: BSD-2-Clause
9 */
10
11#include "ViewWidget.h"
12#include <AK/LexicalPath.h>
13#include <AK/StringBuilder.h>
14#include <LibCore/DeprecatedFile.h>
15#include <LibCore/Directory.h>
16#include <LibCore/MappedFile.h>
17#include <LibCore/MimeData.h>
18#include <LibCore/Timer.h>
19#include <LibGUI/Application.h>
20#include <LibGUI/MessageBox.h>
21#include <LibGfx/Bitmap.h>
22#include <LibGfx/Orientation.h>
23#include <LibGfx/Palette.h>
24#include <LibImageDecoderClient/Client.h>
25
26namespace ImageViewer {
27
28ViewWidget::ViewWidget()
29 : m_timer(Core::Timer::try_create().release_value_but_fixme_should_propagate_errors())
30{
31 set_fill_with_background_color(false);
32}
33
34void ViewWidget::clear()
35{
36 m_timer->stop();
37 m_decoded_image.clear();
38 m_bitmap = nullptr;
39 if (on_image_change)
40 on_image_change(m_bitmap);
41 set_original_rect({});
42 m_path = {};
43
44 reset_view();
45 update();
46}
47
48void ViewWidget::flip(Gfx::Orientation orientation)
49{
50 m_bitmap = m_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
51 set_original_rect(m_bitmap->rect());
52 set_scale(scale());
53
54 resize_window();
55}
56
57void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
58{
59 m_bitmap = m_bitmap->rotated(rotation_direction).release_value_but_fixme_should_propagate_errors();
60 set_original_rect(m_bitmap->rect());
61 set_scale(scale());
62
63 resize_window();
64}
65
66bool ViewWidget::is_next_available() const
67{
68 if (m_current_index.has_value())
69 return m_current_index.value() + 1 < m_files_in_same_dir.size();
70 return false;
71}
72
73bool ViewWidget::is_previous_available() const
74{
75 if (m_current_index.has_value())
76 return m_current_index.value() > 0;
77 return false;
78}
79
80Vector<DeprecatedString> ViewWidget::load_files_from_directory(DeprecatedString const& path) const
81{
82 Vector<DeprecatedString> files_in_directory;
83
84 auto current_dir = LexicalPath(path).parent().string();
85 // FIXME: Propagate errors
86 (void)Core::Directory::for_each_entry(current_dir, Core::DirIterator::Flags::SkipDots, [&](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
87 auto full_path = LexicalPath::join(directory.path().string(), entry.name).string();
88 if (Gfx::Bitmap::is_path_a_supported_image_format(full_path))
89 files_in_directory.append(full_path);
90 return IterationDecision::Continue;
91 });
92 return files_in_directory;
93}
94
95void ViewWidget::set_path(DeprecatedString const& path)
96{
97 m_path = path;
98 m_files_in_same_dir = load_files_from_directory(path);
99 m_current_index = m_files_in_same_dir.find_first_index(path);
100}
101
102void ViewWidget::navigate(Directions direction)
103{
104 if (!m_current_index.has_value()) {
105 return;
106 }
107
108 auto index = m_current_index.value();
109 if (direction == Directions::Back) {
110 index--;
111 } else if (direction == Directions::Forward) {
112 index++;
113 } else if (direction == Directions::First) {
114 index = 0;
115 } else if (direction == Directions::Last) {
116 index = m_files_in_same_dir.size() - 1;
117 }
118
119 m_current_index = index;
120 this->load_from_file(m_files_in_same_dir.at(index));
121}
122
123void ViewWidget::doubleclick_event(GUI::MouseEvent&)
124{
125 on_doubleclick();
126}
127
128void ViewWidget::paint_event(GUI::PaintEvent& event)
129{
130 Frame::paint_event(event);
131
132 GUI::Painter painter(*this);
133 painter.add_clip_rect(event.rect());
134 painter.add_clip_rect(frame_inner_rect());
135
136 Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
137
138 if (!m_bitmap.is_null())
139 painter.draw_scaled_bitmap(content_rect(), *m_bitmap, m_bitmap->rect(), 1.0f, m_scaling_mode);
140}
141
142void ViewWidget::mousedown_event(GUI::MouseEvent& event)
143{
144 if (event.button() == GUI::MouseButton::Primary)
145 start_panning(event.position());
146 GUI::AbstractZoomPanWidget::mousedown_event(event);
147}
148
149void ViewWidget::mouseup_event(GUI::MouseEvent& event)
150{
151 if (event.button() == GUI::MouseButton::Primary)
152 stop_panning();
153 GUI::AbstractZoomPanWidget::mouseup_event(event);
154}
155
156void ViewWidget::load_from_file(DeprecatedString const& path)
157{
158 auto show_error = [&] {
159 GUI::MessageBox::show(window(), DeprecatedString::formatted("Failed to open {}", path), "Cannot open image"sv, GUI::MessageBox::Type::Error);
160 };
161
162 auto file_or_error = Core::MappedFile::map(path);
163 if (file_or_error.is_error()) {
164 show_error();
165 return;
166 }
167
168 auto& mapped_file = *file_or_error.value();
169
170 // Spawn a new ImageDecoder service process and connect to it.
171 auto client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
172 auto mime_type = Core::guess_mime_type_based_on_filename(path);
173 auto decoded_image_or_error = client->decode_image(mapped_file.bytes(), mime_type);
174 if (!decoded_image_or_error.has_value()) {
175 show_error();
176 return;
177 }
178
179 m_decoded_image = decoded_image_or_error.release_value();
180 m_bitmap = m_decoded_image->frames[0].bitmap;
181 if (m_bitmap.is_null()) {
182 show_error();
183 return;
184 }
185
186 set_original_rect(m_bitmap->rect());
187 if (on_image_change)
188 on_image_change(m_bitmap);
189
190 if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
191 auto const& first_frame = m_decoded_image->frames[0];
192 m_timer->set_interval(first_frame.duration);
193 m_timer->on_timeout = [this] { animate(); };
194 m_timer->start();
195 } else {
196 m_timer->stop();
197 }
198
199 m_path = Core::DeprecatedFile::real_path_for(path);
200 GUI::Application::the()->set_most_recently_open_file(String::from_utf8(path).release_value_but_fixme_should_propagate_errors());
201 reset_view();
202}
203
204void ViewWidget::drag_enter_event(GUI::DragEvent& event)
205{
206 auto const& mime_types = event.mime_types();
207 if (mime_types.contains_slow("text/uri-list"))
208 event.accept();
209}
210
211void ViewWidget::drop_event(GUI::DropEvent& event)
212{
213 event.accept();
214 if (on_drop)
215 on_drop(event);
216}
217
218void ViewWidget::resize_window()
219{
220 if (window()->is_fullscreen() || window()->is_maximized())
221 return;
222
223 auto absolute_bitmap_rect = content_rect();
224 absolute_bitmap_rect.translate_by(window()->rect().top_left());
225 if (window()->rect().contains(absolute_bitmap_rect))
226 return;
227
228 if (!m_bitmap)
229 return;
230
231 auto new_size = content_rect().size();
232
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
238 new_size.set_height(new_size.height() + m_toolbar_height);
239 window()->resize(new_size);
240}
241
242void ViewWidget::set_bitmap(Gfx::Bitmap const* bitmap)
243{
244 if (m_bitmap == bitmap)
245 return;
246 m_bitmap = bitmap;
247 set_original_rect(m_bitmap->rect());
248 update();
249}
250
251// Same as ImageWidget::animate(), you probably want to keep any changes in sync
252void ViewWidget::animate()
253{
254 if (!m_decoded_image.has_value())
255 return;
256
257 m_current_frame_index = (m_current_frame_index + 1) % m_decoded_image->frames.size();
258
259 auto const& current_frame = m_decoded_image->frames[m_current_frame_index];
260 set_bitmap(current_frame.bitmap);
261
262 if ((int)current_frame.duration != m_timer->interval()) {
263 m_timer->restart(current_frame.duration);
264 }
265
266 if (m_current_frame_index == m_decoded_image->frames.size() - 1) {
267 ++m_loops_completed;
268 if (m_loops_completed > 0 && m_loops_completed == m_decoded_image->loop_count) {
269 m_timer->stop();
270 }
271 }
272}
273
274void ViewWidget::set_scaling_mode(Gfx::Painter::ScalingMode scaling_mode)
275{
276 m_scaling_mode = scaling_mode;
277 update();
278}
279
280}