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.
This commit is contained in:
Konstantinos Sideris 2018-02-19 23:33:11 +02:00
parent 1764bacd4b
commit 127c52e39a
4 changed files with 18 additions and 14 deletions

View file

@ -33,7 +33,7 @@ ImageType
scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source) scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source)
{ {
if (source.isNull()) if (source.isNull())
return source; return QPixmap();
auto width_ratio = (double)max_width / (double)source.width(); auto width_ratio = (double)max_width / (double)source.width();
auto height_ratio = (double)max_height / (double)source.height(); auto height_ratio = (double)max_height / (double)source.height();

View file

@ -26,6 +26,10 @@
#include "MatrixClient.h" #include "MatrixClient.h"
namespace dialogs {
class ImageOverlay;
}
class ImageItem : public QWidget class ImageItem : public QWidget
{ {
Q_OBJECT Q_OBJECT
@ -75,4 +79,5 @@ private:
mtx::events::RoomEvent<mtx::events::msg::Image> event_; mtx::events::RoomEvent<mtx::events::msg::Image> event_;
QSharedPointer<MatrixClient> client_; QSharedPointer<MatrixClient> client_;
QSharedPointer<dialogs::ImageOverlay> image_dialog_;
}; };

View file

@ -64,7 +64,6 @@ PreviewUploadOverlay::init()
auto window = QApplication::activeWindow(); auto window = QApplication::activeWindow();
auto winsize = window->frameGeometry().size(); auto winsize = window->frameGeometry().size();
auto center = window->frameGeometry().center(); auto center = window->frameGeometry().center();
auto img_size = image_.size();
fileName_.setText(QFileInfo{filePath_}.fileName()); fileName_.setText(QFileInfo{filePath_}.fileName());
@ -87,13 +86,12 @@ PreviewUploadOverlay::init()
if (isImage_) { if (isImage_) {
infoLabel_.setAlignment(Qt::AlignCenter); infoLabel_.setAlignment(Qt::AlignCenter);
// Scale image preview to the size of the current window if it is larger. const auto maxWidth = winsize.width() * 0.8;
if ((img_size.height() * img_size.width()) > (winsize.height() * winsize.width())) { const auto maxHeight = winsize.height() * 0.8;
infoLabel_.setPixmap(image_.scaled(winsize, Qt::KeepAspectRatio));
} else { // Scale image preview to fit into the application window.
infoLabel_.setPixmap(image_); infoLabel_.setPixmap(utils::scaleDown<QPixmap>(maxWidth, maxHeight, image_));
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
}
} else { } else {
infoLabel_.setAlignment(Qt::AlignLeft); infoLabel_.setAlignment(Qt::AlignLeft);
} }

View file

@ -150,8 +150,9 @@ ImageItem::mousePressEvent(QMouseEvent *event)
if (textRegion_.contains(event->pos())) { if (textRegion_.contains(event->pos())) {
openUrl(); openUrl();
} else { } else {
auto image_dialog = new dialogs::ImageOverlay(image_, this); image_dialog_ =
image_dialog->show(); QSharedPointer<dialogs::ImageOverlay>(new dialogs::ImageOverlay(image_, this));
image_dialog_->show();
} }
} }