ctkBasePopupWidget.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /// Although a popup widget is a top-level widget, if a parent is
  51. /// passed the popup widget will be deleted when that parent is
  52. /// destroyed (as with any other QObject).
  53. /// ctkBasePopupWidget is a top-level widget (Qt::ToolTip), so
  54. /// even if a parent is passed, the popup will display outside the possible
  55. /// parent layout.
  56. /// \sa baseWidget().
  57. explicit ctkBasePopupWidget(QWidget* parent = 0);
  58. virtual ~ctkBasePopupWidget();
  59. /// Widget the popup is attached to. It opens right under \a baseWidget
  60. /// and if the ctkBasePopupWidget sizepolicy contains the growFlag/shrinkFlag,
  61. /// it tries to resize itself to fit the same width of \a baseWidget.
  62. /// By default, baseWidget is the parent widget.
  63. QWidget* baseWidget()const;
  64. enum AnimationEffect
  65. {
  66. WindowOpacityFadeEffect = 0,
  67. ScrollEffect,
  68. FadeEffect
  69. };
  70. AnimationEffect animationEffect()const;
  71. void setAnimationEffect(AnimationEffect effect);
  72. QEasingCurve::Type easingCurve()const;
  73. void setEasingCurve(QEasingCurve::Type easingCurve);
  74. Qt::Alignment alignment()const;
  75. void setAlignment(Qt::Alignment alignment);
  76. Qt::Orientations orientation()const;
  77. void setOrientation(Qt::Orientations orientation);
  78. enum VerticalDirection{
  79. TopToBottom = 1,
  80. BottomToTop = 2
  81. };
  82. VerticalDirection verticalDirection()const;
  83. void setVerticalDirection(VerticalDirection direction);
  84. Qt::LayoutDirection horizontalDirection()const;
  85. void setHorizontalDirection(Qt::LayoutDirection direction);
  86. public slots:
  87. /// Hide the popup if open or opening. It takes around 300ms
  88. /// for the fading effect to hide the popup.
  89. virtual void hidePopup();
  90. /// Open the popup if closed or closing. It takes around 300ms
  91. /// for the fading effect to open the popup.
  92. virtual void showPopup();
  93. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  94. /// signal.
  95. inline void showPopup(bool show);
  96. signals:
  97. void popupOpened(bool open);
  98. protected:
  99. explicit ctkBasePopupWidget(ctkBasePopupWidgetPrivate* pimpl, QWidget* parent = 0);
  100. QScopedPointer<ctkBasePopupWidgetPrivate> d_ptr;
  101. Q_PROPERTY(double effectAlpha READ effectAlpha WRITE setEffectAlpha DESIGNABLE false)
  102. Q_PROPERTY(QRect effectGeometry READ effectGeometry WRITE setEffectGeometry DESIGNABLE false)
  103. double effectAlpha()const;
  104. QRect effectGeometry()const;
  105. virtual void setBaseWidget(QWidget* baseWidget);
  106. virtual bool event(QEvent* event);
  107. virtual void paintEvent(QPaintEvent*);
  108. protected slots:
  109. virtual void onEffectFinished();
  110. void setEffectAlpha(double alpha);
  111. void setEffectGeometry(QRect geometry);
  112. void onBaseWidgetDestroyed();
  113. private:
  114. Q_DECLARE_PRIVATE(ctkBasePopupWidget);
  115. Q_DISABLE_COPY(ctkBasePopupWidget);
  116. };
  117. Q_DECLARE_METATYPE(ctkBasePopupWidget::AnimationEffect)
  118. Q_DECLARE_METATYPE(ctkBasePopupWidget::VerticalDirection)
  119. // -------------------------------------------------------------------------
  120. void ctkBasePopupWidget::showPopup(bool show)
  121. {
  122. if (show)
  123. {
  124. this->showPopup();
  125. }
  126. else
  127. {
  128. this->hidePopup();
  129. }
  130. }
  131. #endif