ctkBasePopupWidget.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /// Description:
  25. class CTK_WIDGETS_EXPORT ctkBasePopupWidget : public QFrame
  26. {
  27. Q_OBJECT
  28. Q_ENUMS(AnimationEffect)
  29. Q_ENUMS(VerticalDirection)
  30. /// ScrollEffect by default
  31. Q_PROPERTY( AnimationEffect animationEffect READ animationEffect WRITE setAnimationEffect)
  32. /// Effect duration in ms
  33. /// Default to 333ms
  34. Q_PROPERTY( int effectDuration READ effectDuration WRITE setEffectDuration);
  35. /// Opening/Closing curve
  36. /// QEasingCurve::InOutQuad by default
  37. Q_PROPERTY( QEasingCurve::Type easingCurve READ easingCurve WRITE setEasingCurve);
  38. /// Where is the popup in relation to the BaseWidget
  39. /// To vertically justify, use Qt::AlignTop | Qt::AlignBottom
  40. /// Qt::AlignJustify | Qt::AlignBottom by default
  41. Q_PROPERTY( Qt::Alignment alignment READ alignment WRITE setAlignment);
  42. /// Direction of the scrolling effect, can be Qt::Vertical, Qt::Horizontal or
  43. /// both Qt::Vertical|Qt::Horizontal.
  44. /// Vertical by default
  45. Q_PROPERTY( Qt::Orientations orientation READ orientation WRITE setOrientation);
  46. /// Control where the popup opens vertically.
  47. /// TopToBottom by default
  48. Q_PROPERTY( ctkBasePopupWidget::VerticalDirection verticalDirection READ verticalDirection WRITE setVerticalDirection);
  49. /// Control where the popup opens horizontally.
  50. /// LeftToRight by default
  51. Q_PROPERTY( Qt::LayoutDirection horizontalDirection READ horizontalDirection WRITE setHorizontalDirection);
  52. public:
  53. typedef QFrame Superclass;
  54. /// Although a popup widget is a top-level widget, if a parent is
  55. /// passed the popup widget will be deleted when that parent is
  56. /// destroyed (as with any other QObject).
  57. /// ctkBasePopupWidget is a top-level widget (Qt::ToolTip), so
  58. /// even if a parent is passed, the popup will display outside the possible
  59. /// parent layout.
  60. /// \sa baseWidget().
  61. explicit ctkBasePopupWidget(QWidget* parent = 0);
  62. virtual ~ctkBasePopupWidget();
  63. /// Widget the popup is attached to. It opens right under \a baseWidget
  64. /// and if the ctkBasePopupWidget sizepolicy contains the growFlag/shrinkFlag,
  65. /// it tries to resize itself to fit the same width of \a baseWidget.
  66. /// By default, baseWidget is the parent widget.
  67. QWidget* baseWidget()const;
  68. enum AnimationEffect
  69. {
  70. WindowOpacityFadeEffect = 0,
  71. ScrollEffect,
  72. FadeEffect
  73. };
  74. AnimationEffect animationEffect()const;
  75. void setAnimationEffect(AnimationEffect effect);
  76. int effectDuration()const;
  77. void setEffectDuration(int duration);
  78. QEasingCurve::Type easingCurve()const;
  79. void setEasingCurve(QEasingCurve::Type easingCurve);
  80. Qt::Alignment alignment()const;
  81. void setAlignment(Qt::Alignment alignment);
  82. Qt::Orientations orientation()const;
  83. void setOrientation(Qt::Orientations orientation);
  84. enum VerticalDirection{
  85. TopToBottom = 1,
  86. BottomToTop = 2
  87. };
  88. VerticalDirection verticalDirection()const;
  89. void setVerticalDirection(VerticalDirection direction);
  90. Qt::LayoutDirection horizontalDirection()const;
  91. void setHorizontalDirection(Qt::LayoutDirection direction);
  92. public Q_SLOTS:
  93. /// Hide the popup if open or opening. It takes around 300ms
  94. /// for the fading effect to hide the popup.
  95. virtual void hidePopup();
  96. /// Open the popup if closed or closing. It takes around 300ms
  97. /// for the fading effect to open the popup.
  98. virtual void showPopup();
  99. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  100. /// signal.
  101. inline void showPopup(bool show);
  102. Q_SIGNALS:
  103. void popupOpened(bool open);
  104. protected:
  105. explicit ctkBasePopupWidget(ctkBasePopupWidgetPrivate* pimpl, QWidget* parent = 0);
  106. QScopedPointer<ctkBasePopupWidgetPrivate> d_ptr;
  107. Q_PROPERTY(double effectAlpha READ effectAlpha WRITE setEffectAlpha DESIGNABLE false)
  108. Q_PROPERTY(QRect effectGeometry READ effectGeometry WRITE setEffectGeometry DESIGNABLE false)
  109. double effectAlpha()const;
  110. QRect effectGeometry()const;
  111. virtual void setBaseWidget(QWidget* baseWidget);
  112. virtual bool event(QEvent* event);
  113. virtual void paintEvent(QPaintEvent*);
  114. protected Q_SLOTS:
  115. virtual void onEffectFinished();
  116. void setEffectAlpha(double alpha);
  117. void setEffectGeometry(QRect geometry);
  118. void onBaseWidgetDestroyed();
  119. private:
  120. Q_DECLARE_PRIVATE(ctkBasePopupWidget);
  121. Q_DISABLE_COPY(ctkBasePopupWidget);
  122. };
  123. Q_DECLARE_METATYPE(ctkBasePopupWidget::AnimationEffect)
  124. Q_DECLARE_METATYPE(ctkBasePopupWidget::VerticalDirection)
  125. // -------------------------------------------------------------------------
  126. void ctkBasePopupWidget::showPopup(bool show)
  127. {
  128. if (show)
  129. {
  130. this->showPopup();
  131. }
  132. else
  133. {
  134. this->hidePopup();
  135. }
  136. }
  137. #endif