dockwidget.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <QFrame>
  38. class QToolBar;
  39. class QXmlStreamWriter;
  40. namespace ADS {
  41. struct DockWidgetPrivate;
  42. class DockWidgetTab;
  43. class DockManager;
  44. class DockContainerWidget;
  45. class DockAreaWidget;
  46. class DockContainerWidgetPrivate;
  47. class FloatingDockContainer;
  48. /**
  49. * The QDockWidget class provides a widget that can be docked inside a
  50. * DockManager or floated as a top-level window on the desktop.
  51. */
  52. class ADS_EXPORT DockWidget : public QFrame
  53. {
  54. Q_OBJECT
  55. private:
  56. DockWidgetPrivate *d; ///< private data (pimpl)
  57. friend struct DockWidgetPrivate;
  58. /**
  59. * Adjusts the toolbar icon sizes according to the floating state
  60. */
  61. void setToolbarFloatingStyle(bool topLevel);
  62. protected:
  63. friend class DockContainerWidget;
  64. friend class DockAreaWidget;
  65. friend class FloatingDockContainer;
  66. friend class DockManager;
  67. friend struct DockManagerPrivate;
  68. friend class DockContainerWidgetPrivate;
  69. friend class DockAreaTabBar;
  70. friend class DockWidgetTab;
  71. friend struct DockWidgetTabPrivate;
  72. friend struct DockAreaTitleBarPrivate;
  73. /**
  74. * Assigns the dock manager that manages this dock widget
  75. */
  76. void setDockManager(DockManager *dockManager);
  77. /**
  78. * If this dock widget is inserted into a dock area, the dock area will
  79. * be registered on this widget via this function. If a dock widget is
  80. * removed from a dock area, this function will be called with nullptr
  81. * value.
  82. */
  83. void setDockArea(DockAreaWidget *dockArea);
  84. /**
  85. * This function changes the toggle view action without emitting any
  86. * signal
  87. */
  88. void setToggleViewActionChecked(bool checked);
  89. /**
  90. * Saves the state into the given stream
  91. */
  92. void saveState(QXmlStreamWriter &stream) const;
  93. /**
  94. * This is a helper function for the dock manager to flag this widget
  95. * as unassigned.
  96. * When calling the restore function, it may happen, that the saved state
  97. * contains less dock widgets then currently available. All widgets whose
  98. * data is not contained in the saved state, are flagged as unassigned
  99. * after the restore process. If the user shows an unassigned dock widget,
  100. * a floating widget will be created to take up the dock widget.
  101. */
  102. void flagAsUnassigned();
  103. /**
  104. * Call this function to emit a topLevelChanged() signal and to update
  105. * the dock area tool bar visibility
  106. */
  107. static void emitTopLevelEventForWidget(DockWidget *topLevelDockWidget, bool floating);
  108. /**
  109. * Use this function to emit a top level changed event.
  110. * Do never use emit topLevelChanged(). Always use this function because
  111. * it only emits a signal if the floating state has really changed
  112. */
  113. void emitTopLevelChanged(bool floating);
  114. /**
  115. * Internal function for modifying the closed state when restoring
  116. * a saved docking state
  117. */
  118. void setClosedState(bool closed);
  119. /**
  120. * Internal toggle view function that does not check if the widget
  121. * already is in the given state
  122. */
  123. void toggleViewInternal(bool open);
  124. /**
  125. * Internal close dock widget implementation.
  126. * The function returns true if the dock widget has been closed or hidden
  127. */
  128. bool closeDockWidgetInternal(bool forceClose = false);
  129. public:
  130. using Super = QFrame;
  131. enum DockWidgetFeature {
  132. DockWidgetClosable = 0x01,
  133. DockWidgetMovable = 0x02,///< this feature is not properly implemented yet and is ignored
  134. DockWidgetFloatable = 0x04,
  135. DockWidgetDeleteOnClose = 0x08, ///< deletes the dock widget when it is closed
  136. CustomCloseHandling = 0x10,
  137. DefaultDockWidgetFeatures = DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable,
  138. AllDockWidgetFeatures = DefaultDockWidgetFeatures | DockWidgetDeleteOnClose | CustomCloseHandling,
  139. NoDockWidgetFeatures = 0x00
  140. };
  141. Q_DECLARE_FLAGS(DockWidgetFeatures, DockWidgetFeature)
  142. enum eState { StateHidden, StateDocked, StateFloating };
  143. /**
  144. * Sets the widget for the dock widget to widget.
  145. * The InsertMode defines how the widget is inserted into the dock widget.
  146. * The content of a dock widget should be resizable do a very small size to
  147. * prevent the dock widget from blocking the resizing. To ensure, that a
  148. * dock widget can be resized very well, it is better to insert the content+
  149. * widget into a scroll area or to provide a widget that is already a scroll
  150. * area or that contains a scroll area.
  151. * If the InsertMode is AutoScrollArea, the DockWidget tries to automatically
  152. * detect how to insert the given widget. If the widget is derived from
  153. * QScrollArea (i.e. an QAbstractItemView), then the widget is inserted
  154. * directly. If the given widget is not a scroll area, the widget will be
  155. * inserted into a scroll area.
  156. * To force insertion into a scroll area, you can also provide the InsertMode
  157. * ForceScrollArea. To prevent insertion into a scroll area, you can
  158. * provide the InsertMode ForceNoScrollArea
  159. */
  160. enum eInsertMode { AutoScrollArea, ForceScrollArea, ForceNoScrollArea };
  161. /**
  162. * This mode configures the behavior of the toggle view action.
  163. * If the mode if ActionModeToggle, then the toggle view action is
  164. * a checkable action to show / hide the dock widget. If the mode
  165. * is ActionModeShow, then the action is not checkable an it will
  166. * always show the dock widget if clicked. If the mode is ActionModeShow,
  167. * the user can only close the DockWidget with the close button.
  168. */
  169. enum eToggleViewActionMode {
  170. ActionModeToggle, //!< ActionModeToggle
  171. ActionModeShow //!< ActionModeShow
  172. };
  173. /**
  174. * This constructor creates a dock widget with the given title.
  175. * The title is the text that is shown in the window title when the dock
  176. * widget is floating and it is the title that is shown in the titlebar
  177. * or the tab of this dock widget if it is tabified.
  178. * The object name of the dock widget is also set to the title. The
  179. * object name is required by the dock manager to properly save and restore
  180. * the state of the dock widget. That means, the title needs to be unique.
  181. * If your title is not unique or if you would like to change the title
  182. * during runtime, you need to set a unique object name explicitly
  183. * by calling setObjectName() after construction.
  184. * Use the layoutFlags to configure the layout of the dock widget.
  185. */
  186. DockWidget(const QString &uniqueId, QWidget *parent = nullptr);
  187. /**
  188. * Virtual Destructor
  189. */
  190. virtual ~DockWidget() override;
  191. /**
  192. * We return a fixed minimum size hint for all dock widgets
  193. */
  194. virtual QSize minimumSizeHint() const override;
  195. /**
  196. * Sets the widget for the dock widget to widget.
  197. * The InsertMode defines how the widget is inserted into the dock widget.
  198. * The content of a dock widget should be resizable do a very small size to
  199. * prevent the dock widget from blocking the resizing. To ensure, that a
  200. * dock widget can be resized very well, it is better to insert the content+
  201. * widget into a scroll area or to provide a widget that is already a scroll
  202. * area or that contains a scroll area.
  203. * If the InsertMode is AutoScrollArea, the DockWidget tries to automatically
  204. * detect how to insert the given widget. If the widget is derived from
  205. * QScrollArea (i.e. an QAbstractItemView), then the widget is inserted
  206. * directly. If the given widget is not a scroll area, the widget will be
  207. * inserted into a scroll area.
  208. * To force insertion into a scroll area, you can also provide the InsertMode
  209. * ForceScrollArea. To prevent insertion into a scroll area, you can
  210. * provide the InsertMode ForceNoScrollArea
  211. */
  212. void setWidget(QWidget *widget, eInsertMode insertMode = AutoScrollArea);
  213. /**
  214. * Remove the widget from the dock and give ownership back to the caller
  215. */
  216. QWidget *takeWidget();
  217. /**
  218. * Returns the widget for the dock widget. This function returns zero if
  219. * the widget has not been set.
  220. */
  221. QWidget *widget() const;
  222. /**
  223. * Returns the tab widget of this dock widget that is shown in the dock
  224. * area title bar
  225. */
  226. DockWidgetTab *tabWidget() const;
  227. /**
  228. * Sets, whether the dock widget is movable, closable, and floatable.
  229. */
  230. void setFeatures(DockWidgetFeatures features);
  231. /**
  232. * Sets the feature flag for this dock widget if on is true; otherwise
  233. * clears the flag.
  234. */
  235. void setFeature(DockWidgetFeature flag, bool on);
  236. /**
  237. * This property holds whether the dock widget is movable, closable, and
  238. * floatable.
  239. * By default, this property is set to a combination of DockWidgetClosable,
  240. * DockWidgetMovable and DockWidgetFloatable.
  241. */
  242. DockWidgetFeatures features() const;
  243. /**
  244. * Returns the dock manager that manages the dock widget or 0 if the widget
  245. * has not been assigned to any dock manager yet
  246. */
  247. DockManager *dockManager() const;
  248. /**
  249. * Returns the dock container widget this dock area widget belongs to or 0
  250. * if this dock widget has not been docked yet
  251. */
  252. DockContainerWidget *dockContainer() const;
  253. /**
  254. * Returns the dock area widget this dock widget belongs to or 0
  255. * if this dock widget has not been docked yet
  256. */
  257. DockAreaWidget *dockAreaWidget() const;
  258. /**
  259. * This property holds whether the dock widget is floating.
  260. * A dock widget is only floating, if it is the one and only widget inside
  261. * of a floating container. If there are more than one dock widget in a
  262. * floating container, the all dock widgets are docked and not floating.
  263. */
  264. bool isFloating() const;
  265. /**
  266. * This function returns true, if this dock widget is in a floating.
  267. * The function returns true, if the dock widget is floating and it also
  268. * returns true if it is docked inside of a floating container.
  269. */
  270. bool isInFloatingContainer() const;
  271. /**
  272. * Returns true, if this dock widget is closed.
  273. */
  274. bool isClosed() const;
  275. /**
  276. * Returns a checkable action that can be used to show or close this dock widget.
  277. * The action's text is set to the dock widget's window title.
  278. */
  279. QAction *toggleViewAction() const;
  280. /**
  281. * Configures the behavior of the toggle view action.
  282. * \see eToggleViewActionMode for a detailed description
  283. */
  284. void setToggleViewActionMode(eToggleViewActionMode mode);
  285. /**
  286. * Sets the dock widget icon that is shown in tabs and in toggle view
  287. * actions
  288. */
  289. void setIcon(const QIcon &icon);
  290. /**
  291. * Returns the icon that has been assigned to the dock widget
  292. */
  293. QIcon icon() const;
  294. /**
  295. * If the WithToolBar layout flag is enabled, then this function returns
  296. * the dock widget toolbar. If the flag is disabled, the function returns
  297. * a nullptr.
  298. * This function returns the dock widget top tool bar.
  299. * If no toolbar is assigned, this function returns nullptr. To get a valid
  300. * toolbar you either need to create a default empty toolbar via
  301. * createDefaultToolBar() function or you need to assign you custom
  302. * toolbar via setToolBar().
  303. */
  304. QToolBar *toolBar() const;
  305. /**
  306. * If you would like to use the default top tool bar, then call this
  307. * function to create the default tool bar.
  308. * After this function the toolBar() function will return a valid toolBar()
  309. * object.
  310. */
  311. QToolBar *createDefaultToolBar();
  312. /**
  313. * Assign a new tool bar that is shown above the content widget.
  314. * The dock widget will become the owner of the tool bar and deletes it
  315. * on destruction
  316. */
  317. void setToolBar(QToolBar *toolBar);
  318. /**
  319. * This function sets the tool button style for the given dock widget state.
  320. * It is possible to switch the tool button style depending on the state.
  321. * If a dock widget is floating, then here are more space and it is
  322. * possible to select a style that requires more space like
  323. * Qt::ToolButtonTextUnderIcon. For the docked state Qt::ToolButtonIconOnly
  324. * might be better.
  325. */
  326. void setToolBarStyle(Qt::ToolButtonStyle style, eState state);
  327. /**
  328. * Returns the tool button style for the given docking state.
  329. * \see setToolBarStyle()
  330. */
  331. Qt::ToolButtonStyle toolBarStyle(eState state) const;
  332. /**
  333. * This function sets the tool button icon size for the given state.
  334. * If a dock widget is floating, there is more space an increasing the
  335. * icon size is possible. For docked widgets, small icon sizes, eg. 16 x 16
  336. * might be better.
  337. */
  338. void setToolBarIconSize(const QSize &iconSize, eState state);
  339. /**
  340. * Returns the icon size for a given docking state.
  341. * \see setToolBarIconSize()
  342. */
  343. QSize toolBarIconSize(eState state) const;
  344. /**
  345. * Set the actions that will be shown in the dock area title bar
  346. * if this dock widget is the active tab.
  347. * You should not add to many actions to the title bar, because this
  348. * will remove the available space for the tabs. If you have a number
  349. * of actions, just add an action with a menu to show a popup menu
  350. * button in the title bar.
  351. */
  352. void setTitleBarActions(QList<QAction *> actions);
  353. /**
  354. * Returns a list of actions that will be inserted into the dock area title
  355. * bar if this dock widget becomes the current widget
  356. */
  357. virtual QList<QAction *> titleBarActions() const;
  358. #ifndef QT_NO_TOOLTIP
  359. /**
  360. * This is function sets text tooltip for title bar widget
  361. * and tooltip for toggle view action
  362. */
  363. void setTabToolTip(const QString &text);
  364. #endif
  365. public: // reimplements QFrame
  366. /**
  367. * Emits titleChanged signal if title change event occurs
  368. */
  369. virtual bool event(QEvent *event) override;
  370. /**
  371. * This property controls whether the dock widget is open or closed.
  372. * The toogleViewAction triggers this slot
  373. */
  374. void toggleView(bool open = true);
  375. /**
  376. * This function will make a docked widget floating
  377. */
  378. void setFloating();
  379. /**
  380. * This function will delete the dock widget and its content from the
  381. * docking system
  382. */
  383. void deleteDockWidget();
  384. /**
  385. * Closes the dock widget
  386. */
  387. void closeDockWidget();
  388. signals:
  389. /**
  390. * This signal is emitted if the dock widget is opened or closed
  391. */
  392. void viewToggled(bool open);
  393. /**
  394. * This signal is emitted if the dock widget is closed
  395. */
  396. void closed();
  397. /**
  398. * This signal is emitted if the window title of this dock widget
  399. * changed
  400. */
  401. void titleChanged(const QString &title);
  402. /**
  403. * This signal is emitted when the floating property changes.
  404. * The topLevel parameter is true if the dock widget is now floating;
  405. * otherwise it is false.
  406. */
  407. void topLevelChanged(bool topLevel);
  408. /**
  409. * This signal is emitted, if close is requested
  410. */
  411. void closeRequested();
  412. /**
  413. * This signal is emitted when the dock widget becomes visible (or invisible).
  414. * This happens when the widget is hidden or shown, as well as when it is
  415. * docked in a tabbed dock area and its tab becomes selected or unselected.
  416. */
  417. void visibilityChanged(bool visible);
  418. /**
  419. * This signal is emitted when the features property changes.
  420. * The features parameter gives the new value of the property.
  421. */
  422. void featuresChanged(DockWidgetFeatures features);
  423. }; // class DockWidget
  424. } // namespace ADS