QuickShow:If image is larger then window size resize it to fit the image

This commit is contained in:
Hüseyin ASLITÜRK 2020-06-18 16:00:19 +03:00 committed by Andreas Kling
parent 79de1a33cf
commit 0001bbf182
Notes: sideshowbarker 2024-07-19 05:33:22 +09:00
3 changed files with 42 additions and 24 deletions

View file

@ -48,7 +48,7 @@ void QSWidget::clear()
m_bitmap = nullptr;
m_path = {};
on_scale_change(100);
set_scale(100);
update();
}
@ -121,6 +121,13 @@ void QSWidget::navigate(Directions direction)
void QSWidget::set_scale(int scale)
{
if (m_bitmap.is_null())
return;
if (m_scale == scale) {
update();
return;
}
if (scale < 10)
scale = 10;
@ -131,6 +138,16 @@ void QSWidget::set_scale(int scale)
m_pan_origin = { 0, 0 };
m_scale = scale;
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)
on_scale_change(m_scale, m_bitmap_rect);
relayout();
}
@ -140,20 +157,13 @@ void QSWidget::relayout()
return;
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);
Gfx::IntSize new_size = m_bitmap_rect.size();
Gfx::IntPoint new_location;
new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x() * scale_factor));
new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y() * scale_factor));
m_bitmap_rect.set_location(new_location);
if (on_scale_change)
on_scale_change(m_scale);
update();
}
@ -245,14 +255,8 @@ void QSWidget::load_from_file(const String& path)
m_path = path;
m_bitmap = bitmap;
m_scale = 100;
m_pan_origin = { 0, 0 };
resize_window();
if (on_scale_change)
on_scale_change(m_scale);
relayout();
m_scale = -1;
set_scale(100);
}
void QSWidget::drop_event(GUI::DropEvent& event)

View file

@ -49,6 +49,7 @@ public:
void set_scale(int);
int scale() { return m_scale; }
void set_toolbar_height(int height) { m_toolbar_height = height; }
int toolbar_height() { return m_toolbar_height; }
void clear();
void flip(Gfx::Orientation);
@ -56,7 +57,7 @@ public:
void navigate(Directions);
void load_from_file(const String&);
Function<void(int)> on_scale_change;
Function<void(int, Gfx::IntRect)> on_scale_change;
Function<void()> on_doubleclick;
Function<void(const GUI::DropEvent&)> on_drop;
@ -79,7 +80,7 @@ private:
int m_toolbar_height { 28 };
Gfx::IntRect m_bitmap_rect;
int m_scale { 100 };
int m_scale { -1 };
Gfx::FloatPoint m_pan_origin;
Gfx::IntPoint m_click_position;

View file

@ -43,6 +43,7 @@
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Palette.h>
#include <LibGfx/Rect.h>
#include <stdio.h>
#include <string.h>
@ -69,6 +70,7 @@ int main(int argc, char** argv)
window->set_double_buffering_enabled(true);
window->set_rect(200, 200, 300, 200);
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-image.png"));
window->set_title("QuickShow");
auto& root_widget = window->set_main_widget<GUI::Widget>();
root_widget.set_fill_with_background_color(true);
@ -79,11 +81,23 @@ int main(int argc, char** argv)
auto& main_toolbar = toolbar_container.add<GUI::ToolBar>();
auto& widget = root_widget.add<QSWidget>();
widget.on_scale_change = [&](int scale) {
if (widget.bitmap())
window->set_title(String::format("%s %s %d%% - QuickShow", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
else
widget.on_scale_change = [&](int scale, Gfx::IntRect rect) {
if (!widget.bitmap()) {
window->set_title("QuickShow");
return;
}
window->set_title(String::format("%s %s %d%% - QuickShow", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
if (window->is_fullscreen())
return;
if (window->is_maximized())
return;
auto w = max(window->width(), rect.width() + 4);
auto h = max(window->height(), rect.height() + widget.toolbar_height() + 6);
window->resize(w, h);
};
widget.on_drop = [&](auto& event) {
window->move_to_front();
@ -281,7 +295,6 @@ int main(int argc, char** argv)
if (path != nullptr) {
widget.load_from_file(path);
}
widget.on_scale_change(100);
window->show();