Add some helpers for exact dpi painting.

This commit is contained in:
John Preston 2023-01-23 21:57:14 +04:00
parent 133acab68f
commit 9cb928b7c4
4 changed files with 105 additions and 0 deletions

View file

@ -35,6 +35,9 @@ PRIVATE
qt_conf/win.qrc
ui/dpr/dpr_icon.cpp
ui/dpr/dpr_icon.h
ui/dpr/dpr_image.h
ui/effects/animation_value.cpp
ui/effects/animation_value.h
ui/effects/animation_value_f.h

32
ui/dpr/dpr_icon.cpp Normal file
View file

@ -0,0 +1,32 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "ui/dpr/dpr_icon.h"
namespace dpr {
QImage IconFrame(
const style::icon &icon,
const QColor &color,
double ratio) {
const auto scale = style::Scale() * ratio;
const auto use = (scale > 200. || style::DevicePixelRatio() > 2)
? (300 / style::DevicePixelRatio())
: (scale > 100.)
? (200 / style::DevicePixelRatio())
: (100 / style::DevicePixelRatio());
auto image = icon.instance(color, use);
image.setDevicePixelRatio(1.);
const auto desired = icon.size() * ratio;
return (image.size() == desired)
? image
: image.scaled(
desired,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
}
} // namespace dpr

20
ui/dpr/dpr_icon.h Normal file
View file

@ -0,0 +1,20 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#pragma once
#include "ui/style/style_core.h"
class QImage;
namespace dpr {
[[nodiscard]] QImage IconFrame(
const style::icon &icon,
const QColor &color,
double ratio);
} // namespace dpr

50
ui/dpr/dpr_image.h Normal file
View file

@ -0,0 +1,50 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#pragma once
#include <rpl/details/callable.h>
#include <QtGui/QImage>
#include <QtGui/QPainter>
namespace dpr {
// Validate(_cache, devicePixelRatioF(), size, [&](QPainter &p, QSize size) {
// ... paint using p ...
// }, (_cacheKey != cacheKey()), Qt::transparent);
template <typename Generator>
void Validate(
QImage &image,
double ratio,
QSize size,
Generator &&generator,
bool force,
std::optional<QColor> fill = {},
bool setResultRatio = true) {
size *= ratio;
const auto sizeChanged = (image.size() != size);
if (sizeChanged || force) {
if (sizeChanged) {
image = QImage(size, QImage::Format_ARGB32_Premultiplied);
}
if (fill) {
image.fill(*fill);
}
image.setDevicePixelRatio(1.);
auto p = QPainter(&image);
using namespace rpl::details;
if constexpr (is_callable_plain_v<Generator, QPainter&, QSize>) {
generator(p, size);
} else {
generator(p);
}
}
image.setDevicePixelRatio(setResultRatio ? ratio : 1.);
}
} // namespace dpr