QtViewPaneManager.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #if !defined(Q_MOC_RUN)
  10. #include "Include/EditorCoreAPI.h"
  11. #include "Resource.h"
  12. #include <AzToolsFramework/API/ViewPaneOptions.h>
  13. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  14. #include <AzQtComponents/Components/DockTabWidget.h>
  15. #include <AzQtComponents/Components/StyledDockWidget.h>
  16. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  17. #include <AzToolsFramework/API/EditorWindowRequestBus.h>
  18. #include <QObject>
  19. #include <QVector>
  20. #include <QPointer>
  21. #include <QSettings>
  22. #include <QAction>
  23. #include <QByteArray>
  24. #include <QList>
  25. #include <LyViewPaneNames.h>
  26. #include <AzCore/std/functional.h>
  27. #endif
  28. class QMainWindow;
  29. class ViewportEditorModeNotificationsBusImpl;
  30. struct ViewLayoutState;
  31. struct QtViewPane;
  32. class XmlNodeRef;
  33. namespace AzQtComponents
  34. {
  35. class DockMainWindow;
  36. class StyledDockWidget;
  37. class FancyDocking;
  38. } // namespace AzQtComponents
  39. typedef AZStd::function<QWidget*(QWidget*)> ViewPaneFactory;
  40. class DockWidget
  41. : public AzQtComponents::StyledDockWidget
  42. {
  43. Q_OBJECT
  44. public:
  45. explicit DockWidget(QWidget* widget, QtViewPane* pane, QSettings* settings, QMainWindow* parent, AzQtComponents::FancyDocking* advancedDockManager);
  46. QString PaneName() const;
  47. void RestoreState(bool forceDefault = false);
  48. /**
  49. * Gets the setting name for a given pane.
  50. */
  51. static QString settingsKey(const QString& paneName);
  52. protected:
  53. bool event(QEvent* qtEvent) override;
  54. private:
  55. void reparentToMainWindowFix();
  56. QRect ProperGeometry() const;
  57. QString settingsKey() const;
  58. QMainWindow* const m_mainWindow;
  59. QtViewPane* const m_pane;
  60. AzQtComponents::FancyDocking* m_advancedDockManager;
  61. };
  62. struct QtViewPane
  63. {
  64. enum class OpenMode
  65. {
  66. None = 0x0,
  67. UseDefaultState = 0x1, // Use default geometry and docking position when opening
  68. MultiplePanes = 0x2,
  69. RestoreLayout = 0x4,
  70. OnlyOpen = 0x8,
  71. };
  72. Q_DECLARE_FLAGS(OpenModes, OpenMode)
  73. enum class CloseMode
  74. {
  75. None = 0x0,
  76. Destroy = 0x1, // Destroy window when closing it
  77. Force = 0x2 // Force the dialog to close instead of querying the view if we can close
  78. };
  79. Q_DECLARE_FLAGS(CloseModes, CloseMode)
  80. int m_id; // between ID_VIEW_OPENPANE_FIRST and ID_VIEW_OPENPANE_LAST
  81. QString m_name;
  82. QString m_category;
  83. ViewPaneFactory m_factoryFunc;
  84. QPointer<DockWidget> m_dockWidget;
  85. AzToolsFramework::ViewPaneOptions m_options;
  86. QList<DockWidget*> m_dockWidgetInstances;
  87. bool IsValid() const
  88. {
  89. return m_id >= ID_VIEW_OPENPANE_FIRST && m_id <= ID_VIEW_OPENPANE_LAST && !m_name.isEmpty();
  90. }
  91. bool IsVisible() const
  92. {
  93. return m_dockWidget && m_dockWidget->isVisible();
  94. }
  95. bool IsConstructed() const
  96. {
  97. return m_dockWidget != nullptr;
  98. }
  99. QWidget* Widget() const
  100. {
  101. return IsConstructed() ? m_dockWidget->widget() : nullptr;
  102. }
  103. bool IsViewportPane() const
  104. {
  105. return m_category == QLatin1String("Viewport") && m_options.viewportType != -1;
  106. }
  107. bool IsPreview() const
  108. {
  109. return m_options.isPreview;
  110. }
  111. QWidget* CreateWidget();
  112. bool Close(CloseModes = CloseMode::Destroy);
  113. bool CloseInstance(QDockWidget* dockWidget, CloseModes closeModes = CloseMode::Destroy);
  114. };
  115. typedef QVector<QtViewPane> QtViewPanes;
  116. class EDITOR_CORE_API QtViewPaneManager
  117. : public QObject
  118. {
  119. Q_OBJECT
  120. public:
  121. explicit QtViewPaneManager(QObject* parent = nullptr);
  122. ~QtViewPaneManager();
  123. void SetMainWindow(AzQtComponents::DockMainWindow*, QSettings* settings, const QByteArray& lastMainWindowState);
  124. void RegisterPane(const QString &name, const QString &category, ViewPaneFactory, const AzToolsFramework::ViewPaneOptions& = {});
  125. void UnregisterPane(const QString& name);
  126. QtViewPane* GetPane(int id);
  127. QtViewPane* GetPane(const QString& name);
  128. QtViewPane* GetFirstVisiblePaneMatching(const QString& name);
  129. QtViewPane* GetViewportPane(int viewportType);
  130. QDockWidget* GetView(const QString& name);
  131. bool IsVisible(const QString& name);
  132. bool IsEnumeratedInstanceVisible(const QString& name);
  133. bool IsPaneRegistered(const QString& name) const;
  134. /**
  135. * Constructs and shows a view pane.
  136. * The pane is a QDockWidget who's widget was created with QtViewPane::m_factoryFunc.
  137. * If useDefaultState is true, the default docking area and geometry are used, not the last one.
  138. *
  139. * Returns the view on success, nullptr otherwise
  140. */
  141. const QtViewPane* OpenPane(const QString& name, QtViewPane::OpenModes = QtViewPane::OpenMode::None);
  142. QDockWidget* InstancePane(const QString& name);
  143. bool ClosePane(const QString& name, QtViewPane::CloseModes = QtViewPane::CloseMode::None);
  144. bool ClosePaneInstance(const QString& name, QDockWidget* dockPanel, QtViewPane::CloseModes = QtViewPane::CloseMode::None);
  145. /**
  146. * If the pane is not visible, it will be opened and made visible.
  147. * If the pane is visible, it will be closed.
  148. */
  149. void TogglePane(const QString& name);
  150. bool CloseAllPanes();
  151. /**
  152. * Closes all non standard panes. Standard panes are for example rollup and console.
  153. */
  154. void CloseAllNonStandardPanes();
  155. /**
  156. * Creates and returns a widget by calling QtViewPane::m_factoryFunc() for the view pane with name paneName.
  157. * This is similar to OpenPane(), except that there's no dock widget involved. The widget will be used in a
  158. * CLayoutViewPane (the embedded viewports).
  159. *
  160. * Returns nullptr if the specified pane name is not registered.
  161. */
  162. QWidget* CreateWidget(const QString& paneName);
  163. void RestoreLayout(bool restoreDefaults);
  164. bool RestoreLayout(QString name);
  165. void RestoreDefaultLayout(bool resetSettings = false);
  166. void SaveLayout();
  167. void SaveLayout(QString name);
  168. void RenameLayout(QString name, QString newName);
  169. void RemoveLayout(QString name);
  170. bool HasLayout(const QString& name) const;
  171. QStringList LayoutNames(bool userLayoutsOnly = true) const;
  172. void SerializeLayout(XmlNodeRef& parentNode) const;
  173. bool DeserializeLayout(const XmlNodeRef& parentNode);
  174. static QtViewPaneManager* instance();
  175. static bool exists();
  176. /**
  177. * Returns the known view panes (regardless of them being open or not).
  178. * If viewPaneMenuOnly is true, only those appearing in "View->Open View Pane" will be show,
  179. * meaning panes such as the rollup bar or console aren't returned.
  180. */
  181. QtViewPanes GetRegisteredPanes(bool viewPaneMenuOnly = true) const;
  182. QtViewPanes GetRegisteredMultiInstancePanes(bool viewPaneMenuOnly = true) const;
  183. QtViewPanes GetRegisteredViewportPanes() const; // only returns the Top/Bottom/Left etc. ones
  184. //! Attempts to closes everything not in the input list. Returns false if any failed, and restores all previously opened windows if it does. Returns true otherwise
  185. bool ClosePanesWithRollback(const QVector<QString>& panesToKeepOpen);
  186. signals:
  187. void savedLayoutsChanged();
  188. void layoutReset();
  189. void viewPaneCreated(const QtViewPane* pane);
  190. void registeredPanesChanged();
  191. private:
  192. friend class DockWidget;
  193. ViewLayoutState GetLayout() const;
  194. bool RestoreLayout(const ViewLayoutState& state);
  195. void SaveStateToLayout(const ViewLayoutState& state, const QString& layoutName);
  196. #if AZ_TRAIT_OS_PLATFORM_APPLE
  197. QDockWidget* ShowFakeNonDockableDockWidget(AzQtComponents::StyledDockWidget* dockWidget, QtViewPane* pane);
  198. #endif
  199. bool ClosePane(QtViewPane* pane, QtViewPane::CloseModes closeModes = QtViewPane::CloseMode::None);
  200. int NextAvailableId();
  201. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  202. QtViewPanes m_registeredPanes;
  203. QByteArray m_defaultMainWindowState;
  204. QByteArray m_loadedMainWindowState;
  205. QMainWindow* m_mainWindow;
  206. QSettings* m_settings;
  207. QList<int> m_knownIdsSet; // Semantically a set, but QList is faster for small collections than QSet
  208. bool m_restoreInProgress;
  209. QMap<QString, QRect> m_fakeDockWidgetGeometries;
  210. QPointer<AzQtComponents::FancyDocking> m_advancedDockManager;
  211. AZStd::unique_ptr<ViewportEditorModeNotificationsBusImpl>
  212. m_componentModeNotifications; //!< Helper for EditorComponentModeNotificationBus so
  213. //!< QtViewPaneManager does not need to inherit directly from it. */
  214. using EditorWindowRequestBusImpl = AzToolsFramework::EditorWindowRequestBusImpl;
  215. EditorWindowRequestBusImpl m_windowRequest; //!< Helper for EditorWindowRequestBus so
  216. //!< QtViewPaneManager does not need to inherit directly from it. */
  217. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  218. };
  219. template<class TWidget>
  220. bool RegisterQtViewPane([[maybe_unused]] IEditor* editor, const QString& name, const QString& category, const AzToolsFramework::ViewPaneOptions& options = {})
  221. {
  222. QtViewPaneManager::instance()->RegisterPane(name, category, [](QWidget* parent = nullptr) { return new TWidget(parent); }, options);
  223. return true;
  224. }
  225. template<class TWidget>
  226. bool RegisterQtViewPaneWithName([[maybe_unused]] IEditor* editor, const QString& name, const QString& category, const AzToolsFramework::ViewPaneOptions& options = {})
  227. {
  228. QtViewPaneManager::instance()->RegisterPane(name, category, [name](QWidget* parent = nullptr) { return new TWidget(name, parent); }, options);
  229. return true;
  230. }
  231. template<typename TWidget>
  232. TWidget* FindViewPane(const QString& name)
  233. {
  234. QtViewPane* pane = QtViewPaneManager::exists() ? QtViewPaneManager::instance()->GetPane(name) : nullptr;
  235. return pane ? qobject_cast<TWidget*>(pane->Widget()) : nullptr;
  236. }
  237. Q_DECLARE_OPERATORS_FOR_FLAGS(QtViewPane::OpenModes)
  238. Q_DECLARE_OPERATORS_FOR_FLAGS(QtViewPane::CloseModes)