From 12b8b3804c1b458c124ca150de4320e3fda5035e Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 7 Sep 2022 12:50:59 +0400 Subject: [PATCH] Add Ui::MapFrom for optimized mapFromGlobal(mapToGlobal(..)). --- ui/ui_utility.cpp | 16 ++++++++++++++++ ui/ui_utility.h | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/ui/ui_utility.cpp b/ui/ui_utility.cpp index ee6173d..283b340 100644 --- a/ui/ui_utility.cpp +++ b/ui/ui_utility.cpp @@ -222,4 +222,20 @@ int WheelDirection(not_null e) { return (delta / absDelta); } +QPoint MapFrom( + not_null to, + not_null from, + QPoint point) { + return (to->window() != from->window()) + ? to->mapFromGlobal(from->mapToGlobal(point)) + : to->mapFrom(to->window(), from->mapTo(from->window(), point)); +} + +[[nodiscard]] QRect MapFrom( + not_null to, + not_null from, + QRect rect) { + return { MapFrom(to, from, rect.topLeft()), rect.size() }; +} + } // namespace Ui diff --git a/ui/ui_utility.h b/ui/ui_utility.h index 420f0ff..3242ed5 100644 --- a/ui/ui_utility.h +++ b/ui/ui_utility.h @@ -196,4 +196,14 @@ QPointer MakeWeak(not_null object) { int WheelDirection(not_null e); +[[nodiscard]] QPoint MapFrom( + not_null to, + not_null from, + QPoint point); + +[[nodiscard]] QRect MapFrom( + not_null to, + not_null from, + QRect rect); + } // namespace Ui