dpr_image.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <rpl/details/callable.h>
  9. #include <QtGui/QImage>
  10. #include <QtGui/QPainter>
  11. namespace dpr {
  12. // Validate(_cache, devicePixelRatioF(), size, [&](QPainter &p, QSize size) {
  13. // ... paint using p ...
  14. // }, (_cacheKey != cacheKey()), Qt::transparent);
  15. template <typename Generator>
  16. void Validate(
  17. QImage &image,
  18. double ratio,
  19. QSize size,
  20. Generator &&generator,
  21. bool force,
  22. std::optional<QColor> fill = {},
  23. bool setResultRatio = true) {
  24. size *= ratio;
  25. const auto sizeChanged = (image.size() != size);
  26. if (sizeChanged || force) {
  27. if (sizeChanged) {
  28. image = QImage(size, QImage::Format_ARGB32_Premultiplied);
  29. }
  30. if (fill) {
  31. image.fill(*fill);
  32. }
  33. image.setDevicePixelRatio(1.);
  34. auto p = QPainter(&image);
  35. using namespace rpl::details;
  36. if constexpr (is_callable_plain_v<Generator, QPainter&, QSize>) {
  37. generator(p, size);
  38. } else {
  39. generator(p);
  40. }
  41. }
  42. image.setDevicePixelRatio(setResultRatio ? ratio : 1.);
  43. }
  44. } // namespace dpr