ctkBasePopupWidget.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkBasePopupWidget_h
  15. #define __ctkBasePopupWidget_h
  16. // Qt includes
  17. #include <QEasingCurve>
  18. #include <QFrame>
  19. #include <QMetaType>
  20. // CTK includes
  21. #include "ctkWidgetsExport.h"
  22. class ctkBasePopupWidgetPrivate;
  23. /// \ingroup Widgets
  24. /// ctkBasePopupWidget is a popup that opens under, above or on the side of
  25. /// another widget (baseWidget() or its parent widget by default).
  26. /// The children (widgets and layout) of the popup define of the content
  27. /// of the popup. Different effects can be applied during the opening or
  28. /// closing of the popup.
  29. /// See ctkPopupWidget for an automatic control of its opening and closing.
  30. /// \sa baseWidget(), animationEffect, ctkPopupWidget
  31. class CTK_WIDGETS_EXPORT ctkBasePopupWidget : public QFrame
  32. {
  33. Q_OBJECT
  34. Q_ENUMS(AnimationEffect)
  35. Q_ENUMS(VerticalDirection)
  36. /// This property controls the effect to apply when the popup is being
  37. /// opened or closed. The total duration and the easing curve of the effect
  38. /// are controlled by \a effectDuration and \easingCurve respectively.
  39. /// ScrollEffect by default.
  40. /// \sa AnimationEffect, animationEffect(), setAnimationEffect(),
  41. /// effectDuration, easingCurve
  42. Q_PROPERTY( AnimationEffect animationEffect READ animationEffect WRITE setAnimationEffect)
  43. /// The property controls the \a animationEffect duration in ms.
  44. /// If the popup state (open or close) is being changed during the animation,
  45. /// the active animation is stopped and a new animation is being created from
  46. /// the current state (geometry, transparency...) to the new final state.
  47. /// Default to 333ms
  48. /// \sa effectDuration(), setEffectDuration(), animationEffect, easingCurve
  49. Q_PROPERTY( int effectDuration READ effectDuration WRITE setEffectDuration);
  50. /// The property controls the behavior of the opening or closing curve of the
  51. /// animation effect.
  52. /// QEasingCurve::InOutQuad by default
  53. /// \sa easingCurve(), setEasingCurve(), animationEffect, effectDuration
  54. Q_PROPERTY( QEasingCurve::Type easingCurve READ easingCurve WRITE setEasingCurve);
  55. /// Where is the popup in relation to the BaseWidget
  56. /// To vertically justify, use Qt::AlignTop | Qt::AlignBottom.
  57. /// Qt::AlignJustify | Qt::AlignBottom by default
  58. Q_PROPERTY( Qt::Alignment alignment READ alignment WRITE setAlignment);
  59. /// Direction of the scrolling effect, can be Qt::Vertical, Qt::Horizontal or
  60. /// both Qt::Vertical|Qt::Horizontal.
  61. /// Vertical by default
  62. Q_PROPERTY( Qt::Orientations orientation READ orientation WRITE setOrientation);
  63. /// Control where the popup opens vertically.
  64. /// TopToBottom by default
  65. Q_PROPERTY( ctkBasePopupWidget::VerticalDirection verticalDirection READ verticalDirection WRITE setVerticalDirection);
  66. /// Control where the popup opens horizontally.
  67. /// LeftToRight by default
  68. Q_PROPERTY( Qt::LayoutDirection horizontalDirection READ horizontalDirection WRITE setHorizontalDirection);
  69. public:
  70. typedef QFrame Superclass;
  71. /// Although a popup widget is a top-level widget, if a parent is
  72. /// passed the popup widget will be deleted when that parent is
  73. /// destroyed (as with any other QObject).
  74. /// ctkBasePopupWidget is a top-level widget (Qt::ToolTip), so
  75. /// even if a parent is passed, the popup will display outside the possible
  76. /// parent layout.
  77. /// \sa baseWidget().
  78. explicit ctkBasePopupWidget(QWidget* parent = 0);
  79. virtual ~ctkBasePopupWidget();
  80. /// Widget the popup is attached to. It opens right under \a baseWidget
  81. /// and if the ctkBasePopupWidget sizepolicy contains the growFlag/shrinkFlag,
  82. /// it tries to resize itself to fit the same width of \a baseWidget.
  83. /// By default, baseWidget is the parent widget.
  84. /// \sa setBaseWidget()
  85. QWidget* baseWidget()const;
  86. enum AnimationEffect
  87. {
  88. WindowOpacityFadeEffect = 0,
  89. ScrollEffect,
  90. FadeEffect
  91. };
  92. /// Return the animationEffect property value.
  93. /// \sa animationEffect
  94. AnimationEffect animationEffect()const;
  95. /// Set the animationEffect property value.
  96. /// \sa animationEffect
  97. void setAnimationEffect(AnimationEffect effect);
  98. /// Return the effectDuration property value.
  99. /// \sa effectDuration
  100. int effectDuration()const;
  101. /// Set the effectDuration property value.
  102. /// \sa effectDuration
  103. void setEffectDuration(int duration);
  104. /// Return the easingCurve property value.
  105. /// \sa easingCurve
  106. QEasingCurve::Type easingCurve()const;
  107. /// Set the easingCurve property value.
  108. /// \sa easingCurve
  109. void setEasingCurve(QEasingCurve::Type easingCurve);
  110. /// Return the alignment property value.
  111. /// \sa alignment
  112. Qt::Alignment alignment()const;
  113. /// Set the alignment property value.
  114. /// \sa alignment
  115. void setAlignment(Qt::Alignment alignment);
  116. /// Return the orientation property value.
  117. /// \sa orientation
  118. Qt::Orientations orientation()const;
  119. /// Set the orientation property value.
  120. /// \sa orientation
  121. void setOrientation(Qt::Orientations orientation);
  122. enum VerticalDirection{
  123. TopToBottom = 1,
  124. BottomToTop = 2
  125. };
  126. /// Return the verticalDirection property value.
  127. /// \sa verticalDirection
  128. VerticalDirection verticalDirection()const;
  129. /// Set the verticalDirection property value.
  130. /// \sa verticalDirection
  131. void setVerticalDirection(VerticalDirection direction);
  132. /// Return the horizontalDirection property value.
  133. /// \sa horizontalDirection
  134. Qt::LayoutDirection horizontalDirection()const;
  135. /// Set the horizontalDirection property value.
  136. /// \sa horizontalDirection
  137. void setHorizontalDirection(Qt::LayoutDirection direction);
  138. public Q_SLOTS:
  139. /// Hide the popup if open or opening. It takes around 300ms
  140. /// for the fading effect to hide the popup.
  141. virtual void hidePopup();
  142. /// Open the popup if closed or closing. It takes around 300ms
  143. /// for the fading effect to open the popup.
  144. virtual void showPopup();
  145. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  146. /// signal.
  147. inline void showPopup(bool show);
  148. Q_SIGNALS:
  149. /// Fired when the popup finished its animation: opening (true) or closing (false).
  150. /// \sa showPopup(), hidePopup()
  151. void popupOpened(bool open);
  152. protected:
  153. explicit ctkBasePopupWidget(ctkBasePopupWidgetPrivate* pimpl, QWidget* parent = 0);
  154. QScopedPointer<ctkBasePopupWidgetPrivate> d_ptr;
  155. Q_PROPERTY(double effectAlpha READ effectAlpha WRITE setEffectAlpha DESIGNABLE false)
  156. Q_PROPERTY(QRect effectGeometry READ effectGeometry WRITE setEffectGeometry DESIGNABLE false)
  157. double effectAlpha()const;
  158. QRect effectGeometry()const;
  159. virtual void setBaseWidget(QWidget* baseWidget);
  160. virtual bool event(QEvent* event);
  161. virtual void paintEvent(QPaintEvent*);
  162. protected Q_SLOTS:
  163. virtual void onEffectFinished();
  164. void setEffectAlpha(double alpha);
  165. void setEffectGeometry(QRect geometry);
  166. void onBaseWidgetDestroyed();
  167. private:
  168. Q_DECLARE_PRIVATE(ctkBasePopupWidget);
  169. Q_DISABLE_COPY(ctkBasePopupWidget);
  170. };
  171. Q_DECLARE_METATYPE(ctkBasePopupWidget::AnimationEffect)
  172. Q_DECLARE_METATYPE(ctkBasePopupWidget::VerticalDirection)
  173. // -------------------------------------------------------------------------
  174. void ctkBasePopupWidget::showPopup(bool show)
  175. {
  176. if (show)
  177. {
  178. this->showPopup();
  179. }
  180. else
  181. {
  182. this->hidePopup();
  183. }
  184. }
  185. #endif