2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-05-14 20:54:31 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-04-22 23:51:19 +03:00
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
2021-09-03 21:36:13 +03:00
|
|
|
* Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
#include "ViewWidget.h"
|
2021-07-09 02:03:16 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2021-03-17 01:37:44 +03:00
|
|
|
#include <AK/MappedFile.h>
|
2020-04-12 14:35:15 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
2021-07-09 02:05:35 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-03-17 01:37:44 +03:00
|
|
|
#include <LibCore/Timer.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-14 15:18:34 +03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-04-12 17:40:34 +03:00
|
|
|
#include <LibGfx/Orientation.h>
|
2020-04-05 14:13:32 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2021-05-14 20:54:31 +03:00
|
|
|
#include <LibImageDecoderClient/Client.h>
|
2019-06-23 17:35:43 +03:00
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
namespace ImageViewer {
|
|
|
|
|
|
|
|
ViewWidget::ViewWidget()
|
2021-03-17 01:37:44 +03:00
|
|
|
: m_timer(Core::Timer::construct())
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
2020-04-05 14:13:32 +03:00
|
|
|
set_fill_with_background_color(false);
|
2019-06-23 17:35:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
ViewWidget::~ViewWidget()
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::clear()
|
2020-04-12 16:40:05 +03:00
|
|
|
{
|
2021-05-16 17:43:11 +03:00
|
|
|
m_timer->stop();
|
|
|
|
m_decoded_image.clear();
|
2020-04-12 16:40:05 +03:00
|
|
|
m_bitmap = nullptr;
|
2021-05-16 11:01:29 +03:00
|
|
|
if (on_image_change)
|
|
|
|
on_image_change(m_bitmap);
|
2020-04-12 16:40:05 +03:00
|
|
|
m_path = {};
|
|
|
|
|
2021-03-15 22:50:46 +03:00
|
|
|
reset_view();
|
2020-04-12 16:40:05 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::flip(Gfx::Orientation orientation)
|
2020-04-12 16:03:31 +03:00
|
|
|
{
|
|
|
|
m_bitmap = m_bitmap->flipped(orientation);
|
|
|
|
set_scale(m_scale);
|
|
|
|
|
|
|
|
resize_window();
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
|
2020-04-12 16:03:31 +03:00
|
|
|
{
|
|
|
|
m_bitmap = m_bitmap->rotated(rotation_direction);
|
|
|
|
set_scale(m_scale);
|
|
|
|
|
|
|
|
resize_window();
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:36:13 +03:00
|
|
|
bool ViewWidget::is_next_available() const
|
2020-04-12 14:35:15 +03:00
|
|
|
{
|
2021-09-03 21:36:13 +03:00
|
|
|
if (m_current_index.has_value())
|
|
|
|
return m_current_index.value() + 1 < m_files_in_same_dir.size();
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-12 14:35:15 +03:00
|
|
|
|
2021-09-03 21:36:13 +03:00
|
|
|
bool ViewWidget::is_previous_available() const
|
|
|
|
{
|
|
|
|
if (m_current_index.has_value())
|
|
|
|
return m_current_index.value() > 0;
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-12 14:35:15 +03:00
|
|
|
|
2021-09-03 21:36:13 +03:00
|
|
|
Vector<String> ViewWidget::load_files_from_directory(const String& path) const
|
|
|
|
{
|
|
|
|
Vector<String> files_in_directory;
|
|
|
|
|
|
|
|
auto current_dir = LexicalPath(path).parent().string();
|
|
|
|
Core::DirIterator iterator(current_dir, Core::DirIterator::Flags::SkipDots);
|
|
|
|
while (iterator.has_next()) {
|
|
|
|
String file = iterator.next_full_path();
|
|
|
|
if (!Gfx::Bitmap::is_path_a_supported_image_format(file))
|
|
|
|
continue;
|
|
|
|
files_in_directory.append(file);
|
2020-04-12 14:35:15 +03:00
|
|
|
}
|
2021-09-03 21:36:13 +03:00
|
|
|
return files_in_directory;
|
|
|
|
}
|
2020-04-12 14:35:15 +03:00
|
|
|
|
2021-09-03 21:36:13 +03:00
|
|
|
void ViewWidget::set_path(const String& path)
|
|
|
|
{
|
|
|
|
m_path = path;
|
|
|
|
m_files_in_same_dir = load_files_from_directory(path);
|
|
|
|
m_current_index = m_files_in_same_dir.find_first_index(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewWidget::navigate(Directions direction)
|
|
|
|
{
|
|
|
|
if (!m_current_index.has_value()) {
|
2020-04-12 14:35:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:36:13 +03:00
|
|
|
auto index = m_current_index.value();
|
2020-04-12 14:35:15 +03:00
|
|
|
if (direction == Directions::Back) {
|
|
|
|
index--;
|
|
|
|
} else if (direction == Directions::Forward) {
|
|
|
|
index++;
|
|
|
|
} else if (direction == Directions::First) {
|
|
|
|
index = 0;
|
|
|
|
} else if (direction == Directions::Last) {
|
|
|
|
index = m_files_in_same_dir.size() - 1;
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:36:13 +03:00
|
|
|
m_current_index = index;
|
2020-04-12 14:35:15 +03:00
|
|
|
this->load_from_file(m_files_in_same_dir.at(index));
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::set_scale(int scale)
|
2020-04-12 16:24:31 +03:00
|
|
|
{
|
2020-06-18 16:00:19 +03:00
|
|
|
if (m_bitmap.is_null())
|
|
|
|
return;
|
|
|
|
|
2020-04-12 16:24:31 +03:00
|
|
|
if (scale < 10)
|
|
|
|
scale = 10;
|
|
|
|
if (scale > 1000)
|
|
|
|
scale = 1000;
|
|
|
|
|
|
|
|
m_scale = scale;
|
2020-06-18 16:00:19 +03:00
|
|
|
float scale_factor = (float)m_scale / 100.0f;
|
|
|
|
|
|
|
|
Gfx::IntSize new_size;
|
|
|
|
new_size.set_width(m_bitmap->width() * scale_factor);
|
|
|
|
new_size.set_height(m_bitmap->height() * scale_factor);
|
|
|
|
m_bitmap_rect.set_size(new_size);
|
|
|
|
|
|
|
|
if (on_scale_change)
|
2021-07-14 00:34:12 +03:00
|
|
|
on_scale_change(m_scale);
|
2020-06-18 16:00:19 +03:00
|
|
|
|
2020-04-12 16:24:31 +03:00
|
|
|
relayout();
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::relayout()
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
2020-04-05 10:26:29 +03:00
|
|
|
if (m_bitmap.is_null())
|
|
|
|
return;
|
|
|
|
|
2020-06-18 16:00:19 +03:00
|
|
|
Gfx::IntSize new_size = m_bitmap_rect.size();
|
2020-04-05 21:25:14 +03:00
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntPoint new_location;
|
2021-03-15 22:34:19 +03:00
|
|
|
new_location.set_x((width() / 2) - (new_size.width() / 2) - m_pan_origin.x());
|
|
|
|
new_location.set_y((height() / 2) - (new_size.height() / 2) - m_pan_origin.y());
|
2020-04-05 21:25:14 +03:00
|
|
|
m_bitmap_rect.set_location(new_location);
|
|
|
|
|
2019-06-23 17:35:43 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::resize_event(GUI::ResizeEvent& event)
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
|
|
|
relayout();
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Widget::resize_event(event);
|
2019-06-23 17:35:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::doubleclick_event(GUI::MouseEvent&)
|
2020-06-16 10:47:47 +03:00
|
|
|
{
|
|
|
|
on_doubleclick();
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::paint_event(GUI::PaintEvent& event)
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
2020-04-23 20:12:50 +03:00
|
|
|
Frame::paint_event(event);
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Painter painter(*this);
|
2019-06-23 17:35:43 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2020-04-23 20:12:50 +03:00
|
|
|
painter.add_clip_rect(frame_inner_rect());
|
2019-06-23 17:35:43 +03:00
|
|
|
|
2020-09-25 17:33:36 +03:00
|
|
|
Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
|
2020-04-05 21:25:14 +03:00
|
|
|
|
|
|
|
if (!m_bitmap.is_null())
|
|
|
|
painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
|
2019-06-23 17:35:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::mousedown_event(GUI::MouseEvent& event)
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
if (event.button() != GUI::MouseButton::Left)
|
2019-06-23 17:35:43 +03:00
|
|
|
return;
|
2020-04-05 21:25:14 +03:00
|
|
|
m_click_position = event.position();
|
|
|
|
m_saved_pan_origin = m_pan_origin;
|
2019-06-23 17:35:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { }
|
2019-06-23 17:35:43 +03:00
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::mousemove_event(GUI::MouseEvent& event)
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
if (!(event.buttons() & GUI::MouseButton::Left))
|
2019-06-23 17:35:43 +03:00
|
|
|
return;
|
|
|
|
|
2020-04-05 21:25:14 +03:00
|
|
|
auto delta = event.position() - m_click_position;
|
|
|
|
m_pan_origin = m_saved_pan_origin.translated(
|
2021-03-15 22:34:19 +03:00
|
|
|
-delta.x(),
|
|
|
|
-delta.y());
|
2020-04-05 21:25:14 +03:00
|
|
|
|
|
|
|
relayout();
|
2019-06-23 17:35:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::mousewheel_event(GUI::MouseEvent& event)
|
2019-06-23 17:35:43 +03:00
|
|
|
{
|
2020-06-28 01:04:11 +03:00
|
|
|
int new_scale = m_scale - event.wheel_delta() * 10;
|
|
|
|
if (new_scale < 10)
|
|
|
|
new_scale = 10;
|
|
|
|
if (new_scale > 1000)
|
|
|
|
new_scale = 1000;
|
2020-04-05 21:25:14 +03:00
|
|
|
|
2020-06-28 01:04:11 +03:00
|
|
|
if (new_scale == m_scale) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-05 21:25:14 +03:00
|
|
|
|
2020-06-28 01:04:11 +03:00
|
|
|
auto old_scale_factor = (float)m_scale / 100.0f;
|
|
|
|
auto new_scale_factor = (float)new_scale / 100.0f;
|
2020-04-05 21:25:14 +03:00
|
|
|
|
2021-03-15 22:34:19 +03:00
|
|
|
// focus_point is the window position the cursor is pointing to.
|
|
|
|
// The pixel (in image space) the cursor points to is located at
|
|
|
|
// (m_pan_origin + focus_point) / scale_factor.
|
|
|
|
// We want the image after scaling to be panned in such a way that the cursor
|
|
|
|
// will still point to the same image pixel. Basically, we need to solve
|
|
|
|
// (m_pan_origin + focus_point) / old_scale_factor = (new_m_pan_origin + focus_point) / new_scale_factor.
|
2021-04-15 10:36:14 +03:00
|
|
|
Gfx::FloatPoint focus_point {
|
|
|
|
event.x() - width() / 2.0f,
|
|
|
|
event.y() - height() / 2.0f
|
|
|
|
};
|
2020-04-05 21:25:14 +03:00
|
|
|
|
2021-03-15 22:34:19 +03:00
|
|
|
// A little algebra shows that new m_pan_origin equals to:
|
|
|
|
m_pan_origin = (m_pan_origin + focus_point) * (new_scale_factor / old_scale_factor) - focus_point;
|
2020-04-05 21:25:14 +03:00
|
|
|
|
2020-06-28 01:04:11 +03:00
|
|
|
set_scale(new_scale);
|
2019-06-23 17:35:43 +03:00
|
|
|
}
|
2019-12-19 22:39:11 +03:00
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::load_from_file(const String& path)
|
2019-12-19 22:39:11 +03:00
|
|
|
{
|
2021-03-17 01:37:44 +03:00
|
|
|
auto show_error = [&] {
|
2020-10-06 15:32:29 +03:00
|
|
|
GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error);
|
2021-03-17 01:37:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto file_or_error = MappedFile::map(path);
|
|
|
|
if (file_or_error.is_error()) {
|
|
|
|
show_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& mapped_file = *file_or_error.value();
|
2021-05-14 20:54:31 +03:00
|
|
|
|
|
|
|
// Spawn a new ImageDecoder service process and connect to it.
|
|
|
|
auto client = ImageDecoderClient::Client::construct();
|
|
|
|
|
2021-09-06 01:59:52 +03:00
|
|
|
auto decoded_image_or_error = client->decode_image(mapped_file.bytes());
|
2021-05-14 20:54:31 +03:00
|
|
|
if (!decoded_image_or_error.has_value()) {
|
2021-03-17 01:37:44 +03:00
|
|
|
show_error();
|
2020-04-05 21:25:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:54:31 +03:00
|
|
|
m_decoded_image = decoded_image_or_error.release_value();
|
|
|
|
m_bitmap = m_decoded_image->frames[0].bitmap;
|
2021-05-16 11:01:29 +03:00
|
|
|
if (on_image_change)
|
|
|
|
on_image_change(m_bitmap);
|
2021-05-14 20:54:31 +03:00
|
|
|
|
|
|
|
if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
|
|
|
|
const auto& first_frame = m_decoded_image->frames[0];
|
2021-03-17 01:37:44 +03:00
|
|
|
m_timer->set_interval(first_frame.duration);
|
|
|
|
m_timer->on_timeout = [this] { animate(); };
|
|
|
|
m_timer->start();
|
2021-05-16 17:43:11 +03:00
|
|
|
} else {
|
|
|
|
m_timer->stop();
|
2021-03-17 01:37:44 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 02:05:35 +03:00
|
|
|
m_path = Core::File::real_path_for(path);
|
2020-06-18 16:00:19 +03:00
|
|
|
m_scale = -1;
|
2021-03-15 22:50:46 +03:00
|
|
|
reset_view();
|
2019-12-19 22:39:11 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::drop_event(GUI::DropEvent& event)
|
2019-12-19 22:39:11 +03:00
|
|
|
{
|
|
|
|
event.accept();
|
2020-04-05 21:25:14 +03:00
|
|
|
if (on_drop)
|
|
|
|
on_drop(event);
|
2019-12-19 22:39:11 +03:00
|
|
|
}
|
2020-04-12 16:03:31 +03:00
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::resize_window()
|
2020-04-12 16:03:31 +03:00
|
|
|
{
|
2021-07-14 00:43:19 +03:00
|
|
|
if (window()->is_fullscreen() || window()->is_maximized())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto absolute_bitmap_rect = m_bitmap_rect;
|
|
|
|
absolute_bitmap_rect.translate_by(window()->rect().top_left());
|
|
|
|
if (window()->rect().contains(absolute_bitmap_rect))
|
2020-04-12 16:03:31 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_bitmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto new_size = m_bitmap->size();
|
|
|
|
|
|
|
|
if (new_size.width() < 300)
|
|
|
|
new_size.set_width(300);
|
|
|
|
if (new_size.height() < 200)
|
|
|
|
new_size.set_height(200);
|
|
|
|
|
2020-04-12 17:40:34 +03:00
|
|
|
new_size.set_height(new_size.height() + m_toolbar_height);
|
2020-04-12 16:03:31 +03:00
|
|
|
window()->resize(new_size);
|
|
|
|
}
|
2021-03-15 22:50:46 +03:00
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::reset_view()
|
2021-03-15 22:50:46 +03:00
|
|
|
{
|
|
|
|
m_pan_origin = { 0, 0 };
|
|
|
|
set_scale(100);
|
|
|
|
}
|
2021-03-17 01:37:44 +03:00
|
|
|
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::set_bitmap(const Gfx::Bitmap* bitmap)
|
2021-03-17 01:37:44 +03:00
|
|
|
{
|
|
|
|
if (m_bitmap == bitmap)
|
|
|
|
return;
|
|
|
|
m_bitmap = bitmap;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as ImageWidget::animate(), you probably want to keep any changes in sync
|
2021-05-14 19:37:08 +03:00
|
|
|
void ViewWidget::animate()
|
2021-03-17 01:37:44 +03:00
|
|
|
{
|
2021-05-14 20:54:31 +03:00
|
|
|
if (!m_decoded_image.has_value())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_current_frame_index = (m_current_frame_index + 1) % m_decoded_image->frames.size();
|
2021-03-17 01:37:44 +03:00
|
|
|
|
2021-05-14 20:54:31 +03:00
|
|
|
const auto& current_frame = m_decoded_image->frames[m_current_frame_index];
|
|
|
|
set_bitmap(current_frame.bitmap);
|
2021-03-17 01:37:44 +03:00
|
|
|
|
2021-05-14 20:54:31 +03:00
|
|
|
if ((int)current_frame.duration != m_timer->interval()) {
|
2021-03-17 01:37:44 +03:00
|
|
|
m_timer->restart(current_frame.duration);
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:54:31 +03:00
|
|
|
if (m_current_frame_index == m_decoded_image->frames.size() - 1) {
|
2021-03-17 01:37:44 +03:00
|
|
|
++m_loops_completed;
|
2021-05-14 20:54:31 +03:00
|
|
|
if (m_loops_completed > 0 && m_loops_completed == m_decoded_image->loop_count) {
|
2021-03-17 01:37:44 +03:00
|
|
|
m_timer->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-14 19:37:08 +03:00
|
|
|
|
|
|
|
}
|