dockmanager.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2020 Uwe Kindler
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 or (at your option) any later version.
  19. ** The licenses are as published by the Free Software Foundation
  20. ** and appearing in the file LICENSE.LGPLv21 included in the packaging
  21. ** of this file. Please review the following information to ensure
  22. ** the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 3 or (at your option) any later version
  28. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  29. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  30. ** included in the packaging of this file. Please review the following
  31. ** information to ensure the GNU General Public License requirements will
  32. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  33. **
  34. ****************************************************************************/
  35. #pragma once
  36. #include "ads_globals.h"
  37. #include "dockcontainerwidget.h"
  38. #include "dockwidget.h"
  39. #include "floatingdockcontainer.h"
  40. // #include <utils/persistentsettings.h>
  41. #include <QByteArray>
  42. #include <QDateTime>
  43. #include <QFlags>
  44. #include <QList>
  45. #include <QMap>
  46. #include <QString>
  47. #include <QStringList>
  48. #include <QtGui/QIcon>
  49. #include <qobjectdefs.h>
  50. class QSettings;
  51. class QMenu;
  52. namespace ADS {
  53. namespace Constants {
  54. const char FACTORY_DEFAULT_NAME[] = "factorydefault";
  55. const char DEFAULT_NAME[] = "default";
  56. const char STARTUP_WORKSPACE_SETTINGS_KEY[] = "QML/Designer/StartupWorkspace";
  57. const char AUTO_RESTORE_WORKSPACE_SETTINGS_KEY[] = "QML/Designer/AutoRestoreLastWorkspace";
  58. } // namespace Constants
  59. struct DockManagerPrivate;
  60. class FloatingDockContainer;
  61. struct FloatingDockContainerPrivate;
  62. class DockComponentsFactory;
  63. class DockContainerWidget;
  64. class DockContainerWidgetPrivate;
  65. class DockOverlay;
  66. class DockAreaTabBar;
  67. class DockWidgetTab;
  68. struct DockWidgetTabPrivate;
  69. struct DockAreaWidgetPrivate;
  70. class IconProvider;
  71. /**
  72. * The central dock manager that maintains the complete docking system.
  73. * With the configuration flags you can globally control the functionality
  74. * of the docking system. The dock manager uses an internal stylesheet to
  75. * style its components like splitters, tabs and buttons. If you want to
  76. * disable this stylesheet because your application uses its own,
  77. * just call the function for settings the stylesheet with an empty
  78. * string.
  79. * \code
  80. * dockManager->setStyleSheet("");
  81. * \endcode
  82. **/
  83. class ADS_EXPORT DockManager : public DockContainerWidget
  84. {
  85. Q_OBJECT
  86. private:
  87. DockManagerPrivate *d; ///< private data (pimpl)
  88. friend struct DockManagerPrivate;
  89. friend class FloatingDockContainer;
  90. friend struct FloatingDockContainerPrivate;
  91. friend class DockContainerWidget;
  92. friend class DockContainerWidgetPrivate;
  93. friend class DockAreaTabBar;
  94. friend class DockWidgetTab;
  95. friend struct DockAreaWidgetPrivate;
  96. friend struct DockWidgetTabPrivate;
  97. friend class FloatingDragPreview;
  98. friend struct FloatingDragPreviewPrivate;
  99. friend class DockAreaTitleBar;
  100. protected:
  101. /**
  102. * Registers the given floating widget in the internal list of
  103. * floating widgets
  104. */
  105. void registerFloatingWidget(FloatingDockContainer *floatingWidget);
  106. /**
  107. * Remove the given floating widget from the list of registered floating
  108. * widgets
  109. */
  110. void removeFloatingWidget(FloatingDockContainer *floatingWidget);
  111. /**
  112. * Registers the given dock container widget
  113. */
  114. void registerDockContainer(DockContainerWidget *dockContainer);
  115. /**
  116. * Remove dock container from the internal list of registered dock
  117. * containers
  118. */
  119. void removeDockContainer(DockContainerWidget *dockContainer);
  120. /**
  121. * Overlay for containers
  122. */
  123. DockOverlay *containerOverlay() const;
  124. /**
  125. * Overlay for dock areas
  126. */
  127. DockOverlay *dockAreaOverlay() const;
  128. /**
  129. * Show the floating widgets that has been created floating
  130. */
  131. virtual void showEvent(QShowEvent *event) override;
  132. public:
  133. using Super = DockContainerWidget;
  134. /**
  135. * These global configuration flags configure some global dock manager
  136. * settings.
  137. */
  138. enum eConfigFlag {
  139. ActiveTabHasCloseButton
  140. = 0x0001, //!< If this flag is set, the active tab in a tab area has a close button
  141. DockAreaHasCloseButton
  142. = 0x0002, //!< If the flag is set each dock area has a close button
  143. DockAreaCloseButtonClosesTab
  144. = 0x0004, //!< If the flag is set, the dock area close button closes the active tab, if not set, it closes the complete dock area
  145. OpaqueSplitterResize
  146. = 0x0008, //!< See QSplitter::setOpaqueResize() documentation
  147. XmlAutoFormattingEnabled
  148. = 0x0010, //!< If enabled, the XML writer automatically adds line-breaks and indentation to empty sections between elements (ignorable whitespace).
  149. XmlCompressionEnabled
  150. = 0x0020, //!< If enabled, the XML output will be compressed and is not human readable anymore
  151. TabCloseButtonIsToolButton
  152. = 0x0040, //! If enabled the tab close buttons will be QToolButtons instead of QPushButtons - disabled by default
  153. AllTabsHaveCloseButton
  154. = 0x0080, //!< if this flag is set, then all tabs that are closable show a close button
  155. RetainTabSizeWhenCloseButtonHidden
  156. = 0x0100, //!< if this flag is set, the space for the close button is reserved even if the close button is not visible
  157. OpaqueUndocking
  158. = 0x0200, ///< If enabled, the widgets are immediately undocked into floating widgets, if disabled, only a draw preview is undocked and the real undocking is deferred until the mouse is released
  159. DragPreviewIsDynamic
  160. = 0x0400, ///< If opaque undocking is disabled, this flag defines the behavior of the drag preview window, if this flag is enabled, the preview will be adjusted dynamically to the drop area
  161. DragPreviewShowsContentPixmap
  162. = 0x0800, ///< If opaque undocking is disabled, the created drag preview window shows a copy of the content of the dock widget / dock are that is dragged
  163. DragPreviewHasWindowFrame
  164. = 0x1000, ///< If opaque undocking is disabled, then this flag configures if the drag preview is frameless or looks like a real window
  165. AlwaysShowTabs
  166. = 0x2000, ///< If this option is enabled, the tab of a dock widget is always displayed - even if it is the only visible dock widget in a floating widget.
  167. DockAreaHasUndockButton
  168. = 0x4000, //!< If the flag is set each dock area has an undock button
  169. DockAreaHasTabsMenuButton
  170. = 0x8000, //!< If the flag is set each dock area has a tabs menu button
  171. DockAreaHideDisabledButtons
  172. = 0x10000, //!< If the flag is set disabled dock area buttons will not appear on the tollbar at all (enabling them will bring them back)
  173. DockAreaDynamicTabsMenuButtonVisibility
  174. = 0x20000, //!< If the flag is set dock area will disable a tabs menu button when there is only one tab in the area
  175. FloatingContainerHasWidgetTitle
  176. = 0x40000,
  177. FloatingContainerHasWidgetIcon
  178. = 0x80000,
  179. DefaultDockAreaButtons = DockAreaHasCloseButton
  180. | DockAreaHasUndockButton
  181. | DockAreaHasTabsMenuButton,///< default configuration of dock area title bar buttons
  182. DefaultBaseConfig = DefaultDockAreaButtons
  183. | ActiveTabHasCloseButton
  184. | XmlCompressionEnabled
  185. | FloatingContainerHasWidgetTitle,///< default base configuration settings
  186. DefaultOpaqueConfig = DefaultBaseConfig
  187. | OpaqueSplitterResize
  188. | OpaqueUndocking, ///< the default configuration with opaque operations - this may cause issues if ActiveX or Qt 3D windows are involved
  189. DefaultNonOpaqueConfig = DefaultBaseConfig
  190. | DragPreviewShowsContentPixmap, ///< the default configuration for non opaque operations
  191. NonOpaqueWithWindowFrame = DefaultNonOpaqueConfig
  192. | DragPreviewHasWindowFrame ///< the default configuration for non opaque operations that show a real window with frame
  193. };
  194. Q_DECLARE_FLAGS(ConfigFlags, eConfigFlag)
  195. /**
  196. * Default Constructor.
  197. * If the given parent is a QMainWindow, the dock manager sets itself as the
  198. * central widget.
  199. * Before you create any dock widgets, you should properly setup the
  200. * configuration flags via setConfigFlags().
  201. */
  202. DockManager(QWidget *parent = nullptr);
  203. /**
  204. * Virtual Destructor
  205. */
  206. virtual ~DockManager() override;
  207. /**
  208. * This function returns the global configuration flags
  209. */
  210. static ConfigFlags configFlags();
  211. /**
  212. * Sets the global configuration flags for the whole docking system.
  213. * Call this function before you create your first dock widget.
  214. */
  215. static void setConfigFlags(const ConfigFlags flags);
  216. /**
  217. * Set a certain config flag
  218. */
  219. static void setConfigFlag(eConfigFlag flag, bool on = true);
  220. /**
  221. * Returns true if the given config flag is set
  222. */
  223. static bool testConfigFlag(eConfigFlag flag);
  224. /**
  225. * Returns the global icon provider.
  226. * The icon provider enables the use of custom icons in case using
  227. * styleheets for icons is not an option.
  228. */
  229. static IconProvider &iconProvider();
  230. /**
  231. * The distance the user needs to move the mouse with the left button
  232. * hold down before a dock widget start floating
  233. */
  234. static int startDragDistance();
  235. /**
  236. * Set the QtCreator settings.
  237. */
  238. void setSettings(QSettings *settings);
  239. /**
  240. * Adds dockwidget into the given area.
  241. * If DockAreaWidget is not null, then the area parameter indicates the area
  242. * into the DockAreaWidget. If DockAreaWidget is null, the Dockwidget will
  243. * be dropped into the container. If you would like to add a dock widget
  244. * tabified, then you need to add it to an existing dock area object
  245. * into the CenterDockWidgetArea. The following code shows this:
  246. * \code
  247. * DockManager->addDockWidget(ads::CenterDockWidgetArea, NewDockWidget,
  248. * ExisitingDockArea);
  249. * \endcode
  250. * \return Returns the dock area widget that contains the new DockWidget
  251. */
  252. DockAreaWidget *addDockWidget(DockWidgetArea area,
  253. DockWidget *dockWidget,
  254. DockAreaWidget *dockAreaWidget = nullptr);
  255. /**
  256. * This function will add the given Dockwidget to the given dock area as
  257. * a new tab.
  258. * If no dock area widget exists for the given area identifier, a new
  259. * dock area widget is created.
  260. */
  261. DockAreaWidget *addDockWidgetTab(DockWidgetArea area, DockWidget *dockWidget);
  262. /**
  263. * This function will add the given Dockwidget to the given DockAreaWidget
  264. * as a new tab.
  265. */
  266. DockAreaWidget *addDockWidgetTabToArea(DockWidget *dockWidget, DockAreaWidget *dockAreaWidget);
  267. /**
  268. * Adds the given DockWidget floating and returns the created
  269. * CFloatingDockContainer instance.
  270. */
  271. FloatingDockContainer *addDockWidgetFloating(DockWidget *dockWidget);
  272. /**
  273. * Searches for a registered doc widget with the given ObjectName
  274. * \return Return the found dock widget or nullptr if a dock widget with the
  275. * given name is not registered
  276. */
  277. DockWidget *findDockWidget(const QString &objectName) const;
  278. /**
  279. * Remove the given Dock from the dock manager
  280. */
  281. void removeDockWidget(DockWidget *dockWidget);
  282. /**
  283. * This function returns a readable reference to the internal dock
  284. * widgets map so that it is possible to iterate over all dock widgets
  285. */
  286. QMap<QString, DockWidget *> dockWidgetsMap() const;
  287. /**
  288. * Returns the list of all active and visible dock containers
  289. * Dock containers are the main dock manager and all floating widgets
  290. */
  291. const QList<DockContainerWidget *> dockContainers() const;
  292. /**
  293. * Returns the list of all floating widgets
  294. */
  295. const QList<FloatingDockContainer *> floatingWidgets() const;
  296. /**
  297. * This function always return 0 because the main window is always behind
  298. * any floating widget
  299. */
  300. virtual unsigned int zOrderIndex() const override;
  301. /**
  302. * Saves the current state of the dockmanger and all its dock widgets
  303. * into the returned QByteArray.
  304. * The XmlMode enables / disables the auto formatting for the XmlStreamWriter.
  305. * If auto formatting is enabled, the output is intended and line wrapped.
  306. * The XmlMode XmlAutoFormattingDisabled is better if you would like to have
  307. * a more compact XML output - i.e. for storage in ini files.
  308. */
  309. QByteArray saveState(int version = Version1) const;
  310. /**
  311. * Restores the state of this dockmanagers dockwidgets.
  312. * The version number is compared with that stored in state. If they do
  313. * not match, the dockmanager's state is left unchanged, and this function
  314. * returns false; otherwise, the state is restored, and this function
  315. * returns true.
  316. */
  317. bool restoreState(const QByteArray &state, int version = Version1);
  318. /**
  319. * This function returns true between the restoringState() and
  320. * stateRestored() signals.
  321. */
  322. bool isRestoringState() const;
  323. signals:
  324. /**
  325. * This signal is emitted if the list of perspectives changed
  326. */
  327. void workspaceListChanged();
  328. /**
  329. * This signal is emitted if perspectives have been removed
  330. */
  331. void workspacesRemoved();
  332. /**
  333. * This signal is emitted, if the restore function is called, just before
  334. * the dock manager starts restoring the state.
  335. * If this function is called, nothing has changed yet
  336. */
  337. void restoringState();
  338. /**
  339. * This signal is emitted if the state changed in restoreState.
  340. * The signal is emitted if the restoreState() function is called or
  341. * if the openWorkspace() function is called
  342. */
  343. void stateRestored();
  344. /**
  345. * This signal is emitted, if the dock manager starts opening a
  346. * perspective.
  347. * Opening a perspective may take more than a second if there are
  348. * many complex widgets. The application may use this signal
  349. * to show some progress indicator or to change the mouse cursor
  350. * into a busy cursor.
  351. */
  352. void openingWorkspace(const QString &workspaceName);
  353. /**
  354. * This signal is emitted if the dock manager finished opening a
  355. * perspective
  356. */
  357. void workspaceOpened(const QString &workspaceName);
  358. /**
  359. * This signal is emitted, if a new floating widget has been created.
  360. * An application can use this signal to e.g. subscribe to events of
  361. * the newly created window.
  362. */
  363. void floatingWidgetCreated(FloatingDockContainer *floatingWidget);
  364. /**
  365. * This signal is emitted, if a new DockArea has been created.
  366. * An application can use this signal to set custom icons or custom
  367. * tooltips for the DockArea buttons.
  368. */
  369. void dockAreaCreated(DockAreaWidget *dockArea);
  370. /**
  371. * This signal is emitted just before the given dock widget is removed
  372. * from the
  373. */
  374. void dockWidgetAboutToBeRemoved(DockWidget *dockWidget);
  375. /**
  376. * This signal is emitted if a dock widget has been removed with the remove
  377. * removeDockWidget() function.
  378. * If this signal is emitted, the dock widget has been removed from the
  379. * docking system but it is not deleted yet.
  380. */
  381. void dockWidgetRemoved(DockWidget *dockWidget);
  382. public:
  383. void showWorkspaceMananger();
  384. // higher level workspace management
  385. QString activeWorkspace() const;
  386. QString lastWorkspace() const;
  387. bool autoRestorLastWorkspace() const;
  388. QStringList workspaces();
  389. QDateTime workspaceDateTime(const QString &workspace) const;
  390. QString workspaceNameToFileName(const QString &workspaceName) const;
  391. bool createWorkspace(const QString &workspace);
  392. bool openWorkspace(const QString &workspace);
  393. bool confirmWorkspaceDelete(const QStringList &workspaces);
  394. bool deleteWorkspace(const QString &workspace);
  395. void deleteWorkspaces(const QStringList &workspaces);
  396. bool cloneWorkspace(const QString &original, const QString &clone);
  397. bool renameWorkspace(const QString &original, const QString &newName);
  398. bool save();
  399. bool isFactoryDefaultWorkspace(const QString &workspace) const;
  400. bool isDefaultWorkspace(const QString &workspace) const;
  401. signals:
  402. void aboutToUnloadWorkspace(QString workspaceName);
  403. void aboutToLoadWorkspace(QString workspaceName);
  404. void workspaceLoaded(QString workspaceName);
  405. void aboutToSaveWorkspace();
  406. private:
  407. bool write(const QByteArray &data, QString *errorString) const;
  408. #ifdef QT_GUI_LIB
  409. bool write(const QByteArray &data, QWidget *parent) const;
  410. #endif
  411. }; // class DockManager
  412. } // namespace ADS