ctkCollapsibleButton.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 __ctkCollapsibleButton_h
  15. #define __ctkCollapsibleButton_h
  16. // Qt includes
  17. #include <QAbstractButton>
  18. #include <QFrame>
  19. // CTK includes
  20. #include <ctkPimpl.h>
  21. #include "ctkWidgetsExport.h"
  22. class ctkCollapsibleButtonPrivate;
  23. class QStyleOptionButton;
  24. /// \ingroup Widgets
  25. /// A collapsible button that shows/hides its children depending on its
  26. /// checked/collapsed property.
  27. /// Warning: &lt;old behavior&gt; As ctkCollapsibleButton forces the Visiblity of its children to
  28. /// true when it get expanded, any child Visibility property is lost. All the widgets
  29. /// will then be visible. To avoid this behavior, use an intermediate widget that
  30. /// contains all the children (they would become grandchildren and their Visibility property
  31. /// will remain relative to their parent, ctkCollapsibleButton's unique child widget.&lt;/old behavior&gt;
  32. /// Note: The user QAbstractButton::icon is not visible (it's placeholder is used to display the
  33. /// collapsible state
  34. class CTK_WIDGETS_EXPORT ctkCollapsibleButton : public QAbstractButton
  35. {
  36. Q_OBJECT
  37. Q_PROPERTY(bool collapsed READ collapsed WRITE setCollapsed NOTIFY contentsCollapsed)
  38. /// This property holds the height in pixels of the contents (excludes the button)
  39. /// when the button is collapsed.
  40. /// 14 pixels by default (same as ctkCollapsibleGroupBox)
  41. Q_PROPERTY(int collapsedHeight READ collapsedHeight WRITE setCollapsedHeight)
  42. /// This property holds whether the button border is raised.
  43. /// This property's default is false. If this property is set, most styles will not pain the button
  44. /// background unless the button is being pressed.
  45. /// \sa isFlat(), setFlat(), QPushButton::flat
  46. Q_PROPERTY(bool flat READ isFlat WRITE setFlat)
  47. Q_PROPERTY(QFrame::Shape contentsFrameShape READ contentsFrameShape WRITE setContentsFrameShape)
  48. Q_PROPERTY(QFrame::Shadow contentsFrameShadow READ contentsFrameShadow WRITE setContentsFrameShadow)
  49. Q_PROPERTY(int contentsLineWidth READ contentsLineWidth WRITE setContentsLineWidth)
  50. Q_PROPERTY(int contentsMidLineWidth READ contentsMidLineWidth WRITE setContentsMidLineWidth)
  51. Q_PROPERTY(Qt::Alignment buttonTextAlignment READ buttonTextAlignment WRITE setButtonTextAlignment)
  52. Q_PROPERTY(Qt::Alignment indicatorAlignment READ indicatorAlignment WRITE setIndicatorAlignment)
  53. public:
  54. ctkCollapsibleButton(QWidget *parent = 0);
  55. ctkCollapsibleButton(const QString& text, QWidget *parent = 0);
  56. virtual ~ctkCollapsibleButton();
  57. ///
  58. /// Property that describes if the widget is collapsed or not.
  59. /// When collapsed, the children are invisible and the widget
  60. /// has a sized defined by CollapsedHeight
  61. void setCollapsed(bool);
  62. bool collapsed()const;
  63. ///
  64. /// \todo resize the widget when setting the new height
  65. void setCollapsedHeight(int heightInPixels);
  66. int collapsedHeight()const;
  67. /// Set the flat property value.
  68. /// \sa flat, isFlat()
  69. void setFlat(bool flat);
  70. /// Return the flat property value.
  71. /// \sa flat, setFlat()
  72. bool isFlat()const;
  73. ///
  74. /// Set the frame shape of the contents
  75. /// Hint: StyledPanel is probably the shape you are looking for
  76. QFrame::Shape contentsFrameShape() const;
  77. void setContentsFrameShape(QFrame::Shape);
  78. ///
  79. /// Set the frame shadow of the contents
  80. /// Hint: Raised is probably the shadow you are looking for
  81. QFrame::Shadow contentsFrameShadow() const;
  82. void setContentsFrameShadow(QFrame::Shadow);
  83. ///
  84. /// Set the line width of the frame around the contents
  85. int contentsLineWidth() const;
  86. void setContentsLineWidth(int);
  87. ///
  88. /// Set the mid line width of the frame around the contents
  89. int contentsMidLineWidth() const;
  90. void setContentsMidLineWidth(int);
  91. ///
  92. /// Set the alignment of the text on the button,
  93. /// Qt::AlignLeft|Qt::AlignVCenter by default.
  94. void setButtonTextAlignment(Qt::Alignment textAlignment);
  95. Qt::Alignment buttonTextAlignment()const;
  96. ///
  97. /// Set the alignment of the indicator (arrow) on the button,
  98. /// Qt::AlignLeft|Qt::AlignVCenter by default.
  99. void setIndicatorAlignment(Qt::Alignment indicatorAlignment);
  100. Qt::Alignment indicatorAlignment()const;
  101. ///
  102. /// Reimplemented for internal reasons
  103. virtual QSize minimumSizeHint()const;
  104. ///
  105. /// Reimplemented for internal reasons
  106. virtual QSize sizeHint()const;
  107. /// Reimplemented to update the size of the button in case of font/style
  108. /// change
  109. virtual bool event(QEvent* event);
  110. /// Reimplemented for internal reasons
  111. /// Catch when a child widget's visibility is externally changed
  112. virtual bool eventFilter(QObject* child, QEvent* e);
  113. /// Reimplemented for internal reasons
  114. /// Don't process Show/Hide events of children when it is
  115. /// ctkCollapsibleButton that generate them.
  116. virtual void setVisible(bool);
  117. Q_SIGNALS:
  118. ///
  119. /// Signal emitted when the widget is collapsed or expanded.
  120. /// See signal toggled(bool) for the opposite.
  121. void contentsCollapsed(bool);
  122. protected Q_SLOTS:
  123. ///
  124. /// Perform the collapse.
  125. virtual void collapse(bool c);
  126. virtual void onToggled(bool clicked = false);
  127. protected:
  128. virtual void paintEvent(QPaintEvent*);
  129. //virtual void mousePressEvent(QMouseEvent* event);
  130. //virtual void mouseReleaseEvent(QMouseEvent* event);
  131. virtual void childEvent(QChildEvent* c);
  132. virtual bool hitButton(const QPoint & pos) const;
  133. /// Compute the size hint of the head button only. The sizehint depends on the text.
  134. virtual QSize buttonSizeHint() const;
  135. /// Initialize option with the values from this ctkCollapsibleButton.
  136. /// This method is useful for subclasses when they need a QStyleOptionButton,
  137. /// but don't want to fill in all the information themselves.
  138. virtual void initStyleOption(QStyleOptionButton* option)const;
  139. protected:
  140. QScopedPointer<ctkCollapsibleButtonPrivate> d_ptr;
  141. private:
  142. Q_DECLARE_PRIVATE(ctkCollapsibleButton);
  143. Q_DISABLE_COPY(ctkCollapsibleButton);
  144. };
  145. #endif