Move scaleImage() in Utils
This commit is contained in:
parent
8b139c32a3
commit
1764bacd4b
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -37,8 +37,6 @@ signals:
|
||||
void closing();
|
||||
|
||||
private:
|
||||
void scaleImage(int width, int height);
|
||||
|
||||
QPixmap originalImage_;
|
||||
QPixmap image_;
|
||||
|
||||
|
@ -53,7 +53,6 @@ private slots:
|
||||
void imageDownloaded(const QString &event_id, const QPixmap &img);
|
||||
|
||||
private:
|
||||
void scaleImage();
|
||||
void openUrl();
|
||||
|
||||
int max_width_ = 500;
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <QDesktopWidget>
|
||||
#include <QPainter>
|
||||
|
||||
#include "Utils.h"
|
||||
#include "dialogs/ImageOverlay.h"
|
||||
|
||||
using namespace dialogs;
|
||||
@ -47,33 +48,6 @@ ImageOverlay::ImageOverlay(QPixmap image, QWidget *parent)
|
||||
raise();
|
||||
}
|
||||
|
||||
// TODO: Move this into Utils
|
||||
void
|
||||
ImageOverlay::scaleImage(int max_width, int max_height)
|
||||
{
|
||||
if (originalImage_.isNull())
|
||||
return;
|
||||
|
||||
auto width_ratio = (double)max_width / (double)originalImage_.width();
|
||||
auto height_ratio = (double)max_height / (double)originalImage_.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 = originalImage_.width();
|
||||
final_height = originalImage_.height();
|
||||
} else {
|
||||
final_width = originalImage_.width() * min_aspect_ratio;
|
||||
final_height = originalImage_.height() * min_aspect_ratio;
|
||||
}
|
||||
|
||||
image_ = originalImage_.scaled(
|
||||
final_width, final_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
void
|
||||
ImageOverlay::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
@ -93,7 +67,7 @@ ImageOverlay::paintEvent(QPaintEvent *event)
|
||||
int max_width = screen_.width() - 2 * outer_margin;
|
||||
int max_height = screen_.height();
|
||||
|
||||
scaleImage(max_width, max_height);
|
||||
image_ = utils::scaleDown<QPixmap>(max_width, max_height, originalImage_);
|
||||
|
||||
int diff_x = max_width - image_.width();
|
||||
int diff_y = max_height - image_.height();
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <QPixmap>
|
||||
|
||||
#include "Config.h"
|
||||
#include "Utils.h"
|
||||
#include "dialogs/ImageOverlay.h"
|
||||
#include "timeline/widgets/ImageItem.h"
|
||||
|
||||
@ -113,30 +114,6 @@ ImageItem::openUrl()
|
||||
qWarning() << "Could not open url" << url_.toString();
|
||||
}
|
||||
|
||||
void
|
||||
ImageItem::scaleImage()
|
||||
{
|
||||
if (image_.isNull())
|
||||
return;
|
||||
|
||||
auto width_ratio = (double)max_width_ / (double)image_.width();
|
||||
auto height_ratio = (double)max_height_ / (double)image_.height();
|
||||
|
||||
auto min_aspect_ratio = std::min(width_ratio, height_ratio);
|
||||
|
||||
if (min_aspect_ratio > 1) {
|
||||
width_ = image_.width();
|
||||
height_ = image_.height();
|
||||
} else {
|
||||
width_ = image_.width() * min_aspect_ratio;
|
||||
height_ = image_.height() * min_aspect_ratio;
|
||||
}
|
||||
|
||||
setFixedSize(width_, height_);
|
||||
scaled_image_ =
|
||||
image_.scaled(width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
QSize
|
||||
ImageItem::sizeHint() const
|
||||
{
|
||||
@ -149,8 +126,13 @@ ImageItem::sizeHint() const
|
||||
void
|
||||
ImageItem::setImage(const QPixmap &image)
|
||||
{
|
||||
image_ = image;
|
||||
scaleImage();
|
||||
image_ = image;
|
||||
scaled_image_ = utils::scaleDown<QPixmap>(max_width_, max_height_, image_);
|
||||
|
||||
width_ = scaled_image_.width();
|
||||
height_ = scaled_image_.height();
|
||||
|
||||
setFixedSize(width_, height_);
|
||||
update();
|
||||
}
|
||||
|
||||
@ -176,9 +158,15 @@ ImageItem::mousePressEvent(QMouseEvent *event)
|
||||
void
|
||||
ImageItem::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
if (!image_)
|
||||
return QWidget::resizeEvent(event);
|
||||
|
||||
scaleImage();
|
||||
scaled_image_ = utils::scaleDown<QPixmap>(max_width_, max_height_, image_);
|
||||
|
||||
width_ = scaled_image_.width();
|
||||
height_ = scaled_image_.height();
|
||||
|
||||
setFixedSize(width_, height_);
|
||||
}
|
||||
|
||||
void
|
||||
|
Loading…
Reference in New Issue
Block a user