ctkPopupWidget.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // CTK includes
  20. #include "ctkWidgetsExport.h"
  21. class ctkPopupWidgetPrivate;
  22. /// Description:
  23. class CTK_WIDGETS_EXPORT ctkPopupWidget : public QFrame
  24. {
  25. Q_OBJECT
  26. /// Final transparency of the widget (after opacity fading)
  27. /// QStyle::SH_ToolTipLabel_Opacity by default.
  28. Q_PROPERTY( int opacity READ opacity WRITE setOpacity)
  29. /// Control wether the popup automatically opens when the mouse
  30. /// is over the baseWidget and automatically closes when it leaves
  31. /// the widget.
  32. Q_PROPERTY( bool autoHide READ autoHide WRITE setAutoHide)
  33. /// ScrollEffect by default
  34. Q_PROPERTY( AnimationEffect animationEffect READ animationEffect WRITE setAnimationEffect)
  35. /// QEasingCurve::InOutQuad by default
  36. Q_PROPERTY( QEasingCurve::Type easingCurve READ easingCurve WRITE setEasingCurve);
  37. /// To vertically justify, use Qt::AlignTop | Qt::AlignBottom
  38. /// Qt::AlignJustify | Qt::AlignBottom by default
  39. Q_PROPERTY( Qt::Alignment alignment READ alignment WRITE setAlignment);
  40. /// Vertical by default
  41. Q_PROPERTY( Qt::Orientation orientation READ orientation WRITE setOrientation);
  42. /// TopToBottom by default
  43. Q_PROPERTY( ctkPopupWidget::VerticalDirection verticalDirection READ verticalDirection WRITE setVerticalDirection);
  44. /// LeftToRight by default
  45. Q_PROPERTY( Qt::LayoutDirection horizontalDirection READ horizontalDirection WRITE setHorizontalDirection);
  46. public:
  47. typedef QFrame Superclass;
  48. explicit ctkPopupWidget(QWidget* parent = 0);
  49. virtual ~ctkPopupWidget();
  50. /// Widget the popup is attached to. It opens right under \a baseWidget
  51. /// and if the ctkPopupWidget sizepolicy contains the growFlag/shrinkFlag,
  52. /// it tries to resize itself to fit the same width of \a baseWidget.
  53. QWidget* baseWidget()const;
  54. void setBaseWidget(QWidget* baseWidget);
  55. int opacity()const;
  56. void setOpacity(int alpha);
  57. bool autoHide()const;
  58. void setAutoHide(bool);
  59. enum AnimationEffect
  60. {
  61. WindowOpacityFadeEffect = 0,
  62. ScrollEffect,
  63. FadeEffect
  64. };
  65. AnimationEffect animationEffect()const;
  66. void setAnimationEffect(AnimationEffect effect);
  67. QEasingCurve::Type easingCurve()const;
  68. void setEasingCurve(QEasingCurve::Type easingCurve);
  69. Qt::Alignment alignment()const;
  70. void setAlignment(Qt::Alignment alignment);
  71. Qt::Orientation orientation()const;
  72. void setOrientation(Qt::Orientation orientation);
  73. enum VerticalDirection{
  74. TopToBottom = 1,
  75. BottomToTop = 2
  76. };
  77. VerticalDirection verticalDirection()const;
  78. void setVerticalDirection(VerticalDirection direction);
  79. Qt::LayoutDirection horizontalDirection()const;
  80. void setHorizontalDirection(Qt::LayoutDirection direction);
  81. public slots:
  82. /// Hide the popup if open or opening. It takes around 300ms
  83. /// for the fading effect to hide the popup.
  84. void hidePopup();
  85. /// Open the popup if closed or closing. It takes around 300ms
  86. /// for the fading effect to open the popup.
  87. void showPopup();
  88. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  89. /// signal.
  90. inline void showPopup(bool show);
  91. protected slots:
  92. void updatePopup();
  93. //void animatePopup();
  94. void onEffectFinished();
  95. void setWindowAlpha(int alpha);
  96. void setWindowGeometry(QRect geometry);
  97. protected:
  98. QScopedPointer<ctkPopupWidgetPrivate> d_ptr;
  99. Q_PROPERTY(int windowAlpha READ windowAlpha WRITE setWindowAlpha DESIGNABLE false)
  100. Q_PROPERTY(QRect windowGeometry READ windowGeometry WRITE setWindowGeometry DESIGNABLE false)
  101. virtual void paintEvent(QPaintEvent*);
  102. virtual void leaveEvent(QEvent* event);
  103. virtual void enterEvent(QEvent* event);
  104. virtual bool eventFilter(QObject* obj, QEvent* event);
  105. int windowAlpha()const;
  106. QRect windowGeometry()const;
  107. private:
  108. Q_DECLARE_PRIVATE(ctkPopupWidget);
  109. Q_DISABLE_COPY(ctkPopupWidget);
  110. };
  111. void ctkPopupWidget::showPopup(bool show)
  112. {
  113. if (show)
  114. {
  115. this->showPopup();
  116. }
  117. else
  118. {
  119. this->hidePopup();
  120. }
  121. }
  122. #endif