ctkPopupWidget.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 __ctkPopupWidget_h
  15. #define __ctkPopupWidget_h
  16. // Qt includes
  17. #include <QEasingCurve>
  18. #include <QFrame>
  19. #include <QMetaType>
  20. // CTK includes
  21. #include "ctkWidgetsExport.h"
  22. class ctkPopupWidgetPrivate;
  23. /// Description:
  24. class CTK_WIDGETS_EXPORT ctkPopupWidget : public QFrame
  25. {
  26. Q_OBJECT
  27. Q_ENUMS(AnimationEffect)
  28. Q_ENUMS(VerticalDirection)
  29. /// Control wether the popup automatically opens when the mouse
  30. /// enter the widget. True by default
  31. Q_PROPERTY( bool autoShow READ autoShow WRITE setAutoShow)
  32. /// Control wether the popup automatically closes when the mouse
  33. /// leaves the widget. True by default
  34. Q_PROPERTY( bool autoHide READ autoHide WRITE setAutoHide)
  35. /// ScrollEffect by default
  36. Q_PROPERTY( AnimationEffect animationEffect READ animationEffect WRITE setAnimationEffect)
  37. /// Opening/Closing curve
  38. /// QEasingCurve::InOutQuad by default
  39. Q_PROPERTY( QEasingCurve::Type easingCurve READ easingCurve WRITE setEasingCurve);
  40. /// Where is the popup in relation to the BaseWidget
  41. /// To vertically justify, use Qt::AlignTop | Qt::AlignBottom
  42. /// Qt::AlignJustify | Qt::AlignBottom by default
  43. Q_PROPERTY( Qt::Alignment alignment READ alignment WRITE setAlignment);
  44. /// Direction of the scrolling effect, can be Qt::Vertical, Qt::Horizontal or
  45. /// both Qt::Vertical|Qt::Horizontal.
  46. /// Vertical by default
  47. Q_PROPERTY( Qt::Orientation orientation READ orientation WRITE setOrientation);
  48. /// Control where the popup opens vertically.
  49. /// TopToBottom by default
  50. Q_PROPERTY( ctkPopupWidget::VerticalDirection verticalDirection READ verticalDirection WRITE setVerticalDirection);
  51. /// Control where the popup opens horizontally.
  52. /// LeftToRight by default
  53. Q_PROPERTY( Qt::LayoutDirection horizontalDirection READ horizontalDirection WRITE setHorizontalDirection);
  54. public:
  55. typedef QFrame Superclass;
  56. explicit ctkPopupWidget(QWidget* parent = 0);
  57. virtual ~ctkPopupWidget();
  58. /// Widget the popup is attached to. It opens right under \a baseWidget
  59. /// and if the ctkPopupWidget sizepolicy contains the growFlag/shrinkFlag,
  60. /// it tries to resize itself to fit the same width of \a baseWidget.
  61. QWidget* baseWidget()const;
  62. void setBaseWidget(QWidget* baseWidget);
  63. bool autoShow()const;
  64. /// Calling setAutoShow automatically updates opens the popup if the cursor
  65. /// is above the popup or the base widget.
  66. void setAutoShow(bool);
  67. bool autoHide()const;
  68. /// Don't automatically close the popup when leaving the widget.
  69. /// Calling setAutoHide automatically updates the state close the popup
  70. /// if the mouse is not over the popup nor the base widget.
  71. void setAutoHide(bool autoHide);
  72. enum AnimationEffect
  73. {
  74. WindowOpacityFadeEffect = 0,
  75. ScrollEffect,
  76. FadeEffect
  77. };
  78. AnimationEffect animationEffect()const;
  79. void setAnimationEffect(AnimationEffect effect);
  80. QEasingCurve::Type easingCurve()const;
  81. void setEasingCurve(QEasingCurve::Type easingCurve);
  82. Qt::Alignment alignment()const;
  83. void setAlignment(Qt::Alignment alignment);
  84. Qt::Orientation orientation()const;
  85. void setOrientation(Qt::Orientation orientation);
  86. enum VerticalDirection{
  87. TopToBottom = 1,
  88. BottomToTop = 2
  89. };
  90. VerticalDirection verticalDirection()const;
  91. void setVerticalDirection(VerticalDirection direction);
  92. Qt::LayoutDirection horizontalDirection()const;
  93. void setHorizontalDirection(Qt::LayoutDirection direction);
  94. public slots:
  95. /// Hide the popup if open or opening. It takes around 300ms
  96. /// for the fading effect to hide the popup.
  97. void hidePopup();
  98. /// Open the popup if closed or closing. It takes around 300ms
  99. /// for the fading effect to open the popup.
  100. void showPopup();
  101. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  102. /// signal.
  103. inline void showPopup(bool show);
  104. /// Convenient function that calls setAutoHide(!pin) and opens the popup
  105. /// if pin is true regardless of the value of \a AutoShow.
  106. /// It is typically connected with a checkable button to anchor the popup.
  107. void pinPopup(bool pin);
  108. protected slots:
  109. void updatePopup();
  110. void onEffectFinished();
  111. void setWindowAlpha(double alpha);
  112. void setWindowGeometry(QRect geometry);
  113. protected:
  114. QScopedPointer<ctkPopupWidgetPrivate> d_ptr;
  115. Q_PROPERTY(double windowAlpha READ windowAlpha WRITE setWindowAlpha DESIGNABLE false)
  116. Q_PROPERTY(QRect windowGeometry READ windowGeometry WRITE setWindowGeometry DESIGNABLE false)
  117. virtual void paintEvent(QPaintEvent*);
  118. virtual void leaveEvent(QEvent* event);
  119. virtual void enterEvent(QEvent* event);
  120. virtual bool eventFilter(QObject* obj, QEvent* event);
  121. double windowAlpha()const;
  122. QRect windowGeometry()const;
  123. private:
  124. Q_DECLARE_PRIVATE(ctkPopupWidget);
  125. Q_DISABLE_COPY(ctkPopupWidget);
  126. };
  127. Q_DECLARE_METATYPE(ctkPopupWidget::AnimationEffect)
  128. Q_DECLARE_METATYPE(ctkPopupWidget::VerticalDirection)
  129. // -------------------------------------------------------------------------
  130. void ctkPopupWidget::showPopup(bool show)
  131. {
  132. if (show)
  133. {
  134. this->showPopup();
  135. }
  136. else
  137. {
  138. this->hidePopup();
  139. }
  140. }
  141. #endif