1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:27:43 +00:00

ImageViewer: Rename QSWidget => ImageViewer::ViewWidget

This commit is contained in:
Andreas Kling 2021-05-14 18:37:08 +02:00
parent 58d73ea36c
commit 3168a4afe8
5 changed files with 44 additions and 34 deletions

View file

@ -1,6 +1,6 @@
set(SOURCES set(SOURCES
main.cpp main.cpp
QSWidget.cpp ViewWidget.cpp
) )
serenity_app(ImageViewer ICON filetype-image) serenity_app(ImageViewer ICON filetype-image)

View file

@ -5,7 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "QSWidget.h" #include "ViewWidget.h"
#include <AK/MappedFile.h> #include <AK/MappedFile.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibCore/DirIterator.h> #include <LibCore/DirIterator.h>
@ -16,17 +16,19 @@
#include <LibGfx/Orientation.h> #include <LibGfx/Orientation.h>
#include <LibGfx/Palette.h> #include <LibGfx/Palette.h>
QSWidget::QSWidget() namespace ImageViewer {
ViewWidget::ViewWidget()
: m_timer(Core::Timer::construct()) : m_timer(Core::Timer::construct())
{ {
set_fill_with_background_color(false); set_fill_with_background_color(false);
} }
QSWidget::~QSWidget() ViewWidget::~ViewWidget()
{ {
} }
void QSWidget::clear() void ViewWidget::clear()
{ {
m_bitmap = nullptr; m_bitmap = nullptr;
m_path = {}; m_path = {};
@ -35,7 +37,7 @@ void QSWidget::clear()
update(); update();
} }
void QSWidget::flip(Gfx::Orientation orientation) void ViewWidget::flip(Gfx::Orientation orientation)
{ {
m_bitmap = m_bitmap->flipped(orientation); m_bitmap = m_bitmap->flipped(orientation);
set_scale(m_scale); set_scale(m_scale);
@ -43,7 +45,7 @@ void QSWidget::flip(Gfx::Orientation orientation)
resize_window(); resize_window();
} }
void QSWidget::rotate(Gfx::RotationDirection rotation_direction) void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
{ {
m_bitmap = m_bitmap->rotated(rotation_direction); m_bitmap = m_bitmap->rotated(rotation_direction);
set_scale(m_scale); set_scale(m_scale);
@ -51,7 +53,7 @@ void QSWidget::rotate(Gfx::RotationDirection rotation_direction)
resize_window(); resize_window();
} }
void QSWidget::navigate(Directions direction) void ViewWidget::navigate(Directions direction)
{ {
if (m_path == nullptr) if (m_path == nullptr)
return; return;
@ -102,7 +104,7 @@ void QSWidget::navigate(Directions direction)
this->load_from_file(m_files_in_same_dir.at(index)); this->load_from_file(m_files_in_same_dir.at(index));
} }
void QSWidget::set_scale(int scale) void ViewWidget::set_scale(int scale)
{ {
if (m_bitmap.is_null()) if (m_bitmap.is_null())
return; return;
@ -131,7 +133,7 @@ void QSWidget::set_scale(int scale)
relayout(); relayout();
} }
void QSWidget::relayout() void ViewWidget::relayout()
{ {
if (m_bitmap.is_null()) if (m_bitmap.is_null())
return; return;
@ -146,18 +148,18 @@ void QSWidget::relayout()
update(); update();
} }
void QSWidget::resize_event(GUI::ResizeEvent& event) void ViewWidget::resize_event(GUI::ResizeEvent& event)
{ {
relayout(); relayout();
GUI::Widget::resize_event(event); GUI::Widget::resize_event(event);
} }
void QSWidget::doubleclick_event(GUI::MouseEvent&) void ViewWidget::doubleclick_event(GUI::MouseEvent&)
{ {
on_doubleclick(); on_doubleclick();
} }
void QSWidget::paint_event(GUI::PaintEvent& event) void ViewWidget::paint_event(GUI::PaintEvent& event)
{ {
Frame::paint_event(event); Frame::paint_event(event);
@ -171,7 +173,7 @@ void QSWidget::paint_event(GUI::PaintEvent& event)
painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect()); painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
} }
void QSWidget::mousedown_event(GUI::MouseEvent& event) void ViewWidget::mousedown_event(GUI::MouseEvent& event)
{ {
if (event.button() != GUI::MouseButton::Left) if (event.button() != GUI::MouseButton::Left)
return; return;
@ -179,9 +181,9 @@ void QSWidget::mousedown_event(GUI::MouseEvent& event)
m_saved_pan_origin = m_pan_origin; m_saved_pan_origin = m_pan_origin;
} }
void QSWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { } void ViewWidget::mouseup_event([[maybe_unused]] GUI::MouseEvent& event) { }
void QSWidget::mousemove_event(GUI::MouseEvent& event) void ViewWidget::mousemove_event(GUI::MouseEvent& event)
{ {
if (!(event.buttons() & GUI::MouseButton::Left)) if (!(event.buttons() & GUI::MouseButton::Left))
return; return;
@ -194,7 +196,7 @@ void QSWidget::mousemove_event(GUI::MouseEvent& event)
relayout(); relayout();
} }
void QSWidget::mousewheel_event(GUI::MouseEvent& event) void ViewWidget::mousewheel_event(GUI::MouseEvent& event)
{ {
int new_scale = m_scale - event.wheel_delta() * 10; int new_scale = m_scale - event.wheel_delta() * 10;
if (new_scale < 10) if (new_scale < 10)
@ -226,7 +228,7 @@ void QSWidget::mousewheel_event(GUI::MouseEvent& event)
set_scale(new_scale); set_scale(new_scale);
} }
void QSWidget::load_from_file(const String& path) void ViewWidget::load_from_file(const String& path)
{ {
auto show_error = [&] { auto show_error = [&] {
GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error); GUI::MessageBox::show(window(), String::formatted("Failed to open {}", path), "Cannot open image", GUI::MessageBox::Type::Error);
@ -259,14 +261,14 @@ void QSWidget::load_from_file(const String& path)
reset_view(); reset_view();
} }
void QSWidget::drop_event(GUI::DropEvent& event) void ViewWidget::drop_event(GUI::DropEvent& event)
{ {
event.accept(); event.accept();
if (on_drop) if (on_drop)
on_drop(event); on_drop(event);
} }
void QSWidget::resize_window() void ViewWidget::resize_window()
{ {
if (window()->is_fullscreen()) if (window()->is_fullscreen())
return; return;
@ -285,13 +287,13 @@ void QSWidget::resize_window()
window()->resize(new_size); window()->resize(new_size);
} }
void QSWidget::reset_view() void ViewWidget::reset_view()
{ {
m_pan_origin = { 0, 0 }; m_pan_origin = { 0, 0 };
set_scale(100); set_scale(100);
} }
void QSWidget::set_bitmap(const Gfx::Bitmap* bitmap) void ViewWidget::set_bitmap(const Gfx::Bitmap* bitmap)
{ {
if (m_bitmap == bitmap) if (m_bitmap == bitmap)
return; return;
@ -300,7 +302,7 @@ void QSWidget::set_bitmap(const Gfx::Bitmap* bitmap)
} }
// Same as ImageWidget::animate(), you probably want to keep any changes in sync // Same as ImageWidget::animate(), you probably want to keep any changes in sync
void QSWidget::animate() void ViewWidget::animate()
{ {
m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count(); m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
@ -318,3 +320,5 @@ void QSWidget::animate()
} }
} }
} }
}

View file

@ -13,8 +13,10 @@
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
#include <LibGfx/Point.h> #include <LibGfx/Point.h>
class QSWidget final : public GUI::Frame { namespace ImageViewer {
C_OBJECT(QSWidget)
class ViewWidget final : public GUI::Frame {
C_OBJECT(ViewWidget)
public: public:
enum Directions { enum Directions {
First, First,
@ -23,7 +25,7 @@ public:
Last Last
}; };
virtual ~QSWidget() override; virtual ~ViewWidget() override;
const Gfx::Bitmap* bitmap() const { return m_bitmap.ptr(); } const Gfx::Bitmap* bitmap() const { return m_bitmap.ptr(); }
const String& path() const { return m_path; } const String& path() const { return m_path; }
@ -43,7 +45,7 @@ public:
Function<void(const GUI::DropEvent&)> on_drop; Function<void(const GUI::DropEvent&)> on_drop;
private: private:
QSWidget(); ViewWidget();
virtual void doubleclick_event(GUI::MouseEvent&) override; virtual void doubleclick_event(GUI::MouseEvent&) override;
virtual void paint_event(GUI::PaintEvent&) override; virtual void paint_event(GUI::PaintEvent&) override;
virtual void resize_event(GUI::ResizeEvent&) override; virtual void resize_event(GUI::ResizeEvent&) override;
@ -76,3 +78,5 @@ private:
Gfx::FloatPoint m_saved_pan_origin; Gfx::FloatPoint m_saved_pan_origin;
Vector<String> m_files_in_same_dir; Vector<String> m_files_in_same_dir;
}; };
}

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "QSWidget.h" #include "ViewWidget.h"
#include <AK/URL.h> #include <AK/URL.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/MimeData.h> #include <LibCore/MimeData.h>
@ -29,6 +29,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
using namespace ImageViewer;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (pledge("stdio recvfd sendfd rpath wpath cpath unix thread", nullptr) < 0) { if (pledge("stdio recvfd sendfd rpath wpath cpath unix thread", nullptr) < 0) {
@ -75,7 +77,7 @@ int main(int argc, char** argv)
auto& toolbar_container = root_widget.add<GUI::ToolbarContainer>(); auto& toolbar_container = root_widget.add<GUI::ToolbarContainer>();
auto& main_toolbar = toolbar_container.add<GUI::Toolbar>(); auto& main_toolbar = toolbar_container.add<GUI::Toolbar>();
auto& widget = root_widget.add<QSWidget>(); auto& widget = root_widget.add<ViewWidget>();
widget.on_scale_change = [&](int scale, Gfx::IntRect rect) { widget.on_scale_change = [&](int scale, Gfx::IntRect rect) {
if (!widget.bitmap()) { if (!widget.bitmap()) {
window->set_title("Image Viewer"); window->set_title("Image Viewer");
@ -185,22 +187,22 @@ int main(int argc, char** argv)
auto go_first_action = GUI::Action::create("Go to &First", { Mod_None, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-first.png"), auto go_first_action = GUI::Action::create("Go to &First", { Mod_None, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-first.png"),
[&](auto&) { [&](auto&) {
widget.navigate(QSWidget::Directions::First); widget.navigate(ViewWidget::Directions::First);
}); });
auto go_back_action = GUI::Action::create("Go &Back", { Mod_None, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), auto go_back_action = GUI::Action::create("Go &Back", { Mod_None, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"),
[&](auto&) { [&](auto&) {
widget.navigate(QSWidget::Directions::Back); widget.navigate(ViewWidget::Directions::Back);
}); });
auto go_forward_action = GUI::Action::create("Go &Forward", { Mod_None, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), auto go_forward_action = GUI::Action::create("Go &Forward", { Mod_None, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"),
[&](auto&) { [&](auto&) {
widget.navigate(QSWidget::Directions::Forward); widget.navigate(ViewWidget::Directions::Forward);
}); });
auto go_last_action = GUI::Action::create("Go to &Last", { Mod_None, Key_End }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-last.png"), auto go_last_action = GUI::Action::create("Go to &Last", { Mod_None, Key_End }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-last.png"),
[&](auto&) { [&](auto&) {
widget.navigate(QSWidget::Directions::Last); widget.navigate(ViewWidget::Directions::Last);
}); });
auto full_sceen_action = GUI::CommonActions::make_fullscreen_action( auto full_sceen_action = GUI::CommonActions::make_fullscreen_action(

View file

@ -51,7 +51,7 @@ void ImageWidget::set_auto_resize(bool value)
set_fixed_size(m_bitmap->size()); set_fixed_size(m_bitmap->size());
} }
// Same as QSWidget::animate(), you probably want to keep any changes in sync // Same as ImageViewer::ViewWidget::animate(), you probably want to keep any changes in sync
void ImageWidget::animate() void ImageWidget::animate()
{ {
m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count(); m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();