From 1764bacd4b1ef3542d3c223cec1c1a64f3db5071 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Mon, 19 Feb 2018 23:32:37 +0200 Subject: Move scaleImage() in Utils --- include/Utils.h | 28 ++++++++++++++++++++++++++++ include/dialogs/ImageOverlay.h | 2 -- include/timeline/widgets/ImageItem.h | 1 - 3 files changed, 28 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/Utils.h b/include/Utils.h index 30e592cd..fa825b7c 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -26,4 +26,32 @@ firstChar(const QString &input); //! Get a human readable file size with the appropriate units attached. QString humanReadableFileSize(uint64_t bytes); + +//! Scale down an image to fit to the given width & height limitations. +template +ImageType +scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source) +{ + if (source.isNull()) + return source; + + auto width_ratio = (double)max_width / (double)source.width(); + auto height_ratio = (double)max_height / (double)source.height(); + + auto min_aspect_ratio = std::min(width_ratio, height_ratio); + + int final_width = 0; + int final_height = 0; + + if (min_aspect_ratio > 1) { + final_width = source.width(); + final_height = source.height(); + } else { + final_width = source.width() * min_aspect_ratio; + final_height = source.height() * min_aspect_ratio; + } + + return source.scaled( + final_width, final_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +} } diff --git a/include/dialogs/ImageOverlay.h b/include/dialogs/ImageOverlay.h index 682999ef..b4d42acb 100644 --- a/include/dialogs/ImageOverlay.h +++ b/include/dialogs/ImageOverlay.h @@ -37,8 +37,6 @@ signals: void closing(); private: - void scaleImage(int width, int height); - QPixmap originalImage_; QPixmap image_; diff --git a/include/timeline/widgets/ImageItem.h b/include/timeline/widgets/ImageItem.h index 28a0b461..0069a9fe 100644 --- a/include/timeline/widgets/ImageItem.h +++ b/include/timeline/widgets/ImageItem.h @@ -53,7 +53,6 @@ private slots: void imageDownloaded(const QString &event_id, const QPixmap &img); private: - void scaleImage(); void openUrl(); int max_width_ = 500; -- cgit 1.5.1