ctkPopupWidget.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <QFrame>
  18. // CTK includes
  19. #include "ctkWidgetsExport.h"
  20. class ctkPopupWidgetPrivate;
  21. /// Description:
  22. class CTK_WIDGETS_EXPORT ctkPopupWidget : public QFrame
  23. {
  24. Q_OBJECT
  25. /// Final transparency of the widget (after opacity fading)
  26. /// QStyle::SH_ToolTipLabel_Opacity by default.
  27. Q_PROPERTY( int opacity READ opacity WRITE setOpacity)
  28. /// Control wether the popup automatically opens when the mouse
  29. /// is over the baseWidget and automatically closes when it leaves
  30. /// the widget.
  31. Q_PROPERTY( bool autoHide READ autoHide WRITE setAutoHide)
  32. public:
  33. typedef QFrame Superclass;
  34. explicit ctkPopupWidget(QWidget* parent = 0);
  35. virtual ~ctkPopupWidget();
  36. /// Widget the popup is attached to. It opens right under \a baseWidget
  37. /// and if the ctkPopupWidget sizepolicy contains the growFlag/shrinkFlag,
  38. /// it tries to resize itself to fit the same width of \a baseWidget.
  39. QWidget* baseWidget()const;
  40. void setBaseWidget(QWidget* baseWidget);
  41. int opacity()const;
  42. void setOpacity(int alpha);
  43. bool autoHide()const;
  44. void setAutoHide(bool);
  45. public slots:
  46. /// Hide the popup if open or opening. It takes around 300ms
  47. /// for the fading effect to hide the popup.
  48. void hidePopup();
  49. /// Open the popup if closed or closing. It takes around 300ms
  50. /// for the fading effect to open the popup.
  51. void showPopup();
  52. /// Show/hide the popup. It can be conveniently linked to a QPushButton
  53. /// signal.
  54. inline void showPopup(bool show);
  55. protected slots:
  56. void updatePopup();
  57. void animatePopup();
  58. protected:
  59. QScopedPointer<ctkPopupWidgetPrivate> d_ptr;
  60. virtual void paintEvent(QPaintEvent*);
  61. virtual void leaveEvent(QEvent* event);
  62. virtual void enterEvent(QEvent* event);
  63. virtual bool eventFilter(QObject* obj, QEvent* event);
  64. private:
  65. Q_DECLARE_PRIVATE(ctkPopupWidget);
  66. Q_DISABLE_COPY(ctkPopupWidget);
  67. };
  68. void ctkPopupWidget::showPopup(bool show)
  69. {
  70. if (show)
  71. {
  72. this->showPopup();
  73. }
  74. else
  75. {
  76. this->hidePopup();
  77. }
  78. }
  79. #endif