summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-02-19 23:32:37 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-02-19 23:32:37 +0200
commit1764bacd4b1ef3542d3c223cec1c1a64f3db5071 (patch)
treef64ec27e78b0e6f284d3eaa8d807aab237b498ad /include
parentMinor adjustments (diff)
downloadnheko-1764bacd4b1ef3542d3c223cec1c1a64f3db5071.tar.xz
Move scaleImage() in Utils
Diffstat (limited to 'include')
-rw-r--r--include/Utils.h28
-rw-r--r--include/dialogs/ImageOverlay.h2
-rw-r--r--include/timeline/widgets/ImageItem.h1
3 files changed, 28 insertions, 3 deletions
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<class ImageType>
+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;