dockoverlay.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <QHash>
  39. #include <QPointer>
  40. #include <QRect>
  41. class QGridLayout;
  42. namespace ADS {
  43. struct DockOverlayPrivate;
  44. class DockOverlayCross;
  45. /**
  46. * DockOverlay paints a translucent rectangle over another widget. The geometry
  47. * of the rectangle is based on the mouse location.
  48. */
  49. class ADS_EXPORT DockOverlay : public QFrame
  50. {
  51. Q_OBJECT
  52. private:
  53. DockOverlayPrivate *d; //< private data class
  54. friend struct DockOverlayPrivate;
  55. friend class DockOverlayCross;
  56. public:
  57. using Super = QFrame;
  58. enum eMode { ModeDockAreaOverlay, ModeContainerOverlay };
  59. /**
  60. * Creates a dock overlay
  61. */
  62. DockOverlay(QWidget *parent, eMode Mode = ModeDockAreaOverlay);
  63. /**
  64. * Virtual destructor
  65. */
  66. virtual ~DockOverlay() override;
  67. /**
  68. * Configures the areas that are allowed for docking
  69. */
  70. void setAllowedAreas(DockWidgetAreas areas);
  71. /**
  72. * Returns flags with all allowed drop areas
  73. */
  74. DockWidgetAreas allowedAreas() const;
  75. /**
  76. * Returns the drop area under the current cursor location
  77. */
  78. DockWidgetArea dropAreaUnderCursor() const;
  79. /**
  80. * Show the drop overly for the given target widget
  81. */
  82. DockWidgetArea showOverlay(QWidget *target);
  83. /**
  84. * Hides the overlay
  85. */
  86. void hideOverlay();
  87. /**
  88. * Enables / disables the semi transparent overlay rectangle that represents
  89. * the future area of the dropped widget
  90. */
  91. void enableDropPreview(bool enable);
  92. /**
  93. * Returns true if drop preview is enabled
  94. */
  95. bool dropPreviewEnabled() const;
  96. /**
  97. * The drop overlay rectangle for the target area
  98. */
  99. QRect dropOverlayRect() const;
  100. /**
  101. * Handle polish events
  102. */
  103. virtual bool event(QEvent *event) override;
  104. protected:
  105. virtual void paintEvent(QPaintEvent *event) override;
  106. virtual void showEvent(QShowEvent *event) override;
  107. virtual void hideEvent(QHideEvent *event) override;
  108. };
  109. struct DockOverlayCrossPrivate;
  110. /**
  111. * DockOverlayCross shows a cross with 5 different drop area possibilities.
  112. * I could have handled everything inside DockOverlay, but because of some
  113. * styling issues it's better to have a separate class for the cross.
  114. * You can style the cross icon using the property system.
  115. * \code
  116. * ADS--DockOverlayCross
  117. {
  118. qproperty-iconFrameColor: palette(highlight);
  119. qproperty-iconBackgroundColor: palette(base);
  120. qproperty-iconOverlayColor: palette(highlight);
  121. qproperty-iconArrowColor: rgb(227, 227, 227);
  122. qproperty-iconShadowColor: rgb(0, 0, 0);
  123. }
  124. * \endcode
  125. * Or you can use the iconColors property to pass in AARRGGBB values as
  126. * hex string like shown in the example below.
  127. * \code
  128. * ADS--DockOverlayCross
  129. * {
  130. * qproperty-iconColors: "Frame=#ff3d3d3d Background=#ff929292 Overlay=#1f3d3d3d Arrow=#ffb4b4b4 Shadow=#40474747";
  131. * }
  132. * \endcode
  133. */
  134. class DockOverlayCross : public QWidget
  135. {
  136. Q_OBJECT
  137. Q_PROPERTY(QString iconColors READ iconColors WRITE setIconColors)
  138. Q_PROPERTY(QColor iconFrameColor READ iconColor WRITE setIconFrameColor)
  139. Q_PROPERTY(QColor iconBackgroundColor READ iconColor WRITE setIconBackgroundColor)
  140. Q_PROPERTY(QColor iconOverlayColor READ iconColor WRITE setIconOverlayColor)
  141. Q_PROPERTY(QColor iconArrowColor READ iconColor WRITE setIconArrowColor)
  142. Q_PROPERTY(QColor iconShadowColor READ iconColor WRITE setIconShadowColor)
  143. public:
  144. enum eIconColor {
  145. FrameColor, ///< the color of the frame of the small window icon
  146. WindowBackgroundColor, ///< the background color of the small window in the icon
  147. OverlayColor, ///< the color that shows the overlay (the dock side) in the icon
  148. ArrowColor, ///< the arrow that points into the direction
  149. ShadowColor ///< the color of the shadow rectangle that is painted below the icons
  150. };
  151. private:
  152. DockOverlayCrossPrivate *d;
  153. friend struct DockOverlayCrossPrivate;
  154. friend class DockOverlay;
  155. protected:
  156. /**
  157. * This function returns an empty string and is only here to silence
  158. * moc
  159. */
  160. QString iconColors() const;
  161. /**
  162. * This is a dummy function for the property system
  163. */
  164. QColor iconColor() const { return QColor(); }
  165. void setIconFrameColor(const QColor &color) { setIconColor(FrameColor, color); }
  166. void setIconBackgroundColor(const QColor &color) { setIconColor(WindowBackgroundColor, color); }
  167. void setIconOverlayColor(const QColor &color) { setIconColor(OverlayColor, color); }
  168. void setIconArrowColor(const QColor &color) { setIconColor(ArrowColor, color); }
  169. void setIconShadowColor(const QColor &color) { setIconColor(ShadowColor, color); }
  170. public:
  171. /**
  172. * Creates an overlay cross for the given overlay
  173. */
  174. DockOverlayCross(DockOverlay *overlay);
  175. /**
  176. * Virtual destructor
  177. */
  178. virtual ~DockOverlayCross() override;
  179. /**
  180. * Sets a certain icon color
  181. */
  182. void setIconColor(eIconColor colorIndex, const QColor &color);
  183. /**
  184. * Returns the icon color given by ColorIndex
  185. */
  186. QColor iconColor(eIconColor colorIndex) const;
  187. /**
  188. * Returns the dock widget area depending on the current cursor location.
  189. * The function checks, if the mouse cursor is inside of any drop indicator
  190. * widget and returns the corresponding DockWidgetArea.
  191. */
  192. DockWidgetArea cursorLocation() const;
  193. /**
  194. * Sets up the overlay cross for the given overlay mode
  195. */
  196. void setupOverlayCross(DockOverlay::eMode mode);
  197. /**
  198. * Recreates the overlay icons.
  199. */
  200. void updateOverlayIcons();
  201. /**
  202. * Resets and updates the
  203. */
  204. void reset();
  205. /**
  206. * Updates the current position
  207. */
  208. void updatePosition();
  209. /**
  210. * A string with all icon colors to set.
  211. * You can use this property to style the overly icon via CSS stylesheet
  212. * file. The colors are set via a color identifier and a hex AARRGGBB value like
  213. * in the example below.
  214. * \code
  215. * ADS--DockOverlayCross
  216. * {
  217. * qproperty-iconColors: "Frame=#ff3d3d3d Background=#ff929292 Overlay=#1f3d3d3d Arrow=#ffb4b4b4 Shadow=#40474747";
  218. * }
  219. */
  220. void setIconColors(const QString &colors);
  221. protected:
  222. virtual void showEvent(QShowEvent *event) override;
  223. void setAreaWidgets(const QHash<DockWidgetArea, QWidget *> &widgets);
  224. }; // DockOverlayCross
  225. } // namespace ADS