From 127c52e39a5455d574fcbffd75753b31897e4877 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Mon, 19 Feb 2018 23:33:11 +0200 Subject: [PATCH] Scale down the preview image to fit in the application window On macOS the modal has some extra space around the main content that might make it unusable with a big enough image. --- include/Utils.h | 2 +- include/timeline/widgets/ImageItem.h | 5 +++++ src/dialogs/PreviewUploadOverlay.cc | 20 +++++++++----------- src/timeline/widgets/ImageItem.cc | 5 +++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/Utils.h b/include/Utils.h index fa825b7c..fba9bf67 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -33,7 +33,7 @@ ImageType scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source) { if (source.isNull()) - return source; + return QPixmap(); auto width_ratio = (double)max_width / (double)source.width(); auto height_ratio = (double)max_height / (double)source.height(); diff --git a/include/timeline/widgets/ImageItem.h b/include/timeline/widgets/ImageItem.h index 0069a9fe..21d91b52 100644 --- a/include/timeline/widgets/ImageItem.h +++ b/include/timeline/widgets/ImageItem.h @@ -26,6 +26,10 @@ #include "MatrixClient.h" +namespace dialogs { +class ImageOverlay; +} + class ImageItem : public QWidget { Q_OBJECT @@ -75,4 +79,5 @@ private: mtx::events::RoomEvent event_; QSharedPointer client_; + QSharedPointer image_dialog_; }; diff --git a/src/dialogs/PreviewUploadOverlay.cc b/src/dialogs/PreviewUploadOverlay.cc index 5b4b9ffa..c535609f 100644 --- a/src/dialogs/PreviewUploadOverlay.cc +++ b/src/dialogs/PreviewUploadOverlay.cc @@ -61,10 +61,9 @@ PreviewUploadOverlay::PreviewUploadOverlay(QWidget *parent) void PreviewUploadOverlay::init() { - auto window = QApplication::activeWindow(); - auto winsize = window->frameGeometry().size(); - auto center = window->frameGeometry().center(); - auto img_size = image_.size(); + auto window = QApplication::activeWindow(); + auto winsize = window->frameGeometry().size(); + auto center = window->frameGeometry().center(); fileName_.setText(QFileInfo{filePath_}.fileName()); @@ -87,13 +86,12 @@ PreviewUploadOverlay::init() if (isImage_) { infoLabel_.setAlignment(Qt::AlignCenter); - // Scale image preview to the size of the current window if it is larger. - if ((img_size.height() * img_size.width()) > (winsize.height() * winsize.width())) { - infoLabel_.setPixmap(image_.scaled(winsize, Qt::KeepAspectRatio)); - } else { - infoLabel_.setPixmap(image_); - move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); - } + const auto maxWidth = winsize.width() * 0.8; + const auto maxHeight = winsize.height() * 0.8; + + // Scale image preview to fit into the application window. + infoLabel_.setPixmap(utils::scaleDown(maxWidth, maxHeight, image_)); + move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); } else { infoLabel_.setAlignment(Qt::AlignLeft); } diff --git a/src/timeline/widgets/ImageItem.cc b/src/timeline/widgets/ImageItem.cc index 591d4e7b..08a057da 100644 --- a/src/timeline/widgets/ImageItem.cc +++ b/src/timeline/widgets/ImageItem.cc @@ -150,8 +150,9 @@ ImageItem::mousePressEvent(QMouseEvent *event) if (textRegion_.contains(event->pos())) { openUrl(); } else { - auto image_dialog = new dialogs::ImageOverlay(image_, this); - image_dialog->show(); + image_dialog_ = + QSharedPointer(new dialogs::ImageOverlay(image_, this)); + image_dialog_->show(); } }