ctkCollapsibleGroupBox.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.apache.org/licenses/LICENSE-2.0.txt
  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 __ctkCollapsibleGroupBox_h
  15. #define __ctkCollapsibleGroupBox_h
  16. // Qt includes
  17. #include <QGroupBox>
  18. // CTK includes
  19. #include "ctkWidgetsExport.h"
  20. class ctkCollapsibleGroupBoxPrivate;
  21. /// \ingroup Widgets
  22. /// A QGroupBox with an arrow indicator that shows/hides the groupbox contents
  23. /// when clicked. It responds to the slot QGroupBox::setChecked(bool) or
  24. /// ctkCollapsibleGroupBox::setCollapsed(bool)
  25. /// When checked is true, the groupbox is expanded
  26. /// When checked is false, the groupbox is collapsed
  27. class CTK_WIDGETS_EXPORT ctkCollapsibleGroupBox : public QGroupBox
  28. {
  29. Q_OBJECT
  30. Q_PROPERTY(bool collapsed READ collapsed WRITE setCollapsed)
  31. /// This property holds the height in pixels of the contents (excludes the title)
  32. /// when the box is collapsed.
  33. /// 14px by default, it is the smallest height that fit Mac Style.
  34. Q_PROPERTY(int collapsedHeight READ collapsedHeight WRITE setCollapsedHeight)
  35. public:
  36. ctkCollapsibleGroupBox(QWidget* parent = 0);
  37. ctkCollapsibleGroupBox(const QString& title, QWidget* parent = 0);
  38. virtual ~ctkCollapsibleGroupBox();
  39. /// Utility function to collapse the groupbox
  40. /// Collapse(close) the group box if collapse is true, expand(open)
  41. /// it otherwise.
  42. /// \sa QGroupBox::setChecked(bool)
  43. inline void setCollapsed(bool collapse);
  44. /// Return the collapse state of the groupbox
  45. /// true if the groupbox is collapsed (closed), false if it is expanded(open)
  46. inline bool collapsed()const;
  47. /// Set the height of the collapsed box. Does not include the title height.
  48. virtual void setCollapsedHeight(int heightInPixels);
  49. int collapsedHeight()const;
  50. /// Reimplemented for internal reasons
  51. /// Catch when a child widget's visibility is externally changed
  52. virtual bool eventFilter(QObject* child, QEvent* e);
  53. /// Reimplemented for internal reasons
  54. virtual void setVisible(bool show);
  55. protected Q_SLOTS:
  56. /// called when the arrow indicator is clicked
  57. /// users can call it programatically by calling setChecked(bool)
  58. virtual void expand(bool expand);
  59. protected:
  60. QScopedPointer<ctkCollapsibleGroupBoxPrivate> d_ptr;
  61. /// reimplemented for internal reasons
  62. virtual void childEvent(QChildEvent*);
  63. #if QT_VERSION < 0x040600
  64. virtual void paintEvent(QPaintEvent*);
  65. virtual void mousePressEvent(QMouseEvent*);
  66. virtual void mouseReleaseEvent(QMouseEvent*);
  67. #endif
  68. private:
  69. Q_DECLARE_PRIVATE(ctkCollapsibleGroupBox);
  70. Q_DISABLE_COPY(ctkCollapsibleGroupBox);
  71. };
  72. //----------------------------------------------------------------------------
  73. bool ctkCollapsibleGroupBox::collapsed()const
  74. {
  75. return !this->isChecked();
  76. }
  77. //----------------------------------------------------------------------------
  78. void ctkCollapsibleGroupBox::setCollapsed(bool collapse)
  79. {
  80. this->setChecked(!collapse);
  81. }
  82. #endif