ui_utility.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "base/unique_qptr.h"
  9. #include <crl/crl.h>
  10. #include <QtCore/QEvent>
  11. #include <QtWidgets/QWidget>
  12. class QPixmap;
  13. class QImage;
  14. class QWheelEvent;
  15. template <typename Object>
  16. class object_ptr;
  17. namespace Ui {
  18. inline constexpr auto kPixelToAngleDelta = 2;
  19. template <typename Widget, typename ...Args>
  20. inline base::unique_qptr<Widget> CreateObject(Args &&...args) {
  21. return base::make_unique_q<Widget>(
  22. nullptr,
  23. std::forward<Args>(args)...);
  24. }
  25. inline void DestroyChild(QWidget *child) {
  26. delete child;
  27. }
  28. template <typename ...Args>
  29. inline auto Connect(Args &&...args) {
  30. return QObject::connect(std::forward<Args>(args)...);
  31. }
  32. template <typename Value>
  33. inline not_null<std::decay_t<Value>*> AttachAsChild(
  34. not_null<QObject*> parent,
  35. Value &&value) {
  36. return WrapAsQObject(parent, std::forward<Value>(value))->value();
  37. }
  38. [[nodiscard]] bool AppInFocus();
  39. [[nodiscard]] bool InFocusChain(not_null<const QWidget*> widget);
  40. template <typename ChildWidget>
  41. inline ChildWidget *AttachParentChild(
  42. not_null<QWidget*> parent,
  43. const object_ptr<ChildWidget> &child) {
  44. if (const auto raw = child.data()) {
  45. raw->setParent(parent);
  46. raw->show();
  47. return raw;
  48. }
  49. return nullptr;
  50. }
  51. void SendPendingMoveResizeEvents(not_null<QWidget*> target);
  52. [[nodiscard]] QPixmap GrabWidget(
  53. not_null<QWidget*> target,
  54. QRect rect = QRect(),
  55. QColor bg = QColor(255, 255, 255, 0));
  56. [[nodiscard]] QImage GrabWidgetToImage(
  57. not_null<QWidget*> target,
  58. QRect rect = QRect(),
  59. QColor bg = QColor(255, 255, 255, 0));
  60. void RenderWidget(
  61. QPainter &painter,
  62. not_null<QWidget*> source,
  63. const QPoint &targetOffset = QPoint(),
  64. const QRegion &sourceRegion = QRegion(),
  65. QWidget::RenderFlags renderFlags
  66. = QWidget::DrawChildren | QWidget::IgnoreMask);
  67. void ForceFullRepaint(not_null<QWidget*> widget);
  68. void ForceFullRepaintSync(not_null<QWidget*> widget);
  69. void PostponeCall(FnMut<void()> &&callable);
  70. template <
  71. typename Guard,
  72. typename Callable,
  73. typename GuardTraits = crl::guard_traits<std::decay_t<Guard>>,
  74. typename = std::enable_if_t<
  75. sizeof(GuardTraits) != crl::details::dependent_zero<GuardTraits>>>
  76. inline void PostponeCall(Guard && object, Callable && callable) {
  77. return PostponeCall(crl::guard(
  78. std::forward<Guard>(object),
  79. std::forward<Callable>(callable)));
  80. }
  81. void SendSynteticMouseEvent(
  82. QWidget *widget,
  83. QEvent::Type type,
  84. Qt::MouseButton button,
  85. const QPoint &globalPoint);
  86. inline void SendSynteticMouseEvent(
  87. QWidget *widget,
  88. QEvent::Type type,
  89. Qt::MouseButton button) {
  90. return SendSynteticMouseEvent(widget, type, button, QCursor::pos());
  91. }
  92. [[nodiscard]] QPixmap PixmapFromImage(QImage &&image);
  93. [[nodiscard]] bool IsContentVisible(
  94. not_null<QWidget*> widget,
  95. const QRect &rect = QRect());
  96. int WheelDirection(not_null<QWheelEvent*> e);
  97. [[nodiscard]] QPoint MapFrom(
  98. not_null<QWidget*> to,
  99. not_null<QWidget*> from,
  100. QPoint point);
  101. [[nodiscard]] QRect MapFrom(
  102. not_null<QWidget*> to,
  103. not_null<QWidget*> from,
  104. QRect rect);
  105. void SetGeometryAndScreen(
  106. not_null<QWidget*> widget,
  107. QRect geometry);
  108. [[nodiscard]] QPointF ScrollDeltaF(
  109. not_null<QWheelEvent*> e,
  110. bool touch = false);
  111. [[nodiscard]] QPoint ScrollDelta(
  112. not_null<QWheelEvent*> e,
  113. bool touch = false);
  114. } // namespace Ui