ctkPopupWidget.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. void setAutoShow(bool);
  65. bool autoHide()const;
  66. void setAutoHide(bool);
  67. enum AnimationEffect
  68. {
  69. WindowOpacityFadeEffect = 0,
  70. ScrollEffect,
  71. FadeEffect
  72. };
  73. AnimationEffect animationEffect()const;
  74. void setAnimationEffect(AnimationEffect effect);
  75. QEasingCurve::Type easingCurve()const;
  76. void setEasingCurve(QEasingCurve::Type easingCurve);
  77. Qt::Alignment alignment()const;
  78. void setAlignment(Qt::Alignment alignment);
  79. Qt::Orientation orientation()const;
  80. void setOrientation(Qt::Orientation orientation);
  81. enum VerticalDirection{
  82. TopToBottom = 1,
  83. BottomToTop = 2
  84. };
  85. VerticalDirection verticalDirection()const;
  86. void setVerticalDirection(VerticalDirection direction);
  87. Qt::LayoutDirection horizontalDirection()const;
  88. void setHorizontalDirection(Qt::LayoutDirection direction);
  89. public slots:
  90. /// Hide the popup if open or opening. It takes around 300ms
  91. /// for the fading effect to hide the popup.
  92. void hidePopup();
  93. /// Open the popup if closed or closing. It takes around 300ms
  94. /// for the fading effect to open the popup.
  95. void showPopup();
  96. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  97. /// signal.
  98. inline void showPopup(bool show);
  99. protected slots:
  100. void updatePopup();
  101. //void animatePopup();
  102. void onEffectFinished();
  103. void setWindowAlpha(double alpha);
  104. void setWindowGeometry(QRect geometry);
  105. protected:
  106. QScopedPointer<ctkPopupWidgetPrivate> d_ptr;
  107. Q_PROPERTY(double windowAlpha READ windowAlpha WRITE setWindowAlpha DESIGNABLE false)
  108. Q_PROPERTY(QRect windowGeometry READ windowGeometry WRITE setWindowGeometry DESIGNABLE false)
  109. virtual void paintEvent(QPaintEvent*);
  110. virtual void leaveEvent(QEvent* event);
  111. virtual void enterEvent(QEvent* event);
  112. virtual bool eventFilter(QObject* obj, QEvent* event);
  113. double windowAlpha()const;
  114. QRect windowGeometry()const;
  115. private:
  116. Q_DECLARE_PRIVATE(ctkPopupWidget);
  117. Q_DISABLE_COPY(ctkPopupWidget);
  118. };
  119. Q_DECLARE_METATYPE(ctkPopupWidget::AnimationEffect)
  120. Q_DECLARE_METATYPE(ctkPopupWidget::VerticalDirection)
  121. // -------------------------------------------------------------------------
  122. void ctkPopupWidget::showPopup(bool show)
  123. {
  124. if (show)
  125. {
  126. this->showPopup();
  127. }
  128. else
  129. {
  130. this->hidePopup();
  131. }
  132. }
  133. #endif