ctkBasePopupWidget.h 4.8 KB

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