QtUtilWin.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <QRect>
  10. #include <QColor>
  11. #include <QPixmap>
  12. #include <QString>
  13. #include <QPointF>
  14. #include <QRectF>
  15. #include <QVector>
  16. #include <QWidget>
  17. #include <QWindow>
  18. #include <QApplication>
  19. #ifdef Q_OS_WIN
  20. # include <QtWinExtras/QtWin>
  21. # include <QtGui/qpa/qplatformnativeinterface.h>
  22. # include <objidl.h>
  23. #endif // Q_OS_WIN
  24. #include "Util/EditorUtils.h"
  25. namespace QtUtil
  26. {
  27. #ifdef Q_OS_WIN
  28. inline HWND getNativeHandle(QWidget* widget)
  29. {
  30. QWindow* window = widget->windowHandle();
  31. QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
  32. return static_cast<HWND>(nativeInterface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
  33. }
  34. #endif // Q_OS_WIN
  35. //! a helper class which captures the HWND of a given widget for the duration of its life cycle.
  36. //! used mainly to set the parent of popup dialogs like file dialogs which are still MFC classes.
  37. class QtMFCScopedHWNDCapture
  38. {
  39. public:
  40. QtMFCScopedHWNDCapture(QWidget* source = nullptr)
  41. {
  42. if (!source)
  43. {
  44. source = qApp->activeWindow();
  45. }
  46. if (source)
  47. {
  48. m_widget = source;
  49. }
  50. }
  51. ~QtMFCScopedHWNDCapture()
  52. {
  53. m_widget = nullptr;
  54. m_attached = false;
  55. }
  56. // this is here so that it will also work for widgets that need parents if we upgrade the file dialog and other dialogs to be widget based
  57. operator QWidget*()
  58. {
  59. return m_widget;
  60. }
  61. private:
  62. bool m_attached = false;
  63. QWidget* m_widget = nullptr;
  64. };
  65. }