diff options
author | Hüseyin ASLITÜRK <asliturk@hotmail.com> | 2020-06-18 16:00:19 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-18 23:18:11 +0200 |
commit | 0001bbf1824c17d048f0ec8dde75eccf91a86f26 (patch) | |
tree | a78cb767fb7efb1a5524d13252590c1128c9cfd6 /Applications | |
parent | 79de1a33cf9b512211c818a23d791e4c38ffbf76 (diff) | |
download | serenity-0001bbf1824c17d048f0ec8dde75eccf91a86f26.zip |
QuickShow:If image is larger then window size resize it to fit the image
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/QuickShow/QSWidget.cpp | 38 | ||||
-rw-r--r-- | Applications/QuickShow/QSWidget.h | 5 | ||||
-rw-r--r-- | Applications/QuickShow/main.cpp | 23 |
3 files changed, 42 insertions, 24 deletions
diff --git a/Applications/QuickShow/QSWidget.cpp b/Applications/QuickShow/QSWidget.cpp index 6732c90085..dccf3a83d2 100644 --- a/Applications/QuickShow/QSWidget.cpp +++ b/Applications/QuickShow/QSWidget.cpp @@ -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) diff --git a/Applications/QuickShow/QSWidget.h b/Applications/QuickShow/QSWidget.h index 432446c806..5128ef2a8b 100644 --- a/Applications/QuickShow/QSWidget.h +++ b/Applications/QuickShow/QSWidget.h @@ -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; diff --git a/Applications/QuickShow/main.cpp b/Applications/QuickShow/main.cpp index d1c1415d59..4b711c3578 100644 --- a/Applications/QuickShow/main.cpp +++ b/Applications/QuickShow/main.cpp @@ -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(); |