ctkBasePopupWidget.h 5.5 KB

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