From 9cb928b7c41f0f1eb36a4d26ce31d053ea12466d Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 23 Jan 2023 21:57:14 +0400 Subject: [PATCH] Add some helpers for exact dpi painting. --- CMakeLists.txt | 3 +++ ui/dpr/dpr_icon.cpp | 32 +++++++++++++++++++++++++++++ ui/dpr/dpr_icon.h | 20 ++++++++++++++++++ ui/dpr/dpr_image.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 ui/dpr/dpr_icon.cpp create mode 100644 ui/dpr/dpr_icon.h create mode 100644 ui/dpr/dpr_image.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 034941a..a8d90fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/ui/dpr/dpr_icon.cpp b/ui/dpr/dpr_icon.cpp new file mode 100644 index 0000000..6515cd3 --- /dev/null +++ b/ui/dpr/dpr_icon.cpp @@ -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 diff --git a/ui/dpr/dpr_icon.h b/ui/dpr/dpr_icon.h new file mode 100644 index 0000000..4b37793 --- /dev/null +++ b/ui/dpr/dpr_icon.h @@ -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 diff --git a/ui/dpr/dpr_image.h b/ui/dpr/dpr_image.h new file mode 100644 index 0000000..34efd5f --- /dev/null +++ b/ui/dpr/dpr_image.h @@ -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 + +#include +#include + +namespace dpr { + +// Validate(_cache, devicePixelRatioF(), size, [&](QPainter &p, QSize size) { +// ... paint using p ... +// }, (_cacheKey != cacheKey()), Qt::transparent); + +template +void Validate( + QImage &image, + double ratio, + QSize size, + Generator &&generator, + bool force, + std::optional 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(p, size); + } else { + generator(p); + } + } + image.setDevicePixelRatio(setResultRatio ? ratio : 1.); +} + +} // namespace dpr