mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 11:00:48 +03:00
parent
1f50d13b9f
commit
b5b5faa5ec
5 changed files with 37 additions and 31 deletions
|
@ -223,11 +223,41 @@ utils::scaleImageToPixmap(const QImage &img, int size)
|
|||
return QPixmap();
|
||||
|
||||
const double sz =
|
||||
ceil(QApplication::desktop()->screen()->devicePixelRatioF() * (double)size);
|
||||
std::ceil(QApplication::desktop()->screen()->devicePixelRatioF() * (double)size);
|
||||
return QPixmap::fromImage(
|
||||
img.scaled(sz, sz, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
|
||||
}
|
||||
|
||||
QPixmap
|
||||
utils::scaleDown(uint64_t maxWidth, uint64_t maxHeight, const QPixmap &source)
|
||||
{
|
||||
if (source.isNull())
|
||||
return QPixmap();
|
||||
|
||||
const double pixelRatio = QApplication::desktop()->screen()->devicePixelRatioF();
|
||||
|
||||
// Take into account the scale factor of the screen.
|
||||
maxWidth = std::ceil(pixelRatio * (double)maxWidth);
|
||||
maxHeight = std::ceil(pixelRatio * (double)maxHeight);
|
||||
|
||||
const double widthRatio = (double)maxWidth / (double)source.width();
|
||||
const double heightRatio = (double)maxHeight / (double)source.height();
|
||||
const double minAspectRatio = std::min(widthRatio, heightRatio);
|
||||
|
||||
// Size of the output image.
|
||||
int w, h = 0;
|
||||
|
||||
if (minAspectRatio > 1) {
|
||||
w = source.width();
|
||||
h = source.height();
|
||||
} else {
|
||||
w = source.width() * minAspectRatio;
|
||||
h = source.height() * minAspectRatio;
|
||||
}
|
||||
|
||||
return source.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
QString
|
||||
utils::mxcToHttp(const QUrl &url, const QString &server, int port)
|
||||
{
|
||||
|
|
28
src/Utils.h
28
src/Utils.h
|
@ -116,32 +116,8 @@ createDescriptionInfo(const Event &event, const QString &localUser, const QStrin
|
|||
}
|
||||
|
||||
//! 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 QPixmap();
|
||||
|
||||
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);
|
||||
}
|
||||
QPixmap
|
||||
scaleDown(uint64_t maxWidth, uint64_t maxHeight, const QPixmap &source);
|
||||
|
||||
//! Delete items in a container based on a predicate.
|
||||
template<typename ContainerT, typename PredicateT>
|
||||
|
|
|
@ -68,7 +68,7 @@ ImageOverlay::paintEvent(QPaintEvent *event)
|
|||
int max_width = screen_.width() - 2 * outer_margin;
|
||||
int max_height = screen_.height();
|
||||
|
||||
image_ = utils::scaleDown<QPixmap>(max_width, max_height, originalImage_);
|
||||
image_ = utils::scaleDown(max_width, max_height, originalImage_);
|
||||
|
||||
int diff_x = max_width - image_.width();
|
||||
int diff_y = max_height - image_.height();
|
||||
|
|
|
@ -108,7 +108,7 @@ PreviewUploadOverlay::init()
|
|||
const auto maxHeight = winsize.height() * 0.8;
|
||||
|
||||
// Scale image preview to fit into the application window.
|
||||
infoLabel_.setPixmap(utils::scaleDown<QPixmap>(maxWidth, maxHeight, image_));
|
||||
infoLabel_.setPixmap(utils::scaleDown(maxWidth, maxHeight, image_));
|
||||
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
|
||||
} else {
|
||||
infoLabel_.setAlignment(Qt::AlignLeft);
|
||||
|
|
|
@ -126,7 +126,7 @@ void
|
|||
ImageItem::setImage(const QPixmap &image)
|
||||
{
|
||||
image_ = image;
|
||||
scaled_image_ = utils::scaleDown<QPixmap>(max_width_, max_height_, image_);
|
||||
scaled_image_ = utils::scaleDown(max_width_, max_height_, image_);
|
||||
|
||||
width_ = scaled_image_.width();
|
||||
height_ = scaled_image_.height();
|
||||
|
@ -165,7 +165,7 @@ ImageItem::resizeEvent(QResizeEvent *event)
|
|||
if (!image_)
|
||||
return QWidget::resizeEvent(event);
|
||||
|
||||
scaled_image_ = utils::scaleDown<QPixmap>(max_width_, max_height_, image_);
|
||||
scaled_image_ = utils::scaleDown(max_width_, max_height_, image_);
|
||||
|
||||
width_ = scaled_image_.width();
|
||||
height_ = scaled_image_.height();
|
||||
|
|
Loading…
Reference in a new issue